Prhub

#21543 [NPU] fix rope_theta get error for baichuan2-13b-chat model

原始 PR 作者 ZeyuanChen2000 合并时间 2026-04-29 12:01 文件变更 1 提交数 1 评论 13 代码增减 +1 / -1

执行摘要

NPU 上 Baichuan2-13B-Chat 因 rope_theta 缺失崩溃

PR#21135 引入了 get_rope_config() 函数,直接访问 config.rope_theta,而 Baichuan2-13B-Chat 等 trust-remote-code 模型的 config.json 中不包含该属性,导致加载模型时 AttributeError。

低风险快速修复,值得核验。建议作者考虑追加单元测试,覆盖 rope_theta 缺失的场景,防止将来回归。

讨论亮点

机器人 reviewer gemini-code-assist[bot] 建议将多行临时变量写法合并为单行 return 语句,但最终合并版本采用了其建议的简洁形式:return getattr(config, "rope_theta", 10000), getattr(config, "rope_scaling", None)。该建议未被直接采纳为 review suggestion 点击,但作者在最终提交中实现了相同效果。

实现拆解

  1. 定位问题:在 python/sglang/srt/utils/hf_transformers/common.pyget_rope_config 函数中,第 172 行原本直接返回 config.rope_theta,当 config 对象没有 rope_theta 属性时抛出 AttributeError。
  2. 修复方案:将 config.rope_theta 替换为 getattr(config, "rope_theta", 10000),当属性缺失时自动使用默认值 10000。10000 是 RoPE 的标准默认值,与大多数模型的默认设置一致。
  3. 影响范围:仅修改了一行代码,但涉及所有通过 get_rope_config 获取 rope_theta 的模型。
文件 模块 状态 重要度
python/sglang/srt/utils/hf_transformers/common.py 配置加载 modified 4.72

关键符号

get_rope_config

关键源码片段

python/sglang/srt/utils/hf_transformers/common.py core-logic

唯一变更文件,修复 get_rope_config 中 rope_theta 缺失时的 AttributeError。

# python/sglang/srt/utils/hf_transformers/common.py (line 157-172)def get_rope_config(config):
    """Get (rope_theta, rope_params) from config, supporting both v4 and v5.    Trust-remote-code configs or parent configs passed to sub-models may not
    have the v5 ``rope_parameters`` property, so we fall back to the v4-style
    ``config.rope_theta`` / ``config.rope_scaling`` attributes.    Returns:
        (rope_theta, rope_params): In v5, rope_params is the full
        rope_parameters dict (which subsumes rope_scaling and includes
        rope_theta). In v4, rope_params is the rope_scaling dict or None.
    """
    rope_params = getattr(config, "rope_parameters", None)
    if rope_params is not None:
        return rope_params["rope_theta"], rope_params
    # 使用 getattr 避免模型 config 缺少 rope_theta 属性时崩溃
    # 默认值 10000 是 RoPE 的标准 default
    return getattr(config, "rope_theta", 10000), getattr(config, "rope_scaling", None)

评论区精华

代码简洁性建议 style

gemini-code-assist[bot] 建议将临时变量合并为单行 return 语句。

结论:最终提交采用了单行写法,但以不同形式(直接内联 getattr)。 · 已解决

风险与影响

低风险。默认值 10000 是绝大多数模型的通用值,且仅当属性缺失时才使用。不会对已有正常模型造成影响。但未添加测试来验证默认值行为。

直接影响:修复 Baichuan2-13B-Chat 在 NPU 上的加载失败。间接影响:所有缺少 rope_theta 属性的模型(如某些 trust-remote-code 模型)将自动获得正确默认值,避免崩溃。影响范围限定在 NPU 后端,但通用代码路径也受益。

缺少测试覆盖 核心路径变更

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论