Prhub

#44965 [Bugfix] Reject out-of-range temperature values in SamplingParams

原始 PR 作者 panpan0000 合并时间 2026-06-15 14:12 文件变更 1 提交数 2 评论 3 代码增减 +6 / -0

执行摘要

为 SamplingParams.temperature 增加上界检查

PR body 指出 Python 前端 SamplingParams._verify_args 仅检查 temperature 下界(>= 0),而 Rust 前端通过 #[validate(range(min = 0.0, max = 2.0))] 在 ChatCompletionRequest.temperature 上已强制 OpenAI 规范 [0,2]。此 gap 导致 temperature=3 等值可静默流入引擎并产生正常响应,与规范不符。

值得合并,这是一个小而正确的 bugfix,解决了前后端验证不一致的问题。变更仅 6 行,逻辑清晰,已通过 reviewer 审核。

讨论亮点

Reviewer DarkLight1337 评论 "Seems reasonable to me as well, but please remove the tests as they aren't really that useful",作者据此删除了单元测试。可见 reviewer 认为该变更逻辑简单,无需额外测试覆盖。

实现拆解

步骤 1:在 vllm/sampling_params.py 的 _verify_args 方法中,在已有的下界检查 if self.temperature < 0.0 之后、top_p 检查之前,新增一个独立的上界检查分支:if self.temperature > 2.0: raise VLLMValidationError(...)。

步骤 2:错误消息统一为 f"temperature must be in [0, 2], got {self.temperature}.",与 Rust 端语义对齐,并附带 parameter="temperature" 和 value=self.temperature 参数。

步骤 3:测试验证(在 REPL 中手动完成),确认 in-range 值通过、out-of-range 值抛出异常。应 reviewer 要求,将原先包含的单元测试移除。

文件 模块 状态 重要度
vllm/sampling_params.py 采样参数 modified 5.12

关键符号

SamplingParams._verify_args

关键源码片段

vllm/sampling_params.py core-logic

这是本次 PR 唯一变更的文件,在 _verify_args 方法中新增 temperature 上界检查。

# vllm/sampling_params.py
# 在 _verify_args 方法中,已有的下界检查之后、top_p 检查之前,新增上界检查
if self.temperature < 0.0:
    raise VLLMValidationError(
        f"temperature must be non-negative, got {self.temperature}.",
        parameter="temperature",
        value=self.temperature,
    )
# 新增:拒绝大于 2.0 的 temperature,与 Rust 端 OpenAI 规范对齐
if self.temperature > 2.0:
    raise VLLMValidationError(
        f"temperature must be in [0, 2], got {self.temperature}.",
        parameter="temperature",
        value=self.temperature,
    )

评论区精华

单元测试是否必要 测试

DarkLight1337 要求删除测试,认为 'aren't really that useful'

结论:作者删除测试,reviewer 随后批准 · 已解决

风险与影响

低风险。变更仅新增一个简单的 if 检查,不涉及核心计算路径或性能敏感代码。唯一风险是:如果用户之前依赖 temperature>2.0 的非标准行为(例如某些自定义采样策略),应用将在升级后抛出异常。但这是预期行为,且与 OpenAI 规范对齐。

用户层面:调用 SamplingParams(temperature=...) 时,超过 2.0 的值将立即收到明确的 VLLMValidationError,而不是静默使用越界值。系统层面:无性能影响,无配置变更需求。团队层面:修复了前后端验证不一致的问题,降低了未来因参数越界导致奇怪行为的可能性。

低风险

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论