spyx.ssm
Diagonal complex-valued state-space models driven by jax.lax.associative_scan — LRU, S4D/S5-style HiPPO-LegS initialisation, the selective (Mamba) SSM and full Mamba block, and a minimal H-Net-style ChunkedSSM skeleton.
State-space models (SSMs) for Spyx.
A first-pass implementation of diagonal complex-valued SSMs targeting the Flax NNX module system. This module focuses on the linear recurrence:
x_k = λ ⊙ x_{k-1} + B u_k
y_k = Re(C x_k) + D u_k
where λ is a diagonal complex decay. The recurrence is run with
:func:jax.lax.associative_scan for O(log T) parallel depth on accelerators.
Two layer classes are provided:
- :class:
LRU— Linear Recurrent Unit (Orvieto et al., 2023, arXiv 2303.06349). Stability-preserving radial/angular parameterisation; no HiPPO required. - :class:
S5Diag— a diagonal S4D / S5-style layer that initialises from the HiPPO-LegS eigenvalues so the layer can represent long-range dependencies out of the box.
Both compose with :class:spyx.nn.Sequential and can be quantized via
:mod:spyx.quant (see the BitNet helper for ternary SSM weights). A tiny
worked example lives in scripts/ssm_demo.py.
ChunkedSSM
Bases: Module
Hierarchical SSM stack — the structural skeleton of an H-Net.
Splits the input sequence into fixed chunks of chunk_size timesteps,
processes each chunk with an inner SSM (inner), pools the chunk to a
single vector, runs the sequence of chunk-vectors through an outer SSM
(outer), and up-samples the outer signal back into the chunk slots
via a learnable affine blend. This captures the H-Net idea — hierarchical
composition of SSMs at different temporal resolutions — without the
dynamic-chunking and byte-level specifics of the full Hwang et al. 2024
recipe, which are separate research pieces.
inner and outer can be any module whose __call__ takes
(T, B, d_model) and returns the same shape — for example
:class:LRU, :class:S5Diag, or :class:MambaBlock.
:chunk_size: number of timesteps per chunk. The input length must be a
multiple of this.
:pool: "mean" or "last" (last-timestep pooling is closer to the
H-Net's "segment-end" summary).
Source code in spyx/ssm.py
__call__(u)
u: (T, B, d_model) → (T, B, d_model), where T is divisible by chunk_size.
Source code in spyx/ssm.py
LRU
Bases: Module
Linear Recurrent Unit (Orvieto et al., 2023).
d_model is the input/output channel count; d_state is the diagonal
hidden-state size. The recurrence runs in fp32 arithmetic with complex64
state; gradients flow through both the radial (ν) and angular (θ)
parameterisations, so the stability constraint |λ| < 1 is enforced by
construction rather than clipping.
Source code in spyx/ssm.py
__call__(u)
Apply the SSM to a time-major input.
:u: real array of shape (T, B, d_model).
:return: real array of the same shape.
Source code in spyx/ssm.py
Mamba
Bases: Module
Selective state-space layer (Gu & Dao, 2023) — the SSM core of a Mamba block.
Implements the input-dependent (Δ, B, C) recurrence with a learned
diagonal A matrix, running the selective scan via
:func:jax.lax.associative_scan (O(log T) parallel depth). This is the
portable pure-JAX fallback for the selective_scan_cuda op in the
reference PyTorch implementation; it has the same semantics but lower
throughput on long sequences compared to the custom CUDA kernel.
Note: Mamba is the SSM subroutine. For the full block with the in-proj,
depthwise conv, SiLU gate and out-proj, use :class:MambaBlock.
Source code in spyx/ssm.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |
__call__(u)
Run the selective SSM.
:u: real array (T, B, d_inner).
:return: same shape.
Source code in spyx/ssm.py
MambaBlock
Bases: Module
Full Mamba block: in-proj → depthwise conv → SSM → gate → out-proj.
Residual connection is left to the caller (usually composed alongside an
RMSNorm inside a stack). The depthwise convolution uses
flax.nnx.Conv with feature_group_count = d_inner to mimic the
reference Mamba conv1d with groups = d_inner.
Source code in spyx/ssm.py
__call__(u)
u: (T, B, d_model) → (T, B, d_model).
Source code in spyx/ssm.py
S5Diag
Bases: Module
Diagonal S4D / S5-style layer with HiPPO-LegS initialisation.
Mechanically the same as :class:LRU but with (a) a continuous-time
eigenvalue prior (HiPPO-LegS) and (b) a learnable log-step log_dt that
controls the effective decay. This is the flavour that performs best on
long-range tasks in the S4/S5 papers.
Source code in spyx/ssm.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |