Prhub

#23669 Clean up noisy startup warnings from third-party deps

原始 PR 作者 merrymercy 合并时间 2026-04-27 18:10 文件变更 7 提交数 4 评论 2 代码增减 +203 / -15

执行摘要

清理启动时第三方库噪音警告

Server startup logs are cluttered with noisy warnings from third-party dependencies (torchao, transformers v5) that are irrelevant to the user. These changes silence the noise without changing any behavior.

值得阅读,尤其是 hf_transformers_patches.py 中的日志压制技巧和 common.py 中的 API 迁移方法,可作为处理第三方库警告和 transformers 升级的参考模式。

讨论亮点

本 PR 无 review 讨论,作者直接合并。仅有 bot 提示 quota 已满和 author 的 /tag-and-rerun-ci 命令。

实现拆解

  1. 压制 torchao 警告:在 hf_transformers_patches.py::_patch_removed_symbols 中,导入 modeling_llama 前临时将 torchao 日志级别设为 ERROR,避免其版本不匹配警告。
  2. 替换弃用配置键:在 common.py::get_hf_text_config 中将 config.torch_dtype 替换为 config.dtype,消除 transformers v5 弃用警告。
  3. 替换图片处理器基类:在多模态处理器 base_processor.pyernie45_vl.py 中将 BaseImageProcessorFast 替换为 BaseImageProcessor,适配 transformers v5 移除 Fast 后缀。
  4. 降低平台检测日志级别:在 platforms/__init__.py 中将 "No platform detected" 从 warning 降为 debug,减少非必要输出。
  5. 新增诊断文档:创建 .claude/skills/clean-startup-log/SKILL.md,记录日志清理方法与排查流程。
    测试方面:PR body 要求验证无警告且多模态模型正常,但未包含自动化测试用例。
文件 模块 状态 重要度
python/sglang/srt/utils/hf_transformers_patches.py 补丁层 modified 6.19
.claude/skills/clean-startup-log/SKILL.md 文档 added 5.37
python/sglang/srt/utils/hf_transformers/common.py 配置层 modified 5.59
python/sglang/srt/multimodal/processors/base_processor.py 多模态处理 modified 4.67
python/sglang/srt/multimodal/processors/ernie45_vl.py 多模态处理 modified 4.67
python/sglang/srt/multimodal/processors/kimi_k25.py 多模态处理 modified 3.92
python/sglang/srt/platforms/__init__.py 平台检测 modified 3.92

关键符号

_patch_removed_symbols get_hf_text_config

关键源码片段

python/sglang/srt/utils/hf_transformers_patches.py dependency-wiring

核心变更:压制 torchao 启动警告

def _patch_removed_symbols():
    """Re-export symbols removed in transformers v5.4.0."""
    # LlamaFlashAttention2 compat
    try:
        import logging
​
        # 导入 modeling_llama 会触发深层导入链:
        # modeling_llama -> quantizers -> torchao,
        # torchao 会发出不兼容 torch 版本的警告,
        # 在这里临时抑制。
        _torchao_logger = logging.getLogger("torchao")
        _prev_level = _torchao_logger.level
        _torchao_logger.setLevel(logging.ERROR)
        try:
            from transformers.models.llama import modeling_llama
        finally:
            _torchao_logger.setLevel(_prev_level)
​
        if not hasattr(modeling_llama, "LlamaFlashAttention2"):
            if hasattr(modeling_llama, "LlamaAttention"):
                modeling_llama.LlamaFlashAttention2 = modeling_llama.LlamaAttention
    except ImportError:
        logger.warning(
            "Could not import transformers.models.llama.modeling_llama; "
            "LlamaFlashAttention2 compat patch not applied."
        )
​
    # is_flash_attn_greater_or_equal_2_10 compat
    try:
        import transformers.utils as _u
​
        if not hasattr(_u, "is_flash_attn_greater_or_equal_2_10"):
            if hasattr(_u, "is_flash_attn_greater_or_equal"):
                _u.is_flash_attn_greater_or_equal_2_10 = (
                    lambda: _u.is_flash_attn_greater_or_equal("2.10.0")
                )
            else:
                _u.is_flash_attn_greater_or_equal_2_10 = lambda: False
    except ImportError:
        logger.warning(
            "Could not import transformers.utils; "
            "is_flash_attn_greater_or_equal_2_10 compat patch not applied."
        )
python/sglang/srt/utils/hf_transformers/common.py core-logic

替换 torch_dtype 为 dtype 消除弃用警告

def get_hf_text_config(config: PretrainedConfig):
    """Get the sub config relevant to LLM for multi modal models."""
    # Skip for custom Llava models
    if config.architectures is not None:
        class_name = config.architectures[0]
        if class_name.startswith("Llava") and class_name.endswith("ForCausalLM"):
            setattr(config, "dtype", torch.float16)
            return config
​
    text_config = None
​
    # Convert dict sub-configs to PretrainedConfig
    parent_dtype = getattr(config, "dtype", None) # 使用 dtype 替代已弃用的 torch_dtype
    for _attr in ("text_config", "llm_config", "language_config", "thinker_config"):
        _sub = getattr(config, _attr, None)
        if isinstance(_sub, dict):
            _converted = PretrainedConfig(**_sub)
            if getattr(_converted, "dtype", None) is None and parent_dtype is not None:
                _converted.dtype = parent_dtype # 保证子配置继承父配置的 dtype
            setattr(config, _attr, _converted)
​
    # Priority: thinker_config > llm_config > language_config > text_config
    if hasattr(config, "thinker_config"):
        thinker_config = config.thinker_config
        if hasattr(thinker_config, "text_config"):
            setattr(
                thinker_config.text_config,
                "dtype", # 原为 torch_dtype
                getattr(thinker_config, "dtype", None), # 原为 torch_dtype
            )
            text_config = thinker_config.text_config
        else:
            text_config = thinker_config
    elif hasattr(config, "llm_config"):
        assert hasattr(config.llm_config, "num_attention_heads")
        text_config = config.llm_config
    elif hasattr(config, "language_config"):
        text_config = config.language_config
    elif hasattr(config, "text_config"):
        assert hasattr(config.text_config, "num_attention_heads")
        text_config = config.text_config
​
    normalize_rope_scaling_compat(config)
​
    if text_config is not None:
        return _patch_text_config(config, text_config)
    return config

评论区精华

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

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

风险与影响

风险极低。

  • torch_dtypedtype:transformers v5 中 torch_dtype 仍可用但触发弃用警告,替换为 dtype 完全向后兼容。
  • BaseImageProcessorFastBaseImageProcessor:前者是后者的子类,isinstance 检查父类同样匹配,多模态处理行为不变。
  • 日志级别调整不影响任何功能逻辑。
  • torchao 日志压制仅作用于特定导入时刻,不影响后续 torchao 功能。

对用户:启动日志显著干净,无功能性影响。对系统:无性能回归。对团队:降低 log 噪音,便于问题诊断;新增的 SKILL.md 文档可作为后续日志清理的模板。

低风险 向后兼容 无行为变更

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论