已开启
【代码侦探Challenge08】personal - challenge08_tanhcustom #4116
【代码侦探Challenge08】personal - challenge08_tanhcustom #4116
已开启
gcw_93rlw6ed创建于 19 天前
gcw_93rlw6ed
19 天前

personal

团队信息

  • 提交者: gcw_93rlw6ed (27271600@qq.com)
  • 身份: 学生
  • 单位: personal

成员

算子: challenge08_tanhcustom

Tiling策略:多核切分blockFactor按16对齐,小张量缩核;ubFactor按UB 24B/元素预算限幅。Kernel:CopyIn→Compute→CopyOut三段流水,fp32直接计算,fp16先Cast到fp32再计算,用Duplicate(1)+Div替代Reciprocal保证精度,双dtype(float/float16)分支覆盖。

likedislike
合并受阻
Ggcw_93rlw6ed
19 天前 创建了 pull request,commit 0f8a96a4
atomgit-bot
atomgit-bot
19 天前 评论:

变更摘要

本 PR 为 challenge08_tanhcustom 新增一个完整的 Tanh 自定义算子工程,覆盖算子定义、Tiling、Kernel 实现、aclnn 调用示例与单元测试。op_host 侧通过 Tanh 的 OpDef 注册 float/float16 双 dtype,tanh_infershape.cpp 做同形状推导,tanh_tiling.cpp 按多核切分 blockFactor(16 对齐、小张量缩核)并以 UB 24B/元素预算限幅 ubFactor,再按输入 dtype 设置 tilingKey。op_kernel 侧以 CopyIn→Compute→CopyOut 三段流水实现,fp32 直接计算、fp16 先 Cast 到 fp32 再计算,使用 Duplicate(1)+Div 替代 Reciprocal 保证精度,最终统一按 2/(1+e^{-2x})-1 形式求 tanh。配套提供构建/运行脚本、aclnnTanh 调用示例,以及 op_host tiling 与 op_kernel 的 UT 与精度比对脚本。

主要改动

  • 算子定义与形状推导: 新增 tanh_def.cpp 注册 Tanh 算子,输入 x/输出 y 均支持 ge::DT_FLOAT 与 ge::DT_FLOAT16、FORMAT_ND 并启用 AutoContiguous;tanh_infershape.cpp 通过 InferShapeTanh 将输入 shape 直接赋给输出 shape。
  • Tiling 策略与模板键: tanh_tiling.cpp 中新增 TanhTilingFunc,按 MIN_SPLIT_THRESHOLD 决定 usedCoreNum(小张量缩为 1 核),blockFactor 按 16 对齐,ubFactor 取 blockFactor 与 UB 预算上限的较小值,并根据输入 dtype 设置 tilingKey(fp16/bf16 与 fp32 分支);配套 tanh_tiling_data.h 定义 totalNum、blockFactor、ubFactor 字段,tanh_tiling_key.h 声明 schMode 模板参数。
  • Kernel 双 dtype 计算: tanh.h 中 Tanh<T> 类实现双缓冲 TQue 流水,Compute 对 fp32 直接计算、对 fp16 先 Cast 到 fp32,采用 Muls(-2)、Exp、Adds(1)、Duplicate(1)+Div、Muls(2)、Adds(-1) 组合,tanh.cpp 按 schMode 分别实例化 half 与 float 分支。
  • aclnn 调用示例: test_aclnn_tanh.cpp 演示构造输入/输出 tensor、调用 aclnnTanhGetWorkspaceSize 与 aclnnTanh 两段接口并申请 workspace,examples/run.sh 提供编译运行入口。
  • 构建与测试基础设施: 新增 build.sh(含 --make_clean、-u/--ut、-e/--example 选项)、tests/ut/run.sh,以及 BuildGoogleTest.cmake(以 _GLIBCXX_USE_CXX11_ABI=0 旧 ABI 源码构建 gtest),并提供 tiling/infershape 上下文 faker、test_tanh_tiling.cpp、test_tanh.cpp 与 gen_data.py、compare_data.py 精度比对脚本。
likedislike
不准确?
gcw_93rlw6ed
19 天前 评论:

/compile

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

代码审查

已完成最终核验:此前提交的 4 条已覆盖本轮可证实的全部问题,无新增可证实问题,也不再重复上报。

审查结论

变更文件逐项确认(12/12 全部已审查):

文件 结果
CMakeLists.txt(顶层) 无问题
build.sh 1 条(P3)
examples/CMakeLists.txt 无问题
examples/run.sh 无问题
examples/test_aclnn_tanh.cpp 无问题
op_host/CMakeLists.txt 无问题
op_host/tanh_def.cpp 无问题
op_host/tanh_infershape.cpp 无问题
op_host/tanh_tiling.cpp 1 条(P3)
op_kernel/CMakeLists.txt 无问题
op_kernel/tanh.cpp 无问题
op_kernel/tanh.h 2 条(P3)

已上报问题统计:P0=0,P1=0,P2=0,P3=4。

  1. build.sh:102-107 — set -e 下 ./run.sh 失败即退出,UT/示例失败处理分支不可达(死代码)。
  2. op_host/tanh_tiling.cpp:100 — DT_BF16 被路由到 fp16(half) kernel 分支,与 op_def 声明及 kernel 模板实例不一致。
  3. op_kernel/tanh.h:76 — 三段流水缺少 MTE3→MTE2 的 SetFlag/WaitFlag 同步(低置信,可能由 TQue 框架内置)。
  4. op_kernel/tanh.h:55-62 — kernel 侧未校验 ubFactor 为 0,存在除零与零字节 UB 分配风险。

总体风险判断:低。 该 PR 为 Tanh 自定义算子的完整新增实现(host tiling + device kernel + 构建脚本 + 调用示例),未改动任何既有代码,无回归或破坏性变更。核心数学逻辑 tanh(x) = 2/(1+e^{-2x}) - 1 在 FP32/FP16 两分支均正确,FP16 先 Cast 到 FP32 再回写,精度处理合理;多核切分(blockFactor 16 对齐、缩核)与 UB 限幅经边界推演未见越界或漏处理;tiling 侧对 totalNum/ubFactor 已有非正校验,workspace 为 0 常量,无整数溢出风险。4 条均为 P3 级可选改进,不阻塞合入。


已完成审查,以下是结论。

审查结论

按优先级统计

  • P0:0
  • P1:0
  • P2:0
  • P3:1

唯一发现

  • op_kernel/tanh_tiling_data.h(第 10-12 行):结构体使用 int64_t 但头文件无任何 #include,缺少 <cstdint>,头文件不自洽(可选改进,P3 / 置信度 0.4)。

各变更文件逐一核对结果

文件 结论
op_kernel/tanh_tiling_data.h 1 项 P3(缺 <cstdint>)
op_kernel/tanh_tiling_key.h no issues(ASCENDC_TPL_ARGS_DECL/ASCENDC_TPL_SEL 形态与 TANH_TPL_SCH_MODE_0/1 一致,宏值未与历史顺序冲突)
tests/ut/CMakeLists.txt no issues(有 cmake_minimum_required/project;无 file(GLOB);ASCEND_HOME_PATH 已校验)
tests/ut/cmake/BuildGoogleTest.cmake no issues(官方模板文件,按规则不提出修改建议)
tests/ut/common/any_value.h no issues(shared_ptr<void> 默认删除器正确释放 new 出的对象;VT_LIST_* 枚举值无重叠)
tests/ut/common/infershape_case_executor.cpp no issues(官方模板文件)
tests/ut/common/infershape_case_executor.h no issues(官方模板文件)
tests/ut/common/infershape_context_faker.cpp no issues(官方模板文件)
tests/ut/common/infershape_context_faker.h no issues(官方模板文件)
tests/ut/common/tiling_case_executor.cpp no issues(官方模板文件)
tests/ut/common/tiling_case_executor.h no issues(官方模板文件)

整体风险判断

本次变更以 UT 公共设施(tests/ut/common、tests/ut/cmake,属 CANNJudge 官方模板,参赛端不可编辑)与两个 kernel 侧头文件为主,未触及 host tiling 计算逻辑与 device kernel 主体。唯一上报项为 P3 级别的头文件自洽性改进,不构成功能性回归;整体风险低。

(补充说明:因探索预算已耗尽,tests/ut 下各文件被判定为官方模板文件是依据其 OPS_MATH_DEV_TESTS_UT_COMMON_* 包含守卫及 BuildGoogleTest.cmake 中 "Reference: ops-math/..." 的注释推断,故未对其提出修改建议;该判定若与仓库实际模板清单不符,相关候选问题(如 tiling_case_executor.cpp 中 socToUpper/socToArch 对缺失 key 使用 operator[] 会静默插入空串)可作为后续复核项。)


所有已确认的问题均已通过 report_finding 提交完毕,无新增问题可报。

审查结论

按优先级统计

  • P0:0 项
  • P1:0 项
  • P2:1 项
  • P3:6 项

逐文件审查确认(11/11)

  1. tests/ut/common/tiling_context_faker.cpp — no issues
  2. tests/ut/common/tiling_context_faker.h — no issues
  3. tests/ut/op_host/CMakeLists.txt — no issues
  4. tests/ut/op_host/test_op_host_main.cpp — 1 项(P3:注册 .so 失败被静默吞掉)
  5. tests/ut/op_host/test_tanh_tiling.cpp — 1 项(P2:expectTilingData 期望值与 tiling 实际结果不符,断言必然失败)
  6. tests/ut/op_kernel/CMakeLists.txt — no issues
  7. tests/ut/op_kernel/tanh_data/compare_data.py — no issues
  8. tests/ut/op_kernel/tanh_data/gen_data.py — 1 项(P3:未使用变量与恒假分支)
  9. tests/ut/op_kernel/tanh_tiling.h — no issues
  10. tests/ut/op_kernel/test_tanh.cpp — 3 项(P3:未引用函数、GmAlloc 未判空、ubFactor 绕过 UB 预算)
  11. tests/ut/run.sh — 1 项(P3:LD_LIBRARY_PATH 未加引号且空值产生尾随冒号)

整体风险判断

本次变更为纯新增的 UT 测试工程,未触及算子 host/kernel 生产代码,不存在算子正确性、整数溢出、流水线同步等高风险缺陷,整体风险低。最需关注的是第 5 项:expectTilingData = "0 1 0 " 与用例输入({8,2048}、64 核、UB 262144)下的真实 tiling 结果(16384/1024/1024)不符,会导致 op_host UT 稳定失败并使 run.sh 以非 0 退出,属必须修正的测试自洽性问题;其余 6 项为可安全清理或提升可诊断性的低优先改进。

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

💬 仅评论

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

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

💡 Tip:

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

CLA Signature Pass

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

likedislike
Ggcw_93rlw6ed
19 天前 修改标题为 “��������̽Challenge08��personal - challenge08_tanhcustom”,原标题为“[代码侦探(第八期)] personal - challenge08_tanhcustom”
Ggcw_93rlw6ed
19 天前 修改标题为 “【代码侦探Challenge08】personal - challenge08_tanhcustom”,原标题为“��������̽Challenge08��personal - challenge08_tanhcustom”
atomgit-bot
atomgit-bot19 天前进行代码检视1
2026/CANN-Code-Detective/Challenge08-TanhCustom/PR_code/personal_gcw_93rlw6ed_08/code/challenge08_tanhcustom/examples/test_aclnn_tanh.cpp
@@ -0,0 +164,4 @@
164+ 
165+ // 同步等待
166+ ret = aclrtSynchronizeStream(stream);
167+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
atomgit-bot
atomgit-bot19 天前评论:

🟡 Medium Priority

变更行:examples/test_aclnn_tanh.cpp 第 143-167 行(构造 yHostData(16384, 0) → 调用 aclnnTanh → aclrtSynchronizeStream)之后,直接进入第 169-181 行的资源释放,全程没有任何 aclrtMemcpy(DEVICE_TO_HOST) 回读、也没有与 tanh 期望值的比对。

受影响行为/契约:该示例是对自定义算子 aclnnTanh 的唯一端到端验证入口,其契约应当是"验证算子结果正确"。

失效模式:无论 kernel/tiling 计算是否错误(例如 fp16→fp32 Cast 分支错误、Div 精度问题、tiling 切分遗漏尾块),测试都会正常返回 0,CI/自测得到"通过"的假信号;算子的精度回归完全无法被发现。yHostData 变量被声明后从未使用,正是这一缺陷的静态证据。

likedislike
不准确?