State Space Models for Language › Mamba-2 and Structured State Space Duality

Michael BrenndoerferNovember 17, 20263 min read
Language AI HandbookAll chapters
Part 19 of 67Alternative Sequence and Generative Architectures
Chapter 133State Space Models for Language › Mamba-2 and Structured State Space Duality
Reading section 8 of 13 · Mamba-2 and Structured State Space Duality · 3 minSection 8 of 13 · 3 min

Section 8 of 13 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Mamba-2 uses a structured state space duality (SSD) layer. The discrete transition for a head is Ak=akIA_k=a_k I, so each state coordinate within that head has the same multiplier. Suppress the head index and look at one value channel with zero initial state. Let BjB_j denote the discrete write vector at input position jj and CiC_i the read vector at output position ii. The sequence mixing matrix for fixed coefficients has entries

Mij={Ci⊤Bj∏k=j+1iak,i≥j,0,i<j.M_{ij} = \begin{cases} C_i^\top B_j\displaystyle\prod_{k=j+1}^i a_k, & i\ge j,\\ 0, & i< j. \end{cases}

The product equals one when i=ji=j. If the discrete inputs in that channel form the vector uu, the outputs are MuMu before any direct path. The inner product Ci⊤BjC_i^\top B_j resembles a query-key score. SSD multiplies this score by a causal mask of decay products without softmax normalization. In standard scaled dot-product attention, softmax normalizes the scaled scores over each query's allowed positions.

Consider the block of entries with inputs j≤bj\le b and outputs i>bi>b, for a time boundary after position bb. Split each decay product at bb. The factors through bb carry the write at jj into the state xbx_b. The later factors propagate that state to the readout at ii. The entire block factors through the NN coordinates of xbx_b, so its rank is at most NN. SSD uses this semiseparable structure to apply matrix multiplications within chunks and carry state summaries between chunks. The resulting implementation can use accelerator matrix multiplication units. Mamba-2 also uses a larger head dimension PP than Mamba-1's single value channel per head. Dao and Gu, sections 2.4, 3.2 and 6.

For the plot, sample the write and read vectors for a synthetic sequence with 32 positions and state size four. Build its dense mixing matrix directly from the positive decay factors. For LL positions, the mixing matrix has L2L^2 entries. The code allocates a second L2L^2 array for decay products. The decay loop visits (L3−L)/6(L^3-L)/6 factors, since the product for indices (i,j)(i,j) has i−ji-j factors. SSD's chunked algorithm applies the mixing through within-chunk products and recurrent boundary states.

PythonIn [7]
CollapseExpand
import numpy as np

ssd_L = 32
ssd_N = 4
ssd_rng = np.random.default_rng(7)

ssd_C = ssd_rng.normal(size=(ssd_L, ssd_N))
ssd_B = ssd_rng.normal(size=(ssd_N, ssd_L))
ssd_step_decay = 0.90 + 0.07 * np.sin(np.linspace(0.0, 3.0 * np.pi, ssd_L))

ssd_scale = np.ones((ssd_L, ssd_L))
for i in range(ssd_L):
    for j in range(i):
        ssd_scale[i, j] = np.prod(ssd_step_decay[j + 1 : i + 1])

ssd_matrix = np.zeros((ssd_L, ssd_L))
for i in range(ssd_L):
    ssd_matrix[i, : i + 1] = ssd_C[i] @ (
        ssd_scale[i, : i + 1] * ssd_B[:, : i + 1]
    )

ssd_abs_max = np.abs(ssd_matrix).max()
ssd_diagonal = np.array([0, ssd_L - 1])
VisualizationOut [8]
CollapseExpand
Heatmap with input position on the horizontal axis and output position increasing upward. The area below the dashed diagonal is zero.
Synthetic SSD mixing matrix for 32 positions and four state coordinates. Future input positions have zero weight. Since output indices increase upward, those entries appear below the dashed diagonal.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy