# PR #25759 完整报告

- 仓库：`sgl-project/sglang`
- 标题：[BugFix][EPD]Fix Qwen3VLMoe encoder-only AttributeError
- 合并时间：2026-05-21 11:07
- 原文链接：http://prhub.com.cn/sgl-project/sglang/pull/25759

---

# 执行摘要

- 一句话：修复 EPD 模式下 Qwen3VL MoE 权重加载崩溃
- 推荐动作：值得快速合并，修复明确且安全。建议关注 MoE 子类是否还有其他未对齐父类防护的模式，可系统审计 `qwen3_vl_moe.py` 中其他可能直接访问 `self.model` 的位置。

# 功能与动机

启动 encoder-only EPD 服务器加载 `Qwen3VLMoeForConditionalGeneration` 权重时崩溃：`AttributeError: 'Qwen3VLMoeForConditionalGeneration' object has no attribute 'model'`。原因是 `encoder_only` 模式下父类跳过创建 `self.model`，而子类 `load_weights` 直接访问 `self.model.start_layer` 未做存在性检查。

# 实现拆解

1. 在 `python/sglang/srt/models/qwen3_vl_moe.py` 的 `load_weights` 方法中，第 238 行原有条件 `hasattr(self.model, "start_layer")` 前增加 `hasattr(self, "model")` 短路保护。
2. 当 `self.model` 不存在时（encoder_only 模式），直接跳过依赖 `self.model` 的 layer 范围过滤逻辑，避免 AttributeError。
3. 对齐父类 `Qwen3VLForConditionalGeneration.load_weights`（qwen3_vl.py:1345）已有的相同防护模式。

关键文件：
- `python/sglang/srt/models/qwen3_vl_moe.py`（模块 模型层；类别 source；类型 bugfix；符号 load_weights）: 修复核心文件，单行变更添加 hasattr 短路保护，避免 encoder-only EPD 模式下权重加载 AttributeError。

关键符号：load_weights

## 关键源码片段

### `python/sglang/srt/models/qwen3_vl_moe.py`

修复核心文件，单行变更添加 hasattr 短路保护，避免 encoder-only EPD 模式下权重加载 AttributeError。

```python
# python/sglang/srt/models/qwen3_vl_moe.py
# load_weights 方法中的关键条件判断片段（第 235-244 行）
if (
    "visual" not in name
    and layer_id is not None
    # 新增：先确保 self.model 存在，再访问其属性
    # encoder_only 模式下父类不会创建 self.model，没有此检查会触发 AttributeError
    and hasattr(self, "model")
    and hasattr(self.model, "start_layer")  # 原检查，仅在 self.model 存在时有效
    and (
        layer_id < self.model.start_layer
        or layer_id >= self.model.end_layer
    )
):
    continue

```

# 评论区精华

Review 中无实质争议，liusy58 和 ShangmingCai 均直接 approve，gemini-code-assist[bot] 仅简评确认变更安全性。

- 暂无高价值评论线程

# 风险与影响

- 风险：变更仅添加一行 `hasattr(self, "model")` 短路检查，风险极低。潜在问题是如果 `self.model` 属性存在但类型异常（如为 None），`hasattr` 仍返回 True，后续 `self.model.start_layer` 仍会 AttributeError。但此设计对齐父类已有模式，且该场景在正常 EPD 流程中不应出现。
- 影响：影响范围高度局限：仅修复 Qwen3VL MoE 模型在 EPD encoder-only 模式下的权重加载崩溃，不涉及推理路径或性能。无用户可见行为变化，系统可用性提升（避免启动失败）。
- 风险标记：边缘路径修复 , 无测试覆盖

# 关联脉络

- PR #19135 ( 推测 ) Qwen3VL MoE 模型引入 : PR body 指出 MoE 子类从 #19135 复制了旧版 load_weights 模式，丢失了 hasattr(self, "model") 防护。