SparseBERT: Rethinking the Importance Analysis in Self-attention

Han Shi, Jiahui Gao, Xiaozhe Ren, Hang Xu, Xiaodan Liang, Zhenguo Li, James Tin-Yau Kwok

Contrary to long-standing assumptions in natural language processing, the diagonal elements in a Transformer's self-attention matrix—where a token attends to itself—are actually the least important for model performance. By leveraging this counterintuitive insight and introducing a new differentiable masking algorithm, SparseBERT achieves superior efficiency and accuracy compared to manually designed sparse Transformers like BigBird and Longformer.

The Problem with Human-Designed Sparsity

The self-attention mechanism is the engine of the Transformer, but it comes with a heavy price: quadratic computational complexity $O(n^2)$ relative to sequence length. To handle long documents efficiently, researchers have developed "sparse Transformers" that use predefined masks to limit which tokens can attend to each other.

However, these masks are typically designed based on human intuition. They usually emphasize local neighborhoods, global "relay" tokens, and the diagonal (self-attention).

Examples of existing, human-designed attention masks (e.g., Longformer, BigBird).

The authors of SparseBERT ask a fundamental question: Do we actually know which attention positions are the most important during pre-training? Most previous analyses looked at models after they were trained; this paper investigates importance during the training process itself.

Methods: Finding Optimal Sparsity

To move away from intuition and find the mathematically "optimal" sparsity, the authors employ two main techniques:

  1. Continuous Relaxation: Inspired by Differentiable Architecture Search (DARTS), they assign a learnable parameter to every single position in the $n \times n$ attention matrix. By training these parameters alongside the model, they can visualize which connections the model naturally "values" most.
  2. Differentiable Attention Mask (DAM): To translate this analysis into a practical model, they propose DAM. This module uses a Gumbel-sigmoid function to learn a discrete binary mask in an end-to-end fashion. They introduce two versions:
  3. DAMu (Unstructured): Every position in the attention matrix is independent.
  4. DAMs (Structured): Parameters are shared along diagonals to ensure the resulting mask is hardware-friendly and efficient to implement with custom CUDA kernels.

Key Insights: The "Dark Diagonal"

The most surprising finding from the continuous relaxation analysis is that diagonal elements are the least important. As seen in the visualization below, the diagonal is consistently "dim," meaning the model places very little weight on a token attending to itself.

Visualization of the attention distribution showing the "dark diagonal" where self-attention is minimized.

Why is the diagonal so useless? The authors argue that because Transformers already use residual (skip) connections, the identity information of a token is inherently passed to the next layer. Attending to oneself in the attention matrix is therefore redundant. Furthermore, the authors provide a mathematical proof showing that a Transformer without diagonal attention remains a universal approximator, fully capable of representing any continuous sequence-to-sequence function.

Finally, while humans tend to design symmetric, uniform patterns, the learned masks in SparseBERT are often asymmetric and vary significantly between different attention heads.

Key Results

The models were pre-trained on the standard BooksCorpus and English Wikipedia, then evaluated across a wide battery of NLP benchmarks including GLUE, SQuAD, and SWAG.

  • Outperforming Standard BERT: A BERT-base model with the diagonal elements entirely removed ("no diag-attention") actually performed slightly better than the standard BERT-base on the GLUE average (83.9% vs 83.8% on the development set).
  • Efficiency Gains: SparseBERT (using DAM) consistently outperformed manually designed sparse models. For example, at ~91% sparsity, DAMs achieved an average GLUE score of 80.9%, significantly higher than BigBird (79.4%) or Longformer (80.1%) at similar or even lower sparsity levels.
  • Task-Specific Redundancy: The authors found that different tasks have different "sparsity thresholds." For instance, the CoLA task (linguistic acceptability) remains stable even at very high sparsity, while the RTE task (entailment) requires a denser attention map to maintain accuracy.

Limitations and Future Work

While SparseBERT offers a compelling step forward, it is not without limitations. The process of searching for the mask during pre-training requires additional parameters and computational overhead. Additionally, the current masks are static once trained; future work could explore "dynamic" sparse attention where the mask adapts based on the specific input sentence. Finally, while the structured DAMs is designed for efficiency, fully realizing the speed benefits of these irregular patterns in production requires highly optimized custom CUDA kernels.

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 Tin-Yau 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