Skip to content

[DSA] Remove host H2D sync in _apply_cuda_graph_metadata - #29415

Merged
Fridge003 merged 2 commits into
kp/dsa-draft-extend-cudagraphfrom
kp/dsa-remove-graph-metadata-sync
Jun 27, 2026
Merged

Fridge003 merged 2 commits into
kp/dsa-draft-extend-cudagraphfrom
kp/dsa-remove-graph-metadata-sync

Conversation

@kpham-sgl

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

Copy link
Copy Markdown
Collaborator

_apply_cuda_graph_metadata built the per-req QO-length tensor with torch.tensor([num_draft_tokens] * bs, device=cuda) — a pageable H2D copy that triggers a cudaStreamSynchronize (~8.5 ms/step on GB200) during graph replay.

Use torch.full((bs,), num_draft_tokens, ...) instead (async, on-device, identical values).

Stack: #29413 → #29414 → #29415

Tested: GSM8K 0.985; the per-step sync is gone from the NVTX trace.

Profile

Before
Screenshot 2026-06-26 at 2 11 48 AM

After
Screenshot 2026-06-26 at 2 12 36 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 optimizes the attention layer by replacing host-to-device tensor copies with on-device initialization using torch.full to avoid blocking the host. The reviewer suggests further optimizing this by caching the allocated tensor buffer on self and slicing it to the current batch size, which avoids GPU memory allocation overhead on the hot path.

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 +1253 to +1261
# Fill the constant per-req qo lengths (num_draft_tokens) on-device;
# torch.tensor(list, device=cuda) does a pageable H2D copy that
# blocks the host on the whole queued stream.
extend_seq_lens = torch.full(
(bs,),
self.speculative_num_draft_tokens,
dtype=torch.int32,
device=self.device,
)

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

While creating extend_seq_lens on-device using torch.full successfully avoids the host-to-device synchronization, calling torch.full on every step still incurs GPU memory allocation overhead on the hot path. Since self.speculative_num_draft_tokens is constant, we can lazily allocate and cache a static buffer on self and slice it to the current batch size bs. This completely eliminates GPU memory allocation overhead for this tensor during hot-path execution.

            # Fill the constant per-req qo lengths (num_draft_tokens) on-device;
            # torch.tensor(list, device=cuda) does a pageable H2D copy that
            # blocks the host on the whole queued stream.
            # Cache the buffer to avoid GPU memory allocation overhead on the hot path.
            if not hasattr(self, "_extend_seq_lens_buf") or self._extend_seq_lens_buf.shape[0] < bs:
                self._extend_seq_lens_buf = torch.full(
                    (bs,),
                    self.speculative_num_draft_tokens,
                    dtype=torch.int32,
                    device=self.device,
                )
            extend_seq_lens = self._extend_seq_lens_buf[:bs]

Comment on lines +1293 to +1300
# See target-verify note: fill on-device to avoid the blocking
# pageable H2D from torch.tensor(list, device=cuda).
extend_seq_lens = torch.full(
(bs,),
self.speculative_num_draft_tokens,
dtype=torch.int32,
device=self.device,
)

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

Similar to the target-verify branch, caching the extend_seq_lens buffer on self avoids GPU memory allocation overhead on the hot path of draft-extend.

            # See target-verify note: fill on-device to avoid the blocking
            # pageable H2D from torch.tensor(list, device=cuda).
            # Cache the buffer to avoid GPU memory allocation overhead on the hot path.
            if not hasattr(self, "_extend_seq_lens_buf") or self._extend_seq_lens_buf.shape[0] < bs:
                self._extend_seq_lens_buf = torch.full(
                    (bs,),
                    self.speculative_num_draft_tokens,
                    dtype=torch.int32,
                    device=self.device,
                )
            extend_seq_lens = self._extend_seq_lens_buf[:bs]

kpham-sgl and others added 2 commits June 26, 2026 09:07
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]>
Both the target-verify and draft-extend branches built the constant per-req
QO-length tensor with torch.tensor([num_draft_tokens] * bs, device=cuda),
which does a pageable host-to-device copy. That copy issues a
cudaStreamSynchronize that blocks the host on the whole queued stream during
graph replay (~8.5 ms/step on GB200), serializing GPU work on the spec-v2
decode critical path.

Build the tensor on-device with torch.full((bs,), num_draft_tokens, ...)
instead, which is enqueued asynchronously and avoids the H2D sync. Values are
identical, so graph replay output is unchanged.

Co-authored-by: Jackey Hua <[email protected]>
Co-authored-by: Cursor <[email protected]>
@Fridge003

Copy link
Copy Markdown
Collaborator

Please fix lint

Base automatically changed from kp/dsa-seq-lens-cpu-optional to kp/dsa-draft-extend-cudagraph June 27, 2026 05:17
@Fridge003
Fridge003 merged commit 4324ffc into kp/dsa-draft-extend-cudagraph Jun 27, 2026
88 of 100 checks passed
@Fridge003
Fridge003 deleted the kp/dsa-remove-graph-metadata-sync branch June 27, 2026 05:18
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