Neural Attention Search

Difan Deng, Marius Lindauer

Abstract

Large Language Models (LLMs) face significant memory bottlenecks due to the linear growth of the Key-Value (KV) cache in long-context tasks. Neural Attention Search (NAtS) addresses this by framing KV cache pruning as a learnable architecture search problem. By categorizing tokens into Global, Local, and Sliding Window roles, NAtS enables models to autonomously manage their memory. Experimental results demonstrate that NAtS can reduce memory usage by up to 90% while maintaining 96% of full-attention performance, significantly outperforming static heuristic-based pruning methods.

Introduction

The expansion of LLM context windows to handle massive datasets—such as entire books or codebases—is limited by the physical constraints of VRAM. The KV cache, which stores states for all previous tokens to avoid redundant computation, grows linearly with sequence length. Traditional solutions rely on fixed heuristics (e.g., keeping the first and most recent tokens), which lack the flexibility to adapt to diverse tasks. NAtS proposes a shift from "hard-coded" rules to a learnable framework where the model identifies which tokens are critical for future predictions.

Methodology: The NAtS Search Space

NAtS introduces a differentiable search space for token importance, inspired by Neural Architecture Search (NAS). Each token is assigned one of three roles:

  1. Global Tokens: Retained indefinitely to serve as long-range anchors.
  2. Local Tokens: Temporary states that summarize local context before being evicted upon the appearance of a new Global token.
  3. Sliding Window Tokens: Retained for a fixed duration ($W$ tokens) to provide immediate context.
Figure 1: Visualization of attention patterns: (a) Full attention, (b) Sliding window, (c) and (d) hybrid patterns discovered by NAtS using Global, Local, and Sliding Window tokens.

To make the discrete selection of these roles differentiable, NAtS employs a learnable "Attention Score Layer" and the Gumbel-Softmax trick. This allows the model to optimize its memory management strategy jointly with its language modeling objective. A sparsity hyperparameter ($\lambda$) allows users to tune the "budget" for memory consumption.

Key Insights: Learning from Regret

A core innovation in NAtS is the "regret" gradient. If the model misclassifies a token (e.g., as "Local" when it was needed for a long-range dependency), the resulting gradient penalizes that classification. Over time, the model learns to prioritize tokens that are consistently relevant for future queries.

Figure 6: Illustration of the dynamic token role assignment and the pruning process for sliding window and global tokens.

Furthermore, NAtS reveals a "natural sparsity" in transformers. Even without aggressive pruning constraints, models often learn to discard over 60% of the KV cache, suggesting that current dense attention mechanisms are inherently redundant.

Experimental Results

The researchers evaluated NAtS on benchmarks including PG-19, RULER, and LongBench using Llama-3.1-8B and Mistral-7B.

  • Performance Retention: On the RULER benchmark, NAtS maintained 96% of full-attention performance while using a fraction of the memory.
  • Extreme Compression: NAtS achieved lower perplexity with 3% of the KV cache compared to baselines using 12.5%.
  • Hardware Efficiency: On an NVIDIA H100, NAtS enabled Llama 3.1 to process 700,000 tokens—a 3.5x increase over full-attention capacity.
  • Speed: The framework provided a 3.0x speedup in the pre-filling phase and a 2.24x reduction in memory footprint for 200k contexts.

Conclusion

Neural Attention Search demonstrates that the most effective way to manage LLM memory is to allow the model to optimize its own attention patterns. By moving beyond static heuristics to a learnable, task-adaptive approach, NAtS paves the way for more efficient long-context inference. Future work will explore extending this search to block-sparse and diagonal attention patterns to further enhance hardware efficiency.

Summary updated

BibTeX
@inproceedings{deng2025neural,
  author = {Difan Deng and Marius Lindauer},
  title = {{Neural Attention Search}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=h3gaIBwemp},
}
Figure