Merged
float类型接口精度优化合入商分 #6028
float类型接口精度优化合入商分 #6028
Merged
xuyiyangAdd on Sep 15
xuyiyang
xuyiyangCollaborators
Sep 15

描述

hypotf,norm3df,norm4df,normf接口在某些场景下(有一个输入是flaot_max)时,返回值与cpu和竞品不一致。
故优化接口实现:
hypotf:从旧的"浮点除法归一化 + sqrtf"(r = b/a; a * sqrtf(1+r²))改为位运算指数缩放方案——用无符号整数比较取 max/min(避免浮点比较),通过 anchor_bits - exponent_bucket 构造 power-of-two 缩放因子将两项拉入 [0.5, 4.0),FMA 累加平方后调用 __internal_norm_sqrt 开方,最后用 exponent_bucket | restore_bit 位运算构造精确倒数还原量级,全程无浮点除法。
norm3df:同样从浮点除法归一化(a/m; b/m; c/m)改为指数缩放方案,用嵌套 fmaxf/fminf 分解出 max/middle/min 三项,缩放后 FMA 累加平方;新增快速路径——当 abs_sum == max_abs(仅一个非零项)时直接返回 abs_sum,跳过开方。
norm4df:与 norm3df 策略一致,改为三层嵌套 fminf/fmaxf 分解出 max + 三个剩余项,缩放后按从小到大顺序 FMA 累加四项平方;同样有 abs_sum > max_abs 快速路径判断。
normf:宏 __INTERNAL_NORMF 从旧的"4 项展开循环 + 浮点除法 a[i]/m 归一化"改为与 hypotf 相同的指数缩放方案——先线性扫描找 max_abs,位运算构造缩放因子,循环中跳过首个 scaled_max、用 FMA 累加其余项,最后一次性 FMA 加入 max,调用 __internal_norm_sqrt 开方并还原量级。

新增 __internal_norm_sqrt 内部函数(line 1033-1091)
所有 norm 系函数共用的 sqrt 实现:

快速路径:rsqrtf(x) + 一次 Newton-Raphson 修正 fmaf(r, y0/2, p),覆盖 [2^-101, FLT_MAX]
慢速路径:零、负数、NaN、Inf、极小正值分别按 IEEE-754 语义处理;极小正值先 ×2^64 再缩回 ×2^-32
前置块注释完整说明数学推导和路径选择逻辑

关联的Issue

测试

测试环境:Ascend NPU,CANN 9.2.0,--npu-arch=dav-3510
覆盖范围:零、非规格化数、有限值、无穷和 NaN
各接口行为与竞品一致

API 用例总数 NPU == GPU NPU != GPU 一致率
hypotf 200,060 200,060 0 100.00%
norm3df 200,000 200,000 0 100.00%
norm4df 200,000 200,000 0 100.00%
normf 200,000 200,000 0 100.00%
合计 800,060 800,060 0 100.00%

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyiyang's contribution.)
xuyiyangxuyiyangCollaborators
Sep 15 Created pull request, commit 72e412ad
atomgit-bot
atomgit-bot
Sep 15 Comments:

变更摘要

该 PR 针对 hypotf、norm3df、norm4df、normf 在某个输入为 FLT_MAX 等极端场景下返回值与 CPU 及竞品不一致的问题,将旧的"浮点除法归一化 + sqrtf"实现统一重构为"位运算指数缩放"方案:通过无符号整型比较取 max/min、用 anchor_bits - exponent_bucket 位运算构造 2 的幂缩放因子(全程无浮点除法),将各项拉入安全区间后经 FMA 累加平方,并新增共用的内部开方函数 __internal_norm_sqrt(rsqrtf + 一次 Newton-Raphson 修正),最后用 exponent_bucket | restore_bit 位运算精确还原量级;同时顺带重写了 ldexpf、抽取共享缩放常量并重构 __internal_ilogbf_finite_abs,ASCENDC_USE_LEGACY_PRECISION 分支中保留旧实现。改动集中在 impl/simt_api/math_functions_impl.h 一个文件。

主要改动

  • 新增共享位模式常量与 __internal_norm_sqrt 内部函数: 引入 __internal_norm_scale_anchor_bits、__internal_norm_exponent_bucket_mask、__internal_norm_restore_mantissa_bit、__internal_fp32_exponent_bias 等常量;新增的 __internal_norm_sqrt(line 1033-1091)提供快速路径(rsqrtf + fmaf(r, y0/2, p) Newton 修正,覆盖约 [2^-101, FLT_MAX])和慢速路径(零/负数/NaN/Inf/极小正值按 IEEE-754 语义处理,极小值先 ×2^64 再 ×2^-32 还原)。
  • hypotf 从浮点除法归一化改为指数缩放方案: 旧的 r = b/a; a * sqrtf(1+r²) 改为无符号整型比较取 max/min(避免浮点比较),通过 anchor_bits - exponent_bucket 构造缩放因子将两项拉入 [0.5, 4.0),FMA 累加平方后调用 __internal_norm_sqrt,再用 exponent_bucket | restore_bit 位运算还原量级,全程无浮点除法。
  • norm3df/norm4df 改为指数缩放 + 快速路径: 用嵌套 fmaxf/fminf 分解出 max/middle/min(四参版本分解 max + 三个剩余项),缩放后按序 FMA 累加平方;新增快速路径——当 abs_sum == max_abs(仅一个非零项)时直接返回 abs_sum 跳过开方;ASCENDC_USE_LEGACY_PRECISION 分支保留旧实现。
  • __INTERNAL_NORMF 宏(normf)重写: 从"4 项展开循环 + 浮点除法 a[i]/m 归一化"改为与 hypotf 一致的指数缩放方案——线性扫描找 max_abs、位运算构造缩放因子、循环中跳过首个 scaled_max 用 FMA 累加其余项、最后一次性 FMA 加入 max,再经 __internal_norm_sqrt 开方并还原量级。
  • ldexpf 重写及共享常量抽取: ldexpf 由循环缩放改为与编译 SIMT 路径一致的 q/r 拆分多级缩放(abs_exp >= 101),并补充 ±278 处溢出/下溢处理;rhypotf/rnorm3df/rnorm4df 用共享的 __internal_norm_scale_anchor_bits/__internal_norm_exponent_bucket_mask 替换局部常量,__internal_ilogbf_finite_abs 用命名常量替换 127/23/8388608 等魔法数字。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 15 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 15 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 15 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
impl/simt_api ✅ wulinyu, louyuxuan (2/2) ✅ wulinyu (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Sep 15 Set zengjuan,zhanghao_0689,suqwe,wulinyu,kong0808,dengjing_aoe,YeZZzzz1,bluesky901,yangbinhua,zhudi22,chenyiyuan,xun_zhuge,XuebinYang,sjtulxh,bianque,houyuyang,Lycheeeee,YANXI_ZHAO,chentianyu19,shi_nanxiang,wuyang_hw,jiangxinyu3,ascendhjh,hujiawen_kaven,wangxiaozhi,DragonBornHD84,louyuxuan,hyolee,yukangchen as approver
CANN-robotCANN-robotCollaborators
Sep 15 Set wulinyu,wangxiaozhi,louyuxuan as approver
gao_dafa
gao_dafaCollaborators
Sep 15 Comments:

xuyiyang0324,感谢您的提交,建议在完成PR充分自验证后,再触发线上构建任务。

若您的PR已具备检视条件,可根据下表的责任田,@对应的committer进行代码检视,我们会在第一时间对您的提交进行处理。

代码责任田:

module committer
adv_api chenyiyuan, wuyang_hw
basic_api chenyiyuan, wuyang_hw
c_api pangfd, haipingmao
cmake xun_zhuge, DragonBornHD84
docs munanhw, ai_xin
examples changxianyu, bianque
simt_api wulinyu, zhanghao_0689
scripts xun_zhuge, kevin365
tools YANXI_ZHAO, XuebinYang, sjtulxh

💡 如您不确定可以@谁,可以直接@我或者ascend-c sig的maintainer,xun_zhuge和ascendhjh,我们会为您分配合适的committer进行代码检视。

再次感谢您参与CANN的社区贡献。

likedislike
xuyiyang
xuyiyangCollaborators
Sep 15 Comments:

compile

likedislike
xuyiyangxuyiyangCollaborators
Sep 15 Pre merge succeeded (commit_id: 0ac5115a21d41452efad78964bc2dd831d89f90a)
CANN-robot
CANN-robotCollaborators
Sep 15 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Sep 15 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 15 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 15 Added label: ci-pipeline-passed
xuyiyangxuyiyangCollaborators
Sep 15 Change title to "float类型接口精度优化合入商分", originally titled "float类型接口修改合入商分".
louyuxuan
louyuxuanCollaborators
Sep 15 Comments:

/lgtm

likedislike
wulinyu
wulinyuCollaborators
Sep 15 Comments:

/approve
/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 15 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Sep 15 Merged pull request
xuyiyangxuyiyangCollaborators
Sep 15 Modify description of pull request
xuyiyangxuyiyangCollaborators
Sep 15 Modify description of pull request
xuyiyangxuyiyangCollaborators
Sep 15 Modify description of pull request
xuyiyangxuyiyangCollaborators
Sep 15 Modify description of pull request