Verify the Gating Connection Numerically
Theorem 1 of the Mamba paper says that a selective SSM with one state, A=−1, B=1, and Δt=softplus(Linear(xt)), discretised with the zero-order hold, is the gated recurrence
gt=σ(wxt+b),ht=(1−gt)ht−1+gtxt.
Implement both sides and compare them.
softplus(z) and sigmoid(z): elementwise, numerically stable for ∣z∣ up to at least 800.
selective_ssm_scalar(x, w, b, A=-1.0, B=1.0, C=1.0, h0=0.0)
x has shape (L,) or (L, ch); w and b are scalars or arrays broadcastable to one row of x (one value per channel); h0 is the state before the first token (scalar or per channel).
- For each t: Δt=softplus(wxt+b), then the exact zero-order hold for a scalar A<0:
Aˉt=eΔtA,Bˉt=AeΔtA−1B,ht=Aˉtht−1+Bˉtxt,yt=Cht.
- Return
(y, delta), both arrays with the shape of x.
gated_rnn(x, w, b, h0=0.0)
- The gated recurrence above with the same shape and broadcasting rules. Return
(h, g), both with the shape of x.
theorem1_check(x, w, b, h0=0.0)
- Run
selective_ssm_scalar with A=−1,B=1,C=1 and gated_rnn on the same inputs. Return a dict with "ssm" (the SSM trajectory y), "rnn" (the RNN trajectory), "gates" (g), "delta" (Δ) as numpy arrays, and "max_abs_diff", the largest absolute difference between the two trajectories as a Python float.
Hints
- e−softplus(z)=1+ez1; write out both sides of the theorem with that identity before coding.
np.expm1 computes ev−1 accurately when v is tiny, which happens when Δt is close to zero.
- The first-order shortcut Bˉ≈ΔB is not exact here: it changes the trajectory and breaks the equality.