Repository navigation
[DSA] Remove host H2D sync in _apply_cuda_graph_metadata - #29415
Conversation
There was a problem hiding this comment.
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.
| # 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, | ||
| ) |
There was a problem hiding this comment.
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]| # 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, | ||
| ) |
There was a problem hiding this comment.
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]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]>
6c24944 to
cdc3784
Compare
4e98553 to
8deae83
Compare
|
Please fix lint |
4324ffc
into
kp/dsa-draft-extend-cudagraph
_apply_cuda_graph_metadatabuilt the per-req QO-length tensor withtorch.tensor([num_draft_tokens] * bs, device=cuda)— a pageable H2D copy that triggers acudaStreamSynchronize(~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

After
