Breaking BERT: Evaluating and Optimizing Sparsified Attention

Siddhartha Brahma, Polina Zablotskaia, David MimnoarXiv 2022

Abstract: Transformers allow attention between all pairs of tokens, but there is reason to believe that most of these connections - and their quadratic time and memory - may not be necessary. But which ones? We evaluate the impact of sparsification patterns with a series of ablation experiments. First, we compare masks based on syntax, lexical similarity, and token position to random connections, and measure which patterns reduce performance the least. We find that on three common finetuning tasks even using attention that is at least 78% sparse can have little effect on performance if applied at later transformer layers, but that applying sparsity throughout the network reduces performance significantly. Second, we vary the degree of sparsity for three patterns supported by previous work, and find that connections to neighbouring tokens are the most significant. Finally, we treat sparsity as an optimizable parameter, and present an algorithm to learn degrees of neighboring connections that gives a fine-grained control over the accuracy-sparsity trade-off while approaching the performance of existing methods.

Introduction

Transformers have revolutionized NLP, but they come with a heavy "quadratic tax"—the cost of every token attending to every other token. Standard Transformers use "dense" attention, meaning for a sequence of length $N$, the model performs $N^2$ operations. This is computationally expensive and often unnecessary; many attention heads naturally learn simple patterns, like looking at the very next token or the first token in a sentence. In "Breaking BERT," researchers from Google Research systematically dismantle BERT’s attention mechanism to find the "load-bearing" connections and propose a way to learn optimal sparsity patterns on the fly.

Methods

The authors conducted three main experiments to evaluate the impact of sparsification. First, they replaced dense attention with four specific graph-based masks: Syntax Tree (dependency parsing), Word Similarity (GloVe embeddings), Neighbor (immediate physical proximity), and Random. The masking process involves element-wise multiplication of the attention weights by a binary mask.

The process of applying a binary mask to a dense attention map to produce sparsified attention.

Second, they tested Layer-wise Sensitivity using "Top-Down" vs. "Bottom-Up" sparsification. Finally, they introduced a Learned Sparsity algorithm. Inspired by Low-Density Parity-Check (LDPC) codes, this method treats connectivity as an optimizable parameter ($\delta$), allowing the model to decide the reach of each attention head while optimizing for a specific sparsity-accuracy trade-off.

Key Insights and Results

The researchers evaluated their methods on three tasks from the GLUE benchmark: SST-2 (sentiment), CoLA (grammar), and MNLI (inference).

The "Neighbor" Supremacy

Surprisingly, simple local neighbor connections were the most robust. Patterns based on complex syntax or semantic similarity actually damaged performance more than just looking at the words next door. This suggests that BERT's power isn't necessarily in its ability to "parse" grammar, but in its ability to build representations from local context.

The "Neighbor" mask pattern, which restricts attention to immediate surrounding tokens.

The Hierarchy of Density

Early layers are "load-bearing." Applying sparsity to the first few layers of BERT causes a massive drop in performance. However, the final layers are incredibly resilient—you can sparsify them by over 78% with almost no impact on accuracy. This suggests that the "heavy lifting" of feature extraction is finished well before the final layer.

Performance Trade-offs

On the SST-2 task, the "Learned Sparsity" model maintained an accuracy of >0.86 even at extreme sparsity levels where fixed-pattern models dropped below 0.82. However, MNLI (the most complex task) became unstable when sparsity was applied to more than a few layers, confirming that complex reasoning still requires more dense interaction.

Conclusion

This study demonstrates that BERT's attention can be significantly sparsified without catastrophic loss in performance, provided the right patterns and layers are targeted. Local neighbor connections are surprisingly effective, and learned sparsity offers a promising path toward more efficient Transformers. While hardware implementation remains a challenge for achieving wall-clock speedups, these findings suggest that "born-sparse" models could be a viable direction for future research.

Summary updated

BibTeX
@misc{brahma2022breaking,
  author = {Siddhartha Brahma and Polina Zablotskaia and David Mimno},
  title = {{Breaking BERT: Evaluating and Optimizing Sparsified Attention}},
  year = {2022},
  eprint = {2210.03841},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2210.03841},
}
Figure