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

Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, Chao Yang

Conference on Machine Learning and Systems 2025 · Day 3 · Session 7: Quantization and Sparsity

Overview

This article delves into SampleAttention, a novel approach designed to drastically accelerate inference for Large Language Models (LLMs) operating with exceptionally long context windows. Presented at MLSys 2025, the work by Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, and Chao Yang tackles the critical bottleneck of time to first token (TTFT) latency, which is exacerbated by the quadratic computational cost of the attention mechanism as sequence lengths grow. With LLMs now capable of processing millions of tokens, the traditional full attention computation becomes prohibitively expensive, rendering many interactive long-context applications impractical.

Watch on SlidesLive · Slides

Visual summary for SampleAttention: Near-Lossless Acceleration of Long Context LLM Inference with Adaptive Structured Sparse Attention by Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, Chao Yang
Visual summary for SampleAttention: Near-Lossless Acceleration of Long Context LLM Inference with Adaptive Structured Sparse Attention by Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, Chao Yang

Key moments

  1. 0:00 Introduction: High TTFT latency challenge in LLMs
  2. 2:00 Sparse attention: static vs. context-aware methods
  3. 3:20 Adaptive and dynamic nature of attention sparsity
  4. 4:30 CIA: Guiding metric for accuracy-efficiency trade-off
  5. 5:30 SampleAttention's two-stage sampling and filtering algorithm
  6. 7:00 Algorithmic and hardware optimizations for SampleAttention efficiency
  7. 8:00 Experimental results: Near-lossless accuracy and speed-up

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

Speakers: Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, Chao Yang

Conference: MLSys 2025

YouTube: https://www.youtube.com/watch?v=None

Overview

This article delves into SampleAttention, a novel approach designed to drastically accelerate inference for Large Language Models (LLMs) operating with exceptionally long context windows. Presented at MLSys 2025, the work by Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, and Chao Yang tackles the critical bottleneck of time to first token (TTFT) latency, which is exacerbated by the quadratic computational cost of the attention mechanism as sequence lengths grow. With LLMs now capable of processing millions of tokens, the traditional full attention computation becomes prohibitively expensive, rendering many interactive long-context applications impractical.

SampleAttention introduces an adaptive structured sparse attention mechanism that intelligently identifies and computes only the most crucial attention scores, thereby significantly reducing computational overhead while maintaining near-lossless accuracy. The core innovation lies in its ability to dynamically adapt sparsity patterns based on the input content, guided by a metric called Cumulative Residual Attention (CIA). This context-aware strategy, combined with sophisticated algorithmic and hardware optimizations, offers substantial speed-ups, making long-context LLMs more efficient and responsive for real-world interactive scenarios.

The significance of this research cannot be overstated. As LLMs become increasingly powerful and their context windows expand, the ability to perform inference efficiently is paramount for their widespread adoption in applications ranging from intelligent assistants to complex document analysis. By addressing the fundamental N-squared problem of attention, SampleAttention paves the way for a new generation of highly performant and interactive long-context AI systems, directly influencing the practical deployability and user experience of cutting-edge LLM technologies.

Background

▶ Watch: Introduction: High TTFT latency challenge in LLMs (0:00)

The rapid advancements in LLMs have pushed the boundaries of context window sizes, with models now routinely handling sequences exceeding 1 million tokens, and some even reaching 10 million. While this expanded context unlocks unprecedented capabilities, it simultaneously introduces a severe performance bottleneck during inference, primarily due to the attention mechanism. The computational complexity of full attention scales quadratically with the sequence length (N^2), meaning that as N grows, the time and resources required for attention calculations increase exponentially. This quadratic scaling is the primary driver of high Time to First Token (TTFT) latency, which refers to the delay before the first output token is generated. For interactive applications, high TTFT is a critical impediment, leading to frustrating user experiences. For instance, in a 1-million-token sequence, attention computation can account for nearly 90% of the total TTFT, making real-time interaction virtually impossible.

To mitigate this N^2 problem, sparse attention has emerged as a promising solution. The fundamental principle of sparse attention is to compute attention scores only for a subset of query-key pairs, rather than all possible pairs in a long sequence. Existing sparse attention methods generally fall into two categories:

  1. Static Mask Approaches: These methods employ predefined, fixed sparsity patterns that remain constant regardless of the input content. While simple to implement and incurring low overhead, their static nature often leads to reduced accuracy because they cannot adapt to the varying importance of different tokens in diverse inputs. Examples include block-sparse attention or sliding-window attention.
  2. Context-Aware Approaches: These methods dynamically determine the sparse pattern based on the specific input content at runtime. This adaptability allows them to achieve higher accuracy, often very close to that of full attention. However, the trade-off historically has been the additional precomputation or runtime overhead required to identify these dynamic patterns, which can sometimes negate the efficiency gains.

A crucial insight highlighted by the SampleAttention team is the inherent nature of attention sparsity itself. Research reveals that attention matrices are indeed highly sparse, often around 90% sparse. More importantly, this sparsity is not uniform or fixed; it is adaptive and dynamic. The level of sparsity varies significantly across different attention heads within the same model, changes considerably depending on the specific input content, and even differs between various LLM architectures. Furthermore, the patterns of sparsity are also highly dynamic, exhibiting diverse structures like column and slash patterns that shift across heads and inputs. This inherent dynamism is precisely why static sparse patterns are insufficient and why a sophisticated context-aware method capable of adapting not just the amount but also the pattern of sparsity at runtime is essential for balancing efficiency and accuracy effectively.

Key Findings

▶ Watch: Adaptive and dynamic nature of attention sparsity (3:20)

SampleAttention presents several pivotal findings that collectively redefine the landscape of efficient long-context LLM inference:

  1. Near-Lossless Accuracy with Significant Speed-ups: The primary achievement of SampleAttention is its ability to deliver near-lossless accuracy when compared to the highly optimized FlashAttention 2 baseline. This is coupled with substantial general inference speed-ups ranging from 1.24 times to 2.36 times across various long-context benchmarks. This demonstrates that efficiency gains do not necessitate a severe compromise in model quality.
  2. Dramatic TTFT Reduction for Ultra-Long Contexts: For extremely long sequences, specifically at 1 million tokens, SampleAttention achieves a dramatic 5.29 times reduction in Time to First Token (TTFT) latency compared to FlashAttention 2. This breakthrough is critical for enabling truly interactive applications with very large context windows, transforming previously impractical use cases into viable ones.
  3. Principled Efficiency-Accuracy Trade-off via Cumulative Residual Attention (CIA): The research identifies a strong, consistent correlation between the preserved Cumulative Residual Attention (CIA) mass and model accuracy. This finding provides a principled, quantifiable handle on the efficiency-accuracy trade-off, allowing practitioners to set a target CIA based on desired accuracy levels and dynamically adjust sparsity accordingly.
  4. Robust and Generalizable Automated Tuning: SampleAttention incorporates an automated offline tuning approach for its hyperparameters (CIA threshold, alpha C, alpha S, number of sampling trunks). A key discovery is the robustness of this tuning: hyperparameters optimized on a small-scale dataset for a specific sequence length range generalize effectively to other diverse tasks for the same model. This significantly reduces the overhead associated with deploying and optimizing sparse attention methods.
  5. Dynamic and Content-Dependent Sparsity Exploitation: The work empirically validates that attention is inherently sparse, but critically, this sparsity is highly dynamic and content-dependent. SampleAttention's adaptive approach, which dynamically selects and computes only essential attention scores, is shown to be far superior to static methods, effectively capturing the diverse sparsity patterns (e.g., column, slash) observed in real-world attention matrices.

These findings collectively demonstrate that SampleAttention provides a robust, efficient, and accurate solution to the long-standing challenge of quadratic attention scaling, making long-context LLMs practical for a broader range of interactive applications.

Technical Deep Dive

▶ Watch: CIA: Guiding metric for accuracy-efficiency trade-off (4:30)

The technical core of SampleAttention revolves around its ability to dynamically adapt sparse attention patterns in a content-aware manner, guided by a novel metric and implemented through a two-stage sampling and filtering algorithm.

At the heart of SampleAttention's strategy is the concept of Cumulative Residual Attention (CIA). The authors define CIA as the minimum amount of attention probability mass that must be preserved for each query after applying sparsity. Through empirical analysis, they discovered a strong and consistent correlation: higher preserved CIA directly translates to higher model accuracy. This provides a principled mechanism for controlling the efficiency-accuracy trade-off. By setting a target CIA threshold, one can determine the desired level of sparsity while ensuring acceptable performance.

The challenge, however, is that precisely calculating the CIA for a given query requires computing the full attention matrix, which is computationally expensive and defeats the purpose of sparse attention. SampleAttention overcomes this by approximating the CIA. This approximation is achieved by intelligently selecting a limited number of the previously identified key sparse patterns, such as column and slash patterns, which are shown to accurately represent the total attention mass needed for a given query.

The SampleAttention algorithm itself is a two-stage sampling and filtering process, meticulously designed for efficiency and accuracy:

  1. Stage 1: Trunk Sampling:
  • This initial stage aims to estimate the important attention scores across the entire attention matrix without performing a full computation.
  • Instead of randomly sampling individual query-key pairs, trunk sampling strategically samples queries from different segments or "chunks" of the input sequence.
  • This chunked approach provides a much more comprehensive and representative picture of the overall sparsity pattern compared to merely sampling from a localized area. It helps capture the global dependencies and identifies regions of the attention matrix that are likely to contain high-importance scores. The output of this stage is an initial, coarse estimation of attention scores across the sequence.
  1. Stage 2: Score-Based Filtering:
  • Leveraging the estimated scores from Stage 1, this stage refines the sparsity pattern.
  • Guided by the pre-defined desired CIA threshold, the algorithm efficiently identifies the specific key column and slash patterns that are essential to preserve the target attention mass.
  • A critical optimization introduced here is decoupled filtering. This technique drastically reduces the computational overhead required to find the correct sparse pattern. Instead of iteratively searching or computing complex metrics, the decoupled filtering allows for a more direct and faster identification of the relevant sparse blocks based on the estimated scores and CIA.
  • The ultimate output of this two-stage process is a dynamic block-level sparse mask. This mask is generated at runtime, based on the specific input content, and explicitly instructs the underlying hardware which attention scores to compute and which unimportant ones to skip, thereby optimizing computation.

The efficiency of SampleAttention stems from a synergistic combination of algorithmic innovations and hardware optimizations:

  • Adaptive Sparsity Exploitation: By dynamically selecting and computing only the essential attention scores, guided by the CIA metric, SampleAttention avoids the wasteful computations of full attention.
  • Efficient Pattern Selection: The two-stage process, particularly the decoupled filtering in Stage 2, ensures that the identification of sparse patterns itself is computationally lightweight, adding minimal overhead.
  • Hardware Acceleration: The design incorporates specific hardware-aware optimizations. This includes fusing sampling operators to minimize costly data movements between memory and processing units, and the utilization of an optimized sparse attention kernel that is specifically tailored to execute the dynamic block-level sparse masks efficiently on modern accelerators.
  • Built-in Performance Tuning: SampleAttention includes mechanisms for performance tuning, such as adjusting the CIA threshold and employing automatic tuning strategies. These allow for finding the optimal balance between efficiency and accuracy for specific tasks and varying context lengths, further enhancing practical deployability.

By integrating these components, SampleAttention provides a robust and highly efficient framework for accelerating long-context LLM inference, addressing the N^2 bottleneck without compromising model performance.

Experimental Setup & Results

▶ Watch: Algorithmic and hardware optimizations for SampleAttention efficiency (7:00)

To rigorously evaluate SampleAttention, the authors conducted extensive experiments on a standard A100 GPU, a common accelerator for LLM workloads. The evaluation focused on popular and representative LLMs, specifically ChatGLM4 and InternLM, to demonstrate broad applicability.

The performance of SampleAttention was benchmarked against the highly optimized FlashAttention 2, serving as a strong baseline for dense attention computation. The experiments spanned various challenging long-context tasks, utilizing established benchmarks such as the Ruler benchmark, LongBench, and InfiniteBench. These benchmarks cover a diverse range of real-world applications, including question answering, summarization, few-shot learning, and code completion, ensuring a comprehensive assessment of both accuracy and efficiency across different context lengths.

Key Performance Metrics and Headline Numbers:

  • Accuracy: SampleAttention consistently achieved near-lossless accuracy compared to FlashAttention 2 across all evaluated benchmarks and tasks. This is a critical result, demonstrating that the significant speed-ups do not come at the cost of substantial performance degradation.
  • General Inference Speed-up: Across various long-context scenarios, SampleAttention demonstrated significant speed-ups ranging from 1.24 times up to 2.36 times compared to FlashAttention 2. This indicates a consistent performance advantage across different configurations and tasks.
  • Time to First Token (TTFT) Speed-up for Ultra-Long Contexts: The most striking result was observed in micro-benchmarks specifically designed to measure TTFT for very long sequences. At a context length of 1 million tokens, SampleAttention achieved a dramatic 5.29 times reduction in TTFT compared to FlashAttention 2. This profound improvement is crucial for enabling interactive applications at such extreme context lengths.

Efficiency-Accuracy Trade-off Visualization:

The results were often visualized on an efficiency-accuracy curve, where each point represented SampleAttention under different configurations. The purple points consistently occupied a strong position, demonstrating SampleAttention's ability to navigate this critical trade-off effectively: a small, often negligible, sacrifice in accuracy yielded significantly higher speed-ups. This highlights the flexibility of the method to be tuned for specific application requirements.

Hyperparameter Tuning and Robustness:

The study also investigated the impact of SampleAttention's hyperparameters, including the CIA threshold, alpha C and alpha S (likely related to column and slash pattern selection), and the number of sampling trunks. These parameters directly influence the degree of sparsity and thus the efficiency-accuracy balance. To streamline deployment, the authors developed an automated offline tuning approach. This involved optimizing parameters for different sequence length ranges using a small-scale dataset. A key finding from this analysis was the robustness of the tuning: hyperparameters optimized on one task were found to generalize effectively to other diverse tasks for the same model. This significantly reduces the effort required for hyperparameter tuning in real-world deployments, making SampleAttention more practical and easier to adopt.

Sampling Overhead Analysis:

An important aspect of the TTFT benchmarks included an analysis of the sampling overhead introduced by SampleAttention's two-stage process. The results showed that the relative proportion of time spent on sampling decreased as the sequence length grew longer. This is a favorable scaling property, indicating that the overhead of determining the sparse mask becomes less significant as the computational burden of full attention increases, further enhancing SampleAttention's efficiency for extremely long contexts.

In summary, the experimental results unequivocally demonstrate SampleAttention's superior performance in accelerating long-context LLM inference. It achieves substantial speed-ups and dramatic TTFT reductions while maintaining near-lossless accuracy, validating its design principles and showcasing its potential for practical deployment.

Practical Implications

▶ Watch: Experimental results: Near-lossless accuracy and speed-up (8:00)

The development of SampleAttention carries significant practical implications for a wide range of stakeholders involved in the LLM ecosystem, from model builders and infrastructure teams to practitioners deploying these advanced AI systems.

For practitioners and application developers, SampleAttention directly addresses the critical issue of Time to First Token (TTFT) latency, which has historically been a major barrier for interactive long-context LLM applications. By achieving up to a 5.29x reduction in TTFT for 1-million-token sequences, SampleAttention transforms previously impractical use cases into viable ones. This means that applications requiring real-time interaction with vast amounts of information – such as intelligent assistants summarizing lengthy legal documents, code completion tools analyzing entire repositories, or research platforms sifting through academic papers – can now offer a much more responsive and fluid user experience. The ability to maintain near-lossless accuracy ensures that this performance gain does not come at the expense of model utility or output quality.

Infrastructure teams and ML engineers responsible for deploying and managing LLM inference systems will find SampleAttention highly beneficial for optimizing resource utilization and throughput. Reducing the computational cost of attention, especially the N^2 bottleneck, translates directly into lower GPU utilization per request or higher throughput for a given set of hardware resources. This can lead to significant cost savings in cloud deployments or enable the serving of more complex models on existing on-premise infrastructure. The dynamic nature of SampleAttention also means that it can adapt to varying input loads and context lengths, offering more consistent performance without the need for static, worst-case provisioning.

For model builders and researchers, SampleAttention provides a robust framework for exploring and deploying sparse attention mechanisms. The principled approach using Cumulative Residual Attention (CIA) offers a clear knob to tune the efficiency-accuracy trade-off, allowing for fine-grained control over model behavior. Furthermore, the discovery of robust and generalizable automated tuning for hyperparameters simplifies the process of integrating sparse attention into new models or adapting it to different tasks, reducing the iterative trial-and-error often associated with performance optimization.

However, like any advanced technique, SampleAttention comes with certain trade-offs and limitations that practitioners must consider:

  • Accuracy-Efficiency Trade-off: While SampleAttention achieves "near-lossless" accuracy, it's important to acknowledge that there might be a minuscule, context-dependent drop in accuracy compared to full dense attention. Practitioners need to evaluate if this marginal trade-off is acceptable for their specific application's requirements. The CIA threshold provides a mechanism to manage this balance.
  • Tuning Overhead (Initial): Although the automated tuning is robust and generalizes well, there is still an initial offline tuning phase required to optimize hyperparameters for specific model architectures and target sequence length ranges. This isn't a "plug-and-play" solution without any setup, but the one-time effort is significantly reduced by the robustness finding.
  • Future Challenges: The authors themselves point to future work, indicating current limitations. These include:
  • Load Balancing Issues: Especially when combining sparse attention with techniques like context parallelism for extremely long sequences, efficiently distributing the sparse computations across multiple devices or cores remains a challenge.
  • Sparse KV Cache Management: During the decoding phase, managing the KV cache for sparse attention, particularly intelligent offloading strategies for very long contexts, requires further optimization to fully realize performance gains.
  • Training with Sparse Attention: While SampleAttention focuses on inference, the full potential of sparse attention could be unlocked by training models from scratch with sparse attention mechanisms, which presents its own set of research and engineering challenges.

Despite these considerations, SampleAttention represents a significant leap forward in making long-context LLMs practical and performant for real-world interactive applications, offering a powerful tool for optimizing both user experience and computational resources.

Key Takeaways

  • The quadratic computational cost of the attention mechanism (N^2) is the primary bottleneck for Time to First Token (TTFT) latency in long-context Large Language Models (LLMs), making interactive applications impractical.
  • SampleAttention introduces an adaptive structured sparse attention method that dynamically identifies and computes only the most important attention scores, significantly accelerating LLM inference.
  • The approach leverages Cumulative Residual Attention (CIA) as a guiding metric, providing a principled way to balance efficiency and near-lossless accuracy by preserving critical attention probability mass.
  • SampleAttention employs a two-stage trunk sampling and score-based filtering algorithm to efficiently generate dynamic block-level sparse masks at runtime, combined with hardware optimizations for maximum efficiency.
  • Experimental results on A100 GPUs with models like ChatGLM4 and InternLM show near-lossless accuracy compared to FlashAttention 2, with general inference speed-ups of 1.24x to 2.36x.
  • Crucially, SampleAttention achieves a dramatic 5.29x reduction in TTFT for 1 million token sequences, transforming the feasibility of highly interactive long-context LLM applications.
  • An automated offline tuning approach for hyperparameters is robust and generalizable across diverse tasks for the same model, simplifying deployment and optimization efforts.

About the Speaker(s)

The paper "SampleAttention: Near-Lossless Acceleration of Long Context LLM Inference with Adaptive Structured Sparse Attention" was authored by Qianchao Zhu, Jiangfei Duan, Chang Chen, Dahua Lin, and Chao Yang. Unfortunately, the primary authors were unable to attend the MLSys 2025 conference due to visa problems. The presentation was instead delivered by Zaifeng, who explicitly stated they were not an author of the paper, but presented on behalf of Qianchao Zhu. Specific titles or company affiliations for the authors were not mentioned during the talk.

Reviews

Simon Wisk (Open Source Developer & AI Tooling Expert) — SOLID

SampleAttention is a legitimate systems paper solving a real problem — quadratic attention scaling at inference time — with a two-stage adaptive sparse attention algorithm guided by a principled metric (CIA). The 5.29x TTFT reduction at 1M tokens is the kind of number that makes engineers pay attention. But this article reads like a PR summary generated from the abstract and related work section, not a reconstruction of what actually happened in the room. The speaker wasn't an author, there's no code link, no architectural diagram walkthrough, and the 'technical deep dive' repeats the same high-level claims three times without getting into what decoupled filtering actually does…

Jensen Hitch (AI Compute Platform CEO) — SOLID

SampleAttention is competent, well-scoped inference optimization work that attacks a real structural constraint — the N-squared attention bottleneck — with a principled method. The CIA metric gives you a quantifiable handle on the efficiency-accuracy tradeoff, and the 5.29x TTFT reduction at 1 million tokens is a number that actually matters for production systems. The problem is the paper stops exactly where it gets interesting. They validate on a single A100, against a single baseline, with two models, and then list multi-GPU load balancing, sparse KV cache management, and training-time integration as future work. Those aren't footnotes — those are the system-level problems that…

→ Top-rated talks at Conference on Machine Learning and Systems 2025

All talks from Conference on Machine Learning and Systems 2025