Update flash_attention.py
Browse files- flash_attention.py +72 -75
flash_attention.py
CHANGED
@@ -1,75 +1,72 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
from einops import rearrange
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
)
|
74 |
-
|
75 |
-
return output, None
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from einops import rearrange
|
4 |
+
|
5 |
+
from triton_flash_atn import _attention
|
6 |
+
|
7 |
+
from triton_bert_pading import pad_input, unpad_input
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
class FlashAttention(nn.Module):
|
12 |
+
"""Implement the scaled dot product attention with softmax.
|
13 |
+
Arguments
|
14 |
+
---------
|
15 |
+
softmax_scale: The temperature to use for the softmax attention.
|
16 |
+
(default: 1/sqrt(d_keys) where d_keys is computed at
|
17 |
+
runtime)
|
18 |
+
attention_dropout: The dropout rate to apply to the attention
|
19 |
+
(default: 0.0)
|
20 |
+
"""
|
21 |
+
|
22 |
+
def __init__(self, softmax_scale=None, attention_dropout=0.0, device=None, dtype=None):
|
23 |
+
super().__init__()
|
24 |
+
self.softmax_scale = softmax_scale
|
25 |
+
self.dropout_p = attention_dropout
|
26 |
+
|
27 |
+
def forward(self, qkv, key_padding_mask=None, causal=False, cu_seqlens=None,
|
28 |
+
max_s=None, need_weights=False):
|
29 |
+
"""Implements the multihead softmax attention.
|
30 |
+
Arguments
|
31 |
+
---------
|
32 |
+
qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) if key_padding_mask is None
|
33 |
+
if unpadded: (nnz, 3, h, d)
|
34 |
+
key_padding_mask: a bool tensor of shape (B, S)
|
35 |
+
"""
|
36 |
+
assert not need_weights
|
37 |
+
assert qkv.dtype in [torch.float16, torch.bfloat16]
|
38 |
+
assert qkv.is_cuda
|
39 |
+
|
40 |
+
if cu_seqlens is None:
|
41 |
+
batch_size = qkv.shape[0]
|
42 |
+
seqlen = qkv.shape[1]
|
43 |
+
if key_padding_mask is None:
|
44 |
+
qkv = rearrange(qkv, 'b s ... -> (b s) ...')
|
45 |
+
max_s = seqlen
|
46 |
+
cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32,
|
47 |
+
device=qkv.device)
|
48 |
+
output = _attention.apply(
|
49 |
+
qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,
|
50 |
+
softmax_scale=self.softmax_scale, causal=causal
|
51 |
+
)
|
52 |
+
output = rearrange(output, '(b s) ... -> b s ...', b=batch_size)
|
53 |
+
else:
|
54 |
+
nheads = qkv.shape[-2]
|
55 |
+
x = rearrange(qkv, 'b s three h d -> b s (three h d)')
|
56 |
+
x_unpad, indices, cu_seqlens, max_s = unpad_input(x, key_padding_mask)
|
57 |
+
x_unpad = rearrange(x_unpad, 'nnz (three h d) -> nnz three h d', three=3, h=nheads)
|
58 |
+
output_unpad = _attention.apply(
|
59 |
+
x_unpad, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,
|
60 |
+
softmax_scale=self.softmax_scale, causal=causal
|
61 |
+
)
|
62 |
+
output = rearrange(pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'),
|
63 |
+
indices, batch_size, seqlen),
|
64 |
+
'b s (h d) -> b s h d', h=nheads)
|
65 |
+
else:
|
66 |
+
assert max_s is not None
|
67 |
+
output = _attention.apply(
|
68 |
+
qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,
|
69 |
+
softmax_scale=self.softmax_scale, causal=causal
|
70 |
+
)
|
71 |
+
|
72 |
+
return output, None
|
|
|
|
|
|