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.
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.
- 1. Mamba's key idea is…
- 2. An important token gets…
- 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.
- 1. softplus(0):
- 2. State fraction kept for λ = −1, Δ = 0.01:
- 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_ssmpackage (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))- 1. Mamba's typical state size per channel:
- 2. Hybrids like Jamba add…
- 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.
- 1. A time-invariant SSM writes every input with…
- 2. Mamba's update resembles…
- 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.
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
- A. Gu and T. Dao (2023). “Mamba: linear-time sequence modeling with selective state spaces.” arXiv:2312.00752.
- T. Dao and A. Gu (2024). “Transformers are SSMs: generalized models and efficient algorithms through structured state space duality.” Proc. ICML.
- O. Lieber et al. (2024). “Jamba: a hybrid Transformer–Mamba language model.” arXiv:2403.19887.