forked from ROCm/aiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fmha_fwd_with_sink_asm.py
More file actions
363 lines (314 loc) · 12.5 KB
/
Copy pathtest_fmha_fwd_with_sink_asm.py
File metadata and controls
363 lines (314 loc) · 12.5 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
# SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
"""Correctness + performance tests for fmha_fwd_with_sink_asm (BF16 ASM, gfx1250).
Public API: aiter.flash_attn_func (the path the model calls)
Ops layer: aiter.fmha_fwd_with_sink_asm (low-level)
Built to the aiter op-test standard (see .claude/skills/aiter-op-test): mirror
test_quant.py — @benchmark + run_perftest candidate loop, a torch reference,
per-candidate us / TFLOPS / TB/s / err, a markdown summary table per test
function, and a __main__ guard so the module is importable.
Sink convention
---------------
`sink` ([q_head_num] fp32) is a per-Q-head logit in the SAME scaled domain as
Q·K^T * softmax_scale; it acts as a zero-value virtual KV column and is passed
to the kernel verbatim (no host-side scaling). D64 kernels read it; D128
kernels ignore it (pass None).
Layout: the API only accepts bshd shape ([b, s, h, d]). To exercise the
kernel's stride handling for sbhd / bhsd memory, qkv are allocated in the
chosen `layout` and `permute()`d to a bshd-shaped non-contiguous view.
"""
import argparse
import itertools
import math
import pandas as pd
import torch
import aiter
from aiter import dtypes
from aiter.jit.utils.chip_info import get_gfx_runtime as get_gfx
from aiter.test_common import benchmark, checkAllclose, run_perftest
torch.set_default_device("cuda")
# Every card these ASM kernels are built/validated for. The .co files only
# ship for gfx1250 (hsa/gfx1250/fmha_fwd_bf16/*.co); on any other arch the
# kernel launch raises 'no kernel for arch=...'.
SUPPORTED_GFX = ["gfx1250"]
# ---------------------------------------------------------------------------
# Reference (fp32 math, cast back). Not timed, not in the table.
# ---------------------------------------------------------------------------
def run_torch(q, k, v, *, is_causal, sink=None):
"""bshd-in / bshd-out attention reference, sink optional.
scores = (Q·K^T) * scale, scale = 1/sqrt(d); lse returned in fp32.
sink (optional): [hq] fp32 per-head logit in the scaled domain (a zero-value
KV column appended to the scaled scores).
"""
b, sq, hq, d = q.shape
_, sk, hk, _ = k.shape
if hq != hk:
k = k.repeat_interleave(hq // hk, dim=2)
v = v.repeat_interleave(hq // hk, dim=2)
qf, kf, vf = q.float(), k.float(), v.float()
scale = 1.0 / math.sqrt(d)
scores = torch.einsum("bshd,bkhd->bhsk", qf, kf) * scale
if is_causal:
m = torch.triu(
torch.ones(sq, sk, dtype=torch.bool, device=q.device), sk - sq + 1
)
scores = scores.masked_fill(m, float("-inf"))
max_attn, _ = scores.max(dim=-1)
if sink is not None:
sink_bhs = sink.float()[None, :, None].expand(b, hq, sq)
max_total = torch.maximum(max_attn, sink_bhs)
else:
max_total = max_attn
denom = torch.exp(scores - max_total.unsqueeze(-1)).sum(dim=-1)
if sink is not None:
denom = denom + torch.exp(sink_bhs - max_total)
probs = torch.exp(scores - max_total.unsqueeze(-1)) / denom.unsqueeze(-1)
out = torch.einsum("bhsk,bkhd->bshd", probs, vf).to(q.dtype)
lse = torch.log(denom) + max_total
return out, lse
# ---------------------------------------------------------------------------
# Input helpers
# ---------------------------------------------------------------------------
def make_qkv_bshd(layout, sq, sk, batch, hq, hk, d, init="randn", dtype=dtypes.bf16):
"""Allocate (q, k, v) in `layout` memory, return bshd-shaped views.
layout: 0 = bshd (contiguous), 1 = bhsd view, 2 = sbhd view. The kernel
reads strides directly so non-contiguous bshd views (layout 1/2) are valid.
init: "randn" (random normal) or "const0.25" (every element = 0.25).
"""
if layout == 0:
q = torch.randn(batch, sq, hq, d, dtype=dtype)
k = torch.randn(batch, sk, hk, d, dtype=dtype)
v = torch.randn(batch, sk, hk, d, dtype=dtype)
elif layout == 1:
q = torch.randn(batch, hq, sq, d, dtype=dtype).permute(0, 2, 1, 3)
k = torch.randn(batch, hk, sk, d, dtype=dtype).permute(0, 2, 1, 3)
v = torch.randn(batch, hk, sk, d, dtype=dtype).permute(0, 2, 1, 3)
elif layout == 2:
q = torch.randn(sq, batch, hq, d, dtype=dtype).permute(1, 0, 2, 3)
k = torch.randn(sk, batch, hk, d, dtype=dtype).permute(1, 0, 2, 3)
v = torch.randn(sk, batch, hk, d, dtype=dtype).permute(1, 0, 2, 3)
else:
raise ValueError(f"unsupported layout={layout}")
if init == "const0.25":
# In-place fill is layout-agnostic (works on non-contiguous views).
q.fill_(0.25)
k.fill_(0.25)
v.fill_(0.25)
elif init != "randn":
raise ValueError(f"unknown init pattern: {init!r}")
return q, k, v
def _d64_sink(hq):
"""Per-head sink logits in [0.5, 2.0] (scaled domain), varied across heads."""
return torch.linspace(0.5, 2.0, hq, dtype=dtypes.fp32)
def run_kernel(q, k, v, *, scale, is_causal, sink=None, via="public"):
"""via="public" → aiter.flash_attn_func (the model path); "ops" → low-level."""
if via == "public":
r = aiter.flash_attn_func(
q,
k,
v,
softmax_scale=scale,
causal=is_causal,
return_lse=True,
sink_ptr=sink,
)
return r[0], r[1]
if via == "ops":
return aiter.fmha_fwd_with_sink_asm(q, k, v, scale, is_causal, True, sink=sink)
raise ValueError(f"unknown via={via!r}")
def _flops_bytes(batch, hq, hk, sq, sk, d, is_causal, esz):
"""Attention roofline numerators: 2 GEMMs (QK^T, PV), HBM traffic q+k+v+o."""
flops = 4.0 * batch * hq * sq * sk * d # 2*(2*M*N*K) over the two matmuls
if is_causal:
flops /= 2.0
nbytes = (
2 * batch * sq * hq * d # q read + o write
+ 2 * batch * sk * hk * d # k + v read
) * esz
return flops, nbytes
# ---------------------------------------------------------------------------
# Shape tables
# ---------------------------------------------------------------------------
# Correctness shapes (torch reference is feasible here). hq=64; hk=8 for D64
# and hk=4 for D128 (GQA ratios 8 / 16). Non-causal (mask=0) kernels require
# sk % 256 == 0 — non-aligned sk rows are causal-only (filtered in the sweep).
# (head_dim, hq, hk, sq, sk, batch)
_CORRECTNESS_SHAPES = [
(64, 64, 8, 128, 2048, 1), # D64 aligned
(64, 64, 8, 128, 2048, 2),
(64, 64, 8, 130, 2048, 1), # D64 q-unaligned (sq not mult of 128)
(64, 64, 8, 128, 2300, 1), # D64 kv-unaligned (sk not mult of 256) -> causal
(128, 64, 4, 128, 2048, 1), # D128 aligned
(128, 64, 4, 128, 2048, 2),
(128, 64, 4, 130, 2048, 1), # D128 q-unaligned
(128, 64, 4, 128, 2300, 1), # D128 kv-unaligned -> causal
(64, 64, 8, 8192, 8192, 1), # D64 perf-sized, aligned
(128, 64, 4, 4096, 4096, 1), # D128 perf-sized, aligned
]
# Perf-only shapes; sq == sk. hq=64, hk=8 (D64) / 4 (D128), batch=1, sbhd.
# The torch reference is O(s^2) in memory (e.g. 32768 -> ~256 GB of scores) so
# these are timed but NOT correctness-checked here (use _CORRECTNESS_SHAPES).
# (head_dim, seqlen)
_PERF_SHAPES = [
(64, 1024),
(64, 4096),
(64, 8192),
(64, 16384),
(64, 32768),
(128, 1024),
(128, 2048),
(128, 4096),
(128, 8192),
(128, 16384),
]
# ---------------------------------------------------------------------------
# Test functions (one markdown table each). @benchmark logs the call args as
# the table's left columns and merges the returned metric dict.
# ---------------------------------------------------------------------------
@benchmark()
def test_fmha_fwd_with_sink_asm(
head_dim, hq, hk, sq, sk, batch, is_causal, layout, init
):
torch.manual_seed(0)
q, k, v = make_qkv_bshd(layout, sq, sk, batch, hq, hk, head_dim, init=init)
scale = 1.0 / math.sqrt(head_dim)
# D64 -> non-zero sink (exercises ENABLE_SINK); D128 -> kernel ignores it.
sink = _d64_sink(hq) if head_dim == 64 else None
ref_out, ref_lse = run_torch(q, k, v, is_causal=is_causal, sink=sink)
flops, nbytes = _flops_bytes(
batch, hq, hk, sq, sk, head_dim, is_causal, q.element_size()
)
# The model calls the public dispatcher (flash_attn_func) → asm path.
candidates = {
"asm": lambda: run_kernel(
q, k, v, scale=scale, is_causal=is_causal, sink=sink, via="public"
)
}
ret = {"gfx": get_gfx()}
for name, fn in candidates.items():
(out, lse), us = run_perftest(fn)
ret[f"{name} us"] = us
ret[f"{name} TFLOPS"] = flops / us / 1e6
ret[f"{name} TB/s"] = nbytes / us / 1e6
ret[f"{name} err(O)"] = checkAllclose(
ref_out.to(dtypes.fp32),
out.to(dtypes.fp32),
rtol=1e-2,
atol=1e-2,
msg=f"{name} O d={head_dim} c={is_causal}",
)
ret[f"{name} err(LSE)"] = checkAllclose(
ref_lse.to(dtypes.fp32),
lse.to(dtypes.fp32),
rtol=1e-2,
atol=1e-2,
msg=f"{name} LSE d={head_dim} c={is_causal}",
)
return ret
@benchmark()
def test_fmha_fwd_with_sink_asm_perf(head_dim, hq, hk, sq, sk, batch, is_causal, init):
torch.manual_seed(0)
q, k, v = make_qkv_bshd(2, sq, sk, batch, hq, hk, head_dim, init=init)
scale = 1.0 / math.sqrt(head_dim)
sink = _d64_sink(hq) if head_dim == 64 else None
flops, nbytes = _flops_bytes(
batch, hq, hk, sq, sk, head_dim, is_causal, q.element_size()
)
candidates = {
"asm": lambda: run_kernel(
q, k, v, scale=scale, is_causal=is_causal, sink=sink, via="public"
)
}
ret = {"gfx": get_gfx()}
for name, fn in candidates.items():
_, us = run_perftest(fn)
ret[f"{name} us"] = us
ret[f"{name} TFLOPS"] = flops / us / 1e6
ret[f"{name} TB/s"] = nbytes / us / 1e6
return ret
def main():
if get_gfx() not in SUPPORTED_GFX:
aiter.logger.warning(
"fmha_fwd_with_sink_asm unsupported on %s; skipping", get_gfx()
)
return
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="config input of test",
)
parser.add_argument(
"-d",
"--head_dim",
type=int,
nargs="*",
choices=[64, 128],
default=[64, 128],
help="head dim(s) to test (default: 64 128)",
)
parser.add_argument(
"-c",
"--causal",
type=int,
nargs="*",
choices=[0, 1],
default=[0, 1],
help="causal mode(s): 0=non-causal 1=causal (default: 0 1)",
)
parser.add_argument(
"-l",
"--layout",
type=int,
nargs="*",
choices=[0, 1, 2],
default=[2],
help="memory layout(s): 0=bshd 1=bhsd 2=sbhd (default: 2)",
)
parser.add_argument(
"--init",
type=str,
nargs="*",
choices=["randn", "const0.25"],
default=["randn", "const0.25"],
help="q/k/v init pattern(s) (default: randn const0.25)",
)
args = parser.parse_args()
causal_modes = [bool(c) for c in args.causal]
# ---- correctness + perf table ----
df = []
for head_dim, hq, hk, sq, sk, batch in _CORRECTNESS_SHAPES:
if head_dim not in args.head_dim:
continue
for is_causal, layout, init in itertools.product(
causal_modes, args.layout, args.init
):
if not is_causal and sk % 256 != 0: # mask=0 kernel needs sk%256==0
continue
df.append(
test_fmha_fwd_with_sink_asm(
head_dim, hq, hk, sq, sk, batch, is_causal, layout, init
)
)
df = pd.DataFrame(df)
aiter.logger.info(
"fmha_fwd_with_sink_asm correctness summary (markdown):\n%s",
df.to_markdown(index=False),
)
# ---- perf-only table (large shapes; ref infeasible) ----
df = []
for head_dim, seqlen in _PERF_SHAPES:
if head_dim not in args.head_dim:
continue
hk = 8 if head_dim == 64 else 4
for is_causal, init in itertools.product(causal_modes, args.init):
df.append(
test_fmha_fwd_with_sink_asm_perf(
head_dim, 64, hk, seqlen, seqlen, 1, is_causal, init
)
)
df = pd.DataFrame(df)
aiter.logger.info(
"fmha_fwd_with_sink_asm perf summary (markdown):\n%s",
df.to_markdown(index=False),
)
if __name__ == "__main__":
main()