Repository navigation
[DSV4.1] Support decoder SWA bounded replay with CUDA DP attention #43812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kpham-sgl
merged 5 commits into
sgl-project:main
from
kpham-sgl:codex/dsv41-decoder-replay-dp
Oct 13, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1af3fc7
decoder-replay-dp(move,mechanical_provable): extract DP replay helpers
kpham-sgl 50c2ed3
decoder-replay-dp(enable,non_mechanical_provable): support CUDA DP at…
kpham-sgl 2015a00
decoder-replay-dp: address review
kpham-sgl 4b8cad0
decoder-replay-dp: drop the geometry unit test
kpham-sgl aba4065
decoder-replay-dp: move the DP helpers under deepseek_common
kpham-sgl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, List, Optional | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.srt.layers.dp_attention import DpPaddingMode, set_dp_buffer_len_from_batch | ||
| from sglang.srt.runtime_context import get_parallel | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sglang.srt.model_executor.forward_batch_info import ForwardBatch | ||
|
|
||
|
|
||
| def late_layer_dp_counts( | ||
| forward_batch: ForwardBatch, local_late_rows: int | ||
| ) -> Optional[List[int]]: | ||
| # Bounded replay shrinks an extending rank's late layers to its tail, but the | ||
| # TP-MoE gather / combine there spans every DP rank, so each rank needs all | ||
| # ranks' late-layer row counts. Eager forwards only: every rank takes this | ||
| # branch together, and graph replays never reach the late-layer switch. | ||
| early = forward_batch.global_num_tokens_padded_cpu | ||
| if early is None: | ||
| early = forward_batch.global_num_tokens_cpu | ||
| if early is None or torch.cuda.is_current_stream_capturing(): | ||
| return None | ||
| device = forward_batch.global_num_tokens_gpu.device | ||
| local = torch.tensor([local_late_rows], dtype=torch.int64, device=device) | ||
| counts = torch.empty(len(early), dtype=torch.int64, device=device) | ||
| get_parallel().tp_group.all_gather_into_tensor(counts, local) | ||
|
kpham-sgl marked this conversation as resolved.
Resolved
|
||
| counts = counts.tolist() | ||
|
YAMY1234 marked this conversation as resolved.
Resolved
|
||
| return None if counts == list(early) else counts | ||
|
|
||
|
|
||
| def enter_late_layer_dp(forward_batch: ForwardBatch, counts: List[int]) -> tuple: | ||
| fb = forward_batch | ||
| saved = ( | ||
| fb.global_num_tokens_cpu, | ||
| fb.global_num_tokens_gpu, | ||
| fb.global_num_tokens_padded_cpu, | ||
| fb.global_num_tokens_for_logprob_cpu, | ||
| fb.global_num_tokens_for_logprob_gpu, | ||
| fb.global_dp_buffer_len, | ||
| fb.dp_padding_mode, | ||
| fb.dp_local_start_pos, | ||
| fb.dp_local_num_tokens, | ||
| fb.global_num_token_non_padded, | ||
| fb.global_num_token_non_padded_cpu, | ||
| fb.num_token_non_padded, | ||
| ) | ||
| fb.global_num_tokens_cpu = counts | ||
| fb.global_num_tokens_gpu = torch.tensor( | ||
| counts, dtype=torch.int64, device=fb.global_num_tokens_gpu.device | ||
| ) | ||
| fb.global_num_tokens_padded_cpu = counts | ||
| # The MoE gatherv reads its sizes from the logprob counts when present. | ||
| fb.global_num_tokens_for_logprob_cpu = counts | ||
| fb.global_num_tokens_for_logprob_gpu = fb.global_num_tokens_gpu | ||
| fb.global_dp_buffer_len = sum(counts) | ||
| # Tail rows are real rows only, so the late layers gather variable lengths. | ||
| fb.dp_padding_mode = DpPaddingMode.SUM_LEN | ||
| fb.dp_local_start_pos = fb.dp_local_num_tokens = None | ||
| # With attention TP=1 the invariant scalar also describes local MoE rows. | ||
| # Preserve an idle peer's zero bound even when it carries fabricated rows. | ||
| local_rows = counts[get_parallel().attn_dp_rank] | ||
| real_rows = min(local_rows, fb.global_num_token_non_padded_cpu) | ||
| fb.global_num_token_non_padded_cpu = real_rows | ||
| if fb.global_num_token_non_padded is not None: | ||
| fb.global_num_token_non_padded = fb.global_num_token_non_padded.new_tensor( | ||
| real_rows | ||
| ) | ||
| fb.num_token_non_padded = fb.global_num_token_non_padded | ||
| set_dp_buffer_len_from_batch(fb) | ||
| return saved | ||
|
|
||
|
|
||
| def exit_late_layer_dp(forward_batch: ForwardBatch, saved: tuple) -> None: | ||
| fb = forward_batch | ||
| ( | ||
| fb.global_num_tokens_cpu, | ||
| fb.global_num_tokens_gpu, | ||
| fb.global_num_tokens_padded_cpu, | ||
| fb.global_num_tokens_for_logprob_cpu, | ||
| fb.global_num_tokens_for_logprob_gpu, | ||
| fb.global_dp_buffer_len, | ||
| fb.dp_padding_mode, | ||
| fb.dp_local_start_pos, | ||
| fb.dp_local_num_tokens, | ||
| fb.global_num_token_non_padded, | ||
| fb.global_num_token_non_padded_cpu, | ||
| fb.num_token_non_padded, | ||
| ) = saved | ||
| set_dp_buffer_len_from_batch(fb) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.