[Bugfix][MRV2] Reserve spec-decode lookahead blocks in V2 warmup (#51438)

Signed-off-by: Rahul Chalamala <22563365+rchalamala@users.noreply.github.com>
Signed-off-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: Rahul Chalamala <22563365+rchalamala@users.noreply.github.com>
Co-authored-by: Janelle Cai <janelle.cai@modal.com>
This commit is contained in:
Nick Hill
2026-08-08 23:58:43 +00:00
committed by GitHub
co-authored by Rahul Chalamala Janelle Cai
parent 9e6be4a72b
commit d608dfabfd
4 changed files with 449 additions and 37 deletions
+27
View File
@@ -592,6 +592,33 @@ class VllmConfig:
return self.diffusion_config.canvas_length
return 0
@property
def num_lookahead_tokens(self) -> int:
"""KV slots to reserve past the tokens the target model is scheduled for.
The drafter writes KV for positions beyond the target model's query
range, so every component that reserves blocks must add this margin:
the scheduler through `allocate_slots`, and the worker warmup, which
builds its own `SchedulerOutput`s. Consumers must read this property
rather than re-deriving their own per-method lookahead, so the
scheduler and warmup cannot drift apart.
"""
speculative_config = self.speculative_config
if speculative_config is None:
return 0
if speculative_config.use_dflash():
# DFlash requires an extra lookahead slot since it uses in-fill-style
# decoding instead of standard next-token sampling, so it has a query
# for the last sampled token plus queries for each draft token.
return self.num_speculative_tokens + 1
if speculative_config.use_eagle() or speculative_config.uses_draft_model():
# DSpark (covered by use_eagle) drafts a block of num_speculative_tokens
# query tokens in which the anchor itself is the first prediction
# position (no separate bonus query), so it needs exactly
# num_speculative_tokens lookahead slots.
return self.num_speculative_tokens
return 0
@property
def use_v2_model_runner(self) -> bool:
use_v2_model_runner = envs.VLLM_USE_V2_MODEL_RUNNER
+2 -16
View File
@@ -245,7 +245,7 @@ class Scheduler(SchedulerInterface):
speculative_config = vllm_config.speculative_config
self.use_eagle = False
self.num_spec_tokens = vllm_config.num_speculative_tokens
self.num_lookahead_tokens = 0
self.num_lookahead_tokens = vllm_config.num_lookahead_tokens
self.dynamic_sd_lookup: list[int] | None = None
if speculative_config is not None:
if speculative_config.num_speculative_tokens_per_batch_size:
@@ -254,21 +254,7 @@ class Scheduler(SchedulerInterface):
vllm_max_batch_size=self.scheduler_config.max_num_seqs,
vllm_num_speculative_tokens=self.num_spec_tokens,
)
if speculative_config.use_eagle():
self.use_eagle = True
self.num_lookahead_tokens = self.num_spec_tokens
if speculative_config.uses_draft_model():
self.num_lookahead_tokens = self.num_spec_tokens
if speculative_config.use_dflash():
# DFlash requires an extra lookahead slot since it uses in-fill-style
# decoding instead of standard next-token sampling, so it has a query
# for the last sampled token plus queries for each draft token.
self.num_lookahead_tokens = self.num_spec_tokens + 1
if speculative_config.use_dspark():
# DSpark drafts a block of num_spec_tokens query tokens in which the
# anchor itself is the first prediction position (no separate bonus
# query), so it needs exactly num_spec_tokens lookahead slots.
self.num_lookahead_tokens = self.num_spec_tokens
self.use_eagle = speculative_config.use_eagle()
# Create the KV cache manager.
if hash_block_size is None:
+59 -21
View File
@@ -18,13 +18,61 @@ from vllm.v1.core.sched.output import (
NewRequestData,
SchedulerOutput,
)
from vllm.v1.kv_cache_interface import CrossAttentionSpec, MambaSpec
from vllm.v1.kv_cache_interface import CrossAttentionSpec, KVCacheSpec, MambaSpec
from vllm.v1.request import Request
from vllm.v1.worker.gpu.model_runner import GPUModelRunner
logger = init_logger(__name__)
def _reserved_block_count(
num_tokens: int,
kvcache_spec: KVCacheSpec,
*,
num_lookahead_tokens: int,
max_model_len: int,
max_encoder_len: int,
) -> int:
"""Number of blocks the scheduler would hold for a request of `num_tokens`.
Warmup hand-builds its `SchedulerOutput`s, so it must reserve what
`KVCacheManager.allocate_slots` reserves: the token range plus
`num_lookahead_tokens`, where the speculator writes the KV of its drafts.
"""
if isinstance(kvcache_spec, CrossAttentionSpec):
# Cross-attention blocks cover the encoder sequence only.
return cdiv(max_encoder_len, kvcache_spec.block_size)
num_speculative_blocks = 0
if isinstance(kvcache_spec, MambaSpec):
# MambaManager appends speculative running-state blocks in every cache
# mode; align mode sizes from the uncapped, lookahead-free token range.
num_speculative_blocks = kvcache_spec.num_speculative_blocks
if kvcache_spec.mamba_cache_mode == "align":
return cdiv(num_tokens, kvcache_spec.block_size) + num_speculative_blocks
num_tokens = min(num_tokens + num_lookahead_tokens, max_model_len)
return cdiv(num_tokens, kvcache_spec.block_size) + num_speculative_blocks
def _warmup_block_counter(
model_runner: GPUModelRunner,
) -> Callable[[int, KVCacheSpec], int]:
"""Bind `_reserved_block_count` to `model_runner`'s reservation policy."""
num_lookahead_tokens = model_runner.vllm_config.num_lookahead_tokens
max_model_len = model_runner.max_model_len
max_encoder_len = getattr(model_runner.model_state, "max_encoder_len", 0)
def block_count(num_tokens: int, kvcache_spec: KVCacheSpec) -> int:
return _reserved_block_count(
num_tokens,
kvcache_spec,
num_lookahead_tokens=num_lookahead_tokens,
max_model_len=max_model_len,
max_encoder_len=max_encoder_len,
)
return block_count
def run_mixed_prefill_decode_warmup(
model_runner: GPUModelRunner,
worker_execute_model: Callable[[SchedulerOutput], Any],
@@ -48,21 +96,20 @@ def run_mixed_prefill_decode_warmup(
kv_cache_groups = model_runner.kv_cache_config.kv_cache_groups
num_kv_cache_groups = len(kv_cache_groups)
group_block_sizes = [g.kv_cache_spec.block_size for g in kv_cache_groups]
block_count = _warmup_block_counter(model_runner)
kv_cache_specs = [g.kv_cache_spec for g in kv_cache_groups]
decode_prefill_block_counts = [
cdiv(decode_prompt_len, block_size) for block_size in group_block_sizes
block_count(decode_prompt_len, s) for s in kv_cache_specs
]
decode_block_counts = [
cdiv(decode_prompt_len + decode_scheduled_tokens, block_size)
for block_size in group_block_sizes
block_count(decode_prompt_len + decode_scheduled_tokens, s)
for s in kv_cache_specs
]
decode_block_deltas = [
decode - prefill
for decode, prefill in zip(decode_block_counts, decode_prefill_block_counts)
]
prefill_block_counts = [
cdiv(prefill_len, block_size) for block_size in group_block_sizes
]
prefill_block_counts = [block_count(prefill_len, s) for s in kv_cache_specs]
required_blocks = sum(decode_block_counts) + sum(prefill_block_counts)
if model_runner.kv_cache_config.num_blocks <= required_blocks:
logger.warning(
@@ -200,19 +247,10 @@ def warmup_kernels(
]
# Compute per-request block counts for each KV cache group.
def _warmup_block_count(num_tokens: int, spec: Any) -> int:
if isinstance(spec, CrossAttentionSpec):
num_tokens = max_encoder_len
num_blocks = cdiv(num_tokens, spec.block_size)
if isinstance(spec, MambaSpec) and spec.mamba_cache_mode == "align":
# Align mode reserves extra blocks beyond the token range for the
# speculative-decode running-state snapshots.
num_blocks += spec.num_speculative_blocks
return num_blocks
block_count = _warmup_block_counter(model_runner)
kv_cache_specs = [g.kv_cache_spec for g in kv_cache_groups]
prefill_block_counts = [_warmup_block_count(prompt_len, s) for s in kv_cache_specs]
decode_block_counts = [_warmup_block_count(decode_len, s) for s in kv_cache_specs]
prefill_block_counts = [block_count(prompt_len, s) for s in kv_cache_specs]
decode_block_counts = [block_count(decode_len, s) for s in kv_cache_specs]
max_blocks_per_req = sum(decode_block_counts)
num_reqs = min(
@@ -316,7 +354,7 @@ def warmup_kernels(
num_tokens = decode_query_len if use_spec else 1
after = req_computed[i] + num_tokens
deltas = [
_warmup_block_count(after, spec) - held
block_count(after, spec) - held
for spec, held in zip(kv_cache_specs, req_blocks[i])
]
cached_req_data.new_block_ids.append(