The standard Transformer architecture has revolutionized machine learning, but it harbors a fundamental scaling flaw: the self-attention mechanism is computationally expensive. Specifically, its time and memory requirements grow quadratically ($O(N^2)$) with the length of the input sequence $N$. As Tianyang Lin et al. note in A Survey of Transformers, this quadratic bottleneck makes processing long-form documents, high-resolution images, or extensive biological sequences prohibitively expensive.
To understand why this happens—and how a massive body of research has successfully bypassed it—we must look at the core mathematics of attention.
The Root of the Bottleneck
In a standard softmax attention layer, we transform an input sequence into three matrices: Queries ($Q$), Keys ($K$), and Values ($V$). If the sequence length is $N$ and the feature dimension per head is $d$, then $Q, K, V \in \mathbb{R}^{N \times d}$.
The attention weights are calculated by multiplying $Q$ by the transpose of $K$:
$$A = \text{softmax}\left(\frac{QK^T}{\sqrt{d}}\right)V$$
Let's look at the shapes of these operations:
- $Q \in \mathbb{R}^{N \times d}$ multiplied by $K^T \in \mathbb{R}^{d \times N}$ yields an intermediate matrix of size $N \times N$.
- The $\text{softmax}$ operation is applied row-wise across this $N \times N$ matrix.
- Finally, this $N \times N$ matrix is multiplied by $V \in \mathbb{R}^{N \times d}$ to produce the output.
When $N = 1,000$, the intermediate matrix holds $1,000,000$ elements. But if we scale $N$ to $100,000$ tokens (the length of a novel or a genomic sequence), the intermediate matrix explodes to $10,000,000,000$ elements. This $O(N^2)$ scaling behavior quickly exhausts GPU memory.
The Associative Trick: Linear Attention
The most elegant solution to this problem relies on a simple rule of matrix multiplication: the associative property. In algebra, $(A \times B) \times C = A \times (B \times C)$.
If we can remove or replace the non-linear $\text{softmax}$ operation, we can change the order in which we multiply our matrices. Suppose we replace the softmax with a feature map $\phi(\cdot)$ applied to $Q$ and $K$ individually. The attention equation becomes:
$$A = \left(\phi(Q)\phi(K)^T\right)V$$
Because matrix multiplication is associative, we can rewrite this as:
$$A = \phi(Q)\left(\phi(K)^T V\right)$$
Let's re-evaluate the computational cost by changing the order of operations:
- First, compute $\phi(K)^T V$. Since $\phi(K)^T \in \mathbb{R}^{d \times N}$ and $V \in \mathbb{R}^{N \times d}$, their product is a small matrix of size $d \times d$. This takes $O(Nd^2)$ time.
- Next, multiply $\phi(Q) \in \mathbb{R}^{N \times d}$ by this new $d \times d$ matrix. This also takes $O(Nd^2)$ time.
Because the head dimension $d$ is a small constant (typically 64 or 128) that does not change with sequence length, the overall computational complexity drops from quadratic $O(N^2d)$ to linear $O(Nd^2)$.
As Imanol Schlag et al. demonstrate in Linear Transformers Are Secretly Fast Weight Programmers, this mathematical shift means the $d \times d$ matrix effectively serves as a fixed-size memory storage that updates dynamically as the sequence processes. To maximize the expressivity of this approach, Jiecheng Lu et al. introduce ZeroS: Zero‑Sum Linear Attention for Efficient Transformers, which eliminates the constant averaging terms typical of linear attention to allow contrastive operations within a single linear layer.
Making Linear Attention Hardware-Efficient
While linear attention is theoretically superior for long sequences, implementing it naively on modern hardware can be surprisingly slow. GPUs are optimized for large matrix multiplications (like $QK^T$), and the sequential nature of accumulating a $d \times d$ state can cause memory transfer bottlenecks.
To bridge the gap between theoretical efficiency and actual hardware speed, Songlin Yang et al. developed Gated Linear Attention Transformers with Hardware-Efficient Training. They introduced a hardware-aware algorithm called FlashLinearAttention that optimizes how data is loaded into GPU SRAM, outperforming standard softmax attention in practice. The authors expanded on this in Parallelizing Linear Transformers with the Delta Rule over Sequence Length, leveraging a numerical linear algebra technique known as the compact WY representation to parallelize updates across long sequences efficiently.
Sparse Attention: Pruning the Matrix
An alternative strategy to reducing complexity does not eliminate the softmax operation entirely, but instead forces the attention matrix to be sparse. The intuition here is that a given token rarely needs to attend to every other token in a long document; it primarily cares about its local context and a few key global anchors.
Instead of computing all $N^2$ entries, sparse attention mechanisms only compute a select subset:
- Locality-Sensitive Hashing: Zhiqing Sun et al. propose Sparse Attention with Learning to Hash, which trains hash functions to sort queries and keys into buckets. Attention is only computed between tokens within the same bucket, dropping complexity to $O(N^{1.5})$.
- Block Sparsity: Rather than evaluating individual tokens, models can evaluate blocks of tokens. Ruyi Xu et al. present XAttention: Block Sparse Attention with Antidiagonal Scoring, which uses a computationally cheap antidiagonal scoring trick to identify and compute only the most critical attention blocks, accelerating inference up to 13.5x.
- Shifted Windows: During model fine-tuning, Yukang Chen demonstrates in LongLoRA: Efficient Fine-tuning of Long-Context Large Language Models that context windows can be extended efficiently by restricting attention to local, shifted windows ($S^2$-Attn), drastically reducing memory overhead while preserving long-range performance.
The Recurrent and State-Space Convergence
When we look closely at linear attention—where a $d \times d$ matrix is updated step-by-step—the mechanism begins to look identical to a Recurrent Neural Network (RNN) or a State Space Model (SSM). This realization has unified attention models with recurrent architectures.
For example, Daniel Y Fu et al. demonstrate in Hungry Hungry Hippos: Towards Language Modeling with State Space Models that SSMs can match the expressivity of traditional Transformers while maintaining linear scaling. Similarly, Zhen Qin et al. propose the Hierarchically Gated Recurrent Neural Network for Sequence Modeling, which uses a hierarchical gating mechanism to compress local context in lower layers and pass long-term dependencies to upper layers without ever materializing an $N \times N$ attention matrix.
Evaluating Long-Context Models
As the field has shifted from standard quadratic attention to subquadratic alternatives, evaluating these architectures has required new benchmarks. Mostafa Dehghani et al. established the Long Range Arena : A Benchmark for Efficient Transformers to test models on sequences ranging from 1K to 16K tokens.
Their findings highlight a crucial lesson for the field: merely reducing the theoretical time and memory complexity to $O(N)$ is only half the battle. The true challenge lies in ensuring that these subquadratic architectures retain the strong inductive biases and optimization properties that made the original Transformer so successful.
Key Foundational Work
Self-Attention Based Molecule Representation for Predicting Drug-Target Interaction (2019) — contributed 2 concept elements to this direction.
ABCNN: Attention-Based Convolutional Neural Network for Modeling Sentence Pairs (2016) — contributed 1 concept elements to this direction.