TL;DR
This paper introduces Memory-Keyed Attention (MKA), a novel hierarchical attention mechanism that efficiently handles long contexts in language models by dynamically routing queries across local, session, and long-term memory. A streamlined version, FastMKA, achieves significant speedups in training and inference with comparable accuracy to existing methods, making long-context LLMs more practical.
What Problem Does This Paper Address?
Large Language Models (LLMs) are increasingly being used for tasks requiring understanding of very long texts, such as summarizing lengthy documents or engaging in extended conversations. However, processing these long contexts comes with a major computational bottleneck: the Key/Value (KV) cache used in the attention mechanism. As the context length grows, the KV cache expands quadratically, leading to massive memory requirements and slow inference speeds. Existing solutions like Multi-Query Attention (MQA) and Multi-Latent Attention (MLA) try to mitigate this by sharing or compressing KV features, but they often compromise on the quality of representation or introduce their own overheads. This paper aims to solve the problem of efficient and effective long-context attention without sacrificing performance.
Methods
The core innovation is Memory-Keyed Attention (MKA), a hierarchical attention mechanism that organizes memory into three distinct levels:
- L1: Local Cache: This is the standard, immediate attention window, similar to what's used in typical Transformers. It's fast and resides on-chip (SRAM).
- L2: Session Memory: This level summarizes or compresses past context, acting as a medium-term memory. It's stored in High Bandwidth Memory (HBM).
- L3: Long-Term Memory: This stores historical attention patterns from much earlier in the context, using vectorized hashing for efficient retrieval from DRAM.
MKA dynamically learns to route each query token to the most relevant memory level using a learned routing gate.
To further boost efficiency, the authors propose FastMKA. This variant simplifies MKA by performing a "broadcast routing" step. Instead of separate attention computations for each memory level, FastMKA fuses the memory sources before the attention computation. This means a single attention pass over the fused memory, significantly reducing computational overhead while retaining the benefits of multi-level memory. FastMKA also incorporates block-wise softmax and GPU-efficient kernel fusion, inspired by FlashAttention.
Datasets
The paper evaluates its methods on the WikiText-2 (raw) dataset. This dataset is commonly used for language modeling tasks and consists of approximately 36,718 training sentences, 3,760 validation sentences, and 4,358 test sentences. The models were fine-tuned for one epoch on this dataset.
Key Insights and Ideas
- Hierarchical Memory is Key: The paper demonstrates that organizing memory into distinct, tiered levels (local, session, long-term) is crucial for efficiently handling long contexts. This mirrors how humans might recall information – immediate details, recent events, and distant memories.
- Dynamic Routing Matters: Simply compressing or sharing KV features (like in MQA/MLA) isn't enough. MKA's ability to dynamically route queries based on their relevance to different memory levels allows for more intelligent and efficient attention.
- Fusion for Speed: FastMKA's insight that fusing memory sources before attention can drastically reduce computation is a significant practical improvement. This "broadcast routing" is a clever way to get the benefits of multi-level memory without the cost of multiple attention passes.
- Hardware-Aware Design: The integration of techniques like block-wise softmax and tiled computation, inspired by FlashAttention, highlights the importance of hardware efficiency for making these advanced attention mechanisms practical.
Key Results
- Accuracy: MKA achieved competitive perplexity on WikiText-2, outperforming the baseline MHA and being comparable to MLA.
- Training Efficiency: FastMKA showed significant speedups, being up to 4x faster than MLA and GQA when training with 16K sequences. It also outperformed MHA by a wide margin.
- Inference Efficiency: FastMKA achieved comparable accuracy to MLA with up to 40% lower evaluation latency.
- Scalability: FastMKA scales linearly with context length, maintaining high throughput, unlike MQA and GQA which suffer from sharp training time growth.
Limitations and Future Work
- Computational Cost of Full MKA: The paper acknowledges that the full MKA formulation, with its repeated projections and attention computations across memory levels, can be computationally expensive. FastMKA is presented as a solution to this.
- Future Work: The authors suggest that MKA provides a practical and extensible framework. Future work could explore further optimizations, different memory retrieval strategies, and applying MKA to even larger models and longer context lengths. The paper also hints at the potential for MKA in memory-intensive inference settings where representation fidelity and cache reusability are critical.
Summary updated