Repository navigation
[DSV4] Fix silent KV corruption when speculative draft tokens > 4 - #34189
Merged
Merged
Conversation
hnyls2002
requested review from
BBuf,
DarkSharpness,
Fridge003,
HydraQYH,
Ying1123,
alphabetc1,
celve,
hanming-lu,
hzh0425,
ispobock,
merrymercy,
xiezhq-hermann,
yizhang2077 and
yuan-luo
as code owners
August 9, 2026 19:01
Collaborator
Author
|
/rerun-test test_deepseek_v4_compress_plan_draft_pad.py test_dsv4_compress_write_pad.py |
Contributor
|
Results for 🚀 🚀 |
Collaborator
Author
|
/tag-and-rerun-ci |
Leoyzen
added a commit
to Leoyzen/sglang
that referenced
this pull request
Aug 10, 2026
…rify Skip idle/padded requests (verify_len=0) in the C++ compress-plan loops in c_plan.cuh and c128_online_v2.cuh. Compact mode pads the batch to the CUDA graph bucket size, so DSpark ragged path passes verify_lens_cpu with zeros, tripping RuntimeCheck(0<extend_len) and crashing on the first request. Ports the guard from dsv4-9303e26-extra (cf54594) onto the integrated branch; keeps the sgl-project#34189 mtp_pad write-retention mechanism intact.
ShangmingCai
added a commit
that referenced
this pull request
Aug 10, 2026
…raft tokens > 4 Cherry-pick of upstream #34189 (merged, squash 4a5d7d3). Replaces the earlier open-PR #33872 attempt: instead of threading num_draft_tokens through the backend/compressor, the compress write-pad is reframed as a property of the ring (ring_size - window_size + 2) in c_plan.cuh, with startup validation (_assert_ring_serves_draft_tokens in pool_configurator.py) that rejects over-configured draft counts. c_plan.cuh is JIT-compiled at runtime, so this needs no kernel rebuild. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
saturn-acc
pushed a commit
to saturn-acc/sglang
that referenced
this pull request
Aug 16, 2026
…l-project#34189) Co-authored-by: Deleter-D <[email protected]>
5 tasks done
4 tasks done
Atituiset
pushed a commit
to Atituiset/sglang
that referenced
this pull request
Sep 10, 2026
…l-project#34189) Co-authored-by: Deleter-D <[email protected]>
Open
5 tasks done
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.
Supersedes #33872.
Motivation
The compress write plan decides which tokens' raw KV get persisted into the compress-state ring for a future compression window to read. A speculative verify batch plans from the optimistic
seq_len = prefix_len + num_draft_tokensbut rolls back toprefix_len + accept_len, so every committed token must stay resident whatever the accept length turns out to be.c_plan.cuhcapped that pad at a hard-coded 4:With
--speculative-num-draft-tokens > 4the plan under-writes the ring, and a later compression reads stale slots. This fails silently -- no illegal access, no NaN, no assertion, just wrong compressed state. The host planner path was additionally missing the pad term entirely, so the CPU and GPU planners disagreed.Modifications
The pad is a property of the ring, not of the batch. A write at
waliases ontow - ring_size, and the earliest position a future compression can still need isprefix_len - window_size + 2(the next batch commits at least one token, andrun_prefilllaunches the compress kernel before the write kernel, so a batch's own compressions read the pre-write ring). So the ring's capacity isring_size - window_size + 2, and the plan can simply always pad that far:Padding past a batch's own extend range is harmless -- the planner loops only span
[prefix_len, seq_len). A non-speculative ring is exactly one window wide, so the pad is 0 there and the write set is unchanged.All three planner paths (both
plan_compress_prefill_kernel0branches and the C++ host loop) now share this value, fixing the CPU/GPU divergence.Capacity is validated at startup.
DSV4PoolConfiguratorrejects a draft count the ring cannot serve, usingmax_speculative_num_draft_tokenssince the ring is sized once and must serve the largest adaptive tier. The planner itself cannot do this -- it cannot tell an over-configured verify batch from an ordinary long prefill -- so an over-configured server now fails at startup instead of corrupting state at run time.The current rings serve
num_draft_tokens <= 10for c4 and<= 130for c128. Raising the c4 bound means growing the ring, which scales the c4 state pool; that tradeoff is left out of this PR.Accuracy Tests
test_deepseek_v4_compress_plan_draft_pad.py(kernel, ~8s in CI):test_c4_ring_residency/test_c128_ring_residency-- for every draft count the ring serves, crossed with residues ofseq_len % compress_ratio, assertplan_wcovers all of[prefix_len, seq_len). This is the sufficient condition: if each step writes its whole extend range, the next compression window is resident by induction whatever the accept length. Reverting the pad turns these red forD > 4.test_cpu_and_gpu_planner_agree-- pinsplan_compress_prefill_kernel0to the host-loop plan, so the fix has to hold on both paths.test_plain_prefill_write_set-- non-speculative rings produce the pre-existing write set for both ratios, i.e. no change for non-speculative serving.test_over_capacity_under_writes-- past the ring capacity the plan silently under-writes, which is why the bound is enforced at startup rather than in the planner.test_dsv4_compress_write_pad.py(CPU unit, ~1s) covers the capacity helper: 0 without speculation, 10/130 for the speculative rings, and 0 for the online-c128 ring of 1.The kernel test fails on the pre-fix planner
A regression test that also passes on the buggy code proves nothing, so this was checked directly — running the test file above against the pre-fix planner gives 19 failures:
test_c4_ring_residencyseq_len % 4)test_c128_ring_residencytest_cpu_and_gpu_planner_agreeTwo things worth calling out. c4 starts losing positions at D=5, exactly
D > kMaxMTPDraftTokens— the boundary is sharp, which is why anndtof 4 or 2 never sees it. And c128 only breaks near the ring's capacity: itsfirst_w_pos = min(last_c_pos, seq_len - pad)usually takeslast_c_pos(asseq_len % 128is typically much larger than the pad), which already reaches far enough back. The exposure is concentrated in the c4 layers.The third row is a separate defect the pre-fix code also had: the C++ host loop carried no pad term at all, only the GPU kernel did, so the two planner paths emitted different write sets.
End-to-end
DeepSeek-V4-Flash + DSPARK at
--speculative-dspark-block-size 7(D=8, past the old cap of 4), TP=4,--moe-runner-backend marlin; two servers launched identically apart from the checkout.Plus an opaque-id echo probe (200 requests, 4000-token prefix, greedy): 200/200 exact on both.
These numbers show no regression; they are not evidence the fix works. GPQA leans positive (4 of 5 rounds, +0.02 mean) but is not significant — paired t = 2.0 at df=4, p ~ 0.12. At
temperature=0over a fixed question set the five rounds should have been identical yet spanned ~4.5pp, so concurrent-batch nondeterminism puts the noise floor above the effect size.That the fix is not visible end-to-end is expected. The skipped positions are the head of the draft tail, and consecutive verify batches have heavily overlapping write sets, so the next step usually rewrites what the previous one skipped. Corruption needs a position to stay unwritten until a compression actually reads it, which depends on accept-length distribution, sequence length, and compression-point alignment lining up.
Speed Tests and Profiling
No performance impact expected. The change replaces a compile-time constant with an expression over values the planner already receives; no extra memory traffic, no new launches, no change to the compress kernels. A speculative verify batch writes more rows than before, which is the fix, bounded by the ring capacity.
Checklist
c_plan.cuhcomment and the test module docstring.