Skip to content

test_unary[log2/log10-...-complex64-enable-DAZ] fail on glibc 2.43: atan2f returns NaN under DAZ #120

Open

Description

@pearu

Summary

On a host with glibc 2.43 (Ubuntu 26.04), two test_accuracy.py cases fail on main (7dbbfd7). CI passes on the same commit (run 18603842629, ubuntu-latest):

FAILED functional_algorithms/tests/test_accuracy.py::test_unary[log2-functional_algorithms-cpu-complex64-enable-DAZ]
FAILED functional_algorithms/tests/test_accuracy.py::test_unary[log10-functional_algorithms-cpu-complex64-enable-DAZ]

The root cause appears to be glibc's atan2f (used by clogf, and therefore by numpy.log on complex64). With MXCSR.DAZ set, it returns NaN for a subnormal y and a tiny normal x, where the correct result is -0.0. functional_algorithms then gives different results for the same input depending on the code path:

  • through numpy.vectorize, which is the batched call in the test, the imaginary part comes out as -0;
  • as a scalar call, which is the test's per-point re-check, it comes out as nan.

The test's internal consistency check assert u == value (test_accuracy.py:316) then fails.

Reproducer

Through the test

pytest -q "functional_algorithms/tests/test_accuracy.py::test_unary[log2-functional_algorithms-cpu-complex64-enable-DAZ]"

Through functional_algorithms directly

import numpy, functional_algorithms as fa
z = numpy.complex64(complex(1.418447e-28, -1e-45))   # sample bsamples[12, 29] from the test
f = fa.utils.numpy_with_algorithms(device="cpu", dtype=numpy.complex64).log2
with fa.fpu.context(DAZ=True):
    print(f(numpy.array([z, z])))   # [-92.509674-0.j  -92.509674-0.j]   (batched path, used for ulp map)
    print(f(z))                      # (-92.509674+nanj)                   (scalar path, used for re-check)
# mpmath reference:                  (-92.509674-1.4252534e-17j)

Through plain numpy (no functional_algorithms code involved except the MXCSR context)

import numpy, functional_algorithms as fa
z = numpy.complex64(complex(1.418447e-28, -1e-45))
with fa.fpu.context(DAZ=True):
    print(numpy.log(z))                    # (-64.12282+nanj)   <-- NaN
    print(numpy.log(numpy.complex128(z)))  # (-64.12281996241157-0j)
    print(numpy.arctan2(numpy.float32(-1e-45), numpy.float32(1.418447e-28)))  # -0.0 (numpy's own atan2 is fine)

Through glibc directly (ctypes)

import ctypes, ctypes.util, functional_algorithms as fa
libm = ctypes.CDLL(ctypes.util.find_library("m"))
atan2f = libm.atan2f; atan2f.restype = ctypes.c_float; atan2f.argtypes = [ctypes.c_float, ctypes.c_float]
with fa.fpu.context(DAZ=True):
    print(atan2f(-1e-45, 1.418447e-28))   # nan   (glibc 2.43); expected -0.0
print(atan2f(-1e-45, 1.418447e-28))       # -9.879103255323902e-18 (DAZ off)

clogf(1.418447e-28 - 1e-45j) with DAZ on also returns (-64.12282, nan).

Trace

_______ test_unary[log2-functional_algorithms-cpu-complex64-enable-DAZ] ________

unary_func_name = 'log2', backend = 'algorithms', device = 'cpu'
dtype = <class 'numpy.complex64'>, fpu = 'enable-DAZ'
...
                    with register(**register_params):
                        r = func(samples[re, im])
                    e = reference(samples[re, im])
                    u = fa.utils.diff_ulp(r, e, flush_subnormals=not include_subnormal, equal_nan=True)
>                   assert u == value, (u, value, (re, im))
E                   AssertionError: (array(4294967296), np.int64(595817688), (np.int64(12), np.int64(29)))
E                   assert array(4294967296) == np.int64(595817688)

functional_algorithms/tests/test_accuracy.py:316: AssertionError

log10 fails at the same line: AssertionError: (array(4294967296), np.int64(580798991), (np.int64(12), np.int64(29))).

The batched ULP map that leads to this check (log2, complex64, enable-DAZ): max ULP difference 4294967296, and 736 samples exceed max_valid_ulp_count=3. They concentrate at |re z| or |im z| around 0, i.e. subnormal inputs; for example, log2(1e-45+1e-45j) gives -inf under DAZ.

What was ruled out

Varied Result
numpy 2.5.4 / 2.3.5 / 2.3.4 / 2.2.6 fails with all
mpmath 1.4.1 / 1.3.0 fails with both
numpy SIMD dispatch (NPY_DISABLE_CPU_FEATURES="AVX512_ICL X86_V4 X86_V3") still fails
clang-format, black not involved

The variable that differs from CI is the C library: glibc 2.43 here, 2.39 on ubuntu-24.04 runners. It is likely related to the CORE-MATH-derived float implementations that glibc adopted in 2.41+, but I have not verified that.

Environment

functional_algorithms main @ 7dbbfd7 (0.15.1.dev20+g7dbbfd7db), editable install
OS Ubuntu 26.04.1 LTS, Linux 7.0.0-38-generic x86_64
glibc 2.43 (Ubuntu GLIBC 2.43-2ubuntu2.4)
CPU AMD Ryzen 9 9950X (Zen 5, AVX-512)
Python 3.13.17 (conda-forge, GCC 15.3.0)
numpy 2.5.4 (pip wheel; baseline X86_V2, dispatch X86_V3/X86_V4/AVX512_ICL)
mpmath 1.4.1
pytest 9.1.1

The full pytest run otherwise gives 1350 passed, 1724 skipped (jax, scipy and pyfma are not installed, as in CI).

Related observations in the test (separate from the glibc behaviour)

  1. The test detects whether subnormals are flushed with square(sqrt(smallest_normal) * 0.5). That catches FTZ (flushed outputs) but not DAZ (flushed inputs), so under enable-DAZ subnormal samples are kept, the mpmath reference is not flushed, and every subnormal input shows up as a huge ULP error.
  2. When ULP errors exceed max_valid_ulp_count, the complex-plane branch only prints a table and returns; it never fails on accuracy. For example, test_unary[log-...-complex64-enable-DAZ] has 701 samples above the limit and still passes. The only assertion that can fail there is the scalar re-check consistency assert at line 316, which is what trips here.
  3. The batched path goes through numpy.vectorize (elements become Python complex, then numpy.complex64(...)), while the re-check calls the function on a numpy.complex64 scalar. Under DAZ the two paths give different results for the same sample, which the line-316 assert assumes cannot happen.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions