Titan-Llama: Retrofitting Pre-trained LLMs with External Neural Memory Modules
Organizations: Massachusetts Institute of Technology · Cornell University
Abstract
Transformers are efficient to pre-train at scale because attention permits parallel computation across sequence positions, but long-context inference requires increasingly large KV caches. Efficient recurrent alternatives reduce the asymptotic complexity of inference, but their sequential dependence limits token-wise parallelism during training, making them harder to scale. Further, the current paradigm of extending long context tasks creates competing notions of retaining information precisely and keeping long-context inference efficient. To address this gap, we introduce Titan-Llama, a post-training method for retrofitting attention-based language models with a hierarchical memory architecture that updates parameters at test-time. Titan-Llama applies a blockwise attention mask and preserves full-fidelity attention over recent tokens while compressing historical context into recurrent, fixed-size neural memory modules (NMMs). Our formulation allows pre-trained attention layers to be replaced without training a new language model from scratch. On Llama-3.2-1B-Instruct, Titan-Llama raises accuracy over segmented attention windows from 23.87% to 40.51% on QuALITY and from 14.50% to 40.75% on LongHealth. Inference benchmarks show more than 20% faster prefill, 37% faster decode at medium context lengths, and 24% lower steady-state decode memory usage in the evaluated configurations. Together, these results demonstrate that incorporating memory architecture mechanisms that utilize test-time training effectively modulates long-context behavior of pre-trained models, offering a practical route to adapting pre-trained transformers for efficient long-context inference. Our code is released on Github GitHub
Figures & tables
| S-NIAH-1 (pass-key) | S-NIAH-2 (number) | S-NIAH-3 (UUID) | Relative decode time | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Model | 8K | 16K | 32K | 8K | 16K | 32K | 8K | 16K | 32K | Mean |
| Full attention | 100.0 | 100.0 | 100.0 | 99.0 | 100.0 | 100.0 | 64.0 | 64.0 | 83.0 | 1.00 |
| Mamba-2 | 99.0 | 49.0 | 26.0 | 99.0 | 43.0 | 28.0 | 77.0 | 36.0 | 24.0 | 2.70 |
| Gated DeltaNet | 100.0 | 50.0 | 26.0 | 100.0 | 43.0 | 27.0 | 91.0 | 45.0 | 23.0 | 2.59 |
| LaCT | 98.0 | 43.0 | 22.0 | 100.0 | 43.0 | 27.0 | 74.0 | 34.0 | 23.0 | |
| TTT-E2E | 100.0 | 46.0 | 24.0 | 99.0 | 43.0 | 28.0 | 77.0 | 44.0 | 24.0 | |
| Model | Single needle | Multi-key | Multi-query | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4K | 8K | 16K | 32K | 4K | 8K | 16K | 32K | 4K | 8K | 16K | 32K | |
| Full attention | 100.0 | 99.3 | 100.0 | 99.3 | 82.0 | 74.0 | 63.3 | 57.3 | 99.5 | 94.0 | 94.0 | 83.5 |
| Segmented attn. | 10.7 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
| Titan-Llama | 97.3 | 96.0 | 88.0 | 78.7 | 91.3 | 88.0 | 66.7 | 39.3 | 96.5 | 90.5 | 70.5 | 34.5 |
| Model | Multi-value | FWE/CWE | HotpotQA | |||||||||
| 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.
Appendix
| Model | QuALITY | LongHealth |
|---|---|---|
| Full attention (Llama-3.2-1B-Instruct) | ||
| Local-visibility variants | ||
| Segmented attention, no NMM | 26.8 | 15.8 |
| Segmented attention, with NMM | 35.2 | 32.3 |
| Model | Single needle | Multi-key | Multi-query | Multi-value | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | |
| Segmented attn. | 0.0 | 3.3 | 1.3 | 0.7 | 0.3 | 1.3 | 0.0 | 0.0 | 2.0 | 1.8 | 0.8 | 0.3 | 1.0 | 3.8 | 2.0 | 0.0 |
| Titan-Llama | 1.0 | 4.7 | 4.0 | 1.7 | 1.3 | 2.3 | 0.7 | 1.0 | 3.5 | 2.8 | 1.0 | 1.3 | 1.0 | 3.8 | 1.8 | 1.0 |
| Model | FWE/CWE | HotpotQA | SQuAD | VT | ||||||||||||
| 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | |
| Segmented attn. | 22.7 | 10.1 | 4.2 | 2.7 | 17.0 | 9.0 | 1.0 | 2.0 | 31.4 | 1.0 | 0.0 | 0.0 | 0.4 | 0.0 | 0.0 | 0.0 |
| Chunk size | Single needle | Multi-key | Multi-query | Multi-value | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | |
| 512 | 97.3 | 96.0 | 88.0 | 78.7 | 91.3 | 88.0 | 66.7 | 39.3 | 96.5 | 90.5 | 70.5 | 34.5 | 88.0 | 81.0 | 63.0 | 53.0 |
| 1024 | 95.3 | 92.7 | 91.0 | 78.7 | 81.3 | 79.3 | 63.0 | 36.3 | 92.8 | 85.0 | 51.2 | 42.2 | 87.0 | 68.8 | 49.0 | 38.2 |
| 2048 | 90.3 | 92.7 | 91.7 | 51.7 | 66.3 | 74.0 | 67.0 | 32.3 | 91.3 | 88.8 | 81.2 | 19.2 | 87.5 | 79.2 | 82.2 | 25.0 |
| Chunk size | FWE/CWE | HotpotQA | SQuAD | VT | ||||||||||||
| 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | 4k | 8k | 16k | 32k | |
Long-Context Language Model Inference
Long-context language model inference makes processing and generating over lengthy prompts faster and more memory-efficient.
See all 221 papers