Skip to content

[Fix] [AMD] tilelang: Zero masked KV loads in the BF16 sparse MLA decode kernel - #44541

Open
yuankaichen-amd wants to merge 3 commits into
sgl-project:mainfrom
yuankaichen-amd:amd/tilelang-zero-masked-kv
Open

yuankaichen-amd wants to merge 3 commits into
sgl-project:mainfrom
yuankaichen-amd:amd/tilelang-zero-masked-kv

Conversation

@yuankaichen-amd

@yuankaichen-amd yuankaichen-amd commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On HIP, sparse_mla_fwd_decode_partial is used for both prefill and decode. For invalid (-1) top-k indices it loaded KV row 0 and relied on a -inf score mask. Padded CUDA-graph rows write their K/V into row 0 (out_cache_loc padding is 0). Once row 0 held a non-finite value, -inf + q·k and 0 * v became 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 !!!!... and None logprobs until the server restarted. Eager decode and FP8 KV were not affected. SGLANG_ENABLE_WAR_BARRIER=1 did 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-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

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 yuankaichen-amd changed the title [AMD] Zero masked KV loads in the BF16 TileLang sparse MLA decode kernel [Fix] [AMD] tilelang: Zero masked KV loads in the BF16 sparse MLA decode kernel Oct 16, 2026

@XinyuJiangCMU XinyuJiangCMU 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.

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

No deployments
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.

2 participants