Repository navigation
[HIP] [MLA] Fix non-uniform query offsets after head folding - #6091
Conversation
QoState indexed seqlens_qo_indptr with the folded pseudo-batch id. That table has one entry per original request, so a 96-to-16 head fold read past it and the decode kernel faulted.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
Cover the padded 96-head decode shape that indexed past qo_indptr, plus a non-uniform length case and the 16-head path that must stay unfolded.
|
@aiter-bot review Trying the review bot on a few representative PRs across different areas, so the team can see what it actually produces on real diffs. How to use it — just comment on any PR:
What it is: advisory only. It surfaces hints for a human reviewer — perf regressions, silent correctness bugs, dispatch-gate holes, AI-generated code patterns. Its judgement is stochastic, it never blocks a merge, and every verdict (✅ / It runs on a self-hosted box against a local model, so a pass takes roughly 25–30 min. Who can trigger it: the logins in |
ROCm/aiter PR #6091 — [HIP] [MLA] Fix non-uniform query offsets after head foldingMaps folded pseudo-batch ids back to their original request inside QoState::get_begin/get_end, so a non-uniform batch no longer indexes seqlens_qo_indptr past its end (faulting the decode kernel) when an unsupported head count such as gfx942 bf16 96 heads is folded onto the 16-head persistent kernel. Review (advisory):
|
|
Thanks for the review. Going through the points in order: 1. Causal trim at On gfx942 it can't be reached: the persistent 16-head kernels stop at 2. Downstream label. Added 3. Planner perf. The new arithmetic in 4. Independent decode oracle for
After the fix, I'll add |
Two bugs in kn_get_mla_metadata_v1_2 when uni_seqlen_qo=-1 and the packed query length enables kQoSplits (e.g. 96 heads, qo_len >= 2): - mla_v12_compute_sum_blocks read each batch's qo length back from LDS (mla_v12_num_qo_tiles -> QoState::get_seqlen) before writing it, so sum_blocks was built from stale LDS. Depending on what was left there, the planner emitted no work, put every work on one CU, or wrote past work_info_set and faulted the decode kernel. - An empty request (graph padding) got zero qo tiles. The tile loop never advanced past it: it emitted one junk work per remaining CU with negative qo ranges and overwrote the reduce maps, corrupting the split requests before it. Write the LDS slot before reading it, and give every request at least one tile, which is what the kQoSplits=false path already does. On gfx942 with 96 bf16 heads, causal, qo_lens [2,1,3] produced no work and [2,2,2,0] / [4,4,4,0] did not match an fp32 reference. All of them match after this change. Uniform batches and qo_len 1 are unaffected. Signed-off-by: Xavier Aguilar <[email protected]>
…decode tests, with a causal fp32 reference for qo_len > 1. KV length 300 forces splits so the reduce maps of requests before the empty row are checked. Also apply black, which the Checks workflow was failing on. Signed-off-by: Xavier Aguilar <[email protected]>
|
@fangche123 I addressed the comments from the review bot, when you have some time could you please check? This bug is crashing Kimi-K3 on gfx942 when using DSpark + DCP. |
|
@aiter-bot re-review |
mla_v12_num_qo_tiles read the qo length through QoState::get_seqlen, which on non-uniform batches comes from an LDS slot that mla_v12_compute_sum_blocks has to write first. Take the length as an argument instead: mla_v12_compute_sum_blocks computes it from seqlens_qo_indptr, passes it in, and still stores it in LDS for the later stages. The other callers pass qo_state.get_seqlen(). No functional change. Signed-off-by: Xavier Aguilar <[email protected]>
CI runs op_tests as scripts, and test_mla_folded_qo_metadata.py had only pytest functions and no __main__, so it never ran there. Fold its cases into the existing suites instead. test_metadata.py: add a non-uniform bf16 sweep (16 and 96 heads, [1]*7+[0], [2,1,3], [2,2,2,0], [4,4,4,0]) with its own summary table. Every work's qo range must stay inside the folded Q layout and cover each pseudo-batch exactly, and the serial and parallel planners must agree on all works that carry queries plus the reduce maps. The serial planner gives an empty request one zero-length work and the parallel planner skips it, so zero-length works are not compared. Runs by default. test_mla_persistent.py: add -pad/--pad_batches (default 0), which turns the last N requests into CUDA-graph padding rows with no queries and no KV and runs the planner with uni_seqlen_qo=-1. The torch references skip empty requests and zero-length works. Signed-off-by: Xavier Aguilar <[email protected]>
Conflicts in the tests only: - op_tests/test_metadata.py: keep both the non-uniform qo sweep and upstream's intra-batch KV coverage check. - op_tests/test_mla_persistent.py: build metadata with upstream's _make_mla_metadata, passing uni_seqlen_qo=-1 when --pad_batches is non-zero, and add the padding axis to the sweep in main(). Signed-off-by: Xavier Aguilar <[email protected]>
|
@samremes thanks for the great feedback! I already addressed your comments. |
|
@xaguilar-amd thanks, I checked the earlier points (test placement moved into Two things before this can go in:
|
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 |
|
@samremes ping. |
|
@fangche123 could you please have a look at this when you have some time? Kimi-K3 (DSpark + DCP) does not work on gfx942 without this fix. It would be really good if we can get the fix into the next AITER cut. |
* [MLA] Fix non-uniform query offsets after head folding QoState indexed seqlens_qo_indptr with the folded pseudo-batch id. That table has one entry per original request, so a 96-to-16 head fold read past it and the decode kernel faulted. * [MLA] Test non-uniform query offsets after head folding Cover the padded 96-head decode shape that indexed past qo_indptr, plus a non-uniform length case and the 16-head path that must stay unfolded. * [MLA] Fix metadata planner on non-uniform batches with query tiles Two bugs in kn_get_mla_metadata_v1_2 when uni_seqlen_qo=-1 and the packed query length enables kQoSplits (e.g. 96 heads, qo_len >= 2): - mla_v12_compute_sum_blocks read each batch's qo length back from LDS (mla_v12_num_qo_tiles -> QoState::get_seqlen) before writing it, so sum_blocks was built from stale LDS. Depending on what was left there, the planner emitted no work, put every work on one CU, or wrote past work_info_set and faulted the decode kernel. - An empty request (graph padding) got zero qo tiles. The tile loop never advanced past it: it emitted one junk work per remaining CU with negative qo ranges and overwrote the reduce maps, corrupting the split requests before it. Write the LDS slot before reading it, and give every request at least one tile, which is what the kQoSplits=false path already does. On gfx942 with 96 bf16 heads, causal, qo_lens [2,1,3] produced no work and [2,2,2,0] / [4,4,4,0] did not match an fp32 reference. All of them match after this change. Uniform batches and qo_len 1 are unaffected. Signed-off-by: Xavier Aguilar <[email protected]> * Add [2,2,2,0] and [4,4,4,0] (96 heads) to the folded query-range and decode tests, with a causal fp32 reference for qo_len > 1. KV length 300 forces splits so the reduce maps of requests before the empty row are checked. Also apply black, which the Checks workflow was failing on. Signed-off-by: Xavier Aguilar <[email protected]> * [MLA] Pass qo length explicitly to mla_v12_num_qo_tiles mla_v12_num_qo_tiles read the qo length through QoState::get_seqlen, which on non-uniform batches comes from an LDS slot that mla_v12_compute_sum_blocks has to write first. Take the length as an argument instead: mla_v12_compute_sum_blocks computes it from seqlens_qo_indptr, passes it in, and still stores it in LDS for the later stages. The other callers pass qo_state.get_seqlen(). No functional change. Signed-off-by: Xavier Aguilar <[email protected]> * [MLA] Move folded non-uniform qo tests into existing op_tests CI runs op_tests as scripts, and test_mla_folded_qo_metadata.py had only pytest functions and no __main__, so it never ran there. Fold its cases into the existing suites instead. test_metadata.py: add a non-uniform bf16 sweep (16 and 96 heads, [1]*7+[0], [2,1,3], [2,2,2,0], [4,4,4,0]) with its own summary table. Every work's qo range must stay inside the folded Q layout and cover each pseudo-batch exactly, and the serial and parallel planners must agree on all works that carry queries plus the reduce maps. The serial planner gives an empty request one zero-length work and the parallel planner skips it, so zero-length works are not compared. Runs by default. test_mla_persistent.py: add -pad/--pad_batches (default 0), which turns the last N requests into CUDA-graph padding rows with no queries and no KV and runs the planner with uni_seqlen_qo=-1. The torch references skip empty requests and zero-length works. Signed-off-by: Xavier Aguilar <[email protected]> --------- Signed-off-by: Xavier Aguilar <[email protected]> Co-authored-by: amd-ruitang3 <[email protected]>
Summary
Fix MLA metadata generation for non-uniform query lengths when an unsupported head count is folded onto the 16-head persistent kernel.
QoState::get_begin()andget_end()indexedseqlens_qo_indptrwith the folded pseudo-batch id. That indptr has one entry per original request, not per pseudo-batch. On gfx942, 96 bf16 heads fold by 6, so an 8-request batch produces ids up to 47 while the indptr has only 9 entries.Map the pseudo-batch back to its original request and head subgroup, then compute the offset in the folded Q layout:
qk_batch_ratio <= 1keeps the absolute indptr values, so native head counts are unchanged. The KV path already mapped folded ids this way. The sparse path is unchanged.Problem
uni_seqlen_qo=-1with a folded head count takes this lookup. An out-of-range indptr value is written intoMlaWorkInfo.qo_start/qo_end. A large value faults the decode kernel (HSA_STATUS_ERROR_MEMORY_FAULT); a small one produces wrong attention.We hit this on gfx942 during Kimi-K3 TP8/DCP8 decode with full graphs and
--max-num-seqs 8. While all 8 requests have query length 1,uni_seqlen_qo=1and the uniform path is fine. The tail of the run is 7 real tokens plus one empty graph-padding row, so vLLM correctly passesuni_seqlen_qo=-1and the folded lookup goes out of range.gfx950 bf16 does not fold 96 heads, so this particular shape does not take the bad path there. The lookup itself is shared.
Planner fixes for non-uniform batches with query tiles
Testing
qo_len > 1against a decode reference exposed two more bugs inkn_get_mla_metadata_v1_2. Both needuni_seqlen_qo=-1withkQoSplitsenabled, e.g. 96 heads withqo_len >= 2:mla_v12_compute_sum_blocksread each batch's qo length from LDS before writing it, so the work split came from stale LDS. Depending on what was left there, the planner emitted no work, put every work on one CU, or wrote pastwork_info_set.mla_v12_num_qo_tilesnow takes the qo length as an argument instead of reading it back throughQoState.mla_v12_compute_sum_blockscomputes it fromseqlens_qo_indptr, passes it in, and still stores it in LDS for the later stages. Every request gets at least one tile, as thekQoSplits=falsepath already does.vLLM's ROCm AITER MLA backend doesn't hit these in practice. It passes
uni_seqlen_qo=-1only for single-token decode or mixed query lengths, and pads spec-decode batches into uniform ones.Test plan
All on gfx942 (MI325X).
op_tests/test_metadata.py(default sweep, run by CI): passes. Besides the existing parallel vs serial planner sweep, it now has a non-uniform bf16 sweep: 16 and 96 heads, query lengths[1,1,1,1,1,1,1,0],[2,1,3],[2,2,2,0],[4,4,4,0],uni_seqlen_qo=-1. Each work's query range must stay inside the folded Q and cover its pseudo-batch exactly. The serial and parallel planners must agree on every work that carries queries and on the reduce maps.3eca6e4a), every 96-head case fails the range check,[2,1,3]and[2,2,2,0]also diverge between planners, and[4,4,4,0]faults the GPU. The 16-head cases pass there too.op_tests/test_mla_persistent.pywith the new--pad_batchesoption, which turns the last N requests into CUDA-graph padding rows (no queries, no KV) and runs the planner withuni_seqlen_qo=-1. Checked against the existing torch decode and split-KV/reduce references:decode:err0. Context 300 forces KV splits, so the reduce maps of the requests before the padding rows are exercised. Padding defaults to 0, so CI does not run these cases; the planner-level checks above cover the regression in CI.uniform_qo_len <= 1check (results below). Run before themla_v12_num_qo_tilesrefactor, which is not meant to change behaviour.op_tests/test_mla_persistent.pysweep, with and without--varlen, compared against the pre-fix module: same results. fp8 KV at context 21 fails on gfx942 in both runs, so that failure predates this PR. Run before themla_v12_num_qo_tilesrefactor.PR Self-Review
Reviewed revision
d07becc7f917956e0ee97c35a75af30942516a24Behavior and intent
Head folding (
qk_batch_ratio = num_heads / 16 > 1) makes the MLA metadata planners walknum_batches * qk_batch_ratiopseudo-batches. For non-uniform batches (uni_seqlen_qo = -1),QoState::get_begin/get_endindexedseqlens_qo_indptr, which has one entry per original request, with the pseudo-batch id. That read past the indptr table and emittedqo_start/qo_endranges that do not match the folded Q layout built byaiter/mla.py. Folded decode with padded (empty) requests or mixed query lengths then reads the wrong Q rows or faults. On the pre-PR base3eca6e4a, every folded 96-head case fails the range check and[4,4,4,0]hits a GPU memory access fault.This PR:
b * ratio + gin the folded layout ((indptr[b] - indptr[0]) * ratio + g * len_b). It keeps the original lookup whenratio <= 1and on the sparse path.kQoSplits, so the serial and parallel planners advance past it identically.Uniform (
uni_seqlen_qo > 0) and unfolded paths produce the same metadata as before.Validation and unverified scope
All runs below were on gfx942 (MI325X), at the reviewed revision
d07becc7, withPYTHONPATHpointing at the checkout:python3 op_tests/test_metadata.py(defaults): passed. All 8 non-uniform cases ([1]*7+[0],[2,1,3],[2,2,2,0],[4,4,4,0]with 16 and 96 heads, bf16) report 0 serial/parallel mismatches and 0 folded qo range errors. Upstream's intra-batch KV coverage check also passed.AITER_MLA_DECODE_PERSISTENT_MAX_BATCH=0 python3 op_tests/test_mla_persistent.py -n 96,1 96,2 96,4 16,2 -b 4 8 -c 300 -d bf16 -kvd bf16 -pad 0 1 3: passed. All 24 combinations havedecode:err = 0against the torch reference, including 1 and 3 padded requests. The_fold_seqlen_indptrcuda-graph capture check passed.fast_mode=False), using a throwaway script that appliesfolded_qo_range_errorsfromtest_metadata.py: passed. Folded ranges are correct for 32/64/96/112 heads ×[2,1,3],[1,3,2,4],[3,1],[1,2]*4,[4,1,4,2](20 cases).3eca6e4a: the sametest_metadata.pycases fail (range errors on every folded 96-head case, planner divergence on[2,1,3]/[2,2,2,0], GPU memory access fault on[4,4,4,0]).ruffandblackare clean on the changed Python files.Unverified:
7cb1f00f), the MI35X shards runningtest_metadata.pyandtest_mla_persistent.pywith default arguments passed. The-padpadded-decode sweep was not run on gfx950. There, bf16 never folds, and fp8 folds only for 48/80/112 heads, or 96 heads whenmax_seqlen_qo > 6.test_mla_persistent.py -mm v1_1 -pad 1already fails for unfolded 16 heads, where the changedQoStatecode is a no-op. That is an existing v1_1 limitation with empty requests, not something this PR introduces.qk_batch_ratio, so it always takes the unchangedratio <= 1branch. It was not run.[2,1,3]folded) are validated at the planner level only. The host Q fold inaiter/mla.pyformax_seqlen_q > 1still assumes every non-empty request hasmax_seqlen_qtokens. That limitation is remaining and unchanged by this PR.PR-specific question
The changed
get_begin/get_endbranch runs only when three things hold at the same time:uni_seqlen_qo = -1, i.e.kUniSeqlenQo <= -1.!kIsSparse.qk_batch_ratio > 1.The ratio comes from
mla_decode_head_plan_v1_2/_v1_1incsrc/include/mla_decode_shape.h:max_seqlen_qo <= 6.QoState. The v1_2 PA planner leavesqk_batch_ratiounset, so it stays on the unchanged branch.The new tile minimum only applies under
kQoSplits(packed qo length >kPackedQoLenPerWg), and only changes the result for empty requests.Invariants that keep unsupported inputs out:
num_heads % 16 == 0. Any other head count is rejected byAITER_CHECKon an unaccepted plan.ratio <= 1returns the exact original indptr values, so unfolded callers are bit-identical._fold_seqlen_indptr(repeat_interleave) and the host Q fold.test_metadata.pychecks every work range against it, so a layout drift fails the test.Changed op tests
op_tests/test_metadata.py: addstest_nonuniform_metadata(-q/--qo-lens,--qo-nhead). It builds non-uniform batches with empty requests and checks two things:[qo_start, qo_end)exactly covers its pseudo-batch in the folded Q layout, with no range past the folded tensor.This is the cheapest direct check of the indptr lookup and fails on the pre-PR base. The existing tests here use uniform
uni_seqlen_qoand never reach the non-uniform folded branch.op_tests/test_mla_persistent.py: adds-pad/--pad_batches, which turns trailing requests into graph padding (qo and kv length 0, souni_seqlen_qo = -1). It also makes the torch references skip empty requests and works.This is the only end-to-end check that the folded metadata produces correct decode output and that padded requests don't fault. The existing sweeps use uniform qlen, so they never hit the changed path. The deleted
op_tests/test_mla_folded_qo_metadata.pyhad no__main__and never ran in CI; its coverage moved into the two files above.