AI Grimoire
Sheet
statuspromising
difficultyadvanced
timeO(n·d²)
memoryO(d²)
described2020
revised4d ago

Linear Attention

The quadratic term comes from computing QKᵀ first. Replace the softmax with a feature map and matrix multiplication becomes associative again — so compute KᵀV instead, and the cost is linear in sequence length.

Standing

PromisingPromising and actively moving. The results are real but narrow — one lab, one model family, or one benchmark suite — and the picture may look different in a year.

The gated variants are genuinely competitive and are shipping in hybrid models. Whether they can match exact attention on long-range recall alone is still open.

judged as of 2026-09 · what the labels mean

Theory

Attention is quadratic because of the order of two matrix multiplications, not because of the amount of information it moves. QKQ K^{\top} is n×nn \times n; KVK^{\top} V is d×dd \times d. The softmax between them is what forbids the second grouping, because softmax(QK)V\softmax(QK^{\top})V does not factor.

Remove it. Write the similarity as an inner product of feature maps and the associativity comes back.

(ϕ(Q)ϕ(K))VO(n2d)  =  ϕ(Q)(ϕ(K)V)O(nd2)\underbrace{\bigl( \phi(Q) \phi(K)^{\top} \bigr) V}_{O(n^2 d)} \;=\; \underbrace{\phi(Q) \bigl( \phi(K)^{\top} V \bigr)}_{O(n d^2)}
eq. 1 — the same numerator, regroupedKatharopoulos et al. §3.2

The normalisation carries over: divide by ϕ(qi)jϕ(kj)\phi(q_i)^{\top} \sum_j \phi(k_j) rather than by a row sum of exponentials. The result is a weighted average with non-negative weights — just not a softmax, and not a sharp one.

It is a linear RNN

St=St1+ϕ(kt)vt,zt=zt1+ϕ(kt),ot=ϕ(qt)Stϕ(qt)ztS_t = S_{t-1} + \phi(k_t) v_t^{\top}, \qquad z_t = z_{t-1} + \phi(k_t), \qquad o_t = \frac{\phi(q_t)^{\top} S_t}{\phi(q_t)^{\top} z_t}
eq. 2 — causal case, as a recurrence over a matrix-valued state

With the mask applied, the cumulative sums are a recurrent state: a d×dd \times d matrix SS and a dd-vector zz, both of fixed size. Decoding is then O(d2)O(d^2) per token with no cache that grows — the property that makes this family interesting, and the same property state space models arrive at from the other direction.

What is actually lost

A softmax can concentrate almost all its mass on one position. A fixed d×dd \times d state cannot: it is a lossy summary of everything seen so far, and the capacity is d2d^2 regardless of nn. Exact recall of a specific earlier token — the thing induction heads do — degrades as the sequence exceeds that capacity.

The ungated recurrence in eq. 2 also only ever adds, so old information is never removed, only diluted. Gated variants insert a decay term, St=GtSt1+ϕ(kt)vtS_t = G_t \odot S_{t-1} + \phi(k_t) v_t^{\top}, which recovers much of the gap and is what the current generation of these models actually uses.

Implementation

python · torch ≥ 2.1
import torch
from torch import Tensor


def linear_attention_chunked(
    q: Tensor, k: Tensor, v: Tensor, chunk: int = 128  # [B, H, N, D]
) -> Tensor:
    """Chunked parallel form: quadratic within a chunk, recurrent across them."""
    q, k = torch.nn.functional.elu(q) + 1, torch.nn.functional.elu(k) + 1
    b, h, n, d = q.shape

    state = q.new_zeros(b, h, d, v.size(-1))   # running K^T V
    z = q.new_zeros(b, h, d)                   # running sum of phi(k)
    out = []

    for i in range(0, n, chunk):
        qc, kc, vc = q[..., i : i + chunk, :], k[..., i : i + chunk, :], v[..., i : i + chunk, :]

        # Contribution of every earlier chunk, through the summarised state.
        inter = qc @ state
        # Contribution of this chunk, computed the quadratic way and masked.
        mask = torch.ones(qc.size(-2), kc.size(-2), dtype=torch.bool, device=q.device).triu(1)
        intra = (qc @ kc.transpose(-2, -1)).masked_fill(mask, 0.0) @ vc

        denom = qc @ z.unsqueeze(-1) + (qc @ kc.transpose(-2, -1)).masked_fill(mask, 0.0).sum(-1, keepdim=True)
        out.append((inter + intra) / denom.clamp(min=1e-6))

        state = state + kc.transpose(-2, -1) @ vc
        z = z + kc.sum(dim=-2)

    return torch.cat(out, dim=-2)

The chunked form is the one to implement. The purely recurrent version is nn serial steps and trains at a fraction of the throughput; the purely parallel version reintroduces the n2n^2 term it was supposed to remove. Chunking pays O(c2)O(c^2) inside a chunk and O(d2)O(d^2) between chunks, and c128c \approx 128 is usually where the two balance on current hardware.

Softmax, n = 8192
4.3 G
Linear, n = 8192
67 M
Break-even n
≈ 128
d = 64 per head, cost per head

Related

References

[1]Katharopoulos et al. — Transformers are RNNs (2020)arXiv:2006.16236
[2]Choromanski et al. — Rethinking Attention with Performers (2020)arXiv:2009.14794
[3]Yang et al. — Gated Linear Attention Transformers with Hardware-Efficient Training (2023)arXiv:2312.06635