Prhub

#30159 [diffusion] Clean up duplicate helper definitions

原始 PR 作者 BBuf 合并时间 2026-07-05 22:05 文件变更 3 提交数 1 评论 1 代码增减 +3 / -58

执行摘要

清理 diffusion 模块重复定义

消除代码库中的重复定义,降低维护成本,避免因重复定义导致的行为不确定性。PR body 明确指出了三类清理:重复的本地方法、重复导入、被后续定义覆盖的 hook 定义。

可直接合并,无需进一步 review。可作为代码清理类 PR 的范例。

讨论亮点

仅有 gemini-code-assist[bot] 自动评论确认了变更内容,未产生人工 review 讨论。

实现拆解

  1. 移除 PipelineConfig 中的重复方法python/sglang/multimodal_gen/configs/pipeline_configs/base.py):删除了 get_model_deployment_configpreprocess_realtime_condition_image 的重复定义(各保留一份定义,删除位置靠后的副本)。
  2. 清理 JoyEcho memory 模块的重复导入python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/joy_echo/memory.py):移除了文件中部和底部的 import torchfrom PIL import Imagefrom torchvision.transforms import functional as TVF 以及 import torchaudio 等重复导入语句,所有导入统一在文件顶部完成。
  3. 复用共享 pytest runnerpython/sglang/multimodal_gen/test/scripts/gen_diffusion_ci_outputs.py):删除了局部定义的 collect_test_items 函数(约 40 行),改为从 sglang.multimodal_gen.test.runner.pytest_runner 导入同名函数。同时移除不再需要的 subprocess 导入。
文件 模块 状态 重要度
python/sglang/multimodal_gen/configs/pipeline_configs/base.py 配置层 modified 5.64
python/sglang/multimodal_gen/test/scripts/gen_diffusion_ci_outputs.py 测试脚本 modified 5.56
python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/joy_echo/memory.py 内存管理 modified 5.1

关键符号

collect_test_items get_model_deployment_config preprocess_realtime_condition_image

关键源码片段

python/sglang/multimodal_gen/configs/pipeline_configs/base.py core-logic

基类 PipelineConfig 中删除了两个方法的重复定义(get_model_deployment_config 和 preprocess_realtime_condition_image),保留了一份正确的定义。

# python/sglang/multimodal_gen/configs/pipeline_configs/base.py
# 删除前(第 225-226 行)存在第一个定义,随后在第 248-250 行又被重复定义了一次。
# 删除后只保留了一份定义,如下所示:def get_model_deployment_config(self) -> ModelDeploymentConfig:
    # return the model-specific config for optimal deployment setting
    return ModelDeploymentConfig()def preprocess_realtime_condition_image(self, batch, _vae_image_processor) -> bool:
    """Realtime hook: optionally preprocess the first-frame condition image
    in-place. Return True if handled (skip the standard path), False to fall
    back to the normal condition-image preprocessing. Default: not handled."""
    return False
python/sglang/multimodal_gen/test/scripts/gen_diffusion_ci_outputs.py test-coverage

删除了局部定义的 collect_test_items 函数(约 40 行实现),改为从共享模块 sglang.multimodal_gen.test.runner.pytest_runner 导入,大幅减少重复代码。

# python/sglang/multimodal_gen/test/scripts/gen_diffusion_ci_outputs.py
# 变更后:从共享模块导入 collect_test_items 和 run_pytest,不再本地定义
import os
import sys
from pathlib import Pathfrom sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
from sglang.multimodal_gen.test.run_suite import (
    SUITES,
    PartitionItem,
    _maybe_pin_update_weights_model_pair,
    get_case_est_time,
    get_suite_files_rel,
    parse_partition_plan,
    partition_items_by_lpt,
)
from sglang.multimodal_gen.test.runner.pytest_runner import (
    collect_test_items, # 之前是本地函数,现在复用共享版本
    run_pytest,
)logger = init_logger(__name__)# 原 collect_test_items 函数(约 40 行)已删除,功能完全由共享版本替代def main():
    ...

评论区精华

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

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

风险与影响

风险极低。变更仅限于删除重复代码(重定义的方法、重复导入、局部替代函数),不改变任何运行时逻辑。但需确认以下两项:一是 PipelineConfig 中被删除的方法在任意子类中均未被依赖(因为重复的方法体相同,删除重复副本不影响行为);二是 gen_diffusion_ci_outputs.pycollect_test_items 的共享版本在行为上与局部版本完全一致(通过 pytest --collect-only 实现,无差异)。

影响范围极小,仅涉及三处代码重复清理。对用户无感知,对开发者而言减少了代码冗余,降低了未来修改时漏改的风险。

无风险

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论