forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mla_decode_pagesize64.py
More file actions
728 lines (659 loc) · 22.9 KB
/
Copy pathtest_mla_decode_pagesize64.py
File metadata and controls
728 lines (659 loc) · 22.9 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
# gfx1250 / mi400 MLA fp8 decode test.
#
# # Single public dispatch case:
# python3 op_tests/test_mla_decode_pagesize64.py -n 8,1
#
# # Sweep all supported public dispatch cases:
# python3 op_tests/test_mla_decode_pagesize64.py
#
# # Peak-performance sweep from the gfx1250 MLA report:
# python3 op_tests/test_mla_decode_pagesize64.py -n 8,1 8,2 16,1 32,1 -b 1024 -c 16384 --split_kv auto
import argparse
import itertools
import os
from pathlib import Path
import pandas as pd
import torch
import aiter
import aiter.mla
from aiter import dtypes
from aiter.jit.utils.chip_info import get_gfx
from aiter.test_common import benchmark, checkAllclose, run_perftest
torch.set_default_device("cuda")
torch.set_printoptions(sci_mode=False)
SUPPORTED_GFX = ["gfx1250"]
def check_support(dtype, kv_dtype, nhead):
return dtype == dtypes.fp8 and kv_dtype == dtypes.fp8
# Public dispatch cases covered by this UT. The aiter MLA dispatcher maps each
# (Gqa, qSeqLen) pair to the fixed registered gfx1250 kernel internally.
_MI400_DISPATCH_CASES = [
(8, 1),
(8, 2),
(8, 3),
(8, 4),
(16, 1),
(16, 2),
(16, 4),
(32, 1),
(64, 1),
(128, 1),
]
def _pack_rope_split3_q_pages(tensor, nope_dim, rope_dim, padded_stride_bytes=768):
shape = tensor.shape
assert shape[-1] == nope_dim + rope_dim
elem_size = tensor.element_size()
if padded_stride_bytes % elem_size != 0:
raise ValueError("rope_split3 padded stride must be element aligned")
padded_dim = padded_stride_bytes // elem_size
if padded_dim < shape[-1]:
raise ValueError(
f"rope_split3 padded dim {padded_dim} is smaller than Q dim {shape[-1]}"
)
# Mirror poc_kl pack_q_page1_padded(): each logical Q row stores
# [nope][rope] followed by zero padding up to a 768-byte row stride.
rows = tensor.reshape(-1, shape[-1])
padded = torch.zeros(
(rows.shape[0], padded_dim),
dtype=tensor.dtype,
device=tensor.device,
)
padded[:, : shape[-1]].copy_(rows)
return torch.as_strided(
padded,
size=shape,
stride=(
shape[1] * shape[2] * padded_dim,
shape[2] * padded_dim,
padded_dim,
1,
),
)
def _pack_rope_split2_kv_pages(tensor, nope_dim, rope_dim):
pages, page_size, nhead_kv, head_dim = tensor.shape
assert nhead_kv == 1
assert head_dim == nope_dim + rope_dim
packed = torch.cat(
(
tensor[..., :nope_dim].reshape(pages, page_size * nope_dim),
tensor[..., nope_dim:].reshape(pages, page_size * rope_dim),
),
dim=-1,
)
return packed.reshape(pages, page_size, nhead_kv, head_dim).contiguous()
def _make_page_permutation(num_pages, *, shuffle):
if not shuffle:
return list(range(num_pages))
if num_pages <= 1:
return list(range(num_pages))
for step in (7, 5, 3):
if num_pages % step != 0:
return [(i * step + 1) % num_pages for i in range(num_pages)]
return list(reversed(range(num_pages)))
def _make_scales(batch, device, *, enabled):
if not enabled:
return (
torch.ones((1,), dtype=torch.float32, device=device),
torch.ones((1,), dtype=torch.float32, device=device),
)
q_scale = torch.linspace(0.75, 1.25, 1, dtype=torch.float32, device=device)
kv_scale = torch.linspace(1.20, 0.80, 1, dtype=torch.float32, device=device)
return q_scale, kv_scale
def _make_mla_mi400_case(
*,
batch,
ctx_lens,
nhead,
decode_qlen,
num_kv_splits,
page_indices_oob=0,
use_non_unit_scales=True,
):
repo_hsa_dir = Path(__file__).resolve().parents[1] / "hsa"
os.environ["AITER_ASM_DIR"] = str(repo_hsa_dir)
device = torch.device("cuda")
page_size = 64
num_pages_per_batch = (ctx_lens + page_size - 1) // page_size
if num_kv_splits is None:
# Mirror mla_decode_fwd(num_kv_splits=None): resolve the auto split count
# and its indptr through the shared meta-param heuristic so the case
# carries a concrete value for the shape checks and the kernel args.
num_kv_splits, num_kv_splits_indptr = aiter.mla.get_meta_param(
None,
batch,
batch * num_pages_per_batch,
nhead,
decode_qlen,
dtypes.fp8,
)
num_kv_splits = int(num_kv_splits)
else:
assert num_kv_splits > 0
num_kv_splits_indptr = (
torch.arange(batch + 1, dtype=torch.int32, device=device) * num_kv_splits
)
torch.manual_seed(
20260513
+ batch * 1009
+ ctx_lens
+ nhead * 7
+ decode_qlen
+ num_kv_splits * 101
)
last_page_len = ctx_lens % page_size or page_size
kv_last_page_lens = torch.full(
(batch,), last_page_len, dtype=torch.int32, device=device
)
# gfx1250/mi400 stage1 asm kernel consumes a PAGE-level kv_indptr directly
# (it walks the page-level kv_indices block table). Build it here as the
# per-batch prefix sum of page counts so mla.py no longer needs to convert a
# token-level kv_indptr. With uniform ctx_lens this is [0, npb, 2*npb, ...].
kv_indptr = torch.zeros(batch + 1, dtype=torch.int32, device=device)
kv_indptr[1:] = torch.cumsum(
torch.full((batch,), num_pages_per_batch, dtype=torch.int32, device=device),
dim=0,
)
q_scale, kv_scale = _make_scales(batch, device, enabled=use_non_unit_scales)
return {
"page_size": page_size,
"num_kv_splits": num_kv_splits,
"num_pages_per_batch": num_pages_per_batch,
"kv_last_page_lens": kv_last_page_lens,
"kv_indptr": kv_indptr,
"num_kv_splits_indptr": num_kv_splits_indptr,
"q_scale": q_scale,
"kv_scale": kv_scale,
}
def _make_mla_mi400_kv_case(
*,
kv_buffer_bf16,
batch,
ctx_lens,
qk_head_dim,
v_head_dim,
page_indices_oob,
fallback_fill_value=None,
shuffle_pages=True,
):
"""Build the KV inputs for the gfx1250 seg asm decode (qk_head_dim=576 =
nope 512 + rope 64).
Returns (kv_buffer, kv_buffer_ref, kv_indices):
kv_buffer : fp8 (float8_e4m3fn), aiter PAGE-level seg-pack, shape
[num_pages, page_size, 1, 576] holding
[page_size*512 (nope) | page_size*64 (pe)] per page
(page_size=64). This is what mla.mla_decode_fwd consumes.
Built by _pack_rope_split2_kv_pages.
kv_buffer_ref : fp8 (float8_e4m3fn), TOKEN-major scattered cache
[num_pages, page_size, 1, 576] (pages placed at their
physical ids); consumed only by the PyTorch fp32 reference.
kv_indices : int32 PAGE-level block table [batch*(npb+oob)] of physical
page ids (compact, OOB padding appended after valid pages).
"""
device = torch.device("cuda")
page_size = 64
nhead_kv = 1
num_pages_per_batch = (ctx_lens + page_size - 1) // page_size
total_page_indices = batch * (num_pages_per_batch + page_indices_oob)
total_pages = batch * num_pages_per_batch
kv_buffer_source_bf16 = kv_buffer_bf16.view(-1, page_size, nhead_kv, qk_head_dim)
available_pages = kv_buffer_source_bf16.size(0)
if available_pages >= total_pages:
kv_buffer_logical_bf16 = kv_buffer_source_bf16[:total_pages].contiguous()
else:
kv_buffer_logical_bf16 = torch.empty(
(total_pages, page_size, nhead_kv, qk_head_dim),
dtype=kv_buffer_source_bf16.dtype,
device=kv_buffer_source_bf16.device,
)
kv_buffer_logical_bf16[:available_pages] = kv_buffer_source_bf16
fallback_shape = (
total_pages - available_pages,
page_size,
nhead_kv,
qk_head_dim,
)
if fallback_fill_value is None:
kv_buffer_logical_bf16[available_pages:] = torch.randn(
fallback_shape,
dtype=kv_buffer_source_bf16.dtype,
device=kv_buffer_source_bf16.device,
)
else:
kv_buffer_logical_bf16[available_pages:] = torch.full(
fallback_shape,
fallback_fill_value,
dtype=kv_buffer_source_bf16.dtype,
device=kv_buffer_source_bf16.device,
)
# Poison the unused tail of every batch's last (partially filled) page with
# NaN. When ctx_lens % page_size != 0 the final logical page of each batch
# keeps only last_page_len valid tokens; slots [last_page_len:page_size] are
# never valid KV. The kernel must honor kv_last_page_lens / kv_indptr and
# never read past them, so a correct kernel still yields a finite, matching
# output. The PyTorch reference excludes this tail via kv[:ctx_lens].
last_page_len = ctx_lens % page_size or page_size
if last_page_len != page_size:
last_logical_pages = [(b + 1) * num_pages_per_batch - 1 for b in range(batch)]
kv_buffer_logical_bf16[last_logical_pages, last_page_len:] = float("nan")
# The kernel consumes a compact block table, with OOB padding only after all
# valid pages. KV pages are scattered into their physical page ids.
shuffled_page_indices = _make_page_permutation(total_pages, shuffle=shuffle_pages)
kv_buffer_scattered_bf16 = torch.empty_like(kv_buffer_logical_bf16)
kv_indices = torch.zeros(total_page_indices, dtype=torch.int32, device=device)
for logical_page, physical_page in enumerate(shuffled_page_indices):
kv_buffer_scattered_bf16[physical_page] = kv_buffer_logical_bf16[logical_page]
kv_indices[logical_page] = physical_page
kv_buffer_ref = kv_buffer_scattered_bf16.to(dtypes.fp8)
kv_buffer = _pack_rope_split2_kv_pages(
kv_buffer_ref.view(total_pages, page_size, nhead_kv, qk_head_dim),
v_head_dim,
qk_head_dim - v_head_dim,
)
return kv_buffer, kv_buffer_ref, kv_indices
def _make_mla_mi400_q_case(
*, q_fp8, batch, decode_qlen, nhead, qk_head_dim, v_head_dim
):
"""Build the Q input for the gfx1250 seg asm decode.
Returns q: fp8 (float8_e4m3fn), shape [total_q, nhead, 576], NON-contiguous
768-padded selected layout -- per-head row stride = 768 elems (=768 B in
fp8), i.e. each head's 576 values ([nope 512][rope 64]) followed by 192 B of
zero padding (_MLA_Q_OUT_PADDED_DIM). Built by _pack_rope_split3_q_pages +
as_strided. (The PyTorch fp32 reference instead reads the unpadded q_fp8
directly.)
"""
q = q_fp8.view(batch, decode_qlen, nhead, qk_head_dim)
q = _pack_rope_split3_q_pages(
q,
v_head_dim,
qk_head_dim - v_head_dim,
)
return torch.as_strided(
q,
size=(batch * decode_qlen, nhead, qk_head_dim),
stride=(nhead * q.stride(2), q.stride(2), q.stride(3)),
)
def _apply_causal_mask_(logits):
# Matches the causal/tail mask shape used by the reference attention.
_, s_q, s_k = logits.shape
mask = torch.ones(s_q, s_k, dtype=torch.bool, device=logits.device).tril(
diagonal=s_k - s_q
)
logits.masked_fill_(mask.logical_not().unsqueeze(0), float("-inf"))
def _ref_mla_mi400(
case,
q_ref,
kv_buffer_ref,
kv_indices,
batch_size,
ctx_lens,
decode_qlen,
nhead_kv,
qk_head_dim,
v_head_dim,
mask,
):
"""PyTorch fp32 analytic reference (qk_head_dim=576 = nope 512 + rope 64).
Inputs it reads (both UNPACKED relative to the aiter kernel layouts; both
fp8 then upcast to fp32 here so the r eference carries no extra quant error):
q_ref : fp8 (float8_e4m3fn), CONTIGUOUS [total_q, nhead, 576]
(the plain q_fp8, NOT the 768-padded selected layout the
asm kernel consumes). Upcast via .float() * q_scale.
kv_buffer_ref : fp8 (float8_e4m3fn), TOKEN-major scattered cache
[num_pages, page_size, 1, 576] (pages at physical ids, NOT
the seg-packed layout). Gathered per batch by physical
page id (kv_indices), upcast via .float() * kv_scale, then
reshaped to [ctx_lens, 1, 576]; key=full 576, value=[:512].
Output: bf16 [total_q, nhead, 512] (softmax(QK^T/sqrt(576))·V, causal mask).
"""
outputs = []
num_pages = case["num_pages_per_batch"]
kv_source = kv_buffer_ref
for b in range(batch_size):
q_start = b * decode_qlen
q_end = q_start + decode_qlen
q_scale = case["q_scale"][0 if case["q_scale"].numel() == 1 else b]
kv_scale = case["kv_scale"][0 if case["kv_scale"].numel() == 1 else b]
q = q_ref[q_start:q_end].float() * q_scale
page_indices = kv_indices[b * num_pages : (b + 1) * num_pages].long()
kv = torch.index_select(kv_source.float(), 0, page_indices) * kv_scale
kv = kv.reshape(-1, nhead_kv, qk_head_dim)
kv = kv[:ctx_lens]
key = kv
value = kv[..., :v_head_dim]
logits = torch.einsum("qhd,kmd->hqk", q, key) * (1.0 / (qk_head_dim**0.5))
if mask:
_apply_causal_mask_(logits)
weights = torch.softmax(logits, dim=-1)
outputs.append(torch.einsum("hqk,kmd->qhd", weights, value).to(torch.bfloat16))
return torch.cat(outputs, dim=0)
def _cosine_diff(actual, expected):
actual = actual.detach().float().cpu()
expected = expected.detach().float().cpu()
assert torch.isfinite(actual).all()
assert torch.isfinite(expected).all()
numerator = 2 * (actual.double() * expected.double()).sum()
denominator = (
(actual.double().square() + expected.double().square()).sum().clamp_min(1e-12)
)
return (1 - (numerator / denominator)).item()
@benchmark()
def test_mla(
batch,
ctx_len,
nhead,
decode_qlen,
split_kv,
mask,
dtype,
kv_dtype,
init,
):
page_size = 64
kv_lora_rank = 512
qk_rope_head_dim = 64
qk_head_dim = kv_lora_rank + qk_rope_head_dim
nhead_kv = 1
v_head_dim = kv_lora_rank
page_indices_oob = 4
kv_max_sz = 65536 * 32 # Remaining framework KV capacity after weights.
num_page = (kv_max_sz + page_size - 1) // page_size
input_fill_value = 0.25 if init == "const0.25" else None
if input_fill_value is None:
kv_buffer = torch.randn(
(num_page * page_size, 1, qk_head_dim),
dtype=torch.bfloat16,
)
else:
kv_buffer = torch.full(
(num_page * page_size, 1, qk_head_dim),
input_fill_value,
dtype=torch.bfloat16,
)
qo_indptr = torch.zeros(batch + 1, dtype=torch.int)
seq_lens_qo = torch.full((batch,), decode_qlen, dtype=torch.int)
qo_indptr[1 : batch + 1] = torch.cumsum(seq_lens_qo, dim=0)
total_q = qo_indptr[-1].item()
if input_fill_value is None:
q = torch.randn((total_q, nhead, qk_head_dim), dtype=torch.bfloat16)
else:
q = torch.full(
(total_q, nhead, qk_head_dim),
input_fill_value,
dtype=torch.bfloat16,
)
kv_buffer_mi400, kv_buffer_ref_mi400, kv_indices_mi400 = _make_mla_mi400_kv_case(
kv_buffer_bf16=kv_buffer,
batch=batch,
ctx_lens=ctx_len,
qk_head_dim=qk_head_dim,
v_head_dim=v_head_dim,
page_indices_oob=page_indices_oob,
fallback_fill_value=input_fill_value,
)
q_fp8_mi400 = q.to(dtypes.fp8)
q_mi400 = _make_mla_mi400_q_case(
q_fp8=q_fp8_mi400,
batch=batch,
decode_qlen=decode_qlen,
nhead=nhead,
qk_head_dim=qk_head_dim,
v_head_dim=v_head_dim,
)
case = _make_mla_mi400_case(
batch=batch,
ctx_lens=ctx_len,
nhead=nhead,
decode_qlen=decode_qlen,
num_kv_splits=split_kv,
page_indices_oob=page_indices_oob,
)
def run_mla_decode(out_tensor):
return aiter.mla.mla_decode_fwd(
q_mi400,
kv_buffer_mi400,
out_tensor,
qo_indptr,
case["kv_indptr"],
kv_indices_mi400,
case["kv_last_page_lens"],
decode_qlen,
case["page_size"],
nhead_kv,
1.0 / (qk_head_dim**0.5),
num_kv_splits=case["num_kv_splits"],
num_kv_splits_indptr=case["num_kv_splits_indptr"],
q_scale=case["q_scale"],
kv_scale=case["kv_scale"],
return_lse=True,
)
out = torch.zeros((batch * decode_qlen, nhead, v_head_dim), dtype=torch.bfloat16)
total_kv = batch * ctx_len
flops = decode_qlen * total_kv * nhead * (qk_head_dim + v_head_dim) * 2
nbytes = (
total_kv * nhead_kv * qk_head_dim * (torch.finfo(dtypes.fp8).bits // 8)
+ total_q * nhead * qk_head_dim * (torch.finfo(dtypes.fp8).bits // 8)
+ total_q * nhead * v_head_dim * (torch.finfo(torch.bfloat16).bits // 8)
)
attn, us = run_perftest(run_mla_decode, out)
attn_logits, attn_lse = attn
out_check = out.clone()
logits_shape = (batch * decode_qlen, case["num_kv_splits"], nhead, v_head_dim)
if case["num_kv_splits"] == 1:
logits_shape = (batch * decode_qlen, nhead, v_head_dim)
assert out_check.shape == (batch * decode_qlen, nhead, v_head_dim)
assert attn_logits.shape == logits_shape
assert attn_lse.shape == (batch * decode_qlen, nhead)
final_out_finite = torch.isfinite(out_check.detach().float().cpu()).all().item()
if final_out_finite:
ref = _ref_mla_mi400(
case,
q_fp8_mi400,
kv_buffer_ref_mi400,
kv_indices_mi400,
batch,
ctx_len,
decode_qlen,
nhead_kv,
qk_head_dim,
v_head_dim,
mask,
)
err = checkAllclose(
ref.to(dtypes.fp32),
out_check.to(dtypes.fp32),
rtol=6e-2,
atol=6e-2,
tol_err_ratio=0.05,
msg="mi400: mla_decode_mi400",
)
cos_diff = _cosine_diff(out_check, ref)
else:
err = float("inf")
cos_diff = float("inf")
ret = {
"gfx": get_gfx(),
"num_kv_splits": case["num_kv_splits"],
"init": init,
"mi400 us": us,
"mi400 TFLOPS": flops / us / 1e6,
"mi400 TB/s": nbytes / us / 1e6,
"mi400 err": err,
"mi400 cos_diff": cos_diff,
"mi400 final_out_finite": final_out_finite,
}
return ret
def _str2split(value):
if isinstance(value, str) and value.lower() == "auto":
return None
return int(value)
def _format_summary(rows):
df = pd.DataFrame(rows)
if "split_kv" in df:
df = df.drop(columns=["split_kv"])
init_order = {init: idx for idx, init in enumerate(["randn", "const0.25"])}
df["_init_order"] = df["init"].map(init_order).fillna(len(init_order))
sort_columns = [
"_init_order",
"batch",
"ctx_len",
"nhead",
"decode_qlen",
"mask",
"num_kv_splits",
]
df = df.sort_values(sort_columns).drop(columns=["_init_order"])
columns = [
"batch",
"ctx_len",
"nhead",
"decode_qlen",
"mask",
"num_kv_splits",
"dtype",
"kv_dtype",
"gfx",
"init",
"mi400 us",
"mi400 TFLOPS",
"mi400 TB/s",
"mi400 err",
"mi400 cos_diff",
"mi400 final_out_finite",
]
return df[[column for column in columns if column in df.columns]]
def main():
if get_gfx() not in SUPPORTED_GFX:
aiter.logger.warning("test_mla_mi400 unsupported on %s; skipping", get_gfx())
return
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="config input of test",
)
parser.add_argument(
"-d",
"--dtype",
type=dtypes.str2Dtype,
choices=[dtypes.fp8],
nargs="*",
default=[dtypes.fp8],
metavar="{fp8}",
help="""Q dtype. MI400 MLA currently supports fp8.
e.g.: -d fp8""",
)
parser.add_argument(
"--kv-dtype",
type=dtypes.str2Dtype,
choices=[dtypes.fp8],
nargs="*",
default=[dtypes.fp8],
metavar="{fp8}",
help="""KV dtype. MI400 MLA currently supports fp8.
e.g.: --kv-dtype fp8""",
)
parser.add_argument(
"-b",
"--batch",
type=int,
nargs="*",
default=[1, 2, 4],
help="""Batch size.
e.g.: -b 1 2 4""",
)
parser.add_argument(
"-c",
"--ctxLen",
type=int,
nargs="*",
default=[17, 65, 128, 1024],
help="""Context length.
e.g.: -c 17""",
)
parser.add_argument(
"-n",
"--nhead",
type=dtypes.str2tuple,
choices=_MI400_DISPATCH_CASES,
nargs="*",
default=_MI400_DISPATCH_CASES,
help="""Public MI400 dispatch case as GQA,decode_qlen.
e.g.: -n 8,3 128,1""",
)
parser.add_argument(
"--split-kv",
"--split_kv",
type=_str2split,
nargs="*",
default=[1, 2, 3],
help="""KV split count per batch, or auto.
e.g.: --split_kv 1 2 3 auto""",
)
parser.add_argument(
"--mask",
type=int,
nargs="*",
choices=[0, 1],
default=[1],
help="""Attention mask selector: 0 disables causal/tail mask, 1 enables it.
e.g.: --mask 0 1""",
)
parser.add_argument(
"--init",
choices=["randn", "const0.25"],
nargs="*",
default=["randn"],
help="""Input initializer. const0.25 fills Q/KV/fallback pages with 0.25.
e.g.: --init randn const0.25""",
)
args = parser.parse_args()
rows = []
for (
(nhead, decode_qlen),
dtype,
kv_dtype,
batch,
ctx_len,
split_kv,
mask,
init,
) in itertools.product(
args.nhead,
args.dtype,
args.kv_dtype,
args.batch,
args.ctxLen,
args.split_kv,
args.mask,
args.init,
):
if not check_support(dtype, kv_dtype, nhead):
aiter.logger.warning(
"skipping unsupported MLA config: dtype=%s kv_dtype=%s nhead=%d",
dtype,
kv_dtype,
nhead,
)
continue
rows.append(
test_mla(
batch,
ctx_len,
nhead,
decode_qlen,
split_kv,
mask,
dtype,
kv_dtype,
init,
)
)
if not rows:
aiter.logger.warning("mla_decode_pagesize64: no supported cases selected")
return
df = _format_summary(rows)
aiter.logger.info(
"mla_decode_pagesize64 summary (markdown):\n%s",
df.to_markdown(index=False),
)
if __name__ == "__main__":
main()