[Paper Review] HyperAttention: Long-context Attention in Near-Linear Time
HyperAttention introduces a near-linear time approximate attention method that uses a perturbation-tolerant diagonal D approximation and AMM with row-norm based sampling, enabling causal masking and practical speedups on long contexts.
We present an approximate attention mechanism named HyperAttention to address the computational challenges posed by the growing complexity of long contexts used in Large Language Models (LLMs). Recent work suggests that in the worst-case scenario, quadratic time is necessary unless the entries of the attention matrix are bounded or the matrix has low stable rank. We introduce two parameters which measure: (1) the max column norm in the normalized attention matrix, and (2) the ratio of row norms in the unnormalized attention matrix after detecting and removing large entries. We use these fine-grained parameters to capture the hardness of the problem. Despite previous lower bounds, we are able to achieve a linear time sampling algorithm even when the matrix has unbounded entries or a large stable rank, provided the above parameters are small. HyperAttention features a modular design that easily accommodates integration of other fast low-level implementations, particularly FlashAttention. Empirically, employing Locality Sensitive Hashing (LSH) to identify large entries, HyperAttention outperforms existing methods, giving significant speed improvements compared to state-of-the-art solutions like FlashAttention. We validate the empirical performance of HyperAttention on a variety of different long-context length datasets. For example, HyperAttention makes the inference time of ChatGLM2 50\% faster on 32k context length while perplexity increases from 5.6 to 6.3. On larger context length, e.g., 131k, with causal masking, HyperAttention offers 5-fold speedup on a single attention layer.
Motivation & Objective
- Motivation: to overcome quadratic time and memory in attention for long sequences in Transformers.
- Goal: design an approximate attention mechanism with spectral guarantees that works under long contexts and causal masking.
- Aim: achieve near-linear time while maintaining performance on real LLMs and long-context benchmarks.
Proposed method
- Define a sampling-based approximation for the attention output that preserves spectral properties.
- Use Hamming sorted LSH to locate large entries in the attention matrix and form a sparse dominant-entry mask.
- Approximate the diagonal scaling D with a near-linear time estimator that relies on uniform sampling of keys/rows.
- Perform Approximate Matrix Multiplication by sampling rows of V according to squared row-norms to approximate D^{-1}AV.
- Extend the approach to causal masking via a recursive block-partitioning scheme.
- Provide a main theoretical guarantee (HyperAttention) that the output approximates Att within an epsilon spectral norm error with near-linear runtime.

Experimental results
Research questions
- RQ1Can we achieve near-linear time approximation for dot-product attention under long context lengths while preserving spectral properties?
- RQ2Can heavy-entry detection plus row-norm-based sampling provide practical and provable guarantees without requiring bounded entries or low stable rank?
- RQ3How does HyperAttention perform with causal masking and on pretrained LLMs for long-context benchmarks?
Key findings
- HyperAttention yields near-linear time complexity for attention approximation under mild assumptions.
- Empirically, it yields substantial speedups (e.g., over 50x faster in forward/backward for 131k context without masking; ~5x with causal masking) while maintaining competitive perplexity on long-context tasks.
- When integrated into pretrained models (e.g., ChatGLM2-6B-32k, Phi-1.5) via monkey patches, it provides significant speedups with modest performance degradation on some tasks, notably preserving quality in summarization and code tasks.
- The method supports causal masking via a recursive partitioning scheme (Algorithm 4) and remains effective across long-context datasets (LongBench).
- An empirical assumption about row-norms of the softmax-attention matrix (alpha bound) holds in practice, supporting the method’s feasibility.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.