Introduction
In the world of Large Language Models (LLMs), generating text autoregressively is a memory-intensive process. To avoid recomputing hidden states for every new word, models utilize a Key-Value (KV) cache, which stores the "keys" and "values" of all previously processed tokens.
However, this cache grows linearly with both sequence length and batch size. For applications requiring long contexts (like analyzing entire books) or high throughput (serving many users simultaneously), the KV cache can become so massive that it exceeds available GPU memory. This bottleneck forces engineers to use slow offloading techniques or limit the model's capabilities. While existing methods like Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) successfully share KVs across different attention heads, this paper explores a novel dimension of efficiency: Can we share KVs across different layers?
By introducing Cross-Layer Attention (CLA), researchers achieved a 2× reduction in KV cache size with minimal impact on model accuracy, effectively pushing the Pareto frontier for efficient LLM inference.
Methods: Cross-Layer Attention (CLA)
The authors introduce Cross-Layer Attention (CLA) as an architectural modification designed to work alongside existing optimizations.
In a traditional Transformer, every layer has its own distinct set of Key (K) and Value (V) projections. In a CLA-equipped model, only a subset of layers computes these projections, and subsequent layers reuse them.
Key features of this approach include:
- Sharing Factor: The authors define a "sharing factor" (e.g., CLA2, CLA3). In CLA2, every pair of adjacent layers shares a single set of KV activations. Layer 1 computes the KVs, and Layer 2 simply reuses them.
- Orthogonality: CLA can be seamlessly combined with MQA (one KV head for all query heads) or GQA. While MQA reduces the "width" of the cache (heads), CLA reduces the "depth" (layers).
- Architectural Minimalism: Aside from skipping the KV projection in alternating layers, the rest of the Transformer block (Query projections, Feed-Forward Networks, Norms) remains entirely unchanged.
Experimental Setup
The researchers evaluated CLA by training models from scratch at the 1-billion (1B) and 3-billion (3B) parameter scales. Models were pretrained on the SlimPajama dataset, utilizing 30B tokens for the 1B models and 100B tokens for the 3B models.
Accuracy was measured using Wikitext perplexity alongside a suite of downstream zero-shot benchmarks, including Hellaswag, PIQA, WinoGrande, SciQ, OpenBookQA, BoolQ, and ARC-Easy.
Key Insights and Results
The success of CLA suggests a fascinating property of LLMs: the semantic information captured in the "Keys" and "Values" does not change drastically between adjacent layers. The model can effectively attend to the same "memory" even if that memory was generated one layer prior.
- 2× Memory Savings: At the 1B scale, CLA2 reduced the KV cache size by 50% compared to an MQA baseline, while only increasing validation perplexity by a negligible 0.04 to 0.06 points.
- Pareto Improvement: CLA represents a more efficient way to utilize a strict memory budget. To match the memory efficiency of CLA2 without using it, one would have to halve the head dimension. Doing so resulted in a much higher perplexity (13.81) compared to the CLA2 model (13.60).
- 3B Scale Success: The benefits held steady as the model scaled. In some 3B-parameter experiments, the CLA2 model actually achieved better perplexity than the MQA baseline when learning rates were optimally tuned.
- Downstream Parity: On zero-shot benchmarks (like Hellaswag), CLA models performed within 1–5 percentage points of their much more memory-hungry counterparts.
- The CLA2 Sweet Spot: Sharing across two layers (CLA2) provided the optimal balance. While CLA3 and CLA4 save even more memory, the accuracy trade-off becomes significantly steeper.
Limitations and Future Work
While CLA is a powerful tool for memory reduction, it has limitations:
- Core Latency: CLA reduces the memory footprint, but it doesn't necessarily reduce the time it takes to compute attention. Each layer still needs to read the shared KV cache from memory, meaning memory bandwidth requirements during the core attention step remain similar.
- Pipeline Parallelism: If a model is split across multiple GPUs, sharing KVs across layers that reside on different chips could introduce communication overhead.
- Extreme Contexts: Experiments focused on sequences of 2048 tokens. While the math suggests CLA will be highly beneficial for 100k+ token contexts, empirical testing at those extreme lengths is left for future work.
- Adaptation vs. Scratch: The authors demonstrated that a standard model can be "uptrained" into a CLA model, but the performance did not fully recover to the level of a model trained with CLA from scratch.
Summary updated