-
Notifications
You must be signed in to change notification settings - Fork 736
[RL] support moe-topk use topk_reduce_func #7218
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
Open
zoooo0820
wants to merge
4
commits into
PaddlePaddle:develop
Choose a base branch
from
zoooo0820:align_moe_topk
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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 |
|---|---|---|
|
|
@@ -90,13 +90,22 @@ def get_moe_scores( | |
| expert_in_rank_num_list: paddle.Tensor = None, | ||
| tokens_per_expert_stats_list: paddle.Tensor = None, | ||
| redundant_ep_rank_num_plus_one: int = 1, | ||
| topk_reduce_func: Callable = lambda x: x.sum(axis=-1, keepdim=True) + 1e-20, | ||
| ) -> paddle.Tensor: | ||
| """ | ||
| compute moe scores using e_score_correction_bias. | ||
| """ | ||
| scores = paddle.nn.functional.sigmoid(gating_output) | ||
| assert e_score_correction_bias is not None, "e_score_correction_bias is none!" | ||
| scores_with_bias = scores + e_score_correction_bias | ||
|
|
||
| if envs.FD_USE_PHI_MOE_TOPK: | ||
| # calculate renormalize and routed_scaling_factor value outside the noaux_tc | ||
| original_renormalize = renormalize | ||
| original_routed_scaling_factor = routed_scaling_factor | ||
| renormalize = False | ||
| routed_scaling_factor = 1.0 | ||
|
|
||
| if expert_id_to_ep_rank_array is None: | ||
| scores, topk_values, topk_idx = noaux_tc( | ||
| scores, | ||
|
|
@@ -123,6 +132,16 @@ def get_moe_scores( | |
| routed_scaling_factor, | ||
| redundant_ep_rank_num_plus_one, | ||
| ) | ||
| if envs.FD_USE_PHI_MOE_TOPK: | ||
| if original_renormalize: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 建议 建议添加参数文档说明: """
compute moe scores using e_score_correction_bias.
Args:
...
topk_reduce_func: Callable function for normalizing topk_values.
Only used when FD_USE_PHI_MOE_TOPK=1 and renormalize=True.
Default: lambda x: x.sum(axis=-1, keepdim=True) + 1e-20
""" |
||
| if topk_reduce_func is not None: | ||
| topk_values = topk_values / topk_reduce_func(topk_values) | ||
| else: | ||
| # 使用默认的 sum + epsilon | ||
| topk_values = topk_values / (topk_values.sum(axis=-1, keepdim=True) + 1e-20) | ||
|
|
||
| if original_routed_scaling_factor != 1.0: | ||
| topk_values *= original_routed_scaling_factor | ||
| return scores, topk_values, topk_idx | ||
|
|
||
|
|
||
|
|
@@ -152,6 +171,8 @@ def __init__( | |
| with_bias: bool = False, | ||
| activation="swiglu", | ||
| model_format: Optional[str] = None, | ||
| topk_reduce_func: Callable = lambda x: x.sum(axis=-1, keepdim=True) | ||
| + 1e-20, # only used when FD_USE_PHI_MOE_TOPK=1, default is same as noaux_tc kernel | ||
| ): | ||
| """ | ||
| Initialize the Moe layer with given parameters. | ||
|
|
@@ -197,6 +218,7 @@ def __init__( | |
| self.moe_tag = moe_tag | ||
| self.with_bias = with_bias | ||
| self.activation = activation | ||
| self.topk_reduce_func = topk_reduce_func | ||
|
|
||
| if self.ep_size > 1: | ||
| expert_id_offset = expert_id_offset + self.ep_rank * self.num_local_experts | ||
|
|
||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.