Repository navigation
[HIP] fix(mla): stop gfx950 MLA decode from reading wrong KV past 4 GiB - #5893
vanshbhatia-amd wants to merge 5 commits into
Conversation
Several gfx950 MLA decode code objects form the KV byte offset as page_id * s_Bs with v_mul_u32_u24 and load through a 32-bit buffer descriptor, so KV more than 4 GiB past ptr_KV is read from the wrapped address with no fault. Where a 64-bit-addressed kernel already exists, route to it; where none exists, refuse instead of returning wrong output. - fp8/fp8 gqa32 qlen1 persistent: fold onto mla_a8w8_qh16_qseqlen4_gqaratio16_v3_ps, as gqa32 qlen 2-3 already are. - bf16 16-head persistent batch gate: keep the persistent kernel when kv_buffer spans past the 32-bit limit instead of falling back to mla_dec_stage1_bf16_a16w16_subQ16_mqa16. - non-persistent single-token decode past the limit (bf16 16/32/128 heads, fp8 128 heads): build a persistent schedule and run the persistent kernels. - launcher: reject the remaining 32-bit-offset code objects when kv_buffer spans past 0xFFFFFFF0 bytes. - op_tests: add test_mla_kv_4gib.py, qh32_fp8_q1 to the ltx global-load presets, and a gate case. Refs ROCm#5765
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Unresolved critical and moderate findings can leave unsafe routing or invalidate regression coverage.
Review effort: Lite
Findings: 2
Open (3)
What changed in this PR
Fixes gfx950 MLA decode KV addressing beyond 4 GiB by rerouting safe shapes, guarding unsafe kernels, and adding regression tests.
Changes:
- Routes supported paths to 64-bit-safe persistent kernels.
- Guards remaining 32-bit-offset code objects.
- Adds boundary, dispatch, and regression coverage.
| File | Summary | Review findings |
|---|---|---|
op_tests/test_mla_ltx.py |
Extends gfx950 coverage with the qh32 fp8 preset. | None |
op_tests/test_mla_kv_4gib.py |
Adds large-KV regression coverage. | Critical (1 vote): helper is collected by pytest; Moderate (2 votes): unrelated exceptions are swallowed. |
op_tests/test_mla_decode_gate.py |
Tests persistent routing at the 4 GiB threshold. | None |
csrc/py_itfs_cu/asm_mla.cu |
Guards unsafe gfx950 code objects and folds fp8 dispatch. | Nit (1 vote): error text misstates the maximum valid span. |
aiter/mla.py |
Updates MLA dispatch and persistent scheduling. | Critical (1 vote): experimental HK routing can bypass the overflow guard. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Don't redirect to persistent when the experimental HK or opus decode paths would run instead of the asm kernels. - Key the guard set on the full co_name and state the exact byte limit. - test_mla_kv_4gib: rename the helper so pytest doesn't collect it, and re-raise errors other than the guard refusal or an unsupported shape.
|
please show all the perf data after your fix, kernel mapping is for best performance, if you need 64bit address mapping, @fangche123 can help add feature support inside the kernel, should not change the kernel mapping |
Keeps the gfx950 kernel mapping unchanged for kv_buffers within the 32-bit span; only calls that would read wrong KV switch kernels.
|
Thanks @shengnxu. Agreed that the mapping should stay as tuned. In 6713d79 I made the one mapping change that applied at every size (fp8 32-head single-token) conditional too. Every kernel-mapping change in this PR now applies only when Perf data below: MI355X (gfx950), main 8253efc vs this PR, same node and container.
|
| path (1 token, page_size 1) | kernel on main | kernel with this PR | main err | PR err | PR / main time |
|---|---|---|---|---|---|
| bf16, 16 heads, persistent caller, batch ≥ 32 | mla_dec_stage1_bf16_a16w16_subQ16_mqa16 |
mla_a16w16_qh64_qseqlen1_gqaratio64_v3_ps |
0.69–1.36 | 0.0000 | 0.97–1.15 (median 1.08) |
| bf16, 16 heads, non-persistent | mla_dec_stage1_bf16_a16w16_subQ16_mqa16 |
mla_a16w16_qh64_qseqlen1_gqaratio64_v3_ps |
0.60–1.28 | 0.0000 | 1.21–2.13 (median 1.58) |
| bf16, 32 heads, non-persistent | MLA_A16W16_1TG_4W_16mx4_32nx1_Coex0_Msk1_QH16 |
mla_a16w16_qh64_qseqlen1_gqaratio64_v3_ps |
0.63–1.44 | 0.0000 | 1.15–2.02 (median 1.43) |
| bf16, 128 heads, non-persistent | mla_dec_stage1_bf16_a16w16_subQ128_mqa128 |
mla_a16w16_qh32_qseqlen4_gqaratio32_ps |
0.66–1.39 | 0.0000 | 0.80–1.33 (median 1.21) |
| fp8, 128 heads, non-persistent | mla_a8w8_qh128_m32x4_n16x2_msk1 |
mla_a8w8_qh32_qseqlen4_gqaratio32_ps |
0.56–1.15 | 0.0000 | 0.83–1.50 (median 1.24) |
| fp8, 32 heads, persistent | mla_a8w8_qh32_qseqlen1_gqaratio32_ps |
mla_a8w8_qh16_qseqlen4_gqaratio16_v3_ps (s_MQA 32) |
0.69–1.19 | 0.0000 | 0.91–1.05 (median 1.00) |
All past-4-GiB rows (µs)
| path | batch | ctx | main µs | main err | PR µs | PR err | PR / main |
|---|---|---|---|---|---|---|---|
| gate_bf16_h16 | 32 | 1024 | 22.8 | 1.331 | 24.1 | 0.0000 | 1.06 |
| gate_bf16_h16 | 32 | 8192 | 61.4 | 0.709 | 63.2 | 0.0000 | 1.03 |
| gate_bf16_h16 | 64 | 1024 | 26.2 | 1.273 | 30.2 | 0.0000 | 1.15 |
| gate_bf16_h16 | 64 | 8192 | 116.4 | 0.690 | 133.0 | 0.0000 | 1.14 |
| gate_bf16_h16 | 128 | 1024 | 35.3 | 1.098 | 39.2 | 0.0000 | 1.11 |
| gate_bf16_h16 | 128 | 8192 | 215.6 | 0.692 | 233.3 | 0.0000 | 1.08 |
| gate_bf16_h16 | 256 | 1024 | 64.3 | 1.356 | 62.6 | 0.0000 | 0.97 |
| gate_bf16_h16 | 256 | 8192 | 416.5 | 0.776 | 435.6 | 0.0000 | 1.05 |
| nps_bf16_h16 | 1 | 1024 | 18.4 | 1.185 | 25.4 | 0.0000 | 1.38 |
| nps_bf16_h16 | 1 | 8192 | 27.7 | 0.596 | 48.0 | 0.0000 | 1.73 |
| nps_bf16_h16 | 32 | 1024 | 22.4 | 1.280 | 35.4 | 0.0000 | 1.58 |
| nps_bf16_h16 | 32 | 8192 | 60.6 | 0.698 | 75.2 | 0.0000 | 1.24 |
| nps_bf16_h16 | 128 | 1024 | 35.5 | 1.141 | 75.6 | 0.0000 | 2.13 |
| nps_bf16_h16 | 128 | 8192 | 218.7 | 0.699 | 265.3 | 0.0000 | 1.21 |
| nps_bf16_h32 | 1 | 1024 | 19.9 | 1.441 | 28.4 | 0.0000 | 1.43 |
| nps_bf16_h32 | 1 | 8192 | 32.3 | 0.631 | 49.8 | 0.0000 | 1.54 |
| nps_bf16_h32 | 32 | 1024 | 25.9 | 1.163 | 36.1 | 0.0000 | 1.39 |
| nps_bf16_h32 | 32 | 8192 | 66.2 | 0.669 | 79.3 | 0.0000 | 1.20 |
| nps_bf16_h32 | 128 | 1024 | 39.0 | 1.133 | 78.9 | 0.0000 | 2.02 |
| nps_bf16_h32 | 128 | 8192 | 234.8 | 0.680 | 269.1 | 0.0000 | 1.15 |
| nps_bf16_h128 | 1 | 1024 | 27.7 | 1.189 | 36.8 | 0.0000 | 1.33 |
| nps_bf16_h128 | 1 | 8192 | 71.9 | 0.664 | 58.6 | 0.0000 | 0.82 |
| nps_bf16_h128 | 32 | 1024 | 46.8 | 1.393 | 56.8 | 0.0000 | 1.21 |
| nps_bf16_h128 | 32 | 8192 | 168.1 | 0.726 | 142.3 | 0.0000 | 0.85 |
| nps_bf16_h128 | 128 | 1024 | 98.7 | 1.242 | 121.7 | 0.0000 | 1.23 |
| nps_bf16_h128 | 128 | 8192 | 520.6 | 0.732 | 414.3 | 0.0000 | 0.80 |
| nps_fp8_h128 | 1 | 1024 | 24.4 | 1.095 | 36.5 | 0.0000 | 1.50 |
| nps_fp8_h128 | 1 | 8192 | 48.0 | 0.562 | 56.7 | 0.0000 | 1.18 |
| nps_fp8_h128 | 32 | 1024 | 40.2 | 1.093 | 49.7 | 0.0000 | 1.24 |
| nps_fp8_h128 | 32 | 8192 | 100.3 | 0.763 | 88.5 | 0.0000 | 0.88 |
| nps_fp8_h128 | 128 | 1024 | 68.7 | 1.153 | 97.0 | 0.0000 | 1.41 |
| nps_fp8_h128 | 128 | 8192 | 315.0 | 0.638 | 261.2 | 0.0000 | 0.83 |
| ps_fp8_h32 | 1 | 1024 | 23.2 | 1.139 | 22.5 | 0.0000 | 0.97 |
| ps_fp8_h32 | 1 | 8192 | 41.4 | 0.713 | 42.4 | 0.0000 | 1.02 |
| ps_fp8_h32 | 32 | 1024 | 21.8 | 1.127 | 21.9 | 0.0000 | 1.00 |
| ps_fp8_h32 | 32 | 8192 | 42.5 | 0.773 | 41.5 | 0.0000 | 0.98 |
| ps_fp8_h32 | 128 | 1024 | 32.8 | 1.186 | 30.0 | 0.0000 | 0.91 |
| ps_fp8_h32 | 128 | 8192 | 138.1 | 0.694 | 140.4 | 0.0000 | 1.02 |
| ps_fp8_h32 | 256 | 1024 | 37.6 | 1.144 | 36.9 | 0.0000 | 0.98 |
| ps_fp8_h32 | 256 | 8192 | 237.6 | 0.767 | 249.2 | 0.0000 | 1.05 |
- Persistent callers (fp8 32 heads, and bf16 16 heads where the batch gate used to fall back): the correct kernel runs at 0.91–1.15× the time of the kernel that currently returns wrong output.
- Non-persistent callers: the redirect builds the persistent work schedule (
get_mla_metadata_v1) inside every call. That is most of the extra time at small shapes. At large shapes the persistent kernels are faster (0.80–0.88×).
If @fangche123 can add 64-bit KV addressing inside these kernels, that is the better fix. Each kernel's entry then comes off kGfx950MlaKv32BitCo, and the corresponding reroute can be dropped. Until then, the choice past 4 GiB is between a reroute (correct, numbers above) and a refusal from the launcher guard. If you'd rather keep this PR to the guard only (no reroutes), I can cut it down to that.
Script: bench_4gib.py (can add it to op_tests if useful).
|
No activity for 15 days, so this is now labelled |
|
Hi @shengnxu - please let me know if there's anything else to be added! |
|
No activity for 15 days, so this is now labelled |


Problem
On gfx950,
mla_decode_fwdreturns wrong attention, with no fault, NaN or warning, when the KV rows it reads sit more than 4 GiB pastkv_buffer's base pointer. #4452 and #4156 fixed this for the kernels they rebuilt. Several paths still pick kernels that were not rebuilt. #5765 reports the bf16 non-persistent ones; persistent paths are affected too:mainmainmla_a8w8_qh32_qseqlen1_gqaratio32_psmla_a16w8_qh16_m16x4_n16x1_coex0_mask1_psmla_dec_stage1_bf16_a16w16_subQ16_mqa16mla_dec_stage1_*,MLA_A16W16_*,mla_a16w16_qh8_*,mla_a16w16_qh16_m16x4_*mla_a8w8_qh8_qseqlen1_gqaratio8_v3,mla_a8w8_qh16_qseqlen2_gqaratio16,mla_a8w8_qh128_m32x4_n16x2_msk1The new
op_tests/test_mla_kv_4gib.pyplaces the same requests at page 0 and just past 4 GiB. Onmainit finds 29 configs that are right at page 0 and wrong past 4 GiB.It is reachable in practice: vLLM's hybrid KV grouping can put a request's pages past 4 GiB in one layer's KV tensor (we hit it with Kimi-K3, FP8 KV and a drafter, on a release from before #4452), and #5765 hits it through a pool-global row index.
Root cause
In every kernel above the KV byte offset is
v_mul_u32_u24 page_id, s_Bs(24×24-bit multiply, low 32 bits kept), used as the offset of abuffer_load ... offenwhose descriptor hasnum_records = 0xFFFFFFF0. None of these code objects contains aglobal_load. The kernels refreshed in #4452 load KV with 64-bitglobal_load_lds_dwordx4instead.Fix
Kernel mapping is unchanged for any
kv_bufferwithin the limit. Past it, where a 64-bit-addressed kernel already exists for the shape, route to it. Where none exists, refuse instead of returning wrong output.mla_a8w8_qh16_qseqlen4_gqaratio16_v3_ps(s_MQA = 32), the same fold that 32-head 2–3-token and 64-head single-token decode already use. The lse variant is picked too. Below the limit the mapping is unchanged._use_persistent_mla_decode) keeps the persistent kernel whenkv_bufferspans more than0xFFFFFFF0bytes, instead of falling back to the 32-bitmla_dec_stage1kernel.get_mla_metadata_v1and runs the persistent kernels, which are 64-bit for these shapes. Only single-token decode is redirected, because the causal mask is a no-op there, so both paths compute the same attention. The redirect is skipped whenAITER_ENABLE_EXPERIMENTAL=1, because the HK kernel it would select also uses 32-bit KV offsets. WithAITER_MLA_USE_OPUS=1the redirect lands on opus, which is correct past 4 GiB (bf16 16/32/64/128 heads and fp8 128 heads verified). Nothing changes below the limit.mla_decode_stage1_asm_fwdrejects the remaining 32-bit-offset code objects (19 gfx950.co, listed inasm_mla.cu) whenkv_bufferspans more than0xFFFFFFF0bytes, for example:Behavior change: such calls used to return correct output only when every page they touched happened to lie below 4 GiB. They now raise. The remaining cases are bf16 Q with fp8 KV, non-persistent multi-token decode, and 8-head non-persistent decode. They need their
.corebuilt with 64-bit KV addressing, and each entry can be dropped from the list once that happens.Refs #5765.
Not in this PR
global_loadby disassembly, so they very likely wrap the same way. I could not test gfx942 (MI355X only here), so none of them are in the guard.mla_pfl_*) are also 32-bit by disassembly. They go through a different entry point and are not covered here.Tests (MI355X, gfx950)
op_tests/test_mla_kv_4gib.py(new; 85 configs over dtype × heads 8–128 × 1/2/4 tokens × persistent/non-persistent):main: 29 wrong past 4 GiB, 25 ok.maindoes not support at any offset; they are unchanged.op_tests/test_mla_ltx.pydefault sweep, now withqh32_fp8_q1inGLOBAL_LOAD_PRESETS: 90/90 pass. Onmain,qh32_fp8_q1failsedge_4g_first_overflow,page_id_16m,page_id_2p24andpage_id_19m, with lse both off and on.op_tests/test_mla_persistent.py -d fp8 -kvd fp8and-d bf16 -kvd bf16(5 default head configs × 64 shapes each),mainvs this PR:test_mla_persistent.py -d fp8 -kvd fp8 -n 32,1(with and without-lse), the old 32-head kernel vs the fold:mla_a8w8_qh32_qseqlen4_gqaratio32_psinstead was 30–45% slower.op_tests/test_mla_decode_gate.py: 6/6, including a new case for the 4 GiB rule.