TorchLean API

NN.Spec.Dynamics.StateSpace

State-space dynamical systems #

State-space sequence models are recurrent dynamical systems:

h_{t+1} = A_t h_t + B_t x_t, 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 definitions intentionally reuse DynamicalSystem / DrivenSystem from NN/Spec/Dynamics/System.lean, so existing notions such as iterate, trajectory, IsFixedPoint, and isContractive apply immediately.

structure NN.Spec.Dynamics.DiagonalSSM (α : Type) (dim : ) :

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.

Instances For

    One recurrent state update h' = A ⊙ h + B ⊙ x.

    Instances For

      Readout y = C ⊙ h + D ⊙ x.

      Instances For

        Convert a token vector into the corresponding affine transition for the hidden state.

        Instances For
          @[simp]

          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
            @[simp]
            theorem NN.Spec.Dynamics.DiagonalSSM.scan_nil {α : Type} [Add α] [Mul α] {dim : } (m : DiagonalSSM α dim) (h0 : Spec.Tensor α (Spec.Shape.dim dim Spec.Shape.scalar)) :
            m.scan h0 [] = []
            @[simp]
            theorem NN.Spec.Dynamics.DiagonalSSM.scan_cons {α : Type} [Add α] [Mul α] {dim : } (m : DiagonalSSM α dim) (h0 x : Spec.Tensor α (Spec.Shape.dim dim Spec.Shape.scalar)) (xs : List (Spec.Tensor α (Spec.Shape.dim dim Spec.Shape.scalar))) :
            m.scan h0 (x :: xs) = m.step h0 x :: m.scan (m.step h0 x) xs

            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