[Bugfix][Frontend] Default max_tokens server-side on /inference/v1/generate (#42329)
Signed-off-by: hallerite <git@hallerite.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
f6e868fbdf
commit
97c4317bf5
@@ -2,7 +2,13 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from pydantic import (
|
||||
BaseModel,
|
||||
Field,
|
||||
PrivateAttr,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
|
||||
from vllm.config import ModelConfig
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionLogProbs
|
||||
@@ -108,6 +114,39 @@ class GenerateRequest(BaseModel):
|
||||
description="KVTransfer parameters used for disaggregated serving.",
|
||||
)
|
||||
|
||||
# Tracks which keys the caller explicitly set inside ``sampling_params``
|
||||
# when the request was parsed from a JSON body. Lets the server tell
|
||||
# "client said max_tokens=16" from "client said nothing → dataclass
|
||||
# default 16" so it can apply server-side defaulting only in the latter
|
||||
# case. ``None`` means the request was constructed with a pre-built
|
||||
# ``SamplingParams`` instance (e.g. from internal callers that have
|
||||
# already resolved values), in which case all fields are considered set.
|
||||
_sampling_params_provided_keys: set[str] | None = PrivateAttr(default=None)
|
||||
|
||||
@model_validator(mode="wrap")
|
||||
@classmethod
|
||||
def _capture_sampling_params_provided_keys(cls, data: Any, handler):
|
||||
provided: set[str] | None = None
|
||||
if isinstance(data, dict):
|
||||
sp = data.get("sampling_params")
|
||||
if isinstance(sp, dict):
|
||||
provided = set(sp.keys())
|
||||
instance = handler(data)
|
||||
instance._sampling_params_provided_keys = provided
|
||||
return instance
|
||||
|
||||
def is_sampling_param_provided(self, name: str) -> bool:
|
||||
"""Whether the caller explicitly set ``sampling_params.<name>``.
|
||||
|
||||
For requests parsed from a JSON body, this reflects the raw input
|
||||
dict. For requests constructed with a pre-built ``SamplingParams``
|
||||
instance, all fields are considered provided so server-side defaults
|
||||
do not clobber values already resolved upstream.
|
||||
"""
|
||||
if self._sampling_params_provided_keys is None:
|
||||
return True
|
||||
return name in self._sampling_params_provided_keys
|
||||
|
||||
def build_tok_params(self, model_config: ModelConfig) -> TokenizeParams:
|
||||
return TokenizeParams(
|
||||
max_total_tokens=None,
|
||||
|
||||
@@ -34,7 +34,7 @@ from vllm.entrypoints.serve.disagg.protocol import (
|
||||
GenerateStreamResponse,
|
||||
)
|
||||
from vllm.entrypoints.serve.render.serving import OpenAIServingRender
|
||||
from vllm.entrypoints.utils import should_include_usage
|
||||
from vllm.entrypoints.utils import get_max_tokens, should_include_usage
|
||||
from vllm.inputs import EngineInput, mm_input
|
||||
from vllm.logger import init_logger
|
||||
from vllm.logprobs import Logprob
|
||||
@@ -81,6 +81,18 @@ class ServingTokens(OpenAIServing):
|
||||
"step for incoming requests."
|
||||
)
|
||||
|
||||
# Mirrors ``OpenAIServingChat`` so we can apply server-side
|
||||
# ``max_tokens`` defaulting when the client omits it. Without this,
|
||||
# ``SamplingParams.max_tokens`` falls back to its dataclass default
|
||||
# of 16 and silently truncates every generation.
|
||||
self.default_sampling_params = self.model_config.get_diff_sampling_param()
|
||||
mc = self.model_config
|
||||
self.override_max_tokens = (
|
||||
self.default_sampling_params.get("max_tokens")
|
||||
if mc.generation_config not in ("auto", "vllm")
|
||||
else getattr(mc, "override_generation_config", {}).get("max_new_tokens")
|
||||
)
|
||||
|
||||
async def serve_tokens(
|
||||
self,
|
||||
request: GenerateRequest,
|
||||
@@ -150,6 +162,20 @@ class ServingTokens(OpenAIServing):
|
||||
# Schedule the request and get the result generator.
|
||||
result_generator: AsyncGenerator[RequestOutput, None] | None = None
|
||||
sampling_params = request.sampling_params
|
||||
|
||||
# Apply server-side ``max_tokens`` defaulting when the client did
|
||||
# not set it, matching the OpenAI-compat endpoints. ``SamplingParams``
|
||||
# defaults ``max_tokens`` to 16, which would otherwise silently cap
|
||||
# every generation that omits the field.
|
||||
if not request.is_sampling_param_provided("max_tokens"):
|
||||
sampling_params.max_tokens = get_max_tokens(
|
||||
max_model_len=self.model_config.max_model_len,
|
||||
max_tokens=None,
|
||||
input_length=self._extract_prompt_len(engine_input),
|
||||
default_sampling_params=self.default_sampling_params,
|
||||
override_max_tokens=self.override_max_tokens,
|
||||
)
|
||||
|
||||
if self.force_no_detokenize:
|
||||
sampling_params.detokenize = False
|
||||
if request.stream:
|
||||
|
||||
Reference in New Issue
Block a user