Skip to content

[Fix] [NVIDIA] deepseek-v4.1: Fix the breakable prefill CUDA graph in the late-layer tail on SM90 - #45407

Merged
hnyls2002 merged 9 commits into
sgl-project:mainfrom
JohnZhao01:fix/dsv41-sm90-prefill-graph-tail
Oct 23, 2026
Merged

hnyls2002 merged 9 commits into
sgl-project:mainfrom
JohnZhao01:fix/dsv41-sm90-prefill-graph-tail

Conversation

@JohnZhao01

@JohnZhao01 JohnZhao01 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On H200 (SM90), DeepSeek-V4.1-Flash with decoder SWA bounded replay (on by default; the cookbook lists breakable prefill as supported with it) cannot serve with --cuda-graph-backend-prefill breakable. The late-layer tail fails in two places, both on paths that SM100 does not take: SM90 has no in-graph low-ratio path (low_ratio_prefill_graph is false), so the ratio-1/2 sources run as eager breaks or, in one place, get captured.

  1. Warmup before capture. The first faulting kernel is the device-side assert in torch.repeat_interleave (Repeat.cu:20: result_size == cumsum_ptr[size - 1]); the CUDA 719 and DeepGEMM errors that follow are knock-on errors. Call chain: bcg_deepseek_v4_low_ratio_sources → forward_low_ratio_sources → token_req_indices → repeat_interleave(req, extend_seq_lens, output_size=positions.shape[0]). Inside the tail, the eager break sees only the tail's live rows, while forward_batch.extend_seq_lens still spans the whole extend. The hoisted low_ratio_req_indices / low_ratio_pos_i64 already hold the tail's rows (live rows first, bucket-sized), but they are only used when their length equals the break's row count, which it does not inside an eager break.
  2. Replay of the first real request. With (1) fixed, capture and startup succeed, and the first real prefill asserts in a replayed graph: the output_size argument (8) must be the same as the sum of the elements in the repeats tensor (6). The source-layer KV write at the start of the tail (write_tail_source_kv) is called directly from the model, not through an eager break, so on SM90 its token_req_indices and the host-side compressor are captured with the capture-time row count.

This still reproduces on main @ c1c287a4: without this change, the breakable launch below crashes while capturing the prefill graph with the same Repeat.cu:20 assert.

#45381 fixes a different capture failure of the same low-ratio prefill-graph path on SM12x.

Modifications

  • deepseek_v4_backend.py (forward_low_ratio_sources): when the hoisted rows are longer than the break's rows in an extend, take their live prefix instead of rebuilding the rows from extend_seq_lens. Bucket-sized calls (the in-graph path) and calls without hoisted rows are unchanged.
    • The hoisted rows are longer only under the prefill graph, where both the prefill-graph metadata and the tail metadata store the live rows first and the padding after them. Outside the tail the live prefix therefore equals what token_req_indices computes; inside the tail it is the only correct value. The prefill-graph full top-k break (_prefill_graph_full_topk_break) already takes low_ratio_req_indices[:real] the same way. If a narrower condition is preferred, it can check the tail metadata explicitly; happy to switch.
  • deepseek_v4.py: a new eager break, bcg_deepseek_v4_write_tail_source_kv, slices the source-layer input to the live rows like deepseek_v4_low_ratio_sources, with the same capture-stub contract as _low_ratio_sources_capture_stub. The tail's source-KV write uses it inside a breakable prefill graph when the backend has no in-graph low-ratio path; every other configuration keeps the direct call. The condition reads low_ratio_prefill_graph the same way as the existing low-ratio call site in _forward_prepare. With no live rows it skips the write but still sets tail_source_kv_layer_id, as write_tail_source_kv does, so the source layer's own pass inside the tail keeps skipping its compressor. Rebased onto main @ c1c287a4, where [Perf] [AMD] dsv41: Skip unneeded layer 20 computation under decoder-bounded-replay #45339 computes source_x at the same call site with a HIP branch; that computation is kept and only the call after it is dispatched.
  • Tests (run on H200 against main @ c1c287a4):
    • TestDSV4LateLayerTailLowRatioRows checks that a tail-sized eager break gets the live prefix of the hoisted rows for both the compressor and the indexer. It fails before this change (RuntimeError: allocated size does not match required size from repeat_interleave, the rows rebuilt from extend_seq_lens not matching the break's row count) and passes after.
    • TestDSV4WriteTailSourceKvBreak checks that the source-KV break passes only the first global_num_token_non_padded_cpu rows of x and positions to write_tail_source_kv, and that with zero live rows it does not call it but still marks the source layer. It covers a function this PR adds, so it has no before/after comparison; it passes.

Accuracy Tests

Setup: DeepSeek-V4.1-Flash on 8×H200, image lmsysorg/sglang:nightly-dev-cu13-20261021-c1c287a4 (main @ c1c287a4), with this PR's commits applied to the image's source tree for the "this change" column.

Launch flags: --tp 8 --attention-backend dsv4 --moe-runner-backend marlin --mem-fraction-static 0.70 --chunked-prefill-size 16384 --prefill-decode-interval 16 --enable-decoder-swa-bounded-replay --swa-prefix-tails 1024 --speculative-algorithm DSPARK --speculative-dspark-block-size 5 --speculative-num-steps 1 --speculative-eagle-topk 1 --speculative-num-draft-tokens 6 --enable-hierarchical-cache --hicache-ratio 2 --language-model-only, plus --cuda-graph-backend-prefill breakable --cuda-graph-max-bs-prefill 4096 for the graph runs. "Eager" is the same launch without the two prefill-graph flags.

eager (no prefill graph) breakable, before this change breakable, this change
startup ready crashes while capturing the prefill graph (Repeat.cu:20 assert, item 1) ready
GSM8K, sgl-eval 0.1.3, 1,319 questions, model preset (thinking on, reasoning effort high, T=1.0, top_p 0.95) 0.9704 — 0.9697
GSM8K, lm-eval with the InferenceX setup, 5-shot, flexible / strict 0.9735 / 0.9742 — 0.9727 / 0.9735
long-context retrieval, 6 prompts 6/6 — 6/6
max_total_num_tokens 23,892,736 — 23,892,736
smoke: 15 real prefills, 1 to 30,000 words (9 single, 6 concurrent, covering the padded bucket sizes) 15/15 — 15/15
  • Server logs of all runs of both columns have no assert and no CUDA error.
  • Which requests exercise the changed path: under decoder SWA bounded replay every prefill extend runs the late layers over each request's last min(extend length, 128) rows, 128 being the model's sliding window (SWA_WINDOW), unless it asks for prompt logprobs or every hidden state; speculative verify extends do not run the tail. (--swa-prefix-tails 1024 is unrelated: it sizes how many radix-cached prefix tails the SWA KV pool keeps room for.) In a separate run of this change with a counting log added (not part of this PR), sgl-eval GSM8K (200 questions) and the smoke ran 40 prefill forwards through the tail after startup: 31 were graph replays (up to 3,447 tail rows), which run the two eager breaks changed here, and 9 ran eager (batches over the 4,096-token capture cap, or chunks whose context exceeds the 16K replay limit).
  • Earlier measurements on nightly-dev-cu13-20261018-b234b5c3, before the rebase: GSM8K (InferenceX setup) 0.9727 / 0.9735 with this change and eager, a second pair with --max-running-requests 64 at 0.9742 / 0.9742, long-context retrieval 6/6, smoke 15/15.
  • Prompt logprobs (on b234b5c; 21 fixed prompts, 40 to 12,000 words, 76,144 prompt tokens, /generate with return_logprob and logprob_start_len 0, T=0): the graph-vs-eager difference (mean |Δlogprob| 0.158 / 0.160) is the same as the run-to-run difference of each configuration (0.159 eager, 0.157 graph). Requests for prompt logprobs run the late layers over the whole prompt rather than the bounded tail, so this check covers the graph's numerics but not the tail path changed here; the tail path is covered by the accuracy rows and the smoke above.

--cuda-graph-max-bs-prefill 4096: with the default capture sizes the graph pool takes about 29.6 GB at --mem-fraction-static 0.70, and the first eager chunk of a 128K cold prefill (chunks past the 16K replay limit run eager) ran out of memory in the eager torch indexer scoring (measured on b234b5c). Capping the capture at 4,096 tokens keeps the pool at 8.75 GB with the same KV pool size. That is a memory-headroom setting for this configuration, not part of the fix: with the default capture sizes the fix also captures, starts and serves the same smoke prefills without errors, with the same KV pool size, long-context retrieval 6/6 and GSM8K 0.9697 (within the ±0.0047 standard error); only the 128K cold prefill ran out of memory.

Speed Tests and Profiling

On main @ c1c287a4 (single requests, same server as the accuracy rows):

eager this change (breakable, bs 4096)
128K cold prefill (131,071 tokens) TTFT 6.30 s 6.05 s
decode TPOT p50, B = 1 / 8 / 16 2.15 / 4.80 / 6.39 ms 2.18 / 4.69 / 6.60 ms
262K-token request with 4 short requests behind it: long TTFT 19.68 s 19.69 s

Measured earlier on b234b5c:

Cold-prefill TTFT (random-word prompts, no cache hit, max_tokens 1, median of 3; the nominal sizes are estimated at 2.3 words per token, so actual token counts are somewhat larger):

nominal prompt eager this change (breakable, bs 4096)
512 0.202 s 0.079 s
2K 0.201 s 0.150 s
8K 0.384 s 0.368 s
16K 0.776 s 0.784 s
32K 1.587 s 1.605 s
128K 9.310 s 9.380 s

Short prefills lose a fixed ~0.2 s of eager launch overhead. Prefill chunks whose context exceeds 16K still run eager on SM90 (#43733 lifts that limit only on SM100), so long prefills are unchanged.

Load test, 900 s per point after a 600 s warmup, InferenceX agentic traces (SLO lines TTFT p90 ≤ 5,750 ms, TPOT p90 ≤ 34.5 ms), same flags as above with --max-running-requests 64:

max concurrency within SLO total throughput at that point
eager 10 56,550 tok/s (TTFT p90 1,902 ms, TPOT p90 19.7 ms)
this change 16 83,542 tok/s (TTFT p90 3,220 ms, TPOT p90 33.4 ms)

Checklist

This PR was prepared with AI assistance (Claude Code). The submitter reviewed every changed line and the validation above.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ⏳ Run #39880852240
Latest PR Test (Extra): ❌ Run #39880852054
Latest PR Test (AMD ROCm 10): ⏳ Run #39880852335

JohnZhao01 and others added 2 commits October 21, 2026 17:44
… the late-layer tail on SM90

Without the in-graph low-ratio path (SM90), the late-layer tail of decoder SWA
bounded replay broke the breakable prefill graph twice: the tail's eager
low-ratio break rebuilt its rows from the whole extend's extend_seq_lens
(repeat_interleave assert during warmup), and the source-layer KV write at the
start of the tail was captured with the capture-time row count (assert on
replay). Take the live prefix of the hoisted rows in that break, and run the
source-layer KV write as an eager break over the live rows.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Cover deepseek_v4_write_tail_source_kv: it writes only the extend's live rows,
and with no live rows it skips the write but still marks the source layer, as
write_tail_source_kv does, so the layer's own pass inside the tail skips its
compressor. Comment the zero-row branch accordingly.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@JohnZhao01
JohnZhao01 force-pushed the fix/dsv41-sm90-prefill-graph-tail branch from 8edbe4b to 09c246d Compare October 21, 2026 15:31
@JohnZhao01
JohnZhao01 marked this pull request as ready for review October 21, 2026 15:33
@hnyls2002

hnyls2002 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

/rerun-test test_deepseek_v4.py test_dsv41_late_layer_tail_readers.py test_encoder_swa_replay.py test_megakernel_binding.py test_deepseek_v41_flash_gb300_tp2ep2.py test_deepseek_v41_flash_gb300_dp2ep2.py test_deepseek_v41_flash_gb300_encoder_bcg.py test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py test_dsv41_prefill_sparse_indexer.py test_dsv41_candidate_blocks.py test_dsv4_nonpaged_indexer.py test_dsv41_candidate_graph_variants.py test_prefill_cuda_graph_runner.py
(3 tries)

@hnyls2002

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py
cd test/ && python3 registered/kernels/ops/attention/test_dsv41_prefill_sparse_indexer.py

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

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py

🚀 ubuntu-latest (7 tests): ✅ View workflow run

cd test/ && python3 registered/unit/layers/test_dsv41_late_layer_tail_readers.py
cd test/ && python3 registered/unit/model_executor/test_encoder_swa_replay.py
cd test/ && python3 registered/unit/megakernels/test_megakernel_binding.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_blocks.py
cd test/ && python3 registered/unit/layers/test_dsv4_nonpaged_indexer.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_graph_variants.py
cd test/ && python3 registered/unit/model_executor/test_prefill_cuda_graph_runner.py

🚀 2-gpu-gb300 (4 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_tp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_dp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_encoder_bcg.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py
cd test/ && python3 registered/kernels/ops/attention/test_dsv41_prefill_sparse_indexer.py

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

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py

🚀 ubuntu-latest (7 tests): ✅ View workflow run

cd test/ && python3 registered/unit/layers/test_dsv41_late_layer_tail_readers.py
cd test/ && python3 registered/unit/model_executor/test_encoder_swa_replay.py
cd test/ && python3 registered/unit/megakernels/test_megakernel_binding.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_blocks.py
cd test/ && python3 registered/unit/layers/test_dsv4_nonpaged_indexer.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_graph_variants.py
cd test/ && python3 registered/unit/model_executor/test_prefill_cuda_graph_runner.py

🚀 2-gpu-gb300 (4 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_tp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_dp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_encoder_bcg.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py
cd test/ && python3 registered/kernels/ops/attention/test_dsv41_prefill_sparse_indexer.py

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

cd test/ && python3 registered/attention/unittests/dsv4/test_deepseek_v4.py

🚀 ubuntu-latest (7 tests): ✅ View workflow run

cd test/ && python3 registered/unit/layers/test_dsv41_late_layer_tail_readers.py
cd test/ && python3 registered/unit/model_executor/test_encoder_swa_replay.py
cd test/ && python3 registered/unit/megakernels/test_megakernel_binding.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_blocks.py
cd test/ && python3 registered/unit/layers/test_dsv4_nonpaged_indexer.py
cd test/ && python3 registered/unit/layers/test_dsv41_candidate_graph_variants.py
cd test/ && python3 registered/unit/model_executor/test_prefill_cuda_graph_runner.py

🚀 2-gpu-gb300 (4 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_tp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_dp2ep2.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_encoder_bcg.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py

@hnyls2002
hnyls2002 merged commit 26396bf into sgl-project:main Oct 23, 2026
85 of 122 checks passed
At1a8 added a commit to At1a8/sglang that referenced this pull request Oct 23, 2026
…io_sources

sgl-project#45407 slices q_lora with [:n] on the hoisted-rows path of
forward_low_ratio_sources. On gfx950 the indexer's wq_b operand is an
Mxfp8Activation or Fp8GridActivation, which refuses slicing, so a DeepSeek-V4.1
DSpark server crashes while capturing the target-verify CUDA graph. Move
_first_rows from the HIP backend into the shared backend and take the rows
through it.
kevin-mii pushed a commit to kevin-mii/sglang that referenced this pull request Oct 23, 2026
…rows

Simpler form of the fix: forward_low_ratio_sources cuts x and q_lora only when
the step has rows past the hoisted ones (DP attention padding an eager extend),
as before sgl-project#45407; in every other case they already have n rows. That cut keeps
HIP's quantized activations whole, field by field. write_tail_source_kv's input
is a bf16 tensor (fused_boundary_attn_input), so it keeps sgl-project#45407's check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kevin-mii pushed a commit to kevin-mii/sglang that referenced this pull request Oct 23, 2026
… rows

The all-rows-live pass-through (the crash sgl-project#45407 caused) and the DP-padding cut are
separate cases, each with the failure it prevents in its docstring. The hoisted
req / pos assertions go: test_tail_break_uses_live_prefix_of_hoisted_rows pins them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants