Repository navigation
[Perf] deepseek-v4.1: Skip wasted layer 20 work on earlier prompt tokens under decoder bounded replay - #42901
Conversation
…under decoder bounded replay
|
@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. |
|
@hnyls2002 a gentle reminder. This still needs a first review and the |
|
@hnyls2002 could you take a first look when you have time? There are no conflicts with main, and it still needs the |
|
I merged main into this branch because #43736 changed the same code.
@Oasis-Git @jyizheng since this now sits on top of #43736, could one of you take a look when you have time? |
|
I merged main at 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 |
|
I merged main at 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. |
…id; assert source layout; fix tail comments
|
/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 |
|
🚀 🚀 |
|
/tag-and-rerun-ci |
|
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: |
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
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
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
Motivation
--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.Modifications
One behavior change to review:
Accuracy Tests
deepseek-ai/DeepSeek-V4.1-Flash, TP4 and EP4.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 32Speed Tests and Profiling
Time spent in layer 20 for one 16K-token prefill chunk, measured with a GPU sync around each layer:
Rerun after merging main at
684fea8af3--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.test/registered/unit/layers/test_dsv4*.py: 70 passed.Rerun after merging main at
abf92826ee--dsv4-attn-backend trtllm).--cuda-graph-backend-prefill breakable --mem-fraction-static 0.7. Each number is one run from one server start.test/registered/unit/layers/test_dsv4*.py: 71 passed.Not tested
Follow-up
CandidateMetadata.tailand 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