Initialise the SSM Parameters of a Mamba Block
A Mamba block stores its state matrix as A_log and its step size through the bias of dt_proj. Both are set by a fixed recipe at initialisation. Implement that recipe together with the two numerically stable helpers it needs.
softplus(x)
- Elementwise softplus(x)=log(1+ex) for a scalar or array, returned as a numpy array of floats. It must stay finite and accurate for large positive and large negative inputs (for example ±800).
inv_softplus(y)
- Elementwise inverse for y>0, so that
softplus(inv_softplus(y)) returns y. It must also stay finite for large y (for example 800.0).
mamba_ssm_init(ED, N, rng, dt_min=0.001, dt_max=0.1, dt_init_floor=1e-4)
"A_log": shape (ED, N), row-wise the S4D-Real initialisation: entry (c,n) is log(n+1) for n=0,…,N−1, the same for every channel c.
"A": shape (ED, N), A=−exp(Alog).
"D": shape (ED,), the skip parameter, all ones.
"delta": shape (ED,), sampled log-uniformly in [Δmin,Δmax]. Draw exactly one uniform vector u = rng.random(ED) (one value per channel, nothing else drawn from rng) and map it to
Δc=exp(uc(logΔmax−logΔmin)+logΔmin),
then clamp from below at dt_init_floor.
"dt_bias": shape (ED,), the inverse softplus of "delta", so that applying softplus to it returns delta exactly (up to round-off).
- Return the five arrays in a dict with exactly these keys.
Hints
- log(1+ex) can be rewritten as max(x,0)+log(1+e−∣x∣), which never exponentiates a large positive number.
- log(ey−1)=y+log(1−e−y), and
np.expm1 evaluates e−y−1 accurately.
- Sampling the exponent uniformly is not the same as sampling Δ uniformly: about half of a log-uniform sample on [0.001,0.1] lies below 0.01.