MemLong: Memory-Augmented Retrieval for Long Text Modeling

Weijie Liu, Zecheng Tang, Juntao Li, Kehai Chen, Min ZhangarXiv 2024 · DOI

Introduction

Standard Large Language Models (LLMs) are increasingly hitting a "long-context wall." As input sequences grow, the computational time and memory required for standard attention mechanisms increase quadratically. This leads to significant bottlenecks, most notably Key-Value (KV) cache bloat, which often results in Out-of-Memory (OOM) errors on consumer-grade hardware.

Existing solutions like sparse attention or standard Retrieval-Augmented Generation (RAG) have notable drawbacks. Sparse attention can sacrifice model capability, while standard RAG retrieves raw text that must be re-encoded, potentially overwhelming the model's processing capacity if the retrieved context is too long. MemLong addresses these issues by bridging the gap between external retrieval and internal model memory.

Architecture and Methodology

MemLong introduces a novel framework that extends the context window of LLMs up to 80k tokens on a single consumer-grade GPU (e.g., RTX 3090). The architecture is centered around two primary components: the Ret-Mem module and Retrieval Causal Attention.

The Ret-Mem Module

This external, non-differentiable component utilizes a pretrained dense embedder to convert text chunks into "Representation Embeddings." These embeddings are stored in a memory bank alongside their corresponding KV pairs extracted from the model's internal layers. By storing KV pairs instead of raw text, MemLong bypasses the "encoding bottleneck," allowing the model to receive information in the "language" of its own internal activations.

The MemLong architecture, highlighting the interaction between the frozen lower layers, the trainable upper layers, and the external Ret-Mem module.

Retrieval Causal Attention

Instead of re-processing raw tokens, MemLong retrieves the actual KV pairs of relevant historical chunks and injects them into the model's upper layers. This allows the model to attend to distant information as if it were part of the local context. To maintain consistency and prevent distribution shifts, the lower layers of the LLM are frozen, while the upper layers are fine-tuned using Low-Rank Adaptation (LoRA).

Experimental Results

The model was trained on a 0.5B token sample from the SlimPajama dataset and evaluated across four major long-text benchmarks: PG-19, BookCorpus, Wikitext-103, and Proof-Pile.

Context Extension and Perplexity

MemLong successfully extended the context window of OpenLLaMA-3B from its original 2k/4k limit to 80k tokens. On the PG-19 dataset at a 16k context length, MemLong achieved a perplexity of 9.73, outperforming vanilla models that typically OOM at such lengths. Notably, the researchers identified a "critical threshold" at a memory size of 65,536 tokens, where the model's ability to utilize historical information shows a substantial leap.

Evaluation of perplexity across different datasets (PG-19, Proof-Pile, BookCorpus, Wikitext-103) at various memory sizes and sequence lengths.

In-Context Learning (ICL)

In 20-shot learning tasks, MemLong improved accuracy by up to 10.2 percentage points over the baseline OpenLLaMA. This improvement stems from the model's ability to effectively retrieve and utilize examples stored within its long-term memory bank.

Conclusion and Future Work

MemLong demonstrates that retrieving internal KV pairs is a highly efficient alternative to standard text-based RAG for long-context modeling. While the current study focused on 3B parameter models, future work will explore scaling these techniques to 70B+ parameter models and investigating multi-layer KV retrieval to further stabilize the system.

Summary updated

BibTeX
@misc{liu2024memlong,
  author = {Weijie Liu and Zecheng Tang and Juntao Li and Kehai Chen and Min Zhang},
  title = {{MemLong: Memory-Augmented Retrieval for Long Text Modeling}},
  year = {2024},
  eprint = {2408.16967},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.16967},
}
Figure