flagos-ai/FlagAttention

A collection of memory efficient attention operators implemented in the Triton language.

What it solves

FlagAttention provides a collection of memory-efficient attention operators designed for model training and inference. It addresses the need for specialized attention mechanisms—such as paged or sparse KV-cache layouts, recurrent linear-attention kernels, and custom attention-score transformations—that go beyond the standard scaled-dot-product-attention interface.

How it works

Built using Triton, the library implements tiled computation and recomputes intermediate values instead of materializing full attention matrices, similar to FlashAttention. It includes a backend routing system that detects your hardware (e.g., NVIDIA, AMD, Ascend, etc.) and dispatches to either generic Triton kernels or vendor-specific optimized implementations. While FlashAttention and Piecewise Attention are stable, many other operators are in alpha and may evolve.

Who it’s for

This project is for developers and researchers working on LLM training and inference who need high-performance, hardware-accelerated attention operators. It is particularly useful for those working with non-standard attention patterns, quantized models, or specific hardware accelerators that require custom kernel tuning.

Highlights

  • Offers a broad inventory of 26 operator families, including dense, sparse, recurrent, and quantized attention.
  • Supports diverse hardware backends including NVIDIA, AMD, Hygon, Iluvatar, Metax, Thead, Enflame, Ascend, Cambricon, Mthreads, and Intel.
  • Includes specialized operators like Paged Attention, SageAttention (INT8 quantization), and various chunked recurrent attention kernels.
  • Provides a unified Python API that automatically selects the best available implementation for the detected device.
  • Implements memory-efficient strategies like tiling and recomputation to handle long-context workloads.

Related

  • Project
  • Dispatch
  • Project
  • Project
  • Dispatch