Beyond KV caching: Shared attention for efficient LLMs

Bingli Liao, Danilo Vasconcellos Vargas

TL;DR

Researchers have introduced Shared Attention (SA), a novel mechanism that reduces the memory and computational footprint of Large Language Models (LLMs) by sharing attention weights across multiple layers. Unlike previous methods that share raw data (Key-Value caches), SA reuses the final attention scores, bypassing expensive softmax operations and significantly shrinking the KV cache with minimal impact on accuracy.

What Problem Does This Paper Address?

As LLMs grow, the "KV cache"—the memory used to store previous keys and values during inference—becomes a massive bottleneck, especially for long sequences. While existing techniques like Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) reduce this cache by sharing heads within a layer, and Cross-Layer Attention (CLA) shares caches between adjacent layers, they all still assume that every layer needs to calculate its own unique attention weights.

The authors identify two main pain points:

  1. Memory: Storing unique K and V matrices for every layer is expensive.
  2. Computation: The $softmax$ operation used to calculate attention weights is computationally intensive and redundant if the attention patterns across layers are similar.

Methods

The core proposal is the Shared Attention (SA) algorithm. The researchers observed that in mature, pre-trained LLMs, the distribution of attention weights becomes "isotropic"—meaning the weights look very similar across many middle layers.

How it works: * The Span: A specific range of layers (e.g., layers 23 to 30) is designated as an "Attention Span." * Calculate Once: The model calculates the attention weights (the result of the $QK^T$ and $softmax$ operations) only for the first layer in that span. * Reuse Many Times: All subsequent layers in that span skip the $QK^T$ and $softmax$ steps entirely, directly applying the pre-computed weights to their own Value ($V$) matrices. * Result: This eliminates the need to store $K$ matrices for the "follower" layers and removes the FLOPs required for their attention weight calculations.

Datasets

The authors evaluated their method across several prominent LLMs, including Llama2-7B, Llama3-8B, Baichuan2-7B, and Qwen2-7B/72B.

To measure performance, they used standard benchmarks:

  • MMLU (General knowledge)
  • GLUE (Language understanding)
  • GSM8k (Mathematical reasoning)
  • HellaSwag (Common sense reasoning)
  • Alpaca/Instruct datasets (Used for fine-tuning to see if models could "re-learn" how to use shared attention).

Key Insights and Ideas

  • The "Group 3" Sweet Spot: The authors categorized LLM layers into four functional groups. They found that Group 3 (the bulk of the middle-to-late layers) shows a high degree of "attention isotropy." This is where Shared Attention is most effective.
  • Self-Organization During Training: By analyzing Baichuan2-7B checkpoints, they found that attention patterns are messy early in training but "stabilize" and become more uniform as the model sees more tokens (around 1.0T tokens).
  • Softmax is the Enemy: By sharing the output of the softmax, the authors bypass one of the most computationally "heavy" parts of the attention mechanism, not just the memory storage.
  • Fine-tuning Recovers Losses: While "plugging in" SA to a pre-trained model causes a slight dip in performance, a small amount of instruction fine-tuning (SFT) allows the model to adapt, often recovering or even exceeding original performance.

Key Results

  • Minimal Accuracy Trade-off: On Llama3-8B, implementing SA across layers 23–30 resulted in a GLUE score of 0.5595, actually outperforming the base model's 0.4804 in that specific configuration.
  • Stability in Reasoning: For Llama2-7B, applying SA to the final layers (27–30) maintained a GSM8k score of 0.1243 (compared to the base 0.1395), showing that the model retains its reasoning capabilities even with shared weights.
  • Efficiency Gains: The method leads to a substantial reduction in the number of floating-point operations (FLOPs) and a significant reduction in the KV cache size, though specific percentage speedups vary by hardware implementation.

Limitations and Future Work

  • Manual Span Selection: Currently, the "span" of layers to share must be determined empirically. The authors suggest that future work could involve automated or dynamic span selection.
  • Training from Scratch: The authors were unable to pre-train a model from scratch with SA due to computational constraints. They hypothesize that "early integration" of SA during pre-training would lead to even better efficiency and performance.
  • Integration with CLA: The paper suggests that combining Shared Attention with Cross-Layer Attention (sharing both weights and KV caches) could provide a "holistic" solution for maximum efficiency.

Summary updated

BibTeX
@article{liao2025beyond,
  author = {Bingli Liao and Danilo Vasconcellos Vargas},
  title = {{Beyond KV caching: Shared attention for efficient LLMs}},
  journal = {Neurocomputing},
  year = {2025},
  doi = {10.1016/j.neucom.2025.130587},
}
Figure