Prhub

#31149 Extract expert location updating into EPLBManager

原始 PR 作者 fzyzcjy 合并时间 2026-07-14 15:54 文件变更 2 提交数 3 评论 0 代码增减 +66 / -50

执行摘要

将专家位置更新逻辑提取到 EPLBManager

PR 标题明确表示要提取专家位置更新逻辑到 EPLBManager。这是对 ModelRunner 进行逐步拆解的系列工作之一,旨在缩小每个类的职责范围,使系统更易于维护和扩展。

建议快速审查,确认参数注入正确即可合并。该 PR 是 ModelRunner 拆解系列的重要一环,值得关注其与 MoE/EP 设置提取 PR 的协调性。

讨论亮点

本次 PR 没有 review 评论,讨论主要反映在 commit message 中。

实现拆解

  1. 准备提取:在第一个 commit 中,对 update_expert_location 方法进行重命名、添加 @staticmethod**kwargs 及回调参数,为移动做准备(涉及 model_runner.py)。
  2. 创建独立函数:在第二个 commit 中,将修改后的逻辑复制粘贴到 eplb/expert_location_updater.py 模块中(临时中间产物)。
  3. 最终移至 EPLBManager:在第三个 commit 中,将函数最终移至 eplb/eplb_manager.py 作为模块级函数 update_expert_location_with_recovery,并更新 EPLBManager.rebalance() 中的调用点,改为显式传参(从 ModelRunner 提取所需字段)。同时删除 ModelRunner 中的旧方法,并清理不再需要的导入。
文件 模块 状态 重要度
python/sglang/srt/model_executor/model_runner.py 模型执行器 modified 7.62
python/sglang/srt/eplb/eplb_manager.py 专家负载均衡 modified 7.71

关键符号

update_expert_location_with_recovery update_expert_location

关键源码片段

python/sglang/srt/eplb/eplb_manager.py core-logic

新增模块级函数 `update_expert_location_with_recovery`,包含从 ModelRunner 迁移的专家位置更新逻辑,改进了内聚性和可测试性。

def update_expert_location_with_recovery(
    *,
    expert_location_updater: ExpertLocationUpdater,
    model: nn.Module,
    new_expert_location_metadata: ExpertLocationMetadata,
    update_layer_ids: List[int],
    nnodes: int,
    tp_rank: int,
    expert_backup_client,
    update_weights_from_disk_callable,
    ep_dispatch_algorithm: str,
    init_lplb_solvers_callable,
):
    # 执行专家位置更新,返回需要从 peer 加载的缺失 expert 列表
    p2p_missing_logical_experts = expert_location_updater.update(
        model.routed_experts_weights_of_layer,
        new_expert_location_metadata,
        update_layer_ids=update_layer_ids,
        nnodes=nnodes,
        rank=tp_rank,
    )
​
    if len(p2p_missing_logical_experts) > 0:
        # 根据模型能力决定是部分加载还是全量重载
        if callable(getattr(model, 'generate_weight_name_filter', None)):
            weight_name_filter = model.generate_weight_name_filter(p2p_missing_logical_experts)
        else:
            logger.info('[Elastic EP] Model does not implement generate_weight_name_filter. Performing full weight reload.')
            weight_name_filter = None
​
        if expert_backup_client is not None and expert_backup_client.use_backup:
            # 从 DRAM 备份加载缺失权重
            expert_backup_client.update_weights(weight_name_filter)
        else:
            # 从磁盘加载缺失权重
            update_weights_from_disk_callable(
                get_server_args().model_path,
                get_server_args().load_format,
                weight_name_filter=weight_name_filter,
            )
​
        # 如果使用 LP 调度算法,需要重新初始化 LPLB 求解器
        if ep_dispatch_algorithm == 'lp':
            init_lplb_solvers_callable()

评论区精华

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

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

风险与影响

风险较低。但需注意:新函数采用显式参数传递,若传递给它的参数有误(如 expert_location_updater 未正确初始化),可能导致运行时错误。原逻辑中的 LPLB 求解器重新初始化依赖于 _init_lplb_solvers 可调用对象,若传入的 callable 行为不正确,可能影响 LP 调度。此外,由于没有新增测试,回归风险存在,但迁移的算法逻辑经过验证。

对用户无影响。对系统内部,EPLBManager 的 rebalance 方法现在直接调用模块级函数,不再通过 ModelRunner 间接调用,增强了 EPLBManager 的独立性。ModelRunner 的代码行数减少,有助于后续进一步拆解。

缺乏测试覆盖 核心路径变更 依赖注入变更

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论