Skip redundant moe_sum_reduce for single-expert routing on XPU#22660
Open
rahulvijayaraghavan wants to merge 1 commit intosgl-project:mainfrom
Open
Skip redundant moe_sum_reduce for single-expert routing on XPU#22660rahulvijayaraghavan wants to merge 1 commit intosgl-project:mainfrom
rahulvijayaraghavan wants to merge 1 commit intosgl-project:mainfrom
Conversation
When topk_ids.shape[1] == 1 and routed_scaling_factor == 1.0, the second invoke_fused_moe_kernel call already writes its output directly into out_hidden_states, so the subsequent moe_sum_reduce is a no-op reduction over a single element. This adds an early-exit check on the XPU path to skip the unnecessary kernel launch, matching the existing optimization already present in the CUDA path. This is particularly relevant for Llama-4-Scout models (e.g. Llama-4-Scout-17B-16E-Instruct), which set num_experts_per_tok = 1, meaning this fast path is hit on every MoE layer forward pass.
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
polisettyvarma
approved these changes
Apr 13, 2026
mingfeima
requested changes
Apr 13, 2026
Comment on lines
+681
to
+688
| if topk_ids.shape[1] == 1 and routed_scaling_factor == 1.0: | ||
| pass # we write directly into out_hidden_states | ||
| else: | ||
| moe_sum_reduce( | ||
| intermediate_cache3.view(*intermediate_cache3.shape), | ||
| out_hidden_states[begin_chunk_idx:end_chunk_idx], | ||
| routed_scaling_factor, | ||
| ) |
Collaborator
There was a problem hiding this comment.
make sure we have test cases to cover:
- topk == 1, routed_scaling_factor == 1.0
- topk == 1, routed_scaling_factor != 1.0
addtionally, is it possible to move topk_ids.shape[1] == 1 and routed_scaling_factor == 1.0 up and skip intermediate_cache3 allocation at the first place.
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.
When topk_ids.shape[1] == 1 and routed_scaling_factor == 1.0, the second invoke_fused_moe_kernel call already writes its output directly into out_hidden_states, so the subsequent moe_sum_reduce is a no-op reduction over a single element. This adds an early-exit check on the XPU path to skip the unnecessary kernel launch, matching the existing optimization already present in the CUDA path.
This is particularly relevant for Llama-4-Scout models (e.g. Llama-4-Scout-17B-16E-Instruct), which set num_experts_per_tok = 1, meaning this fast path is hit on every MoE layer forward pass.