test_flash_mla_with_q_concat.py 12.1 KB
Newer Older
zhanghj2's avatar
zhanghj2 committed
1
2
3
4
5
6
7
import argparse
import math
import random

import torch
import triton

zhanghj2's avatar
zhanghj2 committed
8
9
from flash_mla import flash_mla_with_kvcache, get_mla_metadata, flash_mla_with_kvcache_q_nope_pe
# from flash_mla import flash_mla_with_kvcache, get_mla_metadata
zhanghj2's avatar
zhanghj2 committed
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
torch.set_printoptions(precision=4, profile="default", sci_mode=False)

def scaled_dot_product_attention(query, key, value, h_q, h_kv, is_causal=False, k_scale=1.0):
    query = query.float()
    key = key.float() * k_scale
    value = value.float() * k_scale
    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 cal_diff(x: torch.Tensor, y: torch.Tensor, name: str) -> None:
    torch_dtype = x.dtype
    x, y = x.double(), y.double()
    RMSE = ((x - y) * (x - y)).mean().sqrt().item()
    cos_diff = 1 - 2 * (x * y).sum().item() / max((x * x + y * y).sum().item(), 1e-12)
    amax_diff = (x - y).abs().max().item()
    print(f"{name}: {cos_diff=}, {RMSE=}, {amax_diff=}")
    assert cos_diff < (1e-4 if torch_dtype == torch.bfloat16 else 1e-5)

zhanghj2's avatar
zhanghj2 committed
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

@torch.inference_mode()
def test_flash_mla(b, s_q, mean_sk, h_q, h_kv, d, dv, causal, varlen, is_prof=False):
    print(
        f"{b=}, {s_q=}, {mean_sk=}, {h_q=}, {h_kv=}, {d=}, {dv=}, {causal=}, {varlen=}"
    )

    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()
    mean_seqlens = cache_seqlens.float().mean().int().item()
    max_seqlen = cache_seqlens.max().item()
    max_seqlen_pad = triton.cdiv(max_seqlen, 256) * 256
    print(f"{total_seqlens=}, {mean_seqlens=}, {max_seqlen=}, {max_seqlen_pad=}")

    q = torch.randn(b, s_q, h_q, d)
    block_size = 64
    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
    )

    # print("q:", q.shape, q.dtype, q)
    # print("cache_seqlens:", cache_seqlens.shape, cache_seqlens)
    # print("block_table:", block_table.shape, block_table)
    # print("blocked_k:", blocked_k.shape, blocked_k[0])
    # print("blocked_v:", blocked_v.shape)
    # torch.set_printoptions(precision=4, profile="full", sci_mode=False)
    # print("tile_scheduler_metadata:", tile_scheduler_metadata.shape, tile_scheduler_metadata)
    # torch.set_printoptions(precision=4, profile="default", sci_mode=False)
    # print("num_splits:", num_splits.shape, num_splits)
    q_nope = q[:, :, :, :512].contiguous()
    q_pe = q[:, :, :, 512:].contiguous()
    def flash_mla():
        return flash_mla_with_kvcache_q_nope_pe(
            q_nope,
            q_pe,
            blocked_k,
            block_table,
            cache_seqlens,
            dv,
            tile_scheduler_metadata,
            num_splits,
            causal=causal,
        )
        # return flash_mla_with_kvcache(
        #     q,
        #     blocked_k,
        #     block_table,
        #     cache_seqlens,
        #     dv,
        #     tile_scheduler_metadata,
        #     num_splits,
        #     causal=causal,
        # )

    def ref_mla():
        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]
            O, LSE = 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),
                h_q=h_q,
                h_kv=h_kv,
                is_causal=causal,
            )
            out[i] = O.transpose(0, 1)
            lse[i] = LSE
        return out, lse

    out_flash, lse_flash = flash_mla()
    
    out_torch, lse_torch = ref_mla()
    # print("out_flash:", out_flash)
    cal_diff(out_flash, out_torch, "out")
    cal_diff(lse_flash, lse_torch, "lse")
    print("out max_diff ", (out_flash - out_torch).abs().max())
    print("lse max_diff ", (lse_flash - lse_torch).abs().max())
    if is_prof: return
    t = triton.testing.do_bench(flash_mla)
    FLOPS = s_q * total_seqlens * h_q * (d + dv) * 2
    bytes = (total_seqlens * h_kv * d + b * s_q * h_q * d + b * s_q * h_q * dv) * (
        torch.finfo(q.dtype).bits // 8
    )
    print(
        f"{t:.3f} ms, {FLOPS / 10 ** 9 / t:.0f} TFLOPS, {bytes / 10 ** 6 / t:.0f} GB/s"
    )

zhanghj2's avatar
zhanghj2 committed
144
@torch.inference_mode()
zhanghj2's avatar
zhanghj2 committed
145
def test_flash_mla_fp8(b, s_q, mean_sk, h_q, h_kv, d, dv, causal, varlen, is_prof=False):
zhanghj2's avatar
zhanghj2 committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    print(
        f"{b=}, {s_q=}, {mean_sk=}, {h_q=}, {h_kv=}, {d=}, {dv=}, {causal=}, {varlen=}"
    )

    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()
    mean_seqlens = cache_seqlens.float().mean().int().item()
    max_seqlen = cache_seqlens.max().item()
    max_seqlen_pad = triton.cdiv(max_seqlen, 256) * 256
    print(f"{total_seqlens=}, {mean_seqlens=}, {max_seqlen=}, {max_seqlen_pad=}")

    q = torch.randn(b, s_q, h_q, d)
    # q = torch.ones(b, s_q, h_q, d)
    block_size = 64
    block_table = torch.arange(
        b * max_seqlen_pad // block_size, dtype=torch.int32
    ).view(b, max_seqlen_pad // block_size)
    
    # blocked_k = torch.randint(low=0, high=4, size = (block_table.numel(), block_size, h_kv, d), dtype = torch.int8)
    # blocked_k = torch.ones(size = (block_table.numel(), block_size, h_kv, d), dtype = torch.int8)
zhanghj2's avatar
zhanghj2 committed
169
    blocked_k = (torch.randn(block_table.numel(), block_size, h_kv, d)).to(torch.half).to(torch.float8_e4m3fn)
zhanghj2's avatar
zhanghj2 committed
170
171
172
173
174
175
176
177
    # blocked_k[0, 0, 0, 56] = 1
    # blocked_k[0, 1, 0, 8] = 2
    # blocked_k[0, 2, 0, 8] = 5
    # blocked_k[0, 3, 0, 8] = 4
    # for i in range(64):
    #     for j in range(64):
    #         blocked_k[0, i, 0, j] = j
            # blocked_k[0, i, 0, j] = (i * 50 + j) % 128
zhanghj2's avatar
zhanghj2 committed
178

zhanghj2's avatar
zhanghj2 committed
179
180
181
182
183
184
    # for i in range(b):
    #     blocked_k.view(b, max_seqlen_pad, h_kv, d)[i, cache_seqlens[i].item():] = (
    #         -128
    #     )
    blocked_v = blocked_k[..., :dv]

zhanghj2's avatar
zhanghj2 committed
185
186
187
    tile_scheduler_metadata, num_splits = get_mla_metadata(
        cache_seqlens, s_q * h_q // h_kv, h_kv
    )
zhanghj2's avatar
zhanghj2 committed
188
189
190
191
192
193
194
195
196
197

    # print("q:", q.shape, q.dtype, q)
    # print("cache_seqlens:", cache_seqlens.shape, cache_seqlens)
    # print("block_table:", block_table.shape, block_table)
    # print("blocked_k:", blocked_k.shape, blocked_k[0])
    # print("blocked_v:", blocked_v.shape)
    # torch.set_printoptions(precision=4, profile="full", sci_mode=False)
    # print("tile_scheduler_metadata:", tile_scheduler_metadata.shape, tile_scheduler_metadata)
    # torch.set_printoptions(precision=4, profile="default", sci_mode=False)
    # print("num_splits:", num_splits.shape, num_splits)
zhanghj2's avatar
zhanghj2 committed
198
    k_scale = torch.tensor(0.17).to(torch.float32).to("cuda:0")  
zhanghj2's avatar
zhanghj2 committed
199
    def flash_mla():
zhanghj2's avatar
zhanghj2 committed
200
        return flash_mla_with_kvcache(
zhanghj2's avatar
zhanghj2 committed
201
202
203
204
205
206
207
208
            q,
            blocked_k,
            block_table,
            cache_seqlens,
            dv,
            tile_scheduler_metadata,
            num_splits,
            causal=causal,
zhanghj2's avatar
zhanghj2 committed
209
210
            k_scale = k_scale,
            kv_cache_dtype = "fp8_e4m3"
zhanghj2's avatar
zhanghj2 committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
        )

    def ref_mla():
        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]
            O, LSE = 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),
                h_q=h_q,
                h_kv=h_kv,
                is_causal=causal,
                k_scale = k_scale
            )
            out[i] = O.transpose(0, 1)
            lse[i] = LSE
        return out, lse
    out_flash, lse_flash = flash_mla()
    out_torch, lse_torch = ref_mla()

zhanghj2's avatar
zhanghj2 committed
234
235
236
237
238
239
    print("out_flash ", out_flash[0, 0, 0, 0:14])
    print("out_torch ", out_torch[0, 0, 0, 0:14])
    print("lse_flash ", lse_flash[0, 0, 0:10])
    print("lse_torch ", lse_torch[0, 0, 0:10])
    cal_diff(out_flash, out_torch, "out")
    cal_diff(lse_flash, lse_torch, "lse")
zhanghj2's avatar
zhanghj2 committed
240
241
    print("out max_diff ", (out_flash - out_torch).abs().max())
    print("lse max_diff ", (lse_flash - lse_torch).abs().max())
zhanghj2's avatar
zhanghj2 committed
242

zhanghj2's avatar
zhanghj2 committed
243
244
    t = triton.testing.do_bench(flash_mla)
    FLOPS = s_q * total_seqlens * h_q * (d + dv) * 2
zhanghj2's avatar
zhanghj2 committed
245
    bytes = (total_seqlens * h_kv * d + b * s_q * h_q * d + b * s_q * h_q * dv) * (
zhanghj2's avatar
zhanghj2 committed
246
        torch.finfo(q.dtype).bits // 8
zhanghj2's avatar
zhanghj2 committed
247
    )
zhanghj2's avatar
zhanghj2 committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    print(
        f"{t:.3f} ms, {FLOPS / 10 ** 9 / t:.0f} TFLOPS, {bytes / 10 ** 6 / t:.0f} GB/s"
    )

def main(torch_dtype, is_prof=False):
    device = torch.device("cuda:0")
    torch.set_default_dtype(torch_dtype)
    torch.set_default_device(device)
    torch.cuda.set_device(device)
    torch.manual_seed(0)
    random.seed(0)
    '''
    h_kv = 1
    d, dv = 576, 512
    causal = True

    for b in [128]:
        for s in [4096, 8192]:
            for h_q in [16, 32, 64, 128]:  # TP = 8, 4, 2, 1
                for s_q in [1, 2]:  # MTP = 1, 2
                    for varlen in [False, True]:
                        test_flash_mla(b, s_q, s, h_q, h_kv, d, dv, causal, varlen)
    #                b, s_q,    s,   h_q, h_kv,   d,  dv, causal, varlen'''
    # test_flash_mla(  1,   1,  64,    16,    1, 576, 512,   True,  False, is_prof=is_prof)
    # test_flash_mla_fp8( 1,   1, 1000,     1,    1, 576, 512,   True,  False, is_prof=is_prof)
    # test_flash_mla_fp8( 1,   1, 4096,     8,    1, 576, 512,   True,  False, is_prof=is_prof)
    # test_flash_mla_fp8(32,   1, 4096,     16,    1, 576, 512,   False,  False, is_prof=is_prof)
    # '''
    h_kv = 1
    d, dv = 576, 512
    causal = True

zhanghj2's avatar
zhanghj2 committed
280
281
282
283
284
285
286
287
288
289
290
291
292
    for b in [3, 6, 9, 12, 15, 18, 21, 24]:
        for s in [111, 112, 123, 1234, 432, 4325, 4000, 8192, 11111]:
            for h_q in [16]:
                for s_q in [1]:  # MTP = 1, 2
                    for varlen in [False,True]:
                        test_flash_mla(b, s_q, s, h_q, h_kv, d, dv, causal, varlen)

    for b in [3, 6, 9, 12, 15, 18, 21, 24, 32, 64, 128, 256]:
        for s in [4000]:
            for h_q in [16]:
                for s_q in [1]:  # MTP = 1, 2
                    for varlen in [False]:
                        test_flash_mla(b, s_q, s, h_q, h_kv, d, dv, causal, varlen)
zhanghj2's avatar
zhanghj2 committed
293
294
295
296
297
    # for b in [1, 32]:
    #     for s in [200, 1002, 2002, 1024, 2000, 4000, 32768, 65536]:
    #         for h_q in [4, 16, 32, 64]:
    #             for s_q in [1, 2]:  # MTP = 1, 2
    #                 for varlen in [True]:
zhanghj2's avatar
zhanghj2 committed
298
    #                     test_flash_mla_fp8(b, s_q, s, h_q, h_kv, d, dv, causal, varlen)
zhanghj2's avatar
zhanghj2 committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                        
    # '''

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--dtype",
        type=str,
        choices=["bf16", "fp16"],
        default="bf16",
        help="Data type to use for testing (bf16 or fp16)",
    )
    parser.add_argument('--prof', default=False, action='store_true', help='prof or not')

    args = parser.parse_args()

    torch_dtype = torch.bfloat16
    if args.dtype == "fp16":
        torch_dtype = torch.float16

    main(torch_dtype, args.prof)