[Spec Decode] Skip metadata build for dflash/dspark when all model backends support it (#60692)

Signed-off-by: Giancarlo Delfin <gdelfin@inferact.ai>
Co-authored-by: Rishi Puri <20074092+puririshi98@users.noreply.github.com>
This commit is contained in:
Giancarlo Delfin
2026-10-12 00:09:10 -07:00
committed by GitHub
co-authored by Rishi Puri
parent 6a70913590
commit 36f3972812
11 changed files with 360 additions and 172 deletions
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Callable, Mapping
from typing import Any
import torch
@@ -84,11 +85,11 @@ class DFlashCudaGraphManager(CudaGraphManager):
kv_cache_config: KVCacheConfig,
max_model_len: int,
causal: bool | Mapping[int, bool],
precompute_context_kv: Callable[[int], None],
prepare_forward: Callable[[int, dict[str, Any] | None], None],
progress_bar_desc: str = "Capturing CUDA graphs",
) -> None:
"""``precompute_context_kv(num_reqs)`` is captured ahead of the query
forward."""
"""``prepare_forward(num_reqs, attn_metadata)`` is captured ahead of the
query forward."""
def create_forward_fn(
desc: BatchExecutionDescriptor,
@@ -115,7 +116,7 @@ class DFlashCudaGraphManager(CudaGraphManager):
attn_metadata, slot_mappings = attn_state
def forward(cg_mode: CUDAGraphMode) -> None:
precompute_context_kv(num_reqs)
prepare_forward(num_reqs, attn_metadata)
forward_fn(
num_reqs,
num_tokens,
@@ -166,12 +166,18 @@ class DFlashSpeculator(DraftModelSpeculator):
self.kv_cache_config,
self.max_model_len,
causal=self._group_causal,
precompute_context_kv=lambda num_reqs: self._precompute_context_kv(
0, self._num_graph_context_tokens(num_reqs)
),
prepare_forward=self._prepare_graph_forward,
progress_bar_desc=f"Capturing {self._speculator_name.lower()} CUDA graphs",
)
def _prepare_graph_forward(
self, num_reqs: int, attn_metadata: dict[str, Any] | None
) -> None:
self._precompute_context_kv(0, self._num_graph_context_tokens(num_reqs))
# Recorded in the FULL graph so replays skip the eager metadata build.
if attn_metadata is not None and self.supports_decode_metadata_update:
self._update_draft_decode_metadata(attn_metadata, num_reqs)
def load_draft_model(
self,
target_model: nn.Module,
@@ -486,29 +492,32 @@ class DFlashSpeculator(DraftModelSpeculator):
else:
self._precompute_context_kv(0, num_target_tokens, dummy_run)
# Rebuild the draft attention metadata even when replaying the FULL
# graph so that any attention metadata builder state is updated.
draft_attn_metadata = self._build_uniform_attn_metadata(
num_reqs=num_reqs,
batch_desc=batch_desc,
num_query_per_req=self.num_query_per_req,
seq_lens_cpu_upper_bound=input_batch.seq_lens_cpu_upper_bound,
step=self.num_query_per_req,
causal=self._group_causal,
)
draft_slot_mappings_by_layer = build_slot_mappings_by_layer(
self.block_tables.slot_mappings[:, :num_tokens_padded],
self.kv_cache_config,
)
is_full_cudagraph = batch_desc.cg_mode == CUDAGraphMode.FULL
# During FULL cudagraph with a model that supports decode metadata updates,
# the metadata update is captured in the graph, and we can skip the full
# build below.
if not (is_full_cudagraph and self.supports_decode_metadata_update):
draft_attn_metadata = self._build_uniform_attn_metadata(
num_reqs=num_reqs,
batch_desc=batch_desc,
num_query_per_req=self.num_query_per_req,
seq_lens_cpu_upper_bound=input_batch.seq_lens_cpu_upper_bound,
step=self.num_query_per_req,
causal=self._group_causal,
)
# DFlash processes all speculative tokens in one forward pass,
# so the real token count is num_query_tokens.
self._prepare_eplb_forward(num_query_tokens)
if batch_desc.cg_mode == CUDAGraphMode.FULL:
if is_full_cudagraph:
assert self.query_cudagraph_manager is not None
self.query_cudagraph_manager.run_fullgraph(batch_desc)
else:
draft_slot_mappings_by_layer = build_slot_mappings_by_layer(
self.block_tables.slot_mappings[:, :num_tokens_padded],
self.kv_cache_config,
)
self._generate_draft(
num_reqs,
num_tokens_padded,