Abstract
Large transformer models have shown extraordinary success in achieving state-of-the-art results in many natural language processing applications. However, training and deploying these models can be prohibitively costly for long sequences, as the standard self-attention mechanism of the Transformer uses $O(n^2)$ time and space with respect to sequence length. In this paper, we demonstrate that the self-attention mechanism can be approximated by a low-rank matrix. We further exploit this finding to propose a new self-attention mechanism, which reduces the overall self-attention complexity from $O(n^2)$ to $O(n)$ in both time and space. The resulting linear transformer, the Linformer, performs on par with standard Transformer models, while being much more memory- and time-efficient.
Introduction
The standard Transformer architecture faces a significant scaling challenge due to its self-attention mechanism. In this setup, every token in a sequence attends to every other token, requiring the computation of an $n \times n$ matrix for a sequence of length $n$. This quadratic bottleneck ($O(n^2)$) means that doubling the sequence length quadruples the memory and computation costs, making it difficult to process long documents or high-resolution data. While previous methods like sparse attention or hashing-based models (e.g., Reformer) attempted to address this, they often introduced performance trade-offs or only became efficient at extreme sequence lengths.
Empirical Motivation: The Low-Rank Property
The core insight behind the Linformer is the theoretical and empirical observation that self-attention matrices are low-rank. By performing Singular Value Decomposition (SVD) on pretrained models like RoBERTa, the authors discovered a "long-tail" spectrum distribution.
As shown in the analysis above, most of the information in the attention matrix is concentrated within the first few singular values. This redundancy implies that the full $n \times n$ matrix is not necessary to capture the essential relationships between tokens. Furthermore, the spectrum analysis revealed that higher layers in the Transformer tend to be even "lower rank" than lower layers, suggesting that information becomes more compressed as it flows through the network. This empirical finding is supported by the Johnson-Lindenstrauss Lemma, providing a mathematical basis for approximating self-attention with a low-rank matrix.
The Linformer Architecture
To leverage this low-rank property, the Linformer introduces a new self-attention mechanism. Instead of computing the full attention matrix, it uses linear projection matrices, $E$ and $F$, to project the $n$-dimensional keys ($K$) and values ($V$) into a lower-dimensional space of size $k$ (where $k \ll n$).
The process involves:
- Linear Projection: Multiplying the original $n \times d$ Key and Value matrices by $k \times n$ projection matrices.
- Reduced Attention: Computing an $n \times k$ context mapping matrix instead of the standard $n \times n$ matrix.
- Linear Complexity: This reduces the complexity to $O(nk)$. By choosing $k$ as a constant independent of $n$, the complexity becomes linear ($O(n)$) in both time and space.
The authors also explored parameter-sharing strategies—such as headwise, key-value, and layerwise sharing—to maintain a manageable model size without sacrificing performance.
Experimental Results
The Linformer was evaluated using the RoBERTa pretraining protocol on BookCorpus and English Wikipedia, followed by fine-tuning on the GLUE benchmark and IMDB reviews.
- Performance Parity: Linformer achieved performance nearly identical to (and occasionally exceeding) the standard RoBERTa-base model across various downstream tasks.
- Efficiency Gains: The model demonstrates massive speedups as sequence length increases. For $n=65,536$, the Linformer is 20x faster and uses 60x less memory than a standard Transformer.
- Scalability: Unlike standard Transformers, which exhibit exponential slowdowns with longer sequences, the Linformer's inference time remains relatively flat, enabling applications involving very long sequences.
Conclusion and Future Work
The Linformer successfully breaks the quadratic barrier of self-attention, making Transformers significantly more efficient for long-sequence tasks. Future work includes adapting the architecture for causal masking in autoregressive models (like GPT), automating the selection of the projection dimension $k$, and exploring non-linear projection methods like pooling or convolutions to further enhance compression.
Summary updated