State Space Attention: Learning Context-Dependent Memory Retention
Organizations: Department of Statistics Harvard University Cambridge, MA 02138, USA · John A. Paulson School of Engineering and Applied Sciences Harvard University
Abstract
Standard attention's key-value cache grows linearly with context length. Bounding it raises the question of what a sequence model should retain for long-context reasoning. We argue this choice should be learned, and introduce State Space Attention (SSA), which learns to keep a bounded set of past representations and reads them with standard attention. A learned scorer rescores every chunk against newly arriving context and the rest of memory. A fixed-context Transformer is then the special case that always keeps the most recent tokens. We train the scorer and reader jointly, either moving gradually from a differentiable soft weighting of memories to hard retention, or with hard retention from the start. To produce signal for the scorer, we generate supervised samples during training, a technique we call recovery. We compare SSA with recurrent models, bounded-memory attention methods, and full-attention Transformers across retrieval, variable tracking, multi-hop reasoning, and language modeling. On our retrieval and reasoning benchmarks, SSA matches or comes within a few points of full attention on most tasks, far outperforms a Transformer with the same readable budget on every long-context retrieval task we evaluate, and matches or outperforms one with about 4 times that budget, while using constant inference memory. The largest remaining gap to full attention at small language model scale is on RULER. SSA makes a fixed number of tokens useful over a longer context, and since it reads memory with standard attention, we propose that it can be stacked with KV cache eviction methods. We also pretrain and post-train 400M-parameter models and measure how SSA's training and inference costs scale with context length and model size. Together, these experiments examine the capabilities and limitations of learned memory retention for long-context sequence modeling.
Figures & tables
| Model | Parameters | NLL at 2K | NLL at 8K |
|---|---|---|---|
| Transformer (576) | 400.1M | 2.695 | 2.686 |
| Transformer (2K) | 400.1M | 2.615 | 5.039 |
| Transformer (8K) | 400.1M | 2.623 | 2.557 |
| Gated DeltaNet | 399.9M | 2.590 | 2.549 |
| HOLA | 399.8M | 2.591 | 2.558 |
| SSA, no recovery | 399.8M | 2.656 | 4.334 |
| MKVT | MQAR | RULER | -hop token accuracy | PPL | |||||||
| Model | 2K | 4K | 8K | Query | 4K | 8K | 2K | 8K | |||
| SSA, frozen scorer | 98.10 | 98.13 | 97.53 | 99.954 | 64.14 | 56.29 | 99.984 | 99.968 | 99.609 | 22.99 | 27.00 |
| SSA, trainable scorer | 96.13 | 95.97 | 96.57 | 99.952 | 64.00 | 56.40 | 99.984 | 99.971 | 99.536 | 23.07 | 26.91 |
| Transformer | 28.90 | 14.67 | 8.13 | 85.739 | 22.04 | 14.61 | 99.997 | 99.970 | 99.472 | 20.34 | 19.79 |
| Transformer | 29.20 | 14.53 | 7.93 | 85.829 | 22.70 | 15.76 | 99.982 | 99.721 | 48.251 | 21.49 | 20.77 |
| Transformer | 99.90 | 66.57 | 34.73 | 98.162 | 60.56 | 36.50 | 99.764 | 34.184 | 25.936 | 19.72 | 19.03 |
Appendix figures & tables36 assets
Supplementary material from the paper’s appendix.
Appendix
| Model / retention policy | Mean exact | Seed range |
|---|---|---|
| Transformer (same backbone) | 100.00 | 100.00–100.00 |
| Gated DeltaNet | 100.00 | 100.00–100.00 |
| HOLA | 100.00 | 100.00–100.00 |
| laLTE | 100.00 | 100.00–100.00 |
| SSA | 98.70 | 97.66–100.00 |
| Drop oldest (SWA at matched memory) | 0.00 | 0.00–0.00 |
| Model | Teacher-forced | Greedy |
|---|---|---|
| Transformer (same backbone) | 20.90 | 17.19 |
| Transformer (larger) | 30.30 | 28.91 |
| Gated DeltaNet | 0.00 [0.00–0.00] | 0.00 [0.00–0.00] |
| HOLA | 0.73 [0.00–2.20] | 0.52 [0.00–1.56] |
| laLTE | 25.47 [22.40–30.60] | 19.01 [17.19–22.66] |
| GDN + full attention | 16.60 [12.80–23.90] | 11.46 [6.25–18.75] |
| Model | ||
|---|---|---|
| Transformer (same backbone) | 99.75 [99.70–99.80] | 97.65 [96.00–99.30] |
| Transformer (larger) | 99.55 [99.40–99.70] | 99.25 [98.80–99.70] |
| Gated DeltaNet | 24.05 [20.90–27.20] | 34.40 [24.00–44.80] |
| HOLA | 70.30 [65.20–75.40] | 73.40 [70.50–76.30] |
| SSA | 99.65 [99.40–99.90] | 99.15 [98.90–99.40] |
| Model | Fixed data | Fresh data, mean [seed range] |
|---|---|---|
| Transformer (same backbone) | 94.35 | 99.75 [99.70–99.80] |
| Transformer (larger) | 82.75 | 99.55 [99.40–99.70] |
| Gated DeltaNet | 0.10 | 34.40 [24.00–44.80] |
| HOLA | 6.50 | 73.40 [70.50–76.30] |
| SSA | 70.05 | 99.65 [99.40–99.90] |
| Task | Learned retention | Drop oldest | Random eviction |
|---|---|---|---|
| Associative recall | 92.19 | 69.27 | 80.99 |
| Cued copy | 98.70 | 0.00 | 2.86 |
| Copy join | 100.00 | 36.46 | 94.53 |
| Task | laLTE | SSA |
|---|---|---|
| Cued copy | 100.00 | 98.70 |
| Associative recall | 100.00 | 92.19 |
| Copy join | 100.00 | 100.00 |
| Alias chain (3 hops) | 89.84 | 48.70 |
| -hop ( ) | 19.01 | 19.53 |
| MQAR, 8 pairs | 99.77 | 99.18 |
| FP32, 14 chunks | BF16, 30 chunks | |||||
|---|---|---|---|---|---|---|
| Task | Shared | Layer | Head | Shared | Layer | Head |
| Cued copy | 98.70 | 99.22 | 99.22 | 97.40 | 98.18 | 100.00 |
| Associative recall | 92.19 | 95.05 | – | 99.22 | 99.48 | – |
| Copy join | 100.00 | 100.00 | 100.00 | 100.00 | 100.00 | 100.00 |
| Alias chain (3 hops) | 48.70 | 44.53 | 50.52 | 71.09 | 38.28 | 30.47 |
| -hop ( ) | 19.53 | 22.14 | 21.88 | 19.53 | 19.27 | 19.01 |
| SSA, FP32, 14 chunks | SSA, BF16, 30 chunks | Full | |||
|---|---|---|---|---|---|
| Task | Layer | Head | Layer | Head | attention |
| Cued copy | 100.00 | 100.00 | 100.00 | 100.00 | 100.00 |
| Associative recall | 100.00 | 100.00 | 100.00 | 100.00 | 100.00 |
| Copy join | 100.00 | 100.00 | 100.00 | 100.00 | 100.00 |
| Alias chain (3 hops) | 94.01 | 90.89 | 85.16 | 72.14 | 88.54 |
| -hop ( ) | 13.54 | 16.41 | 13.28 | 14.32 | 11.46 |
| Model or attention window | Persistent state (MiB) |
|---|---|
| Transformer, 576-token window | 54.000 |
| Transformer, 2K window | 192.000 |
| Transformer, 8K window | 768.000 |
| SSA, | 54.005 |
| GDN | 54.797 |
| HOLA | 54.680 |
| Stage | Updates | Examples | Reader LR |
|---|---|---|---|
| Joint pilot | 1,560 | 99,840 | |
| Mixed refinement | 6,240 | 399,360 | |
| -hop specialization | 1,760 | 112,640 | |
| Total | 9,560 | 611,840 | — |
| Model | |||
|---|---|---|---|
| SSA, frozen scorer | 96.48 / 99.985 | 94.92 / 99.971 | 65.63 / 99.667 |
| SSA, trainable scorer | 97.66 / 99.985 | 95.31 / 99.974 | 56.25 / 99.600 |
| Transformer | 98.83 / 99.995 | 92.58 / 99.969 | 55.47 / 99.513 |
| Transformer | 96.48 / 99.983 | 65.23 / 99.748 | 0.00 / 57.008 |
| Transformer | 78.52 / 99.768 | 0.00 / 39.908 | 0.00 / 38.676 |
| Transformer | 5.86 / 83.319 | 0.00 / 35.718 | 0.00 / 38.831 |
| Metric | Global recovery | Equal-family recovery |
|---|---|---|
| MKVT, 2K | 32.63 | 89.70 |
| MKVT, 4K | 17.77 | 91.67 |
| MKVT, 8K | 8.20 | 90.90 |
| MQAR | 99.95 | 99.95 |
| RULER, 4K | 21.86 | 56.65 |
| RULER, 8K | 12.64 | 45.36 |
| Metric | Original scorer | Larger scorer |
|---|---|---|
| MKVT, 2K | 89.70 | 98.13 |
| MKVT, 4K | 91.67 | 98.40 |
| MKVT, 8K | 90.90 | 97.87 |
| MQAR | 99.951 | 99.959 |
| RULER, 4K | 56.65 | 63.08 |
| RULER, 8K | 45.36 | 55.59 |
| 3,120 total updates | 7,800 total updates | |||
|---|---|---|---|---|
| Metric | Original | Larger | Original | Larger |
| MKVT, 2K | 92.03 | 99.50 | 98.43 | 99.83 |
| MKVT, 4K | 92.57 | 99.33 | 98.23 | 99.90 |
| MKVT, 8K | 92.63 | 98.83 | 98.40 | 99.87 |
| MQAR | 99.957 | 99.977 | 99.963 | 99.995 |
| RULER, 4K | 58.13 | 66.08 | 68.73 | 68.84 |
| 4K | 8K | |||
|---|---|---|---|---|
| Task | Original | Larger | Original | Larger |
| NIAH single-1 | 100.00 | 100.00 | 99.80 | 100.00 |
| NIAH single-2 | 100.00 | 100.00 | 99.40 | 99.80 |
| NIAH single-3 | 96.40 | 97.80 | 88.80 | 88.40 |
| NIAH multi-key-1 | 99.40 | 99.80 | 90.60 | 97.20 |
| NIAH multi-key-2 | 15.60 | 15.00 | 8.80 | 10.80 |
| MKVT | MQAR | RULER | ||||
| Model | 2K | 4K | 8K | Query | 4K | 8K |
| SSA, frozen scorer | ||||||
| SSA, trainable scorer | ||||||
| Transformer | ||||||
| Transformer | ||||||
| Transformer | ||||||
| Model | PPL, 2K | PPL, 8K |
|---|---|---|
| SSA, frozen scorer | ||
| SSA, trainable scorer | ||
| Transformer | ||
| Transformer | ||
| Transformer | ||
| Transformer |
| Model | 880 | 1,320 | 1,760 |
|---|---|---|---|
| SSA, frozen scorer | 26.16 | 49.52 | 99.61 |
| SSA, trainable scorer | 25.96 | 34.79 | 99.54 |
| Transformer | 25.97 | 47.04 | 99.47 |
| Transformer | 26.29 | 56.81 | 99.46 |
| Arm | Change from the full recipe | Run A | Run B | Learned |
|---|---|---|---|---|
| Full recipe | none | 29.7 / 15.4 / 7.8 | 28.8 / 14.7 / 7.3 | 1 of 3 ∗ |
| No recovery | counterfactual recovery off | 28.5 / 14.8 / 8.0 | 29.3 / 14.4 / 7.1 | 0 of 2 |
| No STE | straight-through read gradient off | 29.1 / 15.9 / 8.0 | 54.0 / 41.9 / 31.5 | 1 of 2, in part |
| Hard only | no soft phase: 1,560 hard updates | 79.2 / 69.8 / 64.4 | 97.6 † / 97.4 / 95.7 | 2 of 2 |
| Contextual, no rescoring | each chunk scored once, on entry | 84.4 / 83.3 / 80.0 | 99.3 / 99.1 / 98.4 | 2 of 2 |
| Non-contextual | the scorer sees only the new chunk | 99.9 / 99.8 / 98.3 | 99.8 / 99.4 / 98.0 | 2 of 2 |
| Method | Cache (tokens) | MKVT 8K | RULER 8K | MQAR |
|---|---|---|---|---|
| Full attention, no eviction | 8,192 | 99.6 / 99.9 | 70.7 / 76.9 | 99.99 / 99.98 |
| StreamingLLM ( Xiao et al., 2024 ) | 576 | 7.6 / 7.8 | 11.6 / 15.7 | 85.8 / 85.7 |
| H 2 O ( Zhang et al., 2023 ) | 576 | 5.3 / 5.0 | 3.8 / 4.2 | 100.0 / 100.0 |
| TOVA ( Oren et al., 2024 ) | 576 | 6.2 / 6.7 | 11.3 / 16.9 | 98.8 / 96.9 |
| SnapKV ( Li et al., 2024 ) , sees the question | 576 | 99.1 / 81.4 | 51.0 / 50.0 | not run |
| H 2 O, paper settings (20% of the prompt) | about 1,600 | 40.3 / 19.6 | 24.1 / 21.3 | not run |
| DMS | DCP | SSA | Transformer | |
|---|---|---|---|---|
| Tokens readable per head at 8K | 344 | 62 | 576 (shared) | 8,192 |
| MKVT 2K / 4K / 8K | 99.93 / 99.03 / 95.43 | 23.93 / 17.90 / 8.63 | 96.13 / 95.97 / 96.57 | 99.97 / 99.97 / 99.60 |
| MQAR | 99.97 | 50.43 | 99.95 | 99.99 |
| RULER 4K / 8K | 63.77 / 50.84 | 12.62 / 13.46 | 64.00 / 56.40 | 78.27 / 70.72 |
| -hop exact, / 8 / 16 | 87.50 / 1.95 / 0.00 | 0.00 / 0.00 / 0.00 | 97.66 / 95.31 / 56.25 | 5.86 / 0.00 / 0.00 |
| -hop token, / 8 / 16 | 99.95 / 97.55 / 38.61 | 24.73 / 24.24 / 24.75 | 99.98 / 99.97 / 99.54 | 82.53 / 29.42 / 26.07 |
| Model | Starting checkpoint | Block at the start | Block at a random position |
|---|---|---|---|
| SSA | mixed-task | 77.4 (100.0 / 99.9 / 25.9) | 26.3 (27.0 / 26.3 / 25.6) |
| Transformer | mixed-task | 22.5 (21.3 / 22.0 / 24.6) | 22.7 (21.3 / 22.3 / 24.9) |
| Transformer | mixed-task | 26.7 (27.9 / 26.3 / 25.8) | 26.6 (27.4 / 26.4 / 25.9) |
| GDN | mixed-task | 26.5 (26.7 / 26.9 / 26.0) | 26.4 (26.8 / 26.4 / 25.9) |
| HOLA | mixed-task | 26.5 (27.1 / 26.2 / 25.9) | 26.3 (27.0 / 26.0 / 25.8) |
| SSA | -hop (Table 2 ) | 99.2 (99.4 / 99.3 / 98.9) | 26.7 (27.5 / 26.4 / 25.9) |
| Model | Retention rule at evaluation | PPL at 2K | PPL at 8K |
|---|---|---|---|
| Pretrained SSA, no recovery | learned | 14.23 | 76.24 |
| sliding window, 34 most recent chunks | 14.94 | 14.83 | |
| first chunk and 33 most recent | 14.14 | 42.06 | |
| Post-trained SSA, trainable scorer | learned | 23.08 | 27.08 |
| sliding window, 34 most recent chunks | 20.81 | 20.16 | |
| first chunk and 33 most recent | 20.58 | 20.87 |
| Model | 2K | 8K | 8K, distances capped |
|---|---|---|---|
| SSA, trainable scorer | 23.07 | 26.91 | 24.92 |
| Transformer | 19.73 | 22.28 | 18.77 |
| Learned memory | Random memory | Reservoir memory | |||||
|---|---|---|---|---|---|---|---|
| Uncapped | Capped | Uncapped | Capped | Uncapped | Capped | ||
| 1 | 34 | 26.91 | 24.92 | 20.73 | 20.58 | 29.39 | 26.43 |
| 5 | 30 | 23.40 | 21.91 | 20.40 | 20.32 | 24.41 | 22.22 |
| 9 | 26 | 22.26 | 21.00 | 20.29 | 20.25 | 23.13 | 21.18 |
| 13 | 22 | 21.68 | 20.59 | 20.19 | 20.18 | 22.57 | 20.77 |
| 17 | 18 | 21.29 | 20.38 | 20.16 | 20.16 | 22.13 | 20.49 |
| SSA | ||||
|---|---|---|---|---|
| Setting | Transformer | Soft phase | Hard, no recovery | Hard, recovery |
| Pretraining at 2K, 4 H100, 524,288 tokens per update | 2.5 s | 9.1 s (3.6 ) | 14.9 s (6.0 ) | 32 s (13 ) |
| Post-training pilot, mixed tasks up to 8K, 2 RTX PRO 6000, 64 rows per update | 19.9 s | 59.5 s (3.0 ) | 45.7 s (2.3 ) | 66.4 s (3.3 ) |
| Training time per update | Decoding time per token | |||||||||||
| Parameters | 2K | 4K | 8K | 16K | 32K | 64K | 2K | 4K | 8K | 16K | 32K | 64K |
| 3M | 12.8 | 18.5 | 24.2 | 25.6 | 21.5 | 16.6 | 1.29 | 1.26 | 1.17 | 1.10 | 0.91 | 0.56 |
| 25M | 6.7 | 11.7 | 14.5 | 13.7 | 10.3 | 7.1 | 1.17 | 1.10 | 0.98 | 0.72 | 0.54 | 0.36 |
| 85M | 5.7 | 8.3 | 10.1 | 9.1 | 6.7 | 4.7 | 1.16 | 0.99 | 0.86 | 0.67 | 0.47 | 0.29 |
| 315M | 5.2 | 6.6 | 7.5 | 6.5 | 5.0 | 3.5 | 0.99 | 0.90 | 0.76 | 0.58 | 0.39 | 0.24 |
| 367M | 5.0 | 6.5 | 6.8 | 6.0 | 4.6 | 3.2 | 0.97 | 0.89 | 0.77 | 0.59 | 0.40 | 0.25 |
| Time per update (s) | Peak memory (GiB) | |||||
|---|---|---|---|---|---|---|
| Context | Transformer | SSA, hard | SSA, soft | Transformer | SSA, hard | SSA, soft |
| 2K | 0.14 | 0.39 (2.8 ) | 0.25 (1.8 ) | 7.6 | 7.7 | 7.7 |
| 4K | 0.28 | 0.72 (2.6 ) | 0.62 (2.2 ) | 7.7 | 7.8 | 7.8 |
| 8K | 0.63 | 1.40 (2.2 ) | 1.64 (2.6 ) | 8.8 | 9.3 | 9.7 |
| 16K | 1.67 | 2.87 (1.7 ) | 4.91 (2.9 ) | 13.1 | 13.2 | 18.8 |
| 32K | 5.19 | 5.89 (1.1 ) | 16.85 (3.2 ) | 21.6 | 21.8 | 53.3 |
| Soft-phase training time | Prompt-reading time | |||||||||||
| Parameters | 2K | 4K | 8K | 16K | 32K | 64K | 2K | 4K | 8K | 16K | 32K | 64K |
| 3M | 12.9 | 16.7 | 23.1 | 28.1 | 31.0 | oom | 11.0 | 34.1 | 38.8 | 41.5 | 33.2 | 21.3 |
| 25M | 5.2 | 10.5 | 15.9 | 21.2 | 25.9 | oom | 10.7 | 12.1 | 17.6 | 14.9 | 11.3 | 7.0 |
| 85M | 3.9 | 7.4 | 12.3 | 18.1 | oom | oom | 5.8 | 9.3 | 9.8 | 8.4 | 5.7 | 3.9 |
| 315M | 3.4 | 5.7 | 10.0 | 14.7 | oom | oom | 5.0 | 5.8 | 6.3 | 4.9 | 3.6 | 2.6 |
| 367M | 3.1 | 5.5 | 9.1 | 14.4 | oom | oom | 5.0 | 5.6 | 5.4 | 4.5 | 3.2 | 2.2 |
| Hard phase | Soft phase | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Parameters | 2K | 4K | 8K | 16K | 32K | 64K | 2K | 4K | 8K | 16K | 32K | 64K |
| 3M | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.02 | 1.04 | 1.09 | 1.66 | 3.12 | oom |
| 25M | 1.00 | 1.00 | 1.00 | 1.01 | 1.01 | 1.01 | 1.02 | 1.04 | 1.47 | 2.69 | 5.20 | oom |
| 85M | 1.12 | 1.06 | 1.01 | 1.01 | 1.01 | 1.01 | 1.09 | 1.19 | 1.82 | 3.41 | oom | oom |
| 315M | 1.01 | 1.15 | 1.29 | 1.30 | 1.36 | 1.17 | 1.01 | 1.24 | 2.09 | 4.06 | oom | oom |
| 367M | 1.01 | 1.06 | 1.17 | 1.14 | 1.17 | 1.01 | 1.01 | 1.13 | 1.78 | 3.28 | oom | oom |
| Per token (ms) | Prompt (s) | Key–value cache (MiB) | ||||
|---|---|---|---|---|---|---|
| Context | Transformer | SSA | Transformer | SSA | Transformer | SSA |
| 2K | 1.54 | 1.49 | 0.011 | 0.046 | 192 | 54 |
| 4K | 1.64 | 1.49 | 0.022 | 0.103 | 384 | 54 |
| 8K | 1.92 | 1.48 | 0.046 | 0.214 | 768 | 54 |
| 16K | 2.45 | 1.46 | 0.115 | 0.439 | 1536 | 54 |
| 32K | 3.53 | 1.44 | 0.326 | 0.887 | 3072 | 54 |
| Language | Retrieval | Paired | Randomized | Source | Solved |
|---|---|---|---|---|---|
| characters | attention | queries | names | head | cases |
| 73M | NormSoftmax | Yes | No | No | 6/12 |
| 73M | NormSoftmax | Yes | Yes | No | 3/12 |
| 73M | NormSoftmax | Yes | No | Yes | 12/12 |
| 998M | NormSoftmax | Yes | No | No | 12/12 |
| 998M | Standard | Yes | No | No | 12/12 |
| Short language phase | Extended language phase | |
| Epochs at each of lengths 256, 512, 1024 | 5 | 68 |
| Language examples per epoch | 8192 | 8192 |
| Language character presentations | 73,400,320 | 998,244,352 |
| Retrieval epochs | 135 | 135 |
| Total epochs | 150 | 339 |
| Retrieval configuration | By epoch 2 | Before stage exit | Final solved |
|---|---|---|---|
| NormSoftmax, paired | 11/12 | 12/12 | 12/12 |
| Standard attention, paired | 11/12 | 12/12 | 12/12 |
| Standard attention, unpaired | 10/12 | 12/12 | 12/12 |
| All three | 32/36 | 36/36 | 36/36 |
| Language intervention | Lowest exact (%) | ||
|---|---|---|---|
| Extended pretraining | 11/12 | 12/12 | 98.0 |
| + Randomized names | 9/12 | 12/12 | 97.6 |
| + Recovery in language | 9/12 | 11/12 | 83.9 |
| + Both | 11/12 | 11/12 | 86.1 |
| All arms | 40/48 | 46/48 | 83.9 |