项目
在 GitHub 查看
HKUSTDial/flash-sparse-attention
关于
可训练、快速且节省内存的稀疏注意力机制
系统与基础设施可以做什么
- Forward and backward passes for dense and sparse attention
- Regular batched inputs and varlen inputs
- Causal attention and local window attention
- Arbitrary combinations of Q and KV sequence lengths
实测星标增长
- 24小时
- +3
- 7天
- +1
- 15天
- +4
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README_zh.md
原文
English | 简体中文
Flash-Sparse-Attention 是一个高性能的可训练稀疏注意力实现, 将 Flash Attention 的内存效率与稀疏计算能力相结合, 用于在 Transformer 模型中处理超长序列.
主要特性
[!NOTE] 支持任意形状 mask 和 bias 的版本为这个分支, 当前主分支不再覆盖这部分功能.
支持的功能
- Dense Attention 和 Sparse Attention 的前向与反向
- 常规批次输入与 varlen 输入
- 因果注意力与局部窗口注意力
- 任意 Q / KV 序列长度组合
- 分组查询注意力和多查询注意力
- 稀疏 softmax 阈值控制
- Flex Local Window Attention 支持逐head的任意窗口大小和局部范围
- Split-KV 适用于前向和解码的工作负载均衡
- Split-QO 适用于反向的工作负载均衡
- PackGQA 适用于解码场景
后端支持
| 后端 | 选择方式 |
|---|---|
| Gluon | SM80 和 SM90 默认使用 |
| Triton | 其他架构默认使用 |
完整API文档请参考 这里
安装
依赖
- Linux: Ubuntu 22.04 或更高版本
- Device: NVIDIA GPU
- Python: 3.12 或更高版本
- PyTorch: 2.5.1 或更高版本
- Triton: 支持 Gluon 的最新 main 分支
安装
直接安装:
pip install flash-sparse-attn
如果您希望从源码安装:
git clone https://github.com/HKUSTDial/flash-sparse-attention.git
cd flash-sparse-attention
pip install .
快速开始
基本用法
以下是前向、反向和解码的示例。
import torch
from flash_sparse_attn import flash_attn_func, flash_attn_varlen_func
dtype = torch.bfloat16
device = torch.device("cuda")
batch_size, seqlen, num_heads, num_kv_heads, head_dim = 2, 4096, 32, 8, 128
前向
组合 local、sparse 和 split。
query = torch.randn(batch_size, seqlen, num_heads, head_dim, dtype=dtype, device=device)
key = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device)
value = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device)
output = flash_attn_func(
query, key, value,
is_causal=True,
is_local=True,
is_sparse=True,
is_split_kv=True,
)
反向
组合 local、sparse 和 split。
query = torch.randn(batch_size, seqlen, num_heads, head_dim, dtype=dtype, device=device, requires_grad=True)
key = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device, requires_grad=True)
value = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device, requires_grad=True)
output = flash_attn_func(
query, key, value,
is_causal=True,
is_local=True,
is_sparse=True,
is_split_qo=True,
)
output.sum().backward()
解码
组合 local、sparse 和 split。
query = torch.randn(batch_size, 1, num_heads, head_dim, dtype=dtype, device=device)
key = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device)
value = torch.randn(batch_size, seqlen, num_kv_heads, head_dim, dtype=dtype, device=device)
def fsa_decode_fn():
return flash_attn_func(
query, key, value,
is_local=True,
is_sparse=True,
is_split_kv=True,
)
# 预热
for _ in range(3):
fsa_decode_fn()
torch.cuda.synchronize()
# 捕获 Graph
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph):
output = fsa_decode_fn()
# 重放
graph.replay()
显式选择后端
常规使用推荐从 package-level API 导入。需要对比实现或使用 Triton 专用功能时,可以直接导入后端。
from flash_sparse_attn.ops.gluon.interface import flash_attn_func as gluon_flash_attn_func
from flash_sparse_attn.ops.triton.interface import flash_attn_func as triton_flash_attn_func
基准测试
基准测试脚本位于 tests 下, 用于评估前向、反向和解码三类场景下的性能。
前向传播性能
# Triton backend
python tests/benchmark_forward.py --backend triton
# Gluon backend
python tests/benchmark_forward.py --backend gluon
# CuTe backend
python tests/benchmark_forward_cute.py
反向传播性能
# Triton backend
python tests/benchmark_backward.py --backend triton
# Gluon backend
python tests/benchmark_backward.py --backend gluon
# CuTe backend
python tests/benchmark_backward_cute.py
解码性能
# Triton backend
python tests/benchmark_decode.py --backend triton
# Gluon backend
python tests/benchmark_decode.py --backend gluon
# CuTe backend
python tests/benchmark_decode_cute.py
正确性测试
python -m pytest tests --backend triton
python -m pytest tests --backend gluon
引用
如果您在研究中使用 FSA, 请引用:
@misc{shi2026cowindowattentioncausalcoverage,
title={CoWindow Attention: Full Causal Coverage Is a Collective Property},
author={Jingze Shi and Zhangyang Peng and Xianduo Li and Yanlin Qi and Xiaotian Lin and Haoxian Chen and Liangdong Wang and Guang Liu and Yuyu Luo},
year={2026},
eprint={2609.32704},
archivePrefix={arXiv},
primaryClass={cs.AI},
url={https://arxiv.org/abs/2609.32704},
}
@misc{shi2026massallocattentionletattention,
title={MassAlloc Attention: Let Attention Allocate Its Own Compute},
author={Jingze Shi and Zhangyang Peng and Xianduo Li and Yanlin Qi and Xiaotian Lin and Haoxian Chen and Liangdong Wang and Guang Liu and Yuyu Luo},
year={2026},
eprint={2609.32712},
archivePrefix={arXiv},
primaryClass={cs.AI},
url={https://arxiv.org/abs/2609.32712},
}
@misc{shi2025trainabledynamicmasksparse,
title={Trainable Dynamic Mask Sparse Attention},
author={Jingze Shi and Yifan Wu and Bingheng Wu and Yiran Peng and Liangdong Wang and Guang Liu and Yuyu Luo},
year={2025},
eprint={2508.02124},
archivePrefix={arXiv},
primaryClass={cs.AI},
url={https://arxiv.org/abs/2508.02124},
}
致谢
本项目基于并集成了几个优秀的工作:
- OpenSeek - 内核开发支持
- Flash-Attention - 内存高效的注意力计算
- NVIDIA CUTLASS - 高性能矩阵运算库
我们感谢开源社区对高效 Transformer 实现的贡献. 🤗
探索相关项目
加载中…