MELODI: Exploring Memory Compression for Long Contexts

Yinpeng Chen, DeLesley Hutchins, Aren Jansen, Andrey Zhmoginov, David Racz, Jesper Sparre Andersen

TL;DR

Processing long documents with Transformers is notoriously memory-hungry due to the massive size of historical Key-Value (KV) caches. MELODI solves this by introducing a hierarchical memory architecture that compresses short-term context across multiple layers and heavily compresses long-term history into a single middle layer. The result is a model that outperforms strong baselines like the Memorizing Transformer while slashing the memory footprint by a factor of 8.

The Challenge of Long Contexts

Large Language Models (LLMs) process text using attention mechanisms, which scale quadratically—meaning that doubling the amount of text quadruples the computational cost. To read a whole book, models typically process text in short "chunks" or context windows.

The central challenge is: how does the model remember what happened in chapter 1 when it is reading chapter 10? Existing solutions generally fall into two camps. They either use a recurrent state to pass information chunk-by-chunk (which suffers from gradual forgetting), or they cache the Key-Value (KV) pairs of every past token (like the Memorizing Transformer), which causes memory usage to explode. The authors of this paper set out to build a memory architecture that bridges the gap between short context windows without running out of RAM.

The MELODI Architecture

The authors introduce MELODI (MEmory with LOw DImension), which relies on a hierarchical compression scheme to manage memory efficiently. It uses a "sandwich" architecture that splits memory duties:

MELODI Architecture
  • Short-Term Memory (Multi-Layer Recurrent Compression): As the model processes a context window (e.g., 512 tokens), it uses linear token mixers to compress that information into a small set of "summary tokens" (e.g., 128 tokens). These summary tokens are passed to the next window across almost all transformer layers. This acts like a multi-layer LSTM, ensuring smooth, immediate transitions between chunks.
  • Long-Term Memory (Single-Layer Memorization): To prevent the model from forgetting distant history, MELODI designates a single middle transformer layer for long-term storage. Here, the context window is compressed even further (e.g., down to 64 tokens). The KV pairs for these highly compressed tokens are saved into a first-in-first-out (FIFO) queue that covers the entire document history.

By compressing the activations rather than the input text, MELODI seamlessly integrates into standard transformer blocks with negligible extra parameters.

Evaluation Datasets

The authors evaluated MELODI by training models from scratch on several long-context datasets:

  • PG-19: A collection of English books published before 1919.
  • arXiv Math: Technical math papers (notable for dense special characters and LaTeX).
  • C4 (4K+): A filtered version of the massive C4 internet dataset, restricted to documents longer than 4,096 tokens.
  • Masked Next Token Prediction (New Benchmark): The authors introduced a novel task where a percentage of input tokens are randomly masked out. This forces the model to rely on distant, unmasked historical memory to predict the next word, explicitly testing long-range dependency capabilities.

Key Insights

  • KV Caches are highly compressible: You don't need to store the exact Key-Value pairs of every past token to remember the past. Compressing a 512-token window into just 64 long-term memory tokens captures the "gist" perfectly well, saving massive amounts of space.
  • Division of memory labor is highly effective: Short-term and long-term memories play complementary roles. Short-term memory handles the immediate local flow and syntax, while long-term memory acts as an anchor to distant past events.
  • One long-term layer is enough: Adding long-term memory to every single transformer layer is computationally wasteful. A single middle layer, if given enough compressed tokens, captures the necessary historical context efficiently.

Key Results

Performance vs. Memory Size
  • Massive Memory Savings: MELODI achieved an 8x reduction in long-term memory footprint compared to the Memorizing Transformer (e.g., 18.5M vs 147.8M floats) while achieving slightly better predictive performance.
  • Superior Perplexity: On the PG-19 dataset, a 13-layer MELODI model achieved a perplexity of 10.44, beating Transformer-XL (11.41), Block Recurrent Transformer (10.98), and the Memorizing Transformer (10.62). (Lower perplexity is better).
  • Robustness to Short Windows: When the context window was aggressively shrunk from 512 down to 128 tokens, MELODI's performance degraded much less than baselines, proving its memory mechanisms successfully bridge the gaps between very short chunks.
  • Excels at Masked Prediction: In the new masked token task, MELODI outperformed baselines significantly. Even with 50% of the local context masked out, a 12-layer MELODI model performed almost as well as a 36-layer Transformer-XL.

Limitations and Future Work

  • Training from Scratch: The models in this paper were trained from scratch. Adapting MELODI to already pre-trained LLMs (which have fixed context windows) will require further research into fine-tuning techniques like LoRA.
  • Evaluation Metrics: The paper primarily evaluates models using perplexity (next-word prediction accuracy). The authors acknowledge the need to test MELODI on actual downstream long-context tasks, like document summarization or question answering.
  • Training Overhead: While MELODI is highly memory-efficient and fast during inference, its training time is slightly slower than standard models due to the need for Backpropagation Through Time (BPTT) to update the recurrent short-term memory.
  • Future Work: The authors plan to explore integrating MELODI with pre-trained models and investigate engineering solutions (like better model parallelism) to scale the context window beyond 2048 tokens.

Summary updated

BibTeX
@inproceedings{chen2025melodi,
  author = {Yinpeng Chen and DeLesley Hutchins and Aren Jansen and Andrey Zhmoginov and David Racz and Jesper Sparre Andersen},
  title = {{MELODI: Exploring Memory Compression for Long Contexts}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=TvGPP8i18S},
}
Figure