已开启
【代码侦探Challenge 04】完成 DivCustomTemplate 工程化算子实现(msopgen 工程 + 核函数 + Tiling + ACLNN 验证) #2143
【代码侦探Challenge 04】完成 DivCustomTemplate 工程化算子实现(msopgen 工程 + 核函数 + Tiling + ACLNN 验证) #2143
已开启
村雨雨雨创建于 3 天前
村雨雨雨
村雨雨雨
3 天前

题目

【代码侦探Challenge 04】工程化算子开发:基于算子原型文件(JSON)使用 msopgen 生成完整算子工程,完成 DivCustomTemplate(z = x / y)核函数与 Tiling 实现,编译安装为自定义算子包,并通过 aclnnDivCustomTemplate 接口完成调用与精度验证。

实现说明

目录结构及各文件职责

murasame/DivCustomTemplate/

  • div_custom_template.json # 算子原型定义文件(x/y 输入、z 输出,支持 float16/float32,ND 布局)
  • run.sh # 编译运行脚本:加载 CANN 环境 -> 生成工程(已存在则跳过) -> 编译 -> 安装 -> 编译测试 -> 运行测试
  • custom_op/ # msopgen 生成的算子工程(-c ai_core-ascend910b3,与本机 SoC Ascend910B3 一致)
    • CMakeLists.txt / CMakePresets.json / build.sh # 顶层构建脚本与 CMake 预设
    • framework/ # 框架适配插件(tf_plugin)
    • op_host/div_custom_template.cpp # host 侧:算子注册、InferShape/InferDataType、Tiling 计算
    • op_kernel/div_custom_template.cpp # kernel 侧:Ascend C 核函数实现(CopyIn -> Compute -> CopyOut)
    • op_kernel/div_custom_template_tiling.h # host/kernel 共享的 tiling 数据结构(元素总数 size)
  • test/ # 测试代码(main.cpp 调用 aclnnDivCustomTemplate;CMakeLists.txt 可选)

实现思路

  • Tiling(op_host):从输入 shape 计算元素总数写入 tiling 数据;核数自适应(每核最多 2048 元素,上限 8 核),workspace 为 0。
  • 核函数(op_kernel):采用标准 Ascend C 范式:
    1. GlobalTensor 管理 GM 侧 x/y/z;
    2. TQue<VECIN/VECOUT, 1> + TPipe::InitBuffer 管理 UB 队列;
    3. DataCopy 将 GM 数据按块搬入 UB;
    4. 使用矢量指令 Div(z, x, y, len) 完成逐元素除法 z = x / y;
    5. 结果经 DataCopy 搬回 GM z 输出。
      多核切分:按 GetBlockNum() 将 totalLength 均匀分配到各核,核内再按 2048 元素分块循环处理。

自验证结果

本机环境:CANN 9.1.0,SoC = Ascend910B3,输入 shape (8, 2048),float16,x = 1.0、y = 2.0。

bash run.sh 全流程输出:
result is:
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
test pass

编译安装与 ACLNN 接口调用均成功,精度校验通过(z = x / y = 1.0 / 2.0 = 0.5)。

likedislike
合并受阻
村雨雨雨村雨雨雨
3 天前 创建了 pull request,commit 6aa686b1
atomgit-bot
atomgit-bot
3 天前 评论:

变更摘要

该 PR 完成 DivCustomTemplate(z = x / y)自定义算子的工程化实现与验证:基于算子原型 JSON 使用 msopgen 生成算子工程,完成 Host 侧算子注册与 Tiling、Kernel 侧 Ascend C 核函数实现,编译安装为自定义算子包,并通过 aclnnDivCustomTemplate 接口完成 float16/float32 两种数据类型的调用与精度验证。核心改动覆盖算子原型定义(div_custom_template.json)、一键构建运行脚本(run.sh)、Host 侧 TilingFunc/InferShape/InferDataType、Kernel 侧模板化核函数 KernelDivCustomTemplate 及共享 tiling 结构 DivCustomTemplateTilingData、ACLNN 测试用例(test/main.cpp),以及 CMake 构建预设与 TensorFlow 框架适配插件。

主要改动

  • 算子原型与工程生成流程:新增 div_custom_template.json 定义 x/y 输入与 z 输出(float16/float32、ND 布局);run.sh 串联环境加载、msopgen 生成工程(已存在则跳过)、编译、安装到 ${HOME}/vendors/customize、编译并运行测试的完整流程,并 source 自定义算子环境脚本以支持运行时加载算子库。

  • Host 侧 Tiling 与算子注册custom_op/op_host/div_custom_template.cpp):TilingFuncint64_t 累乘 shape 各维得到元素总数写入 tiling->size,超 UINT32_MAX 时返回失败;按 dtype 记录数据类型(0=float16,1=float32);核数自适应(每核最多 2048 元素、上限 8 核,blockDim 至少为 1),workspace 置 0;InferShape 将输入 shape 透传给输出,InferDataType 透传输入数据类型;DivCustomTemplateOpDef 注册输入输出及 ascend910b AICore 配置。

  • Kernel 侧核函数实现custom_op/op_kernel/div_custom_template.cpp):模板类 KernelDivCustomTemplate<T>half/float)采用标准 Ascend C 范式,经 GlobalTensor 管理 GM 侧 x/y/z、TPipe::InitBuffer 分配 UB 队列(深度 1、单块 2048 元素),按 CopyIn → Compute(Div 矢量指令逐元素除法)→ CopyOut 循环处理;多核按 GetBlockNum()/GetBlockIdx() 均匀切分区间,核内按 TILE_SIZE 分块;入口 div_custom_template 依据 tilingData.dtype 选择对应模板实例。

  • 共享 Tiling 数据结构custom_op/op_kernel/div_custom_template_tiling.h):新增 Host/Kernel 共享的 DivCustomTemplateTilingData 结构,包含元素总数 size 与数据类型标识 dtype 两个字段。

  • ACLNN 验证测试test/main.cpp):通过 aclnnDivCustomTemplateGetWorkspaceSizeaclnnDivCustomTemplate 执行算子,覆盖 float16(aclFloat16)与 float32 两种用例,以 x=1.0、y=2.0 校验结果 z=0.5,并打印前 10 个结果判定 test pass,最后统一释放 tensor、workspace、stream 等资源。

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

代码审查

✅ 未发现问题

likedislike
CANN-robotCANN-robot成员
3 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
3 天前 评论:

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.
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: 王艳秋, yanyawen, yanhf, shaoyf, Carolina_yuan) ❌ (0/1)(You can also ask: 傅涛, yanhf, jxlang, shaoyf, li-shengxian3)

💡 Tip:

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

CLA Signature Pass

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

likedislike