Skip to content
mlmentorship

Linear attention (Linformer, Performer, kernel methods)

Approximate the softmax attention matrix with a low-rank or kernel factorization so cost is linear in sequence length.

Published · 6 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

Distinguish the dimension each linear-attention family compresses and trace how that choice prevents an n-by-n attention matrix from being materialized.

Preparing the visual…

Summary

Linear attention avoids materializing the softmax matrix by projecting the sequence axis or approximating the softmax kernel with a low-dimensional feature map. The per-layer cost drops from to or for .

Sparse attention (see sparse attention) keeps the softmax exact but on fewer pairs. Linformer instead uses a learned low-rank projection along the sequence dimension; Performer approximates the softmax kernel with random features and changes the order of multiplication.

In practice, modern decoder LLMs do not use linear attention. Quality drops are non-trivial at scale and FlashAttention has made dense attention competitive in wall-clock. Linear attention is most relevant in domains with extreme (genomics, time series of millions of steps) or in research on sub-quadratic alternatives.

Two main families

Project the sequence axis (Linformer, Wang et al., 2020)

Learn fixed projection matrices with . Replace with :

The softmax is now . Cost: , linear in . Caveat: is fixed at training time, so you cannot extrapolate to longer sequences without re-training.

Replace softmax with a kernel (Performer, Choromanski et al., 2020)

Softmax can be written as a kernel . Approximate this kernel with random features such that .

Write and . Because softmax attention is row-normalized, the approximation needs both a value summary and a normalizer. For query row ,

Compute the right-hand contractions first: is and is . Each transformed query reads these summaries, so no matrix is formed. Cost: , linear in , and the feature map works for arbitrary at inference (no fixed sequence-length projection).

Learning objective

Where does each method remove the n × n matrix?

Linformer compresses the sequence axis before computing attention scores Keys and values each start with shape n by d. Learned projections E and F reduce their token dimension from n to k, producing E K and F V with shape k by d. Queries retain shape n by d. Queries meet only the k projected keys, producing n by k weights, which mix the k projected values into an n by d output. An n by n score matrix is never formed. LINFORMER · COMPRESS TOKENS n → k Kn × d Vn × d EF EKk × d FVk × d Qn × d softmaxQ(EK)ᵀ: n × k outputn × d n × n scoresnever created
Performer summarizes transformed keys before queries read them Transformed keys K prime have shape n by r and values have shape n by d. Contracting across n first creates a value summary K prime transpose V of shape r by d and a normalizer K prime transpose one of shape r by one. A transformed query row with shape one by r reads both summaries and divides the numerator by the normalizer to produce one output row with shape one by d. Repeating this per query never forms n by n scores. PERFORMER · SUMMARIZE FEATURES r K′ = φ(K)n × r Vn × d K′ᵀV firstr × d summary K′ᵀ1 firstr × 1 normalizer φ(qᵢ)ᵀ1 × r read bothnumerator÷ normalizer oᵢ1 × d n × n scoresnever created
Read it this way: follow the shapes, not the colors. Linformer reduces the token axis from n to k before queries meet keys, so its weight matrix is n × k. Performer keeps all n tokens but contracts transformed keys with values first, leaving an r × d summary and an r × 1 normalizer for each query to read. Both routes avoid n × n, but by compressing different axes. Original schematic based on Linformer and Performer.

When to use linear attention in 2026

  • Sequence length where FlashAttention is still too slow or doesn’t fit memory.
  • Encoder-only models on very long inputs.
  • Real-time inference with strict latency budgets and tolerable quality loss.

For chat-style decoder LLMs, dense attention with FlashAttention + GQA + KV cache remains the production default.

Common pitfalls

  • Comparing FLOPs without measuring wall-clock. Linear attention’s scaling only beats FlashAttention at large ; the crossover is implementation-dependent and often higher than naive analysis suggests.
  • Forgetting the constants. Linformer’s and Performer’s may need to be hundreds for good quality, so the linear scaling has a large constant.
  • Assuming all softmax-replacement schemes preserve the autoregressive mask trivially. Kernelized attention requires careful handling for causal masking (recursive cumulative sums).