已合并
【代码侦探Challenge05】完成逐元素乘法算子实现 #2448
【代码侦探Challenge05】完成逐元素乘法算子实现 #2448
已合并
小东创建于 9月2日
小东
小东
9月2日

题目

代码侦探 Challenge05 - MulCustom 逐元素乘法算子

实现内容

  • 基于 Ascend C 实现逐元素乘法算子 z = x * y
  • 使用 Double Buffer(BUFFER_NUM=2)完成 Tiling 切分与 CopyIn/Compute/CopyOut 流水线
  • 使用 AscendC::Mul 指令完成逐元素乘法运算
  • 数据通路:GM(x,y) --CopyIn--> UB --Compute(Mul)--> UB --CopyOut--> GM(z)

验证结果

  • 环境:昇腾 910B,CANN 9.1.0
  • 构造 x=1.2、y=2.3,输出与 golden 一致
  • 运行结果:[Success] Case accuracy is verification passed.

![验证截图]a8faff7e89b415433cccdfddcb2e6664.png

关联 Issue

https://gitcode.com/cann/cann-learning-hub/issues/700

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 小东 的贡献)
小东小东
9月2日 创建了 pull request,commit 6b0d8daf
小东小东
9月2日 关联了issue:代码侦探(第五期)|补全算子代码有奖,周周赢取定制礼品!
atomgit-bot
atomgit-bot
9月2日 评论:

变更摘要

本 PR 完成「代码侦探 Challenge05 - MulCustom」逐元素乘法算子的实现:基于 Ascend C 编写 mul_custom.asc 内核,通过 Tiling 切分与 Double Buffer(BUFFER_NUM=2)构建 CopyIn/Compute/CopyOut 流水线,使用 AscendC::Mul 指令计算 z = x * y,数据通路为 GM(x,y) → UB → Compute → UB → GM(z);Host 侧通过 ACL 接口完成内存分配、数据搬运、内核调用与结果校验,并在昇腾 910B(CANN 9.1.0)上以 x=1.2、y=2.3 验证输出与 golden 一致。此外,本 PR 的改动集还一并包含同作者 Challenge03 的 CoshCustom 双曲余弦算子(cosh_custom.asc 及对应 run.sh)。

主要改动

  • 新增 MulCustom 算子内核:mul_custom.asc 中定义 MulCustomTilingData(字段 totalLength、tileNum)与 KernelMul 类,Init 按核号 GetBlockIdx() 对 GM 地址做偏移切分并为输入输出队列申请双缓冲 UB 内存,Compute 使用 AscendC::Mul 完成逐元素乘法 z = x * y,Process 按 tileNum * BUFFER_NUM 次迭代执行 CopyIn/Compute/CopyOut 三级流水。
  • 新增 Host 侧调用与验证逻辑:kernel_mul 通过 ACL API(aclInit、aclrtMalloc、aclrtMemcpy、aclrtSynchronizeStream 等)完成 Host/Device 数据搬运,以 mul_custom<<<blockDim, nullptr, stream>>> 调用内核并释放资源;main 构造 x=1.2、y=2.3 的输入与 golden,经 VerifyResult 校验输出一致性。
  • 新增构建脚本:run.sh 激活 CANN 环境(ASCEND_TOOLKIT_HOME/set_env.sh)、执行 cmake/make 编译并运行 ./mul_test 完成端到端测试。
  • 附带 Challenge03 CoshCustom 实现:cosh_custom.asc 使用 AscendC::Exp、Muls、Add 计算 cosh(x) = (Exp(x) + Exp(-x)) / 2,通过 TBuf 管理两个中间结果,并提供对应的 run.sh 构建脚本。
likedislike
不准确?
atomgit-bot
atomgit-bot
9月2日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
9月2日 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
9月2日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
9月2日 评论:

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
repo-cann/cann-outreach ✅ shaoyf, 傅涛 (2/2) ✅ shaoyf, 傅涛 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
shaoyf成员
14 天前 评论:

当前PR涵盖2道题,建议每个PR只负责一道题

likedislike
shaoyf成员
14 天前 评论:

/approve
/lgtm

likedislike
fulltower成员
14 天前 评论:

/approve
/lgtm

likedislike
CANN-robotCANN-robot成员
14 天前 添加了label:approved
CANN-robotCANN-robot成员
14 天前 添加了label:lgtm
fulltower成员
14 天前 评论:

/check-pr

likedislike
CANN-robotCANN-robot成员
14 天前 合入了pull request