Repository navigation
Expand file tree
/
Copy pathtest_metadata.py
More file actions
421 lines (363 loc) · 15 KB
/
Copy pathtest_metadata.py
File metadata and controls
421 lines (363 loc) · 15 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
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
import argparse
import itertools
import os
import random
import pandas as pd
import torch
import aiter
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)
# ---------------------------------------------------------------------------
# Kimi-K2.5-MXFP4 decode MLA configuration (per TP rank).
# ---------------------------------------------------------------------------
KIMI_TOTAL_QO_HEADS = 64 # Kimi-K2.5 num_attention_heads
MLA_MIN_HEADS = 16 # atom _MLA_MIN_HEADS: AITER MLA kernels need >= 16 q-heads
KIMI_NHEAD_KV = 1 # MLA: a single latent KV head
# Decode metadata knobs, identical to atom's persistent worker buffers.
PAGE_SIZE = 1
KV_GRANULARITY = max(PAGE_SIZE, 16)
MAX_SEQLEN_QO = 1 # pure decode, no MTP/spec tokens
UNI_SEQLEN_QO = 1
MAX_SPLIT_PER_BATCH = 16
IS_CAUSAL = True
# Default serving sweep (kimi Makefile / perf_sweep.sh), plus the batch counts
# that only prefill reaches. num_batches is the concurrency for decode, so the
# kimi row tops out at 128 -- but a DSA prefill chunk carries one batch per query
# token, which puts --max-num-batched-tokens on this axis instead. Those large
# counts are where the parallel planner chunks its LDS scratch, and 4096 / 8192 /
# 16384 sit at one chunk, the old 8191-batch LDS cliff, and several chunks. Below
# 4096 none of that code runs, so without these rows the default sweep cannot
# tell a working chunk carry from a broken one.
DEFAULT_BATCHES = [4, 8, 16, 32, 64, 128, 4096, 8192, 16384]
DEFAULT_CTX_LENS = [2048, 4096, 8192]
_PARALLEL_ENV = "AITER_MLA_META_USE_PARALLEL"
# Lowering the planner's chunk ceiling forces the runtime-chunk kernel. A card
# whose LDS fits the full chunk always gets the folded instantiation, so this is
# the only way a gfx950 run reaches the path gfx942 takes in production.
_CHUNK_ENV = "AITER_MLA_META_BATCH_CHUNK"
# Mirrors MLA_V12_PARALLEL_BATCH_CHUNK; only ever a label in the table.
MLA_V12_DEFAULT_CHUNK = 4096
# Every card the MLA metadata planner is built and validated for.
SUPPORTED_GFX = ["gfx942", "gfx950"]
def kimi_nhead(tp: int) -> int:
"""q-heads per rank for Kimi-K2.5 at the given TP, padded to MLA_MIN_HEADS."""
assert KIMI_TOTAL_QO_HEADS % tp == 0, f"TP{tp} does not divide 64 heads evenly"
return max(KIMI_TOTAL_QO_HEADS // tp, MLA_MIN_HEADS)
def build_decode_inputs(batch_size, ctx_len, dtype, kvtype, nhead, *, jitter, seed):
"""
Build the decode-time inputs for one (batch_size, ctx_len) shape, mirroring
what atom feeds get_mla_metadata_v1:
* cu_seqlens_q : arange(batch+1) -- 1 query token per sequence.
* kv_indptr : cumulative KV page counts (page_size == 1 => 1 page/token),
so each sequence holds ``seqlen_kv`` pages.
* kv_last_page_lens : ones (page_size == 1).
With ``jitter`` the per-sequence KV length is drawn from
[ctx_len // 2, ctx_len] to emulate the spread of a real in-flight batch;
otherwise every sequence is exactly ``ctx_len`` long.
"""
if jitter:
rng = random.Random(seed)
kv_lens = [
rng.randint(max(1, ctx_len // 2), ctx_len) for _ in range(batch_size)
]
else:
kv_lens = [ctx_len] * batch_size
qo_indptr = torch.arange(batch_size + 1, dtype=torch.int32, device="cuda")
kv_indptr = torch.zeros(batch_size + 1, dtype=torch.int32, device="cuda")
kv_indptr[1:] = torch.tensor(kv_lens, dtype=torch.int32, device="cuda").cumsum(0)
kv_last_page_lens = torch.ones(batch_size, dtype=torch.int32, device="cuda")
# Output buffers, sized exactly as atom does via get_mla_metadata_info_v1.
(
(work_meta_data_size, work_meta_data_type),
(work_indptr_size, work_indptr_type),
(work_info_set_size, work_info_set_type),
(reduce_indptr_size, reduce_indptr_type),
(reduce_final_map_size, reduce_final_map_type),
(reduce_partial_map_size, reduce_partial_map_type),
) = aiter.get_mla_metadata_info_v1(
batch_size,
MAX_SEQLEN_QO,
nhead,
dtype,
kvtype,
is_sparse=False,
fast_mode=True,
)
inputs = {
"qo_indptr": qo_indptr,
"kv_indptr": kv_indptr,
"kv_last_page_lens": kv_last_page_lens,
"nhead": nhead,
}
out_meta = {
"work_meta_data": (work_meta_data_size, work_meta_data_type),
"work_indptr": (work_indptr_size, work_indptr_type),
"work_info_set": (work_info_set_size, work_info_set_type),
"reduce_indptr": (reduce_indptr_size, reduce_indptr_type),
"reduce_final_map": (reduce_final_map_size, reduce_final_map_type),
"reduce_partial_map": (reduce_partial_map_size, reduce_partial_map_type),
}
return inputs, out_meta, kv_lens
def alloc_outputs(out_meta):
return {
name: torch.empty(size, dtype=t, device="cuda")
for name, (size, t) in out_meta.items()
}
def call_metadata(inputs, outs, dtype, kvtype):
"""Run get_mla_metadata_v1 into the provided output buffers (in place)."""
aiter.get_mla_metadata_v1(
inputs["qo_indptr"],
inputs["kv_indptr"],
inputs["kv_last_page_lens"],
inputs["nhead"] // KIMI_NHEAD_KV,
KIMI_NHEAD_KV,
IS_CAUSAL,
outs["work_meta_data"],
outs["work_info_set"],
outs["work_indptr"],
outs["reduce_indptr"],
outs["reduce_final_map"],
outs["reduce_partial_map"],
page_size=PAGE_SIZE,
kv_granularity=KV_GRANULARITY,
max_seqlen_qo=MAX_SEQLEN_QO,
uni_seqlen_qo=UNI_SEQLEN_QO,
fast_mode=True,
max_split_per_batch=MAX_SPLIT_PER_BATCH,
dtype_q_nope=dtype,
dtype_kv_nope=kvtype,
)
def run_path(inputs, out_meta, dtype, kvtype, use_parallel):
"""Allocate fresh buffers, force a planner via env, and run it once."""
prev = os.environ.get(_PARALLEL_ENV)
os.environ[_PARALLEL_ENV] = "1" if use_parallel else "0"
try:
outs = alloc_outputs(out_meta)
call_metadata(inputs, outs, dtype, kvtype)
torch.cuda.synchronize()
finally:
if prev is None:
os.environ.pop(_PARALLEL_ENV, None)
else:
os.environ[_PARALLEL_ENV] = prev
return outs
def compare_metadata(golden, test):
"""
Compare the meaningful (written) regions of two metadata buffer sets.
The buffers are over-allocated (worst case), so only the populated prefixes
are deterministic; tails come from torch.empty. We derive the valid prefix
lengths from the planner's own indptr outputs:
* work_indptr is fully written (size #cu+1) and ends with the total work
count; work_info_set[:num_works] is the populated work region.
* reduce_indptr is fully written; each split qo-tile contributes a strictly
increasing step, so the number of reduce groups is the count of positive
steps and the total partial-tile count is reduce_indptr's final value.
Returns (ok, details) where details maps a field name to its mismatch count.
"""
details = {}
def exact(name, g, t, mask=None):
# Planner output is indices, not arithmetic: any difference at all is a
# divergence, so compare to the bit (rtol=atol=tol_err_ratio=0).
details[name] = checkAllclose(
g, t, rtol=0, atol=0, tol_err_ratio=0, mask=mask, printLog=False
)
wi_g = golden["work_indptr"]
wi_t = test["work_indptr"]
exact("work_indptr", wi_g, wi_t)
num_works = int(wi_g[-1].item())
exact(
"work_info_set",
golden["work_info_set"][:num_works],
test["work_info_set"][:num_works],
)
ri_g = golden["reduce_indptr"]
ri_t = test["reduce_indptr"]
exact("reduce_indptr", ri_g, ri_t)
# Valid regions for the reduce maps, derived from the golden reduce_indptr.
steps = ri_g[1:] - ri_g[:-1]
num_groups = int((steps > 0).sum().item())
num_partial = int(ri_g[-1].item())
# reduce_final_map is NOT a packed prefix. Both planners write row i only
# when tile i is split, at that tile's own index, so the written rows are
# scattered over the whole buffer. Taking the first num_groups rows compared
# memory neither planner wrote -- at batch=4096 with --jitter, 207 of those
# 218 rows, which is 414 of the elements. Since alloc_outputs() hands each
# run a fresh torch.empty buffer, that read whatever the two allocations
# happened to hold and reported a mismatch that no kernel produced. A split
# tile is exactly one whose reduce_indptr step is positive, so compare those
# rows and leave the untouched ones alone.
rfm_g = golden["reduce_final_map"].reshape(-1, 2)
rfm_t = test["reduce_final_map"].reshape(-1, 2)
written = (steps > 0)[: rfm_g.shape[0]].unsqueeze(1)
exact("reduce_final_map", rfm_g, rfm_t, mask=written)
exact(
"reduce_partial_map",
golden["reduce_partial_map"][:num_partial],
test["reduce_partial_map"][:num_partial],
)
ok = all(v == 0 for v in details.values())
return ok, details, num_works, num_groups, num_partial
@benchmark()
def test_metadata(
batch_size, ctx_len, dtype, kvtype, nhead, jitter, seed, num_iters, batch_chunk=0
):
if batch_chunk:
os.environ[_CHUNK_ENV] = str(batch_chunk)
else:
os.environ.pop(_CHUNK_ENV, None)
inputs, out_meta, _kv_lens = build_decode_inputs(
batch_size, ctx_len, dtype, kvtype, nhead, jitter=jitter, seed=seed
)
# Golden (serial planner) vs parallel planner -- must be bit identical.
golden = run_path(inputs, out_meta, dtype, kvtype, use_parallel=False)
parallel = run_path(inputs, out_meta, dtype, kvtype, use_parallel=True)
ok, mism, num_works, num_groups, num_partial = compare_metadata(golden, parallel)
if not ok:
print(f" [MISMATCH] bs={batch_size} ctx={ctx_len} nhead={nhead}: {mism}")
# The planner does no floating-point at all, so TFLOPS would be a column of
# zeros. Bytes are the metric that means something here: it reads three
# per-batch indptrs and writes the work/reduce descriptors, and the GB/s
# column against a card that does TB/s is the whole diagnosis -- this kernel
# is bound by a single-lane scan, not by traffic.
wis = golden["work_info_set"]
work_bytes = wis.element_size() * (wis[0].numel() if wis.dim() > 1 else 1)
nbytes = (
3 * (batch_size + 1) * 4 # qo_indptr, kv_indptr, kv_last_page_lens
+ num_works * work_bytes # work_info_set
+ (batch_size + 1) * 4 # reduce_indptr
+ num_groups * 2 * 4 # reduce_final_map, split tiles only
+ num_partial * 4 # reduce_partial_map, one entry per partial fragment
)
# The serial planner is both the reference and a kernel under test: it is
# what runs today whenever the parallel path bails out, so it is timed.
candidates = {"serial": "0", "parallel": "1"}
ret = {
"gfx": get_gfx(),
"chunk": batch_chunk or MLA_V12_DEFAULT_CHUNK,
"num_works": num_works,
"num_split_groups": num_groups,
}
us = {}
for name, env in candidates.items():
os.environ[_PARALLEL_ENV] = env
_, us[name] = run_perftest(
call_metadata,
inputs,
alloc_outputs(out_meta),
dtype,
kvtype,
num_iters=num_iters,
)
ret[f"{name} us"] = round(us[name], 3)
ret[f"{name} GB/s"] = round(nbytes / us[name] / 1e3, 2)
os.environ.pop(_PARALLEL_ENV, None)
# Only the parallel path can diverge; serial is the reference it is checked
# against, so its err is 0 by construction and would be a noise column.
ret["parallel err"] = max(mism.values()) if mism else 0
ret["match"] = ok
ret["speedup"] = (
round(us["serial"] / us["parallel"], 3) if us["parallel"] else float("nan")
)
return ret
def main():
if get_gfx() not in SUPPORTED_GFX:
aiter.logger.warning(
"mla metadata planner unsupported on %s; skipping", get_gfx()
)
return
parser = argparse.ArgumentParser(
description=(
"MLA metadata planner microbench/correctness test, with shapes "
"aligned to ATOM serving Kimi-K2.5-MXFP4 decode."
)
)
parser.add_argument(
"-tp",
"--tensor-parallel",
type=int,
default=8,
help="TP degree (Kimi-K2.5-MXFP4 recipe uses TP8). Sets q-heads per rank.",
)
parser.add_argument(
"--nhead",
type=int,
default=None,
help="Override q-heads per rank (default: derived from --tp).",
)
parser.add_argument(
"-b",
"--batch",
type=int,
nargs="+",
default=DEFAULT_BATCHES,
help="Batch sizes (== serving concurrency).",
)
parser.add_argument(
"-c",
"--ctx-len",
type=int,
nargs="+",
default=DEFAULT_CTX_LENS,
help="KV context lengths (== serving ISL).",
)
parser.add_argument(
"--jitter",
action="store_true",
help="Randomize per-sequence KV length in [ctx/2, ctx] (decode spread).",
)
parser.add_argument(
"--batch-chunk",
type=int,
nargs="+",
default=[0],
help="Planner chunk ceilings to sweep; 0 leaves the built-in one. A value\n"
"below it forces the runtime-chunk kernel, which is what gfx942 runs.",
)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--num-iters", type=int, default=101)
args = parser.parse_args()
nhead = args.nhead if args.nhead is not None else kimi_nhead(args.tensor_parallel)
# MXFP4 recipe: --kv_cache_dtype fp8, q quantized to fp8 to match KV.
dtype = dtypes.fp8
kvtype = dtypes.fp8
print(
f"gfx={get_gfx()} cu={torch.cuda.get_device_properties(0).multi_processor_count} "
f"| Kimi-K2.5-MXFP4 decode | nhead={nhead} nhead_kv={KIMI_NHEAD_KV} "
f"dtype={dtype} kv={kvtype} page_size={PAGE_SIZE} "
f"kv_gran={KV_GRANULARITY} max_split_per_batch={MAX_SPLIT_PER_BATCH} "
f"jitter={args.jitter}"
)
rows = []
all_match = True
for batch_chunk, ctx_len, batch_size in itertools.product(
args.batch_chunk, args.ctx_len, args.batch
):
row = test_metadata(
batch_size,
ctx_len,
dtype,
kvtype,
nhead,
args.jitter,
args.seed,
args.num_iters,
batch_chunk,
)
rows.append(row)
all_match = all_match and row["match"]
df = pd.DataFrame(rows)
aiter.logger.info(
"mla metadata planner summary (markdown):\n%s", df.to_markdown(index=False)
)
assert rows, (
"the sweep ran no shapes -- an empty axis would otherwise report a pass "
"without comparing anything"
)
assert all_match, "parallel MLA metadata planner diverged from serial reference"
if __name__ == "__main__":
main()