DL0170 Mamba Selective SSM vs Attention

What is Mamba’s Selective State Space Model (S6), and how does its input-dependent gating compare to Transformer self-attention in computational complexity and long-range recall?

Answer

Mamba’s S6 layer is a linear recurrence that carries a fixed-size hidden state h_t per channel and reads it out with a learned projection, exactly like the earlier S4 state space model. The one change that matters is selection: the timestep \Delta_t and the input and output maps B_t, C_t are linear functions of the current token instead of fixed parameters, which turns the discretized decay \bar{A}_t = \exp(\Delta_t A) into an input-dependent forget gate. That single change gives the model the ability to skip filler tokens and write informative ones into the state, but it destroys linear time invariance, so the layer can no longer be evaluated as one long FFT convolution and instead needs a hardware-aware parallel associative scan. On complexity the comparison is lopsided in Mamba’s favor: sequence mixing is O(L d N) instead of O(L^2 d), and decoding needs a constant O(d N) state rather than a KV cache that grows with context. On recall the comparison reverses, because a bounded state is a lossy summary of the past, so tasks that need verbatim retrieval of an arbitrary earlier span degrade in a way that attention’s exact, if expensive, cache does not.

(1) Selection Is The Whole Idea: \Delta_t, B_t, C_t become projections of x_t, while A stays a learned diagonal matrix whose effective decay \exp(\Delta_t A) is nonetheless input-dependent.
(2) \Delta_t Is A Forget Gate: small \Delta_t gives \bar{A}_t \approx I and the token is ignored, large \Delta_t drives \bar{A}_t toward zero and overwrites the state with the new input.
(3) Time Invariance Is Lost: S4 could precompute one global convolution kernel, Mamba cannot, so training uses a work-efficient associative scan in SRAM with activation recomputation instead of an FFT.
(4) Linear Compute: mixing costs O(L d N) with N = 16 in Mamba-1, so the quadratic attention term dominates once L approaches the model width d.
(5) Bounded Decode State: 2 d N values per layer regardless of context length, versus 2 L d for an MHA KV cache, which is what makes million-token streaming decode cheap.
(6) Recall Is The Trade: fixed capacity caps exact copying and multi-query associative recall, which is precisely why production long-context models are usually hybrids with a few full-attention layers.

Diagram of four input tokens the, key, is, 7Q4 each producing an input-dependent timestep Delta_t shown as 0.04, 1.6, 0.05 and 2.1 with skip or write annotations, feeding a chain of fixed-size state boxes h_1 through h_4 linked by input-dependent decay arrows, and a readout row of outputs y_1 through y_4, with a side box noting the state holds 2dN values for any sequence length

Figure 1: The recurrence is ordinary, the gate is not. Because \Delta_t is computed from the token itself, a filler word produces \bar{A}_t \approx I and passes through without disturbing the state, while a rare identifier produces a large step that writes into the state and forgets older content. The state width never depends on L, which is both the efficiency win and the recall ceiling.

The engineering consequence of dropping time invariance is that the layer becomes memory-bound rather than FLOP-bound. Materializing the expanded states, which have shape (B, L, 2d, N), would move far more bytes through HBM than the arithmetic justifies, so the reference implementation fuses discretization, scan, and readout into one kernel that keeps states in SRAM and recomputes them during the backward pass. The recall side has a cleaner theoretical story. Attention stores every key and value, so retrieving a specific earlier token is a lookup, whereas a selective SSM must have decided at write time to keep that token, and its 2 d N slots bound how many distinct key-value associations can survive. This is why Mamba matches or beats Transformers on language modeling perplexity, audio, and DNA, yet lags on induction-head style copying and needle-in-a-haystack retrieval, and why Mamba-2 raises N from 16 to as much as 256 while hybrids keep a small number of attention layers to do the exact lookups.

Mathematical Formulation:
h'(t) = A h(t) + B x(t)
y(t) = C h(t)
\Delta_t = \mathrm{softplus}(W_{\Delta} x_t + b)
B_t = W_B x_t, \quad C_t = W_C x_t
\bar{A}_t = \exp(\Delta_t A)
h_t = \bar{A}_t h_{t-1} + \Delta_t B_t x_t
y_t = C_t^{\top} h_t

Where:

  • y_t is the layer output for token t and h_t \in \mathbb{R}^{N} is the hidden state held for one channel, so the full layer keeps 2 d N values under the standard expansion factor of 2.
  • x_t is the input activation, t \in \{1, \ldots, L\} indexes the sequence, and h_0 = 0 is the required initial condition.
  • A is a learned diagonal matrix parameterized as A = -\exp(A_{\log}) so every eigenvalue is negative and the discrete decay stays stable.
  • \Delta_t > 0 is the input-dependent timestep, and W_{\Delta}, W_B, W_C are the low-rank projections that make the layer selective; b is initialized so that \Delta_t starts in a useful timescale range.
  • \bar{A}_t is the zero-order-hold discretization of A, and \Delta_t B_t x_t is the simplified input term Mamba uses in place of the exact \bar{B}_t.
  • d is the model width, N the state dimension (16 in Mamba-1), and L the sequence length; the scan is O(L d N) work with O(\log L) depth.

Decode Memory At 32k Context (values per layer):
L = 32768, \quad d = 2048, \quad N = 16
\mathrm{cache} = 2 L d = 1.34 \times 10^{8}
\mathrm{state} = 2 d N = 6.55 \times 10^{4}
\mathrm{ratio} = L / N = 2048

Those are element counts, so in bytes the gap narrows a little when the cache is fp16 and the SSM state is fp32, and it narrows further with grouped-query attention, which divides the cache by the query-to-key-value head ratio. The structural point survives every such adjustment: the attention term is proportional to L and the SSM term is not, so past a few thousand tokens the recurrent model is decoding from a constant working set while the Transformer is streaming a cache that eventually dominates both memory and bandwidth.

Two log-log panels: the left panel plots forward-pass FLOPs per layer against sequence length for a self-attention block with a quadratic term and a Mamba block with a linear term, with a dash-dotted vertical crossover line near two thousand tokens, and the right panel plots decode state in megabytes showing a rising fp16 KV cache line against a flat constant Mamba state line at 0.26 megabytes

Figure 2: Below roughly L \approx d the wider Mamba block, which expands the channel dimension by 2, actually costs more than attention, because both are dominated by their projection matmuls. Above it the O(L^2 d) term takes over, and at 128k tokens the attention layer needs about 20 times the FLOPs and a per-layer cache about 4,000 times larger than the constant SSM state.

PropertySelective SSM (Mamba S6)Self-attention (MHA)
Sequence-mixing costO(L d N), linear in LO(L^2 d), quadratic in L
Cost per decoded tokenO(d N), independent of contextO(L d), grows with context
State carried between tokens2 d N values per layer, fixed2 L d values per layer, unbounded
Training parallelismAssociative scan with recomputation; no FFT convolution, since the recurrence is time-varyingPure matmuls, no sequential dependence at all
Exact retrieval and copyingLossy; bounded by state capacity, degrades as the number of stored associations growsExact within the window; copying long spans is easy
Natural fitAudio, DNA, streaming, very long inputs summarized rather than quotedIn-context retrieval, many-shot prompts, verbatim citation

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *