SampleAttention: Near-Lossless Acceleration of Long Context LLM Inference with Adaptive Structured Sparse Attention

Qianchao Zhu, Jiangfei Duan, Chang Chen, Siran Liu, Guanyu Feng, Xin Lv, Xiao Chuanfu, Dahua Lin, Chao Yang

TL;DR

SampleAttention is a novel, training-free approach that dramatically accelerates long-context Large Language Model (LLM) inference by dynamically filtering out unimportant attention tokens at runtime. By introducing a robust accuracy proxy called Cumulative Residual Attention (CRA), it achieves up to a 5.29× reduction in Time-to-First-Token (TTFT) latency compared to FlashAttention2 while maintaining near-lossless (>99%) model accuracy.

What Problem Does This Paper Address?

As LLMs scale to support context windows of over a million tokens, processing the initial prompt (the prefill phase) becomes a massive computational bottleneck. Because the standard attention mechanism scales quadratically ($O(N^2)$) with sequence length, calculating the attention matrix for a 1-million-token prompt can take over 25 minutes on an enterprise GPU like the NVIDIA A100, dominating the Time-to-First-Token (TTFT) latency.

While attention scores are inherently sparse, existing sparse attention methods fall short. They either rely on static, predefined patterns or enforce a fixed sparsity budget. This fails because attention sparsity is highly dynamic—it changes drastically across different attention heads, input contents, and model architectures.

Methods

To capture this dynamic sparsity without expensive re-training, the authors present SampleAttention, which relies on two core innovations:

  1. Cumulative Residual Attention (CRA): The authors found that downstream model accuracy highly correlates with attention recall—the sum of the remaining attention probabilities after sparsification. CRA serves as a runtime metric to dynamically guide how many tokens can be safely ignored.
  2. Two-Stage Query-Guided Key-Value Filtering:
  3. Stage 1 (Chunked Sampling): The algorithm splits queries into equidistant chunks and computes token-level attention scores for just a few queries. This provides a low-overhead estimation of the overall attention matrix structure.
  4. Stage 2 (Score-Based Block Filtering): The sampled scores are reduced to block-level granularity along two primary structural dimensions: columns (representing global context/attention sinks) and slashes (representing local window patterns). Independent thresholds ($\alpha_c, \alpha_s$) are applied to dynamically select the minimal set of key-value blocks needed to satisfy the target CRA.
Overview of SampleAttention's two-stage implementation

The authors also implemented hardware-aware optimizations, fusing small operators (like softmax and reduction) to minimize GPU I/O overhead.

Datasets

The method was evaluated across three popular long-context benchmarks using off-the-shelf models (ChatGLM4-9B, YI-9B, and InternLM2-7B):

  • RULER: Evaluates complex long-context behaviors (like multi-hop tracing and aggregation) across sequence lengths up to 128K.
  • LongBench: A multi-task benchmark covering QA, summarization, and code completion (lengths from 4K to 35K).
  • InfiniteBench: Specifically designed for extreme context lengths exceeding an average of 200K tokens.

Key Insights and Ideas

  • Sparsity is Multi-Dimensional: Attention sparsity isn't uniform. It is Head-Specific (some heads show 27.4% sparsity while others hit 99.8% in the same layer), Content-Aware (sparsity naturally increases as context grows), and Model-Aware. Enforcing a flat sparsity budget across an entire model severely damages accuracy.
Figure 3. The sparsity ratio of ChatGLM3 (28 layers×32 heads) and InternLM2 (32 layers×32 heads), evaluated over different tasks during prefill. The sparsity ratio varies across different atten- tion heads, input contents and model architectures.
  • Structured Patterns Dominate: Even when dynamic, the important attention weights consistently manifest as structured "column stripes" and "slash stripes". This structure allows block-level filtering, which is highly friendly to modern GPU hardware architectures.
Visualization of column and slash attention patterns
  • CRA is a Reliable Proxy: Instead of optimizing for a specific task score, optimizing for attention recall (CRA) consistently translates to stable downstream task performance across different domains.

Key Results

  • Massive Speedups: For a 1-million token context window, SampleAttention reduces TTFT latency by 5.29× compared to FlashAttention2.
  • Near-Lossless Accuracy: Across RULER, LongBench, and InfiniteBench, SampleAttention consistently maintained over 99% of the full-attention baseline accuracy, outperforming existing baselines like MInference, BigBird, and HyperAttention.
  • Pareto Frontier: Unlike prior methods that trade away significant accuracy for speed, SampleAttention establishes a new Pareto frontier, delivering both high acceleration and high precision.
Figure 1. Compared to previous static and dynamic sparse attention methods, SampleAttention captures adaptive structured sparse patterns for each head. It achieves a significant reduction in TTFT compared to FlashAttention2.

Limitations and Future Work

  • Short-Sequence Overhead: In short-sequence scenarios (e.g., less than 8K tokens), the computational overhead of dynamic sampling and index construction outweighs the sparse computation savings, making performance nearly identical to FlashAttention2.
  • Focus on Prefill Only: SampleAttention specifically targets the prefill (prompt processing) stage. While it preserves an uncompressed KV cache for the decoding phase, it does not inherently reduce decoding memory footprint. However, the authors note that SampleAttention is orthogonal to, and can be combined with, existing KV cache eviction methods to tackle memory constraints during generation.

Summary updated

BibTeX
@inproceedings{zhu2024sampleattention,
  author = {Qianchao Zhu and Jiangfei Duan and Chang Chen and Siran Liu and Guanyu Feng and Xin Lv and Xiao Chuanfu and Dahua Lin and Chao Yang},
  title = {{SampleAttention: Near-Lossless Acceleration of Long Context LLM Inference with Adaptive Structured Sparse Attention}},
  booktitle = {MLSys Withshepherding},
  year = {2024},
  url = {https://openreview.net/forum?id=RuZ80yl71h},
}
Figure