State Space Models for Language › Mamba-2 and Structured State Space Duality
Language AI HandbookAll chapters Mamba-2 uses a structured state space duality (SSD) layer. The discrete transition for a head is , 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 denote the discrete write vector at input position and the read vector at output position . The sequence mixing matrix for fixed coefficients has entries
The product equals one when . If the discrete inputs in that channel form the vector , the outputs are before any direct path. The inner product 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 and outputs , for a time boundary after position . Split each decay product at . The factors through carry the write at into the state . The later factors propagate that state to the readout at . The entire block factors through the coordinates of , so its rank is at most . 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 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 positions, the mixing matrix has entries. The code allocates a second array for decay products. The decay loop visits factors, since the product for indices has factors. SSD's chunked algorithm applies the mixing through within-chunk products and recurrent boundary states.
PythonIn [7]Expand
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]Expand

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!