Nyströmformer: A Nyström-based Algorithm for Approximating Self-Attention

Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh

Abstract

Transformers have emerged as a powerful tool for a broad range of natural language processing tasks. A key component that drives the impressive performance of Transformers is the self-attention mechanism that encodes the influence or dependence of other tokens on each specific token. While beneficial, the quadratic complexity of self-attention on the input sequence length has limited its application to longer sequences—a topic being actively studied in the community. To address this limitation, we propose Nyströmformer—a model that exhibits favorable scalability as a function of sequence length. Our idea is based on adapting the Nyström method to approximate standard self-attention with $O(n)$ complexity. The scalability of Nyströmformer enables application to longer sequences with thousands of tokens. We perform evaluations on multiple downstream tasks on the GLUE benchmark and IMDB reviews with standard sequence length, and find that our Nyströmformer performs comparably, or in a few cases, even slightly better, than standard self-attention. On longer sequence tasks in the Long Range Arena (LRA) benchmark, Nyströmformer performs favorably relative to other efficient self-attention methods.

Introduction

The "Standard" Transformer architecture relies on the self-attention mechanism, which calculates interactions between every pair of tokens in a sequence. While powerful, this mechanism requires computing an $n \times n$ attention matrix for a sequence of length $n$, leading to quadratic complexity ($O(n^2)$) in both time and memory. This scaling bottleneck makes it computationally prohibitive to process long sequences, such as entire books, legal documents, or high-resolution image data.

To overcome this, researchers have proposed various "efficient" Transformers. However, many existing solutions either sacrifice accuracy or introduce complex clustering algorithms that do not scale well on modern hardware. The Nyströmformer addresses these challenges by leveraging the Nyström method—a classic technique from numerical linear algebra—to approximate the attention matrix with linear $O(n)$ complexity.

Methodology: The Nyström Approximation

The core innovation of Nyströmformer is the application of the Nyström method to the softmax-based self-attention. In standard attention, the output is computed as $\text{Softmax}(QK^T/\sqrt{d_k})V$. The Nyström method approximates a large matrix by using a small subset of its columns and rows, known as "landmarks."

Architecture Overview

As illustrated in the architecture diagram below, the Nyströmformer processes the input $X$ to generate Query ($Q$), Key ($K$), and Value ($V$) matrices. It then employs a three-step reconstruction process to approximate the full attention matrix without ever explicitly computing the $n \times n$ product.

The detailed computational pipeline of the Nyströmformer, showing the use of Segment-means (sMEANS) for landmark selection and the iterative pseudoinverse (pINV) for matrix reconstruction.

Key Components

  1. Landmark Selection (Segment-means): Instead of using all $n$ tokens, the model selects $m$ landmarks ($m \ll n$). While some models use complex K-means clustering, Nyströmformer uses "Segment-means," which simply averages blocks of tokens. This is computationally efficient and provides a strong basis for approximation.
  2. Matrix Reconstruction: The model computes interactions between Queries and Landmark Keys ($n \times m$), Landmark Queries and Landmark Keys ($m \times m$), and Landmark Queries and Keys ($m \times n$).
  3. Iterative Pseudoinverse (pINV): Combining these matrices requires the Moore-Penrose pseudoinverse of the middle $m \times m$ matrix. To maintain efficiency on GPUs, the authors use a third-order iterative method for this calculation, avoiding slow Singular Value Decomposition (SVD).
  4. Local Context Preservation: A skip connection with a 1D depthwise convolution (DConv) is added to the attention output. This helps the model retain local dependencies that might be smoothed over by the low-rank Nyström approximation.

Experimental Results

Efficiency and Scalability

The Nyströmformer achieves true linear scaling. At a sequence length of $n=8192$, it is approximately 12.7x faster and uses 22.8x less memory than a standard Transformer. Compared to other efficient models, it offers a 3x speedup over Longformer and 1.7x memory savings over Linformer.

Approximation Quality

Visual analysis confirms that the Nyström approximation captures the essential patterns of the true self-attention mechanism. The heatmaps below demonstrate that the approximate attention matrix maintains the structural integrity of the original, supporting the hypothesis that self-attention matrices in trained models are often low-rank.

Visual comparison between true self-attention and the Nyström approximate self-attention, demonstrating the high fidelity of the approximation.

Benchmark Performance

  • GLUE Benchmark: On standard NLP tasks, Nyströmformer performs comparably to BERT-base. For instance, it achieved 91.4 on SST-2 compared to BERT's 90.0.
  • Long Range Arena (LRA): On tasks specifically designed to test long-range dependencies (like ListOps and Pathfinder), Nyströmformer outperformed efficient variants like Reformer, Linformer, and Performer by an average of 3.4% in accuracy.

Conclusion

The Nyströmformer provides a robust and efficient solution to the quadratic scaling problem of Transformers. By adapting the Nyström method, it enables the processing of significantly longer sequences without the typical memory and time penalties. Its ability to be initialized with pre-trained BERT weights makes it a practical "plug-and-play" upgrade for existing NLP pipelines. Future work includes adapting this linear-complexity approach for auto-regressive (causal) models like GPT.

Summary updated

BibTeX
@inproceedings{xiong2021nystromformer,
  author = {Yunyang Xiong and Zhanpeng Zeng and Rudrasis Chakraborty and Mingxing Tan and Glenn Fung and Yin Li and Vikas Singh},
  title = {{Nyströmformer: A Nyström-based Algorithm for Approximating Self-Attention}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2021},
  url = {https://openreview.net/forum?id=DxzDmP4FdzgH},
}
Figure