[BugFix][ModelRunner V2] Fix stale attn metadata in speculator prefill cudagraph capture (#48261)
Signed-off-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
@@ -8,8 +8,6 @@ from vllm.config.compilation import CUDAGraphMode
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig
|
||||
from vllm.v1.worker.gpu.block_table import BlockTables
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import (
|
||||
AttentionState,
|
||||
AttentionStatePair,
|
||||
BatchExecutionDescriptor,
|
||||
CudaGraphManager,
|
||||
prepare_inputs_to_capture,
|
||||
@@ -19,45 +17,15 @@ from vllm.v1.worker.gpu.model_states.interface import ModelState
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
|
||||
class PrefillSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
"""CudaGraphManager for draft prefill, using pre-built attention states
|
||||
from the target model's capture."""
|
||||
class SpeculatorCudaGraphManager(CudaGraphManager):
|
||||
"""CudaGraphManager for draft prefill and decode.
|
||||
|
||||
def capture(
|
||||
self,
|
||||
forward_fn: Callable,
|
||||
attn_states: dict[BatchExecutionDescriptor, AttentionStatePair],
|
||||
progress_bar_desc: str = "Capturing CUDA graphs",
|
||||
) -> None:
|
||||
def create_forward_fn(
|
||||
desc: BatchExecutionDescriptor,
|
||||
warmup: bool,
|
||||
) -> tuple[Callable[[CUDAGraphMode], None], AttentionState]:
|
||||
num_tokens = desc.num_tokens
|
||||
num_reqs = desc.num_reqs or min(num_tokens, self.max_num_reqs)
|
||||
num_tokens_across_dp = (
|
||||
torch.full((self.dp_size,), num_tokens, dtype=torch.int32, device="cpu")
|
||||
if self.dp_size > 1
|
||||
else None
|
||||
)
|
||||
attn_state_pair = attn_states[desc]
|
||||
attn_state = attn_state_pair.warmup if warmup else attn_state_pair.captured
|
||||
attn_metadata, slot_mappings = attn_state
|
||||
fwd = lambda cg_mode: forward_fn(
|
||||
num_reqs,
|
||||
num_tokens,
|
||||
attn_metadata,
|
||||
slot_mappings,
|
||||
num_tokens_across_dp,
|
||||
cg_mode,
|
||||
)
|
||||
return fwd, attn_state
|
||||
|
||||
super().capture(create_forward_fn, progress_bar_desc)
|
||||
|
||||
|
||||
class DecodeSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
"""CudaGraphManager for draft decode, building its own attention metadata."""
|
||||
Builds fresh dummy inputs and attention metadata for every warmup and
|
||||
capture pass so that the contents of the shared persistent buffers
|
||||
(e.g. query_start_loc, seq_lens, FA3 scheduler metadata) always match
|
||||
the batch descriptor being captured. Reusing metadata built during an
|
||||
earlier capture would execute kernels with stale buffer contents.
|
||||
"""
|
||||
|
||||
def capture(
|
||||
self,
|
||||
@@ -72,7 +40,7 @@ class DecodeSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
def create_forward_fn(
|
||||
desc: BatchExecutionDescriptor,
|
||||
warmup: bool,
|
||||
) -> tuple[Callable[[CUDAGraphMode], None], AttentionState]:
|
||||
) -> Callable[[CUDAGraphMode], None]:
|
||||
num_tokens = desc.num_tokens
|
||||
num_reqs = desc.num_reqs or min(num_tokens, self.max_num_reqs)
|
||||
num_tokens_across_dp = (
|
||||
@@ -80,7 +48,7 @@ class DecodeSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
if self.dp_size > 1
|
||||
else None
|
||||
)
|
||||
attn_state = prepare_inputs_to_capture(
|
||||
attn_metadata, slot_mappings = prepare_inputs_to_capture(
|
||||
num_reqs,
|
||||
num_tokens,
|
||||
model_state,
|
||||
@@ -90,9 +58,8 @@ class DecodeSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
kv_cache_config,
|
||||
skip_attn=(desc.cg_mode == CUDAGraphMode.PIECEWISE),
|
||||
)
|
||||
attn_metadata, slot_mappings = attn_state
|
||||
|
||||
fwd = lambda cg_mode: forward_fn(
|
||||
return lambda cg_mode: forward_fn(
|
||||
num_reqs,
|
||||
num_tokens,
|
||||
attn_metadata,
|
||||
@@ -100,6 +67,5 @@ class DecodeSpeculatorCudaGraphManager(CudaGraphManager):
|
||||
num_tokens_across_dp,
|
||||
cg_mode,
|
||||
)
|
||||
return fwd, attn_state
|
||||
|
||||
super().capture(create_forward_fn, progress_bar_desc)
|
||||
|
||||
@@ -12,15 +12,13 @@ from vllm.multimodal import MULTIMODAL_REGISTRY
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.v1.worker.gpu.attn_utils import build_slot_mappings_by_layer
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import (
|
||||
AttentionStatePair,
|
||||
BatchExecutionDescriptor,
|
||||
get_uniform_token_count,
|
||||
)
|
||||
from vllm.v1.worker.gpu.dp_utils import dispatch_cg_and_sync_dp
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch, InputBuffers
|
||||
from vllm.v1.worker.gpu.spec_decode.autoregressive.cudagraph_utils import (
|
||||
DecodeSpeculatorCudaGraphManager,
|
||||
PrefillSpeculatorCudaGraphManager,
|
||||
SpeculatorCudaGraphManager,
|
||||
)
|
||||
from vllm.v1.worker.gpu.spec_decode.speculator import DraftModelSpeculator
|
||||
|
||||
@@ -47,8 +45,8 @@ class AutoRegressiveSpeculator(DraftModelSpeculator):
|
||||
self.max_num_tokens, self.hidden_size, dtype=self.dtype, device=device
|
||||
)
|
||||
|
||||
self.prefill_cudagraph_manager: PrefillSpeculatorCudaGraphManager | None = None
|
||||
self.decode_cudagraph_manager: DecodeSpeculatorCudaGraphManager | None = None
|
||||
self.prefill_cudagraph_manager: SpeculatorCudaGraphManager | None = None
|
||||
self.decode_cudagraph_manager: SpeculatorCudaGraphManager | None = None
|
||||
|
||||
@property
|
||||
def advance_draft_positions(self) -> bool:
|
||||
@@ -62,7 +60,7 @@ class AutoRegressiveSpeculator(DraftModelSpeculator):
|
||||
|
||||
def init_cudagraph_manager(self, cudagraph_mode: CUDAGraphMode) -> None:
|
||||
# Initialize cudagraph manager for draft prefill (draft position 0).
|
||||
self.prefill_cudagraph_manager = PrefillSpeculatorCudaGraphManager(
|
||||
self.prefill_cudagraph_manager = SpeculatorCudaGraphManager(
|
||||
self.vllm_config,
|
||||
self.device,
|
||||
cudagraph_mode,
|
||||
@@ -76,17 +74,14 @@ class AutoRegressiveSpeculator(DraftModelSpeculator):
|
||||
cudagraph_mode = CUDAGraphMode.NONE
|
||||
|
||||
# Initialize cudagraph manager for draft decodes (draft positions > 0).
|
||||
self.decode_cudagraph_manager = DecodeSpeculatorCudaGraphManager(
|
||||
self.decode_cudagraph_manager = SpeculatorCudaGraphManager(
|
||||
self.vllm_config,
|
||||
self.device,
|
||||
cudagraph_mode,
|
||||
decode_query_len=1,
|
||||
)
|
||||
|
||||
def capture(
|
||||
self,
|
||||
attn_states: dict[BatchExecutionDescriptor, AttentionStatePair],
|
||||
) -> None:
|
||||
def capture(self) -> None:
|
||||
logger.info("Capturing model for speculator...")
|
||||
# Reset indices to zeros to prevent stale values from prior
|
||||
# dummy runs to cause out-of-bounds indexing during capture.
|
||||
@@ -97,12 +92,19 @@ class AutoRegressiveSpeculator(DraftModelSpeculator):
|
||||
# For FULL graphs, the entire routine is recorded as one graph.
|
||||
# For PIECEWISE, only the model's compiled regions are captured
|
||||
# and the rest (compute_logits, gumbel_sample) runs eagerly.
|
||||
# Draft prefill reuses the target model's attention metadata at
|
||||
# runtime, so capture builds its dummy metadata through the target
|
||||
# model runner's builders and buffers.
|
||||
assert self.prefill_cudagraph_manager is not None
|
||||
if self.prefill_cudagraph_manager.use_breakable_cg:
|
||||
self.prefill_cudagraph_manager.init_breakable_cg_runner(self.model)
|
||||
self.prefill_cudagraph_manager.capture(
|
||||
self._prefill,
|
||||
attn_states,
|
||||
self.model_state,
|
||||
self.target_input_buffers,
|
||||
self.block_tables,
|
||||
self.target_attn_groups,
|
||||
self.kv_cache_config,
|
||||
progress_bar_desc="Capturing prefill CUDA graphs",
|
||||
)
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ class DFlashCudaGraphManager(CudaGraphManager):
|
||||
def create_forward_fn(
|
||||
desc: BatchExecutionDescriptor,
|
||||
warmup: bool,
|
||||
) -> tuple[Callable[[CUDAGraphMode], None], AttentionState]:
|
||||
) -> Callable[[CUDAGraphMode], None]:
|
||||
num_tokens = desc.num_tokens
|
||||
num_reqs = desc.num_reqs or min(num_tokens, self.max_num_reqs)
|
||||
num_tokens_across_dp = (
|
||||
@@ -98,7 +98,7 @@ class DFlashCudaGraphManager(CudaGraphManager):
|
||||
)
|
||||
attn_metadata, slot_mappings = attn_state
|
||||
|
||||
fwd = lambda cg_mode: forward_fn(
|
||||
return lambda cg_mode: forward_fn(
|
||||
num_reqs,
|
||||
num_tokens,
|
||||
attn_metadata,
|
||||
@@ -106,6 +106,5 @@ class DFlashCudaGraphManager(CudaGraphManager):
|
||||
num_tokens_across_dp,
|
||||
cg_mode,
|
||||
)
|
||||
return fwd, attn_state
|
||||
|
||||
super().capture(create_forward_fn, progress_bar_desc)
|
||||
|
||||
@@ -25,6 +25,7 @@ from vllm.v1.worker.gpu.spec_decode.dflash.utils import (
|
||||
)
|
||||
from vllm.v1.worker.gpu.spec_decode.speculator import DraftModelSpeculator
|
||||
from vllm.v1.worker.gpu.spec_decode.utils import get_parallel_drafting_token_id
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -97,7 +98,7 @@ class DFlashSpeculator(DraftModelSpeculator):
|
||||
decode_query_len=self.num_query_per_req,
|
||||
)
|
||||
|
||||
def capture(self, attn_states: dict | None = None) -> None:
|
||||
def capture(self) -> None:
|
||||
logger.info("Capturing model for %s speculator...", self._speculator_name)
|
||||
# Reset sampling indices to zero to prevent stale values from prior
|
||||
# dummy runs from being baked into the captured graph.
|
||||
@@ -128,8 +129,16 @@ class DFlashSpeculator(DraftModelSpeculator):
|
||||
model_state: ModelState,
|
||||
kv_cache_config: KVCacheConfig,
|
||||
block_tables: BlockTables,
|
||||
target_input_buffers: InputBuffers,
|
||||
target_attn_groups: list[list[AttentionGroup]],
|
||||
) -> None:
|
||||
super().set_attn(model_state, kv_cache_config, block_tables)
|
||||
super().set_attn(
|
||||
model_state,
|
||||
kv_cache_config,
|
||||
block_tables,
|
||||
target_input_buffers,
|
||||
target_attn_groups,
|
||||
)
|
||||
|
||||
self.draft_kv_cache_group_ids = [
|
||||
gid for gid, g in enumerate(self.attn_groups) if g
|
||||
|
||||
@@ -18,13 +18,10 @@ from vllm.v1.worker.gpu.attn_utils import (
|
||||
init_attn_backend,
|
||||
)
|
||||
from vllm.v1.worker.gpu.block_table import BlockTables
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import (
|
||||
AttentionStatePair,
|
||||
BatchExecutionDescriptor,
|
||||
)
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch, InputBuffers
|
||||
from vllm.v1.worker.gpu.model_states.interface import ModelState
|
||||
from vllm.v1.worker.gpu.sample.gumbel import gumbel_sample
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -35,10 +32,7 @@ class BaseSpeculator(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def capture(
|
||||
self,
|
||||
attn_states: dict[BatchExecutionDescriptor, AttentionStatePair],
|
||||
) -> None:
|
||||
def capture(self) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
@@ -186,6 +180,8 @@ class DraftModelSpeculator(BaseSpeculator):
|
||||
model_state: ModelState,
|
||||
kv_cache_config: KVCacheConfig,
|
||||
block_tables: BlockTables,
|
||||
target_input_buffers: InputBuffers,
|
||||
target_attn_groups: list[list[AttentionGroup]],
|
||||
) -> None:
|
||||
self.model_state = model_state
|
||||
self.kv_cache_config = kv_cache_config
|
||||
@@ -196,6 +192,12 @@ class DraftModelSpeculator(BaseSpeculator):
|
||||
active_layer_names=self.draft_attn_layer_names,
|
||||
)
|
||||
self.block_tables = block_tables
|
||||
# The target model runner's buffers and attention groups. Draft
|
||||
# prefill reuses the target model's attention metadata, so its
|
||||
# cudagraph capture must build dummy metadata through the same
|
||||
# builders and buffers.
|
||||
self.target_input_buffers = target_input_buffers
|
||||
self.target_attn_groups = target_attn_groups
|
||||
|
||||
def _build_draft_attn_metadata(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user