You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dask array failing with numpy 1.17.0rc1 and np.min #5031
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 6, in aminTypeError: no implementation found for 'numpy.amin' on types that implement __array_function__: [<class 'dask.array.core.Array'>]
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.
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 ....
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.
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.
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:
ifnothasattr(module, func.__name__):
warning.warn('not implemented by dask')
# inspect args/kwargs to find/replace dask arrays with their# computed versionsreturnfunc(*computed_args, **computed_kwargs)
Don't think I like the decorator so much, that messes with stacklevel
That's not true:
defmatches_numpy_api(np_func):
defdecorator(my_func):
_numpy_to_my_funcs[np_func] =my_funcreturnmy_func# no wrapper created, so no arg mangling or stack additionreturndecorator
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 :)
The decorator giving explicit aliases is already the recommendation from
the NEP. But for a library like dask that already maintains a module with
an API matching NumPy, matching names is definitely much easier. And it
works already, probably 98% of the time.
On Sun, Jun 30, 2019 at 11:09 PM Eric Wieser ***@***.***> wrote:
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
return decorator
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#5031?email_source=notifications&email_token=AAJJFVV7XFACCQFVC42NHFDP5GNSZA5CNFSM4H4OODWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY5CYBA#issuecomment-507128836>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAJJFVTCU4IEGMMRJZTAT53P5GNSZANCNFSM4H4OODWA>
.
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?
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.
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?
Activity
jni commentedon Jul 1, 2019
xref scikit-image/scikit-image#3985
shoyer commentedon Jul 1, 2019
It looks like NumPy defines
numpy.minas an alias ofnumpy.amin.dask.array.minexists but notdask.array.amin, so dask's__array_function__method doesn't pick it up.rgommers commentedon Jul 1, 2019
There'll be many of these though.
And with 1.17.0 it doesn't work anymore:
Anyone who was relying on NumPy functions coercing to ndarray is now going to have to do that explicity
It's actually a quite significantly backwards compat break, without any deprecation warnings in the previous release ....
rgommers commentedon Jul 1, 2019
It may not be too late to change
TypeErrorwith a warning + old behavior.eric-wieser commentedon Jul 1, 2019
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
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 swappingnp.minandnp.aminto 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 simpleif/elifchain should probably instead create adictmapping from numpy functions to their functions, perhaps with a decorator like@matches_numpy_api(np.abs), to make it easy to do so.rgommers commentedon Jul 1, 2019
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.
shoyer commentedon Jul 1, 2019
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:eric-wieser commentedon Jul 1, 2019
That's not true:
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 :)
shoyer commentedon Jul 1, 2019
jni commentedon Jul 1, 2019
☝️ 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.
Well, yes and no? If
__array_function__was considered experimental, then it's somewhat up to NumPy not to break partial implementations of it?Add amin/amax aliases to min/max in dask array
pentschev commentedon Jul 1, 2019
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.
Add regression tests for daskgh-5031 (__array_function__ min/amin alias)
Deal with numpy aliases and functions and numpy functions not in dask
Deal with numpy aliases and functions and numpy functions not in dask
Change __array_function__ implementation for backwards compatibility (#…
rgommers commentedon Jul 5, 2019
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?
jrbourbeau commentedon Jul 5, 2019
+1 from me (ref #5063)
jakirkham commentedon Jul 5, 2019
We had previously discussed doing a bug fix soonish. Were there other fixes we needed?
jrbourbeau commentedon Jul 8, 2019
Closing as Dask 2.1.0 is out which includes the fix from #5043. Thanks all!