Skip to content

Reduce #splits and assign workload more evenly for MLA cases nhead=128 and mtp>=1 - #1626

Merged
valarLip merged 3 commits into
mainfrom
jruan/mla_less_split
Dec 12, 2025
Merged

valarLip merged 3 commits into
mainfrom
jruan/mla_less_split

Conversation

@ruanjm

@ruanjm ruanjm commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

Introduce a new solution in metadata v1.2 which bring great performance gain under the case that batch_size is small:

  prev     curr     statistics  
  main reduce total main reduce total delta ratio
-b 1 -c 233 -n 128,2 -d fp8 -kvd fp8 47.546 14.266 61.812 27.342 9.546 36.888 -24.924 -40.32%
-b 3 -c 233 -n 128,2 -d fp8 -kvd fp8 39.120 18.018 57.138 28.380 14.794 43.174 -13.964 -24.44%
-b 1 -c 2333 -n 128,2 -d fp8 -kvd fp8 49.065 20.763 69.828 37.881 13.658 51.539 -18.289 -26.19%
-b 300 -c 233 -n 128,2 -d fp8 -kvd fp8 325.997 4.938 330.935 325.997 4.969 330.966 0.031 0.01%

There should be no impact on other cases.

Metadata details for comparison:
prev_1_233_128-2_fp8_fp8.txt
curr_1_233_128-2_fp8_fp8.txt

@ruanjm
ruanjm requested review from a team and Copilot December 12, 2025 06:18

Copilot AI 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.

Pull request overview

This PR introduces metadata version 1.2 to optimize Multi-head Latent Attention (MLA) workload distribution for cases with nhead=128 and mtp>=1, particularly improving performance when batch sizes are small. The changes achieve up to 40% performance improvement in small batch scenarios while maintaining similar performance for large batches.

Key Changes:

  • Refactored workload assignment logic to distribute work more evenly across compute units by calculating overhead per QO tile instead of per batch
  • Modified reduction metadata calculation to eliminate redundant local data store usage
  • Adjusted work calculation in Python helper to account for clustering effects

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
csrc/kernels/mla/metadata/v1_2_device.cuh Refactored metadata generation kernel to improve work distribution granularity and eliminate unnecessary shared memory usage
aiter/ops/attention.py Updated work and split tile calculations to use cluster-based partitioning instead of direct CU count

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread aiter/ops/attention.py Resolved
@ruanjm
ruanjm requested review from Zzz9990 and valarLip December 12, 2025 08:08
@Zzz9990

Zzz9990 commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

Please attach perf comparison with shape "-b 64 -c 6400 -n 128,4 -d fp8 -kvd fp8" between previous code and your commit.

@ruanjm

ruanjm commented Dec 12, 2025

Copy link
Copy Markdown
Contributor Author

Please attach perf comparison with shape "-b 64 -c 6400 -n 128,4 -d fp8 -kvd fp8" between previous code and your commit.

Sure. Here are the results:

  prev     curr        
  main reduce total main reduce total delta ratio
64_6400_128-4_fp8_fp8 2076.000 150.959 2226.959 2003.000 61.681 2064.681 -162.278 -7.29%

Metadata details:
prev_64_6400_128-4_fp8_fp8.txt
curr_64_6400_128-4_fp8_fp8.txt

@valarLip
valarLip merged commit 95352ad into main Dec 12, 2025
@valarLip
valarLip deleted the jruan/mla_less_split branch December 12, 2025 09:30
zhuyuhua-v pushed a commit that referenced this pull request Dec 17, 2025
…8 and mtp>=1 (#1626)

* introduce a new solution which can reduce #splits

rev 1.1

1.2

* Revert kFixedOverheadNumBlocks

* revert attention.py
ZhangLirong-amd pushed a commit that referenced this pull request Dec 29, 2025
…8 and mtp>=1 (#1626)

* introduce a new solution which can reduce #splits

rev 1.1

1.2

* Revert kFixedOverheadNumBlocks

* revert attention.py
valarLip pushed a commit that referenced this pull request Mar 18, 2026
…8 and mtp>=1 (#1626)

* introduce a new solution which can reduce #splits

rev 1.1

1.2

* Revert kFixedOverheadNumBlocks

* revert attention.py
nehmathe2 added a commit to nehmathe2/aiter that referenced this pull request Nov 3, 2026
…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]>
kaiyang-1 pushed a commit that referenced this pull request Nov 4, 2026
…s them (#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 #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]>
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants