Skip to content

[HIP] [MLA] Store per-request qo lengths before the v1_2 planner reads them - #7418

Merged
kaiyang-1 merged 1 commit into
ROCm:mainfrom
nehmathe2:nm/mla-v12-qo-lens-fix
Nov 4, 2026
Merged

kaiyang-1 merged 1 commit into
ROCm:mainfrom
nehmathe2:nm/mla-v12-qo-lens-fix

Conversation

@nehmathe2

Copy link
Copy Markdown
Contributor

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). With uni_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_blocks asks mla_v12_num_qo_tiles for 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_v1 calls (fast_mode, page size 1, kv_granularity 16, max_split_per_batch -1) on MI355X; the last three columns list the number of works each call planned. "Per-request" passes uni_seqlen_qo = -1; "uniform" passes uni_seqlen_qo = qlen and is the reference, since a batch whose qo lengths are all equal should get the same plan either way.

Heads Dtype qo lengths KV lengths main, per-request main, uniform (reference) This PR, per-request
128 fp8 3 10250 0 ×8 243 ×8 243 ×8
128 bf16 3 10250 0, 10, 0, 0, 0, 0, 0, 0 243 ×8 243 ×8
128 fp8 3, 3 10250, 4000 6, 6, 6, 256, 6, 6, 6, 256 246 ×8 246 ×8
96 fp8 4 10250 256 ×8 236 ×8 236 ×8
96 fp8 1, 5, 3, 4 1205, 849, 1746, 2733 13 ×8 n/a (lengths differ) 199 ×8

On main:

  • A plan with 0 works gives the decode kernel nothing to do, so the output is never written.
  • 6 works for two requests of 3 tiles means every tile runs whole, on 6 of the 256 CUs. 13 works for the four-request batch is the same kind of plan.
  • The count changes between identical calls because it depends on what the previous kernel left in LDS.

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_blocks unchanged.

Any get_mla_metadata_v1 call 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 uniform uni_seqlen_qo are not.

Changes

  • csrc/kernels/mla/metadata/v1_2_device.cuh: store the request's qo length in LDS before mla_v12_num_qo_tiles reads it. Each lane reads back only the request it stored, so no barrier is needed.
  • The parallel planner (one tile per request) and the XCD planner (uni_seqlen_qo only) don't read it there and are unchanged.
  • op_tests/test_metadata.py: new case test_varlen_qo_matches_uniform, also run from main().

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Tested on MI355X (gfx950). See "Validation and unverified scope".

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

A fix inside the planner kernel; no documented interface or behavior changes.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None. Plans with uni_seqlen_qo = -1 change from nondeterministic to deterministic; plans with a uniform uni_seqlen_qo are 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 = -1 gets the same plan as with uni_seqlen_qo = qlen.

Validation and unverified scope

All runs on MI355X (gfx950), with the same test files on main and on this PR:

Test What it checks main This PR
pytest op_tests/test_metadata.py -k varlen_qo (new case) per-request plans the same as uniform on 96 shapes fails: 96 of 96 shapes differ passes: 96 of 96 match
python3 op_tests/test_metadata.py (default sweep) serial and parallel planners agree at qlen 1; intra-batch KV coverage; the new case 27 of 27 shapes agree, then the new case fails 27 of 27 shapes agree, then the new case passes
test_mla_decode_update_metadata.py, test_mla_decode_gate.py, test_mla_shape_support.py existing metadata and decode checks 65 passed, 29 skipped, 6 xfailed the same
The 8-call repro in "Before and after" plans are stable and match the uniform plan every row is unstable or differs from the fixed plan stable in every row; per-request matches uniform

The qlen 1 sweep and the existing tests are unchanged by this PR because they plan with uni_seqlen_qo set (or one tile per request), which never reads the slot.

Unverified:

  • MI300X and MI250X were not run. The change is in architecture-independent planner code, and the new case skips shapes that are not native on the GPU it runs on.
  • Sparse plans (topk >= 0) look up the qo length at bid / 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.py gains test_varlen_qo_matches_uniform (with helpers plan_mtp_decode and check_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 with uni_seqlen_qo = -1 as with uni_seqlen_qo = qlen, since the varlen path only reads the same lengths from qo_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.

…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]>
@nehmathe2
nehmathe2 requested a review from a team November 3, 2026 22:18
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7418 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot added the HIP label Nov 3, 2026
@aiter-bot

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 5e4083850aa0 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — PR-specific question has no substantive answer

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@kaiyang-1 kaiyang-1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kaiyang-1
kaiyang-1 merged commit 3850463 into ROCm:main Nov 4, 2026
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants