Skip to content

BLD: Do not use clang default to ignore floating point exceptions #18005

Description

@tomgoddard

Remarkably numpy.log() gives a runtime overflow warning on a float32 array containing values larger than 1e9. Numpy 1.19.4, macOS 10.15.7, MacBookPro15,3. Warning does not appear for float64 array even for much larger values (e.g. 1e35).

Reproducing code example:

import numpy as np
a = np.array([1e9], np.float32)
np.log(a)

Error message:

RuntimeWarning: overflow encountered in log

NumPy/Python version information:

1.19.4 3.8.6 (default, Nov 20 2020, 18:29:40)
[Clang 12.0.0 (clang-1200.0.32.27)]

Activity

  1. seberg commented on Dec 16, 2020

    @seberg
    Member

    @tomgoddard, I cannot reproduce this on linux. Since you are on 1.19.x, could you print:

    np.core._multiarray_umath.__cpu_features__
    

    Just so we have the information?

    The most likely cause will be that you have AVX512 instructions available and our AVX512 code has an error here. If it is in the AV512 path, @r-devulap could you have a look?

    Of course it could also be a clang bug in the glibc version...

  2. tomgoddard commented on Dec 16, 2020

    @tomgoddard
    Author
  3. tomgoddard commented on Dec 16, 2020

    @tomgoddard
    Author

    Numpy 1.19.4 was installed using brew on macOS 10.15.7.

  4. seiko2plus commented on Dec 16, 2020

    @seiko2plus
    Member

    could you run np.log(np.array([1e9]*8, np.float32)) and check if the overflow error still remains? newest versions of clang committing aggressive optimization to partial load operations lead to undefined the vector tail on AVX2.

  5. r-devulap commented on Dec 16, 2020

    @r-devulap
    Member

    I don't see this problem when using NumPy built with gcc 9.3 or clang 10.0. Makes me think it has to something to do with clang 12. Still figuring out how to install clang 12 on my Ubuntu to reproduce the error :/

  6. tomgoddard commented on Dec 16, 2020

    @tomgoddard
    Author
  7. tomgoddard commented on Dec 16, 2020

    @tomgoddard
    Author
  8. r-devulap commented on Dec 16, 2020

    @r-devulap
    Member

    confirmed that this behavior is only with clang (clang 10 and clang 12), and does not happen with gcc. It decides to set an overflow flag for a blend instruction (which is weird and incorrect) x = _mm256_blendv_ps(x, temp, denormal_mask); here:

    x = _mm256_blendv_ps(x, temp, denormal_mask);

  9. seberg commented on Dec 17, 2020

    @seberg
    Member

    @r-devulap thanks for digging this down, I am assuming it is a clang bug. Can we file it there? I am not sure there is a good way for us to work around it in NumPy to begin with...

  10. r-devulap commented on Dec 18, 2020

    @r-devulap
    Member

    Trying to narrow down to a simple C program to replicate the bug, unsuccessful so far ..

  11. r-devulap commented on Dec 19, 2020

    @r-devulap
    Member

    Found the culprit! clang optimizes out a blend instruction which was precisely meant to avoid this reported issue of an overflow warning. While the compiler was being clever, it is still a bug.

    PR #18030 has a simple work around for this issue. Let me know if that works.

    EDIT: didn't mean to include the code snippet.

  12. added this to the 1.19.5 release milestone on Dec 19, 2020
  13. 15 remaining items

  14. changed the title [-]log(1e9) gives RuntimeWarning: overflow encountered in log[/-] [+]BLD: Do not use clang default to ignore floating point exceptions[/+] on Dec 21, 2020
  15. seberg commented on Dec 21, 2020

    @seberg
    Member

    Reopened and changed title. We should probably have a quick look around for clang workaround, but I don't think we need to backport getting rid of those.

  16. r-devulap commented on Dec 21, 2020

    @r-devulap
    Member

    This was the other time clang give me an incorrect FP flag: #13586 (comment) and the associated PR #13623

  17. seberg commented on Jan 25, 2021

    @seberg
    Member

    Hmmm, I thought I would look into this. But I am honestly not sure where we should be adding this flag, or if we actually should (additionally) even prod Python itself to add this flag. Am I right to think that most clang specific flags currently are inherited from Python itself?

  18. charris commented on Jan 29, 2021

    @charris
    Member

    What is the status of this?

  19. seberg commented on Jan 29, 2021

    @seberg
    Member

    I think this is important, but I am not very familiar with distutils, and am not sure where this flag should be inserted correctly. (It probably is also correct for SciPy, but do we want to set it by default for scipy as well?).
    I am happy to check for the related code cleanups, but not too fond of digging through distutils to find the right place.

    It shouldn't matter too much to push it off to 1.21 all known issues due to this are fixed after all. While the flag change is something we could do, the (probably) related code cleanup we do not want to backport anyway probably.

  20. seberg commented on Mar 18, 2021

    @seberg
    Member

    @rgommers I somewhat assume you just know where to put this. Can you point where we would add a clang specific compiler flag to distutils (maybe NumPy specific, although this flag is really also necessary for scipy, I bet).

  21. rgommers commented on Mar 18, 2021

    @rgommers
    Member

    In CCompiler_customize: https://github.com/numpy/numpy/blob/main/numpy/distutils/ccompiler.py#L366

    maybe NumPy specific, although this flag is really also necessary for scipy, I bet

    It's hard to make it NumPy-specific, and I think other libraries should get this flag too, so I'd say just add it unconditionally.

  22. seiko2plus commented on Mar 18, 2021

    @seiko2plus
    Member

    @seberb,

    Can you point where we would add a clang specific compiler flag to distutils?

    for a quickfix, we can link the flag with one of baseline features flags, etc SSE on x86.`

    on_x86 = self.cc_on_x86 or self.cc_on_x64
    is_unix = self.cc_is_gcc or self.cc_is_clang
    if on_x86 and is_unix: return dict(
    SSE = dict(flags="-msse"),
    SSE2 = dict(flags="-msse2"),

  23. added
    sprintableIssue fits the time-frame and setting of a sprint
    on May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions