Merged
[Fix] foreach_reciprocal: 开启高精度除法编译选项,消除 fp32 倒数 1 ULP 误差 #13740
[Fix] foreach_reciprocal: 开启高精度除法编译选项,消除 fp32 倒数 1 ULP 误差 #13740
Merged
wenqi_wuAdd on 17 days ago
wenqi_wu
wenqi_wu
17 days ago

描述

针对社区 issue #31(torch._foreach_reciprocal fp32 精度失败)定位根因并修复:

根因:arch35 SIMT 内核以 1.0f / fval 计算倒数,但 foreach_reciprocal 的 kernel 编译单元未开启高精度编译选项(同族 foreach_addcdiv_scalar/foreach_addcdiv_scalar_list/foreach_atan/foreach_exp/foreach_expm1/foreach_pow_* 等 8 个算子均已开启)。默认 fast-math 下该除法被编译为近似倒数(rcp 近似 + 牛顿迭代),结果与 IEEE 正确舍入除法(CPU/GPU torch 的 div.rn)相差 ≤1 ULP:相对误差 ~2^-23(1.2e-7),但在输出 3.3e9 量级下 1 ULP = 256,触发 TTK cross_check 的 L1 rmse_ratio 判据放大(rmse_party=0 时分母被 small_value 地板夹住),表现为精度 FAIL。

修复:foreach_reciprocal/op_kernel/CMakeLists.txt 的 arch35 编译单元增加 OPTIONS "--cce-use-fast-math=false"(与既有同族范式一致;arch22/910B 路径走共享库 Div API,本次未改动)。

修复前后对照(Ascend950PR,device 5,ACLNN 直调 vs CPU IEEE 正确舍入,bit 级 ULP 对比,每例 70893 元素):

用例 修复前 修复后
issue 输入域 ±2^-15 maxULP=1,6510 处不匹配,最大绝对差 128 0 ULP / 0 mismatch
uniform ±1 maxULP=1,5947 处不匹配 0 ULP
uniform ±1e30 maxULP=1,6461 处不匹配 0 ULP
2^k 邻近对抗集 maxULP=1,20924 处不匹配 0 ULP
精确 2 的幂 0 ULP 0 ULP

关联的Issue

测试

  • 真机:上述 5 例 ACLNN 探针(修复后)全部 0 ULP / 0 mismatch / maxAbsDiff=0
  • UT:build.sh -u --ops=foreach_reciprocal(默认)与 --soc=ascend950 全绿(op_api/kernel/tiling)
  • 环境说明:--cce-use-fast-math=false 需 CANN 9.3.0+ 工具链(9.2.0-beta.1 不支持该选项;仓内已有 8 个算子使用该选项,CI 工具链满足)

文档更新

  • 无

类型标签

AI/Agent生成声明

  • AI辅助编写
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for wenqi_wu's contribution.)
wenqi_wuwenqi_wu
17 days ago Created pull request, commit 62d3c462
wenqi_wuwenqi_wu
17 days ago Associated issue: [新精度问题][torch._foreach_reciprocal]torch._foreach_reciprocal算子存在 新精度问题
atomgit-bot
atomgit-bot
17 days ago Comments:

变更摘要

该 PR 针对 torch._foreach_reciprocal 在 fp32 精度上的失败(社区 issue #31、#8219)做根因修复:foreach_reciprocal 的 arch35 SIMT 内核以 1.0f / fval 计算倒数,但该算子 kernel 编译单元未开启高精度编译选项,默认 fast-math 下除法被编译为近似倒数(rcp 近似 + 牛顿迭代),与 IEEE 正确舍入除法(CPU/GPU 的 div.rn)存在 ≤1 ULP 偏差,在 3.3e9 量级输出下 1 ULP 达 256,导致 cross_check 的 rmse_ratio 判据 FAIL。改动集中在 foreach_reciprocal/op_kernel/CMakeLists.txt 的 arch35 编译单元,通过新增 OPTIONS "--cce-use-fast-math=false" 与同族已开启该选项的算子(foreach_addcdiv_scalar、foreach_atan、foreach_exp、foreach_pow_* 等 8 个)保持一致,从而恢复 IEEE 正确舍入的除法语义。

主要改动

  • arch35 编译选项开启高精度除法: 在 foreach_reciprocal/op_kernel/CMakeLists.txt 的 arch35 编译单元新增 OPTIONS "--cce-use-fast-math=false",关闭 fast-math 近似路径,使倒数计算走高精度除法。
  • fp32 倒数精度对齐 IEEE 正确舍入: 取消 fast-math 后 1.0f / fval 不再使用 rcp 近似 + 牛顿迭代,消除了相对误差 ~2^-23 带来的 1 ULP 偏差;PR 给出的真机探针结果显示多组输入域(±2^-15、±1、±1e30、2^k 邻近对抗集)由 maxULP=1、数千处不匹配变为 0 ULP / 0 mismatch。
  • 改动范围限定在 arch35 路径: arch22/910B 路径仍使用共享库 Div API,本次未改动,即修复仅影响 arch35 编译单元。
  • 工具链依赖: --cce-use-fast-math=false 需 CANN 9.3.0+ 工具链支持(9.2.0-beta.1 不支持该选项),与仓内已有 8 个使用该选项的算子保持一致。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
17 days ago Comments:

代码审查

✅ 未发现问题


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
wenqi_wuwenqi_wu
17 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
17 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
17 days ago Comments:

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
foreach ✅ 汤平川, 王星 (2/2) ✅ 汤平川, 王星 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
wenqi_wu
wenqi_wu
17 days ago Comments:

compile

likedislike
wenqi_wuwenqi_wu
17 days ago Pre merge succeeded (commit_id: a3b99a7e588f2ca152bfa36e086cb16848e2e0b2)
CANN-robotCANN-robotCollaborators
17 days ago Set zengjuan,u010470851,chaotang233,kevin_huang1234,crystalhu,yangyang016,fanqirui,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,chenqi317,wang-xing001,liubo75,tangweiwei2,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
CANN-robotCANN-robotCollaborators
17 days ago Set u010470851,kevin_huang1234,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,wang-xing001,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
CANN-robot
CANN-robotCollaborators
17 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
17 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
17 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
17 days ago Added label: ci-pipeline-passed
yuning_chen
yuning_chenCollaborators
17 days ago Comments:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!
@liubo75 @u010470851 @zhajianqing123 @su-yueming @Chen_HaoWen @chenxingyu18 @pingchuantang @kevin_huang1234 @yourealize

likedislike
BartonFang
BartonFangCollaborators16 days agoStart code review1
foreach/foreach_reciprocal/op_kernel/CMakeLists.txt
@@ -18,4 +18,5 @@ add_kernel_sources(
1818 KERNEL_SRC arch35/foreach_reciprocal.cpp
1919 COMPUTE_UNITS ascend950 ascend350
2020 AUTO_SYNC false
21+ OPTIONS "--cce-use-fast-math=false"
BartonFang
BartonFang16 days agoComments:

【review】
问题分类:可测
位置:foreach/foreach_reciprocal/op_kernel/CMakeLists.txt:21,代码:OPTIONS "--cce-use-fast-math=false"
问题:本次修复的关键行为是 arch35 SIMT 倒数从相差 1 ULP 变为正确舍入,但 PR 仅增加编译选项,没有把能识别该差异的回归用例纳入测试。现有 kernel UT 的 CMake 只登记 ascend910B1,未执行此处修改的 arch35 编译单元;其 compare_data.py 对 float32 使用 rtol=1e-4。直接调用该比较器验证,golden=3300000000、output=3300000256(恰差 1 ULP、绝对差 256)仍返回 PASSED。现有 201 条 ATK 配置也都使用默认精度标准和 [-5,5] 随机值域,未固定 PR 描述中的对抗输入或断言 ULP 上限。
可能导致的结果:编译选项后续遗漏、被覆盖或未传到 SIMT 编译阶段时,现有 UT 无法识别本次修复所针对的数值回归;PR 描述中的手工探针结果也不能作为持续运行的回归门禁。
建议的修改方法:将已定位的输入按 float32 位模式固定成 arch35 ACLNN 或端到端测试,覆盖近零输入和 2 的幂邻域,并断言与正确舍入参考结果的 ULP 差为 0;把探针纳入可重复执行的测试入口,验证移除该选项时用例确实失败。

likedislike
BartonFangBartonFangCollaborators
16 days ago Resolved final issue
TangPC
TangPCCollaborators
16 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
16 days ago Added label: approved
wang-xing001
wang-xing001Collaborators
16 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
16 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
16 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
16 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
16 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike