Skip to content

[Fix] dsa: Fix ragged seed indices before MTP draft decode - #42565

Merged
BBuf merged 6 commits into
sgl-project:mainfrom
cognaiger9:fix/dsa-seed-handoff-mtp
Oct 16, 2026
Merged

BBuf merged 6 commits into
sgl-project:mainfrom
cognaiger9:fix/dsa-seed-handoff-mtp

Conversation

@cognaiger9

@cognaiger9 cognaiger9 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fused DSA prefill can produce top-k indices into the batch’s flattened logical KV space. The draft model captures these indices as MTP seeds and reuses them in the next draft decode, which expects physical KV slots.
This mismatch can select incorrect KV entries or cause out-of-bounds reads, particularly when long requests share cached prefixes and logical offsets exceed physical cache capacity.

The engine config that exercises this error:

SGLANG_DSA_FUSE_TOPK=1 \
python3 -m sglang.launch_server \
  --model-path /models/PhalaCloud/GLM-5.3-W4AFP8 \
  --quantization w4afp8 \
  --tp 8 \
  --mem-fraction-static 0.75 \
  --kv-cache-dtype fp8_e4m3 \
  --dsa-prefill-backend flashmla_sparse_q8 \
  --speculative-algorithm EAGLE \
  --speculative-num-steps 3 \
  --speculative-eagle-topk 1 \
  --speculative-num-draft-tokens 4

Modifications

  • Add prepare_mtp_seed_indices() to map fused RAGGED seeds through the producer’s page table before capture.

Accuracy Tests

CPU: tests added in test/registered/unit/layers/attention/test_index_topk_share.py

Speed Tests and Profiling

The conversion applies only to fused RAGGED seed export at the draft-prefill (EXTEND) → next draft-decode (DECODE) boundary. Post-verification draft extend (DRAFT_EXTEND_V2) already exports PAGED seeds and requires no conversion.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): 🚫 Run #38691837325
Latest PR Test (Extra): ❌ Run #38691837344
Latest PR Test (AMD ROCm 10): ❌ Run #38691837469

@cognaiger9
cognaiger9 force-pushed the fix/dsa-seed-handoff-mtp branch from 3d35505 to d1be205 Compare October 7, 2026 07:42
@cognaiger9 cognaiger9 changed the title [BugFix] Fix ragged DSA seed indices before MTP draft reuse [BugFix] Fix ragged DSA seed indices before MTP draft decode Oct 7, 2026
@cognaiger9
cognaiger9 marked this pull request as ready for review October 7, 2026 08:23
@aistackdev

aistackdev commented Oct 9, 2026 •

Copy link
Copy Markdown

@Fridge003 @ispobock could you take a look when you have time?

We hit this in production with this PR applied as a hotfix, and it does not fully fix the issue.

Setup: GLM-5.3-W4AFP8, TP8 / DP4 dp-attention, EAGLE 3/1/4, fp8 KV, flashmla_sparse_q8 prefill, fused top-k (default). The engine still crashed with Triton Error [CUDA]: an illegal memory access was encountered in EAGLE verify (eagle_prepare_for_verify -> dsa_backend._apply_cuda_graph_metadata -> fused_dsa_target_verify_metadata). This happened right after the KV router sent a burst of ~10 requests sharing a ~220k-token cached prefix to one DP rank.

The gap: this PR converts seeds only on the MHA path (forward_mha.py). MHA is used only when kv_len <= SGLANG_DSA_PREFILL_DENSE_ATTN_KV_LEN_THRESHOLD (2048 by default). Longer prefills go through MLA sparse, and there the draft model exports seeds through IndexTopKShareState.publish() in index_topk_share.py. That method still copies the RAGGED (batch-global logical) indices into dsa_seed_topk_capture unchanged. So the long-context case this PR describes (logical offsets exceeding physical KV capacity) is still not covered. I think prepare_mtp_seed_indices() needs to be applied in the seed_buf branch of publish() as well.

(Code paths read from our deployed image, sglang-runtime 1.5.0. We have not reproduced it on main yet.)

Workaround we are running now: SGLANG_DSA_FUSE_TOPK=0. With it, seeds stay request-local and draft decode maps them through the page table. (does not work, see update below)


Update (2026-10-09):

  1. SGLANG_DSA_FUSE_TOPK=0 is not a valid workaround. With it, the engine crashed about 2 min after startup on the first long prefill. The error was RuntimeError: The size of tensor a (742) must match the size of tensor b (741) at dsa_backend.py forward_extend (topk_indices + topk_indices_offset) during _draft_extend_for_prefill. On the unfused RAGGED path, topk_indices is padded to the q row count by _pad_topk_indices, but topk_indices_offset is not padded. This is a separate bug from the one this PR fixes.

  2. The latest commit (6ad90aae, write_mtp_seed on the sparse-MLA publish() path) works for us. We backported the three files onto our sglang-runtime 1.5.0 image (applied cleanly) and ran a burst test: 10 concurrent requests sharing a ~220k-token cached prefix, 3 rounds. Results:

    • 30/30 requests OK, no engine restart, no IMA.
    • The engine logs show prefill batches with #new-seq: 4, #cached-token: 882432, so the batch's logical sum(seq_lens) (~890k) exceeds the per-rank KV pool (756,544 tokens). That is the condition that crashed the engine before this commit.
    • EAGLE accept length is unchanged (~2.8).

Minor review notes:

  • write_mtp_seed falls back silently when the resolved backend has no prepare_mtp_seed_indices. If a wrapper backend is ever returned there, the old bug would come back without any signal. An assert, or a one-time warning, for the fused + EXTEND case would make that visible.
  • A GPU regression test covering many long shared-prefix requests in one extend batch (sum(seq_lens) > pool size) would lock this in.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the MHA export and IndexTopKShareState.publish now use the seed conversion, and the CPU mapping tests cover the intended representation change. Could we add one GPU regression that exercises the real prefill-to-first-draft-decode transition with SGLANG_DSA_FUSE_TOPK=1 and a shared cached prefix, where ragged logical offsets differ from physical KV slots? Please cover both the MHA and sparse-MLA seed export paths and compare attention outputs or logprobs with a reference. The current tests mock backend selection and the index producer, so they do not yet validate that producer/consumer handoff.

@BBuf

BBuf commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/layers/attention/test_index_topk_share.py

Targeted validation for 6ad90aae3303. This CPU coverage does not replace the GPU seed-to-draft integration regression requested in review.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/layers/attention/test_index_topk_share.py

@BBuf BBuf added the run-ci CI: run the baseline test suite on this PR label Oct 11, 2026
@cognaiger9
cognaiger9 force-pushed the fix/dsa-seed-handoff-mtp branch from c5e290f to 7190ed1 Compare October 11, 2026 14:46
@cognaiger9

Copy link
Copy Markdown
Contributor Author

Both the MHA export and IndexTopKShareState.publish now use the seed conversion, and the CPU mapping tests cover the intended representation change. Could we add one GPU regression that exercises the real prefill-to-first-draft-decode transition with SGLANG_DSA_FUSE_TOPK=1 and a shared cached prefix, where ragged logical offsets differ from physical KV slots? Please cover both the MHA and sparse-MLA seed export paths and compare attention outputs or logprobs with a reference. The current tests mock backend selection and the index producer, so they do not yet validate that producer/consumer handoff.

@BBuf thanks for reviewing, i just added a GPU regression test, which covers the handoff from draft prefill to the first draft decode end to end on real kernels. With SGLANG_DSA_FUSE_TOPK=1 and an fp8 KV cache, it runs the real indexer and fused top-k, the seed export on both paths (dense MHA and sparse MLA, with the backend's own routing choosing between them), and the flashmla_kv decode. Requests share cached-prefix pages scattered across the pool, so the ragged logical offsets exceed the KV pool, which is the shape of the production crash. The test checks that every seed index stays inside the pool before any kernel reads it, that the seed exactly matches the physical slots from an independent offset-to-slot mapping, and that decode attention matches an fp32 torch reference.

@BBuf

BBuf commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Can we clean up the test files? The test files change is too large.

@cognaiger9
cognaiger9 force-pushed the fix/dsa-seed-handoff-mtp branch from 7190ed1 to bf4f910 Compare October 15, 2026 04:33
cognaiger9 and others added 6 commits October 15, 2026 13:20
Draft prefill longer than index_topk runs sparse MLA and exports its seed
through IndexTopKShareState.publish(), which copied fused RAGGED
(batch-global logical) top-k indices unchanged. Draft decode reads them as
physical KV slots, so long shared prefixes overran the KV pool and caused
illegal memory accesses.

Route both MHA and sparse-MLA seed exports through write_mtp_seed(), which
applies the backend's prepare_mtp_seed_indices conversion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the standalone GPU test with a kit helper built on
DSAMockModelRunner and build_dsa_sparse_attention_fixture, with two
cases in test_dsa.py covering the dense-MHA and sparse-MLA export paths.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@cognaiger9
cognaiger9 force-pushed the fix/dsa-seed-handoff-mtp branch from bf4f910 to 017956a Compare October 15, 2026 06:20
@cognaiger9

Copy link
Copy Markdown
Contributor Author

Can we clean up the test files? The test files change is too large.

@BBuf agreed it was too much, i dropped the standalone GPU test file and moved the check into the existing DSA attention fixture instead

@cognaiger9 cognaiger9 changed the title [BugFix] Fix ragged DSA seed indices before MTP draft decode [Fix] Fix ragged DSA seed indices before MTP draft decode Oct 15, 2026
@cognaiger9 cognaiger9 changed the title [Fix] Fix ragged DSA seed indices before MTP draft decode [Fix] dsa: Fix ragged seed indices before MTP draft decode Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants