Skip to content

xlite-dev/ffpa-attn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

422 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค–FFPA: Yet another Faster Flash Prefill Attention
with O(1)โšก๏ธGPU SRAM complexity for large headdim๐Ÿ‘


FFPA(Split-D): Yet another Faster Flash Prefill Attention with Split-D strategy, achieve O(1) SRAM complexity and O(d/4) register complexity for large headdim (> 256), 1.5~3x ๐ŸŽ‰ faster than SDPA. ๐Ÿ“š๐Ÿ‘‡The Core features:

Self Attn GQA/MQA Cross Attn Causal/Mask Dropout Headdim Fwd/Bwd
โœ”๏ธ(Nq=Nkv) โœ”๏ธ(Hq!=Hkv) โœ”๏ธ(Nq!=Nkv) โœ”๏ธ(attn_mask) โœ”๏ธ(p>0) 320~1024 1.5~3xโ†‘

๐ŸŽ‰๐ŸŽ‰ Latest News

๐Ÿ“– Quick Start

First, install the prebuilt package from PyPI or build ffpa-attn from source:

# Fisrt, install the prebuilt package from PyPI
pip3 install -U ffpa-attn # CUDA 13.0+, PyTorch 2.11+
# Or, build ffpa-attn from source, just follow the cmds
git clone https://github.com/xlite-dev/ffpa-attn.git
# Then, build the wheel package (Triton + CuTeDSL backends)
cd ffpa-attn && pip3 install -e . --no-build-isolation
# Optional: install ffpa-attn w/ CUDA backend (forward only)
ENABLE_FFPA_CUDA_IMPL=1 MAX_JOBS=32 pip3 install -e .

Then, try to accelerate the attention for large headdim with just one-line of code:

>>> import torch.nn.functional as F
>>> from ffpa_attn import ffpa_attn_func
>>> # Monkey-patch SDPA to point to FFPA. Every thing that FFPA
>>> # does not support will auto fallback to SDPA: D <= 256, etc.
>>> F.scaled_dot_product_attention = ffpa_attn_func # one-line code

For more advanced features, please refer to our online docs at ๐Ÿ“˜ffpa-attn.io.

๐Ÿ“– Split-D

We extend FlashAttention to support large headdim ($D&gt;256$) via fine-grained tiling at the MMA level for $QK^\top$ and $PV$ matrix multiplication, referred to as Split-D. This design keeps SRAM usage fixed at $B_r \times 16$ (with $B_r=B_c$) for Q, K and V, yielding constant SRAM complexity $O(B_r \times 16) \approx O(1)$ and register complexity $O(d/4)$.

FFPA enables headdim > 256, and outperforms standard SDPA by 1.5~3x๐ŸŽ‰.

Note

FFPA has been tested on Ampere, Ada, Hopper, and Blackwell architectures (e.g., A30, L20, 4090, H200, 5090), achieves 1.5~3ร—โ†‘๐ŸŽ‰ speedup over SDPA. FFPA is mainly design for prefill and large headdim, and may not be faster than SDPA for ๐Ÿ˜ˆ small sequence length (N<512) or small headdim (D<=256).

๐ŸŽ‰ Benchmark

Runnable benchmark are provided under bench. The performance benchmarks for the NVIDIA L20 (Ada), NVIDIA Geforce RTX 5090 (Blackwell), NVIDIA H800 PCIE (Hopper), NVIDIA H200 SXM (Hopper, CuTeDSL backend, up to 427 TFLOPS!๐ŸŽ‰) with large headdims can be found at bench.


๐Ÿค– Backends

FFPA supports multiple backends for the forward and backward pass, including: SDPA (baseline), CUDA (forward only), Triton, and CuTeDSL. The CuTeDSL backend is currently in early stage and has some constraints, but it can achieve up to 427๐ŸŽ‰ TFLOPS on H200! Stay tuned for future updates.

Backend Arch Fwd Bwd Headdim Autotune Speedup Recommend
SDPA sm>=75 โœ” โœ” All โŒ 1.0x๐Ÿค— sm>=75
CUDA sm>=80 โœ” โŒ 320~1024 โŒ 1.5x~3x๐ŸŽ‰ sm80~89,120
Triton sm>=80 โœ” โœ” 320~1024 โœ” 1.5x~5x๐ŸŽ‰ sm>=80
CuTeDSL sm>=80 โœ” โœ” 320~1024 โŒ 1.5x~2x๐ŸŽ‰ sm80~89,120
CuTeDSL sm90 โœ” โœ” 320~512 โŒ 3x~6x๐ŸŽ‰ sm90

Special thanks to Butterfingrz for contributing to the CuTeDSL backend! Awesome work!๐ŸŽ‰

Note

๐Ÿ”ด AMD ROCm/HIP support: the Triton backend (forward + backward) also runs on AMD GPUs, install ffpa-attn into a ROCm build of PyTorch and it will dispatch to Triton AMD automatically. Validated on AMD Instinct MI250X (gfx90a, Linux) and Radeon RX 9070 XT (gfx1201, Windows).

How to use different backends for your own scenario? Users can simply pass the Backend configs (SDPABackend, CUDABackend, TritonBackend or CuTeDSLBackend) to ffpa_attn_func, for example:

>>> from ffpa_attn import ffpa_attn_func, CuTeDSLBackend
>>> # CuTeDSL backend, D=512 scenario, fastest on H200!๐ŸŽ‰
>>> o = ffpa_attn_func(q, k, v, backend=CuTeDSLBackend())

Persistent Autotune

Generate device-specific tuned configs for production deployment (currently, Triton only), avoiding per-process autotune cost. The generated JSON is saved under configs dir and automatically loaded when runtime autotune is disabled (the default). See the docs of Triton Autotune for details.

python -m ffpa_attn.autotune --mode max --full-tasks --overwrite # 1 GPU
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 # Multi-GPU (`pip install ray`)
python -m ffpa_attn.autotune --mode max --full-tasks --num-gpus 8 --overwrite

End-to-End (E2E) Training

NVIDIA-NeMo Automodel PR #2436 shows that on Gemma4-31B training (L=8192, 8xH200, FSDP2 + Activation Checkpointing), accelerating the 10/60 D=512 full-attention layers with ffpa-attn delivers about 1.4x-1.5x๐ŸŽ‰ higher throughput (E2E) than SDPA at similar memory footprint, with loss aligned within normal bf16 noise.

ยฉ๏ธLicense

Apache License 2.0

ยฉ๏ธCitations

@misc{deftruth2026ffpa,
  author       = {DefTruth and Butterfingrz},
  title        = {FFPA: Efficient Flash Prefill Attention for Large Head Dimensions via Split-D},
  year         = {2026},
  publisher    = {Zenodo},
  version      = {v1.0},
  doi          = {10.5281/zenodo.20638547},
  url          = {https://doi.org/10.5281/zenodo.20638547}
}

๐Ÿ“– References

About

๐Ÿค–FFPA: Extends FlashAttention-2 via Split-D for large headdims, 1.5x~3ร—โ†‘๐ŸŽ‰ vs SDPA, up to 430T๐ŸŽ‰ on H200.

Topics

Resources

License

Stars

315 stars

Watchers

4 watching

Forks

Packages

 
 
 

Contributors

Languages