All martial arts in the world are invincible only with speed: speculative decoding
"TLDR: This article explores three major categories of methods for accelerating LLM inference: model-level, computation-level, and decoding process. It provides a detailed introduction to the principles of speculative decoding and its equivalence proof, where a small model generates candidate sequences and a large model validates them, thereby improving inference speed."
According to the scaling law, the more model parameters and data, the better the performance. However, the side effect of more parameters is large GPU memory usage and slow inference speed.
Generally speaking, current LLM acceleration techniques fall into three categories: model-level, computation-level, and decoding process.
Model-Level Acceleration:
- Model pruning: Remove unimportant weights
- Model quantization: High-precision parameters are unnecessary; 8-bit is sufficient
- Model distillation: In specific domains, only part of the LLM's intelligence is needed, not all of it—so teach that part to a small model
Computation-Level Acceleration:
- Model parallelism: Too many parameters, split them across multiple GPUs
- Data parallelism: Too much data, split it across multiple GPUs
- Mixed precision computation: Use high precision where necessary, low precision where not, wrapped in PyTorch's amp decorator
Decoding Process Acceleration:
- Speculative decoding: Use a small, fast model to generate multiple candidate sequences, have the LLM evaluate the most reasonable one, then continue generating
- KV cache: Leverage the computational principles of self-attention to cache previously computed KV products
Speculative Decoding
Speculative decoding seems to draw inspiration from computer architecture—when designing a five-stage pipeline CPU, there's an acceleration technique called branch prediction.
The Speculative Process
- Draft phase: A small model quickly generates a prefix sequence
- Verification phase: The LLM performs inference on this prefix, computes the probability of the draft, and if it meets expectations, samples the next token , yielding the sequence . Specifically:
- For the small model's generated sequence , the LLM computes the probability of each token as , while the small model computes the probability of each token as
- If (the LLM considers this token unreasonable), reject this token with probability , and resample the token from the new probability distribution
Equivalence Proof
The speculative process is listed above—it sounds reasonable, but is it actually valid? Won't it reduce the model's intelligence? Through mathematical proof, we can show that speculative sampling is equivalent to LLM autoregressive sampling.
-
: Probability distribution generated by the LLM
-
: Probability distribution generated by the small model
-
The probability of a token at time t () is
has only two cases:
- The LLM verifies and finds it reasonable, i.e., , and directly accepts the token. Then
- The LLM verifies and finds it unreasonable, i.e., , rejects it and resamples to obtain the token.
- The probability of rejecting all other tokens is
- The probability that the LLM's resampling happens to yield this token:
- Then
Therefore: . I can't continue the proof—my math skills are too weak. Anyway, it roughly belongs to the same distribution as .