HiPPO State Space Model Effect: PyTorch Architecture & Implementation

Navigation:

Overview & Formula Token

The HiPPO effect is exposed in formulas via the hippo() token:

# Univariate HiPPO memory with translated Legendre (sliding window)
formula = "Y ~ hippo(temperature, k=16, measure='legt', dt=0.05)"

# Full-history scaled Legendre with ZOH discretization
formula = "Y ~ hippo(temperature, k=12, measure='legs', dt=0.02, discretization='zoh')"

# Combined with non-linear spline and tensor interaction
formula = "Y ~ s(temperature, k=10) + hippo(temperature, k=12) + te(hippo(temperature, k=8), c(day_type))"

Parameter Specification

Parameter

Type

Default

Description

feature_name

str

required

Primary feature column name.

k

int

16

State dimension / order of orthogonal polynomial projection \(N\).

measure

str

'legt'

HiPPO polynomial measure: 'legt' (sliding window), 'legs' (scale-invariant), or 'lagt' (Laguerre).

dt

float

0.05

Discretization time-step \(\Delta\).

discretization

str

'bilinear'

Discretization method: 'bilinear' (Tustin transform) or 'zoh' (Zero-Order Hold via Van Loan matrix exponential).

degree_pen

float

1.0

Polynomial Dirichlet degree penalty exponent \(p\) in \(\operatorname{diag}(1 + (n(n+1))^p)\).

ap

float

default_alpha_p

Log10 of the regularization parameter \(\lambda_p = 10^{\text{ap}}\).

extrapolate

str

'continue'

OOD extrapolation strategy beyond \([-1, 1]\).

Complexity & Performance

  • Memory Complexity: The feature map requires \(\mathcal{O}(T \cdot N)\) floats. The diagonal penalty matrix requires \(\mathcal{O}(N)\) storage.

  • Compute Complexity: Feature map evaluation executes in \(\mathcal{O}(N T \log T)\) via parallelized 1D causal zero-padded FFT convolutions (torch.fft.rfft / torch.fft.irfft) across all groups.

  • State Continuation: The terminal state \(s_{T-1} \in \mathbb{R}^{G \times N}\) is cached upon completion of training, allowing subsequent batch and online prediction frames to continue the recurrence smoothly without boundary resets.

State, Complexity and Limits

  • Class and file: HiPPOEffect, src/tam/model/spectrum/_hippo.py. Label: (BETA).

  • State: {"c": (B, k)}. Layout: (B, T) in, (B, T, k) out.

  • Time and memory: \(\mathcal{O}(B\,T\,k\log T)\) with the FFT convolution (passes of 64 steps or more) plus \(\mathcal{O}(T\,k^2)\) for the kernel; the loop is used below 64 steps.

  • Extrapolation: 'linear' and 'saturation' are refused at construction.

  • Limits: one input column; the kernel is built for the length of the pass, so very long passes (hundreds of thousands of steps) are better cut in chunks continued with continue_state=True.

  • Chunk equivalence bound: tests/spectrum/test_stateful_contract.py checks that the second of two chunks, started from the state of the first, equals the matching rows of the one-pass features within atol=1e-8 (float64), and that every group computed alone equals the same group inside a batch within atol=1e-10.

Code

Feature map of HiPPOEffect (build_feature_map):

    def build_feature_map(self, x_col: torch.Tensor, state_in: Optional[Dict[str, Any]] = None) -> torch.Tensor:
        """Builds explicit SSM state basis matrix: ``(B, T)`` in, ``(B, T, k)`` out; each group has its own memory ``{"c": (B, k)}``."""
        x3, shape, layout = to_btd(x_col, False)
        if x3.shape[-1] != 1:
            raise ValueError(f"hippo reads one column, got {x3.shape[-1]}")
        x_2d = x3[..., 0]
        B = x_2d.shape[0]
        init_s = None
        if state_in is not None and "c" in state_in:
            init_s = state_in["c"].to(device=x_2d.device, dtype=x_2d.dtype)
            if tuple(init_s.shape) != (B, self.k):
                raise ValueError(f"state 'c' has shape {tuple(init_s.shape)}, expected {(B, self.k)}: continue_state needs the same groups and features as training")
        states = self._compute_ssm_states(x_2d, initial_state=init_s)
        self._state_out = {"c": states[:, -1, :].detach().clone()}
        return from_btk(states, shape, layout)

Penalty of HiPPOEffect (build_penalty_matrix):

    def build_penalty_matrix(self) -> torch.Tensor:
        """Builds diagonal Legendre polynomial degree penalty matrix."""
        n_idx = torch.arange(self.k, device=TORCH_DEVICE, dtype=torch.get_default_dtype())
        dirichlet_energy = (n_idx * (n_idx + 1.0)) ** self.degree_pen
        diag = self.lambda_p * (1.0 + dirichlet_energy)
        return torch.diag(diag)

Parsing of the hippo(...) token in the factory:

        elif ttype == 'hippo':
            k = int(params_resolved.get('k', 16))
            measure = str(params_resolved.get('measure', 'legt')).lower().strip()
            dt = float(params_resolved.get('dt', 0.05))
            discretization = str(params_resolved.get('discretization', 'bilinear')).lower().strip()
            degree_pen = float(params_resolved.get('degree_pen', 1.0))
            extrap_val = params_resolved.get('extrapolate', 'continue')

            effects_list.append(HiPPOEffect(
                feature_name=feature_name,
                k=k,
                measure=measure,
                dt=dt,
                discretization=discretization,
                degree_pen=degree_pen,
                lambda_p=lambda_p,
                extrapolate=extrap_val
            ))