Skip to content

Commit d001fb6

Browse files
committed
moe_hybridep_pad_variable_tokens
Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
1 parent 2ee3bfb commit d001fb6

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎megatron/core/transformer/moe/token_dispatcher.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,11 @@ def setup_metadata(self, routing_map: torch.Tensor, probs: torch.Tensor):
10611061
self._original_num_tokens = num_tokens
10621062

10631063
padded_num_tokens = num_tokens
1064-
if self.config.sequence_packing_scheduler is not None:
1064+
equalize_thd_token_counts = (
1065+
self.config.sequence_packing_scheduler is not None
1066+
or self.config.moe_hybridep_pad_variable_tokens
1067+
)
1068+
if equalize_thd_token_counts:
10651069
# Use the actual tp_ep max so all ranks in the MoE communication
10661070
# group pass the same token count to HybridEP.
10671071
max_num_tokens_across_ep = torch.tensor(
@@ -1076,7 +1080,7 @@ def setup_metadata(self, routing_map: torch.Tensor, probs: torch.Tensor):
10761080

10771081
routing_map = routing_map.reshape(num_tokens, self.num_experts)
10781082
probs = probs.reshape(num_tokens, self.num_experts)
1079-
if self.config.sequence_packing_scheduler is not None and padded_num_tokens > num_tokens:
1083+
if equalize_thd_token_counts and padded_num_tokens > num_tokens:
10801084
pad_rows = padded_num_tokens - num_tokens
10811085
routing_map = torch.cat(
10821086
[routing_map, routing_map.new_zeros((pad_rows, self.num_experts))], dim=0

‎megatron/core/transformer/transformer_config.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,13 @@ class TransformerConfig(ModelParallelConfig):
866866
moe_permute_fusion_into_hybridep: bool = False
867867
"""Fuse token rearrangement ops during token dispatching for HybridEP."""
868868

869+
moe_hybridep_pad_variable_tokens: bool = False
870+
"""Pad uneven local token counts to the HybridEP group maximum before dispatch.
871+
872+
This is needed when the frontend supplies locally packed THD inputs whose token counts
873+
can differ across ranks, without using Megatron Core's sequence_packing_scheduler.
874+
"""
875+
869876
moe_per_layer_logging: bool = False
870877
"""Enable per-layer logging for MoE, currently supports auxiliary loss and z loss."""
871878

0 commit comments

Comments
 (0)