Repository navigation
ufunc: avoid __array_wrap__ in favor of __array_function__ - #4708
Conversation
Note: NEP-18 has to be active
|
This is great @pentschev! Thanks for working on this 😄 Did this work for you locally with CuPy or are there more changes still needed? |
|
There is still one fix that needs to be done, but honestly, I don't understand what happens here. Do you have any idea of why is this happening? This was caused by the change of |
|
Ah, there's some other failures that I didn't see before, I'll check that as well. |
|
So the reason for dataframes to fail is that calling some np.ufuncs doesn't call __array_wrap__
(or __array_ufunc__ starting from numpy v.1.13.0), it should work as below
- da.ufunc(dd.Series) => dd.Series
- da.ufunc(pd.Series) => np.ndarray
- np.ufunc(dd.Series) => np.ndarray
- np.ufunc(pd.Series) => np.ndarrayAs of today, this is what we expect to happen. However, before trying to fix things, what is that we want these functions to return? To me, it seems strange that |
This seems odd to me as well. I would expect things to generally preserve type. I don't know the reasons for the Pandas policies though. It might be worth asking on the pandas issue tracker. |
From this comment I assume you would want to have both
For the moment, I'm not sure we need that information, I'm primarily concerned with what Dask should be doing. But of course, if that's something we would like to clarify either to potentially fix our tests or to mimic Pandas behavior, we can certainly do that. |
|
From this comment I assume you would want to have both da.ufunc(dd.Series)
and np.ufunc(dd.Series) returning dd.Series, am I correct.
Yes
…On Tue, Apr 23, 2019 at 3:26 PM Peter Andreas Entschev < ***@***.***> wrote:
This seems odd to me as well. I would expect things to generally preserve
type.
From this comment I assume you would want to have both da.ufunc(dd.Series)
and np.ufunc(dd.Series) returning dd.Series, am I correct.
I don't know the reasons for the Pandas policies though. It might be worth
asking on the pandas issue tracker
<https://github.com/pandas-dev/pandas/issues/new>.
For the moment, I'm not sure we need that information, I'm primarily
concerned with what Dask should be doing. But of course, if that's
something we would like to clarify either to potentially fix our tests or
to mimic Pandas behavior, we can certainly do that.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4708 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AACKZTBZBBCKTWNO4KP6CSDPR5WH3ANCNFSM4HGUPSHA>
.
|
* Use da.equal ufunc for isneginf/ispoinf * Keep on using __array_wrap__ for dataframe
|
Alright, so this new commit should fix the issues from before. Regarding IMHO, we can move forward with merging this PR when it succeeds and then create a new issue to handle that properly, and contribute |
|
@shoyer if you have a moment this PR could use your numpy expertise. I genuinely don't recall the intent behind |
|
There's some details in the NumPy docs, which might be helpful. Basically this precedes Does that help? If not, maybe Stephan can do a better job explaining it. 🙂 |
|
I think the recommendation these days is to avoid using |
|
Right. We are not really trying to use it. We are trying to work around it’s use in |
| not_equal = ufunc(np.not_equal) | ||
| equal = ufunc(np.equal) | ||
| isneginf = partial(equal, -np.inf) | ||
| isposinf = partial(equal, np.inf) |
There was a problem hiding this comment.
Can you please remind me why these are needed?
There was a problem hiding this comment.
Sure, these are not ufuncs:
In [1]: import numpy as np
In [2]: isinstance(np.equal, np.ufunc)
Out[2]: True
In [3]: isinstance(np.isposinf, np.ufunc)
Out[3]: FalseThere was a problem hiding this comment.
Right. Am more curious why these have been added to replace their wrap_elemwise equivalents below. Just to clarify am not objecting to doing this if it helps move things along. Merely interested in why it was needed is all.
There was a problem hiding this comment.
So when __array_function__ is enabled, those are dispatched through that. Libraries such as sparse don't implement those directly, so it gives us better compatibility in such cases.
There was a problem hiding this comment.
Sparse doesn't implement __array_function__ or doesn't implement isneginf and isposinf?
There was a problem hiding this comment.
Thanks for the info. Have raised issue ( pydata/sparse#252 ) on that point. Happy to carry this workaround for now.
There was a problem hiding this comment.
I don't know if we want to have that implemented in Sparse. I was only using Sparse as an example, CuPy also doesn't implement it.
There's another good point (at least on the NumPy side), it is faster compute that using np.equal, as shown here.
In my opinion, it should remain as it is in this PR, seems faster and more reliable when dealing with other NumPy-like libraries.
There was a problem hiding this comment.
Well I think one outcome of the __array_function__ protocol is it will put pressure on other libraries to implement a fully compatible NumPy API for their arrays. Currently for us today that is seeing isneginf and isposinf implemented in Sparse and CuPy. Maybe it will be something else later.
That to me is a sign that NumPy should improve their isneginf and isposinf implementations. Not that the API should be dispensed with completely. I don't think we can maintainably tweak our implementations based on this sort of thing because some function we special case could be suboptimal for some other library (either for performance or something totally different).
As noted above am not objecting to having this here for now. Have also already approved the PR. Though I think long term we shouldn't rely on this sort of thing. Perhaps in this case we put some pressure on NumPy to implement isneginf and isposinf in a more friendly way (as Marten showed in that comment).
There was a problem hiding this comment.
Actually I forgot Eric's earlier comment in that thread. We might be able to just alias NumPy's implementations as they already leverage the __array_ufunc__ protocol under the hood. Anyways this is work for another day (not this PR). 🙂
There was a problem hiding this comment.
You make a fair point @jakirkham. I was simply putting that given the current conditions, we may be better off with the new implementation (from a performance point of view), but you're probably right, the long-term solution is indeed to improve the NumPy implementations.
jakirkham
left a comment
There was a problem hiding this comment.
One question above. Otherwise LGTM (assuming CI passes). Planning on merging in 24hrs if no comments.
|
Thanks @pentschev! Also thanks everyone for the reviews. |
Solves #4496.
@jakirkham as per our conversation yesterday, I think this is a good solution for #4496, this way we push NEP-18 compatibility forward and avoid the need for changes on NumPy side, or any other libraries.
@mrocklin FYI
flake8 dask