[Feature] Add server-level token limits and prompt truncation control#7842
Conversation
|
Thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
本 PR 在服务端引入了若干"默认 token 长度限制"配置 (max_completion_tokens / reasoning_max_tokens / response_max_tokens / min_completion_tokens / input_max_tokens),允许通过 CLI 设置 server-level 默认值;当请求未携带相应字段时使用这些默认值,超过 input_max_tokens 的请求将被拒绝。
Changes:
- 在
EngineArgs/ModelConfig上新增 5 个长度相关参数,并在 CLI 和文档中暴露 - 在
BaseDataProcessor上新增set_server_defaults,并在engine_client/async_llm/engine/common_engine各入口处调用以同步 server defaults - 在
base_processor.py与multimodal_processor.py中加入"超长拒绝"以及"用户值/服务端默认值/上下文上限取最小"的合并逻辑
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| fastdeploy/engine/args_utils.py | 新增 5 个 server-level token 长度相关参数及对应 CLI 选项 |
| fastdeploy/config.py | ModelConfig 初始化新字段(默认 None / 1)以接受新参数 |
| fastdeploy/input/base_processor.py | 新增 set_server_defaults 和 process_request_dict 中的长度合并/拒绝逻辑 |
| fastdeploy/input/multimodal_processor.py | 多模态处理流程中加入同样的长度合并/拒绝逻辑 |
| fastdeploy/entrypoints/engine_client.py | 调用 set_server_defaults,并在缺失 max_tokens 时用 max_completion_tokens 兜底 |
| fastdeploy/engine/engine.py | 同上:注入 server defaults 并优先使用 max_completion_tokens 作为缺省 |
| fastdeploy/engine/common_engine.py | 创建 data_processor 后注入 server defaults |
| fastdeploy/engine/async_llm.py | 创建 data_processor 后注入 server defaults |
| docs/parameters.md / docs/zh/parameters.md | 文档同步新增 5 个参数说明 |
CI报告基于以下代码生成(30分钟更新一次): 1 任务总览所有 Required 任务全部通过 ✅,PR 可合并。
2 任务状态汇总2.1 Required任务 : 10/10 通过
2.2 可选任务 — 29/32 通过
3 失败详情(仅 required)无 required 失败任务。
|
| model_group.add_argument( | ||
| "--truncate-prompt-tokens", | ||
| type=lambda x: x.lower() in ("true", "1", "yes"), | ||
| default=EngineArgs.truncate_prompt_tokens, | ||
| help="Whether to truncate prompts that exceed max_model_len. " | ||
| "If True (default), prompts are silently truncated. " | ||
| "If False, a ValueError is raised.", | ||
| ) |
| ) | ||
| # Create data processor | ||
| self.data_processor = self.input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(cfg.serving_limits_config) |
| enable_mm_runtime=self.cfg.enable_mm_runtime, | ||
| ) | ||
| self.data_processor = self.input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(self.cfg.serving_limits_config) |
| ) | ||
| self.enable_logprob = self.fd_config.model_config.enable_logprob | ||
| self.data_processor = input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(self.fd_config.serving_limits_config) |
| ) | ||
| self.enable_logprob = self.fd_config.model_config.enable_logprob | ||
| self.data_processor = input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(self.fd_config.serving_limits_config) |
| enable_mm_runtime=self.cfg.enable_mm_runtime, | ||
| ) | ||
| self.data_processor = self.input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(self.cfg.serving_limits_config) |
| ) | ||
| # Create data processor | ||
| self.data_processor = self.input_processor.create_processor() | ||
| self.data_processor.set_server_defaults(cfg.serving_limits_config) |
| if effective_min > max_tokens: | ||
| raise ValueError(f"min_tokens ({effective_min}) must not exceed max_tokens ({max_tokens})") | ||
| request["min_tokens"] = effective_min |
| if effective_min > max_tokens: | ||
| raise ValueError(f"min_tokens ({effective_min}) must not exceed max_tokens ({max_tokens})") | ||
| request["min_tokens"] = effective_min |
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-06-01 10:21:23
📋 Review 摘要
PR 概述:新增服务级 token 长度限制与 prompt 截断控制配置能力
变更范围:config、engine、entrypoints、input processor、docs、tests
影响面 Tag:[FDConfig] [Engine] [APIServer] [DataProcessor] [Docs]
问题
未发现新的阻塞性问题。历史 Findings 状态见下方。
历史 Findings 修复情况
| Finding | 问题 | 状态 |
|---|---|---|
| F1 | 超长 prompt 从静默截断改为硬拒绝(破坏性行为变更) | |
| F2 | base_processor 与 multimodal_processor 长度限制逻辑完全重复 | |
| F3 | engine_client 中 max_tokens 默认值逻辑与 processor 重复 | |
| F4 | test_engine_client 中 reasoning_max_tokens/response_max_tokens 幽灵属性 |
📝 PR 规范检查
PR 标题 [Feature] Add server-level token limits and prompt truncation control 格式合规,Tag 匹配变更内容。描述结构完整,包含 Motivation / Modifications / Usage or Command / Accuracy Tests / Checklist 所有必填段落。✓ 符合规范。
总体评价
功能实现完整,逻辑正确,测试覆盖充分。历史 review 指出的代码重复和行为变更问题仍未解决,建议后续迭代中抽取公共方法消除 base_processor 与 multimodal_processor 的重复逻辑。
Motivation
本 PR 为服务端新增了统一的长度参数默认值配置能力,使用户在未显式传入请求级参数时,也可以通过服务级配置控制生成长度相关行为;同时新增了输入 token 长度限制,用于提前拦截超长请求。
Modifications
ServingLimitsConfig,并挂载到FDConfig中统一管理。max_completion_tokensreasoning_max_tokensresponse_max_tokensmin_completion_tokensinput_max_tokensasync_llm、common_engine、engine_client初始化阶段,将服务级默认长度配置注入data_processor。max_tokens时,默认使用服务级max_completion_tokens,并受剩余上下文长度约束;max_tokens时,会同时受服务级上限和上下文剩余长度限制;reasoning_max_tokens/response_max_tokens会被约束为不超过最终生效的max_tokens;min_tokens采用max(server_value, request_value)规则,并在超过max_tokens时直接报错。input_max_tokens校验:input_max_tokens时,直接拒绝请求。max_tokens的处理逻辑:max_completion_tokens,优先使用该值作为默认生成长度;max_model_len的默认行为。docs/parameters.mddocs/zh/parameters.mdUsage or Command
示例启动参数:
行为说明:
max_tokens时,默认使用服务级配置max_completion_tokens,并受上下文剩余长度约束;max_tokens时,最终值会被限制为min(请求值, 服务级上限, 上下文剩余长度);reasoning_max_tokens/response_max_tokens时,可使用服务级默认值;reasoning_max_tokens/response_max_tokens的最终值不会超过max_tokens;min_tokens的最终值取服务端配置与请求值中的较大者,若超过max_tokens会直接报错;input_max_tokens时,请求会被直接拒绝;max_model_len时,直接报错。Accuracy Tests
该 PR 不涉及模型前向计算逻辑或 kernel 行为修改,因此无精度测试影响。
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.