togethercomputer/flash-attention-3
forked from Dao-AILab/flash-attention
Captured source
source ↗togethercomputer/flash-attention-3
Description: Fast and memory-efficient exact attention
License: BSD-3-Clause
Stars: 32
Forks: 1
Open issues: 0
Created: 2024-11-06T23:46:11Z
Pushed: 2024-12-02T20:28:28Z
Default branch: main
Fork: yes
Parent repository: Dao-AILab/flash-attention
Archived: no
README:
FlashAttention
This repository provides the official implementation of FlashAttention and FlashAttention-2 from the following papers.
FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré Paper: https://arxiv.org/abs/2205.14135 IEEE Spectrum article about our submission to the MLPerf 2.0 benchmark using FlashAttention. 
FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning Tri Dao
Paper: https://tridao.me/publications/flash2/flash2.pdf

Usage
We've been very happy to see FlashAttention being widely adopted in such a short time after its release. This page contains a partial list of places where FlashAttention is being used.
FlashAttention and FlashAttention-2 are free to use and modify (see LICENSE). Please cite and credit FlashAttention if you use it.
FlashAttention-3 beta release
FlashAttention-3 is optimized for Hopper GPUs (e.g. H100).
Blogpost: https://tridao.me/blog/2024/flash3/
Paper: https://tridao.me/publications/flash3/flash3.pdf

This is a beta release for testing / benchmarking before we integrate that with the rest of the repo.
Currently released:
- FP16 forward and backward
Coming soon in the next couple of days / next week:
- BF16
- Variable length (FP16, BF16)
- FP8 forward.
Requirements: H100 / H800 GPU, CUDA >= 12.3.
To install:
cd hopper python setup.py install
To run the test:
export PYTHONPATH=$PWD pytest -q -s test_flash_attn.py
Installation and features
Requirements:
- CUDA toolkit or ROCm toolkit
- PyTorch 1.12 and above.
packagingPython package (pip install packaging)ninjaPython package (pip install ninja) *- Linux. Might work for Windows starting v2.3.2 (we've seen a few positive reports) but Windows compilation still requires more testing. If you have ideas on how to set up prebuilt CUDA wheels for Windows, please reach out via Github issue.
\* Make sure that ninja is installed and that it works correctly (e.g. ninja --version then echo $? should return exit code 0). If not (sometimes ninja --version then echo $? returns a nonzero exit code), uninstall then reinstall ninja (pip uninstall -y ninja && pip install ninja). Without ninja, compiling can take a very long time (2h) since it does not use multiple CPU cores. With ninja compiling takes 3-5 minutes on a 64-core machine using CUDA toolkit.
To install:
pip install flash-attn --no-build-isolation
Alternatively you can compile from source:
python setup.py install
If your machine has less than 96GB of RAM and lots of CPU cores, ninja might run too many parallel compilation jobs that could exhaust the amount of RAM. To limit the number of parallel compilation jobs, you can set the environment variable MAX_JOBS:
MAX_JOBS=4 pip install flash-attn --no-build-isolation
Interface: src/flash_attention_interface.py
NVIDIA CUDA Support
Requirements:
- CUDA 11.7 and above.
We recommend the Pytorch container from Nvidia, which has all the required tools to install FlashAttention.
FlashAttention-2 with CUDA currently supports: 1. Ampere, Ada, or Hopper GPUs (e.g., A100, RTX 3090, RTX 4090, H100). Support for Turing GPUs (T4, RTX 2080) is coming soon, please use FlashAttention 1.x for Turing GPUs for now. 2. Datatype fp16 and bf16 (bf16 requires Ampere, Ada, or Hopper GPUs). 3. All head dimensions up to 256. ~~Head dim > 192 backward requires A100/A800 or H100/H800~~. Head dim 256 backward now works on consumer GPUs (if there's no dropout) as of flash-attn 2.5.5.
AMD ROCm Support
ROCm version uses composable_kernel as the backend. It provides the implementation of FlashAttention-2.
Requirements:
- ROCm 6.0 and above.
We recommend the Pytorch container from ROCm, which has all the required tools to install FlashAttention.
FlashAttention-2 with ROCm currently supports: 1. MI200 or MI300 GPUs. 2. Datatype fp16 and bf16 3. Forward's head dimensions up to 256. Backward head dimensions up to 128.
How to use FlashAttention
The main functions implement scaled dot product attention (softmax(Q @ K^T * softmax_scale) @ V):
from flash_attn import flash_attn_qkvpacked_func, flash_attn_func
flash_attn_qkvpacked_func(qkv, dropout_p=0.0, softmax_scale=None, causal=False, window_size=(-1, -1), alibi_slopes=None, deterministic=False): """dropout_p should be set to 0.0 during evaluation If Q, K, V are already stacked into 1 tensor, this function will be faster than calling flash_attn_func on Q, K, V since the backward pass avoids explicit concatenation of the gradients of Q, K, V. If window_size != (-1, -1), implements sliding window local attention. Query at position i will only attend to keys between [i - window_size[0], i + window_size[1]] inclusive. Arguments: qkv: (batch_size, seqlen, 3, nheads, headdim) dropout_p: float. Dropout probability. softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim). causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling). window_size: (left, right). If not (-1, -1), implements sliding window local attention. alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of (-alibi_slope * |i - j|) is added to the attention score of query i and key j. deterministic: bool. Whether to use the deterministic implementation of the backward pass, which is slightly slower and uses more memory. The forward pass is always deterministic. Return: out: (batch_size, seqlen, nheads, headdim). """
Excerpt shown — open the source for the full document.
Notability
notability 2.0/10Routine fork, low stars.