Skip to content

[DSV4] Fix silent KV corruption when speculative draft tokens > 4 - #34189

Merged
hnyls2002 merged 5 commits into
mainfrom
lsyin/dsv4-compress-pad
Aug 9, 2026
Merged

hnyls2002 merged 5 commits into
mainfrom
lsyin/dsv4-compress-pad

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

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_tokens but rolls back to prefix_len + accept_len, so every committed token must stay resident whatever the accept length turns out to be.

c_plan.cuh capped that pad at a hard-coded 4:

constexpr int32_t kMaxMTPDraftTokens = 4;
const auto mtp_pad = std::min(ring_size - compress_ratio, kMaxMTPDraftTokens);

With --speculative-num-draft-tokens > 4 the 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 w aliases onto w - ring_size, and the earliest position a future compression can still need is prefix_len - window_size + 2 (the next batch commits at least one token, and run_prefill launches the compress kernel before the write kernel, so a batch's own compressions read the pre-write ring). So the ring's capacity is ring_size - window_size + 2, and the plan can simply always pad that far:

const auto mtp_pad = ring_size > window_size ? ring_size - window_size + 2 : 0;

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_kernel0 branches and the C++ host loop) now share this value, fixing the CPU/GPU divergence.

Capacity is validated at startup. DSV4PoolConfigurator rejects a draft count the ring cannot serve, using max_speculative_num_draft_tokens since 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 <= 10 for c4 and <= 130 for 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 of seq_len % compress_ratio, assert plan_w covers 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 for D > 4.
  • test_cpu_and_gpu_planner_agree -- pins plan_compress_prefill_kernel0 to 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 failing cases before the fix
test_c4_ring_residency D = 5, 9, 10 (each across 4 residues of seq_len % 4)
test_c128_ring_residency D = 129, 130
test_cpu_and_gpu_planner_agree compress_ratio=128, D=130

Two things worth calling out. c4 starts losing positions at D=5, exactly D > kMaxMTPDraftTokens — the boundary is sharp, which is why an ndt of 4 or 2 never sees it. And c128 only breaks near the ring's capacity: its first_w_pos = min(last_c_pos, seq_len - pad) usually takes last_c_pos (as seq_len % 128 is 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.

GSM8K (200) GPQA diamond (198, 5 rounds) latency throughput
before 0.990 0.7172 65.1s 354.8 tok/s
after 0.985 0.7374 64.7s 352.7 tok/s

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=0 over 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

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-test test_deepseek_v4_compress_plan_draft_pad.py test_dsv4_compress_write_pad.py

@github-actions

github-actions Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test_deepseek_v4_compress_plan_draft_pad.py test_dsv4_compress_write_pad.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_deepseek_v4_compress_plan_draft_pad.py

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

cd test/ && python3 registered/unit/mem_cache/test_dsv4_compress_write_pad.py

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Aug 9, 2026
@hnyls2002
hnyls2002 merged commit 4a5d7d3 into main Aug 9, 2026
134 of 177 checks passed
@hnyls2002
hnyls2002 deleted the lsyin/dsv4-compress-pad branch August 9, 2026 22:54
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
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants