Pull Request已成功合入, 合并人@CANN-robot
(感谢 OpenGood_WeChat 的贡献)变更摘要
本 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两段式接口的调用验证。


/compile


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


🟠 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 的元素做写回保护。


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


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.


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.


闪捷信息科技有限公司
团队信息
成员
算子: 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与理论值一致)