Repository navigation
[AMD] fix(moe): correct fused shared-expert scaling on aiter/DeepEP path (mori all-to-all) #28237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HaiShaw
merged 4 commits into
sgl-project:main
from
rbrugaro-amd:fix/fused-shared-expert-scaling-aiter-deepep
Jun 25, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e9ef411
fix(moe): correct fused shared-expert scaling on aiter/deepep path
rbrugaro-amd d5c2786
Merge remote-tracking branch 'upstream/main' into fix/fused-shared-ex…
rbrugaro-amd a87077c
fix(test): patch get_parallel, not removed get_moe_expert_parallel_* …
rbrugaro-amd e18f88a
Merge branch 'main' into fix/fused-shared-expert-scaling-aiter-deepep
HaiShaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
122 changes: 122 additions & 0 deletions
122
test/registered/unit/layers/moe/test_fused_shared_expert_scaling.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """Unit tests for fused shared-expert weight scaling on the DeepEP layout. | ||
|
|
||
| These tests pin the contract of ``_remap_topk_for_deepep`` for the fused shared | ||
| expert's topk weight on the two paths this fix covers: | ||
|
|
||
| * aiter (HIP) path: routed_scaling_factor is folded into the routed weights and | ||
| forward_deepep skips the post-MoE multiply, so the shared weight must be 1.0 | ||
| for a net 1.0x contribution. | ||
| * post-MoE scaling path (default): the whole MoE output is multiplied by | ||
| routed_scaling_factor afterward, so the shared weight must be 1/rsf. | ||
| """ | ||
|
|
||
| from sglang.test.ci.ci_register import register_cpu_ci | ||
|
|
||
| register_cpu_ci(est_time=7, suite="base-a-test-cpu") | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.srt.layers.moe import topk as topk_module | ||
| from sglang.srt.layers.moe.topk import TopKConfig | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
|
|
||
| class TestFusedSharedExpertScaling(CustomTestCase): | ||
| # 256 physical routed experts over ep_size=8 -> 32 local routed per rank. | ||
| NUM_PHYSICAL_ROUTED = 256 | ||
| EP_SIZE = 8 | ||
| EP_RANK = 0 | ||
| ROUTED_SCALING_FACTOR = 2.5 | ||
|
|
||
| def _run_remap(self, *, use_aiter): | ||
| # Layout: [routed, routed, routed, shared]; the trailing column is the | ||
| # fused shared expert. The shared weight starts at a sentinel to prove | ||
| # the function overwrites it. | ||
| topk_ids = torch.tensor([[5, 40, 100, 999]], dtype=torch.int32) | ||
| routed_weights = torch.tensor([1.0, 0.5, 0.25], dtype=torch.float32) | ||
| topk_weights = torch.tensor([[1.0, 0.5, 0.25, -123.0]], dtype=torch.float32) | ||
|
|
||
| topk_config = TopKConfig( | ||
| top_k=4, | ||
| num_fused_shared_experts=1, | ||
| routed_scaling_factor=self.ROUTED_SCALING_FACTOR, | ||
| ) | ||
|
|
||
| with ( | ||
| patch.object(topk_module, "_use_aiter", use_aiter), | ||
| patch.object( | ||
| topk_module, | ||
| "get_moe_expert_parallel_world_size", | ||
| return_value=self.EP_SIZE, | ||
| ), | ||
| patch.object( | ||
| topk_module, | ||
| "get_moe_expert_parallel_rank", | ||
| return_value=self.EP_RANK, | ||
| ), | ||
| ): | ||
| _out_ids, out_weights = topk_module._remap_topk_for_deepep( | ||
| topk_ids.clone(), | ||
| topk_weights.clone(), | ||
| num_fused_shared_experts=1, | ||
| num_physical_routed_experts=self.NUM_PHYSICAL_ROUTED, | ||
| topk_config=topk_config, | ||
| ) | ||
|
|
||
| # Routed weights must never be touched by the shared-weight override. | ||
| self.assertTrue(torch.equal(out_weights[0, :-1], routed_weights)) | ||
| return out_weights[0, -1].item() | ||
|
|
||
| def test_aiter_path_uses_unit_shared_weight(self): | ||
| # routed_scaling_factor is folded into the routed weights and the | ||
| # post-MoE multiply is skipped -> shared weight must be 1.0, NOT 1/rsf. | ||
| shared_weight = self._run_remap(use_aiter=True) | ||
| self.assertAlmostEqual(shared_weight, 1.0) | ||
|
|
||
| def test_post_moe_scaling_path_compensates_with_inverse_rsf(self): | ||
| # Default path: the whole MoE output is multiplied by rsf afterward, so | ||
| # the shared weight must be 1/rsf to net out to 1.0. | ||
| shared_weight = self._run_remap(use_aiter=False) | ||
| self.assertAlmostEqual(shared_weight, 1.0 / self.ROUTED_SCALING_FACTOR) | ||
|
|
||
| def test_shared_expert_ids_route_to_home_rank(self): | ||
| # Sanity: the shared slot id is placed at this rank's interleaved | ||
| # position (ep_rank * num_local_experts + num_local_routed). | ||
| topk_ids = torch.tensor([[5, 40, 100, 999]], dtype=torch.int32) | ||
| topk_weights = torch.tensor([[1.0, 0.5, 0.25, 0.0]], dtype=torch.float32) | ||
| topk_config = TopKConfig( | ||
| top_k=4, | ||
| num_fused_shared_experts=1, | ||
| routed_scaling_factor=self.ROUTED_SCALING_FACTOR, | ||
| ) | ||
| with ( | ||
| patch.object(topk_module, "_use_aiter", True), | ||
| patch.object( | ||
| topk_module, | ||
| "get_moe_expert_parallel_world_size", | ||
| return_value=self.EP_SIZE, | ||
| ), | ||
| patch.object( | ||
| topk_module, | ||
| "get_moe_expert_parallel_rank", | ||
| return_value=self.EP_RANK, | ||
| ), | ||
| ): | ||
| out_ids, _ = topk_module._remap_topk_for_deepep( | ||
| topk_ids.clone(), | ||
| topk_weights.clone(), | ||
| num_fused_shared_experts=1, | ||
| num_physical_routed_experts=self.NUM_PHYSICAL_ROUTED, | ||
| topk_config=topk_config, | ||
| ) | ||
| num_local_routed = self.NUM_PHYSICAL_ROUTED // self.EP_SIZE # 32 | ||
| num_local_experts = num_local_routed + 1 # 33 | ||
| expected_shared_id = self.EP_RANK * num_local_experts + num_local_routed | ||
| self.assertEqual(out_ids[0, -1].item(), expected_shared_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rbrugaro-amd is it expected that we register the test file to cpu test ? thanks.