README.md 2.76 KB
Newer Older
1
2
3
4
# FlashAttention
This repository provides the official implementation of FlashAttention from the
following paper.

Tri Dao's avatar
Tri Dao committed
5
**FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness**  
Tri Dao's avatar
Tri Dao committed
6
7
Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré  
Paper: https://arxiv.org/abs/2205.14135
8
![FlashAttention](assets/flashattn_banner.jpg)
9
10

## Alpha release (0.1).
Tri Dao's avatar
Tri Dao committed
11

Tri Dao's avatar
Tri Dao committed
12
To compile (requiring NVCC and an A100 GPU):
Tri Dao's avatar
Tri Dao committed
13
```
Tri Dao's avatar
Tri Dao committed
14
cd csrc/flash_attn
Tri Dao's avatar
Tri Dao committed
15
16
17
python setup.py install
```

Tri Dao's avatar
Tri Dao committed
18
Interface: `src/flash_attention.py`
Tri Dao's avatar
Tri Dao committed
19

Tri Dao's avatar
Tri Dao committed
20
21
22
23
24
25
26
27
28
29
To run the benchmark against PyTorch standard attention: 
```
PYTHONPATH=$PWD python benchmarks/benchmark_flash_attention.py
```

FlashAttention currently supports:
1. A100 GPUs.
2. fp16.
3. Head dimensions 16, 32, 64.

Tri Dao's avatar
Tri Dao committed
30
31
32
33
34
35
36
37
38
39
Our tentative roadmap:
1. [Jun 2022] Make package pip-installable.
2. [Jun 2022] Support SM86 GPUs (e.g., RTX 3080, 3090).
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).
Tri Dao's avatar
Tri Dao committed
40

Tri Dao's avatar
Tri Dao committed
41
## Speedup and Memory Savings
Dan Fu's avatar
Dan Fu committed
42
43
44
45
46
47

We present expected speedup (combined forward + backward pass) and memory savings from using FlashAttention against PyTorch standard attention, depending on sequence length.
We display FlashAttention speedup using these parameters (similar to BERT-base):
* Batch size 8
* Head dimension 64
* 12 attention heads
Dan Fu's avatar
Dan Fu committed
48

Dan Fu's avatar
Dan Fu committed
49
50
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.

Tri Dao's avatar
Tri Dao committed
51
### Speedup
Dan Fu's avatar
Dan Fu committed
52

53
![FlashAttention speedup](assets/flashattn_speedup.jpg)
Dan Fu's avatar
Dan Fu committed
54
55
56
57

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.

Tri Dao's avatar
Tri Dao committed
58
### Memory
Dan Fu's avatar
Dan Fu committed
59

60
![FlashAttention memory](assets/flashattn_memory.jpg)
Dan Fu's avatar
Dan Fu committed
61
62
63
64
65

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.
Tri Dao's avatar
Tri Dao committed
66

Tri Dao's avatar
Tri Dao committed
67
## Acknowledgments
Tri Dao's avatar
Tri Dao committed
68
69
70
71
72
73
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.