已合并
新增Mul算子AICPU实现 #4111
liu-wei创建于 7月16日
新增Mul算子AICPU实现 #4111
已合并
liu-wei创建于 7月16日
liu-wei
liu-wei成员
7月16日

Closes #2284

描述

为 Mul 算子新增 AICPU 后端实现,支持 AI CPU 上执行逐元素乘法计算。

新增文件

文件 行数 说明
mul_aicpu.h 46 CpuKernel 类声明,含模板方法分发与对齐检查
mul_aicpu.cpp 487 基于 Eigen 的 Mul 内核实现,支持广播和多数据类型
mul_aicpu_def.cpp 33 OpDef 注册,支持 13 种数据类型
test_geir_mul.cpp 309 GE IR 测试示例
test_mul_aicpu.cpp 267 单元测试

修改文件

  • docs/zh/op_list.md:Mul 支持后端更新为 AI Core/AI CPU

关键特性

  • 支持 INT8/UINT8/INT16/UINT16/INT32/UINT64/FLOAT/FLOAT16/BF16/DOUBLE/COMPLEX64/COMPLEX128 共 13 种数据类型
  • 支持广播(broadcast)语义
  • 使用 Eigen Tensor 库实现高性能计算
  • 自动并行处理阈值:同 shape 7K / 中等 33K / 通用 6K

测试情况

  • 单元测试通过
  • 算子泛化验证

类型标签

  • 新特性

🤖 Generated with Claude Code

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 liu-wei 的贡献)
liu-weiliu-wei成员
7月16日 创建了 pull request,commit 218f21ec
atomgit-bot
atomgit-bot
7月16日 评论:

变更摘要

本 PR 为 Mul(逐元素乘法)算子新增 AICPU 内核实现,支持同类型和混合类型的张量乘法运算。核心实现包括 MulCpuKernel 类,提供完整的广播(broadcast)语义、Eigen 加速的并行计算以及覆盖 10 余种数据类型的同类型/混合类型调度逻辑。同时新增了单元测试和 GE IR 集成测试示例。

主要改动

  • 新增 mul_aicpu.cpp 内核实现: 实现了 MulCpuKernel::Compute 主入口,通过 MulSameTypeCompute 处理同类型输入(如 DT_FLOAT16, DT_FLOAT, DT_INT32, DT_COMPLEX64 等 14 种类型),并通过 GetMulDiffTypeCalls 静态查找表处理混合类型输入(覆盖 int/float/complex 等多种组合),统一支持标量、同 shape 及广播乘法。
  • 引入广播与并行计算策略: MulCompute 内部使用 Bcast 生成广播信息,按 0~8 维分别 dispatch 到 MulCalculateBcastComputeNoBcastCompute / SpecialCompute 通过 CpuKernelUtils::ParallelFor 实现多核并行,阈值通过 kParallelDataNum 等常量控制。
  • 新增 mul_aicpu.h 头文件: 声明 MulCpuKernel 类及其私有模板方法 MulComputeAlignedCheckMulCalculateWithAlignedCheckMulCalculate,继承自 CpuKernel
  • 新增 mul_aicpu_def.cpp 算子定义: 注册 Mul 算子,声明两个输入 x1/x2 和一个输出 y,支持 INT8COMPLEX128 共 14 种数据类型,并调用 ApplyMathAicpuDefaultCfg 设置默认 AICPU 配置。
  • 新增单元测试与集成测试示例: test_mul_aicpu.cpp 覆盖同 shape、标量、广播、大张量并行、混合类型、不支持类型、广播 shape 不匹配、空输入等场景;test_geir_mul.cpp 提供 GE IR 运行示例,验证 DT_COMPLEX128 的端到端乘法正确性。
likedislike
atomgit-bot
atomgit-bot
7月16日 评论:

代码审查

所有文件均已审查完毕。以下是最终汇总:

审查结束 — 汇总

  • docs/zh/op_list.md:无问题(文档修复,将 Mul 算子支持平台从 "AI Core" 更新为 "AI Core/AI CPU")。

  • math/mul/examples/test_geir_mul.cpp:无问题(GE IR 示例代码,资源管理正确,逻辑合理)。

  • math/mul/op_kernel_aicpu/mul_aicpu.cpp:发现 2 个问题

    • P1(第 317 行):分发表 DT_UINT8DT_INT32 条目中,模板参数 TIn1 误写为 int8_t,应为 uint8_t。当 uint8_t 值 ≥ 128 时,符号扩展会导致错误的乘法结果。
    • P3(第 218-220 行):异类型计算路径缺少对 GetData() 返回值的空指针校验,与同类型路径的防御深度不一致。
  • math/mul/op_kernel_aicpu/mul_aicpu.h:无问题(头文件定义清晰,const 正确,Eigen 宏定义顺序合理)。

  • math/mul/op_kernel_aicpu/mul_aicpu_def.cpp:无问题(算子注册正确,类型列表与内核实现一致)。

  • math/mul/tests/ut/op_kernel_aicpu/test_mul_aicpu.cpp:无问题(测试用例覆盖了同类型、广播、标量、并行、异类型、不支持类型、形状不匹配、空输入等场景;但缺少 DT_UINT8 + DT_INT32 组合的测试,导致上述 P1 问题未被捕获)。

总体风险判断

中等风险。P1 问题是实际的逻辑错误,会在特定输入类型组合(uint8_t × int32_t,且 uint8_t 值 ≥ 128)下产生静默错误结果。好在该组合目前未在测试用例中覆盖,生产环境中也属于较罕见的数据类型组合。修复仅需将单个字符 int8_t 改为 uint8_t,风险极低。建议补充相应测试用例以覆盖该路径。

类型 数量
🔴 阻塞 1
🟡 建议 1

⛔ 需要修改

likedislike
CANN-robotCANN-robot成员
7月16日 添加了label:cann-cla/yes
liu-wei
liu-wei成员
7月16日 评论:

compile

likedislike
CANN-robot
CANN-robot成员
7月16日 评论:

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
math/mul 宋恺, wangrui_ (2/2) 宋恺 (1/1)
repo-cann/ops-math 宋恺, wangrui_ (2/2) 宋恺 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

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

likedislike
liu-weiliu-wei成员
7月16日 update merge request[project id: 7649531, iid: 4111, commit_id: c8c84be334bd34f5c6963d104ae63dc36e059cd5] virtual merging success
CANN-robotCANN-robot成员
7月16日 将FelixTang7,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,zhangyuxiang0119,llimwang,gubaocheng,andong_hw,xuejinghui,zhanw_coding设为评审人
CANN-robotCANN-robot成员
7月16日 将FelixTang7,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,zhangyuxiang0119,llimwang,gubaocheng,andong_hw,xuejinghui,zhanw_coding设为审查人
CANN-robotCANN-robot成员
7月16日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月16日 评论:

流水线任务触发成功
任务链接 [df51d58698da4066808bf46ffbfba6e2][流水线指导]

任务名称状态日志下载链接
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer ✅ SUCCESS >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
UT_Test_experimental ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ⚠️ WARNING >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-math ✅ SUCCESS

[2026-07-16 17:16:22]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月16日 评论:

流水线任务触发成功
任务链接 [b4f1540a4312453383f5cc4b2d1b8b55][流水线指导]

任务名称状态日志下载链接
codecheck ❌ FAILED >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_codestyle ⚠️ WARNING >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
likedislike
CANN-robotCANN-robot成员
7月16日 添加了label:api-check-failed
CANN-robotCANN-robot成员
7月16日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月16日 添加了label:ci-pipeline-failed
atomgit-bot
atomgit-bot7月16日进行代码检视2
math/mul/op_kernel_aicpu/mul_aicpu.cpp
已过期
@@ -0,0 +314,4 @@
314+ {DT_UINT8,
315+ {{DT_INT8, MulDiffTypeCompute<uint8_t, int8_t, int16_t>},
316+ {DT_INT16, MulDiffTypeCompute<uint8_t, int16_t, int16_t>},
317+ {DT_INT32, MulDiffTypeCompute<int8_t, int32_t, int32_t>},
atomgit-bot
atomgit-bot7月16日评论:

🟠 High Priority

GetMulDiffTypeCalls() 函数的分发表中,外层 key 为 DT_UINT8 的块内,DT_INT32 条目(第 317 行)错误地使用了 MulDiffTypeCompute<int8_t, int32_t, int32_t>,而该块其他所有条目均正确使用 uint8_t 作为 TIn1。

证据链:

  • 外层 key 为 DT_UINT8,表示第一个输入张量的数据类型是 uint8_t,因此模板参数 TIn1 应为 uint8_t
  • 同块其他所有条目(DT_INT8、DT_INT16、DT_INT64、DT_BFLOAT16 等)均正确使用 uint8_t 作为 TIn1,仅此一处有误。

建议:将 int8_t 改为 uint8_t,使模板参数与分发表外层 key DT_UINT8 一致。

改动建议
317
- {DT_INT32, MulDiffTypeCompute<int8_t, int32_t, int32_t>},
317
+ {DT_INT32, MulDiffTypeCompute<uint8_t, int32_t, int32_t>},
应用建议
likedislike
System
系统消息系统
7月16日 评论:
likedislike
atomgit-bot
atomgit-bot7月16日进行代码检视2
math/mul/examples/test_geir_mul.cpp
已过期
@@ -0,0 +113,4 @@
113+ return FAILED;
114+ }
115+ 
116+ uint32_t data_len = size * sizeof(T);
atomgit-bot
atomgit-bot7月16日评论:

🟡 Medium Priority

GenTensorData 函数中,第 116 行 uint32_t data_len = size * sizeof(T);size_t 类型的乘积赋值给 uint32_tsizesize_tsizeof(T) 也是 size_t,两者乘积在 64 位系统上可达远超 4 GB 的值。当张量元素数 × 元素大小超过 UINT32_MAX(约 4 GB)时,data_len 发生静默截断,随后传递给 Tensor 构造函数,导致 Tensor 仅拷贝截断后的字节数,与 p_data 实际分配的大小不一致。例如 FP16 类型下约 21.5 亿元素即可触发。虽然此示例文件当前仅使用 2×3 的小张量,但该模板函数可被复用,存在隐患。

触发条件:大 shape 乘积使 size * sizeof(T) > UINT32_MAX

建议:将 data_len 的类型从 uint32_t 改为 size_t,避免乘积截断。

改动建议
116
- uint32_t data_len = size * sizeof(T);
116
+ size_t data_len = size * sizeof(T);
应用建议
likedislike
System
系统消息系统
7月16日 评论:
likedislike
liu-weiliu-wei成员
7月16日 update merge request[project id: 7649531, iid: 4111, commit_id: f08f87c03772e32b7eda302e40a0bc8d7bcea165] virtual merging success
liu-weiliu-wei成员
7月16日 强制推送  1 个提交:bb422295-新增Mul算子AICPU实现
liu-weiliu-wei成员
7月16日 update merge request[project id: 7649531, iid: 4111, commit_id: 797df95f52de90a5e21c5e1c47fb4737ccefc1f9] virtual merging success
liu-wei
liu-wei成员
7月16日 评论:

compile

likedislike
liu-weiliu-wei成员
7月16日 update merge request[project id: 7649531, iid: 4111, commit_id: 1e37270482591fe9d684c9f782f1fe91b3ff6999] virtual merging success
CANN-robotCANN-robot成员
7月16日 删除了label:ci-pipeline-failedapi-check-failed
CANN-robotCANN-robot成员
7月16日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月16日 评论:

流水线任务触发成功
任务链接 [e30cd452ec764770813f0c42983b54bb][流水线指导]

任务名称状态日志下载链接
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer ✅ SUCCESS >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
UT_Test_experimental ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ⚠️ WARNING >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-math ✅ SUCCESS

[2026-07-16 20:15:18]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月16日 评论:

流水线任务触发成功
任务链接 [c5edb56f24a24c94b575d18c7ed106ca][流水线指导]

任务名称状态日志下载链接
codecheck ❌ FAILED >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_codestyle ⚠️ WARNING >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
likedislike
liu-weiliu-wei成员
7月16日 解决了最后一个问题
CANN-robotCANN-robot成员
7月16日 添加了label:api-check-failed
CANN-robotCANN-robot成员
7月16日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月16日 添加了label:ci-pipeline-failed
liu-weiliu-wei成员
7月16日 修改了pull request 的描述
liu-weiliu-wei成员
7月16日 修改了pull request 的描述
liu-wei
liu-wei成员
25 天前 评论:

compile

likedislike
liu-weiliu-wei成员
25 天前 update merge request[project id: 7649531, iid: 4111, commit_id: 83936f98d9e5db600a5bf9ea1981d63dea9193f0] virtual merging success
CANN-robotCANN-robot成员
25 天前 删除了label:ci-pipeline-failedapi-check-failed
CANN-robotCANN-robot成员
25 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
25 天前 评论:

流水线任务触发成功
任务链接 [efe9a1235c8f4ea3b15251dc0e0b886c][流水线指导]

任务名称状态日志下载链接
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer ✅ SUCCESS >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
UT_Test_experimental ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ⚠️ WARNING >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-math ✅ SUCCESS

[2026-07-21 15:36:44]    CI执行结束

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

流水线任务触发成功
任务链接 [2627a4c705654517bf8dbc7656b30111][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_codestyle ⚠️ WARNING >>>>>
codecheck_precommit ✅ SUCCESS >>>>>

[2026-07-21 15:21:52]    CI执行结束

likedislike
CANN-robotCANN-robot成员
25 天前 添加了label:api-check-failed
CANN-robotCANN-robot成员
25 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
25 天前 添加了label:ci-pipeline-passed
liu-weiliu-wei成员
25 天前 修改了pull request 的描述
RuiWang_成员
25 天前 评论:

/lgtm

likedislike
songkai111成员
25 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
25 天前 添加了label:lgtmapproved
CANN-robotCANN-robot成员
25 天前 关闭了关联的issue
CANN-robotCANN-robot成员
25 天前 合入了pull request