A very timely and nerdy question! "Speculative decoding" is a fascinating optimization technique used in serving large language models (LLMs), so let's dive in!
Background: Autoregressive Decoding in LLMs
Large language models, like transformer-based architectures (e.g., BERT, RoBERTa, GPT-3), generate text autoregressively. This means that when predicting the next token (e.g., word or character) in a sequence, the model conditions on the previous tokens it has generated. In other words:
- The model takes an input prompt (or context) and generates the first token.
- Then, it feeds the generated token back into the model as additional context.
- The model generates the next token, conditioned on the original prompt and the previously generated token(s).
- Steps 2-3 repeat until a termination condition (e.g., max length, EOS token).
This autoregressive process is sequential and token-by-token, which can be slow for long outputs, as each token's generation depends on the previous ones. For instance, generating a 512-token response requires 512 sequential model evaluations.
The Bottleneck: Sequential Compute & Memory Access
The main performance bottlenecks in autoregressive decoding are:
- Sequential computation: Each token generation depends on the previous token, preventing parallelization across the sequence length dimension.
- Memory access patterns: At each step, the model reads the entire previous context (KV cache, more on this later) from memory, which is slow due to memory bandwidth limitations.
Enter Speculative Decoding
Speculative decoding, introduced in the paper "Fast Transformer Decoding: One Write-Head is All You Need" (2019) and later improved in "Speculative Decoding: Accelerating Autoregressive Language Models" (2022), addresses these bottlenecks. The core idea:
Predict multiple tokens at once, then verify them
Instead of generating tokens one-by-one, speculative decoding:
- Predicts a sequence of tokens (e.g., 4-8 tokens) in parallel, using a single forward pass through the model. This is the speculative part.
- Verifies each predicted token sequentially, using the original model, but only if the previous token(s) in the sequence were correctly predicted.
Here's a step-by-step breakdown:
Speculative Decoding Algorithm
- Draft (predict)
K tokens in parallel using a modified forward pass:
- Run the model once, but instead of generating a single token, produce
K probability distributions over the vocabulary (e.g., using K softmax outputs).
- Sample
K tokens from these distributions (or use greedy/top-k sampling).
- This step is speculative, as we're making educated guesses about future tokens.
- Verify the drafted tokens sequentially:
- For each drafted token
i (starting from the first):
- Re-use the KV cache (more on this below) from the previous verified token.
- Run a single model evaluation (forward pass) conditioned on the verified prefix (previous tokens).
- Compare the verified token (from step 2) with the drafted token
i. If they match, proceed to the next drafted token.
- If they don't match, discard the remaining drafted tokens and accept the verified token as the new output. Restart the drafting process from this new token.
- Repeat steps 1-2 until the desired output length is reached.
KV Cache (Key-Value Cache)
A crucial optimization in autoregressive models is the KV cache. It stores intermediate results (keys and values) from the self-attention mechanism, which are reusable across time steps. When generating the next token, the model only needs to compute the new query (based on the last token) and attend to the cached keys and values.
In speculative decoding, the KV cache is shared across the verification steps, reducing the computational overhead.
Benefits of Speculative Decoding
- Speedup: By predicting multiple tokens at once and verifying them efficiently, speculative decoding can achieve 2-5x speedup over traditional autoregressive decoding, depending on the model, sequence length, and drafting size (
K).
- Reduced memory accesses: Verifying tokens sequentially reuses the KV cache, minimizing memory reads.
Challenges & Limitations
- Drafting accuracy: The effectiveness of speculative decoding relies on the model's ability to accurately predict future tokens. Poor drafting accuracy leads to more verification mismatches, reducing the speedup.
- Increased compute (initial draft): Predicting
K tokens at once requires more computation than a single token generation.
- Hyperparameter tuning: Choosing the optimal drafting size (
K) and verification threshold (e.g., when to restart drafting) requires experimentation.
Real-world Adoption
Speculative decoding has garnered interest in the industry, with:
- Hugging Face Transformers (popular library) implementing speculative decoding as an optional optimization.
- NVIDIA and Google researching and deploying variants of speculative decoding in their LLM serving infrastructure.
In summary, speculative decoding accelerates autoregressive language model serving by:
- Predicting multiple tokens in parallel (drafting)
- Verifying them sequentially, reusing the KV cache
This technique strikes a balance between parallelism and sequential verification, mitigating the traditional bottlenecks in LLM decoding.
If you're interested in more details, I recommend checking the original papers and some open-source implementations!
Happy decoding!