Merged
MoeGatingTopK A2/A3补充支持 softplus (norm_type=2) #11117
MoeGatingTopK A2/A3补充支持 softplus (norm_type=2) #11117
Merged
SunAdd on Sep 2
Sun
SunCollaborators
Sep 2

描述

为MoeGatingTopK算子的A2/A3实现补充支持SqrtSoftplus, 即norm_type=2。

normOut = sqrt(Softplus(x)) = sqrt(ln(exp(x) + 1))

文件 修改
op_host/moe_gating_top_k_tiling.cpp CheckAttr 中 norm_type 合法取值由 {0,1} 扩展为 {0,1,2};新增校验:norm_type=2 时要求 group_count==1
op_kernel/moe_gating_top_k_common.h 新增常量 NORM_TYPE_SOFTPLUS = 2
op_kernel/moe_gating_top_k_without_group.h ① ComputeX 新增 SqrtSoftplus 计算分支:Exp → Adds(1) → Ln → Sqrt;② SelectTopKExpertScore 中 needRenorm 条件补充 norm_type==2 恒做归一化。该 kernel 同时服务 TopK 模式(tiling key=1)与 Hash 模式(tiling key=3~6),一次修改两种模式同时生效
README.md、docs/aclnnMoeGatingTopK.md、docs/aclnnMoeGatingTopKV2.md 去除 norm_type=2"仅 Ascend 950 支持"的限制说明;renorm 公式分母由 ReduceSum(normOut) 修正为 ReduceSum(gathered),消除歧义
tests/ut/op_host/test_moe_gating_top_k_tiling.cpp 新增 succ_05(TopK+softplus,tiling key=1)与 succ_06(Hash+softplus,tiling key=4)用例

增量性说明:

  • 所有新增计算/校验仅在 norm_type==2 时可达:tiling 的 group_count 校验条件含 norm_type==2,对 0/1 恒不触发;kernel 的 else 分支在 norm_type=0/1 时控制流不变;needRenorm 对 norm_type=1 仍恒真、对 0 仍只看 renorm==1
  • normType_ == 1/0 改写为 == NORM_TYPE_SIGMOID/SOFTMAX 命名常量,数值相同,编译后等价
  • tiling key 编码、kernel 分发、接口签名、tiling data 结构均无变化
  • generalized / e_k_fullload 内核未改动(group_count==1 约束保证 softplus 只走 without_group 路径)

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/4870

测试

TTK测试norm_type=2已通过

文档更新

moe/moe_gating_top_k/README.md
moe/moe_gating_top_k/docs/aclnnMoeGatingTopK.md
moe/moe_gating_top_k/docs/aclnnMoeGatingTopKV2.md

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Sun's contribution.)
SunSunCollaborators
Sep 2 Created pull request, commit 8ef264dc
atomgit-bot
atomgit-bot
Sep 2 Comments:

变更摘要

本 PR 为 MoeGatingTopK 算子的 A2/A3 实现新增 norm_type 取值为 2(即 SqrtSoftPlus/softplus)的支持。核心改动包括:在 host 侧 moe_gating_top_k_tiling.cpp 与 kernel 侧 moe_gating_top_k_common.h 中统一新增常量 NORM_TYPE_SOFTPLUS = 2;在 CheckAttr() 中放开对 norm_type 取值的校验,并新增约束:当 norm_type 为 2 时 group_count 必须为 1;在无分组归一化实现 moe_gating_top_k_without_group.h 的 ComputeX() 中新增 sqrt(ln(exp(x) + 1)) 的计算分支,并在 SelectTopKExpertScore() 中将 softplus 与 sigmoid 一样视为恒做归一化的场景。

主要改动

  • 新增 norm_type 常量定义: 在 moe_gating_top_k_tiling.cpp 和 moe_gating_top_k_common.h 中分别加入 NORM_TYPE_SOFTPLUS = 2,与既有的 NORM_TYPE_SOFTMAX、NORM_TYPE_SIGMOID 并列,供 tiling 与 kernel 两侧统一引用。
  • 放宽并补充 tiling 属性校验: MoeGatingTopKTilingBase::CheckAttr() 允许 norm_type 取 0、1、2(错误提示相应更新为 "0, 1 or 2"),并新增 OP_CHECK_IF 校验:当 norm_type 为 2(softplus)时 group_count 必须为 1,否则报错返回 GRAPH_FAILED。
  • kernel 侧新增 sqrt softplus 归一化计算: 在 moe_gating_top_k_without_group.h 的 ComputeX() 中把原有的数字字面量分支替换为具名常量判断,并为 NORM_TYPE_SOFTPLUS 增加 else 分支,通过 Exp、Adds(+1)、Ln、Sqrt 序列实现 sqrt(ln(exp(x) + 1))。
  • 归一化条件扩展至 softplus: 在 SelectTopKExpertScore() 中,needRenorm 的计算条件改为 NORM_TYPE_SIGMOID || NORM_TYPE_SOFTPLUS 恒为真,或 NORM_TYPE_SOFTMAX && renorm_ == 1,使 softplus 路径与 sigmoid 一样固定执行归一化。
  • 补充 tiling 单元测试: 在 test_moe_gating_top_k_tiling.cpp 中新增 moe_gating_top_k_tiling_succ_05_norm_type_softplus 和 moe_gating_top_k_tiling_succ_06_hash_norm_type_softplus 两个用例,分别验证非 hash 与 hash 场景下 norm_type=2 的 tiling 数据、tilingKey(1 与 4)及 workspace 期望值。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 2 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 2 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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
*/*/README.md ✅ 陈娇, 於欣洁 (2/2) ✅ 陈娇 (1/1)
*/*/docs/acl*.md ✅ 陈娇, 於欣洁 (2/2) ✅ 陈娇 (1/1)
moe ✅ 宋恺, 於欣洁 (2/2) ✅ 宋恺 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robot
CANN-robotCollaborators
Sep 2 Comments:

CLA Signature Pass

LuckySun, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Sep 2 Set cpy_123456,juyangokok,monologue815,chen-linxin4,yu-xinjie62,jiang-lirui,Allan_Yu,gitee-duhuiping,claire8090,songkai111,liudan12,gitcode-chenjiao,cc-z,Ww7012,mabing1118,fengying555682,guoqiuhao,caiwenwen,yanglu-1 as approver
CANN-robotCANN-robotCollaborators
Sep 2 Set cpy_123456,juyangokok,monologue815,chen-linxin4,yu-xinjie62,jiang-lirui,Allan_Yu,gitee-duhuiping,claire8090,songkai111,liudan12,gitcode-chenjiao,cc-z,Ww7012,mabing1118,fengying555682,guoqiuhao,caiwenwen,yanglu-1 as approver
SunSunCollaborators
Sep 2 Change title to "MoeGatingTopK A2/A3补充支持 softplus (norm_type=2)", originally titled "MoeGatingTopK A2/A3支持norm_type为SqrtSoftPlus".
SunSunCollaborators
Sep 2 Modify description of pull request
Sun
SunCollaborators
Sep 2 Comments:

compile

likedislike
SunSunCollaborators
Sep 2 Pre merge succeeded (commit_id: 35d3a11f839063a0db5c90d678af69e79ad2dcfc)
CANN-robot
CANN-robotCollaborators
Sep 2 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Sep 2 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 2 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 2 Added label: ci-pipeline-passed
Sun
SunCollaborators
Sep 3 Comments:

compile

likedislike
SunSunCollaborators
Sep 3 Pre merge succeeded (commit_id: 9226c51816f6441a2c5736c59ba095b18ecbe58b)
CANN-robot
CANN-robotCollaborators
Sep 3 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Sep 3 Removed label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
Sep 3 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 3 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 3 Added label: ci-pipeline-passed
Sun
SunCollaborators
Sep 4 Comments:
likedislike
chenjiao
chenjiaoCollaborators
Sep 4 Comments:

/approve

likedislike
songkai111
songkai111Collaborators
Sep 4 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 4 Added label: approved
於欣洁
於欣洁Collaborators
Sep 4 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 4 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 4 Closed associated issue
CANN-robotCANN-robotCollaborators
Sep 4 Merged pull request