Back to Unilm

Rectified Sparse Attention (ReSA)

ReSA/README.md

latest3.8 KB
Original Source

Rectified Sparse Attention (ReSA)

📝 TL;DR

Rectified Sparse Attention (ReSA) improves sparse decoding by periodically refreshing the KV cache, achieving near-lossless quality and up to 2.42x speedup at 256K context length.

📝 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.42x end-to-end speedup under decoding at 256K sequence length, making it a practical solution for scalable long-context inference.

Method Overview

Performance Comparison on Math Reasoning Tasks

SettingGaokao2023EnMinervaOlympiadBenchAIME24AMC23Avg
R1-Qwen-Distill 1.5B
Dense71.628.740.827.465.646.82
Sparse67.929.038.721.360.643.50
ReSA71.828.139.523.065.445.56
Avg Length4915.86390.88991.612126.47866.48058.2
R1-Qwen-Distill 7B
Dense73.840.452.348.189.060.72
Sparse72.938.148.446.183.157.72
ReSA73.539.752.351.186.060.52
Avg Length2889.94018.77520.010474.55732.26127.1

Table: Performance comparison on math reasoning tasks. ReSA maintains near-lossless performance, whereas sparse decoding alone degrades as decoding progresses.

🚀 Getting Started

Usage

Pretrained Model Preparation

Download the pretrained model to /path/to/pretrained/. For e.g.:

sh
huggingface-cli download deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B --local-dir /path/to/pretrained/ --local-dir-use-symlinks False

Math Evaluation

Evaluate on math data with:

sh
bash scripts/local_eval_math.sh

Replace /path/to/pretrained/ in the script with your pretrained model path. Replace /path/to/output/ with the path to save evaluation results. Pass in the configuration of ReSA using save_feature.

Collect Math Evaluation Results

Install the needed packages:

sh
bash scripts/setup_math_eval.sh

Collect math evaluation results from the output file:

sh
bash scripts/math_eval_result.sh /path/to/output/ file_name

Replace file_name with your math result file, for e.g., DeepSeek-R1-Distill-Qwen-1.5B_resa_0.1_32_local.jsonl

📚 References

(To be completed)

Acknowledgements

This project incorporates code from Qwen2.5-Math. We appreciate the contributions of the original authors.