test_flashmla.py 6.43 KB
Newer Older
1
2
# Adapted from: https://github.com/deepseek-ai/FlashMLA/blob/main/tests/test_flash_mla.py
# SPDX-License-Identifier: Apache-2.0
3
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
4
5
6
7
8
9
10
11
12
import math
import random

import pytest
import torch

from vllm.attention.ops.flashmla import (flash_mla_with_kvcache,
                                         get_mla_metadata,
                                         is_flashmla_supported)
13
from vllm.triton_utils import triton
14
15


16
17
18
19
def cal_diff(x: torch.Tensor,
             y: torch.Tensor,
             name: str,
             use_fp8: bool = False) -> None:
20
21
22
    x, y = x.double(), y.double()
    cos_diff = 1 - 2 * (x * y).sum().item() / max(
        (x * x + y * y).sum().item(), 1e-12)
23
24
25
26
    if (use_fp8):
        assert cos_diff < 1e-4
    else:
        assert cos_diff < 1e-5
27
28
29
30
31
32
33
34
35

FLASH_MLA_UNSUPPORTED_REASON = is_flashmla_supported()[1] \
    if not is_flashmla_supported()[0] else "FlashMLA is supported"


@pytest.mark.skipif(not is_flashmla_supported()[0],
                    reason=FLASH_MLA_UNSUPPORTED_REASON)
@pytest.mark.parametrize("b", [128])
@pytest.mark.parametrize("s_q", [1, 2])
36
@pytest.mark.parametrize("mean_sk", [4096, 8192, 16384])
37
38
39
40
41
42
43
@pytest.mark.parametrize("h_q", [16, 32, 64, 128])
@pytest.mark.parametrize("h_kv", [1])
@pytest.mark.parametrize("d", [576])
@pytest.mark.parametrize("dv", [512])
@pytest.mark.parametrize("block_size", [64])
@pytest.mark.parametrize("causal", [True])
@pytest.mark.parametrize("varlen", [False, True])
44
45
@pytest.mark.parametrize("torch_dtype",
                         [torch.bfloat16, torch.float16, torch.float8_e4m3fn])
46
47
@torch.inference_mode()
def test_flash_mla(b, s_q, mean_sk, h_q, h_kv, d, dv, block_size, causal,
48
                   varlen, torch_dtype):
49
    device = torch.device("cuda:0")
50
51
52
53
54
    if torch_dtype == torch.float8_e4m3fn:
        init_dtype = torch.bfloat16
    else:
        init_dtype = torch_dtype
    torch.set_default_dtype(init_dtype)
55
56
57
58
59
60
    torch.set_default_device(device)
    torch.cuda.set_device(device)
    torch.manual_seed(0)
    random.seed(0)

    print(f"{b=}, {s_q=}, {mean_sk=}, {h_q=}, {h_kv=}, "
61
          f"{d=}, {dv=}, {causal=}, {varlen=}, {torch_dtype=}")
62

63
    use_fp8 = torch_dtype == torch.float8_e4m3fn
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    cache_seqlens = torch.full((b, ), mean_sk, dtype=torch.int32)
    if varlen:
        for i in range(b):
            cache_seqlens[i] = max(random.normalvariate(mean_sk, mean_sk / 2),
                                   s_q)
    total_seqlens = cache_seqlens.sum().item()
    max_seqlen = cache_seqlens.max().item()
    max_seqlen_pad = triton.cdiv(max_seqlen, 256) * 256

    q = torch.randn(b, s_q, h_q, d)
    block_table = torch.arange(b * max_seqlen_pad // block_size,
                               dtype=torch.int32).view(
                                   b, max_seqlen_pad // block_size)
    blocked_k = torch.randn(block_table.numel(), block_size, h_kv, d)
    for i in range(b):
        blocked_k.view(b, max_seqlen_pad, h_kv,
                       d)[i, cache_seqlens[i].item():] = float("nan")
    blocked_v = blocked_k[..., :dv]

    tile_scheduler_metadata, num_splits = get_mla_metadata(
        cache_seqlens, s_q * h_q // h_kv, h_kv)

86
87
88
89
90
91
92
93
94
95
96
97
98
    init_dtype = q.dtype
    if use_fp8:
        fp8_dtype = torch.float8_e4m3fn
        descale_q = torch.ones((1), dtype=torch.float32)
        descale_k = torch.ones((1), dtype=torch.float32)

        q = q.to(fp8_dtype)
        blocked_k = blocked_k.to(fp8_dtype)
        blocked_v = blocked_v.to(fp8_dtype)
    else:
        descale_q = None
        descale_k = None

99
    def flash_mla():
100
101
102
103
104
105
106
107
108
109
        return flash_mla_with_kvcache(q,
                                      blocked_k,
                                      block_table,
                                      cache_seqlens,
                                      dv,
                                      tile_scheduler_metadata,
                                      num_splits,
                                      causal=causal,
                                      descale_q=descale_q,
                                      descale_k=descale_k)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

    def scaled_dot_product_attention(query, key, value, is_causal=False):
        query = query.float()
        key = key.float()
        value = value.float()
        key = key.repeat_interleave(h_q // h_kv, dim=0)
        value = value.repeat_interleave(h_q // h_kv, dim=0)
        attn_weight = query @ key.transpose(-2, -1) / math.sqrt(query.size(-1))
        if is_causal:
            s_q = query.shape[-2]
            s_k = key.shape[-2]
            attn_bias = torch.zeros(s_q, s_k, dtype=query.dtype)
            temp_mask = torch.ones(s_q, s_k,
                                   dtype=torch.bool).tril(diagonal=s_k - s_q)
            attn_bias.masked_fill_(temp_mask.logical_not(), float("-inf"))
            attn_bias.to(query.dtype)
            attn_weight += attn_bias
        lse = attn_weight.logsumexp(dim=-1)
        attn_weight = torch.softmax(attn_weight, dim=-1, dtype=torch.float32)
        return attn_weight @ value, lse

    def ref_mla():
132
133
134
135
136
        q_ = (q.to(torch.float) * descale_q).to(init_dtype) if use_fp8 else q
        blocked_k_ = (blocked_k.to(torch.float) *
                      descale_k).to(init_dtype) if use_fp8 else blocked_k
        blocked_v_ = (blocked_v.to(torch.float) *
                      descale_k).to(init_dtype) if use_fp8 else blocked_v
137
138
139
140
141
        out = torch.empty(b, s_q, h_q, dv, dtype=torch.float32)
        lse = torch.empty(b, h_q, s_q, dtype=torch.float32)
        for i in range(b):
            begin = i * max_seqlen_pad
            end = begin + cache_seqlens[i]
142
143
144
145
            out_i, lse_i = scaled_dot_product_attention(
                q_[i].transpose(0, 1),
                blocked_k_.view(-1, h_kv, d)[begin:end].transpose(0, 1),
                blocked_v_.view(-1, h_kv, dv)[begin:end].transpose(0, 1),
146
147
                is_causal=causal,
            )
148
149
            out[i] = out_i.transpose(0, 1)
            lse[i] = lse_i
150
151
152
153
        return out, lse

    out_flash, lse_flash = flash_mla()
    out_torch, lse_torch = ref_mla()
154
    cal_diff(out_flash, out_torch, "out", use_fp8)
155
156
    cal_diff(lse_flash, lse_torch, "lse")

157
    t = triton.testing.do_bench(flash_mla)
158
    FLOPS = s_q * total_seqlens * h_q * (d + dv) * 2
159
160
161
162
163
    bytes = (total_seqlens * h_kv * d +
             b * s_q * h_q * d) * (torch.finfo(torch_dtype).bits // 8) + (
                 b * s_q * h_q * dv) * (torch.finfo(init_dtype).bits // 8)
    print(f"{t:.3f} ms, {FLOPS / 10 ** 9 / t:.0f} TFLOPS,",
          f"{bytes / 10 ** 6 / t:.0f} GB/s")