Part XII — Modern Sequence Architectures: RoPE, FlashAttention & State-Space Models · Chapter 8

Selective state spaces (Mamba / Mamba-2)

Hook

Last chapter's state decayed by the same fixed factor AA every single step — whether the current token carried real information or was pure filler. A model that could tell the difference wouldn't decay on filler at all. What if AA and BB weren't fixed?

Intuition
nothing processed yet

Step through the sequence. The faint curve is last chapter's fixed rule — it decays every step, filler or not. The bold curve is selective: dead flat through every run of zeros, then snapping instantly to a new value the moment a real signal arrives.

Formalize

A selective state-space model (the mechanism behind Mamba) makes AA and BB functions of the current input instead of fixed constants:

ht=A(xt)ht1+B(xt)xth_t = A(x_t)\,h_{t-1} + B(x_t)\,x_t
  • hth_t — the hidden state at time step tt.
  • ht1h_{t-1} — the hidden state from the previous time step.
  • xtx_t — the current input.
  • A(xt)A(x_t) — the decay factor, now itself a function of the current input instead of a fixed constant.
  • B(xt)B(x_t) — the input gate, also a function of the current input instead of a fixed constant.

This chapter's selection rule is the simplest one that still makes the point — a token is either signal (xt0x_t \neq 0) or filler (xt=0x_t = 0):

A(xt)={1xt=00xt0,B(xt)={0xt=01xt0A(x_t) = \begin{cases} 1 & x_t = 0 \\ 0 & x_t \neq 0 \end{cases}, \qquad B(x_t) = \begin{cases} 0 & x_t = 0 \\ 1 & x_t \neq 0 \end{cases}
  1. On filler: freeze the state

    A=1,B=0A{=}1,B{=}0 freezes the state exactly — nothing decays, because there's nothing worth forgetting from.

  2. On signal: overwrite it completely

    A=0,B=1A{=}0,B{=}1 overwrites the state completely — the model chooses to forget everything it was holding, on purpose, because something worth remembering just arrived.

Play

Same 7-token sequence, same starting state, two different final answers. The selective rule's answer is exactly the sequence's last real value; the fixed rule's is a decayed, blended approximation of it — contaminated by a signal from four steps earlier that the fixed rule had no way to just drop.

Worked example

Sequence x=(5, 0, 0, 0, 3)x = (5,\ 0,\ 0,\ 0,\ {-}3) — one signal, three filler tokens, a second signal:

  1. The first signal writes the state

    x1=5x_1=5 is signal, so A=0,B=1A{=}0, B{=}1: h1=0(0)+1(5)=5h_1 = 0(0) + 1(5) = 5.

  2. Three filler tokens change nothing

    x2=x3=x4=0x_2{=}x_3{=}x_4{=}0, each filler: A=1,B=0A{=}1,B{=}0 gives ht=1(ht1)+0=ht1h_t = 1(h_{t-1}) + 0 = h_{t-1} every time. h2=h3=h4=5h_2=h_3=h_4=5 — the exact same number, three steps running.

  3. The second signal overwrites it completely

    x5=3x_5={-}3 is signal again: h5=0(5)+1(3)=3h_5 = 0(5) + 1({-}3) = -3. The 55 isn't faded — it's gone, replaced outright, in exactly the step where replacing it was the right call.

Checkpoint

Find the timestep, among the seven, where the fixed and selective models' states diverge the most.

Pick a timestep to try it
Summary
ht=A(xt)ht1+B(xt)xth_t = A(x_t)\,h_{t-1} + B(x_t)\,x_t

"Forget on purpose" cuts both ways: the selective rule forgets nothing on filler and forgets everything on signal — both are choices, made per token, instead of one fixed rate applied uniformly. Real Mamba layers learn AA, BB, and the discretization step size as smooth functions of the input (not this chapter's hard on/off switch), so selection can be partial and learned rather than all-or- nothing. This closes out the mechanism side of the part. The last chapter puts a Transformer and a Mamba-style model side by side on one long sequence and watches the cost and memory gap this whole part has been building actually show up.