The loop that beats attention
Attention won by reading every token at once, and the bill was quadratic compute plus a cache that grows forever. State-space models like Mamba go back to the recurrent loop everyone thought was dead: a fixed-size summary carried forward in linear time. We work out why the loop came back, the one change that made it competitive, the trick that keeps it parallel, and why its little finite memory is both the win and the catch.
In the transformer post I made attention sound like an unalloyed win: every token reads every other token in a single parallel step, so there's no slow recurrent baton crawling left to right. That parallelism is exactly what let attention be trained on a GPU, and it's why the old recurrent networks were left for dead.
Here's the catch I glossed over. Reading every token against every other costs you work for a sequence of length , and at generation time you have to keep every past token's keys and values around in a cache that grows without limit. I spent a whole post on tricks to shrink that cache. State-space models ask a more radical question: what if you never re-read the past at all?
The bill attention can't stop paying
Slide the context length up on this and watch the two costs pull apart. Attention's work climbs as the square of the length while a state-space model's climbs in a straight line, and the gap is the whole reason anyone went back to the loop.
The square is structural, not an implementation detail. If token 4,000 must attend to all 3,999 tokens before it, and so must every other token, you've signed up for comparisons no matter how clever your kernels are. And the cache that holds those keys and values grows one slab per token, forever. For a chatbot that's annoying; for a model meant to read a whole codebase or an hour of audio, it's the binding constraint.
Going back to the baton
So we go back to the idea the transformer threw away: a running summary, updated one token at a time. Not an LSTM's tangle of gated nonlinearities, though. A plain linear recurrence with learnable matrices.
Read it slowly, because the payoff is in the shapes. The state has a fixed size, set once and never growing. To process the next token you update that one state and move on, so inference is overall and the memory you carry between steps is , completely independent of how much text you've already read. Where attention's cost grows with the conversation, this doesn't.
Which probably looks too simple to compete with attention, and honestly it should. Getting from "too simple" to "actually competitive" took until 2023, and it's worth seeing what was missing.
Where A, B and C come from
These matrices aren't picked out of the air. The cleanest way to read equation (1) is as a discretisation of an underlying continuous-time system, , sampled at a step size . The discrete matrices are what you get when you ask "if the continuous system runs for seconds, where does the state land?".
The clever bit in S4 was choosing the structure of (the HiPPO construction) so that the state provably keeps a useful compression of a long history rather than forgetting it after a few steps. I'll wave at that rather than derive it; the point you need is that the matrices encode a principled, long memory, not an arbitrary one.
The same loop is also a convolution
Now the duality that makes the whole thing trainable. Unroll the recurrence, and the output is just the input convolved with one long, fixed kernel.
One operation, two ways to compute it. At inference you run the recurrence: cheap, streaming, state. At training you treat the whole sequence as a single convolution and do it in one parallel shot, which (since a long convolution is a multiplication in the frequency domain, the Fourier trick) S4 evaluated with an FFT. That's how a "recurrent" model trained as fast as a transformer. Hold onto this duality; in a moment we lose it and have to win it back.
Why the first version underperformed
Here's the puzzle that stumped the field for a while. S4 had the linear-time recurrence and the convolution trick, yet transformers still beat it on language. Why?
Because , and were time-invariant: the same filter applied to every token, regardless of what that token was. A fixed kernel has no way to say "this token is a delimiter I should ignore" or "this is the variable name I'll need later, hold onto it". It treats the word the and the one fact that answers the question with exactly the same response. Attention, by contrast, decides what to look at based on content. That missing content-awareness is the whole gap.
Selectivity: let the input steer the state
Mamba's fix is almost cheeky in its directness. Make the step size and the read/write matrices functions of the current token.
Recall from equation (2) that a big takes a big step and writes the current input in strongly, while a small barely moves the state. Make depend on the token and you've handed the model a remember/forget gate: open it wide to overwrite the state with an important token, leave it nearly shut to let a filler word pass through untouched. Step through it here and watch the single fixed-size state get steered token by token, with attention's all-pairs wiring drawn above for contrast.
The catch, and the parallel scan that saves it
There's a price for selectivity, and it's the duality we were told to hold onto.
Input-dependence breaks the convolution
The moment , and depend on the token, the kernel in equation (3) is no longer fixed across the sequence, so the FFT-convolution that made S4 fast is gone. Mamba doesn't get the convolution view back. It gives it up, and rescues parallelism a different way. The post's whole pivot is this: the fix for quality (input-dependence) breaks the trick for speed, so a second, older trick has to put the speed back.
That older trick is the parallel scan, and it's the prettiest idea in the post.
The loop was never really serial
A linear recurrence is associative: applying step and then step to a state is itself a single step of the same form, so partial steps can be combined in any grouping you like. That is exactly the precondition for a parallel prefix scan, the same primitive that turns a serial cumulative sum into an -depth operation on a GPU. The recurrence only looked serial because we wrote it left to right. Blelloch's 1990 work on prefix sums had the machinery waiting decades before the architecture needed it.
So Mamba computes all states with a scan in depth instead of an crawl, keeps the whole thing in fast on-chip memory, and never materialises the full sequence of states. The loop trains in parallel after all.
The catch you can't optimise away
I've been selling the win. Here's the honest cost, and it's not a bug you can patch out.
A finite state is a lossy memory
A fixed-size state is a compressed summary of an unbounded past, and no amount of training removes the compression. Ask a state-space model to copy a long random string back verbatim, or to fetch the exact token sitting at position 4,000, and it struggles where attention, which literally kept every token's key and value, does not. The finite state is the source of the linear-time win and the source of the retrieval weakness. They are the same fact wearing two hats.
Which is why nobody actually had to pick a side.
The answer to 'recurrence or attention' was 'yes'
Production systems like Jamba interleave a few attention layers, for the exact lookups a finite state can't do, with many state-space layers that carry the long-context bulk at linear cost. The cheap loop does the heavy lifting; the occasional expensive attention layer handles the retrieval. The field didn't crown a winner, it built a hybrid, and that's usually the sign of two ideas that are each right about something different.
One honest aside before the recap. If equation (1) looks familiar to anyone who's done control or signals, that's because it is familiar. is the standard discrete-time linear state-space model, the same an engineer writes down for a filter or feeds to a Kalman filter. The "hidden state" a control engineer means by state is, almost word for word, the "hidden state" the old RNN meant. What's new isn't the equation. It's making , , learned and input-dependent and stacking the thing fifty layers deep. Mamba quietly reunited two fields that had been writing down the same maths for different reasons.
So, which loop wins
Attention reads everything and pays for it forever. The state-space model reads once, keeps a small steerable summary, and trades exact recall for linear cost. Neither strictly wins, which is why the field, sensibly, kept both, layered together. The thing I find satisfying is that the "dead" recurrent loop didn't come back unchanged. It came back having borrowed a content-gate from attention and a parallel scan from 1990s parallel computing, and only then could it compete.
Recap
Attention's cost and ever-growing cache are structural. A linear recurrence with a fixed-size state fixes both, at time and memory per step. S4 had that already and lost, because a time-invariant filter can't choose what to ignore. Mamba makes the update input-dependent (selectivity), which costs it the convolution trick and forces a parallel scan to keep training fast. The finite state is the whole bargain: linear cost in exchange for fuzzy recall, which is why the best systems hybridise it with a little attention.
Reading further
- Gu & Dao (2023), Mamba: Linear-Time Sequence Modeling with Selective State Spaces: selectivity (S6) and the hardware-aware scan, the central reference here. arXiv:2312.00752
- Gu, Goel & Ré (2021), Efficiently Modeling Long Sequences with Structured State Spaces (S4): the continuous-time SSM, HiPPO, and the convolution view. arXiv:2111.00396
- Smith, Warrington & Linderman (2022), Simplified State Space Layers (S5): the parallel-scan formulation that bridges S4's convolution to Mamba's scan. arXiv:2208.04933
- Blelloch (1990), Prefix Sums and Their Applications: the associative scan primitive, decades older than the architecture that needed it. cs.cmu.edu
- Rush, The Annotated S4: a runnable, heavily-annotated walk through the discretisation and the kernel. srush.github.io
- Lieber et al. (2024), Jamba: A Hybrid Transformer-Mamba Language Model: the hybrid that keeps exact retrieval while paying linear cost for the bulk. arXiv:2403.19887
Next I want to take that parallel scan apart on its own, because the prefix-sum primitive quietly powers half of modern GPU code and it deserves its own bench. Watch this space.
Try it in the lab
All effects →Double Pendulum
mathsChaotic pendulums diverging from near-identical starting conditions.
chaosodeLorenz Attractor
mathsThe classic chaotic 3D butterfly — two trajectories diverge from near-identical starts.
chaosodebutterfly effectPhase Portrait
mathsODE trajectories flowing through vector fields — Lotka-Volterra, Van der Pol, Duffing.
odedynamical systems
More from the blog
The delta rule: linear attention for a million-token context
Full attention pays an n² bill that a 1M-token context can't afford. Linear attention swaps the bill for a memory you write to — and the delta rule is what makes that memory smart. Kimi calls K3's KDA a 'hybrid linear attention mechanism'; this is the family it belongs to, from the kernel trick to gated delta updates.
The behavioural scorer caught a model lying about its own game
We wired three frontier-class models (Gemini 3.6 Flash, Claude Opus 4.6 thinking, and GPT-OSS 120B) into the same 7-task harness via the Agy CLI, then switched the scorer from HTML structure to Playwright behavioural checks. The headline result: a model that scored a perfect 100 on the platformer task under the old scorer scored 30 under the new one, five iterations in a row, because the Space key never actually jumped.
Twelve free models just walked into our benchmark — three of them beat the frontier
We wired OpenRouter's free tier into our 7-task LLM harness, registered 13 models with full metadata, and ran a fair 5-iteration sweep across all of them. Ling 3.0 Tiny, Laguna XS 2.1 and Gemma 4 26B posted averages above 98 on a board that Kimi K3 leads at 90.5 — and the entire run cost us nothing.