Looking for the lab runner…

Hands-on lab 07 of 10 · Mamba selective scan

Mamba's selective scan: sequential, parallel and recurrent

Implement the core of a Mamba layer, the selective state-space scan, three ways that must agree:

Time: 60-90 minCheck: python labs/check.py 7

The maths, step by step

b batch, L sequence length, D channels, N state size per channel. Every channel d runs its own little linear system with an N-dim state.

Inputs   x [b, L, D]       the sequence (the paper's x_t; mamba.py calls it u)
         delta [b, L, D]   step size, > 0, per token and channel (input-dependent:
                           delta = softplus(Linear(x)) in Mamba)
         A [D, N]          learned, negative (decay rates)
         B [b, L, N]       input → state, per token (input-dependent)
         C [b, L, N]       state → output, per token (input-dependent)
         D_skip [D]        direct skip connection (optional)
  1. 1
    Discretize (continuous system → one update per token):
    A_bar[t, d, n] = exp(delta[t, d] * A[d, n])            in (0, 1): how much to keep
    B_bar[t, d, n] = delta[t, d] * B[t, n]                  ("simple", what Mamba's kernel uses)
    or, exact zero-order hold for diagonal A:
    B_bar[t, d, n] = (exp(delta A) - 1) / A * B[t, n]       ("zoh"; equals the simple rule
                                                            to first order in delta)

    Large delta: forget the past, write the input. Small delta: keep the state.

  2. 2
    Recurrence (h_0 = 0, or a given h0):
    h_t = A_bar_t * h_{t-1} + B_bar_t * x_t               h: [b, D, N]
    y_t = sum_n C_t[n] * h_t[:, :, n]  + D_skip * x_t      y: [b, D]
  3. 3
    Each step is an affine map h → a * h + c with a = A_bar_t, c = B_bar_t x_t.

    Doing step 1 then step 2 is again affine:

    (a2, c2) o (a1, c1) = (a2 * a1,  a2 * c1 + c2)

    This combine is associative, so all prefixes h_1..h_L can be computed in ceil(log2 L) rounds (Hillis-Steele): in round r, every position t combines with position t - 2^r. After the last round position t holds the composition of steps 1..t, whose c part is h_t (because h_0 = 0).

  4. 4

    Generation: carry only h [b, D, N] (constant size, no KV cache) and apply the recurrence once per new token.

Worked numbers (A = -1, B = 1): delta = 0.1 → A_bar = 0.905; delta = 5 → 0.0067.

Read alongside

How to work on this page

  1. Write each function in its cell, in order. The hints open one at a time: try Hint 1 first.
  2. ▶ Run (Ctrl/⌘+Enter) defines the cell in this lab's Python process; variables persist between cells, as in a notebook. Try your functions in the scratch cell at the bottom.
  3. ✓ Run checks saves the file and runs python labs/check.py 7: each step turns from TODO to PASS. A FAIL shows what was expected.
  4. Save (Ctrl/⌘+S) writes all cells to labs/lab07_mamba_scan.py (the previous version is kept in labs/.backup/), so you can also continue in any editor. After ↺ Reset kernel, press ▶ Run all.

Editor keys: Tab / Shift+Tab indent and dedent by 4 spaces, Enter keeps the indentation, Shift+Enter runs the cell and moves to the next one.

Cells on this page:

0 Imports

Imports

run first

The lab file's imports. Run this cell first (or press Run all).

1 discretization

discretize(delta, A, B, method='simple')

to write

Args:

delta: [b, L, D]   A: [D, N]   B: [b, L, N]
method: "simple" (B_bar = delta * B) or "zoh" (B_bar = (exp(delta A) - 1) / A * B)

Returns:

A_bar, B_bar: each [b, L, D, N]
Hint 1
line the shapes up for broadcasting: delta[..., None] is [b, L, D, 1], A is [D, N], B[:, :, None, :] is [b, L, 1, N].
Hint 2
dA = delta[..., None] * A; A_bar = torch.exp(dA)
Hint 3
simple: B_bar = delta[..., None] * B[:, :, None, :] zoh: B_bar = (A_bar - 1) / A * B[:, :, None, :]

2 the sequential scan (the definition)

selective_scan_sequential(x, delta, A, B, C, D_skip=None, h0=None)

to write

Loop over t: h_t = A_bar_t * h_{t-1} + B_bar_t * x_t; y_t = <C_t, h_t> + D_skip * x_t.

Args:

x, delta: [b, L, D]   A: [D, N]   B, C: [b, L, N]   D_skip: [D] or None
h0: initial state [b, D, N] or None (zeros)

Returns:

y:      [b, L, D]
h_last: [b, D, N]   the state after the last token (lets you continue later)

Use discretize(..., method="simple").

Hint 1
A_bar, B_bar = discretize(delta, A, B); h starts as zeros [b, D, N] (or h0).
Hint 2
inside the loop: h = A_bar[:, t] * h + B_bar[:, t] * x[:, t, :, None]
Hint 3
y_t = (h * C[:, t, None, :]).sum(-1) # [b, D]; stack the y_t on dim 1, then add D_skip * x if D_skip is not None.

3 the associative combine and the parallel scan

scan_combine(a1, c1, a2, c2)

to write

Compose two affine steps: first (a1, c1), then (a2, c2).

Applying the result to h must equal a2 * (a1 * h + c1) + c2. Returns (a, c), elementwise.

Hint 1
expand a2 * (a1 * h + c1) + c2 and read off the factor of h and the constant.

parallel_scan(a, c)

to write

All prefixes of h_t = a_t * h_{t-1} + c_t with h_0 = 0, in O(log L) rounds.

a, c: [b, L, ...] (time is dim 1). Returns h: [b, L, ...] with h[:, t] = h_{t+1} in 1-based maths, i.e. the state AFTER processing element t. Do not loop over L; loop over the rounds offset = 1, 2, 4, ... < L.

Hint 1
in each round every position t >= offset combines with position t - offset, which comes EARLIER in time: (a, c)[t] = (a, c)[t-offset] then (a, c)[t].
Hint 2
positions t < offset combine with the identity step (a = 1, c = 0); pad with a_prev = torch.cat([torch.ones_like(a[:, :offset]), a[:, :-offset]], dim=1) and the same with zeros for c.
Hint 3
a, c = scan_combine(a_prev, c_prev, a, c); offset *= 2. At the end return c.

selective_scan_parallel(x, delta, A, B, C, D_skip=None)

to write

Same y as selective_scan_sequential (h0 = 0), via discretize + parallel_scan.

Hint 1
the affine step of token t is a = A_bar_t, c = B_bar_t * x_t ([b, L, D, N]).
Hint 2
h = parallel_scan(A_bar, B_bar * x[..., None]); y = (h * C[:, :, None, :]).sum(-1)

4 one recurrent step (generation)

recurrent_step(h, x_t, delta_t, A, B_t, C_t, D_skip=None)

to write

Process ONE token.

Args:

h: [b, D, N] state   x_t, delta_t: [b, D]   A: [D, N]   B_t, C_t: [b, N]

Returns:

y_t: [b, D],  h_new: [b, D, N]
Hint 1
the same three lines as one iteration of your sequential loop, with the time dim already removed: A_bar = exp(delta_t[..., None] * A), ...
Hint 2
h_new = A_bar * h + (delta_t[..., None] * B_t[:, None, :]) * x_t[..., None]
Hint 3
y_t = (h_new * C_t[:, None, :]).sum(-1) (+ D_skip * x_t)

✎ Scratch cell

Experiment here: print shapes, call your functions on small inputs. This cell runs in the same Python process as the cells above, is not part of Run all and is not saved to the lab file (your browser keeps it).