Imports
run firstThe lab file's imports. Run this cell first (or press Run all).
Hands-on lab 07 of 10 · Mamba selective scan
Implement the core of a Mamba layer, the selective state-space scan, three ways that must agree:
O(log L) rounds (how training runs),in_proj, conv, gating) are ordinary layers and are left out.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)
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.
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]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).
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.
selective_scan_reference / selective_scan_parallelEditor 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:
The lab file's imports. Run this cell first (or press Run all).
discretize(delta, A, B, method='simple')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]selective_scan_sequential(x, delta, A, B, C, D_skip=None, h0=None)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").
scan_combine(a1, c1, a2, c2)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.
parallel_scan(a, c)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.
selective_scan_parallel(x, delta, A, B, C, D_skip=None)Same y as selective_scan_sequential (h0 = 0), via discretize + parallel_scan.
recurrent_step(h, x_t, delta_t, A, B_t, C_t, D_skip=None)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]
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).