State Space Models for Language › Mamba and Input-Dependent Selection
Language AI HandbookAll chapters The fixed S4 kernel gives two inputs at the same lag the same weight before reading their values. An input-conditioned update gives the layer a second way to distinguish them. The Mamba paper motivates this with a selective copying task. The sequence places relevant symbols among distractors with variable spacing, so the location of the information that needs to survive changes from one example to the next.
The Three Projections
In Mamba-1's selective layer, let be the channel vector being processed at position . Three learned projections of that vector give a step size for each channel, a write vector, and a read vector. A simplified expression is
Here , , and is a learned bias. The projections are shared across positions. Mamba-1 shares the resulting and vectors across the channels. Each channel has its own learned diagonal continuous-time rates in , which are shared across positions. The input-dependent step size makes the discrete transition vary by position. The reference block implements the step-size projection through a low-rank intermediate. Mamba, Algorithm 2 and section 3.6. Mamba-1 reference block, v2.2.4.
For a negative rate in a particular channel, is the multiplier on the previous state. A small positive brings it close to one. A larger makes the multiplier smaller, so less of the old state carries through. The step size also affects the write strength. controls which coordinates receive the input, and controls which coordinates the readout uses.
The Recurrence
For one channel, write its scalar input as and its state as . The channel index is suppressed in the step size and the transition matrix. After discretization the computation takes the form
After computing the coefficients for a given input sequence, the write at position contributes to . For a coordinate with continuous rate , the intervening multipliers are . When , the product has no factors and equals one. The readout then takes the dot product of with the state. Editing an input can change both its own write and the transition that carries earlier writes forward.
Once those coefficients have been computed, the update is affine in the previous state. The full map from an input sequence to its outputs is generally nonlinear because the coefficients themselves depend on the input. A different input sequence can have a different matrix of lag interactions, so the fixed-kernel convolution from the LTI case gives way to a scan.
The Reference Implementation's Discretization
The Mamba v2.2.4 selective scan reference computes the transition with the same exponential as exact ZOH and uses a first-order approximation for the write. In one channel, for state coordinate , the code implements the following recurrence.
The exact held-input term uses as its coefficient on for . Expanding the exponential around gives for the fraction at fixed . This is why the implementation's coefficient agrees through first order. In a numerical replication, use the discrete update from the code version you intend to match.
The Affine Map and Associative Composition
For scan, represent one update by a pair , where is the transition and is the input contribution. The pair acts on a state as . Applying update and then update gives
The whole segment has the same form as a single update, with transition and write term . Define composition with the later pair on the left.
Composition is associative in exact arithmetic since it is just composition of the represented affine maps. It is generally order-sensitive. The identity pair leaves a state unchanged. A prefix scan can group the compositions in a tree while preserving the order. Applying the resulting pair for each prefix to gives the state at the end of that prefix.
With diagonal transitions, each product of matrices becomes an elementwise product of their diagonals. Each pair composition uses arithmetic for one channel. A dense implementation can still use the same pair formula but composes its dense transitions with matrix multiplication.
Work and Depth of a Scan
Keep the state width fixed and count each affine composition as one unit of work. Span counts levels along the longest chain of dependencies. The plot shows the levels used by each schedule, including any wait between phases. The sequential baseline starts from the identity pair and composes one new pair into the running prefix at each position. Under this convention it uses compositions in levels.
- Hillis–Steele. In round , positions with a predecessor places away combine the two ordered results from the previous round. Round starts at offset one. Over rounds, work is and span is .
- Blelloch. An up-sweep combines each adjacent pair of subtrees. A down-sweep distributes the prefix to each position. On a power-of-two input, the exclusive scan schedules the sweeps in order, each with compositions and levels. An exclusive prefix omits the current update. Composing it with that update yields the inclusive prefix and adds at most compositions on one more level. The work stays and the span stays .
For a power-of-two , there are Hillis–Steele compositions in round , which sum to . The plot counts operations and levels in these schedules. Elapsed time also depends on how hardware schedules the work and moves intermediate arrays.
PythonIn [5]Expand
scan_lengths = np.array([2**k for k in range(4, 19)], dtype=float)
hillis_steele_levels = np.log2(scan_lengths)
hillis_steele_work = scan_lengths * hillis_steele_levels - scan_lengths + 1
blelloch_levels = 2.0 * hillis_steele_levels
blelloch_work = 2.0 * (scan_lengths - 1)VisualizationOut [6]Expand

One curve shows both sequential counts. The number of scheduled levels grows logarithmically for both parallel scans. Hillis–Steele revisits overlapping prefixes in each round, while Blelloch uses a two-sweep tree and does less work as sequence length grows.
A Teaching Reference and a Fused Kernel
A NumPy loop can implement the recurrence exactly as written and makes a useful reference for checking a faster implementation. Accelerator kernels also need to control which intermediate arrays they write to high-bandwidth memory (HBM).
An implementation that keeps every intermediate state builds an array of shape for batch size , channel width , sequence length , and state dimension . At , , , and , one such float32 array holds scalars. At four bytes each, it occupies bytes, or GiB.
Mamba-1's fused design combines discretization, an associative scan, and readout in the same kernel. It works on chunks in fast on-chip memory and carries the scan state across chunk boundaries. The forward implementation can thereby avoid saving the full expanded state array to HBM. Recomputing intermediates in the backward pass reduces the amount that must be saved for differentiation. Compared with an implementation that writes the full state trajectory to HBM, the fused forward pass avoids that write. The backward pass trades some saved intermediates for recomputation. Both evaluate the same recurrence, subject to rounding differences from arithmetic order or precision. Mamba, section 3.3 and Appendix D.
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!