Linear Attention, Mamba & SSMs

An alternative to attention that scales to a million words.

🔴 advanced5 min readarchitectures
State space models like Mamba are an alternative to attention for long sequences. Attention compares every word with every other, so its cost grows with the square of the length. A state space model instead reads the sequence in order and carries a fixed-size summary forward, updating it at each step, so the cost grows only in a straight line. Mamba's key idea is letting each word decide what to keep or forget in that summary, which makes inputs of a million words practical.

The Quadratic Attention Wall

Standard Transformer self attention compares every token to every other token, scaling quadratically O(N2)O(N^2).

At sequence lengths of 1 Million tokens, self attention crashes due to compute and memory limits.

State Space Models (SSMs) like Mamba (Gu & Dao, 2023) achieve linear O(N)O(N) training time and constant O(1)O(1) generation memory per token!

  TRANSFORMER ATTENTION:    N x N Pairwise Matrix ──► Quadratic O(N^2) Compute & Memory Growth
  MAMBA STATE SPACE MODEL:  Selective State Dynamic ──► Linear O(N) Compute & O(1) Step Memory!

Continuous State Space Foundations

Derived from classical control theory, a continuous State Space Model maps an input signal x(t)∈Rx(t) \in \mathbb{R} to an output signal y(t)∈Ry(t) \in \mathbb{R} via a hidden state h(t)∈RNh(t) \in \mathbb{R}^N:

State Equation: h′(t)=Ah(t)+Bx(t)\text{State Equation: } h'(t) = A h(t) + B x(t) Output Equation: y(t)=Ch(t)\text{Output Equation: } y(t) = C h(t)

Using discretization step Δ\Delta, continuous equations are converted into discrete recurrence steps:

ht=Aˉht−1+Bˉxt,yt=Chth_t = \bar{A} h_{t-1} + \bar{B} x_t, \quad y_t = C h_t

The Two Views of SSMs: Convolution vs Recurrence

SSMs possess a mathematical superpower:

  TRAINING MODE (Parallel Convolution):
  y = x * K   (Convolve entire sequence x with 1D long filter kernel K in O(N log N) time on GPUs!)

  INFERENCE MODE (Step-by-Step Recurrence):
  h_t = A_bar * h_{t-1} + B_bar * x_t   (Update tiny state vector in O(1) constant time per token!)

This combines the fast GPU parallel training of Transformers with the efficient O(1)O(1) memory generation of RNNs!

Mamba: Selective State Space Models

Legacy SSMs (S4) used time invariant matrices (A,B,CA, B, C were constant for all tokens).

Because matrices never changed, S4 could not perform content selection (for example, it could not filter out filler words or copy exact facts from context).

Mamba (Gu & Dao, 2023) introduced Selection Mechanisms:

Make matrices B(xt)B(x_t), C(xt)C(x_t), and step size Δ(xt)\Delta(x_t) data dependent functions of input token xtx_t!

  1. If token xtx_t is an important keyword, Δ(xt)\Delta(x_t) is large →\to Store xtx_t deeply in state hth_t.
  2. If token xtx_t is a filler word, Δ(xt)\Delta(x_t) is near zero →\to Ignore xtx_t and pass state hth_t through unchanged.

Say this out loud

State Space Models like Mamba replace quadratic self attention with linear time state dynamics. SSMs act as parallel 1D convolutions during GPU training and constant O(1) recurrent steps during token generation. Mamba introduces Selective SSMs by making state transition matrices data dependent, allowing models to process million token context windows efficiently.

Followups to expect

  1. What is Hardware Aware Scan in Mamba? Mamba executes data dependent recurrent updates in GPU SRAM cache using a custom parallel scan kernel, avoiding slow HBM memory read write bottlenecks.
  2. Can Mamba replace Transformers completely? Hybrid architectures (like Jamba) combining Mamba layers for long context efficiency with Transformer attention layers for exact retrieval achieve optimal overall performance.

Check yourself

Question 1 of 3

What primary computational advantage does Mamba (Selective State Space Model) offer over standard Transformer attention for long sequences?

More in NLP & Transformers

See all →
The Attention Mechanism5 minTransformer Architecture5 minTokenization & BPE5 min