forked from flagos-ai/FlagGems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flash_attention_forward_no_dropout_inplace.py
More file actions
124 lines (107 loc) · 3.8 KB
/
Copy pathtest_flash_attention_forward_no_dropout_inplace.py
File metadata and controls
124 lines (107 loc) · 3.8 KB
1
2
3
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import pytest
import torch
import flag_gems
from . import base, consts
device = flag_gems.device
# (batch, num_heads, q_seq_len, kv_seq_len, head_dim) configs.
FLASH_FWD_CONFIGS = [
(1, 2, 512, 512, 64),
(1, 8, 1024, 1024, 128),
(2, 4, 512, 512, 64),
(1, 2, 1024, 2048, 64),
]
def torch_flash_attention_forward_no_dropout_inplace(
q,
k,
v,
scale,
is_causal,
return_debug_mask=False,
**extra_kwargs,
):
"""Reference: aten::_flash_attention_forward with dropout_p=0.0."""
return torch.ops.aten._flash_attention_forward(
q,
k,
v,
None,
None,
q.shape[-3],
k.shape[-3],
0.0, # dropout_p = 0.0 (no dropout)
is_causal,
return_debug_mask,
scale=scale,
**extra_kwargs,
)
def gems_flash_attention_forward_no_dropout_inplace(
q,
k,
v,
scale,
is_causal,
return_debug_mask=False,
**extra_kwargs,
):
"""FlagGems Triton implementation (no dropout, in-place into ``q``)."""
# ``do_bench`` reuses the same tensors across iterations, so clone ``q`` to
# preserve the original data between runs (the kernel writes in-place).
return flag_gems._flash_attention_forward_no_dropout_inplace(
q.clone(),
k,
v,
None,
None,
q.shape[-3],
k.shape[-3],
is_causal,
return_debug_mask,
scale=scale,
**extra_kwargs,
)
def flash_attention_forward_no_dropout_inplace_input_fn(config, dtype, device):
batch, num_head, q_seq_len, kv_seq_len, head_size = config
q = torch.empty(
(batch, q_seq_len, num_head, head_size), device=device, dtype=dtype
).uniform_(-0.05, 0.05)
k = torch.empty(
(batch, kv_seq_len, num_head, head_size), device=device, dtype=dtype
).uniform_(-0.05, 0.05)
v = torch.empty(
(batch, kv_seq_len, num_head, head_size), device=device, dtype=dtype
).uniform_(-0.05, 0.05)
scale = float(1.0 / math.sqrt(head_size))
# BSHD layout; no dropout; non-causal for the default benchmark configs.
yield q, k, v, scale, False, False, {}
class FlashAttentionForwardNoDropoutInplaceBenchmark(base.GenericBenchmark):
def set_shapes(self, shape_file_path=None):
# Use the configs defined in FLASH_FWD_CONFIGS directly, since this
# operator has no entry in the shared core-shapes yaml file.
self.shapes = [tuple(c) for c in FLASH_FWD_CONFIGS]
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is not available")
@pytest.mark.skipif(flag_gems.device == "cpu", reason="Unsupported in CPU mode")
@pytest.mark.flash_attention_forward_no_dropout_inplace
def test_flash_attention_forward_no_dropout_inplace_impl():
bench = FlashAttentionForwardNoDropoutInplaceBenchmark(
op_name="flash_attention_forward_no_dropout_inplace",
torch_op=torch_flash_attention_forward_no_dropout_inplace,
input_fn=flash_attention_forward_no_dropout_inplace_input_fn,
# FlashAttention only supports fp16/bf16; filter from FLOAT_DTYPES.
dtypes=[d for d in consts.FLOAT_DTYPES if d in (torch.float16, torch.bfloat16)],
)
bench.set_gems(gems_flash_attention_forward_no_dropout_inplace)
bench.run()