[Bugfix][Frontend] Leave the final stop token out of chat derender text (#61757)

Signed-off-by: khluu <khluu000@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
Kevin H. Luu
2026-10-15 21:51:34 +08:00
committed by GitHub
co-authored by Claude Opus 5.5 Cyrus Leung
parent 71536bd5f8
commit b7b20b1b8f
2 changed files with 132 additions and 3 deletions
+53 -3
View File
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import Sequence
from functools import cached_property
from typing import Any
from vllm.config import ModelConfig
@@ -130,6 +131,43 @@ class OnlineDerenderer:
generate_response, chat_request, prompt_token_ids
)
@cached_property
def _model_eos_token_ids(self) -> frozenset[int]:
"""EOS ids the engine stops on: the tokenizer's and generation_config's."""
eos_ids: set[int] = set()
if (eos_token_id := self.renderer.get_eos_token_id()) is not None:
eos_ids.add(eos_token_id)
config_eos = self.model_config.try_get_generation_config().get("eos_token_id")
if config_eos is not None:
eos_ids.update([config_eos] if isinstance(config_eos, int) else config_eos)
return frozenset(eos_ids)
def _text_token_ids(
self,
token_ids: list[int],
finish_reason: str | None,
stop_reason: int | str | None,
request: ChatCompletionRequest | None,
) -> list[int]:
"""The ids whose text the engine returns. Like its detokenizer, leave
out the token that stopped generation (EOS when `stop_reason` is None,
a `stop_token_ids` entry when it is an int) unless
`include_stop_str_in_output` is set. The last-id check guards against
`finish_reason` defaulting to "stop" when the client sends neither."""
if (
finish_reason != "stop"
or isinstance(stop_reason, str)
or not token_ids
or (request is not None and request.include_stop_str_in_output)
):
return token_ids
last = token_ids[-1]
if stop_reason is None:
stopped = last in self._model_eos_token_ids
else:
stopped = last == stop_reason
return token_ids[:-1] if stopped else token_ids
def _derender_chat(
self,
generate_response: GenerateTokensResponse,
@@ -182,7 +220,12 @@ class OnlineDerenderer:
# (default True when no request was given).
decoded_text, _ = self._detokenize_delta(
tokenizer,
choice.token_ids,
self._text_token_ids(
choice.token_ids,
choice.finish_reason,
choice.stop_reason,
chat_request,
),
seed_state,
skip_special_tokens=skip_special,
spaces_between_special_tokens=spaces_between,
@@ -447,7 +490,9 @@ class OnlineDerenderer:
delta_tids = choice.token_ids or []
new_text, updated_state = await self._detokenize_delta_async(
tokenizer,
delta_tids,
self._text_token_ids(
delta_tids, choice.finish_reason, choice.stop_reason, chat_request
),
updated_state,
skip_special_tokens=skip_special,
spaces_between_special_tokens=spaces_between,
@@ -617,7 +662,12 @@ class OnlineDerenderer:
# in this method's docstring.
text, detok_state = self._detokenize_delta(
tokenizer,
delta_tids,
self._text_token_ids(
delta_tids,
choice.finish_reason,
choice.stop_reason,
chat_request,
),
detok_state,
skip_special_tokens=skip_special,
spaces_between_special_tokens=spaces_between,