TopGit
GitHub Repo Review

SpargeAttn: accelerate LLM inference without training

thu-ml/SpargeAttn
STopGit review image for thu-ml/SpargeAttn
Review by Topgit.dev for thu-ml/SpargeAttn, with GitHub repository stats and README context.
Quick verdict

SpargeAttn sparse attention earns its place in your inference stack once you need to squeeze more throughput out of existing models without retraining. The topk knob gives you direct control over the accuracy-speed tradeoff, and the block-sparse mask API lets power users target specific attention patterns. Skip it if your deployment environment can't meet the CUDA version requirements or if you need exact numerical output from your attention computation.

Stars
β˜… 1.3k
Forks
β‘‚ 108
Contributors
πŸ‘₯ 10
Language
Cuda
License
Apache-2.0
Topic
AI Tools
Updated
Feb 2026
Homepage

What is SpargeAttention

SpargeAttn sparse attention is a CUDA kernel library that implements training-free sparse attention for accelerating LLM inference. It accepts standard Q, K, V tensors and returns sparse-attention outputs, with a recommended plug-and-play API that swaps in place of PyTorch's built-in attention function. The library is published by the thu-ml lab and appears at ICML 2025.

Key features of SpargeAttn

  • βœ“Plug-and-play replacement for torch.nn.functional.scaled_dot_product_attention β€” swap one function call and get sparse attention with no model changes
  • βœ“Training-free: no calibration, no fine-tuning, no extra dataset required before inference
  • βœ“Per-head block-sparse mask support with a 128Γ—64 block granularity for custom sparsity patterns
  • βœ“Configurable topk parameter to dial accuracy vs sparsity tradeoff at runtime
  • βœ“Supports language models, vision transformers, and video generation models on Ampere, Hopper, Ada, and Blackwell GPUs
  • βœ“Built on SageAttention and SageAttention2++ from the same authors, leveraging their 8-bit quantization and outlier smoothing work
How this repository's GitHub stars have grown over time. Source: star-history.com.View the star history β†—

Installing SpargeAttention

The package requires Python >=3.9, PyTorch >=2.3.0, and CUDA >=12.0 for Ampere (with stricter version requirements for fp8 on newer architectures). Install ninja for parallel compilation, then run `python setup.py install` or `pip install -e .`. Blackwells need CUDA >=12.8.

Using the plug-and-play API

python from spas_sage_attn import spas_sage2_attn_meansim_topk_cuda # Replace PyTorch's built-in SDPA attn_output = spas_sage2_attn_meansim_topk_cuda(q, k, v, topk=0.5, is_causal=False) The topk parameter controls sparsity: lower values give more speedup at the cost of accuracy. Set is_causal the same way you would for standard attention.

Strengths

  • βœ“Drop-in replacement for `torch.nn.functional.scaled_dot_product_attention` β€” no training, fine-tuning, or calibration required, just swap the function call and adjust `topk` to taste.
  • βœ“Tunable accuracy-sparsity trade-off via a single `topk` parameter: higher values preserve more attention heads, lower values increase speed.
  • βœ“Per-head block-sparse mask support lets you define arbitrary 128Γ—64 block patterns per attention head through a mask tensor shaped `(batch_size, num_heads, ⌈seq_len/128βŒ‰, ⌈seq_len/64βŒ‰)`.
  • βœ“Accelerates language, image, and video models with the same codebase, per the paper's scope.
  • βœ“Built on SageAttention2++ (ICML 2025), which itself ships int4 quantization and thorough outlier smoothing β€” the sparse attention layer inherits that inference-efficiency foundation.

Known limitations

  • β–³Requires CUDA 12.0+ (and up to 12.8 for Blackwell), limiting use on older GPU generations or environments with constrained driver versions.
  • β–³Installation requires building from source with `python setup.py install` rather than a prebuilt wheel β€” `ninja` is also a required dependency for parallel compilation.
  • β–³No training or fine-tuning support β€” it is explicitly training-free, so it cannot be used to adapt or distill models into sparser forms.
  • β–³The block-sparse mask API enforces a fixed block size of 128Γ—64, offering no runtime configurability for different sequence lengths or hardware profiles.
  • β–³The accuracy–sparsity balance via the `topk` parameter is left to the user to tune; overly aggressive sparsity (low `topk`) risks measurable quality degradation that is not automatically compensated.

Alternatives to SpargeAttn

SageAttention β€” the predecessor from the same authors, focusing on 8-bit quantized attention rather than sparsity β†—FlashAttention β€” the widely-used exact attention implementation that trades speed for correctness via IO-awarenessFlexAttention β€” PyTorch's built-in flexible attention API that supports custom mask shapes but not sparse patterns out of the boxLightLLM β€” a Python inference framework that includes attention optimization strategies but requires architectural changes

Frequently asked questions

Is SpargeAttention free for commercial use?

Yes. SpargeAttention is licensed under Apache-2.0, which permits commercial use, modification, and redistribution without royalty fees β€” you can integrate it into proprietary products.

Which CUDA versions does SpargeAttn support?

SpargeAttn requires CUDA >=12.0 for Ampere, >=12.3 for fp8 on Hopper, >=12.4 for fp8 on Ada, and >=12.8 for Blackwell GPUs. Python must be 3.9 or higher, with PyTorch 2.3.0 or later.

Can I use SpargeAttn with vision transformers and video models?

Yes. The README explicitly lists vision-transformer and video-generation as supported topics. The block-sparse API works across any model architecture that uses standard QKV attention.

What is the difference between SageAttention and SpargeAttn?

SageAttention focuses on 8-bit quantized exact attention to speed up inference through lower precision. SpargeAttn sparse attention instead drops attention tokens entirely for sparsity gains, trading some accuracy in exchange for greater memory bandwidth reduction.

Does SpargeAttn require training or fine-tuning?

No. SpargeAttn is entirely training-free β€” you can apply it to any existing model checkpoint without retraining, calibration datasets, or additional fine-tuning runs.

How do I tune the topk parameter for accuracy vs speed?

The topk parameter accepts values between 0 and 1. Lower topk means sparser attention (faster, less accurate); higher topk means more tokens are retained (slower, more accurate). Benchmark your specific model and task to find the sweet spot.

The problem it solves

Full attention in modern transformers is expensive at long sequence lengths, and most sparse alternatives require training, fine-tuning, or calibration runs before they work β€” friction that makes them impractical for fast iteration or production deployment.

Best use cases

  • β€’Accelerating inference for long-context language models where full attention dominates latency
  • β€’Speeding up vision transformer inference in image classification or detection pipelines without retraining
  • β€’Reducing memory bandwidth for video generation models that process long frame sequences
  • β€’Production deployments where you need to trade a small amount of output quality for higher throughput
  • β€’Integrating into existing inference serving stacks as a drop-in attention replacement with minimal code changes
  • β€’Custom sparsity patterns per attention head via block_sparse_sage2_attn_cuda for domain-specific workloads

Who should try it β€” and who should skip

Pick up SpargeAttn if you're running open-weight language or vision models in production and want measurable throughput gains without touching training code. It's especially useful when you control the inference stack end-to-end and can benchmark the accuracy impact of different topk values. If you need exact numerical outputs, work in a CUDA-constrained environment, or lack the resources to tune topk per model, look elsewhere.

Related repositories

Source & attribution

GitHub repository thu-ml/SpargeAttn β€” ICML 2025 paper.

GitHub data Β· last synced Sep 30, 2026Reviewed by Henry
← Back to TopGit

Still deciding about SpargeAttn?

One click hands the question to an AI along with this page β€” see what it says about SpargeAttn.

GitHub