Pull Request已成功合入, 合并人@CANN-robot
(感谢 田野 的贡献)变更摘要
此 PR 新增了 SoftmaxGradExt 融合 Pass,将计算图中由 Mul、ReduceSum、Sub、Mul 组成的 softmax 反向梯度计算子图替换为单个 SoftmaxGradExt 算子,以减少算子数量、提升执行效率。共包含两个融合 Pass:SoftmaxGradExtFusionPass(v1 模式)和 SoftmaxGradExtV2FusionPass(4 种 v2 变体模式),同时补充了 SoftmaxGradExt 算子的 Infershape 实现及对应单元测试。
主要改动
-
新增
SoftmaxGradExtFusionPass(v1 模式):匹配Mul(grad, x1) → ReduceSum → Sub(grad, sum) → Mul(x2, x1) → Mul(mul1, sub)的计算子图,将其融合为SoftmaxGradExt(grad, x1, x2)算子,并从匹配到的ReduceSum节点中提取axes和keep_dims属性传递给融合算子。 -
新增
SoftmaxGradExtV2FusionPass(v2 模式):覆盖 4 种mul1和mulGrad输入顺序不同的子图变体(Mul(x1, sub)/Mul(sub, x1)与Mul(mul1, x2)/Mul(x2, mul1)的组合),确保多种 IR 生成模式均能被正确识别和融合。 -
新增
SoftmaxGradExtInfershape 实现:在softmax_grad_ext_infershape.cpp中注册了SoftmaxGradExt的形状推导和数据类型推导逻辑——输出形状等同于第一个输入(grad)的形状,输出数据类型与输入grad一致。 -
新增融合 Pass 单元测试:覆盖 v1 和 v2 各变体在
DT_FLOAT16/DT_FLOAT类型、不同axis和keep_dims参数下的融合正确性验证,以及未知 shape 跳过融合、非 Ascend950 平台跳过融合、v1/v2 模式互斥(交叉不匹配)等边界场景。 -
新增 Infershape 单元测试:验证 3D/4D 不同输入形状下输出 shape 的正确性,以及输出 dtype 与输入
graddtype 一致的逻辑。


代码审查
我已经完成了对所有 8 个变更文件的审查。以下是审查结果汇总。
审查结果汇总
| 优先级 | 数量 | 说明 |
|---|---|---|
| P0 | 0 | — |
| P1 | 0 | — |
| P2 | 1 | BuildAndReset 返回值未做空指针检查即传入 InferShape 解引用 |
| P3 | 5 | 日志中错误的 Pass 名称、未使用的 #include <list>、未使用的 CURRENT_DIRS、CMake 调试日志残留、文件末尾缺少换行符 |
各文件审查结论
| 文件 | 结论 |
|---|---|
activation/softmax_grad_ext/op_graph/CMakeLists.txt |
有 1 个 P3 问题(调试日志残留) |
activation/softmax_grad_ext/op_graph/fusion_pass/softmax_grad_ext_fusion_pass.cpp |
有 2 个问题(1 个 P2 + 1 个 P3) |
activation/softmax_grad_ext/op_graph/fusion_pass/softmax_grad_ext_fusion_pass.h |
无问题 |
activation/softmax_grad_ext/op_host/softmax_grad_ext_infershape.cpp |
有 2 个 P3 问题(未使用 include + 缺少换行符) |
activation/softmax_grad_ext/tests/ut/op_graph/CMakeLists.txt |
有 1 个 P3 问题(未使用 CURRENT_DIRS) |
activation/softmax_grad_ext/tests/ut/op_graph/test_softmax_grad_ext_fusion_pass.cpp |
无问题 |
activation/softmax_grad_ext/tests/ut/op_host/CMakeLists.txt |
无问题 |
activation/softmax_grad_ext/tests/ut/op_host/test_softmax_grad_ext_infershape.cpp |
无问题 |
整体风险判断
低风险。 该变更是一个新增的融合 Pass 及其配套的 UT,核心逻辑(模式匹配、属性提取、替换图构建)经过仔细审查,未发现逻辑错误或安全漏洞。唯一的 P2 问题(BuildAndReset 空指针)在正常流程中不太可能触发,但建议增加防御性检查以提升健壮性。其余 P3 问题均为不影响运行时正确性的轻微改进项。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


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 |
|---|---|---|
| activation | ✅ 王星, 苏跃明 (2/2) | ✅ 苏跃明, 王星 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
transformer910, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
在 activation/softmax_grad_ext 下新增 SoftmaxGradExt 融合规则,使用新框架
PatternFusionPass 写法重写 canndev 原版 v1/v2 两个 pass,逻辑保持一致;
同时补充 infershape 实现及完整 UT。
主要改动
融合规则 (op_graph/fusion_pass/)
softmax_grad_ext_fusion_pass.h/.cpp
mul=Mul(grad,x1) -> sum=ReduceSum(mul) -> sub=Sub(grad,sum) ->
mul1=Mul(x2,x1) -> mulGrad=Mul(mul1,sub),
替换为单个 SoftmaxGradExt(grad, x1, x2)。
输入顺序的 4 种排列,其余结构与 v1 一致。
keep_dims 从属性读取,透传到 SoftmaxGradExt 的 axes/keep_dims 属性,
与 canndev 原版 ops::GetConstIntData(op, 1, axis) 逻辑一致。
成环问题修复:pattern 中 ReduceSum 的 axes 使用 CreateConst(内部
Const 节点)而非 CreateInput(Data 边界节点)。CreateInput 会创建
第 4 个 Data 节点,其 required-but-unconnected input anchor 在某些
GE 库版本下导致 TopologicalSorting 判定 pattern graph 不可达节点,
报 "cycle in pattern graph"。改回 CreateConst 后 pattern graph
为标准 3-Data + Const 结构,与 canndev 原版一致。
InferShape (op_host/)
UT
其他
注释 dlog_setlevel 调用(预存编译错误,与 rms_norm_grad 修复方式一致)
验证
关联的Issue
https://gitcode.com/cann/ops-nn/issues/4807
测试
文档更新
类型标签
AI/Agent生成声明