question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numerical differences in latest numba with NumPy 1.21 vs 1.22

See original GitHub issue
  • I have tried using the latest released version of Numba (most recent is visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem. i.e. it’s possible to run as ‘python bug.py’.

I observe numerical differences with latest numba when using NumPy 1.21 vs 1.22:

# Adapted from https://github.com/poliastro/poliastro/blob/8ac93be/tests/test_stumpff.py
from math import gamma

import numpy as np
from numpy.testing import assert_equal
from numba import njit


@njit
def stumpff_c2(psi):
    r"""Second Stumpff function.

    For positive arguments:

    .. math::

        c_2(\psi) = \frac{1 - \cos{\sqrt{\psi}}}{\psi}

    """
    eps = 1.0
    if psi > eps:
        res = (1 - np.cos(np.sqrt(psi))) / psi
    elif psi < -eps:
        res = (np.cosh(np.sqrt(-psi)) - 1) / (-psi)
    else:
        res = 1.0 / 2.0
        delta = (-psi) / gamma(2 + 2 + 1)
        k = 1
        while res + delta != res:
            res = res + delta
            k += 1
            delta = (-psi) ** k / gamma(2 * k + 2 + 1)

    return res


if __name__ == "__main__":
    psi = -3.0
    expected_c2 = (np.cosh((-psi) ** 0.5) - 1) / (-psi)

    assert_equal(stumpff_c2(psi), expected_c2)

With these dependencies, the assertion passes silently:

$ cat good_requirements.txt 
numba==0.55.2
numpy==1.21.6

But with these, the test fails:

$ cat bad_requirements.txt 
numba==0.55.2
numpy==1.22.4
$ python test_numba.py 
Traceback (most recent call last):
  File "/tmp/numba-weird/test_numba.py", line 40, in <module>
    assert_equal(stumpff_c2(psi), expected_c2)
  File "/tmp/numba-weird/.venv310/lib/python3.10/site-packages/numpy/testing/_private/utils.py", line 425, in assert_equal
    raise AssertionError(msg)
AssertionError: 
Items are not equal:
 ACTUAL: 0.6381924800586426
 DESIRED: 0.6381924800586427

Tested with Python 3.8 and 3.10. The numerical failure happens with all micro versions of NumPy 1.22 (1.22.0 to 1.22.4).

I know it’s generally unreasonable to test floating point equality 😓 but I’m reporting this because this particular test has been working fine for a few years until now, just in case it’s worth looking into it.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Jun 8, 2022

@astrojuanlu no problem, thanks for confirming. This is the bit of code Numba’s using to detect the issue/what NumPy is aware of: https://github.com/numba/numba/blob/65cbe1cab01cc186ce873a65d94b5096acbf07f3/numba/misc/numba_sysinfo.py#L387-L402 the _numpy_AVX512_SKX_detected key in the sys_info dictionary essentially maps to the “NumPy is probably using the lower precision SVML ufunc loops, need to disable dispatch to that” situation (if NumPy version>=1.22).

1reaction
gmarkallcommented, Jun 8, 2022

Could this change in NumPy 1.22 be the reason? https://github.com/numpy/numpy/pull/19478

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numba needs NumPy 1.20 or less for shapley import
I use Numpy==1.21.4 and Numba==0.53.0 , it works: ... the numba's dependency with numpy 1.22 have been resolved in the most recent version....
Read more >
Release notes — NumPy v1.24 Manual
Different C numeric types of the same size have unique names · argwhere now produces a consistent result on 0d arrays · Add...
Read more >
[ANN] Numba 0.56.0 RC1 - Announcements
Dear all, on behalf of the Numba crew I am happy to announce the ... of Numerical differences in latest numba with NumPy...
Read more >
Release Notes — Numba 0.56.4+0.g288a38bbd.dirty-py3.7 ...
Version 0.56. 2 (1 September, 2022) This is a bugfix release that supports NumPy 1.23 and fixes CUDA function caching. Pull-Requests:
Read more >
importerror: numba needs numpy 1.21 or less ( Get Solution )
The error statement is also self-explanatory. Numba only supports the lower version of numpy specifically 1.21. Although while creating this article the current ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found