Repository navigation
[HIP] [MLA] Store per-request qo lengths before the v1_2 planner reads them - #7418
Conversation
…s them With per-request qo lengths (uni_seqlen_qo -1) and more than one qo tile per request, mla_v12_compute_sum_blocks asks mla_v12_num_qo_tiles for a batch's tile count, which reads the batch's qo length from LDS, before it stores that length there. kn_get_mla_metadata_v1_2 then sizes its per-CU budget from whatever the previous kernel left in LDS. Identical calls (128 heads, qlen 3, one request of 10k KV tokens) planned 0, 177, 243 or 256 works, so some plans were empty or left qo tiles out, and others were stable but barely split (96 heads, four requests of qlen 1-5: 13 works with every tile whole, 199 once the length is stored first). The order dates from ROCm#1626, which multiplied each batch's blocks by its qo tile count; before that the sum did not depend on the qo length, so storing it after the sum was harmless. Store it first. Each lane reads back only the batch it stored, so no barrier is needed. The parallel planner (one tile per request) and the XCD planner (uni_seqlen_qo only) do not read it there. test_metadata.py gains test_varlen_qo_matches_uniform: 96 and 128 heads, fp8 and bf16, qlen 3, 5 or 6, with and without cp round robin. A batch whose qo lengths are all equal must get the same plan with uni_seqlen_qo -1 as with uni_seqlen_qo = qlen. Without the fix all 96 shapes differ. Co-authored-by: Cursor <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR 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 |
…s them (ROCm#7418) With per-request qo lengths (uni_seqlen_qo -1) and more than one qo tile per request, mla_v12_compute_sum_blocks asks mla_v12_num_qo_tiles for a batch's tile count, which reads the batch's qo length from LDS, before it stores that length there. kn_get_mla_metadata_v1_2 then sizes its per-CU budget from whatever the previous kernel left in LDS. Identical calls (128 heads, qlen 3, one request of 10k KV tokens) planned 0, 177, 243 or 256 works, so some plans were empty or left qo tiles out, and others were stable but barely split (96 heads, four requests of qlen 1-5: 13 works with every tile whole, 199 once the length is stored first). The order dates from ROCm#1626, which multiplied each batch's blocks by its qo tile count; before that the sum did not depend on the qo length, so storing it after the sum was harmless. Store it first. Each lane reads back only the batch it stored, so no barrier is needed. The parallel planner (one tile per request) and the XCD planner (uni_seqlen_qo only) do not read it there. test_metadata.py gains test_varlen_qo_matches_uniform: 96 and 128 heads, fp8 and bf16, qlen 3, 5 or 6, with and without cp round robin. A batch whose qo lengths are all equal must get the same plan with uni_seqlen_qo -1 as with uni_seqlen_qo = qlen. Without the fix all 96 shapes differ. Co-authored-by: Cursor <[email protected]>
Summary
Fixes a read-before-write of per-request qo lengths in the fast_mode serial MLA metadata planner (
kn_get_mla_metadata_v1_2). Withuni_seqlen_qo = -1(the default) and more than one qo tile per request, it planned from uninitialized LDS, so identical calls could produce different plans, and some plans were empty.Motivation
mla_v12_compute_sum_blocksasksmla_v12_num_qo_tilesfor each request's qo tile count. That reads the request's qo length from LDS, but the length is stored there only after the call. The per-CU budget is then sized from whatever the previous kernel left in LDS.Before and after
Each row makes 8 identical
get_mla_metadata_v1calls (fast_mode, page size 1,kv_granularity16,max_split_per_batch-1) on MI355X; the last three columns list the number of works each call planned. "Per-request" passesuni_seqlen_qo = -1; "uniform" passesuni_seqlen_qo = qlenand is the reference, since a batch whose qo lengths are all equal should get the same plan either way.On main:
With this PR, every call returns the same count, and per-request matches uniform.
The order dates from #1626, which multiplied each request's KV blocks by its qo tile count. Before that the sum did not depend on the qo length, so storing it after the sum was harmless; #1626 added the read above the existing store. #3465 later moved the loop into
mla_v12_compute_sum_blocksunchanged.Any
get_mla_metadata_v1call with per-request qo lengths and more than one qo tile per request is affected, for example MTP or speculative decode at 96 or 128 heads. Callers that pass a uniformuni_seqlen_qoare not.Changes
csrc/kernels/mla/metadata/v1_2_device.cuh: store the request's qo length in LDS beforemla_v12_num_qo_tilesreads it. Each lane reads back only the request it stored, so no barrier is needed.uni_seqlen_qoonly) don't read it there and are unchanged.op_tests/test_metadata.py: new casetest_varlen_qo_matches_uniform, also run frommain().Testing
Tested on MI355X (gfx950). See "Validation and unverified scope".
Documentation
A fix inside the planner kernel; no documented interface or behavior changes.
Dependencies
Breaking Changes
None. Plans with
uni_seqlen_qo = -1change from nondeterministic to deterministic; plans with a uniformuni_seqlen_qoare unchanged.PR Self-Review
Reviewed revision
Behavior and intent
The serial planner now reads each request's qo length after it is stored, so the per-CU budget, and the plan, depend only on the inputs. A uniform batch planned with
uni_seqlen_qo = -1gets the same plan as withuni_seqlen_qo = qlen.Validation and unverified scope
All runs on MI355X (gfx950), with the same test files on main and on this PR:
pytest op_tests/test_metadata.py -k varlen_qo(new case)python3 op_tests/test_metadata.py(default sweep)test_mla_decode_update_metadata.py,test_mla_decode_gate.py,test_mla_shape_support.pyThe qlen 1 sweep and the existing tests are unchanged by this PR because they plan with
uni_seqlen_qoset (or one tile per request), which never reads the slot.Unverified:
topk >= 0) look up the qo length atbid / ori_seqlen_qo, not the slot this loop stores. That only matters for sparse plans with more than one qo tile per request, which this PR does not test or change.PR-specific question
Changed op tests
op_tests/test_metadata.pygainstest_varlen_qo_matches_uniform(with helpersplan_mtp_decodeandcheck_varlen_qo_matches_uniform). It plans 96 and 128 heads, fp8 and bf16, qlen 3, 5 or 6, batches of 1, 8, 33 and 128, with and without CP round-robin, skipping shapes that are not native on the GPU. A batch whose qo lengths are all equal must get the same plan withuni_seqlen_qo = -1as withuni_seqlen_qo = qlen, since the varlen path only reads the same lengths fromqo_indptr.The existing metadata and decode tests all plan with a uniform
uni_seqlen_qo, so none of them catches this; the new case fails on main.