已开启
【代码侦探Challenge 04】完成 DivCustomTemplate 工程化算子开发 #2273
【代码侦探Challenge 04】完成 DivCustomTemplate 工程化算子开发 #2273
已开启
leozxw创建于 2 天前
leozxw
leozxw
2 天前

实现说明

基于 msopgen 生成的算子工程完成 DivCustomTemplate(z = x / y)工程化算子开发,通过 ACLNN 接口验证。

算子工程目录结构

custom_op/

CMakeLists.txt / CMakePresets.json / build.sh —— 顶层构建与打包脚本(msopgen 生成)
framework/ —— 框架适配插件(tf_plugin)
op_host/div_custom_template.cpp —— host 侧:算子原型注册、InferShape/InferDataType、Tiling 计算
op_kernel/div_custom_template.cpp —— kernel 侧:Ascend C 核函数实现
op_kernel/div_custom_template_tiling.h —— tiling 数据结构定义(host 与 kernel 共享)

实现思路

Tiling:16384 元素按 8 核均分(每核 2048),核内再切tileNum×BUFFER_NUM=16分片,每片128元素,Double Buffer 流水
Kernel:标准 CopyIn -> Compute -> CopyOut 三段流水,Div 指令完成逐元素除法

自验证

环境:CANN 9.0.0 / Ascend910B4,bash run.sh 输出 test pass
image.png

likedislike
合并受阻
leozxwleozxw
2 天前 创建了 pull request,commit dbbeaa6b
atomgit-bot
atomgit-bot
2 天前 评论:

变更摘要

本 PR 基于 msopgen 生成的算子工程,完成 DivCustomTemplate(z = x / y)工程化自定义算子开发,并通过 ACLNN 接口完成端到端验证(CANN 9.0.0 / Ascend910B4,bash run.sh 输出 test pass)。整套工程包含算子原型定义(div_custom_template.json)、host 侧算子注册与 Tiling 计算、kernel 侧 Ascend C 核函数实现(CopyIn -> Compute -> CopyOut 三段流水 + Double Buffer)、TensorFlow 框架适配插件,以及构建脚本与 ACLNN 测试用例。

主要改动

  • 算子原型与 host 侧实现op_host/div_custom_template.cpp 中注册 DivCustomTemplate 算子(输入 xy,输出 z,支持 float16/float、ND 格式),实现 InferShape(输出形状复制输入)、InferDataType(输出数据类型跟随输入)以及 TilingFunc(计算总元素数写入 size,设 tileNum = 8SetBlockDim(8) 按 8 核均分)。
  • Tiling 数据结构op_kernel/div_custom_template_tiling.h 定义 host 与 kernel 共享的 DivCustomTemplateTilingData 结构体(sizetileNum 两个字段),作为 host 侧 Tiling 与核函数间的数据传递载体。
  • kernel 核函数实现op_kernel/div_custom_template.cpp 中实现模板类 KernelOp<T>,按 tileNum × BUFFER_NUM = 16 分片(每片 128 元素),通过 inX/inY/outZ 队列完成 CopyIn -> Compute -> CopyOut 三段流水,Compute 阶段用 Div(z, x, y, tileLength) 指令执行逐元素除法,入口函数为 div_custom_template
  • 框架适配与原型定义framework/tf_plugin/tensorflow_div_custom_template_plugin.cc 通过 REGISTER_CUSTOM_OP("DivCustomTemplate") 将算子注册到 GE(TensorFlow 框架);div_custom_template.json 描述算子输入/输出原型(xyz,float16/float)。
  • 构建与测试验证CMakePresets.jsonbuild.sh 配置编译目标(ascend910b、Release、打包安装);run.sh 串联环境加载、算子工程生成、构建安装及测试编译执行;test/main.cpp 通过 aclnnDivCustomTemplateGetWorkspaceSize / aclnnDivCustomTemplate 接口对 {8, 2048} 的 float16 张量(x=1.0, y=2.0)执行除法并校验输出为 0.5。
likedislike
atomgit-bot
atomgit-bot
2 天前 评论:

代码审查

关闭总结

各文件审查结果

文件 审查结论
custom_op/CMakeLists.txt no issues(标准 msopgen 顶层构建脚本,set(package_name ${vendor_name}) 依赖 preset 的 cache 变量,build.sh 固定走 preset,无实际缺陷)
custom_op/CMakePresets.json no issues(标准配置;ASCEND_CANN_PACKAGE_PATH 为作者本机路径,属环境配置而非代码缺陷)
custom_op/build.sh no issues([ "$cmake_version" \< "3.19.0" ] 为 bash test 内合法转义比较;opts 未加引号是刻意做词拆分)
custom_op/framework/CMakeLists.txt no issues
custom_op/framework/tf_plugin/CMakeLists.txt no issues
custom_op/framework/tf_plugin/tensorflow_div_custom_template_plugin.cc no issues(标准 REGISTER_CUSTOM_OP 注册)
custom_op/op_host/CMakeLists.txt no issues(file(GLOB ...) 用于生成文件是 msopgen 标准模式)
custom_op/op_host/div_custom_template.cpp 发现 2 个问题(int32 溢出;缺少可整除性校验)
custom_op/op_kernel/CMakeLists.txt no issues
custom_op/op_kernel/div_custom_template.cpp 发现 1 个问题(整数除法截断静默丢尾块)
custom_op/op_kernel/div_custom_template_tiling.h no issues(结构体字段与 host/kernel 读写一致,uint32_t 收窄风险已在 host 侧溢出问题中覆盖)
div_custom_template.json no issues(key 拼写与 msopgen 规范一致)
run.sh 发现 1 个问题(ASCEND_TOOLKIT_HOME 可能未设置导致编译失败)
test/CMakeLists.txt no issues
test/main.cpp 发现 1 个问题(tensors/deviceAddrs 保存 nullptr 副本导致资源清理空转)

汇总

  • P0: 0 个
  • P1: 0 个
  • P2: 2 个(kernel 静默丢尾块、host tiling 32 位溢出)
  • P3: 2 个(测试资源泄漏、run.sh 环境变量脆弱性)

总体风险判断

该算子在 PR 自验证的精确测试形状(16384={8,2048},恰好被 8×2×8=128 整除)下可以正确运行,且 host/kernel 的 tiling 数据契约、双缓冲流水、ACLNN 接口调用链均一致。但作为对外暴露的通用逐元素除法算子,其 tiling 缺少可整除性校验与大尺寸溢出防护:任意非 128 整数倍的输入会静默丢失尾块(输出保持未初始化数据)、超大输入会因 int32_t 溢出导致设备端越界——这两项是 P2 级正确性/可靠性缺陷,建议在 TilingFunc 中补校验后合入。测试与脚本问题均为 P3 级,不影响当前用例通过,但会掩盖回归并降低可移植性。整体风险中等,合入前建议至少修复 P2 两项。

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

💬 仅评论

likedislike
CANN-robotCANN-robot成员
2 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
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

⚠️ 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: luzx66, shaoyf, jxlang, li-shengxian3, gcw_tgPdOLZT) ❌ (0/1)(You can also ask: li-shengxian3, luzx66, 王艳秋, 傅涛, jxlang)

💡 Tip:

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

CLA Signature Pass

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

likedislike
atomgit-bot
atomgit-bot2 天前进行代码检视1
2026/CANN-Code-Detective/Challenge04-DivCustomTemplate/ftrghujhgf/DivCustomTemplate/custom_op/op_kernel/div_custom_template.cpp
@@ -0,0 +10,4 @@
10+ {
11+ this->tileNum = tiling.tileNum;
12+ blockLength = tiling.size / GetBlockNum();
13+ tileLength = blockLength / BUFFER_NUM / tileNum;
atomgit-bot
atomgit-bot2 天前评论:

🟡 Medium Priority

likedislike