Attention layers can pull up any token from anywhere in the sequence, but pay O(n²) for it. SSM layers run in O(n), but everything before the current token has to have already been squeezed into one fixed-size state. Rather than pick a side, why not build a model out of both — and let most layers be cheap while a few stay expensive?
Flip a layer from SSM to attention and its bar jumps — even at this toy sequence length of 8, an attention layer already costs noticeably more than an SSM layer at the same width. Flip all four and the total climbs every time; flip them all back and it falls. The stack's total cost is just the sum of whatever each layer happens to be.
A hybrid stack's total cost is the sum of each layer's own cost, and only the count of attention layers matters — not where they sit in the stack:
- — how many of the layers are full attention.
- — the total number of layers in the stack.
- , — sequence length and model width, shared by every layer.
- — the SSM's fixed hidden-state size.
- Each attention layer contributes its own full n²d term
There's no sharing between layers — every attention layer independently pays the full quadratic cost at whatever the model is run at.
- Every SSM layer instead contributes a linear nds term
Swapping an attention layer for an SSM layer doesn't shrink the existing attention layers' cost — it just removes one term from the sum and replaces it with a much smaller one.
- Even one leftover attention layer keeps the whole stack quadratic in n
is still for any — a hybrid with even a single attention layer scales the same way the all-attention stack does, just with a far smaller constant in front. Only (pure SSM) removes the quadratic term entirely.
The bold curve (all 4 layers attention) curves upward fast — pure O(n²). The dashed line (all 4 layers SSM) stays nearly flat by comparison — O(n). The middle curve is the hybrid with just one attention layer: still curving upward, since it's still quadratic in shape, but starting from a much smaller base — three of its four layers are paying the cheap linear cost instead.
, SSM state size , , layers:
- All 4 layers attention
Each layer costs . Total: .
- All 4 layers SSM
Each layer costs . Total: . Ratio to all-attention: reduction.
- Hybrid: 1 attention layer, 3 SSM layers
. Ratio to all-attention: reduction, while keeping one layer able to retrieve any token exactly, however far back it sits.
At n = 64, find the number of attention layers, among the candidates, whose total cost lands on 38,912 — the cost of a stack with exactly 1 attention layer.
A hybrid architecture's total cost is just the sum of its layers' individual costs, so every attention layer swapped for an SSM layer trades a full term for a much smaller one — but the stack stays fundamentally quadratic in shape as long as even one attention layer remains. Positional schemes fixed attention's blindness to order, RoPE and YaRN made that scheme rotate and stretch cleanly, FlashAttention made the quadratic cost that's left as memory-cheap as possible, and hybrid architectures are the move that questions whether every layer needs to pay that cost at all. The closing capstone puts a pure Transformer and a pure Mamba stack head to head on exactly that trade-off, at a sequence length long enough for it to matter.