Repository navigation
[Fix] dsa: Isolate masked KV rows in FP8 TileLang attention - #45988
HanHan009527 wants to merge 2 commits into
Conversation
Zero invalid key/value rows before QK and PV so NaN in the unused padding slot cannot contaminate valid attention outputs. Cover eager and CUDA Graph execution, all-masked rows, and zero/nonzero RoPE.
|
We ran into the same failure while looking into #48964 (GLM-5.3-Flash, FP8 KV, TileLang DSA, H20) and arrived at the same change. On cost: #47013 measured this PR's version at 1.3–3.3× the kernel time of main on H200, and we see the same on H20. The cost comes from how the guard is written, not from the guard itself. With the condition taken from the unclamped index in shared memory and an FP8 zero, the kernel time matches main: fp8_zero = T.cast(0, fp8_dtype)
for k_i in T.serial(inner_iter):
topk_block_i = group_i * inner_iter + k_i
for bi_i in T.Parallel(BI):
idx = indices[b_i, s_i, g_i, topk_block_i * BI + bi_i]
page_idx_shared[bi_i] = idx # no longer clamped to 0
mask[bi_i] = idx >= 0
for bi_i, j in T.Parallel(BI, group_size):
page = page_idx_shared[bi_i]
row = T.if_then_else(page >= 0, page, 0)
kv_tile0[bi_i, j] = T.if_then_else(
page >= 0, kv_fp8[b_i, row, g_i, j], fp8_zero
)
# kv_tile1..3 and k_tail_shared the same way
Over all 38 shapes (8/16/64 heads, 1–8192 tokens, RoPE tail 0 or 64), this PR takes 1.28–3.24× the time of main and the variant 0.988–1.023× (geometric mean 1.000). The variant passes this PR's regression test (all three layouts, eager and graph, all-masked rows), which fails on main. With a finite row 0, its output is bit-identical to main. @HanHan009527, would you like to take this into your PR? If you'd rather not, we can open it separately with you as co-author. cc @JackChuang |
Motivation
Invalid (
-1) sparse indices insparse_mla_fwd_decode_partial_fp8map to KV row 0. Masking the attention score does not isolate that row: a NaN in it can propagate through both QK (-inf + NaN) and PV (0 * NaN), corrupting otherwise valid outputs and generation logprobs.This was reproduced on GLM-5.3-Flash with FP8 KV and TileLang DSA on H20, with both PP1 and PP2, including a plain-main deployment without EPLB. A direct operator reproduction changed only padding KV row 0 from zero to NaN and changed the output from finite to 196608 NaN elements. This is a correctness fix; it does not require changing the deployment's CUDA Graph, overlap, or speculative decoding settings.
Related prior work: #44541 applies the same masked-load isolation to the BF16 kernel and explicitly leaves the FP8 kernel unchanged. This PR covers that FP8 gap. #36207 tracks producer-side writes to reserved KV row 0; this consumer-side fix does not depend on row 0 remaining finite and does not claim to resolve every writer path. The FlashInfer masked-gather fix discussed in #42440 concerns a different backend.
Modifications
Validation
GPU results were collected on base
8d8916815b9f2717a34dac16190157189e54fb57plus fix985608ef7795830643a2fa74f5f287c286640643. The delivery branch merges main3f36fc08331f64cd63c4fbbdf39cc402f38bb38b; both modified files are byte-identical to those GPU-tested files, and both corresponding pre-fix files are unchanged between the two main snapshots. GPU tests and the full service were not rerun on the refreshed base. This is a small quality check, not a full-model accuracy evaluation. AMD and other hardware are unvalidated for this FP8 change.Reproduce in an installed SGLang GPU environment:
The initial runtime write that made row 0 non-finite was not captured. The deterministic masked-index defect and the only-kernel-change recovery are confirmed; allocator initialization is not claimed as the cause.
Performance
No speedup is claimed. This restores correct outputs, so the incorrect-output deployment is not a performance baseline. End-to-end automatic-EPLB performance measurement is a separate, incomplete validation effort.
Checklist
CI States
Latest PR Test (Base): ❌ Run #39910411746
Latest PR Test (Extra): ❌ Run #39910411642
Latest PR Test (AMD ROCm 10): ❌ Run #39910411709