已合并
feat(softmax_grad_ext): 新增 Ascend950 融合规则及 infershape 并补充 UT #8089
feat(softmax_grad_ext): 新增 Ascend950 融合规则及 infershape 并补充 UT #8089
已合并
田野创建于 19 天前
田野成员
19 天前

描述

在 activation/softmax_grad_ext 下新增 SoftmaxGradExt 融合规则,使用新框架
PatternFusionPass 写法重写 canndev 原版 v1/v2 两个 pass,逻辑保持一致;
同时补充 infershape 实现及完整 UT。

主要改动

  1. 融合规则 (op_graph/fusion_pass/)

    • softmax_grad_ext_fusion_pass.h/.cpp

      • SoftmaxGradExtFusionPass (v1):匹配
        mul=Mul(grad,x1) -> sum=ReduceSum(mul) -> sub=Sub(grad,sum) ->
        mul1=Mul(x2,x1) -> mulGrad=Mul(mul1,sub),
        替换为单个 SoftmaxGradExt(grad, x1, x2)。
      • SoftmaxGradExtV2FusionPass (v2):4 个变体,覆盖 mul1/mulGrad
        输入顺序的 4 种排列,其余结构与 v1 一致。
      • axis 从匹配到的 ReduceSum const 输入 (index 1) 读取,
        keep_dims 从属性读取,透传到 SoftmaxGradExt 的 axes/keep_dims 属性,
        与 canndev 原版 ops::GetConstIntData(op, 1, axis) 逻辑一致。
      • 平台校验:仅 Ascend950 生效;未知 shape 跳过。
      • 注册阶段:CustomPassStage::kAfterInferShape。
    • 成环问题修复: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 原版一致。

  2. InferShape (op_host/)

    • softmax_grad_ext_infershape.cpp
      • y.shape = grad.shape (input0)
      • y.dtype = grad.dtype (input0)
  3. UT

    • tests/ut/op_graph/test_softmax_grad_ext_fusion_pass.cpp (18 用例)
      • v1/v2 各变体 fp16/fp32、不同 axis 与 keep_dims 组合
      • 未知 shape、非 950 平台跳过
      • v1↔v2 互不误匹配
      • Mul 输入顺序调换不误匹配
      • 控制边成环场景 (WillCauseCycleIfFuse 检测)
      • axes const 共享场景
      • mulGrad 控制消费者场景
      • DumpToFile 输出图、node_count 校验
    • tests/ut/op_host/test_softmax_grad_ext_infershape.cpp (3 用例)
      • fp16 3D / fp32 4D shape 推导、dtype 推导
  4. 其他

    • op_graph/CMakeLists.txt:add_graph_plugin_sources()
    • tests/ut/op_graph/CMakeLists.txt:注册 OP_GRAPH_UT
    • tests/ut/op_host/CMakeLists.txt:启用 OP_INFERSHAPE_MODULE
    • tests/ut/op_host/arch35/test_softmax_grad_ext_tiling.cpp:
      注释 dlog_setlevel 调用(预存编译错误,与 rms_norm_grad 修复方式一致)

验证

  • infershape UT: 3 passed
  • fusion UT: 18 passed
  • 编译包 (--ops=softmax_grad_ext --soc=ascend950): 成功
  • clang-format / trailing-whitespace / end-of-file: 通过 pre-commit 检查

关联的Issue

https://gitcode.com/cann/ops-nn/issues/4807

测试

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:

AI/Agent生成声明

  • AI辅助编写
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 田野 的贡献)
田野成员
19 天前 创建了 pull request,commit cf65e0ad
atomgit-bot
atomgit-bot
19 天前 评论:

变更摘要

此 PR 新增了 SoftmaxGradExt 融合 Pass,将计算图中由 MulReduceSumSubMul 组成的 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 节点中提取 axeskeep_dims 属性传递给融合算子。

  • 新增 SoftmaxGradExtV2FusionPass(v2 模式):覆盖 4 种 mul1mulGrad 输入顺序不同的子图变体(Mul(x1, sub) / Mul(sub, x1)Mul(mul1, x2) / Mul(x2, mul1) 的组合),确保多种 IR 生成模式均能被正确识别和融合。

  • 新增 SoftmaxGradExt Infershape 实现:在 softmax_grad_ext_infershape.cpp 中注册了 SoftmaxGradExt 的形状推导和数据类型推导逻辑——输出形状等同于第一个输入(grad)的形状,输出数据类型与输入 grad 一致。

  • 新增融合 Pass 单元测试:覆盖 v1 和 v2 各变体在 DT_FLOAT16/DT_FLOAT 类型、不同 axiskeep_dims 参数下的融合正确性验证,以及未知 shape 跳过融合、非 Ascend950 平台跳过融合、v1/v2 模式互斥(交叉不匹配)等边界场景。

  • 新增 Infershape 单元测试:验证 3D/4D 不同输入形状下输出 shape 的正确性,以及输出 dtype 与输入 grad dtype 一致的逻辑。

likedislike
atomgit-bot
atomgit-bot
19 天前 评论:

代码审查

我已经完成了对所有 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 问题均为不影响运行时正确性的轻微改进项。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
CANN-robotCANN-robot成员
19 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
19 天前 评论:

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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
此处折叠了116条消息 查看更多
CANN-robotCANN-robot成员
4 天前 合入了pull request
CANN-robot
CANN-robot成员
4 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
田野成员
4 天前 修改了pull request 的描述
CANN-robot
CANN-robot成员
4 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
CANN-robot
CANN-robot成员
4 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike