Move expert_mask_gpu from FusedMoE layer to StandardDispatcher (#23585)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cheng Wan
2026-04-23 17:17:27 -07:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 95d021b523
commit 000a2525e1
6 changed files with 25 additions and 25 deletions
@@ -213,8 +213,6 @@ class FusedMoE(torch.nn.Module):
self.num_local_experts = self._num_local_routed + num_fused_shared_experts self.num_local_experts = self._num_local_routed + num_fused_shared_experts
self._has_fused_shared = num_fused_shared_experts > 0 self._has_fused_shared = num_fused_shared_experts > 0
self.expert_mask_gpu = None
assert intermediate_size % self.moe_tp_size == 0 assert intermediate_size % self.moe_tp_size == 0
self.intermediate_size_per_partition = intermediate_size // self.moe_tp_size self.intermediate_size_per_partition = intermediate_size // self.moe_tp_size
self.reduce_results = reduce_results self.reduce_results = reduce_results
@@ -1010,16 +1008,6 @@ class FusedMoE(torch.nn.Module):
hidden_states=hidden_states, topk_output=topk_output hidden_states=hidden_states, topk_output=topk_output
) )
if _use_aiter and self.dispatcher.local_expert_mapping is not None:
self.expert_mask_gpu = (
(
(self.dispatcher.local_expert_mapping >= 0)
& (self.dispatcher.local_expert_mapping < self.num_local_experts)
)
.to(torch.int32)
.to(device="cuda")
)
combine_input = self.run_moe_core( combine_input = self.run_moe_core(
dispatch_output=dispatch_output, dispatch_output=dispatch_output,
) )
@@ -99,12 +99,14 @@ class StandardDispatcher(BaseDispatcher):
get_moe_runner_backend().is_flashinfer_trtllm_routed() get_moe_runner_backend().is_flashinfer_trtllm_routed()
) )
self.num_experts = moe_runner_config.num_experts self.num_experts = moe_runner_config.num_experts
self.num_local_experts = moe_runner_config.num_local_experts
self.num_local_shared_experts = moe_runner_config.num_fused_shared_experts self.num_local_shared_experts = moe_runner_config.num_fused_shared_experts
self.num_local_routed_experts = ( self.num_local_routed_experts = (
moe_runner_config.num_local_experts - self.num_local_shared_experts self.num_local_experts - self.num_local_shared_experts
) )
self.moe_ep_rank = get_moe_expert_parallel_rank() self.moe_ep_rank = get_moe_expert_parallel_rank()
self.local_expert_mapping = None self.local_expert_mapping = None
self.expert_mask_gpu = None
def dispatch( def dispatch(
self, hidden_states: torch.Tensor, topk_output: TopKOutput self, hidden_states: torch.Tensor, topk_output: TopKOutput
@@ -187,13 +189,23 @@ class StandardDispatcher(BaseDispatcher):
) )
) )
if self.local_expert_mapping is not None and not _use_aiter: if self.local_expert_mapping is not None:
if TopKOutputChecker.format_is_standard(topk_output): if _use_aiter:
topk_output = topk_output._replace( self.expert_mask_gpu = (
topk_ids=self.local_expert_mapping[topk_output.topk_ids] (
(self.local_expert_mapping >= 0)
& (self.local_expert_mapping < self.num_local_experts)
)
.to(torch.int32)
.to(device="cuda")
) )
elif TopKOutputChecker.format_is_triton_kernels(topk_output): else:
raise NotImplementedError() if TopKOutputChecker.format_is_standard(topk_output):
topk_output = topk_output._replace(
topk_ids=self.local_expert_mapping[topk_output.topk_ids]
)
elif TopKOutputChecker.format_is_triton_kernels(topk_output):
raise NotImplementedError()
return StandardDispatchOutput( return StandardDispatchOutput(
hidden_states=hidden_states, hidden_states=hidden_states,
+2 -2
View File
@@ -1821,7 +1821,7 @@ class Fp8MoEMethod(FusedMoEMethodBase):
if activation == "silu" if activation == "silu"
else ActivationType.Gelu else ActivationType.Gelu
), ),
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
) )
else: else:
return fused_moe( return fused_moe(
@@ -1838,7 +1838,7 @@ class Fp8MoEMethod(FusedMoEMethodBase):
if activation == "silu" if activation == "silu"
else ActivationType.Gelu else ActivationType.Gelu
), ),
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
) )
return None return None
@@ -856,7 +856,7 @@ class Mxfp4MoEMethod(FusedMoEMethodBase):
w2_weight, w2_weight,
topk_weights, topk_weights,
topk_ids, topk_ids,
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
activation=ActivationType.Swiglu, activation=ActivationType.Swiglu,
quant_type=QuantType.per_1x32, quant_type=QuantType.per_1x32,
w1_scale=layer.w13_weight_scale, w1_scale=layer.w13_weight_scale,
@@ -1045,6 +1045,6 @@ class Mxfp4DynamicQuantMoEMethod(FusedMoEMethodBase):
else ActivationType.Gelu else ActivationType.Gelu
), ),
doweight_stage1=False, doweight_stage1=False,
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
) )
return StandardCombineInput(hidden_states=output) return StandardCombineInput(hidden_states=output)
@@ -227,6 +227,6 @@ class QuarkW4A4MXFp4MoE(QuarkMoEScheme):
else ActivationType.Gelu else ActivationType.Gelu
), ),
doweight_stage1=False, doweight_stage1=False,
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
) )
return StandardCombineInput(hidden_states=output) return StandardCombineInput(hidden_states=output)
@@ -484,7 +484,7 @@ class UnquantizedFusedMoEMethod(FusedMoEMethodBase, MultiPlatformOp):
if moe_runner_config.activation == "silu" if moe_runner_config.activation == "silu"
else ActivationType.Gelu else ActivationType.Gelu
), ),
expert_mask=layer.expert_mask_gpu, expert_mask=layer.dispatcher.expert_mask_gpu,
) )
return StandardCombineInput(hidden_states=output) return StandardCombineInput(hidden_states=output)
except RuntimeError as e: except RuntimeError as e: