Skip to main content

Helpful tools and examples for working with flex-attention

Project description

Attention Gym

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

🎯 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
  • 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/pytorch-labs/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 = generate_sliding_window(window_size=1024)
    block_mask = create_block_mask(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
  • examples/: Detailed implementations using FlexAttention

🛠️ Dev

Install dev requirements

pip install -e ".[dev]"

Install pre-commit hooks

pre-commit install

🤝 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. [Optinally] Add an end to end example using your new func in the examples/ directory.

See CONTRIBUTING.md for more details.

⚖️ License

attention-gym is released under the BSD 3-Clause License.

Project details


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.2.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

attn_gym-0.0.2-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: attn_gym-0.0.2.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for attn_gym-0.0.2.tar.gz
Algorithm Hash digest
SHA256 080b37eaff73111b7b3b0ca9679eef0c1b39be6ca1393c2af2117c0eaa9848d6
MD5 07dddec9b282cae9d2da1cb885a1818f
BLAKE2b-256 e55b313f46ad9064e04c8270a840ca9e944caae83c00a4c4034a2b8036bb4aac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: attn_gym-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for attn_gym-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8e3226507843e92beee04dafe2a1a450fe98a92ea68f48be886d461adc6194de
MD5 1d78a873e7cae3ed7afb065e24ceb4c7
BLAKE2b-256 301e670a784e4b3c38e7630de06e0f1b61cfedbfae696f6857a689dfb8cbb3c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page