datarekha
Deep Learning Hard Asked at NvidiaAsked at TogetherAsked at Mistral

How do state-space models like Mamba differ from attention, and when would you use one?

The short answer

A state-space model carries a fixed-size hidden state forward through the sequence like a selective recurrence, giving O(N) time and constant per-step memory with no KV cache that grows with context. Attention instead compares every token to every other, which is O(N^2) but allows exact lookup of any past token. Mamba's gates are input-dependent, recovering much of attention's content-awareness; the trade-off is that a fixed state can't recall arbitrary far-back tokens as precisely. In practice, hybrids that interleave Mamba layers with a few attention layers give near-linear cost with near-attention quality.

How to think about it

The two mix sequence information in fundamentally different ways.

Attention compares every query to every key — O(N²) compute and, at inference, a KV cache that grows linearly with context. Its superpower is exact lookup: any token can directly retrieve any other, however far back.

State-space models (Mamba) treat the sequence as a selective linear recurrence: a fixed-size hidden state is carried forward and updated with input-dependent gates that decide what to keep or forget.

AttentionSSM (Mamba)
TimeO(N²)O(N)
Inference memoryKV cache grows with Nfixed-size state
Long-range recallexact lookupcompressed into state

When to reach for an SSM: streaming or very long sequences where throughput and constant memory matter more than perfect recall — the absence of a growing KV cache is a major serving win.

Learn it properly Sparse & sub-quadratic attention

Keep practising

All Deep Learning questions

Explore further

Skip to content