Learn: Linear & Sub-Quadratic Attention (& the SSM Turn)
What you're learning
Sparse attention kept softmax and just skipped entries of the matrix. This lesson asks a more radical question: can we change the algorithm so the matrix is never formed at all, buying true time and — at inference — a constant-size state instead of a growing KV cache? Two families answer yes: (a) linearize attention with the kernel trick + associativity, and (b) replace attention with state-space / recurrent models (S4, Mamba). By the end you’ll be able to derive why associativity gives , write the SSM recurrence and its convolutional dual, explain Mamba’s selectivity, and reason about the recall/quality tax you pay for the constant state.
This closes Tier 2 of the Attention Mechanisms curriculum. It assumes lessons 01–06: you should be fluent in , the softmax, the wall, the KV cache, and the sparse/sliding-window tricks that only thin that matrix. Vocabulary follows the attention-glossary.
1. Learning map
graph TD A["The O(s²) wall<br/>(forms s×s scores)"] --> B["Sparse attn (lesson 06)<br/>keeps softmax,<br/>skips entries"] A --> C["Change the ALGORITHM<br/>→ never form s×s"] C --> D["(a) Linearize attention<br/>kernel trick + associativity"] C --> E["(b) Replace attention<br/>state-space / recurrent"] D --> D1["Linear Attention<br/>φ(Q)(φ(K)ᵀV) → O(s)<br/>Katharopoulos 2020"] D --> D2["Performers (FAVOR+)<br/>random-feature softmax"] D --> D3["Linformer<br/>low-rank K,V projection"] E --> E1["S4: structured SSM<br/>conv ⇄ recurrence duality"] E1 --> E2["Mamba: SELECTIVE SSM<br/>input-dependent B,C,Δ<br/>→ content-based routing"] D1 --> F["Constant state,<br/>linear time"] E2 --> F F --> G["⚠ recall gap<br/>vs full softmax attention"] G --> H["Hybrids: interleave<br/>attention + SSM/linear"] style A fill:#a44,color:#fff style D1 fill:#44a,color:#fff style E2 fill:#483,color:#fff style G fill:#a83,color:#fff style H fill:#666,color:#fff
2. Intuition: don’t skip the matrix — dissolve it
Sparse attention (lesson 06) is a bookkeeping fix. The operation is still softmax attention; you just decide, ahead of time, which query–key pairs are allowed to interact so you compute fewer of the entries. The moment you want a token to potentially see any other token, you’re back to the quadratic.
The deeper move is to notice why attention is quadratic in the first place. The culprit is the softmax nonlinearity sitting between and :
Because softmax couples an entire row of scores together (it normalizes across all keys), you must materialize each row before you can multiply by . That single nonlinearity is the reason you can’t reassociate the product. Kill or replace it and two doors open:
-
(a) Linearize. Approximate by a separable similarity . Now the score is a plain inner product of features, and matrix multiplication is associative: compute first (a small object that summarizes the whole past), then multiply by . The matrix never exists. This also exposes a recurrent form — attention is an RNN with a matrix-valued hidden state.1
-
(b) Replace. Drop the attention framing entirely and use a linear recurrence: carry a fixed-size hidden state forward one token at a time, . This is a state-space model (SSM). It’s by construction and, remarkably, when the recurrence is linear time-invariant it can be unrolled into a single long convolution and trained in parallel like a Transformer. S4 made this competitive; Mamba made the dynamics input-dependent and recovered the content-based routing that made attention special in the first place.23
The unifying picture
Full attention keeps a lossless but growing memory of the past — the KV cache literally stores every token’s K and V. Linear attention and SSMs keep a fixed-size, lossy summary — a (or ) state that every new token updates in place. That is the whole trade in one sentence: unbounded exact recall at memory vs. bounded compressed recall at memory.
3. The math
3.1 Linear attention: the kernel trick + associativity
Write softmax attention for a single output row explicitly, exposing the exponential kernel:
The generalization is to replace with a kernel for some feature map with non-negative entries (so the weights stay positive).1 Katharopoulos et al. use the simple, cheap map . Substituting:
The last equality is the whole trick: because no longer sits inside a nonlinearity, we pulled it out of the sum. Define the two accumulators that do not depend on :
Then every output is just . In matrix form (bidirectional case):
where is row-wise division. Read the parenthesization: computing first costs and produces an matrix; multiplying by costs another . Total is linear in — the matrix is gone. Contrast with softmax, where the nonlinearity forces the left-to-right order and .
The recurrent form (causal case). For autoregressive models, token may only sum over , so the accumulators become running sums — a linear recurrence with a matrix-valued state:
This is exactly why the paper is titled “Transformers are RNNs”: a causal linear-attention layer is an RNN whose hidden state is the fixed matrix . At inference it needs constant memory and constant compute per token — no KV cache that grows with .1
The quality gap is real
with a cheap is a poor approximation of : it can’t produce the sharp, spiky, near-one-hot distributions that softmax attention uses for precise retrieval, and cramming all history into a fixed state is lossy. Vanilla linear attention consistently underperforms softmax attention on language modeling and especially on tasks needing exact recall / copying / in-context lookup. Much follow-up work (feature maps, gating, normalization, delta-rule updates) narrows but has not fully closed this gap. Treat “linear attention = free lunch” as a misconception.
3.2 Two cousins, one line each
- Performers (FAVOR+). Instead of a heuristic , use random features that make an unbiased estimator of the softmax kernel , with positive features for stability. This keeps the associativity trick while provably approximating true softmax attention.4
- Linformer. Observe that the attention matrix is empirically low-rank, then project and along the sequence dimension down to a small before attending: . Cheap and effective, but the projection mixes across time, so it’s awkward to make strictly causal.5
3.3 State-space models: S4 and the conv ⇄ recurrence duality
A continuous-time linear state-space model maps a 1-D input signal to output through a hidden state :
To use it on a token sequence we discretize with a step size (zero-order hold), giving matrices :
and a clean linear recurrence:
Here is the magic that made SSMs trainable at scale. When are constant across time (a linear time-invariant, LTI, system), unrolling the recurrence gives — i.e. is a convolution of with a single (very long) kernel:
This is the duality: train as a convolution (fully parallel over the sequence, computed in via FFT), then run inference as a recurrence (one step at a time, state). The remaining problem was that a naive either explodes, vanishes, or is too expensive; S4’s contribution was a structured parameterization of (a diagonal-plus-low-rank / HiPPO form) that makes the long kernel both stable and cheap to compute, letting SSMs finally beat Transformers on very long-range benchmarks like Long Range Arena.26
Why constant state beats a KV cache for long sequences
An SSM’s hidden state has a fixed size (per channel), no matter how long the sequence. Generation is a fixed-cost matrix–vector update per token, and memory never grows with context — the opposite of attention, whose KV cache grows linearly with (lesson 05) and dominates long-context inference. That constant-state, linear-time profile is the reason SSMs became the serious challenger to attention for very long sequences.
3.4 Mamba: selectivity recovers content-based routing
S4’s Achilles heel is exactly what made it efficient: LTI means the dynamics are the same at every position, so the model cannot decide, based on the current token’s content, what to remember or ignore. Softmax attention’s superpower is precisely this content-dependent routing. Mamba closes the gap by making the SSM parameters functions of the input — a selective SSM (dubbed S6):3
with a learned parameter whose effective discretized form is now input-dependent through . Intuitively: a large makes the model “pay attention” and write the current token strongly into state (and reset old state via the decay), while a small makes it “coast,” ignoring an unimportant token. This is content-based gating — a selective forget/keep decision per token — which vanilla linear attention (fixed , no gating) simply does not have.
The cost of selectivity: once vary with , the system is no longer LTI, so the convolutional form is gone. Mamba recovers parallel training with a hardware-aware parallel scan (an associative scan over the recurrence, kept in fast SRAM), giving linear-time training and constant-state, linear-time inference — matching or beating Transformers of similar size on language, audio, and genomics while scaling far better in sequence length.3
Selectivity narrows the recall gap — it doesn't erase it
Mamba is dramatically better than S4/linear attention at content-based tasks, but its state is still a fixed-size compression of the past. On tasks that need verbatim recall of arbitrary earlier tokens (copying, associative recall, long-context retrieval / “needle in a haystack”), pure SSMs still trail full attention, whose KV cache stores every token losslessly. This is the empirical driver behind hybrids. [recent]
3.5 The delta-rule wave: DeltaNet → Gated DeltaNet → KDA
Vanilla linear attention has a fatal flaw: its state update is purely additive, . Write two similar keys and their values superimpose in — there’s no way to overwrite or forget, so the fixed-size state saturates and recall collapses. The delta rule fixes this and is the engine behind the strongest 2024–2026 linear-attention models.7
DeltaNet treats the state as a fast weight and applies one step of online gradient descent per token: before writing , subtract the value the state currently associates with :
The factor erases the old association before writing the new one — error-correcting associative memory that additive linear attention lacks.7
Gated DeltaNet (GDN) adds Mamba-2-style data-dependent forgetting — a scalar decay — on top of the delta rule:8
so the state both forgets (via ) and corrects (via the delta term). GDN is the linear layer in Qwen3.8-Next’s hybrid (3 GDN : 1 full attention).9
Kimi Delta Attention (KDA) — the frontier variant, used in Kimi K3 (2026) — makes the forget gate channel-wise: is a vector, one decay per key channel, so different feature dimensions retain memory over different timescales:10
K3 adds two practical tweaks: a lower-bounded decay with (keeps the chunkwise rescaling factor inside BF16 range so all tiles run as dense Tensor-Core matmuls), and a full-rank data-dependent output gate .10 Like all these methods it keeps a constant-size state per head (not a growing KV cache) and trains via a chunkwise-parallel form. Details: delta-rule, gated-deltanet, kda.
Why the delta rule matters now
Additive linear attention traded recall for efficiency and lost. The delta rule + gating recovered enough recall that these are no longer toy alternatives — they’re the linear half of the hybrid models frontier labs actually ship (§3.6). The state is still constant-size, so inference is memory per step — the property softmax attention can never have.
3.6 Hybrids: get recall and efficiency
Since attention excels at exact recall and SSM/linear layers excel at cheap long-range mixing, the pragmatic answer is to interleave them: mostly linear/delta layers for throughput and constant-state efficiency, with a few full-attention layers to restore precise retrieval. This is now the frontier default, not a research curiosity:
- Kimi K3 (2026): a 3 KDA : 1 Gated-MLA block repeated through 93 layers (69 KDA + 24 MLA), with NoPE on the MLA layers — KDA’s gating supplies position implicitly — trained natively at 1M context.10 Only the 24 MLA layers keep a growing (latent-compressed) cache; the 69 KDA layers keep fixed-size states.
- Qwen3.8-Next (2026): a 3 GDN : 1 full-attention hybrid; the full-attention layers are further sparsified by QSA (qwen-sparse-attention).9
- Earlier/other lines: Jamba interleaves Transformer + Mamba blocks with MoE
[verify]; DeepMind’s Griffin/Hawk mix gated linear recurrences with local attention[verify].11
The recurring finding: a small fraction of full-attention layers recovers most of the recall a linear-only model loses, at a large efficiency win. The 2026 frontier converged on ~1 full-attention layer per 3–4 linear/delta layers.
Quiz (retrieval): Why does the associativity give time, while softmax attention is stuck at ?
Answer
Matrix multiplication is associative, so we can multiply first. That contracts the sequence dimension immediately, producing an summary of the entire past whose size is independent of ; multiplying by then costs — linear in . The matrix is never materialized. Softmax attention can’t do this because the softmax nonlinearity sits between and : you must form and normalize each full score row before multiplying by , forcing the order and work + memory.
Quiz (retrieval): What does Mamba's selectivity add over vanilla linear attention, and what does it cost algorithmically?
Answer
Vanilla linear attention uses a fixed feature map and a fixed additive state update — the dynamics are the same regardless of token content, so it can’t decide what to remember or forget. Mamba makes (and hence the effective ) functions of the input, giving per-token content-based gating: large writes/keeps, small ignores. That recovers attention-like content routing. The cost: input-dependent parameters make the system non-LTI, so the parallel convolutional form is lost; Mamba instead trains via a hardware-aware parallel (associative) scan.
4. Illustration: three ways to move information
The contrast is entirely about what object you form and how big your state is.
flowchart TB subgraph SM["Softmax attention — O(s²), forms s×s"] direction LR Q1["Q (s×d)"] --> M1["QKᵀ<br/>(s×s) MATERIALIZED"] K1["K (s×d)"] --> M1 M1 -->|"softmax"| A1["A (s×s)"] A1 --> O1["A·V (s×d)"] V1["V (s×d)"] --> O1 end subgraph LA["Linear attention — O(s), associativity"] direction LR K2["φ(K) (s×r)"] --> S2["φ(K)ᵀV<br/>(r×d) tiny state"] V2["V (s×d)"] --> S2 S2 --> O2["φ(Q)·(φ(K)ᵀV)<br/>(s×d)"] Q2["φ(Q) (s×r)"] --> O2 end subgraph SSM["SSM recurrence — O(s), constant state"] direction LR X3["x₁"] --> H1["h₁ = Āh₀+B̄x₁"] H1 --> H2["h₂ = Āh₁+B̄x₂"] H2 --> H3["h₃ = Āh₂+B̄x₃"] H1 --> Y1["y₁=Ch₁"] H2 --> Y2["y₂=Ch₂"] H3 --> Y3["y₃=Ch₃"] end style M1 fill:#a44,color:#fff style S2 fill:#44a,color:#fff style H2 fill:#483,color:#fff
The red block is the quadratic object you’re trying to avoid. Linear attention replaces it with a tiny summary matrix computed once; the SSM replaces it with a hidden state threaded through the sequence — no per-pair object exists at all. In both alternatives, inference carries only a fixed-size state forward.
Figure: at generation time, attention’s KV cache grows linearly with context while an SSM’s (or linear-attention’s) recurrent state stays constant — the crux of why sub-quadratic models win on very long sequences.
5. FLOPs & memory
Take batch , sequence , model dim ; for linear attention let the feature dim be , and for an SSM let the state size be (per channel). Count a multiply-add as 2 FLOPs.
| Model | Train/prefill time | Materialized memory | Inference state (per layer) | Per-token decode cost |
|---|---|---|---|---|
| Softmax attention | scores (or w/ FlashAttention, still compute) | KV cache — grows with | (attends to all past) | |
| Linear attention | state | — constant in | ||
| S4 (LTI SSM) | scan, or FFT-conv | — constant in | ||
| Mamba (selective) | parallel scan | — constant in |
Two headlines:
- Time. Every sub-quadratic model is linear in for training/prefill (Mamba via parallel scan, S4 via FFT convolution or scan), versus attention’s . Double the context, double — not quadruple — the compute.
- Inference memory is the bigger prize. Attention’s KV cache grows as and is the binding constraint for long-context serving (lesson 05, 12-llm-inference-systems). Linear attention and SSMs carry a fixed-size state ( or ), independent of how much context has streamed by — constant memory and constant per-token latency during generation.
The tax you pay for the constant state
That fixed-size state is a lossy compression of unbounded history. Empirically this shows up as a recall gap: pure linear-attention and SSM models underperform full attention on exact-copy, associative-recall, and long-context retrieval tasks, precisely because they cannot store every past token losslessly the way a KV cache does.
[verify]the exact magnitude — it’s task- and scale-dependent and actively researched — but the direction is robust. This is why frontier long-context systems increasingly go hybrid rather than pure-SSM. [recent]
Problem: KV cache vs. SSM state at long context
A model serves a single stream () with , 32 layers, fp16 (2 bytes). Compare the per-token state of (a) full attention at context vs. (b) a Mamba layer with state size .
Solution. (a) KV cache stores one K and one V of size per token per layer: B 67 GB — and it grows with every generated token. (b) A Mamba state is per layer (roughly): B 0.17 GB, and it is constant regardless of context length. Two-plus orders of magnitude, and the gap widens as grows — the whole economic case for sub-quadratic models at long context.
Problem: when is linear attention actually cheaper than softmax?
Ignoring the projections, softmax attention’s core is FLOPs (lesson 01); linear attention’s is . At what sequence length does linear attention win, and what’s the catch?
Solution. Set . So linear attention is cheaper once the sequence is longer than the feature dimension — which is essentially always for long context (typically or smaller). The catch is quality, not FLOPs: at the same parameter count, the fixed state loses the exact-recall ability of softmax’s interactions, so the FLOP win is only free if your task tolerates the recall gap. This asymmetry — cheap but lossy — is exactly why the field moved toward selective SSMs (Mamba) and hybrids rather than plain linear attention.
6. Read next / primary sources
- Linear attention: Katharopoulos, Vyas, Pappas, Fleuret, “Transformers are RNNs” (2020)1 — read §3 for the kernel formulation and §3.2–3.3 for the recurrent form. This is the cleanest derivation of the associativity trick.
- State-space models: Gu, Goel, Ré, “Efficiently Modeling Long Sequences with Structured State Spaces” (S4) (2021)2 for the conv⇄recurrence duality and the structured ; then Gu & Dao, “Mamba” (2023)3 §3 for selectivity (S6) and §3.3 for the hardware-aware scan.
- Approximation cousins: skim Performer4 (§2, FAVOR+) and Linformer5 (§3, low-rank) — one section each is enough to place them.
Ask your teacher
Want the delta-rule / gated-linear-attention lineage (that unifies linear attention and modern RNNs like RWKV, RetNet, GLA), the associative-scan math in full, or a worked Mamba forward pass? Ask — I’m your teacher for this course and can go as deep as you like, or build you a harder problem set.
7. What’s next
- 08-attention-in-vits-and-vlms — Tier 3 opens: how attention adapts to images and multimodal inputs (ViTs, patchification, cross-attention, vision–language models).
- Back to 06-sparse-and-sliding-window — the “keep softmax, thin the matrix” approach this lesson contrasts against. Holding both in mind (skip entries vs. dissolve the matrix) is the right mental model for the efficiency frontier.
References
Reference: attention-glossary | Topic: attention-mechanisms | Filed: 2026-09-10
Footnotes
-
Katharopoulos, Vyas, Pappas, Fleuret, “Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention” — arXiv:2006.16236 (2020). Introduces the kernel-feature-map linearization , the associativity trick for attention, the feature map , and the causal recurrent (RNN) form with matrix-valued state. [established] ↩ ↩2 ↩3 ↩4
-
Gu, Goel, Ré, “Efficiently Modeling Long Sequences with Structured State Spaces (S4)” — arXiv:2111.00396 (2021). Structured (diagonal-plus-low-rank / HiPPO) SSM parameterization; the convolution⇄recurrence duality; strong Long Range Arena results. [established] ↩ ↩2 ↩3
-
Gu & Dao, “Mamba: Linear-Time Sequence Modeling with Selective State Spaces” — arXiv:2312.00752 (2023). Selective SSM (S6) with input-dependent ; hardware-aware parallel scan; linear-time training and constant-state inference matching/beating Transformers at similar scale. [recent] ↩ ↩2 ↩3 ↩4
-
Choromanski et al., “Rethinking Attention with Performers” — arXiv:2009.14794 (2020). FAVOR+ uses positive random features to give an unbiased, low-variance estimate of the softmax kernel, retaining linear time/space. [established] ↩ ↩2
-
Wang, Li, Khabsa, Fang, Ma, “Linformer: Self-Attention with Linear Complexity” — arXiv:2006.04768 (2020). Projects K and V along the sequence dimension to a small , exploiting the empirical low-rank structure of the attention matrix for cost. [established] ↩ ↩2
-
Gu, Dao, Ermon, Rudra, Ré, “HiPPO: Recurrent Memory with Optimal Polynomial Projections” — arXiv:2008.07669 (2020). The initialization theory behind S4’s matrix (optimal online function approximation / long-range memory). [established] ↩
-
The delta rule as a linear-attention update: Schlag, Irie, Schmidhuber, “Linear Transformers Are Secretly Fast Weight Programmers” — arXiv:2102.11174 (2021); parallel/chunkwise training in Yang et al., “Parallelizing Linear Transformers with the Delta Rule over Sequence Length” — arXiv:2406.06484 (2024). See delta-rule. [established] ↩ ↩2
-
Yang, Kautz, Hatamizadeh, “Gated Delta Networks: Improving Mamba2 with Delta Rule” (GDN) — arXiv:2412.06464 (2024)
[verify id]. Data-dependent scalar decay combined with the delta rule. See gated-deltanet. [recent] ↩ -
Qwen Team (Alibaba), “On the Design of Qwen3.8-Next Architecture” — arXiv:2608.30320 (2026). 3 Gated-DeltaNet : 1 full-attention hybrid; full-attention layers sparsified by QSA. Transcribed from the primary PDF (§2.1). See qwen-sparse-attention, 2026-09-12-qwen3.8-next. [recent] ↩ ↩2
-
Kimi Team (Moonshot AI), “Kimi K3: Open Frontier Intelligence” — arXiv:2607.24653 (2026); KDA originates in “Kimi Linear: An Expressive, Efficient Attention Architecture” — arXiv:2510.26692 (2025). Channel-wise-gated delta-rule linear attention; K3’s bounded-sigmoid decay () and full-rank output gate; 3 KDA : 1 Gated-MLA hybrid with NoPE at 1M context. Transcribed from the primary PDF (§2.1). See kda, 2026-09-12-kimi-k3. [recent] ↩ ↩2 ↩3
-
Jamba (AI21 Labs), “Jamba: A Hybrid Transformer-Mamba Language Model” — arXiv:2403.19887 (2024). Interleaves Transformer and Mamba layers with MoE for long context at reduced KV-cache cost. Exact layer ratios/benchmarks
[verify]. DeepMind’s Griffin/Hawk (arXiv:2402.19427) similarly mix gated linear recurrences with local attention[verify]. [recent] ↩