Skip to main content

Attention Gym

Attention Gym is a collection of helpful tools and examples for working with flex-attention

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

📖 Overview

This repository aims to provide a playground for experimenting with various attention mechanisms using the FlexAttention API. It includes implementations of different attention variants, performance comparisons, and utility functions to help researchers and developers explore and optimize attention mechanisms in their models.

favorite

🎯 Features

  • Implementations of various attention mechanisms using FlexAttention
  • Reference implementations for linear-attention recurrences
  • 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

git clone https://github.com/meta-pytorch/attention-gym.git
cd attention-gym
pip install .

💻 Usage

There are two main ways to use Attention Gym:

  1. Run Example Scripts: Many files in the project can be executed directly to demonstrate their functionality:

    python attn_gym/masks/document_mask.py
    

    These scripts often generate visualizations to help you understand the attention mechanisms.

  2. Import in Your Projects: You can use Attention Gym components in your own work by importing them:

    from torch.nn.attention.flex_attention import flex_attention, create_block_mask
    from attn_gym.masks import generate_sliding_window
    
    # Use the imported function in your code
    sliding_window_mask_mod = generate_sliding_window(window_size=1024)
    block_mask = create_block_mask(sliding_window_mask_mod, 1, 1, S, S, device=device)
    out = flex_attention(query, key, value, block_mask=block_mask)
    

For comprehensive examples of using FlexAttention in real-world scenarios, explore the examples/ directory. These end-to-end implementations showcase how to integrate various attention mechanisms into your models.

Note

Attention Gym is under active development, and we do not currently offer any backward compatibility guarantees. APIs and functionalities may change between versions. We recommend pinning to a specific version in your projects and carefully reviewing changes when upgrading.

📁 Structure

Attention Gym is organized for easy exploration of attention mechanisms:

🔍 Key Locations

  • attn_gym.masks: Examples creating BlockMasks
  • attn_gym.mods: Examples creating score_mods
  • attn_gym.linear: Reference linear-attention implementations
  • examples/: Detailed implementations using FlexAttention
  • examples/paged_attention: Paged KV-cache examples using FlexAttention

🛠️ 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 is released under the BSD 3-Clause License.

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.5.tar.gz (2.4 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.5-py3-none-any.whl (317.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: attn_gym-0.0.5.tar.gz
  • Upload date:
  • Size: 2.4 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.5.tar.gz
Algorithm Hash digest
SHA256 fc1499319638119e20acfd2362bd4199f0bba3a0e48ea1cfd86ccf08aed5267e
MD5 c657f0756d88958a0f22cc5988de924c
BLAKE2b-256 07e5e8013dacd919845b181c3ac27621d479340bd3a1dbbdeecf852286174776

See more details on using hashes here.

Provenance

The following attestation bundles were made for attn_gym-0.0.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: attn_gym-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 317.4 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0217fe1dfd8856bd43d4d319608e79a19beb9409ed388e03220deebe448b859b
MD5 ca53581a271348c238145e87b1fc932a
BLAKE2b-256 a5590644a2858a18e5e9a5c5dfa3f9abb7505a30ae34845e7042b7a95b89f203

See more details on using hashes here.

Provenance

The following attestation bundles were made for attn_gym-0.0.5-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

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

This release

0.0.5 This release

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