执行摘要
- 一句话:迁移 MistralLarge3 权重加载至 AutoWeightsLoader
- 推荐动作:建议维护者审查权重映射的验证方法,确保覆盖所有可能的权重名称。此 PR 是标准化模型加载的重要一步,值得关注 AutoWeightsLoader 的使用模式。
功能与动机
这是 Issue #15697(统一所有模型使用 AutoWeightsLoader)的一部分。通过迁移,标准化的权重加载模式减少了重复代码,并移除了对第三方 regex 模块的运行时依赖,改为使用 WeightsMapper 接口。
实现拆解
- 在 mistral_large_3.py 中,用 WeightsMapper 替换原有的 remapping 字典和 _remap_mistral_to_ds 方法,所有正则表达式改为双端锚定(\A...\Z)以确保顺序应用的安全。
- 新增 AutoWeightsLoader 导入,将 load_weights 简化为直接调用 loader.load_weights(weights, mapper=self.hf_to_vllm_mapper),不再调用父类。
- 在 mistral_large_3_eagle.py 中,使用 WeightsMapper.or() 合并 eagle 特定的 3 个映射,替换原来的字典合并。
- 保留 regex 导入但不再作为运行时依赖,兼容 duck-typing。
- 通过正则回溯测试验证所有 25 个模式输出一致,并检查导入无属性回归。
关键文件:
vllm/model_executor/models/mistral_large_3.py(模块 权重加载;类别 source;类型 data-contract;符号 _remap_mistral_to_ds): 核心文件,完成了从手动权重名映射到 WeightsMapper + AutoWeightsLoader 的迁移,移除对第三方 regex 模块的依赖,简化了 load_weights 逻辑。
vllm/model_executor/models/mistral_large_3_eagle.py(模块 草稿模型;类别 source;类型 data-contract): 同步迁移 Eagle 子类的映射,使用 or() 运算符合并映射,展示了 WeightsMapper 的组合用法。
关键符号:MistralLarge3ForCausalLM.load_weights, MistralLarge3ForCausalLM.hf_to_vllm_mapper, EagleMistralLarge3ForCausalLM.hf_to_vllm_mapper
关键源码片段
vllm/model_executor/models/mistral_large_3.py
核心文件,完成了从手动权重名映射到 WeightsMapper + AutoWeightsLoader 的迁移,移除对第三方 regex 模块的依赖,简化了 load_weights 逻辑。
# WeightsMapper 按顺序应用所有匹配模式(不像原来字典只匹配第一个就 break)。
# 由于所有模式都使用双端锚定( \A...\Z ),且替换后的键都以 "model." 或 "lm_head." 开头,
# 不会发生后续模式再次匹配已替换键的情况,从而保证了安全。
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_regex={
regex.compile(r"\Alayers\.(\d+)\.attention_norm\.weight\Z"):
r"model.layers.\1.input_layernorm.weight",
regex.compile(r"\Alayers\.(\d+)\.attention\.wq_a\.(\w+)\Z"):
r"model.layers.\1.self_attn.q_a_proj.\2",
# 共计 22 条 Mistral → DeepseekV2 映射,此处省略中间条目
regex.compile(r"\Aoutput\.weight\Z"): "lm_head.weight",
},
orig_to_new_suffix={
".qscale_act": ".input_scale",
".qscale_weight": ".weight_scale",
},
)
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
# 直接构造 AutoWeightsLoader ,将权重名通过 mapper 转换后分发至各子模块
loader = AutoWeightsLoader(self)
return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper)
vllm/model_executor/models/mistral_large_3_eagle.py
同步迁移 Eagle 子类的映射,使用 or() 运算符合并映射,展示了 WeightsMapper 的组合用法。
# 通过 __or__() 运算符将 Eagle 特定的 3 个映射合并到父类映射,
# 这样既复用了父类的映射关系,又明确扩展了草稿模型特有的 fc 层映射,
# 保持了子类与父类的映射同步更新。
hf_to_vllm_mapper = MistralLarge3ForCausalLM.hf_to_vllm_mapper | WeightsMapper(
orig_to_new_regex={
regex.compile(r"\Aeagle_linear\.weight\Z"): r"model.fc.weight",
regex.compile(r"\Aeagle_linear\.qscale_act\Z"): r"model.fc.input_scale",
regex.compile(r"\Aeagle_linear\.qscale_weight\Z"): r"model.fc.weight_scale",
},
)
评论区精华
无公开讨论,合并者 DarkLight1337 在审批中表示 'this LGTM'。PR Body 说明 AI 辅助了代码审查和映射验证。
风险与影响
- 风险:主要风险是权重映射的正确性。作者通过正则回溯测试验证了所有模式输出一致,但可能遗漏边界情况(如未覆盖的特殊量化权重)。跳过父类 load_weights 可能丢失某些父类逻辑,但 PR 说明父类是 AutoWeightsLoader 的包装,风险较小。
- 影响:影响范围仅限 MistralLarge3 和 EagleMistralLarge3 模型。用户无感知,权重映射结果应完全一致。移除对第三方 regex 模块的运行时依赖,减少了依赖体积。对系统性能无影响。
- 风险标记:核心路径变更, 缺少测试覆盖
关联脉络
- PR #15697 [Feature]: Composite model loading using AutoWeightsLoader for all models: 此 PR 是该 feature 的一部分,将 MistralLarge3 迁移到 AutoWeightsLoader
参与讨论