Skip to content

Commit c5fc842

Browse files
committed
Relax sin/cos ULP test for float32 on non-FMA
Allow up to 3 ULP error for float32 sin/cos when native FMA is not available.
1 parent 919d0b8 commit c5fc842

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎numpy/_core/tests/test_umath.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
assert_raises,
3333
assert_raises_regex,
3434
)
35-
from numpy.testing._private.utils import _glibc_older_than
35+
from numpy.testing._private.utils import _glibc_older_than, _ufunc_has_fma
3636

3737
UFUNCS = [obj for obj in np._core.umath.__dict__.values()
3838
if isinstance(obj, np.ufunc)]
@@ -2097,12 +2097,15 @@ def test_sincos_float32(self):
20972097
# this is known to be problematic on old glibc, so skip it there
20982098
x_f32[index] = np.float32(10E+10 * np.random.rand(M))
20992099
x_f64 = np.float64(x_f32)
2100-
assert_array_max_ulp(np.sin(x_f32), np.float32(np.sin(x_f64)), maxulp=2)
2101-
assert_array_max_ulp(np.cos(x_f32), np.float32(np.cos(x_f64)), maxulp=2)
2100+
# numpy-simd-rounding gives ~3 ulp when fma is not available and kLowAccuracy
2101+
# been set in the precision mode
2102+
maxulp_f32 = 2 if _ufunc_has_fma(np.sin, np.float32) else 3
2103+
assert_array_max_ulp(np.sin(x_f32), np.float32(np.sin(x_f64)), maxulp=maxulp_f32)
2104+
assert_array_max_ulp(np.cos(x_f32), np.float32(np.cos(x_f64)), maxulp=maxulp_f32)
21022105
# test aliasing(issue #17761)
21032106
tx_f32 = x_f32.copy()
2104-
assert_array_max_ulp(np.sin(x_f32, out=x_f32), np.float32(np.sin(x_f64)), maxulp=2)
2105-
assert_array_max_ulp(np.cos(tx_f32, out=tx_f32), np.float32(np.cos(x_f64)), maxulp=2)
2107+
assert_array_max_ulp(np.sin(x_f32, out=x_f32), np.float32(np.sin(x_f64)), maxulp=maxulp_f32)
2108+
assert_array_max_ulp(np.cos(tx_f32, out=tx_f32), np.float32(np.cos(x_f64)), maxulp=maxulp_f32)
21062109

21072110
def test_strided_float32(self):
21082111
np.random.seed(42)

‎numpy/testing/_private/utils.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,16 @@ def check_free_memory(free_bytes):
27242724
return msg if mem_free < free_bytes else None
27252725

27262726

2727+
# avx2 implies fma
2728+
_fma_pattern = re.compile("fma|avx2|avx512|vsx|vx|asimd|vpfv4|rvv|lsx", re.IGNORECASE)
2729+
2730+
def _ufunc_has_fma(ufunc, dtype) -> bool:
2731+
"""Check if a ufunc has SIMD kernel with native FMA support for a given dtype."""
2732+
ufunc_name = ufunc.__name__
2733+
res = np.lib.introspect.opt_func_info(ufunc_name, signature=dtype.__name__)
2734+
dres = res[ufunc_name]
2735+
return all(bool(_fma_pattern.search(v["current"])) for v in dres.values())
2736+
27272737
def _parse_size(size_str):
27282738
"""Convert memory size strings ('12 GB' etc.) to float"""
27292739
suffixes = {'': 1, 'b': 1,

0 commit comments

Comments
 (0)