Prhub

#44356 [Bugfix] Fix Deepseek v4 non-mega-moe model init error

原始 PR 作者 wzhao18 合并时间 2026-06-03 09:26 文件变更 1 提交数 1 评论 1 代码增减 +8 / -0

执行摘要

修复 DeepSeek V4 非 Mega MoE 模型初始化崩溃

PR#43339 引入了 DeepSeek V4 的 EPLB(Expert Parallel Load Balancing)支持,但仅修改了 _init_mega_moe_experts 方法,未同步更新 _init_fused_moe_experts。这导致非 Mega MoE 模型在初始化时,从 DeepseekV4MoE 对象上访问 example_moe.n_logical_experts 时抛出 AttributeError,因为该属性在 _init_fused_moe_experts 中未被定义。PR body 中附带了完整的错误栈。

建议合入。该 PR 修复了明确的回归问题,改动量小且安全。代码结构上已将 _init_fused_moe_experts_init_mega_moe_experts 对齐,避免了后续出现类似的属性缺失问题。

讨论亮点

PR 提交后很快获得了两名 reviewer 的批准,没有公开的 review 评论或争议。唯一的评论来自作者请求合入。

实现拆解

  1. 在文件 vllm/models/deepseek_v4/nvidia/model.pyDeepseekV4MoE._init_fused_moe_experts 方法中,在创建 FusedMoE 对象之前,新增了 6 行属性赋值代码,与 _init_mega_moe_experts 保持对称。
  2. 新增的属性包括:n_redundant_experts, n_shared_experts, n_logical_experts, n_physical_experts, n_local_physical_experts, physical_expert_start, physical_expert_end
  3. 这些属性在后续的 extract_moe_parameters 方法中会被使用,确保非 Mega MoE 路径下也能正确获取 expert 数量信息。
文件 模块 状态 重要度
vllm/models/deepseek_v4/nvidia/model.py 模型定义 modified 5.48

关键符号

_init_fused_moe_experts

关键源码片段

vllm/models/deepseek_v4/nvidia/model.py data-contract

修复入口,在 `_init_fused_moe_experts` 方法中补充了因 PR#43339 遗漏的 6 个属性赋值,确保非 Mega MoE 路径初始化时不会因缺少 `n_logical_experts` 等属性而崩溃。

# vllm/models/deepseek_v4/nvidia/model.py
def _init_fused_moe_experts(self, config, quant_config, prefix: str) -> None:
    self.tp_rank = get_tensor_model_parallel_rank()
    assert config.n_routed_experts % self.tp_size == 0
​
    self.n_local_experts = config.n_routed_experts // self.tp_size
    self.experts_start_idx = self.tp_rank * self.n_local_experts
    self.experts_end_idx = self.experts_start_idx + self.n_local_experts
​
    # 以下属性为修复 PR#43339 回归而添加,确保非 Mega MoE 路径与 Mega MoE
    # 路径在 `_init_mega_moe_experts` 中的属性初始化保持一致,避免后续
    # `extract_moe_parameters` 访问 `n_logical_experts` 等属性时崩溃。
    self.n_redundant_experts = 0
    self.n_shared_experts = config.n_shared_experts or 0
    self.n_logical_experts = self.n_routed_experts
    self.n_physical_experts = self.n_logical_experts
    self.n_local_physical_experts = self.n_local_experts
    self.physical_expert_start = self.experts_start_idx
    self.physical_expert_end = self.experts_end_idx
​
    self.experts = FusedMoE(
        shared_experts=self.shared_experts,
        gate=self.gate,
        num_experts=config.n_routed_experts,
        top_k=config.num_experts_per_tok,
        hidden_size=config.hidden_size,
        intermediate_size=config.moe_intermediate_size,
        renormalize=config.norm_topk_prob,
        quant_config=quant_config,
        prefix=f"{prefix}.experts",
        scoring_func=self.scoring_func,
        routed_scaling_factor=self.routed_scaling_factor,
        e_score_correction_bias=self.gate.e_score_correction_bias,
        hash_indices_table=self.gate.tid2eid,
        swiglu_limit=self.swiglu_limit,
        router_logits_dtype=torch.float32,
    )

评论区精华

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

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

风险与影响

风险极低。变更仅添加属性赋值,不修改任何现有逻辑。但需要注意:如果 future 修改改动了 _init_mega_moe_experts 中的属性初始值,需要同步更新 _init_fused_moe_experts,否则类似的回归可能再次出现。

直接影响所有使用非 Mega MoE 模式的 DeepSeek V4 模型(如 DeepSeek-V4-Pro)。修复后,这些模型能正常初始化并运行。不影响 Mega MoE 路径或其他模型。

回归修复 无测试覆盖

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论