Skip to content

dask array failing with numpy 1.17.0rc1 and np.min #5031

Closed

Description

@jni

Using Python 3.7, dask 2.0, (from conda-forge) and NumPy 1.17.0rc1 (from pip):

import numpy as np
import dask.array as da

image = da.from_array(np.array([[0, 1], [1, 2]]), chunks=(1, 2))
image_min = int(np.min(image))

results in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 6, in amin
TypeError: no implementation found for 'numpy.amin' on types that implement __array_function__: [<class 'dask.array.core.Array'>]

Activity

  1. jni commented on Jul 1, 2019

    @jni
    ContributorAuthor
  2. shoyer commented on Jul 1, 2019

    @shoyer
    Member

    It looks like NumPy defines numpy.min as an alias of numpy.amin. dask.array.min exists but not dask.array.amin, so dask's __array_function__ method doesn't pick it up.

  3. rgommers commented on Jul 1, 2019

    @rgommers
    Contributor

    There'll be many of these though.

    In [1]: import dask.array as da                                                                
    
    In [2]: x = np.arange(5)                                                                       
    
    In [3]: d = da.from_array(x)                                                                   
    
    In [4]: np.__version__                                                                         
    Out[4]: '1.16.4'
    
    In [5]: np.median(d)                                                                           
    Out[5]: 2.0
    

    And with 1.17.0 it doesn't work anymore:

    In [1]: import dask.array as da                                                                
    
    In [2]: x = np.arange(5)                                                                       
    
    In [3]: d = da.from_array(x)                                                                   
    
    In [4]: np.__version__                                                                         
    Out[4]: '1.17.0.dev0+f0e64a1'
    
    In [5]: np.median(d)                                                                           
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-5-4bf961ea5541> in <module>
    ----> 1 np.median(d)
    
    <__array_function__ internals> in median(*args, **kwargs)
    
    TypeError: no implementation found for 'numpy.median' on types that implement __array_function__: [<class 'dask.array.core.Array'>]
    

    Anyone who was relying on NumPy functions coercing to ndarray is now going to have to do that explicity

    In [6]: np.median(np.asarray(d))                                                               
    Out[6]: 2.0
    

    It's actually a quite significantly backwards compat break, without any deprecation warnings in the previous release ....

  4. rgommers commented on Jul 1, 2019

    @rgommers
    Contributor

    It may not be too late to change TypeError with a warning + old behavior.

  5. eric-wieser commented on Jul 1, 2019

    @eric-wieser

    To more concretely summarize the problem here, since I was confused by the mailing list post - this is dask's __array_function__ implementation:

    dask/dask/array/core.py

    Lines 1208 to 1221 in a57b029

    def __array_function__(self, func, types, args, kwargs):
    import dask.array as module
    for submodule in func.__module__.split(".")[1:]:
    try:
    module = getattr(module, submodule)
    except AttributeError:
    return NotImplemented
    if not hasattr(module, func.__name__):
    return NotImplemented
    da_func = getattr(module, func.__name__)
    if da_func is func:
    return NotImplemented
    return da_func(*args, **kwargs)

    The problem is that the function __name__ is being used to map between numpy and dask functions, which makes dask sensitive to which function is the original and which is the alias. This, for instance, makes swapping np.min and np.amin to be aliases in the other direction a breaking change. If they mapped by value the problem wouldn't exist (and would arguably be a faster lookup, keying by a single pointer instead of a string).

    In my opinion, we should document that np.some_func.__name__ is an implementation detail, and not something that should be relied upon inside __array_ufunc__. Users scaling beyond a simple if / elif chain should probably instead create a dict mapping from numpy functions to their functions, perhaps with a decorator like @matches_numpy_api(np.abs), to make it easy to do so.

  6. rgommers commented on Jul 1, 2019

    @rgommers
    Contributor

    I like the dict mapping idea, that deal with aliases and name mismatches nicely. Don't think I like the decorator so much, that messes with stacklevel and isn't less typing than an explicit mapping.

  7. shoyer commented on Jul 1, 2019

    @shoyer
    Member

    It's actually a quite significantly backwards compat break, without any deprecation warnings in the previous release ....

    To be clear, this a backwards compatibility break within dask, not NumPy. But I agree, for libraries like dask that also implement __array__ it would be friendlier to define __array_function__ for unimplemented functions by coercing arguments to NumPy arrays and then calling the NumPy function again. Something like:

         if not hasattr(module, func.__name__): 
             warning.warn('not implemented by dask')
             # inspect args/kwargs to find/replace dask arrays with their
             # computed versions
             return func(*computed_args, **computed_kwargs)
  8. eric-wieser commented on Jul 1, 2019

    @eric-wieser

    Don't think I like the decorator so much, that messes with stacklevel

    That's not true:

    def matches_numpy_api(np_func):
        def decorator(my_func):
            _numpy_to_my_funcs[np_func] = my_func
            return my_func  # no wrapper created, so no arg mangling or stack addition
        return decorator

    isn't less typing than an explicit mapping

    It's not less typing, but it distributes the mapping alongside each function, rather than centralizing it. That means that it's easier to spot a missing map entry.

    The name is lousy, but it's up to dask to pick that :)

  9. shoyer commented on Jul 1, 2019

    @shoyer
    Member
  10. jni commented on Jul 1, 2019

    @jni
    ContributorAuthor

    But I agree, for libraries like dask that also implement __array__ it would be friendlier to define __array_function__ for unimplemented functions by coercing arguments to NumPy arrays and then calling the NumPy function again.

    ☝️ this! This enables gradual implementation of __array_function__, rather than having it be all-or-nothing. And as @shoyer pointed out in the scikit-image issue, this is already what many libraries rely on with dask arrays anyway: transparent coercion to NumPy arrays, which will work fine in cases where dask is used for multiprocessing rather than for out-of-core/distributed computations.

    Having said this, the min/amin problem is a big one and will hopefully be addressed by the dict/decorator approach.

    To be clear, this a backwards compatibility break within dask, not NumPy.

    Well, yes and no? If __array_function__ was considered experimental, then it's somewhat up to NumPy not to break partial implementations of it?

  11. added a commit that references this issue on Jul 1, 2019
    f135bf5
  12. pentschev commented on Jul 1, 2019

    @pentschev
    Member

    Well, yes and no? If __array_function__ was considered experimental, then it's somewhat up to NumPy not to break partial implementations of it?

    To me, experimental means that it isn't guaranteed no breaking changes will happen, since there may be issues with the original proposal or new features added that were deemed necessary after some experimental testing.

    Thanks @mrocklin for the fix.

  13. rgommers commented on Jul 5, 2019

    @rgommers
    Contributor

    Fix is in master now. That still leaves the issue of what to do with latest released dask + numpy 1.17.0. At the moment that's still going to break scikit-image (discussion in scikit-image/scikit-image#3985). Would a bugfix release be an option @mrocklin, so there's at least one compatible dask/numpy version combination that people could upgrade to?

  14. jrbourbeau commented on Jul 5, 2019

    @jrbourbeau
    Member

    Would a bugfix release be an option

    +1 from me (ref #5063)

  15. jakirkham commented on Jul 5, 2019

    @jakirkham
    Member

    We had previously discussed doing a bug fix soonish. Were there other fixes we needed?

  16. jrbourbeau commented on Jul 8, 2019

    @jrbourbeau
    Member

    Closing as Dask 2.1.0 is out which includes the fix from #5043. Thanks all!

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions