Merged
MoeInitRouting和MoeReRouting算子支持topk_weight重排 #6793
BruceLPAdd on Jun 12
MoeInitRouting和MoeReRouting算子支持topk_weight重排 #6793
Merged
Merged fromBruceLP/ops-transformertopk_weightintocann/ops-transformermaster
Pull Request has been successfully merged by CANN-robot.
(Thank you for BruceLP's contribution.)Jun 12 Added label: cann-cla/yes
CANN-robot
Jun 12 Comments:
Jun 12 Comments:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| */*/README.md | ✅ 陈娇, 周文芳, 蒋礼锐 (3/2) | ✅ 陈娇 (1/1) |
| */*/docs/acl*.md | ✅ 陈娇, 周文芳, 蒋礼锐 (3/2) | ✅ 陈娇 (1/1) |
| */*/op_graph/*_proto.h | ✅ 王永光, 蒋礼锐 (2/2) | ✅ 王永光 (1/1) |
| */*/op_host/*_def.cpp | ✅ 王永光, 蒋礼锐 (2/2) | ✅ 王永光 (1/1) |
| */*/op_host/op_api/aclnn*.h | ✅ 王永光, 蒋礼锐 (2/2) | ✅ 王永光 (1/1) |
| docs | ✅ wang-minbo, 黄俊健, 蒋礼锐 (3/2) | ✅ wang-minbo, 黄俊健, 蒋礼锐 (3/1) |
| moe | ✅ 黄俊健, 蒋礼锐, cpy_123456, guoqiuhao (4/2) | ✅ 黄俊健, 蒋礼锐, cpy_123456 (3/1) |
| sig-ops-transformer | ✅ 黄俊健, 蒋礼锐 (2/2) | ✅ 黄俊健, 蒋礼锐 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
BruceLP, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Jun 12 Added label: stat/needs-squash
Jun 12 Pre merge succeeded (commit_id: 2ec7e5164176c2769d3e9e3a7703b124996f2b0c)
703 messages are collapsed here。 See more
monologue815
Aug 10 Comments:
Aug 10 Comments:
PR 审查:MoeInitRoutingV4和MoeReRoutingV2算子支持topk_weight重排
变更概述
本PR为 MoE 路由算子新增 topk_weight 重排能力,使路由权重与输出 token 对齐。主要改动:
- MoeInitRoutingV4(新算子,仅 A950):继承 V3 tiling,新增
topk_weight/expanded_topk_weight可选输入输出,active_num从属性改为可选输入(当前未读取实际值),isInputTopkWeighttiling 标志控制重排逻辑 - V3 内核扩展:
MoeV3FullLoadBase新增TopkWeightScatterOut/TopkWeightGatherOut/TopkWeightZeroOut,非 FullLoad 路径新增MoeV3TopkWeightOut类;V3 apt.cpp 传 nullptr 占位 - V3 tiling 重构:常量和辅助函数从 .cpp 积入 .h,
const static→constexpr,供 V4 继承复用 - MoeReRoutingV2(新算子,仅 A950):复用 V1 kernel,新增
expert_topk_weight/permute_topk_weight输入输出,tiling 新增hasTopkWeight字段 - V1 内核扩展:
MoeReRoutingRRegbase/MoeReRoutingReRegbase新增CopyTopkWeight方法,按条件搬运 topkWeight
审查结果
- [严重] 0项
- [建议] 4项 — V3 nullptr 空指针风险、V4 activeNum_ 未读取输入、逐元素 DataCopyPad 性能差、RESERVE_UB_GAP 逻辑不对称
- [优化] 1项 — V3 tiling 重构与功能变更混提交
重点关注
- V3 nullptr 安全性:V3 apt.cpp 传
nullptr给 FullLoad Init 的 topkWeight 参数,依赖isInputTopkWeight_==0守护。虽然当前 V3 不会触发,但缺少防御性校验,未来维护风险。 - V4 activeNum_ 未生效:PR 描述说「active_num 从属性改为可选输入」,但实现硬编码
activeNum_=-1,未读取输入 tensor 值。当前行为与 V3 完全一致,用户传入 active_num 输入会被静默忽略。 - topkWeight 重排逻辑正确:FullLoad 路径按 epFullload/isInputScale 选择 Scatter/Gather,非 FullLoad 路径按 dropPadMode 选择 DropPadGather/DroplessScatter,逻辑与 expandedRowIdx 重排一致。
- V4 复用 V3 kernel 设计合理:通过继承 + tiling 标志控制,避免重复实现。但 V4 apt.cpp 的注释「If MoeInitRoutingV3 kernel is modified, MoeInitRoutingV4 must be adapted accordingly」需持续维护。
- ReRouting CopyTopkWeight 实现简洁:批量 DataCopyPad 按 INDEX_UB_SIZE 分段,比 MoeInitRouting 的逐元素拷贝高效。


jiang-lirui
Aug 10 Comments:
Aug 10 Comments:
/approve
/lgtm


Aug 10 Added label: lgtm
Aug 10 Closed associated issue
Aug 10 Merged pull request
描述
topk_weight 重排能力使 MoE 推理和训练场景中路由权重与输出 token 保持对齐,支持后续 expert 计算时的加权聚合。通过新建独立算子(V4/V2)而非修改现有算子(V3/V1)实现,避免影响非 950 架构,降低维护成本。V4 同时将 active_num 从属性改为可选输入,支持图模式下动态传入。V4/V2 最大程度复用 V3/V1 的 tiling 和 kernel 代码,避免重复实现。
关联的Issue
关联Issue #4068
测试
ST用例测试正常
文档更新
新增文档
moe/moe_init_routing_v4/docs/aclnnMoeInitRoutingV4.md
torch_extension/cann_ops_transformer/docs/zh/moe_init_routing.md
torch_extension/cann_ops_transformer/docs/zh/moe_re_routing.md
类型标签