| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | |
| 5 | import pytest |
| 6 | import torch |
| 7 | |
| 8 | from vllm.platforms import current_platform |
| 9 | from vllm.utils.torch_utils import set_random_seed |
| 10 | from vllm.v1.attention.backends.flash_attn import cascade_attention, merge_attn_states |
| 11 | |
| 12 | try: |
| 13 | from vllm.vllm_flash_attn import ( |
| 14 | fa_version_unsupported_reason, |
| 15 | flash_attn_varlen_func, |
| 16 | is_fa_version_supported, |
| 17 | ) |
| 18 | except ImportError: |
| 19 | if current_platform.is_rocm(): |
| 20 | pytest.skip( |
| 21 | "vllm_flash_attn is not supported for vLLM on ROCm.", |
| 22 | allow_module_level=True, |
| 23 | ) |
| 24 | |
| 25 | NUM_HEADS = [(4, 4), (8, 2), (16, 2)] |
| 26 | HEAD_SIZES = [128, 192, 256] |
| 27 | BLOCK_SIZES = [16] |
| 28 | DTYPES = [torch.float16, torch.bfloat16] |
| 29 | |
| 30 | |
| 31 | @pytest.mark.parametrize("num_tokens", [1, 39, 16912]) |
| 32 | @pytest.mark.parametrize("num_heads", NUM_HEADS) |
| 33 | @pytest.mark.parametrize("head_size", HEAD_SIZES) |
| 34 | @pytest.mark.parametrize("dtype", DTYPES) |
| 35 | @torch.inference_mode() |
| 36 | def test_merge_kernel( |
| 37 | num_tokens: int, |
| 38 | num_heads: tuple[int, int], |
| 39 | head_size: int, |
| 40 | dtype: torch.dtype, |
| 41 | ): |
| 42 | torch.set_default_device("cuda") |
| 43 | set_random_seed(0) |
| 44 | num_query_heads = num_heads[0] |
| 45 | num_kv_heads = num_heads[1] |
| 46 | assert num_query_heads % num_kv_heads == 0 |
| 47 | |
| 48 | # Prepare inputs. |
| 49 | prefix_output = torch.randn(num_tokens, num_query_heads, head_size, dtype=dtype) |
| 50 | suffix_output = torch.randn(num_tokens, num_query_heads, head_size, dtype=dtype) |
| 51 | prefix_lse = torch.randn(num_query_heads, num_tokens, dtype=torch.float32) |
| 52 | suffix_lse = torch.randn(num_query_heads, num_tokens, dtype=torch.float32) |
| 53 | |
| 54 | # Run the kernel. |
| 55 | output = torch.empty(num_tokens, num_query_heads, head_size, dtype=dtype) |
| 56 | merge_attn_states(output, prefix_output, prefix_lse, suffix_output, suffix_lse) |
| 57 | |
| 58 | # Reference implementation. |
| 59 | max_lse = torch.maximum(prefix_lse, suffix_lse) |
| 60 | p_lse = torch.exp(prefix_lse - max_lse) |
| 61 | s_lse = torch.exp(suffix_lse - max_lse) |
| 62 | p_scale = p_lse / (p_lse + s_lse) |
| 63 | s_scale = s_lse / (p_lse + s_lse) |
| 64 | p_scale = p_scale.transpose(0, 1).unsqueeze(2) |
| 65 | s_scale = s_scale.transpose(0, 1).unsqueeze(2) |
| 66 | ref_output = p_scale * prefix_output + s_scale * suffix_output |
| 67 | ref_output = ref_output.to(dtype) |
| 68 | |
| 69 | # Compare the results. |
| 70 | torch.testing.assert_close(output, ref_output, atol=1e-2, rtol=1e-2) |
| 71 | |
| 72 | |
| 73 | CASES = [ |
| 74 | # Case 1. A general case. |
| 75 | ([(129, 871), (18, 280), (37, 988), (1023, 2304), (1, 257)], 256), |
| 76 | # Case 2. Flash-decoding case. |
| 77 | ([(1, 1023), (1, 879), (1, 778), (1, 1777)] * 100, 512), |
| 78 | ] |
| 79 | |
| 80 | |
| 81 | @pytest.mark.parametrize("seq_lens_and_common_prefix", CASES) |
| 82 | @pytest.mark.parametrize("num_heads", NUM_HEADS) |
| 83 | @pytest.mark.parametrize("head_size", HEAD_SIZES) |
| 84 | @pytest.mark.parametrize("dtype", DTYPES) |
| 85 | @pytest.mark.parametrize("block_size", BLOCK_SIZES) |
| 86 | @pytest.mark.parametrize("soft_cap", [None, 50]) |
| 87 | @pytest.mark.parametrize("num_blocks", [2048]) |
| 88 | @pytest.mark.parametrize("fa_version", [2, 3]) |
| 89 | @torch.inference_mode() |
| 90 | def test_cascade( |
| 91 | seq_lens_and_common_prefix: tuple[list[tuple[int, int]], int], |
| 92 | num_heads: tuple[int, int], |
| 93 | head_size: int, |
| 94 | dtype: torch.dtype, |
| 95 | block_size: int, |
| 96 | soft_cap: float | None, |
| 97 | num_blocks: int, |
| 98 | fa_version: int, |
| 99 | ) -> None: |
| 100 | torch.set_default_device("cuda") |
| 101 | if not is_fa_version_supported(fa_version): |
| 102 | pytest.skip( |
| 103 | f"Flash attention version {fa_version} not supported due " |
| 104 | f'to: "{fa_version_unsupported_reason(fa_version)}"' |
| 105 | ) |
| 106 | |
| 107 | set_random_seed(0) |
| 108 | |
| 109 | window_size = (-1, -1) |
| 110 | scale = head_size**-0.5 |
| 111 | num_query_heads = num_heads[0] |
| 112 | num_kv_heads = num_heads[1] |
| 113 | assert num_query_heads % num_kv_heads == 0 |
| 114 | key_cache = torch.randn( |
| 115 | num_blocks, block_size, num_kv_heads, head_size, dtype=dtype |
| 116 | ) |
| 117 | value_cache = torch.randn_like(key_cache) |
| 118 | |
| 119 | seq_lens, common_prefix_len = seq_lens_and_common_prefix |
| 120 | num_seqs = len(seq_lens) |
| 121 | query_lens = [x[0] for x in seq_lens] |
| 122 | kv_lens = [x[1] for x in seq_lens] |
| 123 | max_query_len = max(query_lens) |
| 124 | max_kv_len = max(kv_lens) |
| 125 | |
| 126 | total_num_query_tokens = sum(query_lens) |
| 127 | query = torch.randn(total_num_query_tokens, num_query_heads, head_size, dtype=dtype) |
| 128 | cu_query_lens = torch.tensor([0] + query_lens, dtype=torch.int32).cumsum( |
| 129 | dim=0, dtype=torch.int32 |
| 130 | ) |
| 131 | kv_lens_tensor = torch.tensor(kv_lens, dtype=torch.int32) |
| 132 | max_num_blocks_per_seq = (max_kv_len + block_size - 1) // block_size |
| 133 | block_tables = torch.randint( |
| 134 | 0, num_blocks, (num_seqs, max_num_blocks_per_seq), dtype=torch.int32 |
| 135 | ) |
| 136 | |
| 137 | assert common_prefix_len > 0 |
| 138 | assert common_prefix_len % block_size == 0 |
| 139 | num_common_kv_blocks = common_prefix_len // block_size |
| 140 | # Make sure the first `num_common_kv_blocks` blocks are the same. |
| 141 | block_tables[:, :num_common_kv_blocks] = block_tables[0, :num_common_kv_blocks] |
| 142 | |
| 143 | # Run the regular attention. |
| 144 | ref_output = flash_attn_varlen_func( |
| 145 | q=query, |
| 146 | k=key_cache, |
| 147 | v=value_cache, |
| 148 | cu_seqlens_q=cu_query_lens, |
| 149 | seqused_k=kv_lens_tensor, |
| 150 | max_seqlen_q=max_query_len, |
| 151 | max_seqlen_k=max_kv_len, |
| 152 | softmax_scale=scale, |
| 153 | causal=True, |
| 154 | window_size=list(window_size), |
| 155 | block_table=block_tables, |
| 156 | softcap=soft_cap if soft_cap is not None else 0, |
| 157 | ) |
| 158 | |
| 159 | # Run cascade attention. |
| 160 | assert all(common_prefix_len < kv_len for kv_len in kv_lens) |
| 161 | cu_prefix_query_lens = torch.tensor([0, total_num_query_tokens], dtype=torch.int32) |
| 162 | prefix_kv_lens = torch.tensor([common_prefix_len], dtype=torch.int32) |
| 163 | suffix_kv_lens = kv_lens_tensor - common_prefix_len |
| 164 | output = torch.empty_like(query) |
| 165 | cascade_attention( |
| 166 | output=output, |
| 167 | query=query, |
| 168 | key_cache=key_cache, |
| 169 | value_cache=value_cache, |
| 170 | cu_query_lens=cu_query_lens, |
| 171 | max_query_len=max_query_len, |
| 172 | cu_prefix_query_lens=cu_prefix_query_lens, |
| 173 | prefix_kv_lens=prefix_kv_lens, |
| 174 | suffix_kv_lens=suffix_kv_lens, |
| 175 | max_kv_len=max_kv_len, |
| 176 | softmax_scale=scale, |
| 177 | alibi_slopes=None, |
| 178 | sliding_window=window_size, |
| 179 | logits_soft_cap=soft_cap if soft_cap is not None else 0, |
| 180 | block_table=block_tables, |
| 181 | common_prefix_len=common_prefix_len, |
| 182 | max_num_splits=0, # no max |
| 183 | fa_version=fa_version, |
| 184 | ) |
| 185 | |
| 186 | # Compare the results. |
| 187 | torch.testing.assert_close(output, ref_output, atol=1e-2, rtol=1e-2) |