fix: handle zero-size tensors in MoE token dispatchers#3626
Open
callum-ward-inflection wants to merge 1 commit intoNVIDIA:mainfrom
Open
fix: handle zero-size tensors in MoE token dispatchers#3626callum-ward-inflection wants to merge 1 commit intoNVIDIA:mainfrom
callum-ward-inflection wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
When an EP rank receives zero tokens from the router, the fused permute/unpermute autograd pair breaks and .view() crashes on a zero-size tensor. Switch to unfused path symmetrically, reconnect gradient graph for backward collectives, and guard .view() in all three dispatcher classes. Fixes: NVIDIA#1877
kvareddy
approved these changes
Feb 26, 2026
Contributor
|
Hi @callum-ward-inflection , I think the ideal way to resolve this issue is to fix the fused permute kernel. Adding more conditions in the token dispatcher will make it more complex. Could you post the detailed error you facing with zero-tokens? IIRC, the TE fused permute kernel should support the zero-token case. cc @hxbai |
Contributor
|
I agree with @Victarry , it is better to create a fix to TE's permute function rather than to MCore. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Fixes MoE token dispatcher crash when an Expert Parallelism rank receives zero tokens from the router.
When an EP rank receives zero tokens from the router, the fused permute/unpermute autograd pair breaks and .view() crashes on a zero-size tensor. Switch to unfused path symmetrically, reconnect gradient graph for backward collectives, and guard .view() in all three dispatcher classes.
Fixes: #1877
Changes
Three changes to
megatron/core/transformer/moe/token_dispatcher.py:Symmetric unfused permute/unpermute for empty EP ranks — TE's
fused_permutesaves state thatfused_unpermutereads during backward. With zero tokens this state is invalid. Bothdispatch_postprocessandcombine_preprocessdetect zero-token ranks and fall back to the unfused PyTorch path together (they must match — mixing fused permute with unfused unpermute crashes due to incompatible index formats).Gradient connectivity for backward collectives — Unfused
unpermutewith zero tokens returns a tensor disconnected from the autograd graph. During backward, distributed collectives (AllGather/ReduceScatter) need every rank to participate. A detached tensor means one rank never triggers its collective and all others hang (NCCL timeout). Reconnected withunpermuted_local_hidden + hidden_states.sum() * 0.Safety guard on
.view()for all three dispatcher classes —MoEAllGatherTokenDispatcher,MoEAlltoAllTokenDispatcher, andMoEFlexTokenDispatcherall have.view(self.hidden_shape)that crashes on zero-size input. Guarded with a zero tensor of the correct shape.Reproduction
Reproducible with
openai/gpt-oss-20b(32 experts, hidden_size=2880) SFT with TP=4, EP=2 on 16 GPUs. The pretrained router's weight distribution combined with reduced tokens-per-rank from TP=4 sequence parallelism causes certain EP ranks to consistently receive zero tokens.Testing
Contribution process
Pre-checks
Core 0.8)