Skip to content

[Fix] deepseek-v4.1: Keep the full SWA window for short bounded-replay extends - #41303

Merged
hnyls2002 merged 8 commits into
sgl-project:mainfrom
modal-projects:fix/dsv41-decoder-swa-replay-short-extend
Oct 16, 2026
Merged

hnyls2002 merged 8 commits into
sgl-project:mainfrom
modal-projects:fix/dsv41-decoder-swa-replay-short-extend

Conversation

@vineelbhat

@vineelbhat vineelbhat commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

With DeepSeek-V4.1's --enable-decoder-swa-bounded-replay, the layers after the last kv_source layer attend over only the last min(window, extend_len) rows of each prefill extend. An extend shorter than the window therefore gives the prompt's first sampled token a truncated window.

This happens on a prefix-cache hit that leaves fewer than window uncached rows, and on a chunked prefill whose final chunk is shorter than window.

Modifications

  • A prefix-cache hit now stops at least one sliding window short of the prompt when decoder bounded replay is on. Only a hit that would have left less than a window is shortened, by at most one page. swa_reprefill_tail_tokens() returns sliding_window_size when the new CacheInitParams.decoder_swa_bounded_replay is set.
  • The late-layer window of a final prefill chunk shorter than the window is now floored at seq_len - window instead of at the chunk start, so the chunk reads the previous chunk's tail rows, which already hold their late-layer KV. This is a two line change in late_layer_tail_layout.
  • A streaming session turn that borrows its slot's record skips the hold-back: the record is the request's own KV, whose last window is already written. swa_reprefill_tail_tokens(req) takes the request for this.
  • A prefix hit now runs the same rows with the same window floors as a cold prefill. A short final chunk does not: its tail reads the previous chunk's tail rows, which were computed under that chunk's floor. The short-chunk table below measures the difference.

Accuracy Tests

4xB200, DeepSeek-V4.1-Flash TP4/EP4, greedy, 12 prompts per row, at the first generated position. Flips count prompts whose sampled token changed; shift is the median change of the reference's sampled-token logprob, in nats.

Prefix hit. An 8192-token cached prefix plus N fresh tokens, against a cold prefill of the same prompt.

uncached rows flips, stock flips, fixed shift, stock shift, fixed
1 10/12 1/12 2.40 0.03
2 7/12 3/12 0.90 0.15
8 3/12 2/12 0.44 0.09
32 2/12 0/12 0.003 0.002
>= 127 0/12 0/12 0.000 0.000

Short final chunk. A cold 16384 + m token prompt prefilled with a 16384-token chunk size, so its final chunk has m rows, against the same prompt prefilled as one 8192 + m extend after priming its first 8192 tokens.

final chunk rows m flips, stock flips, fixed shift, stock shift, fixed
1 5/12 0/12 0.45 0.000
2 4/12 1/12 0.26 0.000
4 1/12 0/12 0.005 0.000
8 1/12 0/12 0.003 0.000
16 to 127 0/12 0/12 0.000 0.000

test_unified_radix_cache_unittest.py: 2789 run on 1xH100, OK (1504 config skips), 1 new case. test_unified_cache_linker.py: 32 passed, one existing test updated.

Speed Tests and Profiling

The prefix-match limit only applies when a hit would leave fewer than window uncached rows, and then recomputes at most one page. The floor change costs each token of a short final chunk at most a full window of 128 keys to score.

Checklist


CI States

Latest PR Test (Base): 🚫 Run #38824417900
Latest PR Test (Extra): ❌ Run #38824417703
Latest PR Test (AMD ROCm 10): ⏳ Run #38824417750

@vineelbhat vineelbhat changed the title [Fix] DeepSeek-V4.1: keep every prefill extend at least one SWA window long under decoder bounded replay [DeepSeek-V4.1] Keep prefill extends at least one SWA window long under decoder bounded replay Sep 26, 2026
@vineelbhat
vineelbhat force-pushed the fix/dsv41-decoder-swa-replay-short-extend branch 7 times, most recently from 6075d03 to 9ec00c1 Compare September 26, 2026 05:50
@vineelbhat vineelbhat changed the title [DeepSeek-V4.1] Keep prefill extends at least one SWA window long under decoder bounded replay [DeepSeek-V4.1] Keep every prefill extend at least one SWA window long under decoder bounded replay Sep 26, 2026
@vineelbhat
vineelbhat marked this pull request as ready for review September 26, 2026 07:46
@vineelbhat
vineelbhat force-pushed the fix/dsv41-decoder-swa-replay-short-extend branch 2 times, most recently from d5a72f1 to b34704b Compare September 26, 2026 09:01
@vineelbhat vineelbhat changed the title [DeepSeek-V4.1] Keep every prefill extend at least one SWA window long under decoder bounded replay [Fix] Keep every prefill extend at least one SWA window long under decoder bounded replay Sep 26, 2026
@vineelbhat
vineelbhat force-pushed the fix/dsv41-decoder-swa-replay-short-extend branch from b34704b to 5f06772 Compare September 27, 2026 20:34
@vineelbhat vineelbhat changed the title [Fix] Keep every prefill extend at least one SWA window long under decoder bounded replay [Fix] Keep every prefill extend at least one SWA window long under DeepSeek-V4.1 decoder bounded replay Sep 28, 2026
vineelbhat and others added 2 commits September 28, 2026 21:25
…ed replay

swa_reprefill_tail_tokens() returns the window when decoder bounded replay is on, so the scheduler's key_limit stops every prefix hit at least one window short of the prompt. Adds a unit test on a real FULL+SWA cache. A streaming session whose slot cannot rewind to the capped match releases the slot and falls back to the radix match.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…long

PrefillAdder._hold_back_final_chunk shortens a chunk so that exactly one window remains when the remainder would be shorter than the window, at the three places a chunk length is committed. The boundary it creates is not page aligned. Adds three unit tests.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vineelbhat
vineelbhat force-pushed the fix/dsv41-decoder-swa-replay-short-extend branch from 5f06772 to 9dac568 Compare September 28, 2026 21:34
@gilfordting

Copy link
Copy Markdown
Contributor

/tag-and-rerun-ci

@vineelbhat

Copy link
Copy Markdown
Contributor Author

@kpham-sgl replaced with your fix in 99d8156 and re-ran the short final chunk accuracy tests.

@kpham-sgl kpham-sgl self-assigned this Oct 1, 2026
if len(extend_lens_cpu) == 1:
n, t, s = extend_lens_cpu[0], tail_lens_cpu[0], seq_lens_cpu[0]
floor = torch.full((t,), s - t, dtype=torch.int32, device=device)
floor = torch.full((t,), max(0, s - tail_len), dtype=torch.int32, device=device)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is missing testing coverage, the new test covers prefix holdback but wouldn't catch the window floor reverting to chunk start

vineelbhat and others added 2 commits October 15, 2026 10:21
Add a late_layer_tail_layout test for a final prefill chunk shorter than a
window, in a batch of one request and a batch of several. It fails if the
floor goes back to the chunk start.

Also update the docstring of test_tail_keeps_its_own_floor_under_fold, which
described the tail floor as the tail start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vineelbhat

Copy link
Copy Markdown
Contributor Author

merged main in and added test coverage for the chunked prefill side

…ase branch; fix hold-back test after Req.prefix_indices removal; refresh comments
@hnyls2002 hnyls2002 changed the title [Fix] Preserve the SWA window of short prefill extends under DeepSeek-V4.1 decoder bounded replay [Fix] deepseek-v4.1: Keep the full SWA window for short bounded-replay extends Oct 15, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator

/rerun-test test_encoder_swa_replay.py test_unified_radix_cache_unittest.py test_unified_cache_linker.py test_radix_force_miss.py test_deepseek_v41_flash_gb300.py

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/unit/model_executor/test_encoder_swa_replay.py
cd test/ && python3 registered/unit/mem_cache/test_radix_force_miss.py

🚀 1-gpu-5090 (2 tests): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_unified_radix_cache_unittest.py
cd test/ && python3 registered/unit/mem_cache/test_unified_cache_linker.py

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

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300.py

@hnyls2002
hnyls2002 merged commit ed8c313 into sgl-project:main Oct 16, 2026
146 of 217 checks passed
akhilg-nv added a commit to akhilg-nv/sglang that referenced this pull request Oct 16, 2026
Resolve conflicts with sgl-project#43922 (MegaAttention) by keeping both sides. After
sgl-project#41303 floors the bounded-replay tail at seq_len - SWA_WINDOW, tail rows bound
their SWA columns by the floored swa_topk_lengths instead of the tail offset.

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 jit-kernel run-ci CI: run the baseline test suite on this PR unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants