Simple Algorithm
Deep Learning · Emergence · 28 min

Hybrid Models & the Limits of Fixed-Size Memory

Why recall needs attention, and architectures that mix it with recurrence.

A transformer generating its 100,000th token consults a cache of 100,000 keys and values. A state space model consults a state of fixed size, the same at token 10 as at token 10 million. That difference has a price, and theorems can measure it: a fixed state cannot copy or recall beyond its capacity, and trained models fall short of even that. This lesson proves the limits, shows them in trained models, and examines the hybrids that keep a little attention to pay for them.

What you’ll learn

  • Compare the inference memory of a KV cache with that of a fixed recurrent state, and state the trade-off between state size and recall.
  • Prove that a model with a state of bb bits fails to copy uniformly random strings of more than about b/log⁡2∣V∣b/\log_2|\mathcal V| tokens, and sketch why two attention layers copy strings exponentially long in their width.
  • Define multi-query associative recall, derive the linear-in-NN lower bound on state size for exact recall, and connect recall to the perplexity gap between attention and efficient models.
  • Describe hybrid architectures (Griffin, Jamba, Mamba-2-Hybrid), what controlled comparisons show about how much attention they need, and what remains open, including state tracking.

Two kinds of memory

To generate token t+1t+1, a transformer reads its KV cache: the keys and values of all tt earlier tokens in every layer. Its memory and its cost per token grow with tt. A recurrent model (an LSTM, a state space model, linear attention) compresses the past into a state whose size is fixed in advance, so each token costs the same and memory never grows. Linear attention, for instance, keeps a matrix St=St−1+vtkt⊤S_t=S_{t-1}+\mathbf v_t\mathbf k_t^\top and reads StqtS_t\mathbf q_t, and delta-rule variants overwrite old associations instead of adding to them (Fast Weights & Test-Time Memory). For this lesson they are all the same kind of object: a fixed-size summary updated token by token.

Predict before you look

A transformer has 32 layers of width 4,096 and stores keys and values in 2-byte floats, with no sharing of keys and values across heads. How many gigabytes does its KV cache need for a context of 100,000 tokens?

Show the answer

52.4 GB

Per token: 2 (keys and values)×32×4,096×22\,(\text{keys and values})\times32\times4{,}096\times2 bytes =524,288=524{,}288 bytes, about 0.520.52 MB; times 100,000100{,}000 tokens, about 5252 GB. A Mamba-style model with 64 blocks of inner width 8,192 and state size 16 keeps about 64×8,192×16≈8.464\times8{,}192\times16\approx8.4 million state numbers, plus small convolution buffers: roughly 20 MB in the same 2-byte floats, at any context length.

The same arithmetic appears in Jamba's report (Lieber et al., 2024): at a context of 256,000 tokens in 16-bit precision, a 7-billion-parameter transformer (Llama-2) needs a 128 GB cache, while Jamba, with attention in one layer out of eight, needs 4 GB. The question is what the fixed state gives up. Arora, Eyuboglu, Zhang et al. (2024) measured it by plotting models' state size against their accuracy at recalling information from the context: the models fall along a trade-off curve, with attention at one end (largest state, best recall) and small recurrent states at the other.

Copying needs memory

The copy task feeds ⟨bos⟩,x1,…,xL,⟨copy⟩\langle\text{bos}\rangle,x_1,\dots,x_L,\langle\text{copy}\rangle, with xx drawn uniformly from VL\mathcal V^L (write D=∣V∣D=|\mathcal V|), and asks for x1,…,xLx_1,\dots,x_L back. It is the simplest task in which the answer is the whole input.

DerivationProof of (a), and the idea of (b)
  1. Pr⁡[correct]≤∣S∣DL⟹err≥1−∣S∣DL\Pr[\text{correct}]\le\frac{|\mathcal S|}{D^L}\qquad\Longrightarrow\qquad\mathrm{err}\ge1-\frac{|\mathcal S|}{D^L}

    After reading ⟨copy⟩\langle\text{copy}\rangle the model's entire future output is a function of its state ss, since no further input arrives. So at most ∣S∣|\mathcal S| distinct output strings are possible, each correct for at most one input, and the DLD^L inputs are equally likely:

  2. mem⁡(S)<Llog⁡2D−1⟹err>12\operatorname{mem}(\mathcal S)<L\log_2D-1\quad\Longrightarrow\quad\mathrm{err}>\tfrac12

    If log⁡2∣S∣<Llog⁡2D−1\log_2|\mathcal S|<L\log_2D-1, then ∣S∣/DL<12|\mathcal S|/D^L<\tfrac12 and the error exceeds one half. ■\blacksquare The bound is information-theoretic, so it holds however the model was trained and however expensive its update is.

  3. pn-gram≤∑i<jPr⁡[xi:i+n=xj:j+n]<L2D−np_{n\text{-gram}}\le\sum_{i<j}\Pr\big[x_{i:i+n}=x_{j:j+n}\big]<L^2D^{-n}

    For (b), the transformer copies by lookup, like an induction head generalized to nn-grams. The first layer hashes each window of nn tokens into a key at its position; the second, given the last nn tokens produced, attends to the earlier position holding the same nn-gram and outputs the token that followed it. It errs only if some nn-gram occurs twice, and for uniform strings a union bound over pairs of positions gives

  4. transformer: width O(log⁡L), cache O~(L)fixed state: mem⁡≥Llog⁡2D−1 bits needed\text{transformer: width }\order{\log L},\ \text{cache }\tilde{\mathcal O}(L)\qquad\text{fixed state: }\operatorname{mem}\ge L\log_2D-1\ \text{bits needed}

    The transformer's parameters grow only logarithmically in LL, while its cache grows linearly, O~(L)\tilde{\mathcal O}(L) bits, which (a) shows is optimal up to logarithmic factors. Copying is cheap for attention because the input is kept and searched, not compressed.

Check your understanding

A recurrent model's state can take 210002^{1000} distinct values. It must copy uniformly random bit strings (D=2D=2). From which length LL on does the theorem guarantee an error above 12\tfrac12?

  1. L≥32L\ge32, since the state has 32-bit numbers
  2. L≥1,002L\ge1{,}002
  3. L≥21000L\ge2^{1000}
  4. Never: copying needs no memory if the model is deep enough
Show the answer

B. L≥1,002L\ge1{,}002

mem⁡(S)=1,000\operatorname{mem}(\mathcal S)=1{,}000 bits, and the condition 1,000<Llog⁡22−1=L−11{,}000<L\log_22-1=L-1 holds for L≥1,002L\ge1{,}002. Up to that length the counting bound allows success; real models fall short much earlier, as the experiments show.

The counting bound alone is loose: a large model's state could, in principle, hold millions of tokens. The experiments show that trained models fall far short of it. At about 160 million parameters, transformers learned to copy with far fewer training examples than state space models, generalized far better to strings longer than those seen in training, and, as tests on strings with deliberately repeated nn-grams showed, copied by nn-gram lookup much as in the construction. Among pretrained models evaluated without fine-tuning, Pythia transformers retrieved a phone number from a "phone book" in their context much more reliably than Mamba models of the same sizes, although the Mamba models had lower perplexity (Jelassi et al., 2024). Good average prediction and exact retrieval are different skills.

Associative recall: where the gap in perplexity comes from

Copying an entire input is extreme; language needs something milder but constant, recalling what was said earlier. Zoology (Arora, Eyuboglu, Timalsina et al., 2024) pretrained 17 language models with 70 million to 1.4 billion parameters and found gated-convolution models (H3, Hyena, RWKV) up to 2.1 perplexity points behind attention on the Pile. By their measure 82% of that gap came from tokens predictable by recalling an association made earlier in the same context ("Hakuna Matata means no worries … Hakuna Matata, it means no" → "worries"). On such tokens a 70-million-parameter attention model beat a 1.4-billion-parameter gated-convolution model.

In Zoology's two-layer experiments, attention solved MQAR at every sequence length with width 64, whereas gated convolutions reached accuracy above 0.9 only when their width was at least the sequence length. Attention can recall at constant width because its cache holds every pair and one head can match a query to its key, the induction-head mechanism of Pointer, Copy & Monotonic Attention. A recurrent model must instead have written every pair that might be queried into its state before any query arrives, and that costs bits:

DerivationProof sketch in a simple case
  1. answers=F(s, queries),s=state after the pairs\text{answers}=F\big(s,\ \text{queries}\big),\qquad s=\text{state after the pairs}

    Take KK pairs with distinct keys, each value drawn independently from cc symbols, followed by one query for each key, so N=Θ(K)N=\Theta(K). After the last pair, every later output depends on the pairs only through the state ss at that boundary.

  2. ∣S∣≥cK⟹mem⁡(S)≥Klog⁡2c=Ω(Nlog⁡c) bits|\mathcal S|\ge c^K\qquad\Longrightarrow\qquad\operatorname{mem}(\mathcal S)\ge K\log_2c=\Omega(N\log c)\ \text{bits}

    Two different assignments of values demand different answers to the same queries, so they must lead to different boundary states. The map from the cKc^K assignments to states is injective:

  3. fixed state of b bits⟹exact recall fails once Klog⁡2c>b\text{fixed state of }b\text{ bits}\quad\Longrightarrow\quad\text{exact recall fails once }K\log_2c>b

    A transformer pays the same bits, but in a cache that grows with the input rather than a state fixed in advance. Exact recall from contexts of unbounded length is therefore impossible for any fixed-state model; what designs can change is how close to the bound they come and how gracefully they degrade.

Arora et al. built Based to sit on the trade-off deliberately: linear attention, whose state size is set by its feature dimension, plus exact attention over a short sliding window. Varying the two traces the curve from small states to attention-like recall. At 1.3 billion parameters Based matched Mamba's perplexity and scored about 10 points higher on recall-intensive tasks.

State tracking is a different limit. Recurrence seems made for following a changing state, such as pieces on a chessboard or variables in a program. Yet Merrill, Petty and Sabharwal (2024) showed that S4, Mamba and similar models with logarithmic precision lie in the circuit class TC0\mathsf{TC}^0, as transformers do, so they cannot compose permutations of five elements, an NC1\mathsf{NC}^1-complete problem, unless TC0=NC1\mathsf{TC}^0=\mathsf{NC}^1, which is widely believed false. Nonnegative transitions are part of the cause: such linear recurrences cannot recognize parity at arbitrary lengths with finite precision (Sarrof, Veitsman & Hahn, 2024), and allowing negative eigenvalues restores it (Grazzi et al., 2025). What Recurrent Networks Can Compute has more.

Hybrids: a little attention, a lot of recurrence

If a little attention buys lookup, mix it in. A hybrid architecture interleaves a few attention layers, global or within a sliding window, with recurrent or state space layers. Griffin (De et al., 2024) uses a gated linear recurrence, the RG-LRU, whose gates depend on the input but not on the state, so it can be computed with a scan:

rt=σ(Waxt+ba),it=σ(Wxxt+bx),at=ac rt,ht=at⊙ht−1+1−at2⊙(it⊙xt)r_t=\sigma(W_ax_t+b_a),\qquad i_t=\sigma(W_xx_t+b_x),\qquad a_t=a^{c\,r_t},\qquad h_t=a_t\odot h_{t-1}+\sqrt{1-a_t^2}\odot\big(i_t\odot x_t\big)
a=σ(Λ)a=\sigma(\Lambda) elementwise, so 0≤at≤10\le a_t\le1, and c=8c=8; Λ\Lambda is initialized so that aca^c lies between 0.9 and 0.999. The factor 1−at2\sqrt{1-a_t^2} is the normalization of the linear recurrent unit (Structured State Spaces: HiPPO & S4). Griffin alternates two residual blocks with this recurrence and one with local multi-query attention over a window of 1,024 tokens; Hawk uses the recurrence alone.
ModelMixWhat the authors report
Griffin (De et al., 2024)2 RG-LRU blocks : 1 local attention blockmatched Llama-2's reported performance despite training on over 6 times fewer tokens; extrapolated to longer inputs; but Griffin and Hawk did worse than transformers at copying and exact retrieval when pretrained models were evaluated without fine-tuning
Jamba (Lieber et al., 2024)1 attention : 7 Mamba layers, mixture of experts every other layer52 billion parameters, 12 billion active; contexts of 256,000 tokens with a 4 GB cache; pure Mamba failed to follow few-shot formats that the hybrid followed, and, as anecdotal evidence of an induction mechanism, heads in the hybrid's attention layers attended to the labels of the few-shot examples
Mamba-2-Hybrid (Waleffe et al., 2024)43% Mamba-2, 7% attention, 50% MLP layersat 8 billion parameters and 3.5 trillion tokens, beat a matched transformer on all 12 standard tasks (2.65 points on average) and was predicted to generate up to 8 times faster; pure Mamba and Mamba-2 lagged on 5-shot MMLU, phone-book lookup and long-context reasoning
Results as reported by each paper, for its own data and training budget; they are not directly comparable across rows.

The controlled comparison of Waleffe et al. is among the cleanest evidence available: the same data, the same budget, the same evaluation. It agrees with the theory of the previous steps. Pure state space models are competitive on tasks that reward good average prediction and fall behind where exact recall from context is needed, and a small fraction of attention layers (7% in their hybrid) closed that gap while keeping most of the inference savings.

Key takeaways

  • A KV cache grows with the context (about 0.5 MB per token for 32 layers of width 4,096 in 16-bit), while a recurrent state is fixed (tens of megabytes for a large SSM); recall ability tracks state size along a trade-off curve.
  • A model with a bb-bit state copies uniformly random strings with error above 12\tfrac12 once b<Llog⁡2D−1b<L\log_2D-1, whatever its training; two attention layers copy strings exponentially long in their width by nn-gram lookup, using a cache that is optimal up to logarithmic factors.
  • Exact multi-query recall needs Ω(N)\Omega(N) bits of state; recall explains most of the perplexity gap between gated convolutions and attention (82% in Zoology), and trained fixed-state models fall short of even the information-theoretic capacity.
  • Common SSMs, like transformers, lie in TC0\mathsf{TC}^0 and cannot track permutations unless TC0=NC1\mathsf{TC}^0=\mathsf{NC}^1; at finite precision, nonnegative transitions cannot even recognize parity at arbitrary lengths.
  • Hybrids keep a few attention layers among many recurrent ones (Griffin, Jamba, Mamba-2-Hybrid); in a controlled comparison about 7% attention layers closed the recall gap while keeping most of the inference savings.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

As the context grows from 1,000 to 1,000,000 tokens, how do the inference memories of a transformer's KV cache and of a state space model's state change?

  1. Both stay constant
  2. Both grow linearly
  3. The KV cache grows linearly with the context; the state stays the same size
  4. The state grows linearly; the KV cache stays the same size
Show the answer

C. The KV cache grows linearly with the context; the state stays the same size

The cache stores keys and values for every earlier token in every layer, so it grows 1,000-fold; the recurrent state is fixed by the architecture. That is the efficiency of recurrence, and also the reason for its recall limits.

Question 2

A causal recurrent model reads KK key–value pairs, with values drawn uniformly from cc symbols, and must then answer one query per key, all correctly. How many bits must its state hold at the boundary between pairs and queries?

  1. At least Klog⁡2cK\log_2c
  2. At least log⁡2K\log_2K, enough to index the pairs
  3. A constant independent of KK, if the model is large enough
  4. At least KK, one bit per pair
Show the answer

A. At least Klog⁡2cK\log_2c

Different value assignments require different answers to the same queries, and the answers depend on the pairs only through the boundary state, so the cKc^K assignments need distinct states: log⁡2(cK)=Klog⁡2c\log_2(c^K)=K\log_2c bits.

Question 3

What makes multi-query associative recall (MQAR) a better probe of language models than single-query recall with a small vocabulary?

  1. It requires copying the entire input
  2. It requires arithmetic on the values
  3. It requires many recalls in one pass, at arbitrary positions, with more distinct tokens than the model has dimensions
  4. It requires positional encodings that extrapolate
Show the answer

C. It requires many recalls in one pass, at arbitrary positions, with more distinct tokens than the model has dimensions

Gated convolutions could solve single-query recall with tiny vocabularies, yet still lost to attention on real text. MQAR's many queries and large vocabulary force a model to keep many associations available at once, which exposes the state-size limit and matches the recall failures found in the Pile.

Question 4

In Waleffe et al.'s controlled comparison at 8 billion parameters, what distinguished the Mamba-2-Hybrid from the pure Mamba-2 model?

  1. It replaced every Mamba-2 layer with attention
  2. About 7% of its layers were attention, and it beat the matched transformer on all 12 standard tasks, where pure Mamba-2 lagged on tasks such as 5-shot MMLU and phone-book lookup
  3. It was trained on six times fewer tokens
  4. It used local attention only, with a window of 1,024 tokens
Show the answer

B. About 7% of its layers were attention, and it beat the matched transformer on all 12 standard tasks, where pure Mamba-2 lagged on tasks such as 5-shot MMLU and phone-book lookup

The hybrid kept mostly Mamba-2 and MLP layers (43% and 50%) and added a few global attention layers (7%). That small amount of lookup closed the recall gap and was still predicted to generate up to 8 times faster than the transformer.

Further reading

  • S. Jelassi, D. Brandfonbrener, S. M. Kakade & E. Malach, "Repeat after me: Transformers are better than state space models at copying," ICML (2024).
  • S. Arora, S. Eyuboglu, A. Timalsina, I. Johnson, M. Poli, J. Zou, A. Rudra & C. Ré, "Zoology: Measuring and improving recall in efficient language models," ICLR (2024).
  • S. Arora, S. Eyuboglu, M. Zhang, A. Timalsina, S. Alberti, D. Zinsley, J. Zou, A. Rudra & C. Ré, "Simple linear attention language models balance the recall-throughput tradeoff," ICML (2024).
  • W. Merrill, J. Petty & A. Sabharwal, "The illusion of state in state-space models," ICML (2024).
  • S. De, S. L. Smith, A. Fernando, A. Botev et al., "Griffin: Mixing gated linear recurrences with local attention for efficient language models," arXiv:2402.19427 (2024).
  • O. Lieber, B. Lenz, H. Bata, G. Cohen et al., "Jamba: A hybrid Transformer-Mamba language model," arXiv:2403.19887 (2024).
  • R. Waleffe, W. Byeon, D. Riach et al., "An empirical study of Mamba-based language models," arXiv:2406.07887 (2024).