Skip to main content

Attention Gym

Attention Gym is a collection of kernels, guides, and examples for FlexAttention and other novel attention variants.

📚 Docs | 🎯 Features | 🚀 Getting Started | 💻 Usage | 🛠️ Dev | 🤝 Contributing | ⚖️ License

📖 Overview

Attention Gym began as a library of examples showing the many ways to express attention variants with the FlexAttention API. It is growing into a broader playground for attention, with the addition of sparse attention kernels, linear attention APIs for training and inference as well as showcasing how to use FlexAttention and friends in real workloads.

Two new Flexys—Sparse Attention and Linear Attention—enter Attention Gym while the original Flexys train

🎯 Features

  • FlexAttention masks and score modifications
  • Sparse attention patterns
  • APIs and kernels for efficient GDN and KDA
  • Utility functions for creating and combining attention masks
  • Examples of how to use FlexAttention in real-world scenarios

🚀 Getting Started

Prerequisites

  • PyTorch (version 2.5 or higher)

Installation

Install the official wheel from PyPI:

pip install attn-gym

The base package intentionally keeps its runtime dependency surface small: it depends only on PyTorch (unpinned). Optional features live behind extras, so install only what you need:

pip install "attn-gym[linear]"  # Linear-attention APIs and kernels
pip install "attn-gym[viz]"     # Visualization and example dependencies

[!WARNING] Attention Gym is under active development. We reserve the right to make backward-incompatible changes between releases. If you depend on a particular API or kernel behavior, hard-pin the version you test, for example: pip install "attn-gym[linear]==X.Y.Z".

💻 Usage

Attention Gym supports three complementary workflows:

  1. Compose FlexAttention building blocks. Import mask_mod and score_mod functions and pass them directly to PyTorch's FlexAttention APIs.
  2. Use sparse and linear-attention APIs and kernels. Build with selected_attention, GDN and KDA chunk, recurrent, and decode paths, and short-convolution primitives. See the compressed sparse attention and KDA training for working examples.
  3. Run real workloads and benchmarks. The examples/ directory covers paged, ring, and variable sparse attention, CUDA Graphs, determinism, compilation, and profiling. Most of this should serve as inspiration for fun things you might build from our building blocks :)

🛠️ Dev

Install dev requirements

pip install -e ".[dev]"

Install and run the repository hooks:

prek install
prek run --all-files

🤝 Contributing

We welcome contributions to Attention Gym, especially new Masks or score mods! Here's how you can contribute:

Contributing Mods

  1. Create a new file in the attn_gym/masks/ for mask_mods or attn_gym/mods/ for score_mods.
  2. Implement your function, and add a simple main function that showcases your new function.
  3. Update the attn_gym/*/__init__.py file to include your new function.
  4. Optionally, add an end-to-end example using your new function in the examples/ directory.

See CONTRIBUTING.md for more details.

⚖️ License

Attention Gym-authored code is released under the BSD 3-Clause License. Vendored third-party components retain the licenses and notices included alongside their source.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

attn_gym-0.0.8.tar.gz (3.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

attn_gym-0.0.8-py3-none-any.whl (667.8 kB view details)

Uploaded Python 3

File details

Details for the file attn_gym-0.0.8.tar.gz.

File metadata

  • Download URL: attn_gym-0.0.8.tar.gz
  • Upload date:
  • Size: 3.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for attn_gym-0.0.8.tar.gz
Algorithm Hash digest
SHA256 16e8e0350139eba26010c4cc81d5ffb8bae033cd667aa171322ed008b1831f40
MD5 5ef80d7644c7c9b3854ec7d93fd7b288
BLAKE2b-256 7da2f5840c2ddedf89c665496fe27b7b5f5ec36dd52dd4ef35638a23d007dfac

See more details on using hashes here.

Provenance

The following attestation bundles were made for attn_gym-0.0.8.tar.gz:

Publisher: publish-to-pypi.yml on meta-pytorch/attention-gym

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file attn_gym-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: attn_gym-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 667.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for attn_gym-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 faddde474d9f6df939e7ab6e4e84c63d4d2266bf8044ac5e95bf5125a12ff4e2
MD5 f1ec12c258b10f016052408370d00269
BLAKE2b-256 43b196b9266ef1a7fcbac0d68a6353e8364cbb5c7609fc1b264781dcac91999f

See more details on using hashes here.

Provenance

The following attestation bundles were made for attn_gym-0.0.8-py3-none-any.whl:

Publisher: publish-to-pypi.yml on meta-pytorch/attention-gym

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page