meta-pytorch/attention-gym
Helpful tools and examples for working with flex-attention
What it solves
Attention Gym solves the challenge of expressing and running many different attention variants — beyond the standard softmax attention — in PyTorch. It provides reusable building blocks and examples so you can compose custom attention masks and score modifications, or use sparse and linear attention kernels, without writing everything from scratch.
How it works
Attention Gym is a Python package that builds on PyTorch's FlexAttention API (version 2.5+). You import mask_mod and score_mod functions from attn_gym and pass them directly to FlexAttention calls. It also offers higher-level APIs and kernels for sparse attention (like gather_attn) and linear attention (GDN/KDA with chunk, recurrent, and decode paths). The README shows three workflows: composing FlexAttention building blocks, using sparse/linear attention APIs and kernels, and running real workloads and benchmarks from the examples directory.
Who it's for
Researchers and developers who work with attention mechanisms in PyTorch and want to experiment with novel variants, sparse patterns, or linear attention training and inference. It's also for anyone who wants practical examples of using FlexAttention in real scenarios.
Highlights
- Collection of FlexAttention mask and score modification functions
- Sparse attention patterns and utilities for creating and combining masks
- APIs and kernels for efficient GDN (Gated DeltaNet) and KDA (Delta Rule) linear attention
- Examples showing real-world usage, including paged, ring, and variable sparse attention, CUDA Graphs, determinism, and profiling
- Small runtime dependency footprint — only PyTorch is required, optional extras for linear attention and visualization
Related
- Project
- Project
- Project
- Project
- Project