Foundations to Frontier
Tests
E. Deep Learning › E17 State-Space and Recurrent-Revival Models

Mamba / Mamba-2

Mamba (Gu and Dao, 2023) made state-space models competitive with Transformers on language by making them selective. The step size Δ and the matrices B and C are computed from the current input, so the model decides token by token what to write into its state and what to ignore. That breaks time invariance, so the convolution trick no longer applies. Mamba instead uses a hardware-aware parallel scan kept in fast GPU memory. Inference is recurrent, with a fixed-size state and no growing KV cache. Mamba-2 (2024) connected selective SSMs to linear attention through structured state-space duality. This page covers selection, the hardware-aware scan and the SSM–attention duality. In the exercises you implement an input-dependent step, then test selective copying: recover the one marked value from 50 noisy steps. A time-invariant bank of 16 SSM modes reached R² 0.005, weak selection 0.59, and strong selection 1.000 (three seeds agree).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A memory that chooses what to remember

Earlier state-space layers treat every input the same way: each step blends the input into the state by a fixed amount. Mamba lets the input itself decide. An important token gets a big step that writes it firmly into memory; an unimportant one gets a tiny step and is mostly ignored. This “selection” lets a small fixed-size state hold exactly what matters, which is the key to Mamba’s success on language. The demo compares a fixed step with an input-dependent one.

State-space lab
Final state 0.532. With an input-dependent step, the important token at t = 10 is written strongly and then held. This is Mamba's selection mechanism.
Why engineers care

Mamba models run with constant memory per token regardless of context length, which suits long logs, streaming sensors and on-device generation, where a Transformer’s growing KV cache is a problem.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Mamba's key idea is…
  2. 2. An important token gets…
  3. 3. Mamba's inference memory…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Selective SSM: per token t, Δ_t = softplus(W_Δ u_t + b) (softplus(0) = ln 2), B_t = W_B u_t and C_t = W_C u_t.
    • Discretise with ZOH: Ā_t = exp(Δ_t A).
    • Update h_t = Ā_t h_(t−1) + B̄_t u_t and output y_t = C_t h_t.
  • Keep or overwrite: the fraction of state kept is e^(λΔ). For λ = −1, Δ = 0.01 keeps 0.990; Δ = 50 keeps essentially nothing, so the state is replaced by the input.
  • Hardware-aware scan: time-varying dynamics rule out convolution, so Mamba runs a fused parallel scan in GPU SRAM, without materialising the full state sequence in slow memory.
  • Memory:
    • a 48-layer Mamba with d_model 2048, expansion 2 and state 16 keeps about 6 MB of state (16-bit), whatever the context;
    • a Transformer of the same size needs about 39 GB of KV cache for 100 k tokens.
  • Mamba-2 (SSD): with scalar-times-identity A, the selective SSM equals a masked form of linear attention, allowing matrix-multiply-friendly chunked algorithms.
📝 Quick check (Level 1)
0/3 answered
  1. 1. softplus(0):
  2. 2. State fraction kept for λ = −1, Δ = 0.01:
  3. 3. Why no convolution in Mamba?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Mamba in code

Exercise 1.

  • softplus matched ln 2 at 0.
  • A huge step overwrote the state and a tiny step kept it.
  • Δ from a marker feature was large when the marker was on and tiny otherwise.

Practice.

  • Use the mamba_ssm package (CUDA kernels) or pure-PyTorch reference implementations for small experiments.
  • Typical block: input projection with expansion 2, a short causal depthwise convolution, SiLU, the selective SSM (state 16), multiplicative gating with a SiLU branch, and an output projection.
  • For mixed workloads, hybrids with a few attention layers (Jamba, Zamba) recover exact recall.
x, z = in_proj(u).chunk(2, dim=-1)                       # expand x2: main path and gate
x = F.silu(causal_conv1d(x))
dt, B, C = x_proj(x).split([dt_rank, d_state, d_state], -1)
dt = F.softplus(dt_proj(dt))                             # input-dependent step sizes
y = selective_scan(x, dt, A, B, C, D)                    # hardware-aware parallel scan
out = out_proj(y * F.silu(z))
🐍 Exercise 1: selection
Implement softplus, an input-dependent ZOH step and Δ computed from an input feature.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Mamba's typical state size per channel:
  2. 2. Hybrids like Jamba add…
  3. 3. The Mamba block's gate uses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why selection matters.

  • In a time-invariant SSM, every input is written into the state with the same weights, so the state is a fixed linear summary of the past. It cannot decide to keep a particular token and discard others, and it cannot perform content-based filtering.
  • Input-dependent Δ, B and C make the update a function of content. The recurrence stays linear in the state, so a parallel scan still applies, but the model can gate information like an LSTM with forget and input gates.

Connections.

  • With Δ_t controlling both decay and write strength, Mamba’s update resembles a gated RNN (h_t = (1 − g_t)h_(t−1) + g_t u_t).
  • With scalar A (Mamba-2), unrolling gives y_t = Σ_s C_tᵀ(Π_(r=s+1)^t a_r)B_s u_s: masked linear attention with data-dependent decay. This is structured state-space duality.

Limits. A fixed-size state must compress the context, so tasks needing exact recall of many arbitrary earlier tokens (long copying, retrieval) still favour attention; hybrids are common.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A time-invariant SSM writes every input with…
  2. 2. Mamba's update resembles…
  3. 3. Mamba-2's duality links SSMs with…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Is selection needed to pick out what matters?

History.

  • A. Gu and T. Dao, “Mamba: linear-time sequence modeling with selective state spaces” (2023; COLM 2024).
  • T. Dao and A. Gu, “Transformers are SSMs: generalized models and efficient algorithms through structured state space duality” (Mamba-2, ICML 2024).

Exercise 2.

  • Task (selective copying): 50 steps of noise values, exactly one of which carries a marker. The target is the marked value.
  • Models, each with 16 modes (decay rates 0.001 to 1) and a least-squares linear readout of the final state:
    • a time-invariant SSM bank driven by both the value and marker channels;
    • a selective SSM whose step is large at marked steps and small otherwise, weak (8 vs 1) or strong (50 vs 0.01).
model R² (marked value)
time-invariant SSM bank 0.005
selective, weak (Δ = 8 marked, 1 otherwise) 0.588
selective, strong (Δ = 50 marked, 0.01 otherwise) 1.000
  • Time-invariant SSMs cannot select: their final state is a fixed weighted sum of all past values, dominated by noise. Knowing the marker separately does not help, because combining “value × marker” needs a multiplication.
  • Selection solves it exactly: a large step at the marked token overwrites the state with its value, and tiny steps elsewhere preserve it, giving R² = 1.000.
  • How strongly the model selects matters: a weak contrast (8 vs 1) lets noise leak in at unmarked steps and fails to write slow modes, giving only 0.59.
  • Seeds: seeds 1 and 2: 0.03/0.61/1.000 and 0.01/0.55/1.000.
🐍 Exercise 2 (research): is selection needed to pick out what matters?
Compare a time-invariant SSM bank with weakly and strongly selective SSMs on selective copying.

Mamba in AI, robotics and aerospace

  • Streaming inference: constant-memory sequence models for onboard telemetry and robot control loops.
  • Long contexts: logs, time series and genomic data far beyond practical attention lengths.
  • Design lesson: if a model must decide what to remember, its update has to depend on the input; fixed dynamics can only average.

References

  1. A. Gu and T. Dao (2023). “Mamba: linear-time sequence modeling with selective state spaces.” arXiv:2312.00752.
  2. T. Dao and A. Gu (2024). “Transformers are SSMs: generalized models and efficient algorithms through structured state space duality.” Proc. ICML.
  3. O. Lieber et al. (2024). “Jamba: a hybrid Transformer–Mamba language model.” arXiv:2403.19887.