meta-pytorch/attention-gym

Helpful tools and examples for working with flex-attention

What it solves

Attention Gym provides a playground and a library of building blocks for implementing and experimenting with novel attention mechanisms. It simplifies the process of creating custom attention masks and score modifications, and provides efficient implementations for sparse and linear attention variants that would otherwise be complex to build from scratch.

How it works

The project operates through three primary workflows:

  1. FlexAttention Integration: It provides mask_mod and score_mod functions that can be passed directly into PyTorch's FlexAttention APIs to customize how attention is calculated.
  2. Specialized Kernels: It includes dedicated APIs and kernels for sparse attention (via gather_attn) and linear attention (including GDN and KDA paths).
  3. Practical Examples: It offers a suite of benchmarks and real-world workloads covering paged attention, ring attention, and CUDA Graphs to demonstrate how to apply these building blocks in production-like scenarios.

Who it’s for

It is designed for AI researchers and developers who are building new attention variants or looking to optimize attention performance using PyTorch 2.5+.

Highlights

  • Integration with PyTorch FlexAttention for easy mask and score modifications.
  • Support for sparse attention patterns and efficient linear attention kernels.
  • Specialized primitives for GDN and KDA (chunk, recurrent, and decode paths).
  • Comprehensive examples covering variable sparse attention, compilation, and profiling.

Related

  • Project
  • Project
  • Project
  • Project
  • Project