TL;DR
Large Language Models (LLMs) struggle with long context windows due to the quadratic complexity of attention, leading to slow inference. This paper introduces SampleAttention, a novel sparse attention mechanism that dynamically adapts sparsity patterns and ratios at runtime. SampleAttention achieves near-lossless accuracy while significantly reducing inference time, outperforming existing methods.
What Problem Does This Paper Address?
Modern LLMs are increasingly capable of handling extremely long context windows, enabling applications like detailed document analysis and extended conversations. However, the core attention mechanism in these models has a computational complexity that scales quadratically with the input sequence length. This quadratic scaling becomes a major bottleneck for inference, particularly for the "Time-to-First-Token" (TTFT), which is crucial for interactive applications. Existing sparse attention methods often rely on static patterns or fixed sparsity ratios, failing to capture the dynamic and adaptive nature of attention sparsity across different heads, input content, and model architectures. This inflexibility limits their ability to achieve both high efficiency and accuracy.
Methods
The paper proposes SampleAttention, a two-stage approach to achieve adaptive structured sparse attention.
- Cumulative Residual Attention (CRA): The core insight is the introduction of CRA, a metric that measures the percentage of attention recall. CRA is shown to be a robust indicator of model accuracy, allowing for a principled trade-off between efficiency and accuracy.
- Query-Guided Chunked Sampling (Stage 1): To estimate attention scores efficiently, SampleAttention divides queries into chunks and performs sampling. This chunked approach, unlike previous methods that might only sample from the bottom, aims to better capture complex column and slash patterns across the attention matrix.
- Score-Based Key-Value Filtering (Stage 2): Based on the sampled attention scores and a desired CRA threshold, this stage dynamically identifies and selects a minimal set of important key-value indices. It decomposes the overall CRA threshold into separate thresholds for columns ($\alpha_c$) and slashes ($\alpha_s$), improving efficiency. This stage filters block-level indices based on aggregated attention scores.
- Hyperparameter Tuning: The method includes an automated, offline tuning process for hyperparameters (CRA thresholds $\alpha_c, \alpha_s$, and chunk number
chunkn) using compact validation datasets. This ensures optimal configurations for different models and sequence lengths. - Hardware-Efficient Implementation: SampleAttention incorporates IO-awareness by fusing operators to reduce overhead and utilizes an optimized sparse attention kernel, building upon FlashAttention2.
Datasets
The paper evaluates SampleAttention on various benchmarks and tasks, including:
- RULER: Used for evaluating understanding capabilities in long-context scenarios and for hyperparameter tuning.
- LongBench: A multi-task benchmark for long-context understanding.
- InfiniteBench: Designed to evaluate LLMs in contexts exceeding 200K tokens.
The models used for evaluation are ChatGLM4-9B, YI-9B, and InternLM2-7B.
Key Insights and Ideas
- Attention Sparsity is Dynamic: The paper strongly emphasizes that attention sparsity is not static but highly adaptive across attention heads, input content, and model architectures. This dynamic nature is the primary reason why static sparse attention methods are suboptimal.
- CRA as a Proxy for Accuracy: The introduction of Cumulative Residual Attention (CRA) as a reliable indicator of model accuracy is a key contribution. It provides a concrete metric to guide the sparsity selection process, ensuring that efficiency gains don't come at the cost of significant accuracy degradation.
- Column and Slash Patterns are Key: The research identifies "column" and "slash" patterns as fundamental structures within attention scores that, when combined, can effectively represent complex attention distributions. SampleAttention leverages these patterns for efficient filtering.
- Runtime Adaptability is Crucial: The success of SampleAttention hinges on its ability to determine sparsity ratios and patterns at runtime, allowing it to adapt to the specific needs of each attention head and input.
Key Results
- Significant TTFT Reduction: SampleAttention reduces Time-to-First-Token (TTFT) by up to 5.29x compared to FlashAttention2.
- Near-Lossless Accuracy: The method consistently achieves accuracy scores above 99% of full attention across various benchmarks, models, and sequence lengths, demonstrating "near-lossless" performance.
- Pareto Frontier: SampleAttention establishes a new Pareto frontier in the accuracy-efficiency trade-off, outperforming existing sparse attention methods like MInference, BigBird, and StreamingLLM.
- Robustness: The method shows consistent performance across different LLMs (ChatGLM4-9B, YI-9B, InternLM2-7B) and diverse tasks (RULER, LongBench, InfiniteBench).
- Scalability: The benefits of SampleAttention become more pronounced with increasing sequence lengths, with significant speedups observed for contexts up to 1 million tokens.
Limitations and Future Work
- Hyperparameter Tuning Overhead: While an automated tuning method is provided, the initial tuning process for hyperparameters still requires a compact validation dataset.
- Short Sequence Performance: For very short sequences, the performance gains are less pronounced due to the overhead of the dynamic sampling and index construction. The paper notes that in such cases, SampleAttention's latency is nearly identical to FlashAttention2.
- KV Cache Compression: The paper mentions that SampleAttention is orthogonal to KV cache compression techniques and can be combined with them to further reduce memory consumption. This suggests a potential avenue for future work to integrate these approaches for even greater efficiency.
Summary updated