Hybrid Attention and State Space Models › Two Kinds of Memory
Language AI HandbookAll chapters The previous chapter, State Space Models for Language, developed selective state-space layers. For one channel of a diagonal model, let be the scalar input and the updated state. The discretized transition and write vector each have components. The read vector turns the updated state into the scalar readout .
In Mamba-1, the full input vector to the state-space computation supplies the projections for the step size, write and read coefficients at each position. The step size for this channel converts its learned continuous rates into the discrete multipliers and also affects the discrete write . The scalar in the equation is this channel's input. The equation gives the state readout within the mixer. At fixed channel count and state size, the number of stored recurrent-state elements stays fixed.
When a decoder processes the next token, that update uses the immediately preceding state. The state is a function of the prefix, but it gives no separate storage slot for position 3 or any other earlier position. Its fixed shape is what makes a recurrent update possible at a cost independent of the prefix length.
Gu and Dao, Mamba, Algorithm 2 and Section 3.3
A full-context attention head scores each available key with its query and uses the weights to combine the corresponding values. At position , use the score and value from position when . Scaling by the square root of the key dimension controls the size of the scores before softmax.
where:
- : the query vector at position
- : the key vector at position
- : the value vector at position
- : the key/query dimension, used in the scaling
- : the attention weight from position to position , normalized over all
- : the attention output at position
At decode time, the layer can retain its projected key and value vectors for each prefix position. The query scores the cached keys and the output combines the cached values. A position can receive a large attention weight, and the retrieved value remains a learned representation of that position.
The number of attention layers relative to the number of state space layers in a hybrid stack, usually written as a pair such as 1:7. It is a design hyperparameter, comparable to depth or width, and its best value depends on the model scale, the token budget, the data, and the workload the model will serve. It is not a property of either layer type.
What the bottleneck costs
Suppose a recurrent decoder carries state elements of 16 bits each, including all input-dependent state that will affect later outputs. There are at most stored bit patterns. If there are more candidate prefixes of the same length than stored patterns, at least two prefixes must map to the same state. At the same position with the same next input, a deterministic recurrence and readout produce the same result for those prefixes.
An update can merge prefixes even when their number is below the upper bound. Identical subsequent inputs keep the states equal. The example below shows a collision with four positions. Attention preserves a separate projected representation for each retained position.
Arora et al. introduced multi-query associative recall, in which the input contains key–value pairs and later queries ask for values bound to keys. Jelassi et al. studied exact string copying and length generalization. Those diagnostics ask whether a model can retain and use particular pieces of a prefix. The comparison depends on the models, training data, and task, including how many distractors an example contains.
Use exact retrieval tests when an application needs to reproduce particular values or strings. Perplexity on natural text averages a different kind of error and can conceal a failure on a small set of long-distance dependencies.
Why the two combine well
In this stack, selective state-space layers summarize a prefix in recurrent state and attention layers can consult representations of individual positions. Both mixers fit the same residual-stream shape.
- Residual stream. Each mixer reads the stream, produces an update of the same shape, and adds it back. Each layer keeps its own projections, normalization, and decode state. Residual Connections describes the shared residual path.
- Causal prediction. The recurrence and the attention mask both limit the mixer to the available prefix. A hybrid and a causal Transformer can use the same next-token objective. At position , either model can predict token using inputs through position .
- Mixer accounting. At fixed widths, full attention contributes prefill work per layer and an cache. A state-space layer contributes prefill work and a decode state whose size is independent of . Sum these operations or stored bytes across the layers using the corresponding per-layer estimates.
Replacing an attention layer removes its prefill and KV-cache terms and adds the recurrent layer's work and fixed decode state. The net change depends on the sequence length and both layers' dimensions.
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!