SparseBERT: Rethinking the Importance Analysis in Self-attention

Han Shi, Jiahui Gao, Xiaozhe Ren, Hang Xu, Xiaodan Liang, Zhenguo Li, James T. Kwok

Abstract

Transformer-based models are popularly used in natural language processing (NLP). Its core component, self-attention, has aroused widespread interest. To understand the self-attention mechanism, a direct method is to visualize the attention map of a pre-trained model. Based on the patterns observed, a series of efficient Transformers with different sparse attention masks have been proposed. From a theoretical perspective, universal approximability of Transformer-based models is also recently proved. However, the above understanding and analysis of self-attention is based on a pre-trained model. To rethink the importance analysis in self-attention, we study the significance of different positions in attention matrix during pre-training. A surprising result is that diagonal elements in the attention map are the least important compared with other attention positions. We provide a proof showing that these diagonal elements can indeed be removed without deteriorating model performance. Furthermore, we propose a Differentiable Attention Mask (DAM) algorithm, which further guides the design of the SparseBERT. Extensive experiments verify our interesting findings and illustrate the effect of the proposed algorithm.

Introduction: The Quadratic Bottleneck

The standard Transformer architecture has revolutionized NLP but suffers from a significant "quadratic bottleneck." As the input sequence length ($n$) increases, the computational and memory costs of self-attention grow by $O(n^2)$. To mitigate this, researchers have developed various "Sparse Transformers" that employ predefined, manually designed masks to limit token interactions.

Common manually designed sparse attention patterns

As shown in Figure 1, patterns like Star, Strided, and BigBird are typically based on human intuition—such as the assumption that local neighborhoods and global tokens are most important. However, these masks are often static and may not reflect the actual importance of attention positions learned during training. This paper re-evaluates these assumptions, specifically questioning the necessity of the diagonal elements where a token attends to itself.

Methods: Differentiable Attention Masking

The authors propose a three-pronged approach to identify and exploit attention importance:

  1. Importance Analysis via Continuous Relaxation: Inspired by Neural Architecture Search (DARTS), learnable weights are assigned to every position in the attention matrix. During pre-training, the model naturally learns to weight the most useful connections.
  2. Differentiable Attention Mask (DAM): To automate mask design, the DAM algorithm uses the Gumbel-sigmoid trick to learn binary masks (0 or 1) end-to-end.
  3. $DAM_u$ (Unstructured): Learns unique masks for every position, providing maximum flexibility.
  4. $DAM_s$ (Structured): Constrains parameters along diagonals to ensure hardware-friendly patterns compatible with efficient CUDA kernels.
  5. Theoretical Foundation: The authors provide a mathematical proof demonstrating that Transformers remain "universal approximators" even when diagonal attention is entirely removed, provided residual connections are maintained.

Key Insights: The Redundancy of Self-Attention

The most striking discovery is that diagonal elements are the least important positions in the attention matrix. While traditional intuition suggests self-attention is vital, the authors hypothesize that residual (skip) connections already effectively pass a token's own information forward, making diagonal attention redundant.

Learned attention distribution and resulting mask sketch

Figure 4 illustrates this finding: the learned importance heatmap (a) shows high significance for local neighborhoods and special tokens (like [CLS]), but the resulting mask sketch (b) clearly leaves the main diagonal empty. This suggests that the attention mechanism is better utilized for capturing relationships between different tokens rather than self-reflection.

Experimental Results

SparseBERT was evaluated against standard BERT and other sparse variants on major benchmarks:

  • GLUE Benchmark: A BERT-base model with diagonal attention removed actually achieved a slightly higher average score (83.9%) than the standard BERT-base (83.8%). SparseBERT ($DAM_s$) achieved an 80.9% average score even at 91.3% sparsity, outperforming BigBird (79.4%) and Longformer (80.1%) at similar or lower sparsity levels.
  • SQuAD & SWAG: On SQuAD v1.1, SparseBERT maintained an F1 score of 87.9, nearly matching the full BERT-base (88.5) despite the massive reduction in active connections.
  • Task-Specific Sparsity: The research found that redundancy varies by task. Linguistic tasks like CoLA are highly robust to sparsity, while entailment tasks like RTE require denser attention maps to maintain accuracy.

Conclusion

SparseBERT challenges the conventional wisdom of Transformer design by proving that diagonal attention is largely redundant. By replacing manual intuition with the Differentiable Attention Mask (DAM) algorithm, the authors demonstrate that models can achieve extreme efficiency without sacrificing performance. Future work will explore whether these findings extend to other domains, such as Vision Transformers (ViT).

Summary updated

BibTeX
@inproceedings{shi2021sparsebert,
  author = {Han Shi and Jiahui Gao and Xiaozhe Ren and Hang Xu and Xiaodan Liang and Zhenguo Li and James T. Kwok},
  title = {{SparseBERT: Rethinking the Importance Analysis in Self-attention}},
  booktitle = {International Conference on Machine Learning},
  year = {2021},
  url = {https://openreview.net/forum?id=V13zeyQ1Sg},
}
Figure