Merged
float类型数学接口性能优化 #6500
float类型数学接口性能优化 #6500
Merged
xuyiyangAdd on Sep 29
xuyiyang
xuyiyangCollaborators
Sep 29

描述

本 PR 针对 impl/simt_api/math_functions_impl.h 中的 float 类型数学接口进行性能优化,整体思路是调整 nextafterf、erff、erfcinvf、erfcxf、exp10f、expm1f、logbf、ilogbf 等函数的实现结构与分支顺序:将特殊值(NaN、±Inf、0)判断从函数入口移至计算之后,通过统一的结果变量与条件选择替代提前返回,使主计算路径更规整;同时改写 nextafterf 为基于整数位运算的方向步进实现,并将部分 exp2f/rsqrtf 调用替换为 __powf/__sqrtf 等价形式。

主要改动

  • nextafterf 改为位运算实现: 使用 __float_as_uint/__uint_as_float 提取符号与幅值位,通过 step_down 判断步进方向、zero_res 处理 ±0、并保留 x==y 与 NaN 传播逻辑,替代原先基于指针 reinterpret_cast 的多分支写法。
  • erff、erfcinvf、erfcxf 统一结果出口: 引入 res/result 变量,将各分支的 return 改为赋值后末尾统一返回;erff 与 erfcxf 中把 exp2f(x) 替换为 __powf(2.0f, x),erfcinvf 中把 rsqrtf(neg_log) 替换为 1.0f / __sqrtf(neg_log);erfcxf 的 x < -9.43f 提前返回也调整为计算结果后再覆盖为 ASCRT_INF_F。
  • exp10f、expm1f 特殊值处理后置: 移除入口处的 NaN/Inf(及 expm1f 的 x == 0)提前返回,改为在完成 __powf 计算后按 __isinf/__isnan/x == 0.0f 覆盖结果;expm1f 同时补充了 __isnan(x) || x == 0.0f 时返回 y = x 的收敛逻辑。
  • logbf、ilogbf 判断顺序调整: 先计算 __internal_ilogbf_finite_abs(ax) 作为默认结果,再依次用 ax == ASCRT_INF_F、ax == 0.0f、__isnan(x) 条件覆盖,替代原先依次提前返回的写法。
  • log1pf 特殊路径合并: 将原先嵌套在 x_bits >= fp32_positive_inf_bits 条件内的 inf 路径与 -0 处理,改为无条件计算 inf_path 并用 need_inf、need_neg_zero 两个布尔选择合并原外、内层条件。

关联的Issue

https://gitcode.com/cann/asc-devkit/issues/1782

测试

SIMT API 是否优化 LEGACY实现 高精度实现
float expm1f(float x) 是 --cce-use-fast-math=true: 29.65
--cce-use-fast-math=false: 51.39
--cce-use-fast-math=true:
优化前:152.72    优化后:114.70
--cce-use-fast-math=false:
优化前:146.30    优化后:115.45
float exp10f(float x) 是 --cce-use-fast-math=true: 49.96
--cce-use-fast-math=false: 49.85
--cce-use-fast-math=true:
优化前:129.32    优化后:98.53
--cce-use-fast-math=false:
优化前:129.40    优化后:98.96
int ilogbf(float x) 是 --cce-use-fast-math=true: 135.34
--cce-use-fast-math=false: 147.20
--cce-use-fast-math=true:
优化前:64.23    优化后:44.99
--cce-use-fast-math=false:
优化前:62.98    优化后:44.91
float log1pf(float x) 是 --cce-use-fast-math=true: 37
--cce-use-fast-math=false: 37
--cce-use-fast-math=true:
优化前:133    优化后:103
--cce-use-fast-math=false:
优化前:133    优化后:103
float atanhf(float x) 是 --cce-use-fast-math=true: 73.37
--cce-use-fast-math=false: 306.50
--cce-use-fast-math=true:
优化前:167.86    优化后:159.99
--cce-use-fast-math=false:
优化前:327.58    优化后:323.14
float logbf(float x) 是 --cce-use-fast-math=true: 96.48
--cce-use-fast-math=false: 98.53
--cce-use-fast-math=true:
优化前:97.21    优化后:53.27
--cce-use-fast-math=false:
优化前:93.32    优化后:56.16

文档更新

类型标签

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

变更摘要

本 PR 针对 impl/simt_api/math_functions_impl.h 中的 float 类型数学接口进行性能优化,整体思路是调整 nextafterf、erff、erfcinvf、erfcxf、exp10f、expm1f、logbf、ilogbf 等函数的实现结构与分支顺序:将特殊值(NaN、±Inf、0)判断从函数入口移至计算之后,通过统一的结果变量与条件选择替代提前返回,使主计算路径更规整;同时改写 nextafterf 为基于整数位运算的方向步进实现,并将部分 exp2f/rsqrtf 调用替换为 __powf/__sqrtf 等价形式。

主要改动

  • nextafterf 改为位运算实现: 使用 __float_as_uint/__uint_as_float 提取符号与幅值位,通过 step_down 判断步进方向、zero_res 处理 ±0、并保留 x==y 与 NaN 传播逻辑,替代原先基于指针 reinterpret_cast 的多分支写法。
  • erff、erfcinvf、erfcxf 统一结果出口: 引入 res/result 变量,将各分支的 return 改为赋值后末尾统一返回;erff 与 erfcxf 中把 exp2f(x) 替换为 __powf(2.0f, x),erfcinvf 中把 rsqrtf(neg_log) 替换为 1.0f / __sqrtf(neg_log);erfcxf 的 x < -9.43f 提前返回也调整为计算结果后再覆盖为 ASCRT_INF_F。
  • exp10f、expm1f 特殊值处理后置: 移除入口处的 NaN/Inf(及 expm1f 的 x == 0)提前返回,改为在完成 __powf 计算后按 __isinf/__isnan/x == 0.0f 覆盖结果;expm1f 同时补充了 __isnan(x) || x == 0.0f 时返回 y = x 的收敛逻辑。
  • logbf、ilogbf 判断顺序调整: 先计算 __internal_ilogbf_finite_abs(ax) 作为默认结果,再依次用 ax == ASCRT_INF_F、ax == 0.0f、__isnan(x) 条件覆盖,替代原先依次提前返回的写法。
  • log1pf 特殊路径合并: 将原先嵌套在 x_bits >= fp32_positive_inf_bits 条件内的 inf 路径与 -0 处理,改为无条件计算 inf_path 并用 need_inf、need_neg_zero 两个布尔选择合并原外、内层条件。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 29 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 29 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 29 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
61 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 9 Added label: ci-pipeline-passed
louyuxuanCollaborators
29 days ago Comments:

/lgtm

likedislike
wulinyuCollaborators
29 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
29 days ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
29 days ago Merged pull request