如何将 Megatron SFT 训练后保存的 checkpoint 转换回 Qwen3.5-35B-A3B 原始 HuggingFace 格式?
Checklist / 检查清单
- [x] I have searched existing issues, and this is a new question or discussion topic. / 我已经搜索过现有的 issues,确认这是一个新的问题与讨论。
Question Description / 问题描述
我最近在使用 swift 框架对 Qwen/Qwen3.5-35B-A3B(MoE 架构)进行 SFT 训练,训练完成后保存的 checkpoint (HuggingFace) 格式与原始 HuggingFace (Qwen/Qwen3.5-35B-A3B)模型在权重存储上格式存在差异。
背景说明
原始模型(Qwen/Qwen3.5-35B-A3B)的权重格式为分散格式,例如:
model.language_model.layers.0.mlp.experts.0.gate_proj.weight
model.language_model.layers.0.mlp.experts.0.up_proj.weight
model.language_model.layers.0.mlp.experts.1.gate_proj.weight
而 swift Megatron SFT 训练后保存的 checkpoint-hf 格式为合并格式,例如:
model.language_model.layers.0.mlp.experts.gate_up_proj # shape: [num_experts, 2*ffn_dim, hidden]
model.language_model.layers.0.mlp.experts.down_proj # shape: [num_experts, hidden, ffn_dim]
遇到的问题
我尝试将合并格式的 checkpoint 直接用于 verl 训练侧,但 verl mbridge 侧要求的是原始 HuggingFace (Qwen/Qwen3.5-35B-A3B)模型格式(即分散格式),导致加载失败。
我的问题 请问有没有成熟的方法或脚本,可以将 swift Megatron SFT 后输出的 gate_up_proj 合并格式 转换回 原始 HuggingFace 分散格式?具体来说,需要完成以下逆向拆分:
合并格式(当前)
experts.gate_up_proj [num_experts, 2*ffn_dim, hidden]
experts.down_proj [num_experts, hidden, ffn_dim]
↓ 逆向转换
### 原始格式(目标)
experts.0.gate_proj.weight [ffn_dim, hidden]
experts.0.up_proj.weight [ffn_dim, hidden]
experts.0.down_proj.weight [hidden, ffn_dim]
experts.1.gate_proj.weight ...
环境信息 模型:Qwen/Qwen3.5-35B-A3B 框架:ms-swift-4.1.0,megatron-core-0.16.0 保存格式:safetensors(多分片) 如果有相关的转换脚本、文档或经验,非常感谢您的分享!🙏 也欢迎指出是否有更推荐的工作流来避免这个格式不一致的问题。
感谢大家!
mark @yangshurong
你试试
import os
os.environ['CUDA_VISIBLE_DEVICS'] = '0'
from transformers import Qwen3_5ForConditionalGeneration, AutoTokenizer
model = Qwen3_5ForConditionalGeneration.from_pretrained('Qwen/Qwen3.5-35B-A3B')
model.save_pretrained('/root/Qwen3.5-35B-A3B', max_shard_size='10GB')
model = Qwen3_5ForConditionalGeneration.from_pretrained('/root/Qwen3.5-35B-A3B')
@yangshurong 请问这个问题你已经解决了吗?
https://github.com/modelscope/mcore-bridge/blob/4546657b3d53133317eb76678f4ca7c424c18434/src/mcore_bridge/bridge/gpt_bridge.py#L727-L729
use mcore_bridge 1.2.0.dev main branch
lora, fp8, mtp还是采用分开的格式,与原始权重对齐
@Jintao-Huang 请问是使用上述版本mcore_bridge重新训练模型吗
use main分支 mcore-bridge
https://github.com/modelscope/mcore-bridge/blob/main/src/mcore_bridge/bridge/gpt_bridge.py#L727-L729
@yangshurong 请问楼主的这个问题解决了吗
This issue has been inactive for over 3 months and will be automatically closed in 7 days. If this issue is still relevant, please reply to this message.
This issue has been automatically closed due to inactivity. If needed, it can be reopened.