[feat] add flyDSL bwd FMHA kernel - #467
Open
amd-weisun wants to merge 1 commit into
Open
Conversation
amd-weisun
marked this pull request as draft
August 4, 2026 15:10
amd-weisun
force-pushed
the
ck_logic_fmha_backward_pr
branch
from
August 4, 2026 16:12
384e979 to
d21ae47
Compare
Ports CK's real trload_kr_ktr_vr backward pipeline (fmha_bwd_mfma_gfx950.py) and wires it into flydsl.BwOp as the gfx950 default, replacing the older fmha_bwd_mfma.py dqdkdv kernel on that arch. dV/dK are written per-query-head (mirroring CK's own production C++ scope) and reduced across the GQA group outside the kernel in flydsl.py, exactly matching CK's attention_backward_generic_ck_tiled.cpp wrapper. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
amd-weisun
force-pushed
the
ck_logic_fmha_backward_pr
branch
from
August 4, 2026 18:15
d21ae47 to
b448d6c
Compare
amd-weisun
marked this pull request as ready for review
August 5, 2026 14:49
|
@q10 has imported this pull request. If you are a Meta employee, you can view this in D114928768. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add FlyDSL FMHA backward kernel (gfx950 + gfx942 fallback)
Summary
Adds a FlyDSL FMHA backward implementation, registered as
flydsl.BwOp(anopt-in
AttentionBwOpBase, following the same pattern asflash.BwOp/flash3.BwOp). It is not wired intodispatch.py's live_dispatch_bw()priority list (which still uses
ck.BwOpon ROCm) — this PR adds the op andits test coverage, not a change to production routing.
Two kernels ship together:
optimized for this architecture (hardware LDS transpose, XOR-swizzled
layouts, register-resident K/V/KT, software-pipelined prefetch — see below).
fmha_bwd_mfma.py) usedwhen gfx950 isn't available.
Files changed
mslk/attention/flydsl/fmha_bwd_mfma_gfx950.pymslk/attention/flydsl/fmha_bwd_mfma.pymslk/attention/flydsl/fmha_bwd_preprocess.pymslk/attention/flydsl/fmha_bwd_convert_dq.pymslk/attention/fmha/flydsl.pyflydsl.BwOp: gfx950 routes to the new kernel withck_scope_dvdk=True; gfx942 falls back tofmha_bwd_mfma.pymslk/attention/fmha/__init__.pyflydsl.BwOpinALL_BW_OPSon ROCm (test enumeration only)test/attention/fmha/test_backward.pyflydsl.BwOpto test_backward + test_backward_gqa; 3 new negative-path testsTest plan
test_backward.py -k flydsl: 316 passed, 0 failedtest_backward_gqa(flydsl.BwOp): 2 passed, 2 skipped (bf16 precision skip, matching CK's own)compile_fmha_bwd_dqdkdv_mfma_gfx950,127 passed / 2 skipped across all supported D/dtype/causal/GQA/varlen/deterministic/
packed-qkv combinations)
Performance: FlyDSL gfx950 FMHA Backward vs Production CK
Device-side kernel time measured via
rocprofv3 --kernel-trace(true GPU dispatchtimestamps, no host overhead). CK baseline is the current MSLK production commit.
FlyDSL kernel uses
ck_scope_dvdk=Truefor GQA shapes (dV/dK written per-query-head, reducedoutside the kernel — matches CK's own production C++ wrapper).
Config: B=1, D=128, bf16, atomic-add dQ, gfx950 (MI350X)
GQA (H=64, Hkv=8, heads_per_kv=8)
MHA (H=8, Hkv=8, heads_per_kv=1)
FmhaBwdDQDKDVKerneldevice time (the kernel directly comparableto FlyDSL's fused dQ+dV+dK kernel)
3-kernel-vs-3-kernel comparison (including preprocess and convert), see the
"Kernel breakdown" section below.
End-to-end wall-clock:
flydsl.BwOpvsck.BwOp(backward only)Wall-clock time calling
BwOp.apply()directly — the real MSLK dispatch path,including all host-side overhead. Both sides launch 3 GPU kernels for the main
GQA/MHA path: FlyDSL's preprocess (D_vec) + fused dQ+dV+dK + convert-dq kernels
vs CK's OGradDotO + FmhaBwdDQDKDVKernel + ConvertDQ. The GQA
.unflatten().sum()reduce for dK/dV remains a PyTorch op on both sides (CK's C++ wrapper does the
same reduce outside its kernels). Forward via
ck.FwOp(shared, not timed).JIT compilation cost is measured separately (first call only); steady-state
numbers reflect the cached kernel path.
Config: B=1, D=128, bf16, gfx950 (MI350X), N_WARMUP=3, N_ITER=20
GQA (H=64, Hkv=8, heads_per_kv=8)
MHA (H=8, Hkv=8, heads_per_kv=1)
Summary
across all seqlens (1K–16K), winning at every single shape. Best result: 4K causal
(2.20x).
~7–11% slower at 1K only (low grid occupancy with H=8, fewer blocks to fill 256 CUs).
FlyDSL dispatches its own FlyDSL preprocess (D_vec) + fused dQ+dV+dK + convert-dq
kernels. The GQA
.unflatten().sum()reduce for dK/dV is a PyTorch op on both sides(CK's C++ wrapper does the same reduce outside its kernels).
Kernel breakdown (device-side, GQA H=64/Hkv=8)
Per-kernel device time via
rocprofv3 --kernel-trace. Both sides launch 3 kernels:GQA (H=64, Hkv=8)
Observations:
multi-row-per-block kernel (16 rows/block for D=128) outperforms CK's OGradDotO.
(except 1K non-causal at 0.98x, within noise).