Repository navigation
[DSA] Make seq_lens_cpu optional for DeepSeek Sparse Attention - #29414
Conversation
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
| 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]>
af13f87 to
7d28d55
Compare
6c24944 to
cdc3784
Compare
| class DeepseekSparseAttnBackend( | ||
| DeepseekSparseAttnBackendMTPPrecomputeMixin, AttentionBackend | ||
| ): | ||
| # Decode/verify/draft graph replay rebuilds metadata from static buffers |
There was a problem hiding this comment.
Nit: remove the extra comments
d4793f9
into
kp/dsa-draft-extend-cudagraph
Opts DSA out of the host D2H sync now that all spec-v2 paths replay under CUDA graphs.
needs_cpu_seq_lens = Falseon both DSA backends →resolve_seq_lens_cpuskips thefwd_prepare_d2h_stream.synchronize()on the decode critical path.init_forward_metadatatoleratesseq_lens_cpu=None(derives page-table width from GPUseq_lens).Stack: #29413 → #29414 → #29415
Tested: GSM8K (no regression);
resolve_seq_lens_cpuno longer on the decode critical path.Profile
Before

After (no streamSynchronize)
