HKUSTDial/flash-sparse-attention

Trainable fast and memory-efficient sparse attention

What it solves

Transformers struggle with very long sequences because standard attention computes relationships between every pair of tokens, which grows quadratically with length and becomes prohibitively slow and memory-hungry. Flash-Sparse-Attention addresses this by skipping irrelevant parts of the attention matrix (sparsity) while using Flash Attention's memory-efficient tiling strategy, making it practical to train and run models on inputs that are far longer than before.

How it works

It provides forward and backward passes for dense, sparse, and gated attention, all in one unified library. The core idea is to combine Flash Attention's chunked computation with user-defined or learned sparsity patterns (like causal masks, local windows, or gated sparsity). It supports both regular batched inputs and varlen (variable sequence length) inputs, along with grouped-query attention and multi-query attention. Under the hood, it uses high-performance kernels written in Triton and a CuTe backend (currently the fastest), with built-in techniques like split-KV for load balancing during forward/decode and split-QO during backward. It also includes fused low-precision quantization and a sparse softmax threshold to skip low-contribution entries, plus special support for KV-cache decode and paged attention.

Who it's for

Researchers and engineers building Transformer-based models that need to handle extremely long contexts—such as long-document processing, multi-turn dialogues, or large-scale sequence modeling—while keeping training and inference affordable. It targets users comfortable with PyTorch and running code on GPU, XPU, NPU, or PPU devices.

Highlights

  • Combined forward and backward support for dense, sparse, and gated attention
  • Works with causal masks, local window attention, and arbitrary Q/KV length combinations (head dims up to 256)
  • Handles both regular batched and varlen inputs, plus grouped-query and multi-query attention
  • Includes split-KV and split-QO for workload balancing, fused quantization, and a sparse softmax threshold
  • Provides top-k gather KV-cache decode and paged attention for efficient inference
  • Actively developed with multiple backends (Triton, CuTe, and a WIP Gluon backend)

Related

  • Project
  • Project
  • Project
  • Project
  • Project