Repository navigation
Add OPUS gfx950 bf16 fmha d192x128 kernel - #4205
Conversation
…(D_QK=192/D_V=128) Integrate the OPUS asymmetric-head-dim (Q/K=192, V/O=128) GQA/MHA forward kernel for gfx950, supporting batch + group/varlen, causal + non-causal, cross-attention, arbitrary seqlen, and GQA/MHA. - Generalize the former D=128-only opus entry point into a shared `fmha_fwd_bf16_opus_fwd` that dispatches by head dim to the symmetric D=128 kernel (batch only, logic unchanged) or the new D_QK=192/D_V=128 kernel (batch + group). Rename module/pybind/symbol accordingly. - Add the D=192/V=128 kernel-only headers (defs/traits/kernel), ported from the standalone gcnasm source; the shared TU compiles both kernels. - mha.py: split the public wrapper into batch (`fmha_fwd_bf16_opus_fwd`) and varlen (`fmha_fwd_bf16_opus_varlen_fwd`) entry points over one low-level stub; wire both into `_flash_attn_forward` / `_flash_attn_varlen_forward`. D=192 is enabled by default (AITER_DISABLE_FMHA_OPUS=1 to opt out); D=128 stays env-gated (AITER_ENABLE_FMHA_OPUS=1). - Add batch + group op tests for the D=192/V=128 path. Inference-only (no LSE/dropout/bias/alibi/swa/sink/quant/paged). Validated against fp32 reference (50 passed) and benchmarked vs fmha_v3 on MI355: batch ~+3%, group ~+34% average.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
There was a problem hiding this comment.
Pull request overview
This PR integrates a new OPUS gfx950 bf16 FMHA forward kernel for asymmetric head dims (D_QK=192, D_V=128), and refactors the previous D=128 OPUS entry point into a single shared dispatching module/wrapper that supports both (128,128) and (192,128) (including a new varlen/group Python entry point for (192,128)).
Changes:
- Adds the gfx950 OPUS (192,128) forward kernel headers and a shared C++/pybind entry point that dispatches by head dims.
- Updates Python dispatch/wrappers in
aiter/ops/mha.pyto expose dense + varlen wrappers and route_flash_attn_forward/_flash_attn_varlen_forwardto OPUS where eligible. - Adds OP tests for dense + group/varlen coverage of the new (192,128) path and updates the existing D=128 test to use the shared wrapper.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
op_tests/test_mha.py |
Adds dense and group/varlen tests to validate routing and numerical correctness for OPUS (192,128); updates D=128 routing check to the new wrapper. |
csrc/pybind/fmha_fwd_bf16_opus_pybind.cu |
Renames/retargets the pybind module to the new shared OPUS forward symbol. |
csrc/py_itfs_cu/fmha_fwd_hd128_bf16_opus_kernels.cu |
Removes the old D=128-only host TU (superseded by shared TU). |
csrc/py_itfs_cu/fmha_fwd_bf16_opus_kernels.cu |
New shared host launcher TU: dispatches (128,128) vs (192,128); adds group/varlen launch plumbing for (192,128). |
csrc/include/rocm_ops.hpp |
Updates pybind macro to bind the new shared OPUS forward API with optional varlen args. |
csrc/include/fmha_fwd_hd192_v128_bf16_opus.h |
New kernel-only header declaring the (192,128) device kernel template under IMPL guard. |
csrc/include/fmha_fwd_hd192_v128_bf16_opus_defs.h |
New shared defs/traits/kargs/constants for the (192,128) kernel. |
csrc/include/fmha_fwd_hd192_v128_bf16_opus_kernel.hpp |
New OPUS kernel implementation for (192,128) on gfx950. |
csrc/include/fmha_fwd_hd128_bf16_opus.h |
Re-scopes the D=128 header to “kernel-only” (public API moved to shared header). |
csrc/include/fmha_fwd_hd128_bf16_opus_defs.h |
Adds a guard to avoid duplicate ceil_div definition across headers. |
csrc/include/fmha_fwd_bf16_opus.h |
New shared public C++ API header for OPUS bf16 fmha forward (dense + optional varlen args). |
aiter/ops/mha.py |
Introduces shared OPUS stub/wrappers (dense + varlen), updates dense/varlen dispatch gating to route to OPUS for (192,128) by default (with env rollback). |
aiter/jit/optCompilerConfig.json |
Renames the JIT module entry to compile the new shared TU + pybind module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Register a gen_fake (meta) implementation for the shared OPUS bf16 forward custom op so it is torch.compile / export / cudagraph safe. The kernel writes `out` in place and returns nothing (out is pre-allocated by the wrapper), so the fake simply returns None; without it the guard would fall back to running the real kernel under FakeTensorMode. - Consolidate the dense (batch) eligibility check into a single can_impl_fmha_fwd_bf16_opus(): shared inference-only conditions are evaluated first (cheapest / most selective, e.g. gfx950 + bf16), and the per-head-dim helpers (D=128 env-gated, D=192 default-on) are only checked once those hold.
The online-softmax running-max update took the wave-wide `all_below` (ballot) decision but then applied the per-lane new-tile max unconditionally. On the !all_below path, a lane whose own tile max is BELOW the running max `m_row` computed rescale = exp2(m_row - row_max) with a large positive argument, which overflows to inf → O/l become inf → NaN (it also wrongly lowered m_row). Clamp per lane: row_max = all_below ? m_row : max(m_row, row_max). This keeps the running max monotonic so rescale = exp2(m_row - max(...)) <= 1 (no overflow) and matches the correct online-softmax semantics. Applied to both the D_QK=192/D_V=128 and the symmetric D=128 kernels. Only manifests in the multi-tile pipeline (seqlen > one KV tile) at large logits (|logit| ~> 86 causal / ~194 non-causal); normal-magnitude inputs are unaffected. Verified: NaN/Inf eliminated up to |logit| ~1378, outputs within bf16 tolerance, and no performance change (batch ~+3%, group ~+30%+ vs v3 unchanged).
3f1d460 to
6473c12
Compare
… seqstart - Plumb the caller's softmax_scale into the D_QK=192/D_V=128 kernel via a new kargs.softmax_scale field (after stride_v_h). The kernel previously hardcoded 1/sqrt(192)*log2(e) and silently ignored a non-default scale, producing wrong results; it now derives temperature_scale from kargs.softmax_scale. - Validate group/varlen inputs in the shared launcher before use: packed q/k/v/out head dims, and each seqstart_* (incl. *_pad) dtype==int32 / 1-D / contiguous / length==num_groups+1 — before reinterpreting data_ptr() as const int*. - Normalize seqstart_* to int32 + contiguous in the varlen Python wrapper so callers passing int64 / non-contiguous cumulative lengths work. - black-format the opus test additions in op_tests/test_mha.py.
|
@shengnxu in VLLM_ENGINE_READY_TIMEOUT_S=1800 \
HIP_VISIBLE_DEVICES="0,1,2,3,4,5,6,7" \
VLLM_ROCM_USE_AITER=1 \
VLLM_ROCM_USE_AITER_MOE=1 \
VLLM_ROCM_USE_AITER_FUSION_SHARED_EXPERTS=0 \
VLLM_DISABLE_SHARED_EXPERTS_STREAM=1 \
vllm serve amd/Kimi-K2.6-MXFP4 \
--tensor-parallel-size 8 \
--trust-remote-code \
--port 8004 \
--max-cudagraph-capture-size 128and crashes due in This is caused by: The code that selects it is unchanged in substance from the container: Line 2895 in fc2e5d5 Line 2923 in fc2e5d5 Line 442 in fc2e5d5 aiter/csrc/include/rocm_ops.hpp Line 1503 in fc2e5d5 torch_to_aiter_pybind creates module_aiter_core.aiter_tensor_t, but module_fmha_fwd_bf16_opus.so rejects those instances despite advertising the same type. Repro: that crashes with
|
|
cc @shay-li77 |
Root cause
|
Motivation
DeepSeek V3 / V3.2 MLA prefill decompresses its latent KV into per-head K(192) / V(128), i.e. a standard causal MHA with an asymmetric head dim (D_QK=192, D_V=128). The default fmha_v3 backend leaves performance on the table for this shape, especially in the varlen (group) path used by flash_attn_varlen_func. This PR integrates the gfx950 OPUS asymmetric-head-dim flash-attention forward kernel into aiter and routes the (192,128) bf16 case to it, giving sizable speedups on the DeepSeek prefill shapes while falling back to fmha_v3/CK everywhere else.
Technical Details
fmha_fwd_bf16_opus_fwd(modulemodule_fmha_fwd_bf16_opus) that dispatches by head dim in C++: the symmetric D=128 kernel (batch only, logic unchanged) or the new D_QK=192/D_V=128 kernel (batch + group/varlen). Both kernels are compiled in one translation unit.fmha_fwd_hd192_v128_bf16_opus_{defs.h,kernel.hpp,.h}(ported from the standalone OPUS source), plus the shared host launcherfmha_fwd_bf16_opus_kernels.cuthat builds kargs, computes the grid + the causal head/tail-mergeoptbit, and launches the right (causal × group) instantiation.fmha_fwd_bf16_opus_fwd(batch, dense BSHD) andfmha_fwd_bf16_opus_varlen_fwd(packed THD) over one low-level@compile_opsstub; wired into_flash_attn_forward/_flash_attn_varlen_forward. D=192 is enabled by default (AITER_DISABLE_FMHA_OPUS=1to opt out); D=128 stays env-gated (AITER_ENABLE_FMHA_OPUS=1). Agen_fakemeta impl makes the op torch.compile / export / cudagraph safe.!all_belowpath the per-lane new-tile max was applied unconditionally, so a lane whose tile max was below the running max computedexp2(m_row - row_max)with a large positive argument →inf→NaN. Clamped per lane:row_max = all_below ? m_row : max(m_row, row_max), keeping the running max monotonic. Applied to the D=192 and D=128 kernels. (Manifests only in the multi-tile pipeline at large logits;fmha_v3and normal-magnitude inputs were unaffected.)softmax_scaleinto the D=192 kernel via a newkargs.softmax_scalefield (was hardcoded to1/sqrt(192)and silently ignored); explicit group-mode input validation (packed head dims +seqstart_*dtype=int32 / 1-D / contiguous / length); normalizeseqstart_*to int32+contiguous in the varlen wrapper.Test Plan
pytest op_tests/test_mha.py -k opus— batch (flash_attn_funcdefault routing) and group/varlen (direct wrapper) correctness vs the fp32attention_ref, across partial/odd/large seqlen and MHA/GQA/MQA.fmha_v3viaaiter.test_common.run_perfteston DeepSeek prefill shapes (nheads ∈ {16,32,64,128}, seqlen×batch=16384, causal, bf16).-Rpass-analysis=kernel-resource-usageto confirmVGPRs Spill: 0/Occupancy 2.Test Result
pytest -k opus→ 50 passed. Group-mode perf shapes (28 combos) all consistent within bf16 tolerance, no NaN/Inf.--verifyon the standalone harness: GPU KERNEL VALID (67M elements).|logit| ~1378(previously failed at|logit| ~86causal /~194non-causal); outputs within bf16 tolerance;fmha_v3was always finite.softmax_scaleplumbing adds 0 VGPR.Performance Summary
One line: across
nheads ∈ {16, 32, 64, 128}, the OPUS D_QK=192/D_V=128 forward beats the defaultfmha_v3backend by +25% to +47% (group mode) and +3% to +4% (batch mode) on average.Toolchain: ROCm 7.2.2, HIP 7.2.53211-671d39a71e,
AMD clang 22.0.0git (roc-7.2.2,
f58b06dce1f9c15707c5f808fd002e18c2accf7e).Per-nheads average gain over
fmha_v3(group mode first, then batch mode):Detailed tables
nheads=16 (DeepSeek V3/R1, TP8) — group mode (causal)
nheads=16 (DeepSeek V3/R1, TP8) — batch mode (causal)
nheads=32 (DeepSeek V3.2 DSA, TP4) — group mode (causal)
nheads=32 (DeepSeek V3.2 DSA, TP4) — batch mode (causal)
nheads=64 (TP2, intermediate) — group mode (causal)
nheads=64 (TP2, intermediate) — batch mode (causal)
nheads=128 (DP attention / TBO) — group mode (causal)
nheads=128 (DP attention / TBO) — batch mode (causal)
Submission Checklist