Skip to content

RuntimeWarning: divide by zero encountered in reciprocal for np.ones(3) ** -1 #18555

Closed

Description

@ogrisel

Reproducing code example:

import numpy as np

with np.errstate(invalid="raise"):
    np.ones(3) ** -1 

raises:

<ipython-input-43-02fd0a095546>:2: RuntimeWarning: divide by zero encountered in reciprocal
  np.ones(3) ** -1

but the result of the evaluation is still correct: array([1., 1., 1.])...

I also tried 1 / np.ones(3), np.ones(2) ** -1, np.ones(4) ** -1 which do not raise the warning.

I get it for any numpy array of 3 items with dtype np.float32 or np.float64 but not np.complex64 for instance.

NumPy/Python version information:

I got the problem with:

1.20.1 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:00:30) 
[Clang 11.0.1 ]

on macos/arm64.

I cannot get it from a linux/aarch64 docker environment running on the same machine numpy installed from PyPI.org with version:

1.20.1 3.9.1 (default, Dec 12 2020, 08:46:48) 
[GCC 8.3.0]

I cannot get it from a linux/aarch64 docker environment running on the same machine numpy installed from conda-forge with version:

1.20.1 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:26) 
[GCC 9.3.0]

So maybe this is a bug related to clang or macos/arm64? Could someone with an intel mac try to reproduce it, preferably using the numpy from conda-forge?

Activity

  1. seberg commented on Mar 5, 2021

    @seberg
    Member

    @ogrisel I think we might be running into #18005, i.e. clang has the terrible default of not using -ffp-exception-behavior=strict! There are already a few places in the code working around that I think. Try setting the flag if that fixes it. We really should force that flag, but I am not well versed with distutils or clang so was never sure where to add it when I looked at it once.

  2. ogrisel commented on Mar 5, 2021

    @ogrisel
    ContributorAuthor

    I built numpy master with:

    CFLAGS="-ffp-exception-behavior=strict" python setup.py install &> /tmp/numpy_build_log.txt
    

    and I still get the RuntimeWarning.

    Here is the build log:

    https://gist.github.com/ogrisel/d104c8cf1b1c13202c204f3cc0004e38

  3. seberg commented on Mar 5, 2021

    @seberg
    Member

    Hmm, thought it was worth a shot, since apparently without the flag clang feels free to do optimizations that can make the floating point exceptions all wrong.
    Still seems likely to be a bug with clang or glibc on macos/arm64, but I am not certain.

  4. ogrisel commented on Mar 7, 2021

    @ogrisel
    ContributorAuthor

    Not a big problem because it's just a warning and the result is correct. Still it's annoying in for libraries that checks that that warnings are not raised in a given context.

  5. isuruf commented on Mar 8, 2021

    @isuruf
    Contributor

    The fix should be the same as in #18571 for reciprocal

  6. erykoff commented on Mar 8, 2021

    @erykoff

    Interesting. Is this also a problem with the latest Xcode compiler or just the conda compiler (which is a version behind). I believe the problem in #17712 was not present with the latest Xcode compiler (clang 12) which presumably fixed that look-ahead exception bug.

  7. isuruf commented on Mar 8, 2021

    @isuruf
    Contributor

    Is this also a problem with the latest Xcode compiler

    Yes.

    which is a version behind

    Not really. conda compiler is actually ahead. The versioning schemes are different though.

  8. added this to the 1.20.2 release milestone on Mar 25, 2021
  9. isuruf commented on Apr 28, 2021

    @isuruf
    Contributor

    This is not the same issue as #18571. It turns out that this is due to branch prediction going wrong in the for loop and floating point exceptions being raised for the wrong branch.
    np.ones(120)[:120] ** -1 gives the warning, but np.ones(120)[:119] ** -1 doesn't.

  10. ogrisel commented on Apr 28, 2021

    @ogrisel
    ContributorAuthor

    Shall we report the issue upstream to LLVM (I assume) and just mark the failing TestUfuncGenericLoops::test_unary_PyUFunc_O_O_method_full[reciprocal] test (see #18143 (comment)) as XFAIL?

  11. isuruf commented on Apr 29, 2021

    @isuruf
    Contributor

    I don't have a small testcase to report this issue upstream. Besides I'm not even sure if it's a compiler issue or a hardware bug.

  12. charris commented on May 6, 2021

    @charris
    Member

    Going to push this off to 1.22.0 for tracking. It seems the problem is not directly connected to NumPy.

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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions