-
Notifications
You must be signed in to change notification settings - Fork 107
Reuse MoETopkFusion in quantile-balancing routing #1775
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
xingmingyyj
merged 3 commits into
PaddlePaddle:develop
from
xingmingyyj:opt_moe_permute
Aug 19, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
Oops, something went wrong.
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.
moe_k次选择后才计算 cutoff;当合法配置moe_k == n_experts时,所有候选都已被置为-inf,alpha会变成-inf。旧路径对min(k + 1, E)取第 E 个值,得到的是最小 biased score;新值会让_accumulate_qb_histogram的required_bias全部落入边界 bin,后续 QB callback 更新出错误 bias。请在屏蔽最后一个专家前保存最小值,或对moe_k >= n_experts特判使用原始候选的最小值。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.
触发条件是 moe_k == n_experts(或 node-limit 下 moe_k == 选中组的候选总数)。这意味着每个 token 路由到全部专家——那是 dense,不是 MoE,而 QB 的唯一目的就是均衡路由负载,全选的时候负载均衡根本没意义。DeepSeek/Kimi 这类配置永远是 moe_k << n_experts(比如 8/256),node-limit 后候选池也还有上百个(topk_group*epg),远大于 moe_k。所以这个分支在任何真实 QB 训练里都不会走到。
moe_k < n_experts 且无 node-limit 时循环后必留有限值,alpha 不可能是 -inf,没有问题。
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.
你指出的
moe_k == n_experts全选边界对实际 QB 配置确实不构成问题,我撤回这部分理由。但当前实现仍绕过了 QB 原有的n_group > 1拒绝:forward在n_group > 1时把use_node_limit=True传给 fusion kernel,kernel 返回的是“选中组内”的 cutoff,而_accumulate_qb_histogram把它当成全E专家的(k+1)-th cutoff 使用。这样 histogram 与原 QB 定义不一致,callback 会据此更新出错误的 per-expert bias。请保持 QB fusion 限制为n_group == 1,或同步实现并验证 node-limit 下的 histogram 语义;在此修复前我保留该 finding。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.
感谢 review。这条 finding 描述的状态——「QB + n_group > 1 走 fusion 路径」——在当前实现下无法构造,因此不会发生,理由如下:
StandardMoERouter.init 里的守卫一直在:
if self.topk_method == "quantile_balancing":
...
if self.n_group != 1:
raise ValueError(
"Quantile Balancing currently only supports n_group=1. "
"Multi-group routing (n_group>1) is not compatible with QB ..."
)
本 PR 删除的只是另一处「quantile_balancing 与 moe_topk_fusion 不兼容」的检查,这个 n_group 守卫一行都没动(可对照 diff 里对 n_group 的改动,仅出现在 forward 的 use_node_limit = self.n_group > 1 与 kernel 传参处)。也就是说,任何 n_group > 1 的 QB router 在构造期就会抛 ValueError,根本进不到 forward。
self.n_group = config.n_group,而 QB 已被强制 n_group == 1,故:
use_node_limit = self.n_group > 1 # QB 分支恒为 False
kernel 不会执行任何 node-limit 组内屏蔽,返回的 alpha 就是全 E 个专家的第 (k+1) 大 cutoff——正是 _accumulate_qb_histogram 所期望的定义。你描述的「kernel 返回选中组内 cutoff、histogram 按全 E 解释」这一步,在 QB 上无法被触发。
第二道守卫在 _topk_quantile_balancing 内,对 n_group != 1 同样抛错。
单测 test_n_group_greater_than_one_rejected 已断言 _build_qb_router(n_group=2) 抛出含 "only supports n_group=1" 的 ValueError。
综上,「保持 QB fusion 限制为 n_group == 1」这一诉求已经被满足,并有测试保证,不是隐式约定。node-limit 下的 histogram 语义无需在本 PR 实现,因为该组合被显式禁止。
为消除「守卫与 fusion 代码有距离」的顾虑,我在 fusion 分支旁补了一行注释,显式标注该不变量,不改变任何行为。
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.
已核对
StandardMoERouter.__init__的守卫和test_n_group_greater_than_one_rejected:QB +n_group > 1确实在构造阶段被拒绝,当前 head 不会进入 node-limit fusion。结合你对全选边界的说明,原 finding 的两个触发路径均不成立,我撤回该 finding;当前 head 未发现新的可阻塞问题。