Pull Request已成功合入, 合并人@ascend-robot
(感谢 hanyyy 的贡献)变更摘要
本 PR 为 MindSpeed-MM FSDP2 训练框架新增 MiniMax M3 VL 多模态模型的完整训练支持,主要包括:引入 NPU Flash Attention 加速推理、实现 MoE 权重融合与 DCP 格式转换、删除本地配置类改为直接依赖 transformers 官方实现、添加多模态数据链路插件与训练模板,以及提供 FSDP2 分布式训练的示例配置和启动脚本。
主要改动
-
NPU Flash Attention 接入:在
modeling_minimax_m3_vl.py中新增full_attention_fa_forward函数,强制使用flash_attention_2且输入布局为BNSD,并分别在文本 attention(MiniMaxM3VLAttention)和视觉 attention(MiniMaxM3VLVisionAttention)的 forward 路径中,当attn_implementation == "flash_attention_2"时路由到该 NPU FA wrapper。 -
NPU Grouped MoE 与融合算子:
MiniMaxM3VLExperts新增_forward_npu_grouped_moe和ep_forward方法,调用grouped_matmul、permute、unpermute等 NPU 融合算子完成 expert 计算,并通过_get_grouped_weights缓存转置权重(按_version失效),避免每次 forward 重复transpose+contiguous;同时MiniMaxM3VLRMSNorm和apply_rotary_pos_emb在IS_NPU_AVAILABLE时分别替换为torch_npu.npu_rms_norm和torch_npu.npu_rotary_mul。 -
MoE/DCP 权重转换器:新增
checkpoint/vlm_model/converters/minimax_m3.py,实现MiniMaxM3Converter类。它将 HF 格式的gate_proj/up_proj合并为gate_up_proj,将 MoE expert 的w1、w3concat 为gate_up_proj并按 expert 堆叠、w2按 expert 堆叠为down_proj,同时完成 key 映射(如block_sparse_moe.gate→mlp.gate),最终追加到 DCP 分片中。 -
配置类收敛与模型结构适配:删除本地
configuration_minimax_m3_vl.py(226 行),__init__.py改为从transformers直接导入MiniMaxM3VLConfig等;MiniMaxM3VLDenseMLP将gate_proj+up_proj双线性层合并为单一的gate_up_proj;MiniMaxM3SparseForConditionalGeneration新增_truncate_minimax_m3_layer_config方法,在配置实例化前按num_hidden_layers截断layer_types、mlp_layer_types等逐层数组,并在forward中支持 chunk loss 模式。 -
数据链路与训练入口:在
mm_plugin.py中新增MiniMaxM3VLPlugin处理图像/视频 token 展开和 processor 调用,在template.py中注册minimax_m3_vl对话模板;新增examples/minimax_m3_vl/minimax_m3_config.yaml与finetune_minimax_m3.sh,提供 DCP 加载、EP alltoall、recompute、chunk loss 等完整 FSDP2 训练配置。


代码审查
现已完成全面审查。以下是最终总结。
关闭摘要
已审查文件数:8
| 文件 | 状态 |
|---|---|
checkpoint/__init__.py |
无问题(仅新增导入行,与其他 converter 一致) |
checkpoint/vlm_model/converters/minimax_m3.py |
无问题(新增文件,转换逻辑正确,key mapping 与原 _checkpoint_conversion_mapping 保持一致) |
examples/minimax_m3_vl/finetune_minimax_m3.sh |
发现 2 个问题(P2、P3) |
examples/minimax_m3_vl/minimax_m3_config.yaml |
无问题(YAML key 名称与代码库约定一致,如 preprocess_parameters) |
mindspeed_mm/fsdp/data/data_utils/func_utils/mm_plugin.py |
无问题(MiniMaxM3VLPlugin 实现正确,_validate_input 守卫充分) |
mindspeed_mm/fsdp/data/data_utils/func_utils/template.py |
无问题(模板注册格式与其他模板一致) |
mindspeed_mm/fsdp/models/minimax_m3_vl/__init__.py |
无问题(迁移到 transformers 库 Config,清理了旧的 data_plugin 导出) |
mindspeed_mm/fsdp/models/minimax_m3_vl/modeling_minimax_m3_vl.py |
发现 3 个问题(2 个 P0、1 个 P3) |
按优先级统计:
- P0:2 个
- P1:0 个
- P2:1 个
- P3:2 个
总体风险评估:🔴 高风险。两个 P0 问题会导致非 NPU 环境下的模块导入崩溃(import torch_npu)以及死代码中存在未定义变量(_DISABLE_NPU_AFFINITY_OPS)。这些应在合入前修复。Shell 脚本的健壮性问题(缺少 set -euo pipefail)属于中等风险,建议一并处理。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 2 |
| 🟡 建议 | 1 |
⛔ 需要修改


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


What this PR does / why we need it?
本 PR 添加并完善了 MiniMax M3 VL 在 MindSpeed-MM FSDP2 下的训练集成,包括:
Does this PR introduce any user-facing change?
是的。用户现在可以使用 MindSpeed-MM 配合 FSDP2 训练 MiniMax M3 VL 模型,并支持从 HF 权重转换到 DCP 后进行大模型训练:
bash examples/minimax_m3_vl/finetune_minimax_m3.sh
How was this patch tested?
Please explain how to verify the correctness and effectiveness of this feature, as well as its usage constraints and limitations.