已合并
常量折叠和infershape整改 #4582
liu-lu创建于 19 天前
常量折叠和infershape整改 #4582
已合并
liu-lu创建于 19 天前
liu-lu成员
19 天前

描述

本 PR 为 6 个 AICPU 算子(add、round、search_sorted、select_v2、square、squared_difference)使能 GE 图编译期常量折叠,并对 search_sorted、select_v2 进行 infershape/OpDef 整改。

变更内容

1. 常量折叠使能(6 算子)

算子 CMakeLists kernel 注册
add 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
round 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
search_sorted 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
select_v2 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
square 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
squared_difference 增加 HOSTCPU TRUE REGISTER_CPU_KERNELOPS_MATH_REGISTER_CPU_KERNELV2
  • 各 kernel 增加 #include "aicpu/math_aicpu_register.h"
  • CMakeLists 增加 HOSTCPU TRUE,将算子编入 host 侧折叠 so(libopconstant_folding_math.so)。

2. infershape / OpDef 整改

  • search_sorted:新增 op_host/search_sorted_infershape.cpp,实现 InferShape4SearchSorted,输出 shape 取 values(input1)的 shape;
  • select_v2:新增 op_kernel_aicpu/select_v2_aicpu_def.cpp(基于 OpDef 注册输入输出数据类型),删除旧 select_v2.json(JSON 配置方式),统一为 OpDef 注册链路。

关联的Issue

#2847

测试

根据代码变更,测试场景如下:

  1. 编译构建测试

    • 执行 cmake 配置与编译,验证 6 个算子 CMakeLists HOSTCPU TRUE 配置变更正确;
    • 确认 libopconstant_folding_math.so 中已编入上述 6 个算子的 host 侧 kernel。
  2. 常量折叠验证

    • 输入为常量时,验证算子可在图编译期被折叠求值(host 侧执行),运行期不再下发 device kernel;
    • 输入为非常量时,验证 device 侧执行逻辑与折叠前保持一致。
  3. InferShape 验证(search_sorted)

    • 验证 search_sorted 输出 shape 与 values 输入 shape 一致;
    • 覆盖多维度、边界 shape 场景。
  4. 算子注册/加载验证(select_v2)

    • 验证 select_v2 基于 OpDef 注册后接口可正确加载与调用;
    • 覆盖全数据类型(COMPLEX128/COMPLEX64/DOUBLE/FLOAT/FLOAT16/INT16/INT32/INT64/INT8/UINT16/UINT32/UINT64/UINT8/BOOL)。
  5. 算子泛化

    • 对 6 个算子执行二级冒烟与泛化用例,确认功能正确性不受影响。

文档更新

本 PR 不涉及 README/aclnn 接口文档更新。

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 liu-lu 的贡献)
Lliu-lu成员
19 天前 创建了 pull request,commit 02aa40f5
atomgit-bot
atomgit-bot
19 天前 评论:

变更摘要

此 PR 主要围绕"常量折叠和 infershape 整改",对 math 算子模块进行统一整改:将多个算子的内核注册宏从 REGISTER_CPU_KERNEL 替换为 OPS_MATH_REGISTER_CPU_KERNELV2,将 SelectV2 的算子定义从 JSON 配置文件迁移到 C++ OpDef 类,并为 SearchSorted 新增 InferShape 实现。这些改动涉及 6 个算子的内核注册、1 个算子的定义方式重构以及 1 个算子的 shape 推导补齐。

主要改动

  • 内核注册宏统一替换: 将 AddRoundSearchSortedSelectV2SquareSquaredDifference 共 6 个算子中的 REGISTER_CPU_KERNEL 替换为 OPS_MATH_REGISTER_CPU_KERNELV2,并新增 #include "aicpu/math_aicpu_register.h" 头文件引用,实现注册方式的统一。

  • SelectV2 算子定义从 JSON 迁移到 C++ OpDef: 删除了 select_v2.json 配置文件(31 行),新增 select_v2_aicpu_def.cpp,通过继承 OpDef 类定义 SelectV2 的输入输出数据类型约束,并调用 ApplyMathAICpuDefaultCfgExtendCfgInfo 完成配置。

  • SearchSorted 新增 InferShape 实现: 新增 search_sorted_infershape.cpp,实现 InferShape4SearchSorted 函数——将输出 shape 设置为与 values 输入相同的 shape,并通过 IMPL_OP_INFERSHAPE(SearchSorted) 注册。

  • 分类规则补充: 在 classify_rule.yaml 中新增 ops/ops-math/math/search_sorted/op_host/ 路径,将 SearchSorted 的 host 端代码纳入分类管理。

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

代码审查

✅ 未发现问题

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.
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
math/add 冯彤, 宋恺 (2/2) 宋恺, 冯彤 (2/1)
math/round 冯彤, 宋恺 (2/2) 冯彤, 宋恺 (2/1)
math/search_sorted 刘伟, 冯彤, 宋恺 (3/2) 冯彤, 宋恺, 刘伟 (3/1)
math/square 冯彤, 宋恺 (2/2) 宋恺, 冯彤 (2/1)
repo-cann/ops-math 冯彤, 宋恺 (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

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

likedislike
此处折叠了134条消息 查看更多
CANN-robotCANN-robot成员
5 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
5 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
5 天前 添加了label:ci-pipeline-passed
CANN-robotCANN-robot成员
5 天前 关闭了关联的issue
CANN-robotCANN-robot成员
5 天前 合入了pull request