Exercises — State-space models (Mamba, S4)
This page is a self-test ladder for State-space models (Mamba, S4). Work each problem on paper first, then open the collapsible solution. Levels climb from recognising the pieces to synthesising the whole selective-SSM idea. Every number you see is machine-checked.

Prerequisite refreshers if a step feels shaky: RNNs and LSTMs (the recurrence idea), Convolutional Neural Networks (the kernel/convolution idea), Linear Attention (the "linear-time sequence mixing" cousin).
Level 1 — Recognition
Exercise 1.1
Below are three descriptions. Match each to one of: recurrent view, convolutional view, selective (Mamba) mechanism.
- (a) "The output is a single fixed kernel slid across the whole input."
- (b) "Each step reads the previous hidden state, decays it, then adds the new input."
- (c) "The decay strength changes token-by-token because it is computed from the token itself."
Recall Solution
- (a) → convolutional view. One kernel is the same for every position, so it is a plain convolution. This is what training uses (parallel, FFT-friendly).
- (b) → recurrent view: . This is what generation/inference uses (one step at a time).
- (c) → selective (Mamba) mechanism. Making depend on is exactly what Mamba adds on top of S4.
Exercise 1.2
In the continuous SSM , what does the symbol mean in plain words, and which matrix decides how fast the state changes?
Recall Solution
is the rate of change of the hidden state — how much moves per tiny slice of time (the derivative). The matrix multiplies the current state and therefore decides how the state pushes on itself: it governs the speed and direction of that change. only controls how much the input nudges the state.
Level 2 — Application
Exercise 2.1
Given the diagonal discrete matrix and vectors with and inputs , , . Compute and outputs .
Recall Solution
Use , then .
Step 1 (): .
Step 2 (): input term vanishes, only decay: .
Step 3 (): .
This is exactly an impulse response: we hit the system with a single spike at and watched it decay. The slow dimension () keeps ringing longer than the fast one ().
Exercise 2.2
The SSM kernel is for length . Using the matrices of Exercise 2.1, compute the three kernel entries and confirm they equal from the impulse above.
Recall Solution
- , so
- , so
Kernel , which matches . That is the whole point: the impulse response is the kernel. Convolving any input with this kernel gives the same output as running the recurrence.
Level 3 — Analysis
Exercise 3.1
The discrete decay for a single (scalar) mode is with . (a) Why must be negative for a stable memory? (b) If , compute for and for . (c) Interpret: which gives longer memory?
Recall Solution
(a) Stability means the state must not blow up when input stops. Over one step the state is multiplied by . We need so repeated multiplication shrinks the state. Since , exactly when . If the state grows without bound (unstable); if it never forgets (marginal).
(b)
(c) Smaller (here ) gives , closer to ⇒ slow decay ⇒ longer memory. Larger () gives ⇒ fast forgetting.
⚠️ Note the subtlety: for a fixed negative , increasing shrinks . The "large = long memory" slogan from the parent note assumes the model co-adapts ; do not read it as a raw mathematical identity. See the trap below.
Exercise 3.2
Mamba makes a function of the token: where . Why is softplus (not a plain linear layer) used to produce ? Compute for pre-activation and .
Recall Solution
Why softplus: is a timestep and must be strictly positive (a negative or zero timestep has no physical meaning in the discretisation ). Softplus maps all of to smoothly, so gradients flow everywhere and can never go negative. A plain linear output could produce negative and break stability.
Values:
Level 4 — Synthesis
Exercise 4.1
Take the fixed-vs-selective comparison. Two tokens have pre-activations feeding : token "John" gives , token "the" gives . Suppose the shared mode has . Compute for each token and state which token is remembered longer, and by how many steps a signal decays to of its value.
Recall Solution
Here the "the" token has (slow decay), while "John" has (fast decay). Time to decay to : number of steps with , i.e. .
- "the": steps.
- "John": steps.
Interpretation & the twist: with fixed at , a large makes small (fast forget). So the raw math says "John" (large ) is forgotten fast — the opposite of the intuitive story. This is exactly why Mamba does not fix : it must learn small jointly so that a large can push toward . This exercise deliberately shows the failure mode of holding constant.
Exercise 4.2
Redo the memory choice the Mamba-consistent way: let the network learn for the important mode. With and , compute for each and confirm the intuitive ordering (John remembered longer).
Recall Solution
Hmm — with a single tiny , "the" still has larger . The real lever is that Mamba's depends on and the input-dependent gating how much input actually enters and exits each mode. The cleaner statement: selectivity gives the model independent control per token over how much it writes (), how much it decays ( through ), and how much it reads (). Memory is the joint result of all three, not alone.
Numerically confirmed: , .
Level 5 — Mastery
Exercise 5.1
Complexity synthesis. A Transformer's attention costs for sequence length and width ; an SSM layer costs recurrently (state size ) or via FFT. For , , : compute the attention cost, the recurrent SSM cost, and the FFT-SSM cost (use ), and state the ratio attention/recurrent.
Recall Solution
- Attention:
- Recurrent SSM:
- FFT SSM:
- Ratio attention/recurrent
So attention is roughly 6250× more expensive here. This is the entire motivation for SSMs on 100K-token sequences — the quadratic term explodes while the SSM stays effectively linear in .
Exercise 5.2
Full-pipeline reasoning. Explain, in ordered steps, why S4 trains with convolution but generates with recurrence, and why Mamba cannot use the convolution shortcut for training. Then state what Mamba does instead to stay fast.
Recall Solution
- Training = whole sequence known. All inputs are available at once. The output is with a single fixed kernel . Convolution parallelises across all positions and runs in via FFT — ideal for GPUs.
- Generation = one token at a time. During autoregressive decoding future tokens do not exist yet, so we cannot form a full convolution. The recurrence carries a constant-size state , giving work and memory per new token — far cheaper than re-attending to all past tokens.
- Why Mamba breaks the shortcut. Mamba makes depend on each input token. The kernel is no longer one fixed filter — it changes per position — so the operation is not a convolution and FFT no longer applies.
- Mamba's fix. It uses a hardware-aware parallel scan (an associative-scan of the linear recurrence) with kernel fusion and careful GPU-memory management, recovering near-FFT speed while keeping the input-dependent (selective) update.
One-line synthesis: S4 buys parallelism with a fixed kernel; Mamba trades the fixed kernel for selectivity and buys back parallelism with a fused scan.
Recall One-line self-check
A single impulse at produces outputs equal to the kernel entries ::: True — the impulse response is the SSM convolution kernel (Exercises 2.1 & 2.2).
Back to the hub: State-space models (Mamba, S4) · Hinglish version: 6.1.11 State-space models (Mamba, S4) (Hinglish).