Repository navigation
Reduce #splits and assign workload more evenly for MLA cases nhead=128 and mtp>=1 - #1626
Conversation
There was a problem hiding this comment.
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.
|
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:
Metadata details: |
…8 and mtp>=1 (#1626) * introduce a new solution which can reduce #splits rev 1.1 1.2 * Revert kFixedOverheadNumBlocks * revert attention.py
…8 and mtp>=1 (#1626) * introduce a new solution which can reduce #splits rev 1.1 1.2 * Revert kFixedOverheadNumBlocks * revert attention.py
…8 and mtp>=1 (#1626) * introduce a new solution which can reduce #splits rev 1.1 1.2 * Revert kFixedOverheadNumBlocks * revert attention.py
…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]>
…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]>
…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]>
Introduce a new solution in metadata v1.2 which bring great performance gain under the case that batch_size is small:
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