[V1][Misc] Shorten FinishReason enum and use constant strings (#12760)

This commit is contained in:
Nick Hill
2025-02-04 22:43:02 -08:00
committed by GitHub
parent fcf2e3d7fc
commit 3d09e592a8
5 changed files with 25 additions and 21 deletions
+9 -3
View File
@@ -14,11 +14,17 @@ if TYPE_CHECKING:
from vllm.multimodal.inputs import PlaceholderRange
from vllm.sampling_params import SamplingParams
# These are possible values of RequestOutput.finish_reason,
# so form part of the external API.
FINISH_REASON_STRINGS = ("stop", "length", "abort")
class RequestFinishedReason(enum.IntEnum):
class FinishReason(enum.IntEnum):
"""
Reason a request finished - stop, length, or abort.
Int rather than Str for more compact serialization.
stop - a stop string was emitted
length - max_tokens was consumed, or max_model_len was reached
abort - aborted for another reason
@@ -29,7 +35,7 @@ class RequestFinishedReason(enum.IntEnum):
ABORT = 2
def __str__(self):
return self.name.lower()
return FINISH_REASON_STRINGS[self.value]
@dataclass
@@ -62,7 +68,7 @@ class EngineCoreOutput(
request_id: str
new_token_ids: List[int]
finished: bool
finish_reason: Optional[RequestFinishedReason] = None
finish_reason: Optional[FinishReason] = None
stop_reason: Union[int, str, None] = None
+3 -4
View File
@@ -8,8 +8,7 @@ from vllm.logger import init_logger
from vllm.sampling_params import RequestOutputKind
from vllm.transformers_utils.detokenizer_utils import (
AnyTokenizer, convert_prompt_ids_to_tokens, detokenize_incrementally)
from vllm.v1.engine import (EngineCoreOutput, EngineCoreRequest,
RequestFinishedReason)
from vllm.v1.engine import EngineCoreOutput, EngineCoreRequest, FinishReason
logger = init_logger(__name__)
@@ -19,7 +18,7 @@ class DetokenizerOutput:
output_text: str
token_ids: List[int]
finished: bool
finish_reason: Optional[RequestFinishedReason] = None
finish_reason: Optional[FinishReason] = None
stop_reason: Union[int, str, None] = None
@@ -148,7 +147,7 @@ class IncrementalDetokenizer:
stop_str, truncate_to = stop
if truncate_to != -1:
self.output_text = self.output_text[:truncate_to]
finish_reason = RequestFinishedReason.STOP
finish_reason = FinishReason.STOP
stop_reason = stop_str
# TODO: handle stop_token_ids here too?