Skip to content

snrm2 overflow for large input floats #332

Closed

Description

@rth

DSNRM2 in BLIS overflows when applied to a an array [1e20] while it doesn't overflow in the Netlib BLAS (or OpenBLAS).

I run into this while trying to pass the scipy test suite with BLIS in scipy/scipy#10744, as mentionned in that issue,

Netlib BLAS does indeed apply a scale in SNRM2 to avoir the overflow, but I don't know it it's actually part of the BLAS spec.

There are also a number of other scipy test failures due to overflow I think in other functions (see the linked issue).

Activity

  1. changed the title [-]nrm2[/-] [+]nrm2 overflow for large float32[/+] on Aug 30, 2019
  2. rth commented on Aug 30, 2019

    @rth
    Author

    (Sorry submitted too early by accident, see edited description above. This issue is related to overflows when trying to pass the scipy test suite with BLIS).

  3. changed the title [-]nrm2 overflow for large float32[/-] [+]snrm2 overflow for large input floats[/+] on Aug 30, 2019
  4. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    Possible solutions:

    1. Cast f32 to f64 before multiplication and accumulate in f64 (looks like OpenBLAS does this maybe?).

    2. Scale:

      • By using isamax to find the largest element.
      • Using isamax over chunks that fit in cache and normalizing each chunk individually.
      • Changing the scale factor as you go along (like netlib), but of course this isn't (easily) vectorizable.

      In each case the scale factor should be a power of two to avoid explicit division.

  5. rth commented on Aug 30, 2019

    @rth
    Author

    Indeed. I imagine one would pay for it with performance in any case...

    Scale
    By using isamax to find the largest element.

    Maybe the scale could only to be applied if max_value**2*n_elements>dtype.max to avoid needless calculations? Or some other way of detecting the overflow.

  6. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    I think the extra calculation is probably not an issue (except division but that can be avoided). The biggest cost would be either the increased memory accesses (going through the array twice) or the loss of vectorization. My preference is method 1) for snrm2 and 2b) for dnrm2. @fgvanzee ?

  7. fgvanzee commented on Aug 30, 2019

    @fgvanzee
    Member

    This is kind of beyond my preferred level of expertise, and also hard to follow without more details / code samples. I nominate @devinamatthews to throw together a pull request.

  8. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    @fgvanzee can you give me a list of the files I need to edit? I'm not sure I could find them all.

  9. fgvanzee commented on Aug 30, 2019

    @fgvanzee
    Member

    ?nrm2(), defined in frame/compat/bla_nrm2.c, is currently based on bli_?normfv(), which is defined in frame/util/bli_util_tapi.c, but is based on a helper function that is defined starting on line 313 in frame/util/bli_util_unb_var1.c. (Be sure to mind the #ifdef FE_OVERFLOW cpp branch.) That helper function currently uses another helper function that implements sumsqv, which is defined starting at line 1118 in frame/util/bli_util_unb_var1.c.

  10. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    So the functions there can't have optimized implementations? Not that it really matters for nrm2 and similar but I just want to make sure this is the only place to change.

  11. rvdg commented on Aug 30, 2019

    @rvdg
    Collaborator
  12. fgvanzee commented on Aug 30, 2019

    @fgvanzee
    Member

    So the functions there can't have optimized implementations?

    As those operations are currently classified, correct. Let me know if it becomes and problem and I can think about reclassifying those functions as level-1v, at which time they will "first-class citizens" of the cntx_t.

  13. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    @rth by any chance were you testing on a Mac? I looked at the code and in fact it is already applying a scale factor, except that it tries to optimize by computing dot(x,x) first and checking for overflow exceptions. On MacOS, though, floating point exceptions are never generated.

  14. rth commented on Aug 30, 2019

    @rth
    Author

    by any chance were you testing on a Mac?

    No, I was testing on Linux (Gentoo), using blis 0.6.0 though -- maybe this was recently changed?

  15. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    It looks like the behavior has been the same since Nov. 2016. I pushed in a new commit (
    a264132 ) to #334 that unconditionally disables the use of dot. See if that works for you.

  16. rth commented on Aug 30, 2019

    @rth
    Author

    I haven't built BLIS from sources yet (and won't be able to look into it before next week).

    Are you saying that you are not able to reproduce the overflow on master with a unit test applying snrm2 on an array [1e20] of dtype float (32 bit)?

  17. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    @fgvanzee can you try and reproduce this and see if the fix in #334 works? @rth is this an array of length 1e20? Are the values drawn from some distribution?

  18. fgvanzee commented on Aug 30, 2019

    @fgvanzee
    Member

    @devinamatthews I think @rth means that the array has one element containing the value 1e20.

    Yeah, I can try to reproduce.

  19. rth commented on Aug 30, 2019

    @rth
    Author

    is this an array of length 1e20? Are the values drawn from some distribution?

    Sorry for the confusion. It's a 1D array with a single element equal to 1e20, so that when you square it, float32 would overflow.

  20. isuruf commented on Aug 30, 2019

    @isuruf
    Contributor

    I can reproduce this issue on linux and can confirm that #334 works

  21. devinamatthews commented on Aug 30, 2019

    @devinamatthews
    Member

    @fgvanzee saved by the bell. I think merging #334 is the ticket then, unless you want to clean it up/add a comment or something.

  22. fgvanzee commented on Aug 30, 2019

    @fgvanzee
    Member

    I also can confirm that snrm2_() on 1e20 doesn't work. (I actually get -nan.)

    I think merging #334 is the ticket then, unless you want to clean it up/add a comment or something.

    I'll take a look at it. Thanks for your quick work, everyone.

  23. rth commented on Sep 2, 2019

    @rth
    Author

    For future reference, here is a python snipped reproducing the original issue,

    import ctypes
    from ctypes import byref, c_int
    import numpy as np
    
    _blas = ctypes.cdll.LoadLibrary("lib/haswell/libblis.so")
    
    
    def snrm2(x):
    
        _blas.snrm2_.restype = ctypes.c_float
        return _blas.snrm2_(
            byref(c_int(len(x)))),
            x.ctypes.data_as(ctypes.c_void_p),
            byref(c_int(1)))
    
    
    x = np.array([2e-28], dtype=np.float32)
    # should not return 0.0
    out = snrm2(x)
    print(out)
    
    x = np.array([2e20], dtype=np.float32)
    # should not return inf
    out = snrm2(x)
    print(out)

    and I can confirm that #334 fixes it.

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