Abstract
Large Language Models (LLMs) like BERT and RoBERTa are computationally expensive and memory-intensive, posing challenges for deployment on edge devices. This paper explores post-hoc inference-time optimizations to reduce the footprint of the attention mechanism without the need for expensive retraining. By analyzing the distribution of attention values, the researchers demonstrate that approximately 80% of these values can be pruned and the remainder quantized to 3 bits with minimal impact on model performance. These findings suggest a potential 96% reduction in attention matrix memory usage.
Introduction
The attention mechanism is a core component of Transformer models but also a primary source of computational overhead. While techniques like quantization-aware training can mitigate this, they require significant resources and retraining. This study investigates "post-hoc" optimizations—tweaks applied to pre-trained models at inference time—to determine how much information is truly necessary within the attention mechanism to maintain accuracy.
Methodology
The authors systematically studied the attention values ($\alpha$) produced after the softmax layer in BERT and RoBERTa. They applied two main techniques:
- Threshold Pruning: Attention values below a specific threshold (e.g., $10^{-3}$) are set to zero. This induces sparsity, focusing the model's computation on the most relevant tokens.
- Logarithmic Quantization: Remaining values are mapped to discrete levels. Unlike linear quantization, logarithmic quantization better matches the lognormal-like distribution of attention values, which tend to cluster near zero or one due to the softmax function.
Crucially, pruning acts as an effective preprocessing step for quantization. By removing negligible values first, the available bits can represent the significant values with much higher precision.
Experimental Evaluation
The techniques were evaluated across three distinct NLP tasks to ensure robustness:
- Question Answering (QA): Using BERT and RoBERTa fine-tuned on SQuAD v1.1.
- Sentiment Analysis (SA): Using RoBERTa fine-tuned on the SST-2 dataset.
- Masked Language Modeling (MLM): Evaluating base pre-trained models on Wikipedia subsets.
Analysis of Sparsity and Distribution
The study reveals that attention mechanisms are inherently redundant. Most attention heads are "lazy," with tokens typically attending strongly to fewer than 10 others.
As shown in the figure above, the distribution of attention values and the resulting sparsity vary significantly across layers. Middle layers often exhibit higher sparsity, while the very first and very last layers often require more "dense" attention to capture broader context or task-specific features. The lognormal-like shape of these distributions justifies the use of logarithmic quantization over standard linear methods.
Key Results
The experiments demonstrate that high levels of sparsity can be achieved with minimal performance degradation.
Specifically, for QA and Sentiment Analysis tasks, roughly 80% of attention values could be pruned with less than a 1.0% relative loss in accuracy. Combining this pruning with 3-bit log-quantization resulted in only a 0.8% accuracy reduction on SQuAD for a fine-tuned RoBERTa model. The authors estimate that specialized hardware leveraging these findings could reduce the arithmetic cost of attention by 80% and the memory footprint by up to 96%.
Conclusion and Future Work
This research highlights the extreme redundancy in Transformer attention mechanisms. While current general-purpose hardware (CPUs/GPUs) may not yet fully exploit these sparse, 3-bit representations, the findings provide a roadmap for designing specialized hardware accelerators (FPGAs/ASICs) that can significantly improve the efficiency of LLM inference. Future work could extend these techniques to other model components, such as feed-forward networks (FFNs), which also contribute significantly to the model's overall size.
Summary updated