Streaming Attention Approximation via Discrepancy Theory

In‐Su Han, Michael Kapralov, Ekaterina Kochetkova, Kshiteej Sheth, Amir Zandieh · DOI

Abstract

Large language models (LLMs) have achieved impressive success, but their high memory requirements present challenges for long-context token generation. In this paper we study the streaming complexity of attention approximation, a key computational primitive underlying token generation. Our main contribution is BalanceKV, a streaming algorithm for $\epsilon$-approximating attention computations based on geometric process for selecting a balanced collection of Key and Value tokens as per Banaszczyk's vector balancing theory. We complement our algorithm with space lower bounds for streaming attention computation. Besides strong theoretical guarantees, BalanceKV exhibits empirically validated performance improvements over existing methods, both for attention approximation and end-to-end performance on various long context benchmarks.

Introduction

The rapid advancement of Large Language Models (LLMs) has enabled sophisticated tasks such as long-document summarization and complex code generation. However, these capabilities are often hampered by the linear growth of the "KV cache"—the storage of Key and Value vectors for all preceding tokens—relative to the sequence length. In long-context scenarios, this cache can consume gigabytes of VRAM, leading to significant memory bottlenecks and increased latency during token generation.

Existing approaches to mitigate this issue typically involve pruning tokens based on simple heuristics (e.g., keeping only the most recent or most frequent tokens) or applying quantization. While these methods offer some relief, they often lack rigorous theoretical guarantees and can fail in tasks requiring precise retrieval from distant parts of the context. This research introduces BalanceKV, a streaming algorithm that leverages discrepancy theory to provide a provably accurate approximation of the attention mechanism with a reduced memory footprint.

Methodology: BalanceKV and Discrepancy Theory

The core innovation of this work is the application of Discrepancy Theory—a mathematical field focused on partitioning sets into balanced groups—to the problem of KV cache compression.

SoftmaxBalance

The foundational offline algorithm, SoftmaxBalance, treats the attention mechanism as a weighted sum of vectors. By utilizing a "Self-Balancing Walk" (a geometric sampling process), the algorithm partitions the set of tokens into two groups. This partitioning ensures that one group effectively "balances" the other, allowing for 50% compression with minimal approximation error.

The Streaming Framework: Merge-and-Reduce

To adapt this approach for real-time token generation, the authors developed a Merge-and-Reduce framework. This framework organizes tokens into a binary tree structure of "buckets." As new tokens arrive, they fill these buckets. Once two buckets at the same level are full, they are merged, and the SoftmaxBalance algorithm is applied to prune the combined set back to the original bucket size. This hierarchical approach allows BalanceKV to maintain a fixed-size memory budget regardless of the total sequence length.

Theoretical Foundations

Unlike heuristic methods, BalanceKV is supported by a formal $\epsilon$-approximation guarantee. Furthermore, the authors establish a space lower bound, proving the fundamental memory requirements for any streaming algorithm attempting to approximate attention. This defines the theoretical limits of KV cache compression.

Experimental Evaluation

The effectiveness of BalanceKV was evaluated across several rigorous benchmarks:

  • LongBench-E: A comprehensive suite for long-context understanding, including multi-document QA, summarization, and code completion.
  • TriviaQA: Utilized for single-layer ablation studies to precisely measure approximation error.
  • Needle-In-A-Haystack: A stress test requiring the model to retrieve a specific fact buried within contexts of up to 100,000 tokens.

Results and Discussion

BalanceKV demonstrates significant improvements over state-of-the-art baselines in both theoretical and empirical metrics.

Superior Retrieval and Accuracy

In the "Needle-In-A-Haystack" test, BalanceKV achieved a near-perfect 0.99 average accuracy. This performance significantly outperforms popular heuristics such as SnapKV (0.83) and PyramidKV (0.90) at equivalent compression levels. Across models like Llama-3.1-8B and Qwen2.5-32B, BalanceKV consistently maintained lower relative errors in attention approximation compared to uniform sampling.

End-to-End Performance and Efficiency

On the LongBench-E benchmark, BalanceKV achieved the highest average scores across nearly all tasks compared to other compression methods like StreamingLLM and SnapKV. Crucially, the algorithm is computationally efficient; it demonstrated the lowest prefill latency among tested methods, making it highly suitable for real-world deployment.

Key Insights

The success of BalanceKV stems from the realization that attention is not merely about identifying "important" tokens, but about preserving the geometric distribution of the entire token set. A "balanced" subset, as defined by discrepancy theory, represents the global context more accurately than simple top-k selection.

Conclusion and Future Work

BalanceKV represents a significant step forward in making long-context LLM inference more efficient without sacrificing accuracy. While the current theoretical proofs assume bounded $\ell_2$ norms for embeddings, future research could explore handling extreme outliers. Additionally, while the prefill stage is highly efficient, the Merge-and-Reduce logic introduces implementation complexities that could be further optimized through custom hardware kernels. A promising future direction is the integration of discrepancy-based pruning with quantization techniques to achieve even higher levels of compression.

Summary updated

BibTeX
@inproceedings{han2025streaming,
  author = {In‐Su Han and Michael Kapralov and Ekaterina Kochetkova and Kshiteej Sheth and Amir Zandieh},
  title = {{Streaming Attention Approximation via Discrepancy Theory}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=p3HBEtNDRY},
}
Figure