Repository navigation
[DSA] Enable draft-extend CUDA graph for DeepSeek Sparse Attention - #29413
Conversation
…ns_cpu host read In the DeepSeek Sparse Attention (DSA) CUDA-graph replay paths, the page-table copy width was derived from `seq_lens_cpu.max().item()`, which forces a per-step device-to-host read of the sequence-length mirror. When the scheduler overlaps CPU work with GPU compute, that host read serializes the critical path between decode steps (most visible on slower host CPUs, e.g. GB200 Grace). Replace the dynamic width with the static captured buffer width (`page_table_1.shape[1]`) in the decode, target-verify, and draft-extend-v2 replay branches, plus the multi-step-draft precompute decode path. The kernel already bounds each row's reads by the GPU `cache_seqlens`, so copying the full captured width is correct (and the captured buffer already reserves `+num_draft_tokens` for verify). This mirrors how `trtllm_mla` sizes its page table. This makes the replay paths independent of the host seq-len mirror (`seq_lens_cpu` may now be None there), a prerequisite for later dropping `needs_cpu_seq_lens` for DSA once the remaining draft-extend host sync is removed. Validated on GLM-5.2-NVFP4 (TP=4, EAGLE spec decode, DSA, fp8 KV cache): GSM8K = 0.9515 (sgl-eval, 1319 examples, chat + thinking) / 0.939 (built-in few_shot_gsm8k, 5-shot greedy); no accuracy regression, with DSA decode CUDA graphs active (accept len ~5.7-6.0, accept rate ~0.94-1.0).
There was a problem hiding this comment.
Code Review
This pull request updates the V2 draft-extend process in dsa_backend.py to use static shapes based on speculative_num_draft_tokens instead of dynamic shapes, allowing CUDA graph replay to remain host-sync-free. Additionally, it lazily imports DeepseekSparseAttnBackend in eagle_worker_v2.py to avoid importing the sparse-attention stack on non-CUDA builds. The reviewer noted that creating a new GPU tensor on every call to _apply_cuda_graph_metadata introduces host-to-device copy and allocation overhead, and suggested caching this tensor on the metadata object to optimize performance.
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.
| extend_seq_lens_cpu = [self.speculative_num_draft_tokens] * bs | ||
| seqlens_expanded = seqlens_expand_triton( | ||
| extend_seq_lens, | ||
| torch.tensor( | ||
| extend_seq_lens_cpu, dtype=torch.int32, device=self.device | ||
| ), | ||
| cache_seqlens, | ||
| sum(extend_seq_lens_cpu), | ||
| self.speculative_num_draft_tokens * bs, | ||
| self.speculative_num_draft_tokens, | ||
| ) |
There was a problem hiding this comment.
Creating a new GPU tensor via torch.tensor(..., device=self.device) on every call to _apply_cuda_graph_metadata during graph replay introduces significant host-to-device copy overhead and GPU memory allocation on the hot path. Since self.speculative_num_draft_tokens and bs are constant for a given captured graph, we can pre-allocate and cache this tensor on the metadata object using object.__setattr__ to completely avoid this overhead.
| extend_seq_lens_cpu = [self.speculative_num_draft_tokens] * bs | |
| seqlens_expanded = seqlens_expand_triton( | |
| extend_seq_lens, | |
| torch.tensor( | |
| extend_seq_lens_cpu, dtype=torch.int32, device=self.device | |
| ), | |
| cache_seqlens, | |
| sum(extend_seq_lens_cpu), | |
| self.speculative_num_draft_tokens * bs, | |
| self.speculative_num_draft_tokens, | |
| ) | |
| extend_seq_lens = getattr(metadata, "extend_seq_lens", None) | |
| if extend_seq_lens is None: | |
| extend_seq_lens = torch.tensor( | |
| [self.speculative_num_draft_tokens] * bs, | |
| dtype=torch.int32, | |
| device=self.device, | |
| ) | |
| object.__setattr__(metadata, "extend_seq_lens", extend_seq_lens) | |
| seqlens_expanded = seqlens_expand_triton( | |
| extend_seq_lens, | |
| cache_seqlens, | |
| self.speculative_num_draft_tokens * bs, | |
| self.speculative_num_draft_tokens, | |
| ) |
Rewrite the draft_extend_v2 branch of _apply_cuda_graph_metadata to use the fixed speculative_num_draft_tokens QO width (like target_verify) instead of the variable num_accept_tokens path, which had a .tolist() host sync and a data-dependent repeat_interleave shape -- both illegal under CUDA graph replay. With static shapes, q_offset stays constant across replays and matches the captured page-table slice. Add DeepseekSparseAttnBackend to the draft-extend graph allowlist in eagle_worker_v2 so cuda_graph_runner_for_draft_extend is created for DSA instead of falling back to the eager path. This removes the last host seq_lens_cpu read on the spec-v2 decode critical path, letting needs_cpu_seq_lens=False skip the resolve_seq_lens_cpu D2H synchronize. Validated on GLM-5.2 (TP4, EAGLE, DSA): draft-extend graph captures for all bs buckets; GSM8K 0.950 (no regression). Co-authored-by: kpham-sgl <khoa.pham@radixark.ai> Co-authored-by: Cursor <cursoragent@cursor.com>
af13f87 to
7d28d55
Compare
|
/tag-and-rerun-ci |
| ) | ||
|
|
||
| extend_seq_lens = spec_info.num_accept_tokens[:bs] | ||
| extend_seq_lens_cpu = extend_seq_lens.tolist() |
There was a problem hiding this comment.
Is this the cause of the big cuda sync in my profile?
|
/rerun-test test/registered/models_e2e/test_dsa_glm5_tp_mtp.py test/registered/models_e2e/test_dsa_glm5_dp_mtp.py test/registered/models_e2e/test_dsa_glm5_tp_mtp.py |
|
Results for 🚀 |
|
/rerun-test test/registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py |
|
Results for 🚀 |
|
/rerun-test test/registered/models_e2e/test_dsa_glm5_tp_mtp.py test/registered/models_e2e/test_dsa_glm5_dp_mtp.py |
|
Results for 🚀 |
Co-authored-by: Jackey Hua <107608053+zhendonghua@users.noreply.github.com>
Co-authored-by: Jackey Hua <107608053+zhendonghua@users.noreply.github.com>
|
/rerun-test test/registered/models_e2e/test_dsa_glm5_tp_mtp.py test/registered/models_e2e/test_dsa_glm5_dp_mtp.py test/registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py |
|
Results for 🚀 🚀 |
…gl-project#29413) Co-authored-by: kpham-sgl <kpham-sgl@users.noreply.github.com> Co-authored-by: Jackey Hua <107608053+zhendonghua@users.noreply.github.com>
Makes the DSA draft-extend path CUDA-graphable, so spec-v2 decode stops reading
seq_lens_cpuon the host.draft_extend_v2branch of_apply_cuda_graph_metadatanow uses the fixedspeculative_num_draft_tokensQO width (liketarget_verify), dropping the.tolist()host sync and data-dependentrepeat_interleaveshape that were illegal under graph replay.DeepseekSparseAttnBackendto the draft-extend graph allowlist ineagle_worker_v2.Stack: #29413 → #29414 → #29415
Tested: GLM-5.2 (TP4, EAGLE, DSA), GSM8K 0.950 (no regression).
Profile
CI States
Latest PR Test (Base): 🚫 Run #28279703598
Latest PR Test (Extra): ❌ Run #28279703539