Transformer Acceleration with Dynamic Sparse Attention

Yuan Xie, Liu Liu, Zheng Qu, Zhaodong Chen, Yufei Ding, Yuan Xie 0001arXiv 2021

Abstract

Self-attention in Transformers presents a significant computational bottleneck due to its quadratic scaling with sequence length. This paper introduces Dynamic Sparse Attention (DSA), a novel approach that utilizes a lightweight, low-precision predictor to identify and compute only the most salient attention weights on the fly. By leveraging data-dependent sparsity, DSA achieves up to 95% sparsity while maintaining or exceeding baseline accuracy. Our results on the Long-Range Arena benchmark demonstrate that DSA effectively reduces computational overhead and enables significant hardware acceleration through specialized co-design.

Introduction

The quadratic time and memory complexity ($O(N^2)$) of the self-attention mechanism limits the scalability of Transformers for long-sequence tasks. While existing "sparse" attention models like Longformer or BigBird employ static patterns (e.g., sliding windows) to mitigate this, they often fail to capture the inherently dynamic, data-dependent nature of natural attention patterns. This restriction can limit a model's expressive power. The fundamental challenge lies in identifying important attention connections for a specific input without incurring the full cost of computing the complete attention matrix.

Dynamic Sparse Attention (DSA)

To address the limitations of static sparsity, we propose Dynamic Sparse Attention (DSA). This method augments standard attention with a parallel, low-cost prediction path to determine sparsity patterns dynamically.

Comparison of dense computation (a) and the proposed DSA sparse computation (b) featuring a prediction path.

The Prediction Path and Masking

DSA uses a low-rank random projection matrix and low-precision arithmetic (e.g., INT4) to approximate attention scores rapidly. Based on these approximations, the model identifies the top-$k$ connections to create a binary mask. Full-precision computations, including Softmax and Value matrix multiplication, are then restricted to these selected sparse locations.

Optimization and Hardware Co-Design

To maintain accuracy, the model and predictor are jointly fine-tuned using a combined loss function that incorporates standard model loss and the Mean Squared Error of the attention approximation. Furthermore, to achieve actual speedups on standard GPUs, DSA employs structural dynamic sparsity (e.g., vector-wise sparsity). For custom AI accelerators, we propose "compute reordering" to maximize data reuse and minimize memory bandwidth.

Evaluation and Results

We evaluated DSA using the Long-Range Arena (LRA) benchmark, focusing on text classification, document retrieval, and image classification tasks with sequence lengths ranging from 1,024 to 4,000.

Accuracy of DSA across different sparsity levels (90% to 99%) compared to the dense baseline for Text, Retrieval, and Image tasks.

Performance and Efficiency

DSA maintains baseline accuracy even at 90% to 95% sparsity. On the LRA benchmark, DSA-90% achieved an average score of 57.48, surpassing the dense baseline (56.79) and other efficient variants like Linformer and BigBird. Computationally, DSA reduces Multiply-Accumulate (MAC) operations by 2.79x to 4.35x. On an NVIDIA V100 GPU, 1x8 vector-wise sparsity provides a 1.94x speedup for Sparse Matrix-Matrix Multiplication (SpMM) with negligible accuracy loss.

Conclusion

Dynamic Sparse Attention demonstrates that high-precision computation is not required to route attention effectively. By predicting important connections in low precision, DSA significantly reduces the overhead of self-attention. While fine-grained sparsity remains challenging for general-purpose GPUs, the proposed hardware-software co-design strategies, including structural sparsity and compute reordering, pave the way for highly efficient Transformer accelerators.

Summary updated

BibTeX
@misc{xie2021transformer,
  author = {Yuan Xie and Liu Liu and Zheng Qu and Zhaodong Chen and Yufei Ding and Yuan Xie},
  title = {{Transformer Acceleration with Dynamic Sparse Attention}},
  year = {2021},
  eprint = {2110.11299},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.11299},
}
Figure