Skip to content

[Perf] deepseek-v4.1: Skip wasted layer 20 work on earlier prompt tokens under decoder bounded replay - #42901

Merged
hnyls2002 merged 7 commits into
sgl-project:mainfrom
vincentzed:dsv41-layer20-tail
Oct 17, 2026
Merged

hnyls2002 merged 7 commits into
sgl-project:mainfrom
vincentzed:dsv41-layer20-tail

Conversation

@vincentzed

@vincentzed vincentzed commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

  • DeepSeek-V4.1 has an option called decoder bounded replay (--enable-decoder-swa-bounded-replay). When it is on, the last layers of the model only process the last 128 prompt tokens of each request during prefill.
  • Today this starts at layer 21. Layer 20 still processes every prompt token.
  • Layer 20 does two things. It writes the compressed KV cache that decoding reads later, and that needs every token. It also runs its own indexer, attention and MoE to produce its output, and layer 21 only reads the last 128 tokens of that output.
  • So the indexer, attention and MoE work of layer 20 on all the earlier tokens is thrown away. The indexer of layer 20 scans the whole context, so this wasted work grows with the prompt length.

Modifications

  • When decoder bounded replay is on, layer 20 now writes its compressed KV cache for every token first, and then runs the rest of the layer on the last 128 tokens only.
  • The compressed KV cache that layer 20 writes is the same as before.
  • Layer 20 now writes its indexer results directly for the last 128 tokens. The code that copied those rows out of the full-size buffers is no longer needed, so this PR removes it.
  • No flag is added. Nothing changes when decoder bounded replay is off.
  • ROCm keeps the old behavior, because its fused layer path runs all of layer 20 in one call.
  • This also works with the prefill CUDA graph that [DeepSeek-V4.1] Run decoder SWA bounded replay inside the breakable prefill CUDA graph #43736 added for decoder bounded replay. Inside the graph, the step that prepares the layer 20 inputs now computes only the part the caller asked for. Before, it always computed the indexer inputs too, and the new full-prompt KV write does not have them.

One behavior change to review:

  • For the last 128 tokens, layer 20 now uses the same shortened sliding window as layers 21 to 39. Before this PR, layer 20 saw the full window.
  • This matches how the DeepSeek-V4.1 report describes decoder replay: "feed their encoder outputs through the decoder layers under the same SWA truncation".

Accuracy Tests

  • Hardware and model: 4x B300, deepseek-ai/DeepSeek-V4.1-Flash, TP4 and EP4.
  • "main" is commit 2f6e0ec45d. The two files this PR changes are the same on current main.
python3 -m sglang.launch_server --trust-remote-code --model-path deepseek-ai/DeepSeek-V4.1-Flash --tp 4 --ep-size 4 --mem-fraction-static 0.8 --cuda-graph-max-bs-decode 64 --max-running-requests 16 --enable-decoder-swa-bounded-replay
# The DSpark row adds: --speculative-algorithm DSPARK --speculative-dspark-block-size 5
python3 -m sglang.test.few_shot_gsm8k --num-questions 1319 --parallel 32
GSM8K, 1319 questions main this PR
No speculative decoding 0.907 0.907
DSpark, block size 5 0.902 0.914
  • Each number is one run. Two runs of the same setup can differ by about 0.007.

Speed Tests and Profiling

  • Same launch command as above, without speculative decoding.
  • The numbers are time to first token. Requests are sent one at a time, as token ids.
  • Each number is the median of 3 to 5 requests. The two numbers in a cell come from two separate server starts.
Request main this PR Change
New prompt, 16K tokens 139.6 / 139.5 ms 132.8 / 131.6 ms 5% faster
New prompt, 64K tokens 577.5 / 564.9 ms 526.6 / 524.5 ms 8% faster
New prompt, 200K tokens 2009.6 / 2015.4 ms 1800.5 / 1811.9 ms 10% faster
New prompt, 500 to 8K tokens 81.7 to 85.7 ms 82.5 to 84.4 ms No change
2K new tokens after 16K cached tokens 85.8 / 87.2 ms 84.4 / 85.5 ms No change
2K new tokens after 64K cached tokens 93.5 / 89.4 ms 93.2 / 91.4 ms No change
2K new tokens after 200K cached tokens 112.4 / 113.9 ms 115.2 / 113.3 ms No change
128 new tokens after 200K cached tokens 109.3 / 107.8 ms 114.2 / 113.0 ms 5 ms slower

Time spent in layer 20 for one 16K-token prefill chunk, measured with a GPU sync around each layer:

Prefill chunk main this PR
Early in the prompt 11.9 ms 3.3 ms
Late in a 200K-token prompt 27.0 ms 5.2 ms
  • Short requests do not get faster. Prefill without a CUDA graph costs about 80 ms per batch no matter how short the prompt is, and this PR does not change that.
  • The last row of the first table is the only one that got slower. In a third pair of server starts it was 109.2 ms on main and 107.3 ms with this PR, so I think it is noise between server starts.

Rerun after merging main at 684fea8af3

  • Main now runs decoder bounded replay inside the breakable prefill CUDA graph ([DeepSeek-V4.1] Run decoder SWA bounded replay inside the breakable prefill CUDA graph #43736), so I tested both with and without the graph.
  • Same hardware and model as above. The graph rows add --cuda-graph-backend-prefill breakable --mem-fraction-static 0.7. Current main ran out of GPU memory with the graph at 0.8, so both sides use 0.7 there.
  • Each number is one run from one server start.
GSM8K, 1319 questions main this PR
No prefill graph 0.906 0.911
Prefill graph 0.898 0.908
Prefill graph and DSpark, block size 5 not run 0.911
Time to first token, new prompt main this PR Change
14,795 tokens, no prefill graph 152.5 ms 143.2 ms 6% faster
14,795 tokens, prefill graph 172.6 ms 159.5 ms 8% faster
6,318 tokens, prefill graph 97.8 ms 90.7 ms 7% faster
590 tokens, prefill graph 32.5 ms 32.4 ms No change
  • No scheduler exceptions in any run.
  • Unit tests under test/registered/unit/layers/test_dsv4*.py: 70 passed.

Rerun after merging main at abf92826ee

GSM8K, 1319 questions main this PR
FlashMLA 0.908 0.904
FlashMLA and DSpark, block size 5 not run 0.905
TRT-LLM 0.897 0.897
Time to first token, new prompt main this PR Change
14,795 tokens, FlashMLA 165.9 ms 155.8 ms 6% faster
14,795 tokens, TRT-LLM 161.2 ms 149.1 ms 8% faster
6,318 tokens, FlashMLA 97.9 ms 90.4 ms 8% faster
6,318 tokens, TRT-LLM 89.9 ms 86.3 ms 4% faster
590 tokens, FlashMLA 31.6 ms 32.9 ms No change
  • Two runs of the same setup can differ by about 0.007 on GSM8K.
  • No scheduler exceptions in any run.
  • Unit tests under test/registered/unit/layers/test_dsv4*.py: 71 passed.

Not tested

  • Prefill context parallelism together with decoder bounded replay.
  • ROCm. Its behavior is unchanged.

Follow-up

  • CandidateMetadata.tail and its implementations are now only called by their own tests. A later PR can remove them.

CI States

Latest PR Test (Base): ⏳ Run #39068298513
Latest PR Test (Extra): ❌ Run #39068298480
Latest PR Test (AMD ROCm 10): ⏳ Run #39068298539

@vincentzed

Copy link
Copy Markdown
Contributor Author

@hnyls2002 could you take a look? This changes the decoder bounded replay code you added in #38798. The last 128 tokens now start at layer 20 instead of layer 21, and layer 20 only writes its compressed KV cache for the earlier tokens.

The one thing I would like your opinion on: layer 20 now uses the same shortened sliding window as layers 21 to 39 for those 128 tokens. I chose this because it matches the DeepSeek-V4.1 report. Keeping the full window for layer 20 is also possible if you prefer it.

cc @Oasis-Git since this is under the bounded replay section of #42170.

@vincentzed

Copy link
Copy Markdown
Contributor Author

@hnyls2002 a gentle reminder. This still needs a first review and the run-ci label, and it has no conflicts with main. The open question is whether layer 20 should use the shortened sliding window for the last 128 tokens.

@vincentzed

Copy link
Copy Markdown
Contributor Author

@hnyls2002 could you take a first look when you have time? There are no conflicts with main, and it still needs the run-ci label.

@vincentzed

Copy link
Copy Markdown
Contributor Author

I merged main into this branch because #43736 changed the same code.

  • There was one conflict in enter_late_layer_tail. I kept main's new handling of padded rows and kept this PR's removal of the top-k copy.
  • After the merge, the server crashed at startup with --cuda-graph-backend-prefill breakable. Inside the graph, the step that prepares the layer 20 inputs always computed the indexer inputs, and this PR's full-prompt KV write does not pass them. It now computes only the part the caller asked for.
  • I tested the merged branch against current main with and without the prefill graph. The results are in the PR description under "Rerun after merging main at 684fea8af3". With the graph, a 14,795-token prompt goes from 172.6 ms to 159.5 ms.

@Oasis-Git @jyizheng since this now sits on top of #43736, could one of you take a look when you have time?

@vincentzed

Copy link
Copy Markdown
Contributor Author

I merged main at f2b73d4f9b again to clear a conflict with #44347. I kept main's version of the conflicting lines.

Main now also allows TRT-LLM attention with decoder bounded replay under the prefill graph (#44326), so I tested that too. With the graph, a 14,795-token prompt is 156.7 ms against 167.2 ms on main with FlashMLA, and 151.6 ms against 157.2 ms with TRT-LLM. GSM8K is 0.908 on both backends. The full tables are in the PR description under "Rerun after merging main at f2b73d4f9b".

@vincentzed

Copy link
Copy Markdown
Contributor Author

I merged main at abf92826ee to clear a conflict with #43607, which moved the DeepSeek-V4 layer code into separate stage files. This PR's new layer method and its changes to the model loop carried over without changes.

I ran the same checks again with FlashMLA and TRT-LLM under the prefill graph. A 14,795-token prompt is 155.8 ms against 165.9 ms on main with FlashMLA, and 149.1 ms against 161.2 ms with TRT-LLM. GSM8K is within noise of main on both backends, and there were no scheduler exceptions. The tables in the PR description are updated.

@vincentzed vincentzed changed the title [DSv4.1] Skip wasted layer 20 work on earlier prompt tokens under decoder bounded replay [Perf] deepseek-v4.1: Skip wasted layer 20 work on earlier prompt tokens under decoder bounded replay Oct 16, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator

/rerun-test test_deepseek_v41_flash_gb300_tp4ep4.py test_deepseek_v41_flash_gb300_dp4ep4.py test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py test_dsv41_late_layer_tail_readers.py test_encoder_swa_replay.py test_deepseek_v4_boundary.py

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_tp4ep4.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_dp4ep4.py
cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_trtllm_bounded_bcg.py

🚀 ubuntu-latest (3 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/models/test_deepseek_v4_boundary.py

@hnyls2002

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 17, 2026
@hnyls2002
hnyls2002 merged commit 05ce0e9 into sgl-project:main Oct 17, 2026
177 of 222 checks passed
@vincentzed

Copy link
Copy Markdown
Contributor Author

Thank you @hnyls2002 for running the extra GB300 tests and for merging this. The GB300 rerun was still queued when it merged. I will watch it and follow up here if it fails.

One small follow-up from the description: CandidateMetadata.tail and its implementations are now only called by their own tests, so I can send a PR to remove them.

chaohu2000 added a commit to flytiger-eco/sglang-for-sail that referenced this pull request Nov 7, 2026
Why:
- Layer 20, the last kv_source layer, ran its indexer and attention over all 16384 prefill rows, but the layers after it only read each request's tail rows, so that full-width result was never consumed.
- Under decoder SWA bounded replay this wasted a large part of the prefill on every long prompt.

How:
- On non-ROCm, start the tail at the last kv_source layer (late_layer_start = max(kv_source_layer_ids)); write_tail_source_kv first writes that layer's compressed KV for every row, so its in-tail pass skips the compressor and computes only the tail rows.
- With top-k and candidates now published inside the tail, drop the row-wise top-k copy in enter_late_layer_tail and the candidate_metadata tail slicing on publish.
- Assert that the last kv_source layer carries no Engram and is both the index and the candidate source; ROCm keeps the previous boundary past the source layer.
- Cherry-picked from upstream commit 05ce0e9 (sglang PR sgl-project#42901); the fork keeps its CPU-only global_num_token_non_padded save/restore, its consume-aware _bcg_low_ratio_source_projections dispatch, and its _check_late_layer_tail_readers signature.

Dependency:
- None
chaohu2000 added a commit to flytiger-eco/sglang-for-sail that referenced this pull request Nov 9, 2026
Why:
- Layer 20, the last kv_source layer, ran its indexer and attention over all 16384 prefill rows, but the layers after it only read each request's tail rows, so that full-width result was never consumed.
- Under decoder SWA bounded replay this wasted a large part of the prefill on every long prompt.

How:
- On non-ROCm, start the tail at the last kv_source layer (late_layer_start = max(kv_source_layer_ids)); write_tail_source_kv first writes that layer's compressed KV for every row, so its in-tail pass skips the compressor and computes only the tail rows.
- With top-k and candidates now published inside the tail, drop the row-wise top-k copy in enter_late_layer_tail and the candidate_metadata tail slicing on publish.
- Assert that the last kv_source layer carries no Engram and is both the index and the candidate source; ROCm keeps the previous boundary past the source layer.
- Cherry-picked from upstream commit 05ce0e9 (sglang PR sgl-project#42901); the fork keeps its CPU-only global_num_token_non_padded save/restore, its consume-aware _bcg_low_ratio_source_projections dispatch, and its _check_late_layer_tail_readers signature.

Dependency:
- None
chaohu2000 added a commit to flytiger-eco/sglang-for-sail that referenced this pull request Nov 10, 2026
Why:
- Layer 20, the last kv_source layer, ran its indexer and attention over all 16384 prefill rows, but the layers after it only read each request's tail rows, so that full-width result was never consumed.
- Under decoder SWA bounded replay this wasted a large part of the prefill on every long prompt.

How:
- On non-ROCm, start the tail at the last kv_source layer (late_layer_start = max(kv_source_layer_ids)); write_tail_source_kv first writes that layer's compressed KV for every row, so its in-tail pass skips the compressor and computes only the tail rows.
- With top-k and candidates now published inside the tail, drop the row-wise top-k copy in enter_late_layer_tail and the candidate_metadata tail slicing on publish.
- Assert that the last kv_source layer carries no Engram and is both the index and the candidate source; ROCm keeps the previous boundary past the source layer.
- Cherry-picked from upstream commit 05ce0e9 (sglang PR sgl-project#42901); the fork keeps its CPU-only global_num_token_non_padded save/restore, its consume-aware _bcg_low_ratio_source_projections dispatch, and its _check_late_layer_tail_readers signature.

Dependency:
- None
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