Predicting Attention Sparsity in Transformers

Marcos V. Treviso, António Góis, Patrick Fernandes, Erick Rocha Fonseca, André F. T. Martins

Introduction

The standard Transformer architecture faces a significant scalability challenge due to its quadratic complexity: memory and computational requirements grow at a squared rate relative to the input sequence length. While various "sparse" Transformers have been proposed to mitigate this, many still necessitate the calculation of a full $O(n^2)$ matrix simply to determine which elements to ignore. This paper introduces Sparsefinder, a novel framework designed to predict the sparsity pattern of $\alpha$-entmax attention in a low-dimensional space, effectively bypassing redundant computations.

Methodology: The Sparsefinder Framework

Sparsefinder treats a pretrained $\alpha$-entmax Transformer as a "teacher" to learn its inherent sparsity patterns. Unlike standard softmax, $\alpha$-entmax naturally produces exact zeros for irrelevant tokens, creating a sparse graph.

Overview of the Sparsefinder framework: (a) extracting the entmax graph, (b) projecting and grouping vectors, and (c) adding local and global patterns.

The framework operates through three primary stages:

  1. Low-Dimensional Projection: High-dimensional query ($q$) and key ($k$) vectors (e.g., $d=64$) are projected into a significantly smaller space (e.g., $r=4$) using a linear layer. This is trained via a contrastive hinge loss to ensure active pairs are proximal in the low-dimensional space.
  2. Prediction Strategies: The authors explore three methods for identifying attention edges:
  3. Distance-based: Connecting pairs within a specific Euclidean distance threshold.
  4. Quantization: Using buckets to group queries and keys.
  5. Clustering: Employing $k$-means to restrict attention to intra-cluster pairs.
  6. Hybrid Pattern Integration: To ensure high recall, learned patterns are augmented with fixed local (sliding window) and global (e.g., [CLS] token) attention patterns.

Evaluation and Datasets

The researchers evaluated Sparsefinder across two core NLP tasks:

  • Machine Translation (MT): Using the IWSLT17 dataset (English to German/French) with a Transformer-Large architecture.
  • Masked Language Modeling (MLM): Finetuning a RoBERTa-base model on WikiText-103. The projection layers were trained on small data subsets, demonstrating the method's data efficiency.

Results and Analysis

Sparsefinder consistently achieves a superior balance between efficiency and accuracy, often residing on the "Pareto frontier" of the sparsity-recall tradeoff.

Recall vs. Sparsity tradeoff comparing Sparsefinder against popular efficient Transformer baselines like BigBird, Longformer, and Reformer.

Key findings include:

  • Performance Dominance: The distance and $k$-means variants of Sparsefinder outperformed established models such as Reformer, Routing Transformer, and BigBird.
  • Scalability: In MT tasks, Sparsefinder maintained high BLEU scores even after pruning over 80% of attention connections.
  • Linguistic Interpretability: Qualitative analysis revealed that the model learns to group tokens by linguistic function, such as linking coreferent pronouns or grouping verbs and nouns.

Conclusion and Future Work

Sparsefinder offers a mathematically grounded approach to attention sparsity, leveraging the properties of $\alpha$-entmax to achieve theoretical efficiency gains of up to 16x. However, realizing these gains in "wall-clock" time currently depends on the availability of optimized GPU kernels for unstructured sparsity. Future research directions include end-to-end training of the sparsity predictor and exploring non-linear projection methods to capture more complex attention dynamics.

Summary updated

BibTeX
@inproceedings{treviso2022predicting,
  author = {Marcos V. Treviso and António Góis and Patrick Fernandes and Erick Rocha Fonseca and André F. T. Martins},
  title = {{Predicting Attention Sparsity in Transformers}},
  booktitle = {SPNLP @ ACL},
  year = {2022},
  doi = {10.18653/v1/2022.spnlp-1.7},
}
Figure