Cross-layer Attention Sharing for Large Language Models

Yongyu Mu, Yuzhang Wu, Yuchun Fan, Chenglong Wang, Hengyu Li, Qiaozhi He, Murun Yang, Tong Xiao, Jingbo Zhu · OpenReview

Abstract: As large language models (LLMs) evolve, the increase in model depth and parameter number leads to substantial redundancy. To enhance the efficiency of the attention mechanism, previous works primarily compress the KV cache or group attention heads, while largely overlooking redundancy between layers. Our comprehensive analyses across various LLMs show that highly similar attention patterns persist within most layers. It's intuitive to save the computation by sharing attention weights across layers. However, further analysis reveals two challenges: (1) Directly sharing the weight matrix without carefully rearranging the attention heads proves to be ineffective; (2) Shallow layers are vulnerable to small deviations in attention weights. Driven by these insights, we introduce LiSA, a lightweight substitute for self-attention in well-trained LLMs. LiSA employs tiny feed-forward networks to align attention heads between adjacent layers and low-rank matrices to approximate differences in layer-wise attention weights. Evaluations encompassing 13 typical benchmarks demonstrate that LiSA maintains high response quality in terms of accuracy and perplexity while reducing redundant attention calculations within 53-84% of the total layers. Our implementations of LiSA achieve a 6X compression of Q and K, with maximum throughput improvements of 19.5% for LLaMA3-8B and 32.3% for LLaMA2-7B.

Introduction

As Large Language Models (LLMs) scale to hundreds of billions of parameters, the self-attention mechanism becomes a massive bottleneck, consuming enormous memory (via the KV cache) and computation time. While researchers have found ways to prune "useless" tokens or group attention heads (like Grouped-Query Attention), they have largely ignored inter-layer redundancy. This paper introduces LiSA (Learnable Sharing Attention), a method that allows adjacent layers to share attention weights by using tiny alignment networks and low-rank "patches," boosting inference throughput by up to 40% with negligible loss in accuracy.

The Redundancy Problem

Standard attention mechanisms compute unique Query ($Q$) and Key ($K$) matrices for every layer to generate specific attention patterns, as illustrated in the standard configuration below.

Standard Attention mechanism where each layer computes its own Q and K matrices.

However, empirical analysis of models like LLaMA reveals that adjacent layers often produce nearly identical attention patterns. This observation suggests that we could calculate the attention once and reuse it for subsequent layers, effectively using an "average" attention map across multiple layers.

The concept of Average Attention, where multiple layers share a common attention pattern to reduce computation.

Despite this intuition, two significant "roadblocks" make direct sharing difficult:

  1. The Permutation Problem: Even if two layers attend to the same tokens, their attention "heads" are often in a different order. Directly sharing weights without alignment is ineffective.
  2. Shallow Layer Sensitivity: Early layers in a transformer are incredibly sensitive. Even a tiny deviation in attention weights in these "diva" layers can cause the model's performance to collapse.

The LiSA Framework

To overcome these challenges, LiSA acts as a lightweight substitute for standard self-attention in pre-trained models. It consists of two primary components:

  • Attention Heads Alignment: Instead of directly copying weights, LiSA uses a tiny Feed-Forward Network (FFN) to act as a "patch panel." This FFN learns how to rearrange and fuse the attention heads from a previous layer so they match the expected input for the current layer.
  • Difference Compensation: To handle the sensitivity of specific layers, LiSA uses low-rank matrices ($Q_{LR}$ and $K_{LR}$) to calculate a small "correction" or delta. This allows the model to reuse the bulk of the shared attention map while still capturing the unique nuances required by the current layer.

LiSA is implemented selectively. The authors found that the very first and very last layers are unique and should remain unshared. For the middle layers, they use a periodic pattern (e.g., two LiSA layers followed by one standard layer) to prevent the model's internal representations from drifting.

Experimental Results

The researchers evaluated LiSA across 13 diverse benchmarks, including PIQA, ARC, MMLU, and GSM8K. For training the LiSA modules (a process called "up-training"), they used a 4.2 billion token subset of the RedPajama-1T dataset.

Key Performance Metrics

  • Massive Compression: LiSA achieves a 6× compression of the $Q$ and $K$ matrices within the attention mechanism for shared layers.
  • Throughput Gains: On an NVIDIA A800 GPU, LiSA improved token-per-second throughput by 19.5% for LLaMA3-8B, 32.3% for LLaMA2-7B, and 40.1% for LLaMA2-13B.
  • Accuracy Retention: The LiSA (17) configuration, which shares over half the layers in LLaMA3-8B, preserved 96.77% of the original model's performance.
  • Efficiency: Implementing LiSA only requires training between 0.46% and 1.64% of the total model parameters, making it significantly cheaper than full fine-tuning.

Conclusion

LiSA demonstrates that inter-layer redundancy is an inherent property of the transformer architecture that can be exploited for significant efficiency gains. By addressing the permutation and sensitivity issues through alignment and low-rank compensation, LiSA provides a path toward more efficient LLM inference without sacrificing reasoning capabilities. Future work may involve automating the search for optimal sharing configurations and extending these techniques to multi-modal architectures.

Summary updated

BibTeX
@misc{mu2024cross,
  author = {Yongyu Mu and Yuzhang Wu and Yuchun Fan and Chenglong Wang and Hengyu Li and Qiaozhi He and Murun Yang and Tong Xiao and Jingbo Zhu},
  title = {{Cross-layer Attention Sharing for Large Language Models}},
  year = {2024},
  eprint = {2408.01890},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.01890},
}

Loading related content...

Figure