[Async][Feat] support apply penalty or bad_words for async + spec (#30495)

Signed-off-by: zhuhaoran <zhuhaoran.zhr@alibaba-inc.com>
Signed-off-by: izhuhaoran <izhuhaoran@qq.com>
Signed-off-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
zhrrr
2026-01-09 02:31:50 +00:00
committed by GitHub
co-authored by Nick Hill
parent a4ec0c5595
commit 8ff4a99566
4 changed files with 70 additions and 34 deletions
+24 -14
View File
@@ -2721,15 +2721,21 @@ class GPUModelRunner(
) -> SamplerOutput:
# Sample the next token and get logprobs if needed.
sampling_metadata = self.input_batch.sampling_metadata
# Update output token ids with tokens sampled in last step
# if async scheduling and required by current sampling params.
self.input_batch.update_async_output_token_ids()
if spec_decode_metadata is None:
# Update output token ids with tokens sampled in last step
# if async scheduling and required by current sampling params.
self.input_batch.update_async_output_token_ids()
return self.sampler(
logits=logits,
sampling_metadata=sampling_metadata,
)
# Update spec_token_ids with real draft tokens from pre step only when
# output_token_ids is needed (penalties or bad_words are in use).
if self.use_async_scheduling and self._draft_token_req_ids is not None:
draft_token_ids_cpu, _ = self._get_draft_token_ids_cpu()
self.input_batch.update_async_spec_token_ids(draft_token_ids_cpu)
sampler_output = self.rejection_sampler(
spec_decode_metadata,
None, # draft_probs
@@ -3352,8 +3358,6 @@ class GPUModelRunner(
) -> ModelRunnerOutput | AsyncModelRunnerOutput | IntermediateTensors:
kv_connector_output = self.kv_connector_output
self.kv_connector_output = None
self._draft_token_ids = None
self._draft_token_req_ids = None
if self.execute_model_state is None:
# Nothing to do (PP non-final rank case), output isn't used.
@@ -3393,6 +3397,8 @@ class GPUModelRunner(
with record_function_or_nullcontext("gpu_model_runner: sample"):
sampler_output = self._sample(logits, spec_decode_metadata)
self._draft_token_ids = None
self._draft_token_req_ids = None
self.input_batch.prev_sampled_token_ids = None
def propose_draft_token_ids(sampled_token_ids):
@@ -3517,17 +3523,18 @@ class GPUModelRunner(
def take_draft_token_ids(self) -> DraftTokenIds | None:
if not self.num_spec_tokens or not self._draft_token_req_ids:
return None
req_ids = self._draft_token_req_ids
draft_token_ids = self._get_draft_token_ids_cpu(len(req_ids))
draft_token_ids, req_ids = self._get_draft_token_ids_cpu()
return DraftTokenIds(req_ids, draft_token_ids)
def _copy_draft_token_ids_to_cpu(
self, scheduler_output: "SchedulerOutput", zeros_only: bool = False
) -> None:
struct_output = scheduler_output.has_structured_output_requests
if self.use_async_scheduling and not struct_output:
# Draft tokens don't need to be copied to the CPU if async
# scheduling is in use and there are no structured output reqs.
# Check if we need to copy draft tokens to CPU. In async scheduling,
# we only copy when needed for structured output, penalties or bad_words.
if self.use_async_scheduling and not (
scheduler_output.has_structured_output_requests
or self.input_batch.sampling_metadata.output_token_ids
):
return
# We must also set the corresponding request ids.
self._draft_token_req_ids = self.input_batch.req_ids.copy()
@@ -3552,13 +3559,16 @@ class GPUModelRunner(
self.draft_token_ids_cpu[:num_reqs] = 0
self.draft_token_ids_event.record()
def _get_draft_token_ids_cpu(self, num_reqs: int) -> list[list[int]]:
def _get_draft_token_ids_cpu(self) -> tuple[list[list[int]], list[str]]:
if isinstance(self._draft_token_ids, list):
return self._draft_token_ids
return self._draft_token_ids, self.input_batch.req_ids
req_ids = self._draft_token_req_ids
if req_ids is None:
return [], []
assert self.draft_token_ids_event is not None
assert self.draft_token_ids_cpu is not None
self.draft_token_ids_event.synchronize()
return self.draft_token_ids_cpu[:num_reqs].tolist()
return self.draft_token_ids_cpu[: len(req_ids)].tolist(), req_ids
def _copy_valid_sampled_token_count(
self, next_token_ids: torch.Tensor, valid_sampled_tokens_count: torch.Tensor