flash-attention/README.md

96 lines
3.6 KiB
Markdown
Raw Normal View History

# FlashAttention
This repository provides the official implementation of FlashAttention from the
following paper.
2022-05-30 06:42:43 +08:00
**FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness**
2022-05-30 09:15:43 +08:00
Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré
Paper: https://arxiv.org/abs/2205.14135
2022-05-30 06:39:17 +08:00
![FlashAttention](assets/flashattn_banner.jpg)
## Alpha release (0.1).
2022-05-21 05:21:58 +08:00
2022-06-02 09:49:24 +08:00
To compile (requiring CUDA 11, NVCC, and an Ampere GPU):
2022-05-21 05:21:58 +08:00
```
python setup.py install
```
2022-05-30 06:42:43 +08:00
Interface: `src/flash_attention.py`
2022-05-21 05:21:58 +08:00
2022-05-27 04:57:38 +08:00
To run the benchmark against PyTorch standard attention:
```
PYTHONPATH=$PWD python benchmarks/benchmark_flash_attention.py
```
FlashAttention currently supports:
2022-06-02 09:49:24 +08:00
1. Ampere GPUs (e.g., A100, RTX 3090).
2022-05-27 04:57:38 +08:00
2. fp16.
3. Head dimensions 16, 32, 64.
2022-05-30 06:42:43 +08:00
Our tentative roadmap:
1. [Jun 2022] Make package pip-installable.
2022-06-02 09:49:24 +08:00
2. ~~[Jun 2022] Support SM86 GPUs (e.g., RTX 3080, 3090)~~[Done].
2022-05-30 06:42:43 +08:00
3. [Jun 2022] Refactor to use Cutlass.
4. [Jun 2022] Support SM75 GPUs (e.g. T4).
5. [Jun 2022] Support bf16.
6. [Jul 2022] Support head dimension 128.
7. [Jul 2022] Support SM70 GPUs (V100).
8. [Aug 2022] Fuse rotary embedding.
9. [Aug 2022] Support Attention linear bias (e.g. ALiBi).
2022-05-27 04:57:38 +08:00
2022-05-30 06:42:43 +08:00
## Speedup and Memory Savings
2022-06-02 11:07:00 +08:00
We present expected speedup (combined forward + backward pass) and memory savings from using FlashAttention against PyTorch standard attention, depending on sequence length, on different GPUs (speedup depends on memory bandwidth - we see more speedup on slower GPU memory).
### A100
We display FlashAttention speedup using these parameters (similar to BERT-base):
* Batch size 8
* Head dimension 64
* 12 attention heads
2022-05-28 05:38:20 +08:00
Our graphs show sequence lengths between 128 and 4096 (when standard attention runs out of memory on an A100), but FlashAttention can scale up to sequence length 64K.
2022-06-02 11:07:00 +08:00
#### Speedup
![FlashAttention speedup](assets/flashattn_speedup.jpg)
We generally see 2-4X speedup at sequence lengths between 128 and 4K, and we see more speedup when using dropout and masking, since we fuse the kernels.
At sequence lengths that are popular with language models like 512 and 1K, we see speedups up to 4X when using dropout and masking.
2022-06-02 11:07:00 +08:00
#### Memory
![FlashAttention memory](assets/flashattn_memory.jpg)
We show memory savings in this graph (note that memory footprint is the same no matter if you use dropout or masking).
Memory savings are proportional to sequence length -- since standard attention has memory quadratic in sequence length, whereas FlashAttention has memory linear in sequence length.
We see 10X memory savings at sequence length 2K, and 20X at 4K.
As a result, FlashAttention can scale to much longer sequence lengths.
2022-05-27 04:57:38 +08:00
2022-06-02 11:07:00 +08:00
### RTX 3090
For the RTX 3090, we use batch size 12 with 12 attention heads.
Memory savings are the same as on an A100, so we'll only show speedup here.
![FlashAttention speedup GTX 3090](assets/flashattn_speedup_3090.jpg)
We see slightly higher speedups (between 2.5-4.5x) on the GTX 3090, since memory bandwidth on the GDDR6X is lower than A100 HBM (~900 GB/s vs. ~1.5 TB/s).
2022-05-30 06:42:43 +08:00
## Acknowledgments
2022-05-27 04:57:38 +08:00
Our implementation uses Apex's
[FMHA](https://github.com/NVIDIA/apex/tree/master/apex/contrib/csrc/fmha) code
as a starting point.
We thank [Young-Jun Ko](https://yjk21.github.io/) for the in-depth explanation of his FMHA implementation
and for his thoughtful answers to our questions about CUDA.
2022-05-31 04:29:04 +08:00
## Citation
If you use this codebase, or otherwise found our work valuable, please cite:
```
@article{dao2022flashattention,
title={FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness},
author={Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\'e}, Christopher},
journal={arXiv preprint arXiv:2205.14135},
year={2022}
}
```