Repository navigation
[rl] Enable TP2EP for unified MoE model in vLLM wrapper - #3142
Conversation
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
…pper" Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
…M wrapper" Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
…M wrapper" Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
…M wrapper" Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
…M wrapper" Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs. Key changes: - Meta-device init + to_empty() + init_states() for large MoE models - EP mesh: ep = tp_size (TP ranks become EP ranks for experts) - Use enable_sp (not inference flag) for output layout and SP splitting - enable_sequence_parallel=True, disable_loss_parallel=True for inference - Remove stale ModelConvertersContainer references - Add MoE debug and 30B-A3B RL configs Verified: TP=2, EP+TP=2, EP+TP=4 all produce correct output matching native vLLM on Qwen3-30B-A3B. Similar as #3057 [ghstack-poisoned]
… and fix combine() shape mismatch (#3595) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #3236 * #3142 * __->__ #3595 ## What's the problem - [Currently] combine() wrongly assume the tokens are evenly sharded on each rank https://github.com/pytorch/torchtitan/blob/c0428bb186f5c97e1d1b4ed89febd22916eee302/torchtitan/models/common/token_dispatcher.py#L439-L456 (infer global SPMD in local SPMD region) - If uneven sharded, out_TD will have different shapes across SP ranks. - We should directly ban if input number of tokens in input batch can not be evenly sharded by SP ranks - [Future] Router will use spmd_types soon, and router is per SP rank. Per SP rank should have even sharding - [Future] we want to avoid dispatch/load_balacing the padded token, we should be able to do that by adding metadata field to record the actually local tokens for each sp rank ## What does this PR do? This PR is doing "virtual padding" , and passing metadata around - Calculate num_local_tokens_after_padding = (T + pad_tokens) // sp_size in MoE module - Pass num_local_tokens_after_padding to GroupedExperts module, then to combine() - combine() returns a tensor with shape (num_local_tokens_after_padding * sp_rank, .... ) - slice the combined tensor to (T, ...) in MoE
[ghstack-poisoned]
| "<|vision_end|>": 2006, | ||
| "<|image_pad|>": 2007, | ||
| "<|video_pad|>": 2008 | ||
| "<|video_pad|>": 2008, |
There was a problem hiding this comment.
These special tokens are needed becuase external render (AutoTokenizer in RL) checks these and they are needed by Qwen3 model
There was a problem hiding this comment.
hf_assets_path="tests/assets/tokenizer"
renderer=RendererConfig(name="qwen3", enable_thinking=False)
| completion_message["reasoning_content"] = parsed.reasoning_content | ||
| if parsed.tool_calls: | ||
| completion_message["tool_calls"] = parsed.tool_calls | ||
|
|
There was a problem hiding this comment.
Changes in this files just adding basic kwargs and make this script runnable with different configs, and align with the generator setup in RL.
| # forwards cannot shard across all SP ranks, so pad to ``sp_size`` and | ||
| # trim before returning. This is a no-op for training/prefill and EP-off; | ||
| # padded tokens only affect the inference-only load-balance counter. | ||
| seq_pad = sp_size - L if L < sp_size else 0 |
There was a problem hiding this comment.
This is to solve #3622
TLDR the issue is: when seq_len >= TP_degree, num_local_tokens_per_expert_E = routing_map_BLE.sum(dim=(0, 1)) suppose to be
V (tokens vary across TP ranks) --sum(seq)--> P (partial per-rank count). But when seq_len < TP, it degraded to R --> sum --> R, which can not be converted to P later.
The simple fix there is to pad when seq_len < TP_degree
There was a problem hiding this comment.
And this path should be only used when inference (however the pretraining might also hit seq_len < SP rank, we can just abandon it) - During inference the token_per_exepert_E would NOT matter much for inference. We need to enforce it's inference only
There was a problem hiding this comment.
Let's add a TODO.
Please wrap the affected region with ------------- so people know this is temporary code which needs to be removed.
[ghstack-poisoned]
|
|
||
| _upstream_enable() | ||
|
|
||
| from vllm.model_executor.layers.batch_invariant import bmm_batch_invariant |
There was a problem hiding this comment.
This needs more discussion: We shouldn't put vllm as dependency in core. I will move these part into generator, as it's generator sepecific
Meanwhile I'm asking Claude to check the trace / read the code, where exactly is bmm used in MoE
There was a problem hiding this comment.
- Does TML package not support batch-invariant bmm?
- which exact tensor is not contiguous? I'd like to check if we could just
.contiguous()on it before doing linear. I think it's the ``x_BLD` here https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/moe.py#L402. Maybe let's call contiguous on it to avoid further complicating the code, and leave a TODO to investigate if we could refactor later.
There was a problem hiding this comment.
- No, TML's version doesn't include batch-invariant bmm. vllm has batch-invariant bmm implemented.
- I'm testing locally if a continous tensor will be dispatched to mm instead of bmm
Update: with only x_BLD.contiguous() and no bmm override, the drift stayed large (0.016, 2e-05, 0.031, ...),
The Aten dispatch rule for matmul is:
if (dim1==1 && dim2==1) → dot
else if (dim1==2 && dim2==1) → mv
else if (dim1==1 && dim2==2) → mm (unsqueeze)
else if (dim1==2 && dim2==2) → mm // line 2044
else if (should_fold(tensor1, tensor2, has_out)) → mm // line 2046 (fold batch
into rows)
else → bmm // line 2112
(expand+broadcast batch)
Our gate is 3‑D activation @ 2‑D weight (dim1=3, dim2=2), so it lands on the should_fold decision. bmm is taken iff should_fold returns false. What should_fold do is decided at graph_tracing time, and it always returns false no matter we call contiguous or not.
In eager, a contiguous 3-D @ 2-D never produced bmm in eager, DTensor (Shard(1)/Replicate), or backed/unbacked-symbolic tests — they all fold to mm. So the bmm is specifically the traced-inference-graph activation being non-contiguous , which eager .contiguous() doesn't reach.
Another potential solution is fold the 3D activation into 2D (and change sharing config accrodingly)
B, L, D = x_BLD.shape
scores_BLE = self.gate(x_BLD.reshape(B * L, D)).view(B, L, -1)
I'm checking if we can implement this cleanly
There was a problem hiding this comment.
this will introduce problem when spmd_types migration happens, so preferring patching vllm bmm for now.
tianyu-l
left a comment
There was a problem hiding this comment.
one comment on the batch-invariant bmm, and some nits
|
|
||
| _upstream_enable() | ||
|
|
||
| from vllm.model_executor.layers.batch_invariant import bmm_batch_invariant |
There was a problem hiding this comment.
- Does TML package not support batch-invariant bmm?
- which exact tensor is not contiguous? I'd like to check if we could just
.contiguous()on it before doing linear. I think it's the ``x_BLD` here https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/moe.py#L402. Maybe let's call contiguous on it to avoid further complicating the code, and leave a TODO to investigate if we could refactor later.
| **self._encode_rollout(rollout), | ||
| } | ||
| }, | ||
| # Validation rollouts can contain renderer-owned message objects. |
There was a problem hiding this comment.
I didn't understand the comment and why we need this. cc @felipemello1
There was a problem hiding this comment.
I hit something (tool calling results) is not serializable error when json dump
There was a problem hiding this comment.
comment that this is to bypass tool calling results.
| # forwards cannot shard across all SP ranks, so pad to ``sp_size`` and | ||
| # trim before returning. This is a no-op for training/prefill and EP-off; | ||
| # padded tokens only affect the inference-only load-balance counter. | ||
| seq_pad = sp_size - L if L < sp_size else 0 |
There was a problem hiding this comment.
Let's add a TODO.
Please wrap the affected region with ------------- so people know this is temporary code which needs to be removed.
| expert routing, breaking on-policy logprob parity. Only the generator needs | ||
| it (the trainer's gate is ``mm``), so it lives here rather than in core. |
There was a problem hiding this comment.
What I still don't understand is why trainer gate is folded to mm. In the [eager, compile, cudagraph] x [training, inference] matrix, possibly furthur expanded by TP/EP/SP configs, when do we hit bmm?
#3142 (comment) explained some, but it's not entirely clear.
My point is that
- if this is e.g. aot_eager compile vs. eager mismatch, then we should ask compile experts to help resolve.
- basically I would like to learn "Unless xxx is willing to change, we couldn't really solve this problem". The way you describe the issue sounds to me that it's some mismatch, and we can fix it.
I don't mind leaving a TODO for investigation.
[ghstack-poisoned]
|
Model Test failure is not related RL test passed: However, for MoE model the loss is 0 (which is expected because we random initialized weights) |

Stack from ghstack (oldest at bottom):
Enable Expert Parallelism for MoE models in the vLLM inference path, with TP on dense layers. Includes meta-device init, init_states() fix for RoPE cache, EP mesh creation, enable_sp-based output layout, and MoE debug configs.
Key changes:
Similar as #3057
Dependency
Verification
Qwen3-30B-A3B model on 8 GPU (Trainer TP4 EP4, generator TP4 EP4):
NOTE: this locally run didn't patch #3604, so the trainer are actually producing wrong numerics
Batch-invariant verification
bmminvariant kernel: dense doesn’t meaningfully exercise a 3D batched matmul path in this parity setup, while MoE/EP introduces batched/grouped per-expert shapes and routing-dependent layouts; mm/addmm fixes 2D linear layers, but not 3D aten::bmm/batched matmul reductions.NOTE: the loss is zero is because it's debug MoE model, not meaningful weights loaded so reward is constantly 0