Repository navigation
[ASM] [HIP] gfx1250 mha bwd: hd64 kernels and hd128 border mask - #7160
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🔵 Needs a closer look
The binary-only gfx1250 kernels lack assembly sources and head-matched MI450 runtime validation needed to independently verify border-mask correctness, deterministic splitting, and performance.
No open issues 🎉
What changed in this PR
Extends gfx1250 (MI450) assembly attention backward support to head dimension 64 and documents the head-dimension-128 border-mask fix for partial key tiles.
Changes:
- Registers head-dimension-64 kernels for gradient computation, dQ conversion, and preprocessing across batch/group and deterministic variants.
- Reduces the default deterministic plane count for causal attention when capped by at least 16 key/value tiles.
- Documents supported configurations, kernel behavior, and MI450 performance results.
| File | Description |
|---|---|
op_tests/cpp/mha/README.md |
Adds gfx1250 capabilities, border-mask details, and performance measurements. |
hsa/gfx1250/fmha_v3_bwd/fmha_bwd_odo.csv |
Registers head-dimension-64 preprocessing kernels. |
hsa/gfx1250/fmha_v3_bwd/fmha_bwd_dqdkdv.csv |
Registers eight head-dimension-64 gradient variants with 256-row key/value tiles. |
hsa/gfx1250/fmha_v3_bwd/fmha_bwd_dq_convert.csv |
Registers four head-dimension-64 dQ conversion variants. |
csrc/cpp_itfs/mha_bwd.cu |
Adjusts automatic deterministic plane selection for causal attention on gfx1250. |
Review effort: Balanced
9537b54 to
f6199d2
Compare
* hd64 bwd chain for gfx1250 (odo, dqdkdv, dq_convert) in every variant the hd128 chain has: batch/group, no mask/causal, stock/deterministic. The hd64 dqdkdv covers 256 kv rows per workgroup (ts = 256). * hd128 non-causal dqdkdv rebuilt with the border mask, which sets S = -inf for the keys past seqlen_k in the last kv tile. * Every hd64 and hd128 dqdkdv object takes its dQ atomics at CU scope, like the hd192_128 ones, and the _perf.co builds are gone. * Deterministic: the default plane count is halved under a causal mask when the kv tile count caps it and is at least 16. * README: gfx1250 feature list and performance. Signed-off-by: Jim,Guo <[email protected]> Co-authored-by: Cursor <[email protected]>
f6199d2 to
6b37add
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Static manifest/export checks passed, but the gfx1250 binary border-mask and CU-scope atomic changes could not be independently validated for runtime correctness, determinism, or performance.
No open issues 🎉
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Numerical correctness and cross-CU dQ coherence depend on gfx1250 assembly and CU-scope atomics that symbol checks alone cannot validate, and no independent GPU runtime validation was available.
No open issues 🎉
Review effort: Balanced
…#7160) * hd64 bwd chain for gfx1250 (odo, dqdkdv, dq_convert) in every variant the hd128 chain has: batch/group, no mask/causal, stock/deterministic. The hd64 dqdkdv covers 256 kv rows per workgroup (ts = 256). * hd128 non-causal dqdkdv rebuilt with the border mask, which sets S = -inf for the keys past seqlen_k in the last kv tile. * Every hd64 and hd128 dqdkdv object takes its dQ atomics at CU scope, like the hd192_128 ones, and the _perf.co builds are gone. * Deterministic: the default plane count is halved under a causal mask when the kv tile count caps it and is at least 16. * README: gfx1250 feature list and performance. Signed-off-by: Jim,Guo <[email protected]> Co-authored-by: Cursor <[email protected]>
Motivation
Add head dim 64 to the gfx1250 (MI450) mha bwd asm path, and the border mask to the hd128 non-causal kernels.
Technical Details
Kernels (
hsa/gfx1250/fmha_v3_bwd)ts = 256.seqlen_kgetS = -infbeforeexp, so a row whose LSE is below about -88 can no longer turn those zero-filled keys intoinf * 0 = NaNin dQ. dq_convert and odo are unchanged._perf.coany more: the 8 hd128 ones are deleted (nothing has loaded them since [ASM] [HIP] gfx1250 mha bwd d192 d128 kernel integration #6996), and none is added for hd64.Host (
csrc/cpp_itfs/mha_bwd.cu)det_split = 0: the plane count is halved under a causal mask when the kv tile count caps it and is at least 16. A causal dqdkdv saturates at half the planes, while dq_convert reads every plane. It only fires on few-head shapes (results below); none of the shapes in the performance table hit it.Docs (
op_tests/cpp/mha/README.md): a gfx1250 section with the feature list (hdim 64, 128 and 192/128) and the performance below; the gfx1250 note in the bwd section now points to it.Test Plan
gfx1250 (MI450 engineering sample), docker
rocm/fw-bringup:gfx1250-atom-20260923, amdgpu-dkms 7.1.0.31300009. aiter built asm-only (GPU_ARCHS=gfx1250 python3 compile.py --api=bwd_v3,ENABLE_CK=0), withbenchmark_mha_bwd_v3.cpplinked asbwd.exe.Function:
bwd.exe -prec=bf16 -v=1against the CPU reference. Deterministic cases also rerun twice and compare dq/dk/dv bit for bit (-dtm_runs=2), and every case checks that the three kernels it loaded belong to its head dim.seqlen_q × seqlen_kgrid on the edges of the 32-row q tile and the 128/256-row kv tiles (seqlen_q1 to 513,seqlen_k16 to 769), with MHA/GQA/MQA and bshd/bhsd rotating across the gridseqlen_q == seqlen_k; group mode with padding (-s_pad, socu_seqlen_*next to paddedseqstart_*) and with-seqlen_ptr=1; explicitdet_split(1, 3, and 64 clamped to the kv tile count); shapes up to s=3000, where up to 24 workgroups add into every dQ rownot supportedand load nothing: fp16,-v3_atomic_fp32=0, hdim 32/48/80/96/192/256,hdim_q != hdim_vother than 192/128, group-mode top-left, window maskPerformance:
bwd.exe -prec=bf16 -iperm=1 -operm=1 -v=0 -warmup=20 -repeat=50, random inputs, median of 3.gfx942 / gfx950: the kernels and CSV rows are gfx1250 only, and the host change sits in the gfx1250 deterministic branch.
Test Result
Function on the CU-scope objects: 666/666 pass, 16 of them hdim 192/128. The same matrix on the earlier device-scope objects, with a denser seqlen grid: 833/833.
Performance, ms / TFLOPS of the whole backward (odo + dqdkdv + dq_convert), MHA,
seqlen_q == seqlen_k. TFLOPS counts2 * b * h * sq * sk * (3 * hdim_q + 2 * hdim_v), halved under the causal mask, so hdim 64 does half the FLOPs of hdim 128 on the same shape.Default
det_spliton shapes where the causal halving fires (causal, deterministic, median of 3):Submission Checklist
PR Self-Review
Reviewed revision
6b37add719cdBehavior and intent
On gfx1250 an
aiter::mha_bwdcall withhdim_q == hdim_v == 64found no asm kernel, sofmha_v3_bwdreturned -1 and the asm-only build could not run it at all. This PR adds the hd64 odo, dqdkdv and dq_convert kernels in every variant hd128 has (batch/group, no mask/causal, stock/deterministic). The hd128 non-causal dqdkdv kernels did not mask the keys pastseqlen_kin the last, partial kv tile; those keys are zero-filled, so a row whose LSE is below about -88 gotP = exp(-LSE) = inffor them and NaN in dQ. They now setS = -infthere. Every hd64/hd128 dqdkdv object now accumulates dQ with CU-scope atomics, matching the hd192_128 objects of #6996, and the_perf.cocopies nothing loads any more are deleted. In deterministic mode the default plane count is halved under a causal mask when the kv tile count caps it and is at least 16, which takes 5% to 12% off the few-head causal shapes where it fires.Validation and unverified scope
Hardware: gfx1250 (MI450 engineering sample), docker
rocm/fw-bringup:gfx1250-atom-20260923, amdgpu-dkms 7.1.0.31300009. Build:GPU_ARCHS=gfx1250 python3 compile.py --api=bwd_v3(asm-only,ENABLE_CK=0), withbenchmark_mha_bwd_v3.cpplinked asbwd.exe.bwd.exe -prec=bf16 -v3_atomic_fp32=1 -v=1against the CPU reference, deterministic cases with-deterministic=1 -dtm_runs=2(dq/dk/dv bit-identical across reruns), and each case checks the three loaded kernels belong to its head dim. Matrix: hdim 64/128 × batch/group × no mask/causal × stock/deterministic overseqlen_q1 to 513 ×seqlen_k16 to 769 with MHA/GQA/MQA and bshd/bhsd, plus-mask=twithseqlen_q == seqlen_k,-s_pad,-seqlen_ptr=1,-det_split=1|3|64, shapes up to s=3000, 16 hdim 192/128 cases, and configs that must printnot supportedwithout loading a kernel. Result on the code objects of this revision: 666/666 passed. The same matrix with a denser seqlen grid on the earlier device-scope objects: 833/833 passed.bwd.exe -prec=bf16 -iperm=1 -operm=1 -v=0 -warmup=20 -repeat=50, median of 3, in the tables above.Unverified: the performance table was measured on the device-scope objects; the CU-scope ones were only re-timed back to back on a subset (0.98x to 1.00x) and on 56 of the table's configurations (0.999x on median). The Python entry points were not run, because
can_impl_fmha_v3_bwdandcan_impl_fmha_v3_bwd_gfx950only accept gfx942 and gfx950, so on gfx1250 they never reach this asm path. hdim 192/128 got only the 16 smoke cases. gfx942/gfx950 were not run on hardware beyond CI; their kernels, CSV rows and host branches are untouched.PR-specific question
The changed kernels are reachable only through
fmha_v3_bwdincsrc/cpp_itfs/mha_bwd.cuon an arch reported asgfx1250: the CSV rows live underhsa/gfx1250/and every generated config key starts with the arch, so no other arch matches them.get_heuristic_kernelpicks a dqdkdv row only on an exact match of dtypebf16, paddedhdim_q/hdim_v(64/64, 128/128 or 192/128) withpddv == 0, mask type (0, or bottom-right 2; top-left is remapped to 2 only in batch mode withseqlen_q == seqlen_k),atomic32 == 1, batch or group mode, and deterministic or not. A head dim that pads up to 64 or 128 (32, 48, 80, 96, ...) needspddv == 1, which no gfx1250 row has. Any miss, as well as fp16,v3_atomic_fp32 = 0, bias, dbias, dropout, a window or generic mask, or a dQ_acc plane past the 32-bit range the kernels address it in, makesfmha_v3_bwdreturn -1 before a kernel is loaded or launched. Within that, anyseqlen_q/seqlen_kis supported; the hd64 grid,Tsand deterministic plane cap come from its CSV row'sts = 256. Thedet_splitchange only runs on gfx1250 withis_deterministicanddet_split == 0; other archs reject deterministic requests up front.Changed op tests
op_tests/cpp/mha/README.mdis the only changed file underop_tests/, and it is documentation rather than a test: it adds the gfx1250 section (the feature list for hdim 64, 128 and 192/128, the border-mask and deterministic behavior, and the performance table) and replaces the long gfx1250 note in the bwd section with a pointer to it. No test code changed. The existingbenchmark_mha_bwd_v3.cpp(bwd.exe) already reaches the new hd64 path through-d=64and was what the validation above ran, so no new op test is needed and nothing duplicates an existing one.