Skip to content

[DSA] Enable draft-extend CUDA graph for DeepSeek Sparse Attention - #29413

Merged
Fridge003 merged 7 commits into
mainfrom
kp/dsa-draft-extend-cudagraph
Jun 27, 2026
Merged

Fridge003 merged 7 commits into
mainfrom
kp/dsa-draft-extend-cudagraph

Conversation

@kpham-sgl

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

Copy link
Copy Markdown
Collaborator

Makes the DSA draft-extend path CUDA-graphable, so spec-v2 decode stops reading seq_lens_cpu on the host.

  • draft_extend_v2 branch of _apply_cuda_graph_metadata now uses the fixed speculative_num_draft_tokens QO width (like target_verify), dropping the .tolist() host sync and data-dependent repeat_interleave shape that were illegal under graph replay.
  • Add DeepseekSparseAttnBackend to the draft-extend graph allowlist in eagle_worker_v2.

Stack: #29413 → #29414 → #29415

Tested: GLM-5.2 (TP4, EAGLE, DSA), GSM8K 0.950 (no regression).

Profile

Screenshot 2026-06-26 at 2 01 54 AM

CI States

Latest PR Test (Base): 🚫 Run #28279703598
Latest PR Test (Extra): ❌ Run #28279703539

…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).

@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 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.

Comment on lines 1277 to 1285
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,
)

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.

high

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.

Suggested change
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>
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jun 26, 2026

@zhendonghua zhendonghua 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.

Overall LGTM

)

extend_seq_lens = spec_info.num_accept_tokens[:bs]
extend_seq_lens_cpu = extend_seq_lens.tolist()

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.

Is this the cause of the big cuda sync in my profile?

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.

seems so

@Fridge003 Fridge003 left a comment

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.

Cool

@Fridge003

Copy link
Copy Markdown
Collaborator

/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

@github-actions

github-actions Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Results for /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:

🚀 8-gpu-h200 (2 tests): ❌ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm5_tp_mtp.py
cd test/ && python3 registered/models_e2e/test_dsa_glm5_dp_mtp.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py:

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

cd test/ && python3 registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_dsa_glm5_tp_mtp.py test/registered/models_e2e/test_dsa_glm5_dp_mtp.py

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_dsa_glm5_tp_mtp.py test/registered/models_e2e/test_dsa_glm5_dp_mtp.py:

🚀 8-gpu-h200 (2 tests): ❌ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm5_tp_mtp.py
cd test/ && python3 registered/models_e2e/test_dsa_glm5_dp_mtp.py

kpham-sgl and others added 3 commits June 26, 2026 22:17
Co-authored-by: Jackey Hua <107608053+zhendonghua@users.noreply.github.com>
Co-authored-by: Jackey Hua <107608053+zhendonghua@users.noreply.github.com>
@Fridge003

Copy link
Copy Markdown
Collaborator

/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

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Results for /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:

🚀 8-gpu-h200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_dsa_glm5_tp_mtp.py
cd test/ && python3 registered/models_e2e/test_dsa_glm5_dp_mtp.py

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

cd test/ && python3 registered/cuda_graph/piecewise/test_pcg_glm5_fp4.py

@Fridge003
Fridge003 merged commit 9214b93 into main Jun 27, 2026
151 of 188 checks passed
@Fridge003
Fridge003 deleted the kp/dsa-draft-extend-cudagraph branch June 27, 2026 06:53
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail high priority run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants