Repository navigation
[Fix] [AMD] tilelang: Zero masked KV loads in the BF16 sparse MLA decode kernel - #44541
Open
yuankaichen-amd wants to merge 3 commits into
Open
yuankaichen-amd wants to merge 3 commits into
yuankaichen-amd wants to merge 3 commits into
Conversation
sparse_mla_fwd_decode_partial loads KV row 0 for invalid top-k indices and relies on the -inf score mask. CUDA-graph padding rows write their K/V to row 0, so a non-finite value there turns -inf + q·k and 0 * v into NaN for every later request. With BF16 KV and decode CUDA graphs, the first padded batch permanently corrupts the server. Load zeros for masked slots.
yuankaichen-amd
requested review from
BBuf,
DarkSharpness,
HaiShaw,
HydraQYH,
celve and
yuan-luo
as code owners
October 15, 2026 18:27
3 of 4 tasks
XinyuJiangCMU
approved these changes
Nov 4, 2026
XinyuJiangCMU
left a comment
Contributor
There was a problem hiding this comment.
LGTM. Simple and correct fix: empty slots now load zeros instead of reading KV row 0, so a bad value in row 0 can no longer turn the scores or the output into NaN. Keeping the safe index inside the select also keeps the load address valid.
On gfx950 I could reproduce the bug before this change. With the patch, padded batches (3 to 4, 5 to 8) stay finite, and normal inputs give bit-identical results.
My guess at how row 0 goes bad: in colocated RL, SGLang gives its GPU memory to training and takes it back before each rollout, so the padded fake request can run on stale data and write NaN into row 0. That would explain why this shows up in RL but not in plain serving.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On HIP,
sparse_mla_fwd_decode_partialis used for both prefill and decode. For invalid (-1) top-k indices it loaded KV row 0 and relied on a-infscore mask. Padded CUDA-graph rows write their K/V into row 0 (out_cache_locpadding is 0). Once row 0 held a non-finite value,-inf + q·kand0 * vbecame NaN for every later request.With BF16 KV and decode CUDA graphs, the server was correct until the first decode batch whose size had no captured graph (for example 3 requests replayed in the batch-size-4 graph). After that, every request returned the right first token followed by
!!!!...andNonelogprobs until the server restarted. Eager decode and FP8 KV were not affected.SGLANG_ENABLE_WAR_BARRIER=1did not help.This showed up on GLM-5.3-Flash BF16 RL rollout on MI355X and is independent of the model: any BF16 sparse-MLA decode that uses this kernel and CUDA graphs can poison the server.
Modifications
In
sparse_mla_fwd_decode_partial, masked slots now load zeros instead of row 0, in both the main and tail parts. The score mask is unchanged. The FP8 kernel (sparse_mla_fwd_decode_partial_fp8) has the same load pattern and is not changed here.Accuracy Tests
On a fresh MI355X server with BF16 KV and decode CUDA graphs, padded batches of 3 and 5 were correct, and so was every request afterward. After 128 concurrent requests with 4K outputs, single-request and 32-request probes were still correct. Teacher-forced logprobs against the previous torch sparse-MLA path differed by about 0.05 per token with no directional bias.
Speed Tests and Profiling
MI355X, one TP8 engine, BF16, TileLang DSA, AITER MoE, decode CUDA graphs up to batch size 128: 3,575 tok/s (17.4 ms/step) at 64 requests of 512 in / 1024 out, and 4,215 tok/s (21.3 ms/step) at 128 requests of 512 in / 4096 out. The zero-fill replaces a load of row 0; it is not a throughput change.
Checklist
Made with Cursor
CI States
Latest PR Test (Base): ❌ Run #42659729194
Latest PR Test (Extra): ❌ Run #42659728766
Latest PR Test (AMD ROCm 10): ❌ Run #42659728775
Tip
Base CI only covers the core cases, and its workload may not exercise your change. Use
/rerun-testto run the tests your change touches, or copy this prompt for an agent: