Skip to main content

Triton-based backend for Flash Attention 2

Project description

Flash Attention Triton

This repository provides a wrapper for the Triton implementation of the Flash Attention algorithm with a Flash Attention 2 compatible API. It allows for a drop-in replacement of the original Flash Attention 2 package for supported functionality. This package provides support for Turing (eg. 2080 Ti, T4) GPUs not supported by the original FA2 CUDA package.

Installation

You can install the package directly from GitHub:

pip install git+https://github.com/rationalism/flash-attn-triton.git

Or from PyPI:

pip install flash-attn-triton

Requirements

  • PyTorch 2.6 or later
  • Triton 3.2 or later
  • CUDA-compatible GPU (compute capability 7.5+)

Usage

The API is designed to be compatible with Flash Attention 2. You can use it in the same way:

from flash_attn_triton import flash_attn_func, flash_attn_qkvpacked_func, FlashAttention

# Basic usage
out = flash_attn_func(q, k, v, causal=True)

# Packed QKV
out = flash_attn_qkvpacked_func(qkv, causal=True)

# Module interface
flash_attn = FlashAttention()
out = flash_attn(q, k, v, causal=True)

Currently Supported Features

  • Basic attention mechanism (forward and backward)
  • FP16 and BF16 (BF16 only on Ampere and above)
  • Causal masking
  • Softmax scaling
  • Basic MQA/GQA support (via tensor repetition)
  • Head dims 16, 32, 64, 128
  • Ampere, Turing cards

Limitations

This implementation does not currently support:

  • Non-causal attention for sequence lengths not divisible by 128
  • Dropout (in progress)
  • Volta, Pascal, and earlier cards (in progress)
  • varlen/unpadded support
  • Attention bias
  • Sliding window attention
  • ALiBi
  • KV caching with in-place updates
  • Softcapping
  • Deterministic backward pass

Benchmarks

RTX 3090 (Ampere)

fused-attention-batch4-head32-d64-fwd-causal=True-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      48.049147
1   2048.0      61.062769
2   4096.0      68.363188
3   8192.0      70.768167
4  16384.0      72.332634
fused-attention-batch4-head32-d64-fwd-causal=False-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      60.190653
1   2048.0      71.126662
2   4096.0      69.049310
3   8192.0      74.579215
4  16384.0      73.911621
fused-attention-batch4-head32-d64-bwd-causal=True-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      33.531732
1   2048.0      40.884683
2   4096.0      45.627974
3   8192.0      47.449394
4  16384.0      48.993511
fused-attention-batch4-head32-d64-bwd-causal=False-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      42.834959
1   2048.0      46.382862
2   4096.0      49.984253
3   8192.0      51.358497
4  16384.0      49.913040

RTX 2080 Ti (Turing)

fused-attention-batch4-head32-d64-fwd-causal=True-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      29.258471
1   2048.0      41.382117
2   4096.0      46.972266
3   8192.0      49.315714
4  16384.0      50.443531
fused-attention-batch4-head32-d64-fwd-causal=False-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      38.110175
1   2048.0      47.640577
2   4096.0      50.301599
3   8192.0      51.136501
4  16384.0      51.826783
fused-attention-batch4-head32-d64-bwd-causal=True-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      22.085938
1   2048.0      26.173398
2   4096.0      28.565586
3   8192.0      30.030201
4  16384.0      31.082861
fused-attention-batch4-head32-d64-bwd-causal=False-dropout=0.0:
     N_CTX  Triton [FP16]
0   1024.0      27.756566
1   2048.0      30.274265
2   4096.0      31.471025
3   8192.0      32.253811
4  16384.0      32.614130

Acknowledgements

This implementation is based on the Triton attention implementation from the original Flash Attention 2 repository by TriDao and the Triton tutorial on fused attention.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

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

flash_attn_triton-0.1.1.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

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

flash_attn_triton-0.1.1-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file flash_attn_triton-0.1.1.tar.gz.

File metadata

  • Download URL: flash_attn_triton-0.1.1.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for flash_attn_triton-0.1.1.tar.gz
Algorithm Hash digest
SHA256 35e03c078d7936426e2b49d8dbfefd9f82a1c7bbc03c4accfed919a287574091
MD5 0ded36c6346a67859d4b4fad19d1e8e3
BLAKE2b-256 a4c1f878e62e92144b266f5e566e50d82b96daef2f88bdfaef2df031c3772949

See more details on using hashes here.

File details

Details for the file flash_attn_triton-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flash_attn_triton-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9bf3b296cacee576a59eadf4e071dbec50961efa2d0296086d9a65f4b68662e8
MD5 dd1981ba86dac805f3adcfbbeb46df23
BLAKE2b-256 c01f5ae511cdb5f2694287dbb682b998dde0672b16cc6ac9ed6503311ef16ccf

See more details on using hashes here.

Supported by

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