Abstract
Efficient long-sequence generation is a critical challenge for Large Language Models. While recent sparse decoding methods improve efficiency, they suffer from KV cache misalignment, where approximation errors accumulate and degrade generation quality. In this work, we propose Rectified Sparse Attention (ReSA), a simple yet effective method that combines block-sparse attention with periodic dense rectification. By refreshing the KV cache at fixed intervals using a dense forward pass, ReSA bounds error accumulation and preserves alignment with the pretraining distribution. Experiments across math reasoning, language modeling, and retrieval tasks demonstrate that ReSA achieves near-lossless generation quality with significantly improved efficiency. Notably, ReSA delivers up to 2.42$\times$ end-to-end speedup under decoding at 256K sequence length, making it a practical solution for scalable long-context inference. Code is available at https://aka.ms/ReSA-LM.
Introduction
As Large Language Models (LLMs) scale to process millions of tokens, the Key-Value (KV) cache becomes a primary bottleneck in terms of memory and computation. Standard dense attention requires attending to all previous tokens, leading to quadratic complexity. Sparse decoding strategies have emerged to mitigate this by attending only to a subset of tokens. However, these approximations introduce "KV cache misalignment." Because sparse attention is an approximation, the hidden states of newly generated tokens deviate from the pretraining distribution. These errors accumulate over long sequences, eventually causing significant performance degradation in complex reasoning tasks.
Methodology: Rectified Sparse Attention (ReSA)
ReSA addresses the misalignment problem by introducing periodic "rectification" steps into a sparse decoding framework. The method alternates between two primary phases:
Group Block Sparse Attention (GBSA)
For most decoding steps, ReSA employs a query-dependent sparsity pattern. The KV cache is partitioned into blocks, and a "Quest-like" algorithm selects the most relevant blocks based on statistical descriptors (e.g., min/max values). To optimize hardware utilization, ReSA enforces a shared sparsity pattern across different attention heads within the same Group-Query Attention (GQA) group.
Periodic Dense Rectification
The core innovation of ReSA is the periodic "refresh" of the KV cache. Every $f$ tokens (typically $f=32$), the model performs a dense rectification step. Instead of generating a new token using sparse attention, it takes the last $f$ generated tokens and re-processes them using full dense attention in parallel. This step replaces the approximate KV cache entries with exact values, effectively resetting the accumulation of approximation errors to zero.
Experimental Evaluation
The authors evaluated ReSA using Qwen2.5-7B and DeepSeek-R1-Qwen-Distill models across several benchmarks:
- Mathematical Reasoning: Minerva Math, Gaokao 2023, OlympiadBench, AIME24, and AMC23.
- Language Modeling: Top-3 accuracy on long-sequence book data.
- Long-Sequence Retrieval: The RULER benchmark for needle-in-a-haystack tasks.
Performance and Quality
ReSA demonstrates near-lossless quality compared to full dense attention. On math reasoning tasks, where pure sparse methods often fail due to logical drift, ReSA maintains high accuracy. For the R1-Qwen-Distill 7B model, ReSA achieved an average score of 60.52, nearly matching the dense baseline of 60.72.
Efficiency and Scalability
By amortizing the cost of the dense rectification pass over $f$ tokens, ReSA maintains high throughput. The overhead of rectification is minimal compared to the savings from sparse decoding. As shown in Figure 6, ReSA's latency scales much more favorably than dense attention as context length increases. At a 256K context length, ReSA achieves a 2.28\times speedup in FP16 and up to 2.44\times in INT4 precision.
Conclusion
Rectified Sparse Attention (ReSA) provides a robust solution to the KV cache misalignment problem in long-context LLM inference. By bounding approximation errors through periodic dense rectification, ReSA preserves the generation quality of dense models while offering the efficiency of sparse attention. It is a training-free, drop-in optimization that significantly improves the scalability of long-sequence generation. Future work includes further hardware-level optimizations and potential integration with sparsity-aware training.
Summary updated