Repository navigation
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…h few splits per tile (cherry picked from commit 81995fc)
978c4b4 to
81995fc
Compare
The decode planner cuts a multi-token query of up to 64 heads into tiles of several rows. In decode those come few, with many splits each, while num_reduce_tile (the table size) is several times the live tile count, so the wave count check passes anyway; one wave walking rows and splits back to back is then slower than kn_mla_reduce_v1 (64 heads x 4-token draft block, batch 8/32: 10.3-20.3 vs 7.5-13.9 us). Take the new kernel for single-row tiles only: more than 64 heads, or max_seqlen_q 1. (cherry picked from commit 5cc0d00)
Multi-row tiles (the decode planner cuts a multi-token query of up to 64 heads into tiles of 2-4 rows) now take the wave-per-head kernel too, replacing the single-row condition of the previous commit: - grid.y spreads a tile's rows over row parts (the planner's rows per tile, at most 4), so each wave owns one (tile, row, head group); - each wave keeps a ring of split loads in flight: split i + kInFlight is requested as soon as split i is accumulated, and the first kInFlight splits' rows before the weights are reduced; - two instances: one-row tiles keep 2 heads per wave with 2 splits in flight; multi-row tiles take 1 head per wave with 4 splits in flight and at most 256 tiles along grid.x, the workgroups walking the rest and stopping at the table's end (an empty graph-batch-sized table otherwise launched 7-10 us of empty waves); - the wave-count check counts row parts. On a 64-head, 4-token draft-block decode (MI455X) the reduce of its 2-row tiles drops 12-50% against kn_mla_reduce_v1 (batch 16-64) and up to 14% at batch 8. op_tests/test_mla_reduce.py gains two 2-row layout cases (64 heads), one past the 256-tile grid with an empty padded tail. (cherry picked from commit c53d322)
A decode batch that mixes very different context lengths gets most of the planner's split budget on its longest request: that request's tiles carry 15-60 splits while the others carry 2-6. kn_mla_reduce_v1_wave_head has one wave walk every split of its heads, so the most-split tile sets the kernel time (about 3.2 us + 0.59 us a split on gfx1250), and above 32 splits it falls back to one split at a time. kn_mla_reduce_v1_adaptive keeps the wave-head walk for tiles with few splits and adds helper workgroups, first in the grid. Each helper stages the live part of reduce_indptr in LDS, prefix-sums the work of the long tiles (more than 12 splits) and takes a contiguous block of it: 2 waves a head up to 24 splits, 4 above, partial sums added in LDS; more than 32 splits are walked 32 at a time. There is no cross-workgroup synchronization. It is taken where kn_mla_reduce_v1_wave_head would be, with a final map, for tables of at most 64 sequences (4 helpers a CU up to 32 sequences, 2 up to 64); larger tables keep kn_mla_reduce_v1_wave_head, whose tiles carry 2-5 splits there. AITER_MLA_REDUCE_ADAPTIVE=0 keeps the wave-head kernel, =1 also takes larger tables; AITER_MLA_REDUCE_ADAPTIVE_HELPERS and AITER_MLA_REDUCE_ADAPTIVE_SPLITS override the helper count and the 12 / 24 split thresholds. op_tests/test_mla_reduce_adaptive.py checks every live tile (output and LSE) against a torch reference on synthetic tables (1-100 splits, 1 / 2 / 4-row tiles, 16-128 heads, padded tails) and on planner tables for verify and draft-block shapes.
c53d322 to
deb5b6c
Compare
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
… VGPR pins (#8736 @ 13b071f, head 2026-11-26T06:02:59Z) (#8743) Port of #8736's commits d9926d2 and 13b071f onto k3-455-dev; its first commit (#8181 + #7913) is already here. Conflicts: - mla_pagesize1_fp8_fp8.py: #8736's file. It carries #7913 @ 51bb7c6 (k3-455-dev had 067fee3), the only difference in that file. - mla_reduce_kernels.cuh: k3-455-dev's file (keeps #7876 and #7914) plus #8736's row-parallel reducer and its dispatch, checked first as in #8736. Co-authored-by: xiangM99 <[email protected]>
… VGPR pins (#8736 @ 13b071f, head 2026-11-26T06:02:59Z) (#8743) Port of #8736's commits d9926d2 and 13b071f onto k3-455-dev; its first commit (#8181 + #7913) is already here. Conflicts: - mla_pagesize1_fp8_fp8.py: #8736's file. It carries #7913 @ 51bb7c6 (k3-455-dev had 067fee3), the only difference in that file. - mla_reduce_kernels.cuh: k3-455-dev's file (keeps #7876 and #7914) plus #8736's row-parallel reducer and its dispatch, checked first as in #8736. Co-authored-by: xiangM99 <[email protected]> (cherry picked from commit 5b18892)
Summary
Adds
kn_mla_reduce_v1_wave_head, an MLA split-KV reduce for wave32 GPUs (gfx1250) with 512-wide heads, for decode and verify steps whose reduce tiles carry few splits each. On MI455X:The host picks it only on wave32 when there is enough parallelism; everything else keeps
kn_mla_reduce_v1, andAITER_MLA_REDUCE_WAVE_HEAD=0turns it off.The fourth commit adds
kn_mla_reduce_v1_adaptivefor batches with very different context lengths, where one long request's tiles carry 15–60 splits and the wave-head kernel walks them in one wave. Helper workgroups share those splits; on MI455X the reduce of such a batch gets 1.3–2.9x faster (e.g. 32.5 → 11.4 µs for a tile with 47 splits), and 18–22 µs draft-block reduces drop to ~10 µs. Tables of more than 64 sequences are unchanged.Motivation
On gfx1250,
kn_mla_reduce_v1[_ps]gives each (tile, head) one 2-wave workgroup, stages the split map in LDS behind__syncthreads, and walks the splits one after another (2 KB per split per workgroup). Little data is in flight, so the reduce is latency-bound. On MI455X it moves 5–12 TB/s and takes about as long as on MI355X, despite MI455X's much higher HBM bandwidth.For MLA decode with 96 heads (DSV3-style MLA, TP8 + DCP8, 4-draft-token verify), the planner's auto split gives 40–192 reduce tiles of 2–7 splits each. At short contexts the reduce was up to 47% of the MLA call.
kn_mla_reduce_v1_wide(#7570) targets the opposite shape (few tiles, at least 8 splits each,heads x rows <= 128). On gfx1250 it is currently never selected (mla_reduce_use_widecomparesdev_prop.warpSizewith akWaveSizethat is 64 in the host pass); #7914 fixes that separately. The two do not compete: the wide check runs first, the 96-head shapes below have 96 heads x 5 rows or fewer than 8 splits, and the 64-head draft-block tables are far past the wide kernel's wave32 size bound in #7914.Changes
kn_mla_reduce_v1_wave_head(csrc/include/mla_reduce_kernels.cuh). One wave ownskHeadsPerWaveadjacent heads of one row of a tile; their partial rows are adjacent in memory.lreads float4 numberc * 32 + lof each head's 512-float row, so every load instruction covers 512 contiguous bytes. The first version gave each lane 16 contiguous floats; one instruction then touched 16 cache lines, a quarter of each, and was 10–30% slower.iholds spliti's slot and LSE. The weights come from a wave max/sum.kInFlightsplit loads in flight. The firstkInFlightsplits' rows are requested before the weights are known, and spliti + kInFlightas soon as splitiis accumulated.max_seqlen_q == 1): 2 heads per wave, 2 splits in flight, one workgroup row per table entry. 4 in flight was up to 1 µs better at batch 8–16 of a 96-head decode, but as much worse at batch 32–64.ceil(q / ceil(heads * q / 128))rows, e.g. 64 heads x 4 tokens → two 2-row tiles):grid.yspreads a tile's rows over that many row parts (at most 4), and 1 head per wave with 4 splits in flight keeps more loads going per CU.kn_mla_reduce_v1here.grid.x; workgroups walk the rest and stop at the first tile that starts at the table's end (reduce_indptrdoes not decrease). Launching every entry of an empty, graph-batch-sized table cost 7–10 µs more thankn_mla_reduce_v1(8.4–12.6 against 1.2–2.4 µs).pwalks rowsp,p + parts, …mla_reduce_use_wave_head), after the wide-kernel check, forkSizeDV == 512only. Requires all of:warpSize == 321 <= max_seqlen_q <= 161 <= num_kv_splits <= 32. Past 32 splits a wave would walk them one at a time; that path is kept for correctness, but not selected.num_reduce_tile * waves_per_tile * row_parts >= 6 * num_cu. Below that, each wave has too much serial work andkn_mla_reduce_v1is as fast or faster (grids below).AITER_MLA_REDUCE_WAVE_HEAD=0keepskn_mla_reduce_v1;=1drops the parallelism condition.kn_mla_reduce_v1.-1split (v1_0 tile) is skipped.op_tests/test_mla_reduce.py: 6LAYOUT_CASESwith few splits per tile (≤ 9).-1tile, a paddedreduce_indptrtail, and tiles with more rows thanmax_seqlen_q.max_seqlen_q4): 64 tiles, and 300 tiles plus a 40-entry empty tail, which goes past the 256-tile grid.kn_mla_reduce_v1_adaptive(fourth commit).kn_mla_reduce_v1_wave_headhas one wave walk every split of its heads, so the most-split tile sets the kernel time (about 3.2 µs + 0.59 µs a split), and past 32 splits it walks them one at a time.reduce_indptrin LDS, prefix-sums the work of the long tiles (more than 12 splits) and takes a contiguous block of it. A long tile's head gets 2 waves up to 24 splits, 4 above; their partial sums are added in LDS. More than 32 splits are walked 32 at a time. The short tiles keep the wave-head walk. There is no cross-workgroup synchronization (a merge through device-scope atomics / fences was measured: it writes back and invalidates L2 that holds the stage-1 partials and was slower).kn_mla_reduce_v1_wave_headwould be, with a final map (persistent decode metadata), for tables of at most 64 sequences (4 helpers a CU up to 32 sequences, 2 up to 64). Larger tables keepkn_mla_reduce_v1_wave_head: their tiles carry 2–5 splits, so there is nothing to share, and the adaptive kernel's wave-head grid compiles to more VGPRs.AITER_MLA_REDUCE_ADAPTIVE=0keepskn_mla_reduce_v1_wave_headeverywhere,=1also takes the larger tables;AITER_MLA_REDUCE_ADAPTIVE_HELPERS=<n>sets the helper workgroups,AITER_MLA_REDUCE_ADAPTIVE_SPLITS=<s1>,<s2>the short / mid split counts (default12,24, measured on gfx1250 over decode batches of 8–128 sequences).AITER_MLA_REDUCE_WAVE_HEAD=0still keepskn_mla_reduce_v1for everything.op_tests/test_mla_reduce_adaptive.pychecks every live tile (output and LSE) against a torch reference: synthetic tables with 1–100 splits per tile (chunked past 32), 1 / 2 / 4-row tiles, 16–128 heads, padded table tails, LSE on / off; and planner tables (get_mla_metadata_v1, persistent fast mode, page 1) for verify (96 heads, 5 rows, causal, round-robin context parallel) and draft blocks (96 / 64 heads, 4 rows, non-causal), skewed and uniform. The 64-head draft-block planner cases need the 32/64-head multi-token decode route ([HIP] [FlyDSL] [gfx1250] MLA PS1: take 32/64-head multi-token decode as several qo tiles #7913) and are skipped without it.Performance (if applicable)
Reduce kernel only (measured with the first commit, one-row instance with 1 split in flight; the current 2-in-flight one-row instance is equal or up to 1 µs faster on these shapes), one MI455X (gfx1250, 256 CUs), L2/MALL flushed before every call, torch profiler kernel time. The inputs are the real partials of
mla_decode_fwd, captured right before the reduce: 96 heads x 512, fp8 MLA decode with persistent page-1 metadata as ATOM builds it (max_split_per_batch=-1), a 4-draft-token verify (5 query rows) for batch 8–128 and a 1-token decode for batch 384. Context is per DCP rank (DCP 8).Whole
mla_decode_fwdcall (stage 1 + reduce), CUDA graph replay, caches flushed, median of 100, with this PR's final revision againstAITER_MLA_REDUCE_WAVE_HEAD=0on the same GPU. Context lengths are per request before DCP. The sweep covers 4k–256k and 8 batch points. The new kernel is faster in 39 of 56 cells. The others are the batch 256/512 rows, whose reduce tables are empty (last row, re-measured after the final change), and three long-context cells within 1%. Selected rows:The last row is a known cost. At batch 256/512 the planner leaves no tile to reduce, but the CUDA-graph-sized table still has 256/512 entries. The one-row instance launches a workgroup per entry and head group, and every one exits after two scalar loads. A grid-stride variant that launched fewer workgroups made the busy shapes slower (3.4 → 5.0 µs at batch 8), so the one-row instance keeps one workgroup per entry. The multi-row instance, whose waves per entry are 4x as many, caps the grid instead (Changes).
Synthetic grid from
op_tests/test_mla_reduce.py(bf16, uniform splits, one row per tile), forced on (=1) vskn_mla_reduce_v1(=0). It sets the routing threshold,waves = tiles x heads / 2against6 x 256 CUs = 1536:Multi-row tiles on real decode metadata: 64 heads, a 4-token non-causal draft block (two 2-row tiles per request), fp8, page-1 persistent metadata as ATOM builds it; reduce kernel time,
kn_mla_reduce_v1(=0) → this PR (default routing):The batch-128 row is the empty-table cost of the capped multi-row grid: workgroups for the first 256 entries still launch and exit. Whole calls there are 22.6 → 24.4, 148.7 → 149.1 and 567.2 → 571.7 µs.
Synthetic grid from
op_tests/test_mla_reduce.py's builder (bf16, uniform splits, the table holds only live tiles), forced on (=1) vskn_mla_reduce_v1(=0):kn_mla_reduce_v1;kn_mla_reduce_v1kn_mla_reduce_v1also has an LDS race on such tiles once a workgroup reduces more than one row on its massive path (fix: #7912): in the=0runs of this grid onmainmany multi-row shapes give wrong output. The new kernel uses no LDS and matches the torch reference on all 60.Fourth commit (adaptive reduce). Reduce kernel only,
main03f9f37 + this PR, one MI455X: real planner tables (get_mla_metadata_v1, fp8, page 1, persistent fast mode, 8-way round-robin context parallel, the shard of one rank), tables sized for the batch's graph size; each CUDA-graph replay first rewrites the live partials (as stage 1 leaves them, warm in L2/MALL), kernel time from the profiler; every live tile checked against torch. 96 heads; verify = 5 rows causal, draft = 4-row non-causal block. Columns: this commit (default),AITER_MLA_REDUCE_ADAPTIVE=0(wave-head, the first three commits),AITER_MLA_REDUCE_WAVE_HEAD=0(kn_mla_reduce_v1).kn_mla_reduce_v1The last row is the known cost: when every tile is short, the helpers find no work, and the adaptive kernel's wave-head grid compiles to more VGPRs (lower occupancy) than
kn_mla_reduce_v1_wave_head: +1.1–1.2 µs at batch 8–16 with uniform contexts. Forcing the occupancy back made it spill and run 3–13 µs slower. Real mixed-length batches always have long tiles and are faster.Testing
LAYOUT_CASES)MI250X/MI300X/MI355X are wave64. There the routing never selects the new kernel and the kernel body is compiled out.
module_mla_reducewas built for gfx950 + gfx1250 but not run on gfx9.Documentation
The kernel is selected automatically.
AITER_MLA_REDUCE_WAVE_HEADis documented next to the routing code, asAITER_MLA_REDUCE_WIDEis.Dependencies
Breaking Changes
None. Output agrees with
kn_mla_reduce_v1to bf16 rounding (max |Δout| 2e-3, max |Δlse| 1e-6 on every row of the real inputs above). The all-zero-weight convention (0 / +inf) is the same.PR Self-Review
Reviewed revision
deb5b6c (on
main03f9f37). Four commits: the kernel (identical to the earlier 978c4b4); a single-row condition; row parts, a ring of split loads, and the multi-row instance, which replace that condition; then the adaptive reduce for skewed batches. The first three are c53d322 rebased ontomain03f9f37 without conflicts (mla_reduce_kernels.cuhandtest_mla_reduce.pyare unchanged onmainsince #7570).Behavior and intent
On wave32 GPUs with 512-wide heads, decode/verify reduces with enough (tile, row, head) parallelism and ≤ 32 splits per tile now run
kn_mla_reduce_v1_wave_headinstead ofkn_mla_reduce_v1. The old kernel is latency-bound there (see Motivation). The new one coalesces its loads, keeps 2–4 split loads in flight per wave, and spreads multi-row tiles over row parts. wave64 behaviour is unchanged.Validation and unverified scope
Fourth commit, on MI455X (gfx1250), aiter
main03f9f37 plus the four commits;module_mla_reducebuilt for gfx950 + gfx1250:op_tests/test_mla_reduce_adaptive.py: ALL PASSED by default and withAITER_MLA_REDUCE_ADAPTIVE=0(36 cases, 2 skipped: the 64-head draft-block planner cases need [HIP] [FlyDSL] [gfx1250] MLA PS1: take 32/64-head multi-token decode as several qo tiles #7913). WithAITER_MLA_REDUCE_WAVE_HEAD=0the 8 multi-row synthetic cases fail inkn_mla_reduce_v1(the LDS race [HIP] [fix] MLA reduce: LDS race between rows in kn_mla_reduce_v1's massive path #7912 fixes); with [HIP] [fix] MLA reduce: LDS race between rows in kn_mla_reduce_v1's massive path #7912 on top: ALL PASSED in all three routings (test_mla_reduce.py170 cases,test_mla_reduce_adaptive.py36 cases + 2 skipped).op_tests/test_mla_reduce.pyunder default /AITER_MLA_REDUCE_ADAPTIVE=0/AITER_MLA_REDUCE_WAVE_HEAD=0: 167 pass; the 3 failures are the 12-head DV-128 multi-row cases inkn_mla_reduce_v1([HIP] [fix] MLA reduce: LDS race between rows in kn_mla_reduce_v1's massive path #7912), identical in all three routings.First three commits, as before: aiter
mainat111fbd8plus this change (the first commit was also run one9efac0);module_mla_reducebuilt for gfx950 + gfx1250:python3 op_tests/test_mla_reduce.py(all default dtypes, head dims and cases), withAITER_MLA_REDUCE_WAVE_HEADunset,=0and=1: every case this kernel can take passes, including the 6 new layout cases.kn_mla_reduce_v1's, the LDS race fixed in [HIP] [fix] MLA reduce: LDS race between rows in kn_mla_reduce_v1's massive path #7912;mainfails them the same way:64x[4], sometimes512x[4]), which never reach this kernel;main:test_mla_reduce.pyALL PASSED withAITER_MLA_REDUCE_WAVE_HEADunset,=0and=1.kn_mla_reduce_v1(numbers under Breaking Changes).PR-specific question
To be answered after the automated PR Self-Review comment.
Changed op tests
op_tests/test_mla_reduce_adaptive.py(new, fourth commit): see Changes. It runsaiter.mla_reduce_v1with the routing the process environment selects, so on wave64 it checkskn_mla_reduce_v1.op_tests/test_mla_reduce.py: 6LAYOUT_CASESwith ≤ 9 splits per tile, DV 512. The existing layout cases (40–250 splits per tile) never reach the new kernel; these cover its layout handling. On wave64 they runkn_mla_reduce_v1and pass as before.-1tile, a paddedreduce_indptrtail, and tiles with more rows thanmax_seqlen_q.max_seqlen_q4): 64 tiles, and 300 tiles plus a 40-entry empty tail past the 256-tile grid (the tile walk and its stop at the table's end).