Skip to content

[Fix] dsa: Isolate masked KV rows in FP8 TileLang attention - #45988

Closed
HanHan009527 wants to merge 2 commits into
sgl-project:mainfrom
bytedance-iaas:codex/fix/dsa-tilelang-masked-kv
Closed

HanHan009527 wants to merge 2 commits into
sgl-project:mainfrom
bytedance-iaas:codex/fix/dsa-tilelang-masked-kv

Conversation

@HanHan009527

@HanHan009527 HanHan009527 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Invalid (-1) sparse indices in sparse_mla_fwd_decode_partial_fp8 map 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

  • Zero invalid KV rows before the FP8 kernel's QK and PV matrix multiplications, including the optional RoPE tail. Keep the existing score mask and valid KV values unchanged.
  • Add poisoned-padding regressions for 256+0, 512+0, and 512+64 geometries, eager and CUDA Graph execution, and all-masked outputs remaining zero.

Validation

Check Result
Poisoned-padding regression before the fix Failed for all three geometries
Full existing kernel test file on H20 after the fix 6 tests passed, including eager/Graph and all-masked cases
Original GLM deployment, changing only the Decode kernel PP1 and PP2 probes each recovered to 8/8 requests with finite logprobs; MTP5/1/6, Replay16, full CUDA Graph and overlap retained
Small integrated quality checks 6/6 answers correct; a 65K-token retrieval request returned the expected answer with finite logprobs
Changed-file pre-commit on the refreshed main base Passed

GPU results were collected on base 8d8916815b9f2717a34dac16190157189e54fb57 plus fix 985608ef7795830643a2fa74f5f287c286640643. The delivery branch merges main 3f36fc08331f64cd63c4fbbdf39cc402f38bb38b; 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:

PYTHONPATH=python python -m pytest -q test/registered/kernels/ops/attention/test_tilelang_dsa_zero_rope.py

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

  • Format changed files with pre-commit.
  • Add a deterministic GPU regression and record pre-fix failure.
  • Record GPU and service validation scope.
  • Upstream CI and maintainer review.

CI States

Latest PR Test (Base): ❌ Run #39910411746
Latest PR Test (Extra): ❌ Run #39910411642
Latest PR Test (AMD ROCm 10): ❌ Run #39910411709

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

QNan0513 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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

tilelang_sparse_fwd with FP8 KV, d_v 512, no RoPE tail, top-k 2112 with 2051 live entries, 1M-row KV with random rows, on one H20. Times are the median of 7 CUDA-graph replays, averaged over 2–3 runs per build:

Heads Tokens main this PR variant above
16 1 19.4 µs 24.9 µs (1.28×) 19.4 µs
16 16 49.0 µs 109.9 µs (2.24×) 49.1 µs
16 64 167.4 µs 513.1 µs (3.06×) 167.0 µs
16 256 640.1 µs 1,956 µs (3.06×) 639.4 µs
16 2048 4,098 µs 12,480 µs (3.05×) 4,102 µs
64 64 544.2 µs 1,623 µs (2.98×) 545.3 µs

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

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