Prhub

#29111 [Bugfix] Fix Ministral3 init argument forwarding

原始 PR 作者 BBuf 合并时间 2026-06-25 18:26 文件变更 1 提交数 2 评论 3 代码增减 +37 / -17

执行摘要

修复 Ministral3 初始化参数传递问题

LlamaAttention.init 和 LlamaDecoderLayer.init 现在包含 start_layer 参数(位于 layer_id 之后),但 Ministral3 仍使用位置参数调用基类构造函数,导致参数位移。例如 rope_theta 可能被当作 start_layer 传入,quant_configprefix 也会错误映射,使模型初始化解码脆弱,尤其是 FP8 路径。

建议精读。该 PR 展示了子类继承父类时使用关键字参数传递的重要性,以及新参数 start_layer 在管线并行中的传递链路,值得作为继承的最佳实践范例。

讨论亮点

无 review 评论。

实现拆解

  1. Ministral3Attention.__init__ 中添加 start_layer 参数,并将其通过关键字参数传递给父类 LlamaAttention.__init__,同时将所有其他参数改为关键字传递,消除位置依赖。
  2. Ministral3DecoderLayer.__init__ 中添加 start_layer 参数,将其通过关键字参数传递给父类 LlamaDecoderLayer.__init__,并在构造 Ministral3Attention 时也显式传递 start_layer
  3. Ministral3Model.__init__,将 super().__init__ 的调用改为关键字参数,并在 make_layers 的 lambda 中传递 start_layer=self.start_layer,确保管线并行场景下正确分发。
  4. _init_model 函数中,将 Ministral3Model 的构造改为关键字参数调用,保持一致性。
文件 模块 状态 重要度
python/sglang/srt/models/ministral3.py 模型层 modified 7.34

关键符号

__init__

关键源码片段

python/sglang/srt/models/ministral3.py data-contract

唯一变更文件,修复了 Ministral3 模型初始化时参数错位的根本原因,涉及注意力层、解码器层和模型的构造逻辑。

class Ministral3Attention(LlamaAttention):
    def __init__(
        self,
        config: PretrainedConfig,
        hidden_size: int,
        num_heads: int,
        num_kv_heads: int,
        layer_id: int = 0,
        # 新增 start_layer 参数,对齐 LlamaAttention 最新签名
        start_layer: int = 0,
        rope_theta: float = 1000000.0,
        rope_scaling: Optional[Dict[str, Any]] = {},
        rope_is_neox_style: bool = True,
        max_position_embeddings: int = 8192,
        quant_config: Optional[QuantizationConfig] = None,
        prefix: str = "",
        bias: bool = False,
    ) -> None:
        # 使用关键字参数调用父类,避免位置偏移
        super().__init__(
            config=config,
            hidden_size=hidden_size,
            num_heads=num_heads,
            num_kv_heads=num_kv_heads,
            layer_id=layer_id,
            start_layer=start_layer, # 显式传递
            rope_theta=rope_theta,
            rope_scaling=rope_scaling,
            rope_is_neox_style=rope_is_neox_style,
            max_position_embeddings=max_position_embeddings,
            quant_config=quant_config,
            prefix=prefix,
            bias=bias,
        )
        # ... 后续逻辑不变
class Ministral3DecoderLayer(LlamaDecoderLayer):
    def __init__(
        self,
        config,
        layer_id=0,
        # 新增 start_layer 参数
        start_layer=0,
        quant_config=None,
        prefix="",
    ):
        # 使用关键字参数调用父类
        super().__init__(
            config=config,
            layer_id=layer_id,
            start_layer=start_layer,
            quant_config=quant_config,
            prefix=prefix,
        )
        self.self_attn = Ministral3Attention(
            config=config,
            hidden_size=self.hidden_size,
            num_heads=config.num_attention_heads,
            num_kv_heads=config.num_key_value_heads,
            layer_id=layer_id,
            start_layer=start_layer, # 显式传递
            rope_theta=config.rope_parameters["rope_theta"],
            rope_scaling=config.rope_parameters,
            max_position_embeddings=getattr(
                config, "original_max_position_embeddings", 16384
            ),
            quant_config=quant_config,
            prefix=add_prefix("self_attn", prefix),
            bias=getattr(config, "attention_bias", False)
                or getattr(config, "bias", False),
        )

评论区精华

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

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

风险与影响

风险较低。改动仅限单个文件,且通过了 CPU 回归测试和 GPU 模型加载验证。但需注意:如果将来 Llama 基类再次修改 __init__ 签名(例如增减参数),仍需同步更新 Ministral3;当前没有监控机制提醒此类变化。

影响范围为使用 Ministral-3-14B-Instruct-2512 模型的用户,特别是启用 FP8 量化或管线并行时。修复后模型初始化不再因参数错位而失败,稳定性提升。

子类继承依赖父类签名 缺少测试文件

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论