README.md 8.83 KB
Newer Older
1
# FlashAttention
Tri Dao's avatar
Tri Dao committed
2
3
4
This repository provides the official implementation of FlashAttention and
FlashAttention-2 from the
following papers.
5

Tri Dao's avatar
Tri Dao committed
6
**FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness**  
Tri Dao's avatar
Tri Dao committed
7
Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré  
8
9
Paper: https://arxiv.org/abs/2205.14135  
IEEE Spectrum [article](https://spectrum.ieee.org/mlperf-rankings-2022) about our submission to the MLPerf 2.0 benchmark using FlashAttention.
10
![FlashAttention](assets/flashattn_banner.jpg)
11

Tri Dao's avatar
Tri Dao committed
12
13
**FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning**  
Tri Dao
Tri Dao's avatar
Tri Dao committed
14

Tri Dao's avatar
Tri Dao committed
15
Paper: https://tridao.me/publications/flash2/flash2.pdf
Tri Dao's avatar
Tri Dao committed
16

Tri Dao's avatar
Tri Dao committed
17
![FlashAttention-2](assets/flashattention_logo.png)
Tri Dao's avatar
Tri Dao committed
18
19


Tri Dao's avatar
Tri Dao committed
20
## Usage
Tri Dao's avatar
Tri Dao committed
21

Tri Dao's avatar
Tri Dao committed
22
23
24
We've been very happy to see FlashAttention being widely adopted in such a short
time after its release. This [page](https://github.com/Dao-AILab/flash-attention/blob/main/usage.md)
contains a partial list of places where FlashAttention is being used.
Tri Dao's avatar
Tri Dao committed
25

Tri Dao's avatar
Tri Dao committed
26
27
FlashAttention and FlashAttention-2 are free to use and modify (see LICENSE).
Please cite and credit FlashAttention if you use it.
Tri Dao's avatar
Tri Dao committed
28

Tri Dao's avatar
Tri Dao committed
29
## Installation and features
Tri Dao's avatar
Tri Dao committed
30

31
32
33
34
Requirements:
- CUDA 11.4 and above.
- PyTorch 1.12 and above.

Tri Dao's avatar
Tri Dao committed
35
36
37
38
We recommend the
[Pytorch](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch)
container from Nvidia, which has all the required tools to install FlashAttention.

39
To install:
Tri Dao's avatar
Tri Dao committed
40
41
42
43
44
1. Make sure that PyTorch is installed.
2. Make sure that `packaging` is installed (`pip install packaging`)
3. 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
Tri Dao's avatar
Tri Dao committed
45
`ninja` (`pip uninstall -y ninja && pip install ninja`). Without `ninja`,
Tri Dao's avatar
Tri Dao committed
46
47
48
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.
4. Then:
49
```sh
Tri Dao's avatar
Tri Dao committed
50
pip install flash-attn --no-build-isolation
51
52
```
Alternatively you can compile from source:
Tri Dao's avatar
Tri Dao committed
53
```
Tri Dao's avatar
Tri Dao committed
54
python setup.py install
Tri Dao's avatar
Tri Dao committed
55
56
```

Tri Dao's avatar
Tri Dao committed
57
Interface: `src/flash_attention_interface.py`
Tri Dao's avatar
Tri Dao committed
58

Tri Dao's avatar
Tri Dao committed
59
60
61
62
63
64
FlashAttention-2 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.
Tri Dao's avatar
Tri Dao committed
65

66
67
68

## How to use FlashAttention

Tri Dao's avatar
Tri Dao committed
69
70
71
72
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
73
74
```

Tri Dao's avatar
Tri Dao committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
```
flash_attn_qkvpacked_func(qkv, dropout_p=0.0, softmax_scale=None, causal=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.
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).
Return:
    out: (batch_size, seqlen, nheads, headdim).
```
90

Tri Dao's avatar
Tri Dao committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
```
flash_attn_func(q, k, v, dropout_p=0.0, softmax_scale=None, causal=False):
"""dropout_p should be set to 0.0 during evaluation
Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
than Q. Note that the number of heads in KV must be divisible by the number of heads in Q.
For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.

Arguments:
    q: (batch_size, seqlen, nheads, headdim)
    k: (batch_size, seqlen, nheads_k, headdim)
    v: (batch_size, seqlen, nheads_k, 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).
Return:
    out: (batch_size, seqlen, nheads, headdim).
109
110
```

Tri Dao's avatar
Tri Dao committed
111
112
To see how these functions are used in a multi-head attention layer (which
includes QKV projection, output projection), see the MHA [implementation](https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py).
113

Tri Dao's avatar
Tri Dao committed
114
## Upgrading from FlashAttention (1.x) to FlashAttention-2
115

Tri Dao's avatar
Tri Dao committed
116
117
118
119
These functions have been renamed:
- `flash_attn_unpadded_func` -> `flash_attn_varlen_func`
- `flash_attn_unpadded_qkvpacked_func` -> `flash_attn_varlen_qkvpacked_func`
- `flash_attn_unpadded_kvpacked_func` -> `flash_attn_varlen_kvpacked_func`
120

Tri Dao's avatar
Tri Dao committed
121
122
123
124
If the inputs have the same sequence lengths in the same batch, it is simpler
and faster to use these functions:
```
flash_attn_qkvpacked_func(qkv, dropout_p, softmax_scale=None, causal=False)
125
```
Tri Dao's avatar
Tri Dao committed
126
127
```
flash_attn_func(q, k, v, dropout_p=0.0, softmax_scale=None, causal=False)
128
129
```

Tri Dao's avatar
Tri Dao committed
130
## Performance
Dan Fu's avatar
Dan Fu committed
131

Dan Fu's avatar
Dan Fu committed
132
133
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).

Dan Fu's avatar
T4  
Dan Fu committed
134
135
We currently have benchmarks for these GPUs:
* [A100](#a100)
Tri Dao's avatar
Tri Dao committed
136
137
138
* [H100](#h100)
<!-- * [RTX 3090](#rtx-3090) -->
<!-- * [T4](#t4) -->
Dan Fu's avatar
T4  
Dan Fu committed
139

Dan Fu's avatar
Dan Fu committed
140
141
### A100

Tri Dao's avatar
Tri Dao committed
142
143
144
145
We display FlashAttention speedup using these parameters:
* Head dimension 64 or 128, hidden dimension 2048 (i.e. either 32 or 16 heads).
* Sequence length 512, 1k, 2k, 4k, 8k, 16k.
* Batch size set to 16k / seqlen.
Dan Fu's avatar
Dan Fu committed
146

Dan Fu's avatar
Dan Fu committed
147
#### Speedup
Dan Fu's avatar
Dan Fu committed
148

Tri Dao's avatar
Tri Dao committed
149
![FlashAttention speedup on A100 80GB SXM5 with FP16/BF16](assets/flash2_a100_fwd_bwd_benchmark.png)
Dan Fu's avatar
Dan Fu committed
150

Dan Fu's avatar
Dan Fu committed
151
#### Memory
Dan Fu's avatar
Dan Fu committed
152

153
![FlashAttention memory](assets/flashattn_memory.jpg)
Dan Fu's avatar
Dan Fu committed
154
155
156
157
158

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
159

Tri Dao's avatar
Tri Dao committed
160
### H100
Dan Fu's avatar
Dan Fu committed
161

Tri Dao's avatar
Tri Dao committed
162
![FlashAttention speedup on H100 SXM5 with FP16/BF16](assets/flash2_h100_fwd_bwd_benchmark.png)
Dan Fu's avatar
Dan Fu committed
163

Tri Dao's avatar
Tri Dao committed
164
## Full model code and training script
Dan Fu's avatar
Dan Fu committed
165

Tri Dao's avatar
Tri Dao committed
166
167
168
169
170
171
172
We have released the full GPT model
[implementation](https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/models/gpt.py).
We also provide optimized implementations of other layers (e.g., MLP, LayerNorm,
cross-entropy loss, rotary embedding). Overall this speeds up training by 3-5x
compared to the baseline implementation from Huggingface, reaching up to 225
TFLOPs/sec per A100, equivalent to 72% model FLOPs utilization (we don't need
any activation checkpointing).
Dan Fu's avatar
T4  
Dan Fu committed
173

Tri Dao's avatar
Tri Dao committed
174
175
176
We also include a training
[script](https://github.com/Dao-AILab/flash-attention/tree/main/training) to
train GPT2 on Openwebtext and GPT3 on The Pile.
Dan Fu's avatar
T4  
Dan Fu committed
177

Tri Dao's avatar
Tri Dao committed
178
## Triton implementation of FlashAttention
Dan Fu's avatar
T4  
Dan Fu committed
179

Tri Dao's avatar
Tri Dao committed
180
181
Phil Tillet (OpenAI) has an experimental implementation of FlashAttention in Triton:
https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
Dan Fu's avatar
T4  
Dan Fu committed
182

Tri Dao's avatar
Tri Dao committed
183
184
185
As Triton is a higher-level language than CUDA, it might be easier to understand
and experiment with. The notations in the Triton implementation are also closer
to what's used in our paper.
Dan Fu's avatar
T4  
Dan Fu committed
186

Tri Dao's avatar
Tri Dao committed
187
188
189
We also have an experimental implementation in Triton that support attention
bias (e.g. ALiBi):
https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/flash_attn_triton.py
Dan Fu's avatar
T4  
Dan Fu committed
190
191


Tri Dao's avatar
Tri Dao committed
192
193
194
195
196
197
198
199
200
201
202
## Tests
We test that FlashAttention produces the same output and gradient as a reference
implementation, up to some numerical tolerance. In particular, we check that the
maximum numerical error of FlashAttention is at most twice the numerical error
of a baseline implementation in Pytorch (for different head dimensions, input
dtype, sequence length, causal / non-causal).

To run the tests:
```
pytest -q -s tests/test_flash_attn.py
```
Tri Dao's avatar
Tri Dao committed
203
204
## When you encounter issues

Tri Dao's avatar
Tri Dao committed
205
This new release of FlashAttention-2 has been tested on several GPT-style
Tri Dao's avatar
Tri Dao committed
206
models, mostly on A100 GPUs.
Tri Dao's avatar
Tri Dao committed
207

Tri Dao's avatar
Tri Dao committed
208
If you encounter bugs, please open a GitHub Issue!
Dan Fu's avatar
Dan Fu committed
209
210
211
212

## Citation
If you use this codebase, or otherwise found our work valuable, please cite:
```
Tri Dao's avatar
Tri Dao committed
213
@inproceedings{dao2022flashattention,
214
  title={Flash{A}ttention: Fast and Memory-Efficient Exact Attention with {IO}-Awareness},
Dan Fu's avatar
Dan Fu committed
215
  author={Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\'e}, Christopher},
Tri Dao's avatar
Tri Dao committed
216
  booktitle={Advances in Neural Information Processing Systems},
Dan Fu's avatar
Dan Fu committed
217
218
  year={2022}
}
Tri Dao's avatar
Tri Dao committed
219
220
221
222
223
@article{dao2023flashattention2,
  title={Flash{A}ttention-2: Faster Attention with Better Parallelism and Work Partitioning,
  author={Dao, Tri},
  year={2023}
}
Dan Fu's avatar
Dan Fu committed
224
```