grpo训练,megatron MoE + vLLM rollout server 模式权重同步 shape mismatch(疑似缺少 MoE 权重重分片)

Open
#10,516 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start by tracing the server mode weight synchronization path from Megatron export_weights into vLLM, then compare it with the colocate path that performs Megatron to vLLM resharding. Reproduce with the reported TP=2, EP=8, ETP=1 setup and inspect the routed expert weight shapes; done means server mode synchronizes the MoE weights successfully across the reported layouts.

Written by the indexing model from the issue text.

Description

bug needs more info
Checklist / 检查清单
  • I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。
Bug Description / Bug 描述

环境

  • ms-swift: 4.6.0(4.5.3 时同样可复现)
  • vLLM: 0.32.0(源码安装,含 PR #53896,支持 qwen4_exp)
  • megatron-core: 0.18.2
  • mcore-bridge: 1.6.4
  • peft: 0.20.0 / transformers: 5.16.1 / torch: 2.13.0+cu130
  • 模型: Qwen3.8-Flash-Next(qwen4_exp,125B MoE + 51B ngram 表)
  • 硬件: 训练侧 8×A800-80G,rollout 侧另 8×A800-80G(两台机器,--vllm_mode server)
How to Reproduce / 如何复现

训练侧(megatron rlhf --rlhf_type grpo):

--tuner_type lora --lora_rank 8
--target_modules in_proj out_proj linear_proj linear_qkv
--tensor_model_parallel_size 2        # 也试过 8
--expert_model_parallel_size 8
--expert_tensor_parallel_size 1       # ETP=1,MoE 中间维不切
--context_parallel_size 4             # 也试过 1
--pipeline_model_parallel_size 1
--use_vllm true --vllm_mode server
--vllm_server_host <rollout_ip> --vllm_server_port 8000

rollout 侧(swift rollout):

--vllm_tensor_parallel_size 8        # 也试过 2
--vllm_enable_expert_parallel true
--vllm_enable_lora false             # 也试过 true

现象:无论怎么调 TP,权重同步都在 shape mismatch 之间来回跳
场景一:rollout TP=8,训练 TP=2(默认)
Worker failed: The size of tensor a (80) must match the size of tensor b (640) at non-singleton dimension 0 (routed_experts, intermediate 维)
640 = moe_intermediate_size,80 = 640/8(vLLM TP=8 切中间维),而训练侧 ETP=1 不切中间维、导出完整 640。

场景二:rollout TP=2(试图对齐训练 TP=2)
不报 shape 错了,但 rollout 8 卡被展开为 TP2 × DP4(EngineCore_DP1/DP2),dense 层整份复制,每卡显存冲到 77.55 GiB / 79.27 GiB,update_flattened_params OOM(torch.empty 3.26 GiB 失败)。

场景三:训练 TP 改成 8(与 rollout TP=8 一致)
intermediate 维(80/640)对上了,但立刻冒出新的 mismatch:

Worker failed: The size of tensor a (40) must match the size of tensor b (2560) at non-singleton dimension 0 (routed_experts.py:623 _load_w2, down_proj hidden 维)

根因判断

megatron 和 vLLM 对 MoE expert 权重的切分布局定义不一致:

megatron 训练:EP 切 experts + ETP=1 不切 intermediate + TP 切 dense hidden;
vLLM 推理:TP 切 intermediate(和 hidden)+ EP(EPLB)横跨 TP×DP 切 experts。
同一个 expert 权重,两边在多个维度(intermediate / hidden / experts)上的切法规则都不一样。所以 server 模式下 export_weights 把 megatron 布局的权重原样传给 vLLM,vLLM 按自己的布局 copy_,必然在一个维度上对不上。

colocate 模式是好的(官方 Qwen3.8-Flash-Next Best Practice 的 GRPO 就是 colocate + TP2/EP4/PP2 + vLLM TP8),因为同进程内 bridge 会做一次 megatron→vLLM 的权重重分片;而 server 模式这条重分片路径缺失,导致 shape mismatch。

期望 / 请求

server 模式下支持 megatron MoE 权重 → vLLM rollout 的权重重分片(对齐 megatron 的 TP/EP/ETP 布局 与 vLLM 的 TP/EP 布局),和 colocate 模式一致;
或给出「megatron MoE 训练 + vLLM rollout server(两台机器)」的官方正确配置姿势——到底 tensor_model_parallel_size / expert_model_parallel_size / expert_tensor_parallel_size / rollout 的 vllm_tensor_parallel_size / vllm_enable_expert_parallel 该怎么配,才能让权重 shape 对齐。
这个问题不是配置能绕过的(场景一/二/三已排除),疑似是 server 同步缺 MoE 重分片,望确认并支持。

Additional Information / 补充信息

No response

Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.