Sparsifiner: Learning Sparse Instance-Dependent Attention for Efficient Vision Transformers

Cong Wei, Brendan Duke, Ruowei Jiang, Parham Aarabi, Graham W. Taylor, Florian ShkurtiCVPR 2023

Abstract

Vision Transformers (ViT) have shown their competitive advantages performance-wise compared to convolutional neural networks (CNNs) though they often come with high computational costs. To this end, previous methods explore different attention patterns by limiting a fixed number of spatially nearby tokens to accelerate the ViT's multi-head self-attention (MHSA) operations. However, such structured attention patterns limit the token-to-token connections to their spatial relevance, which disregards learned semantic connections from a full attention mask. In this work, we propose a novel approach to learn instance-dependent attention patterns, by devising a lightweight connectivity predictor module to estimate the connectivity score of each pair of tokens. Intuitively, two tokens have high connectivity scores if the features are considered relevant either spatially or semantically. As each token only attends to a small number of other tokens, the binarized connectivity masks are often very sparse by nature and therefore provide the opportunity to accelerate the network via sparse computations. Equipped with the learned unstructured attention pattern, sparse attention ViT (Sparsifiner) produces a superior Pareto-optimal trade-off between FLOPs and top-1 accuracy on ImageNet compared to token sparsity. Our method reduces 48% to 69% FLOPs of MHSA while the accuracy drop is within 0.4%. We also show that combining attention and token sparsity reduces ViT FLOPs by over 60%.

Introduction

Vision Transformers (ViTs) have revolutionized computer vision, but their widespread deployment is hindered by a significant "computational tax." The core of the ViT architecture, the Multi-Head Self-Attention (MHSA) mechanism, possesses quadratic complexity ($O(N^2)$). This means that as the number of image patches (tokens) increases—essential for high-resolution images or long video sequences—the computational requirements grow fourfold.

Existing efficiency solutions generally fall into two categories: token pruning and fixed sparse attention. Token pruning removes "unimportant" patches entirely, but aggressive pruning often leads to irreversible information loss and accuracy drops. Fixed sparse attention, such as the window-based approach in Swin Transformers, restricts tokens to local neighborhoods. While efficient, this rigid structure ignores the long-range semantic relationships that make Transformers powerful. Sparsifiner addresses these limitations by allowing the model to dynamically learn which token-to-token connections are worth maintaining for every specific input image.

The Sparsifiner Approach

Sparsifiner introduces a module that replaces standard MHSA with an instance-dependent, unstructured sparse version. The goal is to identify and compute only the most critical attention scores.

1. The Connectivity Mask Predictor

To avoid the overhead of computing a full $N \times N$ attention matrix only to discard entries, Sparsifiner utilizes a lightweight predictor to estimate connectivity:

  • Low-Rank Approximation: The module first projects keys into a significantly lower dimension to generate a coarse, computationally inexpensive attention map.
  • Up-Projection: This coarse map is projected back to the original size using a learned sparse basis ($W^{up}$).
  • Top-k Selection: The model selects the top-$k$ most relevant connections to generate a binary mask. This mask dictates which attention scores will be computed in the full-rank space.

2. Sparse MHSA

With the mask generated, the model performs a "sparse-dense" product. It calculates attention scores only for the connections identified by the mask. Because these masks are highly sparse by nature, the computational cost of the MHSA operation is dramatically reduced without sacrificing the ability to capture long-range dependencies.

3. Training via Knowledge Distillation

Training sparse models from scratch can be unstable. Sparsifiner employs a Knowledge Distillation (KD) framework where a standard, pre-trained ViT serves as a "teacher." The Sparsifiner "student" is trained to mimic the teacher’s output tokens, class predictions, and attention maps, ensuring the sparse model retains the representational power of the dense baseline.

Experimental Results

The authors evaluated Sparsifiner on ImageNet-1k using DeiT-S and LV-ViT-S architectures. The results demonstrate a superior Pareto-optimal trade-off between efficiency and accuracy.

  • Efficiency Gains: On DeiT-S, Sparsifiner reduced MHSA FLOPs by 68% while maintaining a top-1 accuracy of 79.5%, matching the dense baseline. Overall, the method reduces MHSA FLOPs by 48% to 69% with less than a 0.4% drop in accuracy.
  • Comparison to Baselines: Sparsifiner outperformed existing low-rank methods like Linformer, achieving a 2.1% absolute improvement in accuracy at similar computational costs. By using the low-rank map as a guide rather than a replacement, Sparsifiner preserves the "long tail" of the attention spectrum.
  • High-Resolution Scaling: When tested on $384 \times 384$ images, Sparsifiner reduced MHSA complexity by over 84%. This suggests the method is particularly effective for high-resolution tasks where quadratic complexity is most punishing.
  • Synergy with Token Pruning: Sparsifiner is complementary to token pruning. While pruning removes entire rows/columns, Sparsifiner thins the remaining entries. Combining both techniques reduced total ViT FLOPs by over 60%.

Discussion and Insights

Visualizations of the learned masks reveal that Sparsifiner adapts its focus based on the layer depth. In early layers, the model focuses on local textures and edges. In deeper layers, it automatically shifts to long-range semantic connections, such as linking disparate parts of an object. This unstructured approach allows for a more flexible and "natural" attention pattern than fixed-window methods.

Furthermore, at certain sparsity levels, Sparsifiner was found to slightly outperform the full-attention baseline. This suggests that pruning noisy or irrelevant connections can act as a form of regularization, helping the model focus on the most salient features of the input.

Conclusion and Future Work

Sparsifiner provides a robust framework for making Vision Transformers leaner by learning instance-dependent attention. While the theoretical FLOP reductions are significant, the authors note that realizing these gains in "wall-clock" time requires specialized hardware or software kernels optimized for unstructured sparsity. Future research may extend Sparsifiner to other high-token modalities, such as video processing and 3D point clouds, where the need for efficient attention is even more critical.

Summary updated

BibTeX
@inproceedings{wei2023sparsifiner,
  author = {Cong Wei and Brendan Duke and Ruowei Jiang and Parham Aarabi and Graham W. Taylor and Florian Shkurti},
  title = {{Sparsifiner: Learning Sparse Instance-Dependent Attention for Efficient Vision Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2023},
  url = {https://openreview.net/forum?id=WSqfX7NX0i},
}
Figure