|
| 1 | +# Copyright 2026 FlagOS Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import math |
| 16 | + |
| 17 | +import pytest |
| 18 | +import torch |
| 19 | + |
| 20 | +import flag_gems |
| 21 | + |
| 22 | +from . import base, consts |
| 23 | + |
| 24 | +device = flag_gems.device |
| 25 | + |
| 26 | +# (batch, num_heads, q_seq_len, kv_seq_len, head_dim) configs. |
| 27 | +FLASH_FWD_CONFIGS = [ |
| 28 | + (1, 2, 512, 512, 64), |
| 29 | + (1, 8, 1024, 1024, 128), |
| 30 | + (2, 4, 512, 512, 64), |
| 31 | + (1, 2, 1024, 2048, 64), |
| 32 | +] |
| 33 | + |
| 34 | + |
| 35 | +def torch_flash_attention_forward_no_dropout_inplace( |
| 36 | + q, |
| 37 | + k, |
| 38 | + v, |
| 39 | + scale, |
| 40 | + is_causal, |
| 41 | + return_debug_mask=False, |
| 42 | + **extra_kwargs, |
| 43 | +): |
| 44 | + """Reference: aten::_flash_attention_forward with dropout_p=0.0.""" |
| 45 | + return torch.ops.aten._flash_attention_forward( |
| 46 | + q, |
| 47 | + k, |
| 48 | + v, |
| 49 | + None, |
| 50 | + None, |
| 51 | + q.shape[-3], |
| 52 | + k.shape[-3], |
| 53 | + 0.0, # dropout_p = 0.0 (no dropout) |
| 54 | + is_causal, |
| 55 | + return_debug_mask, |
| 56 | + scale=scale, |
| 57 | + **extra_kwargs, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def gems_flash_attention_forward_no_dropout_inplace( |
| 62 | + q, |
| 63 | + k, |
| 64 | + v, |
| 65 | + scale, |
| 66 | + is_causal, |
| 67 | + return_debug_mask=False, |
| 68 | + **extra_kwargs, |
| 69 | +): |
| 70 | + """FlagGems Triton implementation (no dropout, in-place into ``q``).""" |
| 71 | + # ``do_bench`` reuses the same tensors across iterations, so clone ``q`` to |
| 72 | + # preserve the original data between runs (the kernel writes in-place). |
| 73 | + return flag_gems._flash_attention_forward_no_dropout_inplace( |
| 74 | + q.clone(), |
| 75 | + k, |
| 76 | + v, |
| 77 | + None, |
| 78 | + None, |
| 79 | + q.shape[-3], |
| 80 | + k.shape[-3], |
| 81 | + is_causal, |
| 82 | + return_debug_mask, |
| 83 | + scale=scale, |
| 84 | + **extra_kwargs, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def flash_attention_forward_no_dropout_inplace_input_fn(config, dtype, device): |
| 89 | + batch, num_head, q_seq_len, kv_seq_len, head_size = config |
| 90 | + q = torch.empty( |
| 91 | + (batch, q_seq_len, num_head, head_size), device=device, dtype=dtype |
| 92 | + ).uniform_(-0.05, 0.05) |
| 93 | + k = torch.empty( |
| 94 | + (batch, kv_seq_len, num_head, head_size), device=device, dtype=dtype |
| 95 | + ).uniform_(-0.05, 0.05) |
| 96 | + v = torch.empty( |
| 97 | + (batch, kv_seq_len, num_head, head_size), device=device, dtype=dtype |
| 98 | + ).uniform_(-0.05, 0.05) |
| 99 | + scale = float(1.0 / math.sqrt(head_size)) |
| 100 | + |
| 101 | + # BSHD layout; no dropout; non-causal for the default benchmark configs. |
| 102 | + yield q, k, v, scale, False, False, {} |
| 103 | + |
| 104 | + |
| 105 | +class FlashAttentionForwardNoDropoutInplaceBenchmark(base.GenericBenchmark): |
| 106 | + def set_shapes(self, shape_file_path=None): |
| 107 | + # Use the configs defined in FLASH_FWD_CONFIGS directly, since this |
| 108 | + # operator has no entry in the shared core-shapes yaml file. |
| 109 | + self.shapes = [tuple(c) for c in FLASH_FWD_CONFIGS] |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is not available") |
| 113 | +@pytest.mark.skipif(flag_gems.device == "cpu", reason="Unsupported in CPU mode") |
| 114 | +@pytest.mark.flash_attention_forward_no_dropout_inplace |
| 115 | +def test_flash_attention_forward_no_dropout_inplace_impl(): |
| 116 | + bench = FlashAttentionForwardNoDropoutInplaceBenchmark( |
| 117 | + op_name="flash_attention_forward_no_dropout_inplace", |
| 118 | + torch_op=torch_flash_attention_forward_no_dropout_inplace, |
| 119 | + input_fn=flash_attention_forward_no_dropout_inplace_input_fn, |
| 120 | + # FlashAttention only supports fp16/bf16; filter from FLOAT_DTYPES. |
| 121 | + dtypes=[d for d in consts.FLOAT_DTYPES if d in (torch.float16, torch.bfloat16)], |
| 122 | + ) |
| 123 | + bench.set_gems(gems_flash_attention_forward_no_dropout_inplace) |
| 124 | + bench.run() |
0 commit comments