已合并
[代码侦探(第八期)] 闪捷信息科技有限公司 - challenge08_tanhcustom #4217
[代码侦探(第八期)] 闪捷信息科技有限公司 - challenge08_tanhcustom #4217
已合并
OpenGood_WeChat创建于 21 天前
OpenGood_WeChat
21 天前

闪捷信息科技有限公司

团队信息

  • 提交者: gcw_LxlU8H9L (opengoodai@163.com)
  • 身份: 企业员工
  • 单位: 闪捷信息科技有限公司

成员

算子: challenge08_tanhcustom

基于Ascend C高精度Tanh指令实现Tanh激活算子(y=tanh(x))。op_host侧完成形状推导与Tiling函数:按输入dtype选择模板实例(float16/float32),总元素数按32核均匀切分(blockFactor=totalNum/32),核内再按ubFactor分片搬入UB;op_kernel侧采用CopyIn→Compute→CopyOut三段式流水线,BUFFER_NUM=2双缓冲使搬运与计算流水并行,核内调用高精度Tanh指令逐元素计算并写回GM。已在昇腾910B实机完成编译与精度验证(tanh(1)=0.761594与理论值一致)

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 OpenGood_WeChat 的贡献)
OpenGood_WeChatOpenGood_WeChat
21 天前 创建了 pull request,commit 3d9239f2
atomgit-bot
atomgit-bot
21 天前 评论:

变更摘要

本 PR 新增了基于 Ascend C 高精度 Tanh 指令的 Tanh 激活算子(y=tanh(x))完整实现,覆盖算子定义、形状推导、Tiling 切分、Kernel 计算以及 UT 测试与调用示例等环节。op_host 侧按输入 dtype 选择模板实例,并将总元素数按核数切分后再在核内分片;op_kernel 侧采用 CopyIn→Compute→CopyOut 三段式流水线与双缓冲,逐元素调用高精度 Tanh 指令写回 GM。

主要改动

  • 算子定义与形状推导:新增 Tanh 的 OpDef,输入 x、输出 y 均为 REQUIRED、DT_FLOAT/DT_FLOAT16、FORMAT_ND 且 AutoContiguous,并 AddConfig("ascend910b");tanh_infershape.cpp 通过 IMPL_OP_INFERSHAPE 将输出 shape 推导为与输入一致。
  • Tiling 切分逻辑:TanhTilingFunc 依据输入 dtype 设置 tilingKey(float16/float32 对应模板实例),逐维相乘得到 totalNum,按 BLOCK_DIM=32 计算 blockFactor,再按 TILE_NUM=4 得到 ubFactor,并 SetBlockDim(32)、将 workspace 设为 0。
  • Kernel 计算实现:tanh.cpp 入口通过 if constexpr 按模板参数 schMode 实例化 NsTanh::Tanh<half> 或 Tanh<float>;tanh.h 中以 BUFFER_NUM=2 双缓冲组织 TQue,按 GetBlockIdx() 偏移 GM 分片,循环执行 CopyIn/Compute/CopyOut 并调用 AscendC::Tanh 逐元素计算。
  • Tiling 数据结构与模板键:新增 TanhTilingData 结构(totalNum、blockFactor、ubFactor)以及 tanh_tiling_key.h 中的 ASCENDC_TPL_ARGS_DECL/ASCENDC_TPL_SEL 模板声明,供 host 与 kernel 共享。
  • UT 测试与精度比对:新增 tests/ut 下以旧 ABI 构建的 Google Test 基础设施、TilingContextFaker/InfershapeContextFaker 及用例执行器,test_tanh_tiling.cpp 校验 tilingKey/tilingData/workspace,test_tanh.cpp 通过 ICPU_RUN_KF 运行 kernel,并由 gen_data.py、compare_data.py 生成 golden 数据并按 dtype 阈值比对精度;此外提供 build.sh、examples/run.sh 与 test_aclnn_tanh.cpp 完成构建和 aclnnTanh 两段式接口的调用验证。
likedislike
不准确?
OpenGood_WeChat
OpenGood_WeChat
21 天前 评论:

/compile

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

代码审查

审查结论

  • [阻塞] 2026/CANN-Code-Detective/Challenge08-TanhCustom/PR_code/secsmart_OpenGoodCANNJu/code/challenge08_tanhcustom/op_host/tanh_tiling.cpp:37 fix: Tiling 多核切分未处理尾部元素,非 32 整除的输入结果错误
类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
不准确?
CANN-robotCANN-robot成员
21 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
21 天前 评论:

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

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

likedislike
atomgit-bot
atomgit-bot21 天前进行代码检视1
2026/CANN-Code-Detective/Challenge08-TanhCustom/PR_code/secsmart_OpenGoodCANNJu/code/challenge08_tanhcustom/op_host/tanh_tiling.cpp
@@ -0,0 +34,4 @@
34+ }
35+ // 多核切分:每核处理 blockFactor 个元素,核内按 TILE_NUM 分片搬入 UB(双缓冲)
36+ tiling->totalNum = totalLength;
37+ tiling->blockFactor = totalLength / BLOCK_DIM;
atomgit-bot
atomgit-bot21 天前评论:

🟠 High Priority

变更行:tanh_tiling.cpp:37 tiling->blockFactor = totalLength / BLOCK_DIM;(BLOCK_DIM=32,整除向下取整),且 context->SetBlockDim(32) 后没有任何 tail/余数处理。

行为链:blockFactor 为每核处理的元素数,kernel 侧 tanh.h:35/36 xGM.SetGlobalBuffer(ptr + blockLength*GetBlockIdx(), blockLength)、Process 循环 tileNum = blockLength/ubLength 只覆盖 [blockIdxblockFactor, blockIdxblockFactor+blockFactor) 区间。因此覆盖总元素数 = blockFactor*32 = floor(totalLength/32)*32 ≤ totalLength。

失败模式:只要 totalLength 不是 32 的整数倍(例如 {8,2048} 之外更常见的 shape,如 4097、{1,1000}、{3,5} 等),尾部 totalLength%32(最多 31 个)元素永远不会被任何核计算,输出 y 中对应位置保持 aclrtMalloc 出来的未初始化/脏数据,精度校验直接失败。PR 中用 16384(=32*512,恰好整除)验证,掩盖了该缺陷。尾块必须在 tiling 侧记录(如 tailNum/lastBlockFactor)并在 kernel 中按边界单独处理,或让 blockFactor 向上取整并在 kernel 侧做越界判断。

建议:在 tiling 侧对 blockFactor 做向上取整或记录尾块信息,并把尾部长度下发(新增 tail 字段或 lastBlockFactor),kernel 侧按实际边界处理尾块;建议 blockFactor = (totalLength + BLOCK_DIM - 1) / BLOCK_DIM,同时在 kernel 中对超出 totalNum 的元素做写回保护。

likedislike
不准确?
OpenGood_WeChat
OpenGood_WeChat
20 天前 评论:

已按审查意见修复:

  1. examples 示例:成功路径与三处失败早退路径均补充 aclReleaseOpExecutor(executor);
  2. 测试数据:输入改为固定种子 [-1,1) 随机数据(覆盖正负值两侧与零附近),并新增自检断言(禁止 golden==input、要求输入含正负号)。

/compile

likedislike
OpenGood_WeChatOpenGood_WeChat
20 天前 预合并成功(commit_id: 7ac54e73c5ac0f15309007b53ce4644caab1466d)
OpenGood_WeChatOpenGood_WeChat
20 天前 推送  1 个提交:bec32c9f-test(kernel): 测试数据改用含正负值的随机输入并增加 golden 自检
OpenGood_WeChatOpenGood_WeChat
20 天前 预合并成功(commit_id: abdc47dcb29b8f18da48fddf7817536fdb300d26)
CANN-robotCANN-robot成员
20 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
20 天前 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
20 天前 添加了label:cann-cla/yes
shaoyf成员
6 天前 评论:

/approve
/lgtm

likedislike
fulltower成员
6 天前 评论:

/approve
/lgtm

likedislike
CANN-robotCANN-robot成员
6 天前 添加了label:lgtmapproved
CANN-robot
CANN-robot成员
6 天前 评论:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
fulltower成员
6 天前 评论:

/check-pr

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

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
Ffulltower成员
6 天前 解决了最后一个问题
fulltower成员
6 天前 评论:

/check-pr

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