TL;DR
Keyformer is a novel inference-time optimization that significantly reduces the memory footprint of Large Language Models (LLMs) by pruning the Key-Value (KV) cache. By using a Gumbel-softmax-based score function to identify and retain only the most critical "key" tokens, it achieves up to a 2.1x reduction in latency and a 2.4x boost in throughput while maintaining over 99% of the original model's accuracy.
What Problem Does This Paper Address?
Generative LLMs are notoriously memory-hungry. During inference, they rely on a KV cache to store previously computed hidden states, avoiding redundant calculations. However, as input sequences get longer, this cache grows linearly. For long-context applications like document summarization or extended conversations, the KV cache can actually become larger than the model weights themselves, creating a massive memory bandwidth bottleneck.
Existing solutions often fall short: "Window Attention" discards old tokens and loses vital context, while other pruning methods often require expensive model retraining or fine-tuning. The challenge is to reduce the cache size dynamically during inference without breaking the model’s ability to "remember" important information from the distant past.
Methods
Keyformer is based on the observation that attention is naturally sparse—roughly 90% of attention weight is concentrated on a small subset of "key" tokens. To exploit this, the authors introduce several innovations:
- Gumbel-based Score Function: Identifying key tokens is tricky because discarding tokens shifts the probability distribution of the remaining ones (a side effect of the softmax function). Keyformer adds Gumbel noise to the unnormalized attention logits. This regularization makes the selection process more robust to the "unseen" context of discarded tokens.
- Dynamic Temperature Scaling: The authors use a temperature parameter ($\tau$) that increases as the generation progresses. This systematically increases the randomness in the score function to compensate for the increasing number of discarded tokens, ensuring the model doesn't over-fixate on a few early tokens.
- Mixed Attention Strategy: Instead of just keeping a window of recent tokens, Keyformer maintains a hybrid cache: a small window of the most recent tokens plus a set of globally important "key" tokens identified by the score function.
- Per-Layer Selection: Rather than a global pruning strategy, Keyformer applies a dedicated score function to each decoder layer, acknowledging that different layers capture different levels of semantic abstraction.
Datasets
The authors evaluated Keyformer across several foundational models (GPT-J, Cerebras-GPT, and MPT) using:
- Summarization: CNN/DailyMail and GovReport (specifically for long-context evaluation).
- Conversation: SODA.
- Few-Shot Reasoning: PIQA, Winogrande, OpenBookQA, and COPA (using the
lm-eval-harnessframework).
Key Insights and Ideas
- The Softmax Shift: A major "aha" moment in the paper is the realization that simply picking tokens with high attention scores (like the H2O method) is insufficient. When you remove tokens, the denominator of the softmax changes, which can lead to an uneven and inaccurate score distribution for the remaining tokens.
- Initial Token Bias: The paper leverages the "attention sink" phenomenon—the tendency of LLMs to attend heavily to the very first tokens in a sequence—but improves upon it by allowing the model to dynamically choose which early tokens are actually important rather than just hard-coding the first four.
- Inference-Only Deployment: Because Keyformer works entirely at inference time without retraining, it is highly practical for deploying existing pre-trained models on hardware with limited memory.
Key Results
- Efficiency: Keyformer reduces KV cache data movement by 2.9x and improves computational efficiency in the attention module by 1.3x.
- Speed: On an NVIDIA A100 GPU, it delivered a 2.1x speedup in inference latency and a 2.4x increase in token generation throughput.
- Accuracy: Even with a 50% reduction in KV cache size, Keyformer maintained accuracy within the strict 99%–99.9% threshold required by industry benchmarks like MLPerf. In long-context tasks (GovReport), it significantly outperformed previous state-of-the-art methods like H2O.
Limitations and Future Work
While Keyformer is a powerful inference-time tool, the authors note that the quadratic complexity of Transformers still exists during the initial prompt processing phase.
Future work includes:
- Integrating Keyformer-based sparse softmax directly into the training phase to address computational complexity from the start.
- Evaluating the method on models that already use Multi-Query Attention (MQA) or Group-Query Attention (GQA).
- Exploring how this pruning interacts with other compression techniques like quantization.
Summary updated