Prhub

#36874 [Diffusion] Respect component weight overrides for upsamplers

原始 PR 作者 mickqian 合并时间 2026-08-29 14:23 文件变更 2 提交数 3 评论 0 代码增减 +34 / -2

执行摘要

upsampler 支持组件权重覆盖

PR body 说明:'route spatial_upsampler through the shared exact component weight resolver',旨在让 upsampler 组件与其他组件一样,支持通过 component_weights_paths 指定权重覆盖路径,便于用户使用本地路径、Hub repo 或显式支持的权重源,提升模型加载的灵活性和一致性。

此 PR 简单且聚焦,值得快速浏览,特别是了解 resolve_component_weights_path 的解析逻辑。对理解组件加载的可扩展性有一定价值。

讨论亮点

该 PR 没有 review 评论或讨论,因此没有需要提炼的争议点。

实现拆解

实现包括:

  1. 声明组件权重覆盖支持:在 UpsamplerLoader 类中新增类属性 supports_component_weight_override = True,标记该组件支持权重覆盖。
  2. 路由权重路径:在 load_customized 方法中,将原来直接使用 component_model_path 查找 safetensors 文件的逻辑,改为先调用 self.resolve_component_weights_path(component_model_path, server_args, component_name) 解析最终权重路径,再传给 _find_safetensors_file。同时保留 _load_explicit_config(safetensors_path, component_model_path) 中的原始路径用于配置解析,确保配置来源不变。
  3. 更新测试:修改 test_component_quantization_admission.py 中的 test_upsampler_rejects_quantization_before_loading_weights,传入构造的 server_args(含空 component_weights_paths)而非 None,并新增 test_upsampler_uses_exact_component_weight_override,通过 mock resolve_component_weights_path_find_safetensors_file 验证路由逻辑正确性。
  4. 测试配套说明:此 PR 没有新增配置文件或部署改动,仅含源码与测试的配套修改。
文件 模块 状态 重要度
python/sglang/multimodal_gen/runtime/loader/component_loaders/upsampler_loader.py 加载器 modified 5.19
python/sglang/multimodal_gen/test/unit/test_component_quantization_admission.py 测试 modified 5.03

关键符号

UpsamplerLoader.load_customized

关键源码片段

python/sglang/multimodal_gen/runtime/loader/component_loaders/upsampler_loader.py core-logic

核心逻辑改动,使 upsample 组件支持权重覆盖。

# upsampler_loader.py 核心加载逻辑
class UpsamplerLoader(PlainStateDictComponentLoader):
    component_names = ["spatial_upsampler"]
    expected_library = "diffusers"
    # 声明该组件支持通过 server_args.component_weights_paths 覆盖权重路径
    supports_component_weight_override = True
​
    def load_customized(self, component_model_path, server_args, component_name):
        # 先通过统一解析器解析最终的权重路径(可能被覆盖为本地路径或 Hub repo)
        component_weights_path = self.resolve_component_weights_path(
            component_model_path, server_args, component_name
        )
        # 基于解析后的路径查找 safetensors 文件
        safetensors_path = _find_safetensors_file(component_weights_path)
        # 注意:配置仍从原始路径解析,保持基础配置不受覆盖影响
        raw_config = _load_explicit_config(safetensors_path, component_model_path)
        if raw_config is not None:
            self.ensure_plain_state_dict_checkpoint(raw_config, component_name)
        state_dict = safetensors_load_file(safetensors_path)
        # ... 后续配置推断与模型加载逻辑保持不变 ...
python/sglang/multimodal_gen/test/unit/test_component_quantization_admission.py test-coverage

新增针对 upsampler 权重覆盖路由的回归测试。

# 测试:验证 upsampler 使用精确组件权重覆盖def test_upsampler_uses_exact_component_weight_override(self):
    # 确认该组件声明支持权重覆盖
    self.assertTrue(UpsamplerLoader.supports_component_weight_override)
    # 构造含覆盖路径的 server_args
    server_args = SimpleNamespace(
        component_weights_paths={"spatial_upsampler": "owner/repo/upsampler"}
    )
    with (
        # mock 解析器,返回缓存路径
        patch.object(UpsamplerLoader, "resolve_component_weights_path",
                     return_value="/cache/upsampler.safetensors") as resolve_weights,
        # mock 文件查找,验证传入的是解析后的路径
        patch("sglang.multimodal_gen.runtime.loader.component_loaders."
              "upsampler_loader._find_safetensors_file",
              side_effect=RuntimeError("stop after routing")) as find_weights,
        # 期望触发异常以中断后续加载
        self.assertRaisesRegex(RuntimeError, "stop after routing"),
    ):
        UpsamplerLoader().load_customized(
            "/base/spatial_upsampler", server_args, "spatial_upsampler"
        )
    # 断言解析器被正确调用
    resolve_weights.assert_called_once_with(
        "/base/spatial_upsampler", server_args, "spatial_upsampler"
    )
    # 断言文件查找使用了解析后的路径
    find_weights.assert_called_once_with("/cache/upsampler.safetensors")

评论区精华

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

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

风险与影响

风险较低:

  • 核心变更集中在 upsampler_loader.pyload_customized 方法,修改了权重路径的解析顺序,但未改变配置解析逻辑,可能影响依赖原路径行为的场景,不过 resolve_component_weights_path 应返回与原路径相同的结果(除非有覆盖配置),因此回归风险较小。
  • 如果 resolve_component_weights_path 实现存在边界情况(如未覆盖 server_args 为 None 的情况),在测试中已改为传入 server_args,但生产代码仍可能被以 server_args=None 调用,需确认该路径的兼容性。
  • 测试集中在单元层面,未覆盖端到端加载流程。

影响范围:

  • 用户:可对 upsampler 组件使用权重覆盖路径,提高灵活性和一致性。
  • 系统:加载逻辑的轻微调整,不影响性能或安全性。
  • 团队:为其他组件提供统一权重覆盖模式的参考,促进代码一致性。
缺少端到端测试覆盖 server_args=None 兼容性未验证

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论