Needle in the Haystack for Memory Based Large Language Models

Elliot Nelson, Georgios Kollias, Payel Das, Subhajit Chaudhury, Soham DanarXiv 2024

Abstract

Current large language models (LLMs) often perform poorly on simple fact retrieval tasks. Here we investigate if coupling a dynamically adaptable external memory to a LLM can alleviate this problem. For this purpose, we test Larimar, a recently proposed language model architecture which uses an external associative memory, on long-context recall tasks including passkey and needle-in-the-haystack tests. We demonstrate that the external memory of Larimar, which allows fast write and read of an episode of text samples, can be used at test time to handle contexts much longer than those seen during training. We further show that the latent readouts from the memory (to which long contexts are written) control the decoder towards generating correct outputs, with the memory stored off of the GPU. Compared to existing transformer-based LLM architectures for long-context recall tasks that use larger parameter counts or modified attention mechanisms, a relatively smaller size Larimar is able to maintain strong performance without any task-specific training or training on longer contexts.

Introduction

Standard Transformer-based LLMs face a significant "context window" bottleneck. The self-attention mechanism scales quadratically ($O(L^2)$) with input length, making the processing of very long documents prohibitively expensive in terms of GPU memory and compute time. Furthermore, as contexts expand, models often suffer from the "lost in the middle" phenomenon, where they struggle to retrieve specific facts (the "needle") buried within a massive amount of irrelevant text (the "haystack").

Researchers at IBM have introduced a novel approach to provide LLMs with effectively "infinite" context without the massive computational overhead. By coupling a 1.3B parameter model called Larimar with an external associative memory, they achieved near-perfect retrieval on 1-million-token tasks—despite the model only being trained on sequences of 384 tokens.

Methodology: The Larimar Architecture

The Larimar architecture consists of a standard encoder-decoder paired with a Linear Associative Memory module. The system operates through several key mechanisms:

  1. Writing to Memory: Instead of maintaining all tokens in the active GPU cache, input text is divided into segments (sentences). Each segment is encoded into a latent vector ($z$). These vectors are written into a memory matrix ($M$) using a least-squares solution, storing a mapping between a "key" (derived from the text) and the "value" (the encoded information).
  2. Reading from Memory: When answering a query, the model uses a "prefix" (the first few words of the query) to generate a search key. This key retrieves the most relevant latent vector from the memory matrix, which then guides the decoder to generate the final answer.
  3. CPU Offloading: Crucially, the memory matrix and its update operations are kept on the CPU. This allows the context window to scale to millions of tokens without increasing the VRAM footprint on the GPU.
  4. Zero-Shot Generalization: Unlike models that require long-sequence training, Larimar was trained on contexts of only 384 tokens but can handle 1M+ tokens at inference by simply expanding its external memory matrix.

Experimental Evaluation

The model was pre-trained on a subset of Wikipedia and evaluated using two primary benchmarks:

  • Passkey Test: A random 5-digit "passkey" is hidden within a large volume of filler sentences ("The grass is green").
  • Needle-in-the-Haystack: A more complex test using Paul Graham’s essays as the "haystack," with a specific fact (the "needle") inserted at various positions.

Key Results

Larimar demonstrates exceptional performance in long-context retrieval, often outperforming much larger models. On a 137K context, Larimar maintained a 0.95 recall rate for 3-digit numbers and perfect recall for the "San Francisco" needle test, significantly exceeding the performance of Mistral 7B and Phi-3-mini.

Comparison of Larimar against other LLMs on needle-in-the-haystack tasks.

Furthermore, Larimar 1.3B achieved 100% accuracy on the passkey test at both 128K and 1M token lengths. In comparison, the Infini-Transformer (without specific fine-tuning) saw accuracy drop to approximately 10% at 128K tokens.

Performance comparison between Larimar and Infini-Transformer at extreme context lengths.

Key Insights

  • Training vs. Inference Length: A model trained on tiny snippets can perform perfectly on massive documents if the retrieval mechanism is decoupled from the attention mechanism.
  • Prefixes as Anchors: Using a short, fixed-length prefix as a key for associative memory significantly improves retrieval accuracy compared to using full sentence encodings.
  • Order Invariance: Because the memory is associative, the model is naturally invariant to information location, performing equally well regardless of where the "needle" is placed in a 100,000-word document.

Conclusion and Future Work

While Larimar excels at fact retrieval, it currently faces limitations regarding global structure. Because segments are written to memory in isolation, the model may lose cross-segment relationships and specific sequence order, potentially impacting tasks requiring deep narrative flow or multi-part reasoning. Future research will explore more sophisticated, task-relevant key computation and methods to better preserve global context while maintaining the efficiency of the associative memory system.

Summary updated

BibTeX
@misc{nelson2024needle,
  author = {Elliot Nelson and Georgios Kollias and Payel Das and Subhajit Chaudhury and Soham Dan},
  title = {{Needle in the Haystack for Memory Based Large Language Models}},
  year = {2024},
  eprint = {2407.01437},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.01437},
}
Figure