Prhub

#32694 [Refactor] Move sampling tokenizer validation helper

原始 PR 作者 merrymercy 合并时间 2026-07-29 07:48 文件变更 1 提交数 1 评论 1 代码增减 +30 / -30

执行摘要

移动 tokenizer 验证函数位置

PR body 指出:“Keep the SamplingParams declaration near the top of its module while retaining tokenizer-dependent validation as a module-level helper.” 即为了让 SamplingParams 类定义更靠近模块顶部,将 tokenizer 相关的验证函数移到最后。

无需精读。这是一个机械性的代码重排,可以直接合入。

讨论亮点

无 review 讨论。只有一条 gemini-code-assist 的自动评论,说明其已停止服务。

实现拆解

  1. python/sglang/srt/sampling/sampling_params.py 中,将 raise_if_tokenizer_required 函数的定义从 SamplingParams 类之前(第 45-72 行)删除。
  2. 将该函数的定义原封不动地追加到文件末尾(第 296-323 行),位于 _max_length_from_subpattern 之后。
  3. 函数体、参数、文档字符串、所有调用点均未做任何其他改动。测试、配置、部署等均无需变更。
文件 模块 状态 重要度
python/sglang/srt/sampling/sampling_params.py 采样器 modified 6.9

关键符号

raise_if_tokenizer_required

关键源码片段

python/sglang/srt/sampling/sampling_params.py core-logic

唯一修改的文件,将 raise_if_tokenizer_required 从类定义之前移动到文件末尾。

# python/sglang/srt/sampling/sampling_params.py# ...(文件头部导入、类型定义、常量、logger 等保持不变)class SamplingParams(msgspec.Struct, kw_only=True, array_like=True):
    """
    The sampling parameters.
    ...
    """
    # 类定义保持不变,现在更靠近文件顶部
    ...# ...(_max_length_from_subpattern 辅助函数保持不变)# 以下是移动后的位置:文件末尾
# 将 tokenizer 依赖验证放在所有辅助函数之后,
# 使 SamplingParams 类定义更靠近模块顶部,提升可读性。def raise_if_tokenizer_required(
    tokenizer, stop_strs, stop_regex_strs, min_new_tokens=0
):
    """Raise ValueError if tokenizer-dependent features are used without a tokenizer.    String-based stop conditions (stop_strs, stop_regex_strs) require tokenizer.decode()
    to convert output token IDs to text for matching. min_new_tokens requires the
    tokenizer's eos_token_id to penalize. When skip_tokenizer_init=True, these cannot
    be used.
    """
    if tokenizer is not None:
        return
​
    if stop_strs:
        raise ValueError(
            f"stop={stop_strs!r} is unavailable when skip_tokenizer_init=True "
            "(requires tokenizer to decode tokens to text for matching)."
        )
    if stop_regex_strs:
        raise ValueError(
            f"stop_regex={stop_regex_strs!r} is unavailable when skip_tokenizer_init=True "
            "(requires tokenizer to decode tokens to text for matching)."
        )
    if min_new_tokens > 0:
        raise ValueError(
            f"min_new_tokens={min_new_tokens} is unavailable when skip_tokenizer_init=True "
            "(requires tokenizer for eos_token_id)."
        )

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险极低。这是纯粹的代码移动,函数签名、行为、调用方均未改变。唯一的微小风险是如果未来有其他代码依赖该函数在文件中的相对位置(极少见),但 Python 中模块级函数的定义顺序不影响运行时行为。

对用户和系统无影响:功能完全等价。对团队阅读代码时的可维护性略有提升,因为 SamplingParams 类现在更靠近模块顶部。

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论