TL;DR
FastGen is a "diagnose-before-compress" framework that slashes the memory footprint of Large Language Model (LLM) inference by up to 40% without requiring any retraining. By profiling attention heads during the initial prompt phase, it adaptively applies tailored eviction policies to each head, preserving model quality while significantly boosting generation speed.
What Problem Does This Paper Address?
LLMs are memory hogs, particularly during long-form text generation. To speed up inference, models use a KV (Key-Value) Cache, which stores the mathematical representations of previous tokens so they don't have to be recomputed. However, this cache grows linearly with the sequence length. For a 65B parameter model, the KV cache can quickly exceed the available GPU memory, forcing researchers to either "offload" data to slower CPU memory or use massive hardware clusters.
Existing compression methods usually apply a "one-size-fits-all" strategy—like keeping only the most recent tokens or tokens with the highest attention scores—across the entire model. The authors argue this is inefficient because different "attention heads" in a Transformer model serve very different purposes.
Methods
The authors introduce FastGen, a dual-phase, plug-and-play algorithm:
- Model Profiling (The Diagnosis): During the "prompt encoding" phase (when the model first reads your input), FastGen observes the attention maps for every head. It looks for patterns: Does this head only look at the last 10 words? Does it only care about the
<s>token? - Adaptive Compression (The Treatment): Based on the profile, FastGen assigns each head an optimal compression policy from a "Feasible Policy Set." These include:
- Special Tokens: Keeping only structural markers (like
<s>or[INST]). - Punctuation: Keeping only tokens like
.,:, or?. - Locality: A sliding window that evicts old tokens.
- Frequency (Heavy Hitters): Keeping tokens that have historically received the most attention.
- Full Cache: Keeping everything (used for heads that attend broadly to the whole context).
FastGen uses a greedy selection process to find the best hybrid policy for each head that recovers at least 95% of the original attention weights with the minimum amount of memory.
Key Insights and Ideas
- Head Diversity is Key: Attention heads are highly specialized. Initial and final layers often need a "Full Cache" to maintain global context, while middle layers are often "sparse," focusing almost exclusively on special tokens or local context.
- Profiling is Stable: One of the most surprising findings is that a head’s behavior during the first few tokens (the prompt) is a remarkably stable predictor of its behavior for the rest of the generation. This allows for "one-shot" profiling with negligible overhead.
- The "Special Token" Anchor: Many heads allocate a massive amount of attention to the very first token (the start-of-sentence token). By ensuring these "anchors" are never evicted, FastGen maintains model stability even with aggressive compression.
Datasets
The researchers evaluated FastGen using the Llama 1 family (7B, 13B, 30B, and 65B parameters). They tested the models on several standard benchmarks:
- GSM8k: Mathematical reasoning.
- HumanEval: Python code generation.
- NQ (Natural Questions) & TQA (TriviaQA): General knowledge and reading comprehension.
- AlpacaEval: A head-to-head comparison using GPT-4 as a judge to evaluate instruction-following quality.
Key Results
- Memory Efficiency: FastGen achieved a ~40% reduction in KV cache size for Llama-65B with negligible loss in generation quality.
- Quality Preservation: In AlpacaEval (win-rate against a full-cache model), FastGen maintained a near-50% win rate (indicating parity) while using significantly less memory than competing fixed-policy methods like H2O or Scissorhands.
- Speedups: Because the GPU has less data to move around, FastGen achieved up to a 55% end-to-end latency reduction compared to the standard Hugging Face implementation for long sequences (16k tokens).
- Low Overhead: The profiling step is incredibly lightweight, accounting for less than 0.35% of the total generation time.
Limitations and Future Work
- Grouped-Query Attention (GQA): The paper focused on the original Llama 1 architecture. Modern models like Llama 2 and 3 use GQA, where multiple query heads share a single KV head. Adapting FastGen to profile these shared heads is a noted area for future work.
- Kernel Optimization: While the authors implemented a custom CUDA kernel to handle the sparse KV cache, they mention that further "polishing" of the sparsity kernels could unlock even greater speedups.
- Synergy with Quantization: The authors suggest that FastGen could be combined with 4-bit or 8-bit KV cache quantization for even more extreme memory savings, though they haven't tested this combination yet.
Summary updated