State-space dynamical systems #
State-space sequence models are recurrent dynamical systems:
$$ h_{t+1}=A_t h_t+B_t x_t,\qquad y_t=C_t h_t+D_t x_t. $$
The full S4/Mamba family uses structured matrices and input-dependent ("selective") parameters. This file starts with the diagonal/channelwise version because it is:
- the primitive implemented by a compact CUDA selective-scan kernel,
- enough to express the formal affine-scan algebra,
- and a useful educational baseline for examples.
The definitions reuse DynamicalSystem / DrivenSystem from
NN/Spec/Dynamics/System.lean, so existing notions such as iterate, trajectory,
IsFixedPoint, and isContractive apply immediately.
A diagonal state-space model with elementwise input, state, and output channels.
This is the smallest useful SSM shape: it captures the recurrent memory path used by Mamba/S4 while remaining easy to verify. Richer models can wrap this core with linear projections and gates.
- A : TorchLean.Tensor α [dim]
Elementwise recurrent multiplier. Stability usually requires $\lvert A_i\rvert<1$.
- B : TorchLean.Tensor α [dim]
Elementwise input-to-state gain.
- C : TorchLean.Tensor α [dim]
Elementwise state-to-output gain.
- D : TorchLean.Tensor α [dim]
Elementwise residual / skip gain.
Instances For
One recurrent state update $h'=A\odot h+B\odot x$.
Instances For
Readout $y=C\odot h+D\odot x$.
Instances For
Convert a token vector into the corresponding affine transition for the hidden state.
Instances For
Applying the affine transition generated by x is exactly the SSM state update.
Run a full input sequence and return all hidden states.
Instances For
Scanning no input produces no hidden states.
A diagonal SSM scan returns one hidden state for each input token.
View a scalar-specialized diagonal SSM recurrence as a DrivenSystem.
DrivenSystem uses TorchLean's default SpecScalar, while the reusable SSM record
above is polymorphic over scalar backends. This adapter is the bridge to the existing dynamics API.
Instances For
Viewing a diagonal SSM as a driven dynamical system does not change its step.
The wrapper exists so the dynamics theorems apply to the layer; this is the rfl that says the
wrapper carries no extra content, so a bound proved about the system holds of the layer.