[Spec Decode] Structured output under concurrency on a PP shape

Ports upstream vLLM PRs #54442 and #56802 (issue #54437): never sample from an
unmasked row, and key draft-token hand-off snapshots by scheduler step so an
alternating PP microbatch cannot overwrite the slot.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 19:43:58 +07:00
co-authored by Hinata
parent 03c8ebae34
commit 17c1c70447
12 changed files with 262 additions and 43 deletions
+56 -3
View File
@@ -186,6 +186,39 @@ from vllm.v1.worker.workspace import lock_workspace, use_workspace_lane
logger = init_logger(__name__)
def grammar_invalid_draft_positions(
input_batch: InputBatch,
grammar_req_ids: list[str],
num_acceptable_drafts: list[int] | None,
device: torch.device,
) -> torch.Tensor | None:
"""Indices of `draft_sampled` entries whose bitmask row is permissive.
Drafts from `num_acceptable_drafts` on were verified against rows carrying
`_full_mask`, so accepting one would sample with no grammar constraint.
Returns None when there is nothing to invalidate.
"""
if not grammar_req_ids or input_batch.num_draft_tokens == 0:
return None
cu_num_logits = input_batch.cu_num_logits_np.tolist()
req_id_to_idx = {req_id: i for i, req_id in enumerate(input_batch.req_ids)}
positions: list[int] = []
for i, req_id in enumerate(grammar_req_ids):
req_idx = req_id_to_idx.get(req_id)
if req_idx is None:
continue
# Without the field (an older scheduler, or warmup) invalidate the whole
# window, which is the conservative choice.
num_acceptable = (
num_acceptable_drafts[i] if num_acceptable_drafts is not None else 0
)
start = cu_num_logits[req_idx] + 1 + num_acceptable
positions.extend(range(start, cu_num_logits[req_idx + 1]))
if not positions:
return None
return torch.tensor(positions, dtype=torch.int64, device=device)
class GPUModelRunner(LoRAModelRunnerMixin):
def __init__(self, vllm_config: VllmConfig, device: torch.device):
self.vllm_config = vllm_config
@@ -288,7 +321,10 @@ class GPUModelRunner(LoRAModelRunnerMixin):
self.use_aux_hidden_state_outputs = True
# Draft tokens propagation - for spec-dec + struct outputs.
self.draft_tokens_handler = DraftTokensHandler(self.device)
self.draft_tokens_handler = DraftTokensHandler(
self.device,
max_snapshots=max(8, vllm_config.max_concurrent_batches + 1),
)
self.pcp_manager: pcp.PCPManager | None = None
@@ -1470,6 +1506,15 @@ class GPUModelRunner(LoRAModelRunnerMixin):
total_num_logits,
self.model_state.num_new_sampled_tokens_per_step,
)
if self.num_speculative_steps > 0:
# Snapshot the drafts that were just written into input_ids, tagged
# with this scheduler step. Proposal-time drafts (set later in
# sample_tokens) are for the next step and must not overwrite this.
self.draft_tokens_handler.snapshot_consumed_drafts(
scheduler_output.scheduler_step,
input_batch,
self.req_states.draft_tokens[input_batch.idx_mapping],
)
fast_prefill = None
if self.fast_prefill is not None:
@@ -1538,6 +1583,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
else None
),
)
invalid_draft_positions = None
if self.pcp_manager is not None:
input_batch = self.pcp_manager.partition_batch(input_batch, batch_desc)
return input_batch
@@ -1620,6 +1666,12 @@ class GPUModelRunner(LoRAModelRunnerMixin):
grammar_output.structured_output_request_ids,
grammar_output.num_acceptable_drafts,
)
invalid_draft_positions = grammar_invalid_draft_positions(
input_batch,
grammar_output.structured_output_request_ids,
grammar_output.num_acceptable_drafts,
self.device,
)
sampler_output: SamplerOutput | None
if input_batch.num_reqs == 0:
@@ -1638,6 +1690,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
input_batch,
# Draft logits are needed for probabilistic rejection sampling.
self.speculator.draft_logits,
invalid_draft_positions,
invalid_drafts,
)
@@ -2278,8 +2331,8 @@ class GPUModelRunner(LoRAModelRunnerMixin):
return async_output
def take_draft_token_ids(self) -> DraftTokenIds | None:
return self.draft_tokens_handler.get_draft_tokens()
def take_draft_token_ids(self, step_id: int | None = None) -> DraftTokenIds | None:
return self.draft_tokens_handler.get_draft_tokens(step_id=step_id)
@torch.inference_mode()
@step_eplb_after()