TL;DR
This paper introduces the Linear Transformer, a model that swaps out the standard quadratic-complexity softmax attention for a kernel-based linear attention mechanism. By leveraging the associative property of matrix multiplication, the authors show that Transformers can be mathematically reformulated as Recurrent Neural Networks (RNNs). This breakthrough enables training that scales linearly with sequence length and autoregressive inference that is up to 4,000x faster.
The Quadratic Bottleneck
The "Achilles' heel" of the standard Transformer architecture is its self-attention mechanism. Specifically, softmax attention requires computing a similarity matrix between every pair of tokens in a sequence. If a sequence has $N$ tokens, this matrix is $N \times N$, leading to $O(N^2)$ time and memory complexity.
While $O(N^2)$ is manageable for short snippets of text, it becomes a brick wall for long-form content like high-resolution images, long documents, or extended audio files. Previous attempts to fix this (like Sparse Transformers or the Reformer) reduced training complexity but often failed to speed up autoregressive inference—the step-by-step process of generating new data where the model must look back at every previous token for every new one it generates.
Methods: Linearizing Attention
The authors propose a "Linearized" attention mechanism to bypass this bottleneck. Here is the technical breakdown:
- Generalized Attention: They redefine the attention function to use a kernel-based similarity: $\text{sim}(Q, K) = \phi(Q)^T \phi(K)$, where $\phi$ is a feature map.
- The Associative Property Trick: In standard attention, we compute $(QK^T)V$. This forces the creation of the $N \times N$ matrix. By using the kernel formulation, they can use the associative property of matrix multiplication to compute $Q(K^T V)$ instead. This simple algebraic shift changes the complexity from $O(N^2)$ to $O(N)$.
- The Feature Map: They use a specific feature map $\phi(x) = \text{elu}(x) + 1$ to ensure the similarity scores remain positive, which is critical for stable training.
- Transformers as RNNs: They prove that when using causal masking (ensuring tokens only look at the past), linear attention can be written as an iterative update. The model maintains an internal "state" (a cumulative sum of keys and values) and updates it with each new token. This effectively turns the Transformer into an RNN with a very large, high-capacity hidden state.
Key Insights
- The "Aha!" Moment: The most profound insight is that Transformers and RNNs are not two entirely different species; they are points on a spectrum. A Transformer with linear attention is an RNN where the hidden state is a matrix representing the accumulated key-value pairs.
- Best of Both Worlds: This formulation allows for parallelized training (the primary advantage of Transformers) while maintaining constant-time inference (the primary advantage of RNNs).
- Constant Memory Inference: Unlike standard Transformers, where memory usage grows as you generate longer sequences, the Linear Transformer uses a fixed amount of memory regardless of how many tokens it has already generated.
Datasets and Key Results
The authors evaluated their model across three distinct domains to prove its versatility: synthetic tasks, image generation (MNIST and CIFAR-10), and Automatic Speech Recognition (ASR) using the Wall Street Journal dataset.
- Incredible Speedups: On CIFAR-10 image generation, the Linear Transformer was 4,000x faster than the standard softmax Transformer during inference.
- Throughput: For MNIST, the model achieved a throughput of 142.8 images/sec, compared to just 0.45 images/sec for the standard version.
- Performance Parity: In speech recognition, the Linear Transformer significantly outperformed LSTMs and the Reformer. Though it trailed slightly behind the standard softmax Transformer in raw Phoneme Error Rate (8.08% vs 5.12%), it was over 3x faster to train per epoch.
Limitations and Future Work
- The Softmax Gap: While linear attention is much faster, standard softmax attention still tends to perform slightly better on certain complex tasks. The authors suggest that the simple ELU-based feature map might not capture all the nuances of the exponential kernel used in softmax.
- Feature Map Exploration: The paper opens the door for research into better feature maps (like those approximating RBF kernels) that might close the performance gap while keeping the linear complexity.
- Information Retrieval: Because the "hidden state" of this RNN-like Transformer is a matrix, there is more work to be done in understanding how it stores and retrieves specific long-term dependencies compared to traditional LSTMs.
Summary updated