Skip to content

[Triton/Gluon] Sparse MLA Improvements and Hardened Invalid index Handling - #5833

Merged
cagrikymk merged 11 commits into
mainfrom
cagri/gluon_sparse_mla_invalid_tail_lanes
Sep 25, 2026
Merged

cagrikymk merged 11 commits into
mainfrom
cagri/gluon_sparse_mla_invalid_tail_lanes

Conversation

@cagrikymk

@cagrikymk cagrikymk commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

VLLM can sometimes have poisoned kv cache where page index 0 contains NaNs. This PR addresses this by using an index valid for a request and using that as placeholder.

Besides that some tuning for models using triton 3.8.0+gitc0a142ff from vllm/vllm-openai-rocm:nightly-rocm100

Sparse MLA per forward step: branch vs aiter main

aiter main a75ba53de vs cagri/gluon_sparse_mla_invalid_tail_lanes at 14ab4a706 · Triton 3.8.0 (gitc0a142ff) · MI355X (gfx950) · 16 heads per GPU (TP4)

Each cell is the attention kernel time for one forward step, summed over the model's sparse-attention layers: main → branch (speedup), where speedup = main time ÷ branch time, so above 1.00× the branch is faster. C is the number of query rows in the step; C ≤ 1,024 is decode, C = 4,096 and 16,384 are prefill.

Headline

64-bit loads (cache pool over 2 GiB, as in vLLM) with has_invalid on:

Model Decode, C = 1 Prefill, C = 16,384
DeepSeek-V4.1-Flash 0.98× 1.45×
DeepSeek-V4-Flash, 1M 1.37× 1.39×
GLM-5.2 · fp8 0.99× 1.00×

NaN-filled cache test (every cache row no valid index points at holds NaN): branch 40 / 40 correct, aiter main 20 / 40 (every has_invalid-on case fails: 16 return NaN, 4 return wrong values).

DeepSeek-V4.1-Flash

128K context · 40 layers: 2 SWA-only, 18 c2, 20 c1

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 657.6 µs → 670.4 µs (0.98×) 638.2 µs → 658.0 µs (0.97×) 796.8 µs → 773.4 µs (1.03×) 1.89 ms → 1.91 ms (0.99×) 5.55 ms → 5.11 ms (1.09×) 20.22 ms → 14.27 ms (1.42×) 77.82 ms → 53.80 ms (1.45×)
64-bit loads, has_invalid off 618.2 µs → 613.1 µs (1.01×) 616.2 µs → 622.2 µs (0.99×) 768.5 µs → 749.4 µs (1.03×) 1.79 ms → 1.80 ms (0.99×) 5.39 ms → 4.92 ms (1.10×) 16.58 ms → 12.66 ms (1.31×) 63.10 ms → 47.64 ms (1.32×)
Buffer loads, has_invalid on 648.6 µs → 658.3 µs (0.99×) 636.5 µs → 652.7 µs (0.98×) 817.0 µs → 779.7 µs (1.05×) 1.82 ms → 1.83 ms (1.00×) 5.20 ms → 5.27 ms (0.99×) 18.57 ms → 13.29 ms (1.40×) 70.93 ms → 49.95 ms (1.42×)
Buffer loads, has_invalid off 608.5 µs → 604.8 µs (1.01×) 609.3 µs → 613.6 µs (0.99×) 787.6 µs → 767.5 µs (1.03×) 1.68 ms → 1.69 ms (0.99×) 4.83 ms → 4.87 ms (0.99×) 12.32 ms → 12.27 ms (1.00×) 46.53 ms → 45.87 ms (1.01×)

DeepSeek-V4-Flash

128K context · 43 layers: 2 SWA-only, 21 c4, 20 c128

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 752.8 µs → 700.3 µs (1.08×) 742.8 µs → 710.1 µs (1.05×) 1.02 ms → 993.2 µs (1.02×) 2.46 ms → 2.44 ms (1.01×) 7.67 ms → 7.02 ms (1.09×) 25.74 ms → 19.46 ms (1.32×) 97.45 ms → 72.62 ms (1.34×)
64-bit loads, has_invalid off 718.1 µs → 650.1 µs (1.10×) 721.1 µs → 672.4 µs (1.07×) 965.6 µs → 941.7 µs (1.03×) 2.33 ms → 2.33 ms (1.00×) 7.45 ms → 6.73 ms (1.11×) 21.42 ms → 17.08 ms (1.25×) 80.30 ms → 63.66 ms (1.26×)
Buffer loads, has_invalid on 749.0 µs → 687.3 µs (1.09×) 736.0 µs → 701.9 µs (1.05×) 991.6 µs → 965.8 µs (1.03×) 2.39 ms → 2.41 ms (0.99×) 7.32 ms → 7.45 ms (0.98×) 23.72 ms → 17.97 ms (1.32×) 89.04 ms → 66.39 ms (1.34×)
Buffer loads, has_invalid off 707.3 µs → 639.0 µs (1.11×) 709.4 µs → 661.3 µs (1.07×) 947.8 µs → 929.8 µs (1.02×) 2.19 ms → 2.19 ms (1.00×) 6.70 ms → 6.71 ms (1.00×) 16.57 ms → 16.52 ms (1.00×) 60.98 ms → 61.64 ms (0.99×)

1M context · 43 layers: 2 SWA-only, 21 c4, 20 c128

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 1.13 ms → 825.9 µs (1.37×) 1.21 ms → 919.5 µs (1.32×) 2.60 ms → 2.35 ms (1.11×) 8.39 ms → 7.64 ms (1.10×) 32.27 ms → 28.48 ms (1.13×) 107.95 ms → 82.08 ms (1.32×) 441.45 ms → 317.24 ms (1.39×)
64-bit loads, has_invalid off 1.09 ms → 767.5 µs (1.42×) 1.17 ms → 863.4 µs (1.35×) 2.48 ms → 2.26 ms (1.10×) 7.93 ms → 7.31 ms (1.08×) 31.15 ms → 26.90 ms (1.16×) 94.19 ms → 71.17 ms (1.32×) 377.66 ms → 270.81 ms (1.39×)
Buffer loads, has_invalid on 1.13 ms → 806.1 µs (1.40×) 1.19 ms → 895.3 µs (1.32×) 2.55 ms → 2.53 ms (1.01×) 8.54 ms → 8.55 ms (1.00×) 31.16 ms → 31.61 ms (0.99×) 95.60 ms → 73.69 ms (1.30×) 395.24 ms → 285.44 ms (1.38×)
Buffer loads, has_invalid off 1.08 ms → 749.2 µs (1.44×) 1.15 ms → 845.5 µs (1.36×) 2.28 ms → 2.25 ms (1.01×) 7.33 ms → 7.35 ms (1.00×) 27.26 ms → 27.31 ms (1.00×) 68.52 ms → 69.11 ms (0.99×) 267.19 ms → 263.95 ms (1.01×)

GLM-5.2 · fp8 KV

Context ≥ 2K · 78 layers, 2048-token rows

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 1.31 ms → 1.33 ms (0.99×) 1.37 ms → 1.41 ms (0.98×) 3.24 ms → 3.33 ms (0.97×) 9.16 ms → 9.27 ms (0.99×) 21.37 ms → 21.61 ms (0.99×) 58.17 ms → 58.24 ms (1.00×) 218.39 ms → 219.05 ms (1.00×)
64-bit loads, has_invalid off 1.25 ms → 1.24 ms (1.00×) 1.36 ms → 1.35 ms (1.01×) 3.31 ms → 3.31 ms (1.00×) 8.98 ms → 9.09 ms (0.99×) 22.15 ms → 22.09 ms (1.00×) 64.55 ms → 64.47 ms (1.00×) 250.02 ms → 250.12 ms (1.00×)
Buffer loads, has_invalid on 1.29 ms → 1.31 ms (0.98×) 1.38 ms → 1.41 ms (0.98×) 3.31 ms → 3.34 ms (0.99×) 9.23 ms → 9.33 ms (0.99×) 21.21 ms → 21.30 ms (1.00×) 57.14 ms → 57.27 ms (1.00×) 218.80 ms → 218.72 ms (1.00×)
Buffer loads, has_invalid off 1.24 ms → 1.25 ms (0.99×) 1.39 ms → 1.39 ms (1.00×) 3.36 ms → 3.35 ms (1.01×) 9.16 ms → 9.23 ms (0.99×) 22.40 ms → 22.36 ms (1.00×) 65.33 ms → 65.29 ms (1.00×) 250.33 ms → 249.94 ms (1.00×)

512-token context · 78 layers, compact rows

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 816.7 µs → 833.0 µs (0.98×) 836.2 µs → 865.8 µs (0.97×) 1.43 ms → 1.47 ms (0.97×) 2.91 ms → 2.93 ms (0.99×) 9.26 ms → 9.06 ms (1.02×) 17.24 ms → 17.44 ms (0.99×) 61.66 ms → 62.20 ms (0.99×)
64-bit loads, has_invalid off 955.5 µs → 936.8 µs (1.02×) 961.0 µs → 965.6 µs (1.00×) 1.45 ms → 1.52 ms (0.95×) 3.06 ms → 3.03 ms (1.01×) 9.60 ms → 9.41 ms (1.02×) 18.58 ms → 18.58 ms (1.00×) 68.18 ms → 68.22 ms (1.00×)
Buffer loads, has_invalid on 811.2 µs → 836.9 µs (0.97×) 844.7 µs → 862.7 µs (0.98×) 1.46 ms → 1.51 ms (0.96×) 2.97 ms → 2.99 ms (0.99×) 9.08 ms → 9.09 ms (1.00×) 16.90 ms → 17.08 ms (0.99×) 60.57 ms → 60.73 ms (1.00×)
Buffer loads, has_invalid off 953.2 µs → 927.4 µs (1.03×) 977.3 µs → 973.4 µs (1.00×) 1.49 ms → 1.55 ms (0.96×) 3.20 ms → 3.21 ms (1.00×) 9.38 ms → 9.44 ms (0.99×) 18.69 ms → 18.73 ms (1.00×) 68.64 ms → 68.46 ms (1.00×)

GLM-5.3-Flash · fp8 KV

Speedup only: the step time depends on the checkpoint's number of sparse-attention layers.

Context ≥ 2K · 2048-token rows

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 0.98× 0.97× 0.97× 0.98× 0.99× 1.00× 1.00×
64-bit loads, has_invalid off 0.99× 1.00× 0.99× 0.99× 1.00× 1.00× 1.00×
Buffer loads, has_invalid on 0.98× 0.98× 0.97× 0.99× 1.00× 0.99× 1.00×
Buffer loads, has_invalid off 1.01× 1.00× 0.99× 1.00× 1.00× 1.00× 1.00×

512-token context · padded to 2048 with -1

Loads, has_invalid C = 1 C = 8 C = 64 C = 256 C = 1,024 C = 4,096 C = 16,384
64-bit loads, has_invalid on 0.98× 0.98× 1.05× 1.07× 1.00× 1.00× 1.00×
Buffer loads, has_invalid on 1.00× 0.98× 1.06× 1.07× 1.00× 1.00× 1.00×

What is compared

  • aiter main: a75ba53de, top of tree when measured.
  • Branch: cagri/gluon_sparse_mla_invalid_tail_lanes at 14ab4a706.
  • 64-bit loads are what a cache pool over 2 GiB gets, which covers vLLM's pools at any real size. Buffer loads apply below 2 GiB.

With has_invalid, the UNI_TILE path sent the lanes past a segment's end to
slot 0 along with the -1 sentinels, and gathered them unmasked. Their
score is masked, but their V still enters the PV dot with p = 0, so a NaN
in slot 0 reached the output (0 * NaN = NaN) in every row whose segment
ends mid-tile. vLLM's null block sits at slot 0 and can hold NaN:
DeepSeek-V4.1 served on this kernel (VLLM_ROCM_USE_AITER_TRITON_SPARSE_MLA=1)
answered garbage from the first token.

Only the sentinels go to slot 0 now. The lanes past the end keep the
duplicate of the segment's last key that the clamped index read gives
them, as they already did without has_invalid. The same goes for the
prefetched slot ids of the prefill config.

Signed-off-by: Mehmet Cagri Kaymak <[email protected]>
With has_invalid, -1 entries still read slot 0 unmasked, so a NaN in vLLM's
null block reached the output through 0 * NaN in the PV dot. The gathers are
now masked on valid. On buffer loads the zero comes from the out-of-range
read, without a select that would stall the prefetch.

Signed-off-by: Mehmet Kaymak <[email protected]>
With has_invalid, -1 entries and lanes past a row's end now gather the first
valid slot of the program's range, so no load needs a mask and the score mask
drops them. A range with no valid slot is skipped. The masked global loads
compiled to a branch per load, which cost GLM fp8 up to 22% against main.

The last tile can also run in the loop body (UNPEEL) instead of a peeled copy
that spills at 256 VGPRs. The launchers use it on global loads, and on buffer
loads for paged prefill.

Signed-off-by: Mehmet Kaymak <[email protected]>
Signed-off-by: Mehmet Kaymak <[email protected]>
The reduce walked the splits one at a time with a single wave, a dependent
load chain per split that cost about 0.35 us each (#5829). Each head's
partials are now one tile with the splits across lanes, loaded in one round
trip and summed across lanes, with 4 warps.

Signed-off-by: Mehmet Kaymak <[email protected]>
Splitting them saves less than the reduce launch costs: the 128-row SWA-only
layer ran 4-18% slower at C <= 128 with two splits than with one.

Signed-off-by: Mehmet Kaymak <[email protected]>
@cagrikymk
cagrikymk requested a review from a team September 25, 2026 03:04
@github-actions

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; main branch always runs both MI35X and MI300X
multigpu 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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 5833 --add-label <label>

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 opt this PR out.

@github-actions github-actions Bot changed the title [TRITON][GLUON] Sparse MLA Improvements and Hardened Invalid index Handling [Triton/Gluon] Sparse MLA Improvements and Hardened Invalid index Handling Sep 25, 2026
@Boss2002n
Boss2002n requested a lite review from Copilot September 25, 2026 05:33

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.

Copilot review overview

🟡 Changes recommended

Critical reduction-layout/warp coverage issues remain, and the regression test does not poison cache slot 0.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 3 High severity · 1 Medium severity · 1 Low severity

Open (5)
What changed in this PR

This PR hardens gfx950 sparse MLA/decode handling for invalid cache indices and adds launch and reduction tuning.

Changes:

  • Redirects invalid lanes to valid request-local cache entries.
  • Tunes split counts, prefetching, unpeeling, warp counts, and reduction layout.
  • Adds large-cache invalid-index regression coverage.
File Summary
op_tests/​triton_tests/​attention/​test_pa_decode_sparse.py Adds invalid-index regression coverage for large cache pools.
aiter/​ops/​triton/​attention/​sparse_mla.py Adjusts reduction launch configuration.
aiter/​ops/​triton/​attention/​pa_decode_sparse.py Tunes split selection, prefetching, unpeeling, and reduction launches.
aiter/​ops/​triton/​_gluon_kernels/​gfx950/​attention/​sparse_mla.py Implements fallback gathers and a revised reduction layout.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@cagrikymk
cagrikymk merged commit e7d2453 into main Sep 25, 2026
70 checks passed
@cagrikymk
cagrikymk deleted the cagri/gluon_sparse_mla_invalid_tail_lanes branch September 25, 2026 18:21
tianxiaojiang4 added a commit to tianxiaojiang4/aiter that referenced this pull request Sep 25, 2026
… and tile budget

_decode_num_splits_occ bounds the split count by cta_cap = 2 * num_sms // base_wg,
which lets base_wg * splits exceed num_sms. Capping that alone is not enough: past
a point, cutting splits stops removing overhead and starts removing parallelism.
A bare one-wave cap, swept over queries 6..252 x top-k {512, 1024, 2048}, wins 6 of
24 shapes and loses 15, worst -43 %.

Bounding from both sides -- one wave above, tiles // 4 below -- gives 10 wins and
0 losses. tiles // 4 is not a new constant: it is the split floor the
base_wg >= num_sms branch of this same function already uses.

Layers under the tiles <= 2 early return from ROCm#5833, which is the same economics at
the other end -- there the reduce launch outweighs the split, here the per-split
partial write and Q re-read do. These bounds apply from tiles >= 3 up.

Composes with _MAX_SPLITS and _launch_splits from ROCm#5797, and helps their purpose:
wave_cap is a power of two, so where it binds the count needs no padding, and it
introduces padding in no cell where main did not already pad. Over queries 1..256 x
top-k {512, 1024, 2048, 8192} it cuts the distinct launched split counts from 16 to
10, so the reduce compiles for fewer counts, not more.

Adds test_decode_num_splits_occ_bounds, asserting the bounds the split count is
built from over 165 shapes. num_sms is patched rather than read from the device, so
the body allocates nothing and the bounds are checked at partitioned CU counts
(256/128/64/32) as well.

Co-Authored-By: Claude Opus 5 <[email protected]>
NOTE: the percentages above were measured before ROCm#5797 and ROCm#5833, both of which
changed the decode launch. A re-measurement against the current base is running;
this note comes out when it lands.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants