Skip to content

[DSA] Make seq_lens_cpu optional for DeepSeek Sparse Attention - #29414

Merged
Fridge003 merged 1 commit into
kp/dsa-draft-extend-cudagraphfrom
kp/dsa-seq-lens-cpu-optional
Jun 27, 2026
Merged

Fridge003 merged 1 commit into
kp/dsa-draft-extend-cudagraphfrom
kp/dsa-seq-lens-cpu-optional

Conversation

@kpham-sgl

@kpham-sgl kpham-sgl commented Jun 26, 2026 •

Copy link
Copy Markdown
Collaborator

Opts DSA out of the host D2H sync now that all spec-v2 paths replay under CUDA graphs.

  • needs_cpu_seq_lens = False on both DSA backends → resolve_seq_lens_cpu skips the fwd_prepare_d2h_stream.synchronize() on the decode critical path.
  • Eager init_forward_metadata tolerates seq_lens_cpu=None (derives page-table width from GPU seq_lens).

Stack: #29413 → #29414 → #29415

Tested: GSM8K (no regression); resolve_seq_lens_cpu no longer on the decode critical path.

Profile

Before
Screenshot 2026-06-26 at 2 04 25 AM

After (no streamSynchronize)
Screenshot 2026-06-26 at 2 22 19 AM

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request configures the DeepSeek sparse attention backends to opt out of host-device synchronization by setting needs_cpu_seq_lens = False and updates init_forward_metadata to fallback to GPU-based sequence lengths when CPU sequence lengths are unavailable. Feedback points out a potential runtime crash when calling .max() on empty tensors (e.g., during idle states or empty pipeline parallel ranks) and suggests guarding these calls with a check on .numel() > 0.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +650 to +656
if forward_batch.seq_lens_cpu is not None:
max_seqlen_k = int(forward_batch.seq_lens_cpu.max().item() + draft_token_num)
else:
# needs_cpu_seq_lens=False nulls the host mirror for spec-v2 relay
# batches; graph replay uses the static page-table width, so only this
# eager (e.g. over-capture-bs) fallback needs a length here.
max_seqlen_k = int(forward_batch.seq_lens.max().item()) + draft_token_num

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If the forward batch is empty (e.g., batch_size == 0 during idle states or on empty pipeline parallel ranks), calling .max() on forward_batch.seq_lens or forward_batch.seq_lens_cpu will raise a RuntimeError: max(): Expected reduction dim to be specified for input of size 0.

To prevent potential runtime crashes, we should guard these calls by checking if the tensors are non-empty (.numel() > 0) before computing the maximum value.

Suggested change
if forward_batch.seq_lens_cpu is not None:
max_seqlen_k = int(forward_batch.seq_lens_cpu.max().item() + draft_token_num)
else:
# needs_cpu_seq_lens=False nulls the host mirror for spec-v2 relay
# batches; graph replay uses the static page-table width, so only this
# eager (e.g. over-capture-bs) fallback needs a length here.
max_seqlen_k = int(forward_batch.seq_lens.max().item()) + draft_token_num
if forward_batch.seq_lens_cpu is not None:
max_seqlen_k = int(forward_batch.seq_lens_cpu.max().item() + draft_token_num) if forward_batch.seq_lens_cpu.numel() > 0 else 0
else:
# needs_cpu_seq_lens=False nulls the host mirror for spec-v2 relay
# batches; graph replay uses the static page-table width, so only this
# eager (e.g. over-capture-bs) fallback needs a length here.
max_seqlen_k = int(forward_batch.seq_lens.max().item()) + draft_token_num if forward_batch.seq_lens.numel() > 0 else 0

Set needs_cpu_seq_lens=False on DeepseekSparseAttnBackend and
DeepseekSparseAttnMultiStepBackend now that the spec-v2 decode / verify /
draft-extend paths replay under CUDA graphs and rebuild metadata from static
buffers (page-table width) instead of reading seq_lens_cpu.max() on the host.
With the flag off, decide_needs_cpu_seq_lens' OR over the backends stays False
and resolve_seq_lens_cpu skips the fwd_prepare_d2h_stream.synchronize() D2H
copy that was sitting on the spec-v2 decode critical path.

Make the eager init_forward_metadata tolerant of seq_lens_cpu=None so the
out-of-graph fallback (e.g. over-capture-bs batches) still derives the
page-table width from GPU seq_lens.

Co-authored-by: Jackey Hua <[email protected]>
Co-authored-by: Cursor <[email protected]>
class DeepseekSparseAttnBackend(
DeepseekSparseAttnBackendMTPPrecomputeMixin, AttentionBackend
):
# Decode/verify/draft graph replay rebuilds metadata from static buffers

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.

Nit: remove the extra comments

@Fridge003
Fridge003 merged commit d4793f9 into kp/dsa-draft-extend-cudagraph Jun 27, 2026
88 of 100 checks passed
@Fridge003
Fridge003 deleted the kp/dsa-seq-lens-cpu-optional branch June 27, 2026 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants