已开启
Add DivCustom sample code for Challenge01 #1796
Add DivCustom sample code for Challenge01 #1796
已开启
西瓜WiFi创建于 7月31日
西瓜WiFi
西瓜WiFi
7月31日

当前 PR 是否有 AI 参与:

[ ] 否
[x] 是
__1. AI Agent 平台: codex
__2. AI 模型: Qwen3.5
__3. Prompt 上下文: 代码文件生成、PR 描述撰写

PR 功能描述 / 为什么需要这个合入**:

完成 CANN Summer Camps 2026 代码侦探活动 Challenge01-DivCustom 任务,实现逐元素除法算子 (DivCustom)。

主要工作:

  • 实现 div_custom.asc 算子,支持两个 float32 类型张量的逐元素除法运算
  • 采用 Tiling 策略,支持大数据分片处理
  • 使用 CopyIn → Compute → CopyOut 流水线优化性能

该 PR 关联的 issue

(格式为 fixes #<issue 号>, 或者 resolves #<issue 号>):
fixes #215

希望检视人员了解:

  • 这是代码侦探活动的参赛作品
  • 代码已按照 CANN 算子开发规范编写
  • 欢迎提出改进建议

改动类型 / Change Type

测试信息 / Testing

验证结果:

  • 输入:x = 1.2, y = 2.3

  • 输出:z = x / y ≈ 0.521739

  • 结果符合预期

检查清单 / Checklist

likedislike
合并受阻
西瓜WiFi西瓜WiFi
7月31日 创建了 pull request,commit 2fc7278c
atomgit-bot
atomgit-bot
7月31日 评论:

变更摘要

本 PR 为 CANN Summer Camps 2026 代码侦探活动 Challenge01-DivCustom 任务新增了逐元素除法算子 DivCustom 的示例实现。核心内容是在 div_custom.asc 文件中实现了一个基于 AscendC 框架的 kernel,支持两个 float32 张量的逐元素除法运算,采用 Tiling 策略分片处理,并通过 CopyIn → Compute → CopyOut 流水线优化数据搬运与计算效率。

主要改动

  • 新增 div_custom.asc 算子文件:新增完整的 AscendC kernel 源文件,包含 DivCustomTilingData 结构体、KernelDiv 类以及 div_custom kernel 入口函数,总计 178 行代码。
  • 实现 KernelDiv 类的 Tiling 分片机制:KernelDiv 通过 totalLength、tileNum、tileLength 三个参数将大数据按 tile 划分,Process 方法循环迭代每个 tile 依次执行 CopyIn、Compute、CopyOut。
  • 实现双缓冲流水线(Double Buffer):使用 TPipe 和 TQue 管理 UB 队列,输入队列 inQueueX、inQueueY 与输出队列 outQueueZ 均配置 BUFFER_NUM=2,通过 EnQue/DeQue/FreeTensor 机制实现数据搬运与计算的流水线并行。
  • 实现 kernel_div host 端封装函数:负责 device 内存的 aclrtMalloc 申请、host-to-device 数据拷贝、AscendC::LaunchKernel 启动 kernel、device-to-host 结果回拷以及 aclrtFree 释放,对外提供简洁的调用接口。
  • 内置验证逻辑:main 函数构造全为 1.2 的输入 x 和全为 2.3 的输入 y,计算输出后与 valueX/valueY 生成的 golden 数据通过 VerifyResult 进行对比验证。
likedislike
不准确?
atomgit-bot
atomgit-bot
7月31日 评论:

代码审查


审查总结

已审查文件:

  • 2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc — 发现 8 个问题

按优先级分布:

优先级 数量 说明
P0 1 VerifyResult 函数未定义,代码无法通过编译
P1 3 缺少 ACL 运行时初始化、aclrtMalloc 与 aclrtMemcpy 返回值未检查,运行时将崩溃
P2 2 tileLength 整数除法截断(非整除时丢数据)、kernel_div 忽略输入 vector 实际大小
P3 2 int32_t/uint32_t 符号不匹配警告、Compute() 未使用 progress 参数

整体风险评估:高风险。 P0 问题直接阻塞编译;P1 问题导致在真实 CANN 环境下运行时必然崩溃(未初始化 ACL + 不检查错误码);P2 问题在当前硬编码参数下恰好不触发,但代码作为 Sample 示例存在隐患,后续使用者修改参数时可能遭遇静默错误。建议优先修复 P0 和所有 P1 问题后再合入。

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

⛔ 需要修改

likedislike
不准确?
CANN-robotCANN-robot成员
7月31日 添加了label:cann-cla/no
CANN-robot
CANN-robot成员
7月31日 评论:

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/cann-outreach ❌ (0/2)(You can also ask: li-shengxian3, yanyawen, 田晓亮, yanhf, jxlang) ❌ (0/1)(You can also ask: jxlang, 傅涛, 田晓亮, shaoyf, 王艳秋)

💡 Tip:

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

CLA Signature Pass

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

likedislike
西瓜WiFi西瓜WiFi
7月31日 修改了pull request 的描述
atomgit-bot
atomgit-bot7月31日进行代码检视1
2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc
@@ -0,0 +145,4 @@
145+ 
146+ // 将 host 数据拷贝到 device
147+ aclrtMemcpy(deviceX, size, x.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
148+ aclrtMemcpy(deviceY, size, y.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
atomgit-bot
atomgit-bot7月31日评论:

🟠 High Priority

变更行:第 147-148 行和第 155 行,三次 aclrtMemcpy 调用均未检查返回值。

受影响的契约:aclrtMemcpy 返回 aclError。若拷贝失败(如设备内存不可访问、权限不足),数据不会正确传输,但代码继续执行后续逻辑。

失败模式:

  • H2D 拷贝(第 147-148 行)失败 → device 内存中为未初始化数据,kernel 计算结果为垃圾值。
  • D2H 拷贝(第 155 行)失败 → z vector 中为未初始化数据,VerifyResult 将误报失败或意外通过。

建议:对每个 aclrtMemcpy 调用检查返回值,失败时清理已分配资源并向上报告错误。

likedislike
不准确?
atomgit-bot
atomgit-bot7月31日进行代码检视1
2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc
@@ -0,0 +175,4 @@
175+ 
176+ std::vector<float> golden(totalLength, valueX / valueY);
177+ return VerifyResult(output, golden);
178+}
atomgit-bot
atomgit-bot7月31日评论:

🟠 High Priority

变更行:main() 函数(第 165-178 行)和 kernel_div() 函数(第 126-163 行)。

受影响的契约:CANN ACL 运行时要求在调用任何 aclrt* API(如 aclrtMalloc、aclrtMemcpy、LaunchKernel)之前必须先调用 aclInit(nullptr) 初始化,运行结束后调用 aclFinalize() 释放资源。

失败模式:运行时 aclrtMalloc 等调用将返回 ACL_ERROR_RT_NOT_INIT 错误码(但因返回值未被检查而静默失败),后续对空指针的 aclrtMemcpy 导致段错误崩溃。

建议:在 main() 开头添加 aclInit(nullptr),在 return 之前添加 aclFinalize(),并检查两者的返回值。

likedislike
不准确?
atomgit-bot
atomgit-bot7月31日进行代码检视1
2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc
@@ -0,0 +141,4 @@
141+ float *deviceZ = nullptr;
142+ aclrtMalloc((void **)&deviceX, size, ACL_MEM_MALLOC_HUGE_FIRST);
143+ aclrtMalloc((void **)&deviceY, size, ACL_MEM_MALLOC_HUGE_FIRST);
144+ aclrtMalloc((void **)&deviceZ, size, ACL_MEM_MALLOC_HUGE_FIRST);
atomgit-bot
atomgit-bot7月31日评论:

🟠 High Priority

变更行:第 142-144 行三次 aclrtMalloc 调用均未检查返回值。

受影响的契约:aclrtMalloc 返回 aclError 类型错误码,成功返回 ACL_SUCCESS。当 device 内存不足或运行时未初始化时分配会失败,返回的 deviceX/deviceY/deviceZ 保持为 nullptr。

失败模式:若任一 aclrtMalloc 失败,后续第 147-148 行的 aclrtMemcpy(deviceX, ...) 将对空指针执行 device 端内存拷贝,导致运行时段错误或不可预期的行为。此外,若 deviceX 分配成功但 deviceY 失败,代码不会释放已成功的 deviceX(资源泄漏在崩溃时次要)。

建议:对每个 aclrtMalloc 调用检查返回的 aclError,失败时执行清理并通过异常或错误码向上传播。

likedislike
不准确?
atomgit-bot
atomgit-bot7月31日进行代码检视1
2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc
@@ -0,0 +29,4 @@
29+ // 保存 Tiling 参数
30+ this->totalLength = totalLength;
31+ this->tileNum = tileNum;
32+ this->tileLength = totalLength / tileNum;
atomgit-bot
atomgit-bot7月31日评论:

🟡 Medium Priority

变更行:第 32 行 this->tileLength = totalLength / tileNum;。

受影响的契约:当 totalLength 不能被 tileNum 整除时,整数除法舍去余数,每个 tile 只处理 tileLength 个元素,循环执行 tileNum 次总计处理 tileNum * (totalLength / tileNum) 个元素,末尾 totalLength % tileNum 个元素永远不会被处理。

失败模式:在当前硬编码参数下(totalLength=16384, tileNum=8)恰好整除,不触发。但若任何人修改 totalLength 或 tileNum 使得不能整除(如 totalLength=16385),末尾元素将保持为设备内存中的未初始化数据,导致静默数据丢失和输出不正确。

建议:在 Process() 循环中动态计算每个 tile 的实际长度:若为最后一个 tile,使用 totalLength - progress * tileLength 作为实际处理长度。同时在 Init 中可对 tileNum ≤ totalLength 做断言。

likedislike
不准确?
atomgit-bot
atomgit-bot7月31日进行代码检视2
2026/CANN-Code-Detective/Challenge01-DivCustom/SampleUse/DivCustom/div_custom.asc
@@ -0,0 +174,4 @@
174+ std::vector<float> output = kernel_div(x, y);
175+ 
176+ std::vector<float> golden(totalLength, valueX / valueY);
177+ return VerifyResult(output, golden);
atomgit-bot
atomgit-bot7月31日评论:

🔴 Critical

变更行:第 177 行调用 VerifyResult(output, golden)。

受影响的契约:main() 函数依赖 VerifyResult 来验证输出精度。该函数在模板文件(Template/DivCustom/div_custom.asc 第 60-81 行)中已定义,但在 SampleUse 实现中被遗漏。

失败模式:编译时链接器报错 undefined reference to VerifyResult(...),代码无法通过编译。

建议:将模板中 VerifyResult 函数(第 60-81 行)完整拷贝到本文件第 164 行之前(kernel_div 函数之后、main 函数之前)。

likedislike
不准确?
西瓜WiFi
西瓜WiFi
7月31日 评论:

/check-cla

CANN-robot
CANN-robot成员
7月31日 评论:

CLA检查未通过,详情可参考这里

likedislike
西瓜WiFi西瓜WiFi
7月31日 预合并成功(commit_id: 003119d0d56396783daa10064cb2a0bfe2ad21ba)
此处折叠了8条事件消息 查看更多
CANN-robotCANN-robot成员
7月31日 添加了label:cann-cla/yes
shaoyf成员
8月3日 评论:

提交目录需要放置到个人目录内容,比如参考cp -r Template/DivCustom <你的用户名>/DivCustom复制个人目录,提交文件可以3个文件一起提交,PR描述需要补充验证截图

likedislike