SpargeAttn

thu-mlApache-2.0更新放缓

免训练稀疏注意力方案,可直接接入任意模型推理,加速 LLM、视觉 Transformer 和视频生成,无需重新训练即可降低注意力计算开销。

[ICML2025] SpargeAttention: A training-free sparse attention that accelerates any model inference.

暂无版本1,267star108fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install ninja
python setup.py install
硬件要求按 README 说明

README 未给出显存要求;需 CUDA,Blackwell 需 >=12.8,Ada fp8 需 >=12.4,Hopper fp8 需 >=12.3,Ampere 需 >=12.0。

需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

免训练稀疏注意力,直接替换 SDPA 加速语言、图像、视频模型推理

免训练稀疏注意力方案,可直接接入任意模型推理,加速 LLM、视觉 Transformer 和视频生成,无需重新训练即可降低注意力计算开销。

核心能力

  • 用 spas_sage2_attn_meansim_topk_cuda 即插即用替换 SDPA
  • 调 topk 在注意力精度与稀疏度之间权衡
  • 支持按注意力头自定义 128×64 块稀疏掩码
  • 基于 SageAttention2 实现,含 Triton Kernel 示例

容易踩的坑

需 python>=3.9、torch>=2.3.0,且 CUDA 版本要匹配对应 GPU 架构;自定义掩码 API 的 mask_id 形状需按 batch、head 和 128×64 分块对齐;README 示例中 block_sparse_sage2_attn_cuda 调用末尾多了一个冒号,直接复制会报语法错误。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    SpargeAttention

    Recommended API

    We highly recommend using the spas_sage2_attn_meansim_topk_cuda and block_sparse_sage2_attn_cuda APIs. They are plug-and-play and customizable:

    Plug-and-Play API

    from spas_sage_attn import spas_sage2_attn_meansim_topk_cuda
    
    attn_output = spas_sage2_attn_meansim_topk_cuda(q, k, v, topk=0.5, is_causal=False)
    

    You can adjust topk to balance between attention accuracy (higher topk is more accurate) and sparsity (lower topk is more sparse).

    Customize your Block-Sparse Mask

    from spas_sage_attn import block_sparse_sage2_attn_cuda
    
    attn_output = block_sparse_sage2_attn_cuda(q, k, v, mask_id=None):
    

    In this API, we support computing attention in any block sparse mask per attention head. Specifically, the per-head attention mask mask_id has shape (batch_size, num_heads, ⌈seq_len / 128⌉, ⌈seq_len // 64⌉) and consists of 0 and 1. Currently, the block size is 128×64.


    The official implementation of SpargeAttn, a universal training-free sparse attention accelerating language, image, and video models.

    Project Updates

    • Please use the spas_sage2_attn_meansim_topk_cuda and block_sparse_sage2_attn_cuda APIs.
    • [2025-07]: Release a Triton Kernel example.
    • [2025-06]: SpargeAttn based on SageAttention2++ is released.
    • [2025-05]: Add a very simple usage without tuning or calibration: o = spas_sage2_attn_meansim_topk_cuda(q, k, v).
    • [2025-05]: 🎉SpargeAttn and SageAttention2 are accepted by ICML 2025!
    • [2025-03] Support high acceleration on more GPUs, e.g., H100.

    Installation

    Base environment

    • python>=3.9 , torch>=2.3.0
    • CUDA:
      • >=12.8 for Blackwell, >=12.4 for fp8 support on Ada, >=12.3 for fp8 support on Hopper, >=12.0 for Ampere

    Install Package

    pip install ninja   # for parallel compilation
    python setup.py install   # or pip install -e .
    

    Available API

    • spas_sage2_attn_meansim_topk_cuda: SpargeAttn based on SageAttention2 that we recommend using.

    • spas_sage2_attn_meansim_cuda: SpargeAttn based on SageAttention2 that we do not recommend.

    • spas_sage_attn_meansim_topk_cuda: SpargeAttn based on SageAttention that we recommend using.

    • spas_sage_attn_meansim_cuda: SpargeAttn based on SageAttention that we do not recommend.

    Usage

    Plug-and-Play Usage

    Just replace torch.nn.functional.scaled_dot_product_attention API using spas_sage2_attn_meansim_topk_cuda:

    from spas_sage_attn import spas_sage2_attn_meansim_topk_cuda
    
    - attn_output = torch.nn.functional.scaled_dot_product_attention(q, k, v, is_causal=False)  # is_causal can be True
    
    + attn_output = spas_sage2_attn_meansim_topk_cuda(q, k, v, topk=0.5, is_causal=False) # is_causal can be True
    

    Plug-and-Play API

    from spas_sage_attn import spas_sage2_attn_meansim_topk_cuda
    
    attn_output = spas_sage2_attn_meansim_topk_cuda(q, k, v, topk=0.5, is_causal=False)
    

    You can adjust topk to balance between attention accuracy (higher topk is more accurate) and sparsity (lower topk is more sparse).

    Customize your Block-Sparse Mask API

    from spas_sage_attn import block_sparse_sage2_attn_cuda
    
    attn_output = block_sparse_sage2_attn_cuda(q, k, v, mask_id=None):
    

    In this API, we support computing attention for any block-sparse mask per attention head. Specifically, the per-head attention mask mask_id has shape (batch_size, num_heads, ⌈seq_len / 128⌉, ⌈seq_len // 64⌉) and consists of 0 and 1. Currently, the block size is 128×64.

    Citation

    @inproceedings{zhang2025spargeattn,
      title={Spargeattn: Accurate sparse attention accelerating any model inference},
      author={Zhang, Jintao and Xiang, Chendong and Huang, Haofeng and Wei, Jia and Xi, Haocheng and Zhu, Jun and Chen, Jianfei},
      booktitle={International Conference on Machine Learning (ICML)},
      year={2025}
    }
    
    @article{zhang2026spargeattention2,
      title={SpargeAttention2: Trainable Sparse Attention via Hybrid Top-k+ Top-p Masking and Distillation Fine-Tuning},
      author={Zhang, Jintao and Jiang, Kai and Xiang, Chendong and Feng, Weiqi and Hu, Yuezhou and Xi, Haocheng and Chen, Jianfei and Zhu, Jun},
      journal={arXiv preprint arXiv:2602.13515},
      year={2026}
    }
    
    @inproceedings{zhang2025sageattention,
      title={SageAttention: Accurate 8-Bit Attention for Plug-and-play Inference Acceleration}, 
      author={Zhang, Jintao and Wei, Jia and Zhang, Pengle and Zhu, Jun and Chen, Jianfei},
      booktitle={International Conference on Learning Representations (ICLR)},
      year={2025}
    }
    
    @inproceedings{zhang2024sageattention2,
      title={Sageattention2: Efficient attention with thorough outlier smoothing and per-thread int4 quantization},
      author={Zhang, Jintao and Huang, Haofeng and Zhang, Pengle and Wei, Jia and Zhu, Jun and Chen, Jianfei},
      booktitle={International Conference on Machine Learning (ICML)},
      year={2025}
    }