已合并
feat: Add MiniMax M3 VL FSDP2 Training Support with NPU fused kernel and DCP Conversion #2867
feat: Add MiniMax M3 VL FSDP2 Training Support with NPU fused kernel and DCP Conversion #2867
已合并
hanyyy创建于 7月21日
hanyyy
hanyyy成员
7月21日

What this PR does / why we need it?

本 PR 添加并完善了 MiniMax M3 VL 在 MindSpeed-MM FSDP2 下的训练集成,包括:

  • NPU Flash Attention 接入 (mindspeed_mm/fsdp/models/minimax_m3_vl/modeling_minimax_m3_vl.py):为文本和视觉 attention 增加 NPU FA wrapper,使用 ALL_ATTENTION_FUNCTIONS 中的 flash_attention_2,并采用 BNSD 输入布局,减少额外 transpose。
  • MoE / DCP 权重转换 (checkpoint/vlm_model/converters/minimax_m3.py):完善 MiniMax M3 HF 到 DCP 的转换逻辑,将 dense/shared expert 的 gate_proj、up_proj 预先合并为 gate_up_proj,并将 MoE expert 的 w1/w3/w2 转成当前模型使用的 fused expert 参数格式,匹配 FSDP2/DCP 加载。
  • 多模态数据链路 (mindspeed_mm/fsdp/data/data_utils/func_utils/template.py, mindspeed_mm/fsdp/data/data_utils/func_utils/mm_plugin.py):新增 MiniMax M3 VL template 与多模态 plugin,复用 huggingface 数据链路处理 ShareGPT/LLaVA 风格图文样本。
  • 训练配置与启动脚本 (examples/minimax_m3_vl/minimax_m3_config.yaml, examples/minimax_m3_vl/finetune_minimax_m3.sh):提供 MiniMax M3 VL FSDP2 示例配置,包含 DCP 加载、FA、EP alltoall、recompute、chunk loss 启动参数。

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.

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 hanyyy 的贡献)
hanyyyhanyyy成员
7月21日 创建了 pull request,commit a985253d
atomgit-bot
atomgit-bot
7月21日 评论:

变更摘要

本 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_moeep_forward 方法,调用 grouped_matmulpermuteunpermute 等 NPU 融合算子完成 expert 计算,并通过 _get_grouped_weights 缓存转置权重(按 _version 失效),避免每次 forward 重复 transpose+contiguous;同时 MiniMaxM3VLRMSNormapply_rotary_pos_embIS_NPU_AVAILABLE 时分别替换为 torch_npu.npu_rms_normtorch_npu.npu_rotary_mul

  • MoE/DCP 权重转换器:新增 checkpoint/vlm_model/converters/minimax_m3.py,实现 MiniMaxM3Converter 类。它将 HF 格式的 gate_proj/up_proj 合并为 gate_up_proj,将 MoE expert 的 w1w3 concat 为 gate_up_proj 并按 expert 堆叠、w2 按 expert 堆叠为 down_proj,同时完成 key 映射(如 block_sparse_moe.gatemlp.gate),最终追加到 DCP 分片中。

  • 配置类收敛与模型结构适配:删除本地 configuration_minimax_m3_vl.py(226 行),__init__.py 改为从 transformers 直接导入 MiniMaxM3VLConfig 等;MiniMaxM3VLDenseMLPgate_proj + up_proj 双线性层合并为单一的 gate_up_projMiniMaxM3SparseForConditionalGeneration 新增 _truncate_minimax_m3_layer_config 方法,在配置实例化前按 num_hidden_layers 截断 layer_typesmlp_layer_types 等逐层数组,并在 forward 中支持 chunk loss 模式。

  • 数据链路与训练入口:在 mm_plugin.py 中新增 MiniMaxM3VLPlugin 处理图像/视频 token 展开和 processor 调用,在 template.py 中注册 minimax_m3_vl 对话模板;新增 examples/minimax_m3_vl/minimax_m3_config.yamlfinetune_minimax_m3.sh,提供 DCP 加载、EP alltoall、recompute、chunk loss 等完整 FSDP2 训练配置。

likedislike
atomgit-bot
atomgit-bot
7月21日 评论:

代码审查

现已完成全面审查。以下是最终总结。


关闭摘要

已审查文件数: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

⛔ 需要修改

likedislike
ascend-robotascend-robot成员
7月21日 添加了label:stat/needs-squash
ascend-robotascend-robot成员
7月21日 添加了label:ascend-cla/yes
此处折叠了204条消息 查看更多
htwang成员
6 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
6 天前 添加了label:approved
ascend-robotascend-robot成员
6 天前 合入了pull request
ascend-robot
ascend-robot成员
6 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike