HMT: Hierarchical Memory Transformer for Long Context Language Processing

Zifan He, Zongyue Qin, Neha Prakriya, Yizhou Sun, Jason Cong · DOI

Abstract

Standard Transformer architectures are constrained by a "context window" bottleneck due to the quadratic computational complexity of self-attention. The Hierarchical Memory Transformer (HMT) is a model-agnostic, "plug-and-play" framework designed to overcome these limitations by mimicking the human brain's memory strata. By organizing memory into sensory, short-term, and long-term layers, HMT enables Large Language Models (LLMs) to process virtually infinite context. Experimental results show that HMT achieves superior generation quality and improves perplexity by up to 25.5%, while utilizing up to 116x less inference memory than native long-context models.

1. Introduction

As Large Language Models (LLMs) are increasingly applied to complex tasks involving long documents, such as technical manuals or entire books, the limitations of the standard self-attention mechanism have become a primary bottleneck. Processing these sequences is prohibitively expensive in terms of VRAM and latency. While previous approaches have utilized recurrence or retrieval-based methods, they often rely on "flat" memory architectures that struggle to filter noise or require intrusive changes to the model's core architecture.

HMT addresses these challenges by introducing a structured, hierarchical memory system. This framework allows models to store and recall information efficiently without modifying the internal attention layers of the backbone model, making it compatible with a wide range of decoder-only architectures, including Llama, Mistral, and State-Space Models like Mamba.

2. The Hierarchical Memory Architecture

HMT implements a three-tiered memory system inspired by cognitive science to manage information flow across long sequences:

  1. Sensory Memory: This layer acts as a high-resolution buffer for the immediate past. It prepends the last $k$ token embeddings from the previous segment to the current processing window, ensuring local continuity.
  2. Short-term Memory: HMT utilizes a learnable "summary prompt" to condense the current segment into a single embedding ($S_n$). This embedding captures the "gist" or essential information of the current topic.
  3. Long-term Memory: A sliding window of these summary embeddings from all previous segments is maintained in a cache.

2.1 Retrieval Mechanism

Rather than simply passing the final hidden state forward, HMT employs a sophisticated retrieval mechanism. The model uses the current segment's summary as a query to perform cross-attention over the long-term memory cache. This allows the model to "recall" relevant historical context and inject it into the current processing step as a "memorization prompt" ($P_n$). This hierarchy naturally filters out irrelevant noise, focusing the model's attention on pertinent historical data.

3. Experimental Evaluation

The authors evaluated HMT across a broad spectrum of benchmarks to test its versatility and performance:

  • Language Modeling: Evaluated on Wikitext-103 and PG-19 (Project Gutenberg) for next-token prediction accuracy.
  • Question Answering: Tested on a modified version of PubMedQA specifically designed for multi-context reasoning.
  • Long-Context Benchmarks: Performance was measured on LongBench, covering tasks such as summarization and multi-document QA.
  • Scalability: The framework was tested on models ranging from 135M parameters (SmolLM) to 14B parameters (Qwen 2.5), demonstrating robust scaling.

4. Key Results and Insights

4.1 Performance and Efficiency

HMT-augmented models demonstrated significant improvements over baseline architectures. Key metrics include:

  • Perplexity Gains: An improvement in perplexity by up to 25.5% and a 1.0% increase in prediction accuracy on long-sequence datasets.
  • Memory Efficiency: Compared to specialized long-context LLMs, HMT utilized 2.5x to 116x less inference memory while maintaining or exceeding generation quality.
  • Competitive Advantage: HMT consistently outperformed other memory-augmented methods, such as RMT and LongMem. Notably, on the PG-19 dataset, HMT was 9.9% more effective than the recent HOMER method.

4.2 Gradient Stability

A significant challenge in training recurrent models is the vanishing or exploding gradient problem. HMT’s retrieval mechanism addresses this by creating "shortcuts" in the backpropagation path, facilitating more stable training compared to traditional recurrent methods like RMT.

4.3 The Power of Efficient Memory

One of the most striking findings is that smaller models equipped with HMT can outperform significantly larger models. For example, an OPT 2.7B model with HMT outperformed an OpenLlama 3B model on long-context tasks, suggesting that sophisticated memory management can be more impactful than raw parameter count.

5. Limitations and Future Work

While HMT offers a significant leap in efficiency, certain challenges remain. As the long-term memory window ($N$) expands to millions of segments, the overhead of cross-attention may become a factor. Future research may explore "intelligent memory prefetching" to mitigate this. Additionally, while HMT simplifies the training process, Backpropagation Through Time (BPTT) remains computationally intensive. Finally, the authors suggest exploring multi-level long-term memory hierarchies, analogous to L1/L2/L3 cache structures in modern CPUs, to further refine information retrieval.

6. Conclusion

The Hierarchical Memory Transformer provides a scalable, efficient, and model-agnostic solution for long-context processing. By mimicking biological memory structures, HMT allows LLMs to maintain high performance across vast contexts while drastically reducing the computational and memory costs associated with standard attention mechanisms.

Summary updated

BibTeX
@inproceedings{he2025hmt,
  author = {Zifan He and Zongyue Qin and Neha Prakriya and Yizhou Sun and Jason Cong},
  title = {{HMT: Hierarchical Memory Transformer for Long Context Language Processing}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.naacl-long.410},
}
Figure