Merged
float类型接口性能优化 #6676
float类型接口性能优化 #6676
Merged
xuyiyangAdd on 26 days ago
xuyiyang
xuyiyangCollaborators
26 days ago

描述

本 PR 针对 impl/simt_api/math_functions_impl.h 中的 SIMT 数学函数实现进行优化与调整,主要通过将分支逻辑改写为无分支/选择(SEL)形式、用浮点指数域运算替代数据相关的循环归一化,来减少 warp 内的分歧区域(divergence groups),同时更换部分底层例程(rsqrtf 改为 1.0f / __sqrtf、exp2f 改为 __powf)并调整特殊值处理与边界判定逻辑。

主要改动

  • __asc_uint_floor_log2 与 __asc_make_float_from_mantissa 无分支化: __asc_uint_floor_log2 由循环移位改为借助 __float_as_uint 读取偏置指数域相减计算 floor(log2);__asc_make_float_from_mantissa 改为同时计算 normal/subnormal/inf/zero 各候选位模式并用条件选择,移除了原先的多分支结构。
  • __asc_remquo_core 长除法重构: 将原来的逐位长除法改为先把除数尾数重缩放至 [2^23, 2^24)、以单次比较相减完成整数尾数步,再按指数差用选择式主体完成小数部分迭代。
  • rnorm3df / rhypotf 的结果与特殊值处理调整: rnorm3df 将 inf/nan/零平方和的提前返回改为统一计算后再对 result 覆写 ASCRT_NAN_F、0.0f、ASCRT_INF_F;两个函数均把 rsqrtf 替换为 1.0f / __sqrtf;rhypotf 的 min/max 选择改由比较布尔量统一选取。
  • __internal_euler_gamma_function 与 __internal_sincospif_core 精简: __internal_euler_gamma_function 去掉 x != 0.0f 的分支保护并对 x - nearbyintf(x) 直接化简,同时用 __internal_fp32_scale_2p24 缩放替代 frexpf/ldexpf 的子正规回退;__internal_sincospif_core 的整数判定改为基于 reduced == 0.0f 且象限为偶数。
  • __internal_stirling_and_euler_reflect 与 sinhf 多项式/指数重构: 前者无条件计算 cos/sin 两套象限多项式再按象限选择;sinhf 中 n 的计算由 truncf 改为 floorf,残差指数项由 exp2f 改为 __powf。

关联的Issue

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

测试

SIMT API 是否优化 LEGACY实现 高精度实现
float rcbrtf(float x) 是 --cce-use-fast-math=true: 266.00
--cce-use-fast-math=false: 2320.25
--cce-use-fast-math=true:
优化前:791.25    优化后:252.00
--cce-use-fast-math=false:
优化前:1259.38    优化后:677.12
float remainderf(float x, float y) 是 --cce-use-fast-math=true: 89.00
--cce-use-fast-math=false: 84.00
--cce-use-fast-math=true:
优化前:429.00    优化后:389.38
--cce-use-fast-math=false:
优化前:625.12    优化后:565.38
float remquof(float x, float y, int *quo) 是 --cce-use-fast-math=true: 429.38
--cce-use-fast-math=false: 432.62
--cce-use-fast-math=true:
优化前:692.88    优化后:397.88
--cce-use-fast-math=false:
优化前:618.62    优化后:406.50
float rhypotf(float x, float y) 是 --cce-use-fast-math=true: 284
--cce-use-fast-math=false: 1116.75
--cce-use-fast-math=true:
优化前:76    优化后:76
--cce-use-fast-math=false:
优化前:507    优化后:102
float rnorm3df(float a, float b, float c) 是 --cce-use-fast-math=true: 160
--cce-use-fast-math=false: 335
--cce-use-fast-math=true:
优化前:224    优化后:149
--cce-use-fast-math=false:
优化前:1378.12    优化后:725.12
void sincospif(float x, float *s, float *c) 是 --cce-use-fast-math=true: 1266
--cce-use-fast-math=false: 1128
--cce-use-fast-math=true:
优化前:192    优化后:90
--cce-use-fast-math=false:
优化前:199    优化后:90
float sinhf(float x 是 --cce-use-fast-math=true: 79
--cce-use-fast-math=false: 191
--cce-use-fast-math=true:
优化前:231.25    优化后:133
--cce-use-fast-math=false:
优化前:409.08    优化后:319
float tgammaf(float x) 是 --cce-use-fast-math=true: 1993
--cce-use-fast-math=false: 191
--cce-use-fast-math=true:
优化前:2106    优化后:1417.12
--cce-use-fast-math=false:
优化前:4102.62    优化后:3051

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyiyang's contribution.)
xuyiyangxuyiyangCollaborators
26 days ago Created pull request, commit 04267b09
atomgit-bot
atomgit-bot
26 days ago Comments:

变更摘要

本 PR 针对 impl/simt_api/math_functions_impl.h 中的 SIMT 数学函数实现进行优化与调整,主要通过将分支逻辑改写为无分支/选择(SEL)形式、用浮点指数域运算替代数据相关的循环归一化,来减少 warp 内的分歧区域(divergence groups),同时更换部分底层例程(rsqrtf 改为 1.0f / __sqrtf、exp2f 改为 __powf)并调整特殊值处理与边界判定逻辑。

主要改动

  • __asc_uint_floor_log2 与 __asc_make_float_from_mantissa 无分支化: __asc_uint_floor_log2 由循环移位改为借助 __float_as_uint 读取偏置指数域相减计算 floor(log2);__asc_make_float_from_mantissa 改为同时计算 normal/subnormal/inf/zero 各候选位模式并用条件选择,移除了原先的多分支结构。
  • __asc_remquo_core 长除法重构: 将原来的逐位长除法改为先把除数尾数重缩放至 [2^23, 2^24)、以单次比较相减完成整数尾数步,再按指数差用选择式主体完成小数部分迭代。
  • rnorm3df / rhypotf 的结果与特殊值处理调整: rnorm3df 将 inf/nan/零平方和的提前返回改为统一计算后再对 result 覆写 ASCRT_NAN_F、0.0f、ASCRT_INF_F;两个函数均把 rsqrtf 替换为 1.0f / __sqrtf;rhypotf 的 min/max 选择改由比较布尔量统一选取。
  • __internal_euler_gamma_function 与 __internal_sincospif_core 精简: __internal_euler_gamma_function 去掉 x != 0.0f 的分支保护并对 x - nearbyintf(x) 直接化简,同时用 __internal_fp32_scale_2p24 缩放替代 frexpf/ldexpf 的子正规回退;__internal_sincospif_core 的整数判定改为基于 reduced == 0.0f 且象限为偶数。
  • __internal_stirling_and_euler_reflect 与 sinhf 多项式/指数重构: 前者无条件计算 cos/sin 两套象限多项式再按象限选择;sinhf 中 n 的计算由 truncf 改为 floorf,残差指数项由 exp2f 改为 __powf。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
26 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
26 days ago Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
26 days ago Added label: cann-cla/yes
70 messages are collapsed here。 See more
wulinyuCollaborators
19 days ago Comments:

/lgtm

likedislike
wulinyuCollaborators
19 days ago Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
19 days ago Added label: approved
CANN-robotCANN-robotCollaborators
19 days ago Merged pull request