Hybrid Attention and State Space Models › Two Kinds of Memory

Michael BrenndoerferNovember 17, 20265 min read
Language AI HandbookAll chapters
Part 19 of 67Alternative Sequence and Generative Architectures
Chapter 134Hybrid Attention and State Space Models › Two Kinds of Memory
Reading section 2 of 12 · Two Kinds of Memory · 5 minSection 2 of 12 · 5 min

Section 2 of 12 · 5 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

The previous chapter, State Space Models for Language, developed selective state-space layers. For one channel of a diagonal model, let xtx_t be the scalar input and ht∈RNh_t \in \mathbb{R}^N the updated state. The discretized transition aˉt\bar a_t and write vector bˉt\bar b_t each have NN components. The read vector ct∈RNc_t \in \mathbb{R}^N turns the updated state into the scalar readout yty_t.

ht=aˉt⊙ht−1+bˉtxt,yt=ct⊤ht.h_t = \bar a_t \odot h_{t-1} + \bar b_t x_t, \qquad y_t = c_t^\top h_t.

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 aˉt\bar a_t and also affects the discrete write bˉt\bar b_t. The scalar xtx_t 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 tt, use the score and value from position t′t' when t′≤tt'\le t. Scaling by the square root of the key dimension controls the size of the scores before softmax.

yt=∑t′≤texp⁡(qt⋅kt′/d)∑s≤texp⁡(qt⋅ks/d)⏟wt,t′ vt′y_t = \sum_{t' \le t} \underbrace{\frac{\exp(q_t \cdot k_{t'} / \sqrt{d})}{\sum_{s \le t} \exp(q_t \cdot k_s / \sqrt{d})}}_{w_{t,t'}} \, v_{t'}

where:

  • qtq_t: the query vector at position tt
  • kt′k_{t'}: the key vector at position t′t'
  • vt′v_{t'}: the value vector at position t′t'
  • dd: the key/query dimension, used in the d\sqrt{d} scaling
  • wt,t′w_{t,t'}: the attention weight from position tt to position t′t', normalized over all s≤ts \le t
  • yty_t: the attention output at position tt

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.

Attention-to-state-space ratio

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 SS state elements of 16 bits each, including all input-dependent state that will affect later outputs. There are at most 216S2^{16S} 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 tt, either model can predict token t+1t+1 using inputs through position tt.
  • Mixer accounting. At fixed widths, full attention contributes O(L2)O(L^2) prefill work per layer and an O(L)O(L) cache. A state-space layer contributes O(L)O(L) prefill work and a decode state whose size is independent of LL. 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!

Newsletter

Stay up to date

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

Unsubscribe at any time. Privacy policy