Skip to content

ufunc: avoid __array_wrap__ in favor of __array_function__ - #4708

Merged
jakirkham merged 8 commits into
dask:masterfrom
pentschev:disable-wrap-on-nep18
May 14, 2019
Merged

jakirkham merged 8 commits into
dask:masterfrom
pentschev:disable-wrap-on-nep18

Conversation

@pentschev

Copy link
Copy Markdown
Member

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

  • Tests added / passed
  • Passes flake8 dask

@pentschev pentschev changed the title Disable wrap on nep18 ufunc: avoid __array_wrap__ in favor of __array_function__ Apr 17, 2019
@jakirkham

Copy link
Copy Markdown
Member

This is great @pentschev! Thanks for working on this 😄

Did this work for you locally with CuPy or are there more changes still needed?

@pentschev

Copy link
Copy Markdown
Member Author

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 isneginf, but I don't understand what's going on with that particular check.

@pentschev

Copy link
Copy Markdown
Member Author

Ah, there's some other failures that I didn't see before, I'll check that as well.

@pentschev

Copy link
Copy Markdown
Member Author

So the reason for dataframes to fail is that calling da.ufunc(dd.Series) and np.ufunc(dd.Series) both return np.ndarray after the changes in the PR, where previously they would return dd.Series and np.ndarray, respectively. Here's the description from test_ufunc_array_wrap verbatim:

        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.ndarray

As 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 da.ufunc(dd.Series) and np.ufunc(dd.Series) are expected to have different types, while np.ndarray is always expected for pd.Series. Could you clarify @jakirkham @mrocklin ?

@mrocklin

Copy link
Copy Markdown
Member

To me, it seems strange that da.ufunc(dd.Series) and np.ufunc(dd.Series) are expected to have different types, while np.ndarray is always expected for pd.Series

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.

@pentschev

Copy link
Copy Markdown
Member Author

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.

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.

@mrocklin

mrocklin commented Apr 24, 2019 via email

Copy link
Copy Markdown
Member

* Use da.equal ufunc for isneginf/ispoinf
* Keep on using __array_wrap__ for dataframe
@pentschev

Copy link
Copy Markdown
Member Author

Alright, so this new commit should fix the issues from before.

Regarding dask.dataframe and the concerns raised by @mrocklin, fixing it on the way suggested is not trivial, I've attempted to do that, without success. If I understand the dependencies correctly, to be capable of using __array_function__ for dask.dataframe and avoid __array_wrap__ (like we're doing now for dask.array), we'll need to add __array_function__ support to Pandas before changing Dask. This is definitely something we can do, but I think the present solution provides a solution without a big compromise.

IMHO, we can move forward with merging this PR when it succeeds and then create a new issue to handle that properly, and contribute __array_function__ to Pandas as well. Any objections @mrocklin @jakirkham?

@mrocklin

mrocklin commented May 4, 2019

Copy link
Copy Markdown
Member

@shoyer if you have a moment this PR could use your numpy expertise. I genuinely don't recall the intent behind __array_wrap__

@jakirkham

Copy link
Copy Markdown
Member

There's some details in the NumPy docs, which might be helpful. Basically this precedes __array_ufunc__ and is still used on some functions that are not proper ufuncs, but are ufunc-like. It can be used to recast the output of a NumPy function into a different type of array (e.g. a masked array).

Does that help? If not, maybe Stephan can do a better job explaining it. 🙂

@shoyer

shoyer commented May 8, 2019

Copy link
Copy Markdown
Member

I think the recommendation these days is to avoid using __array_wrap__. There's really no good use for it now that __array_ufunc__ exists.

@jakirkham

Copy link
Copy Markdown
Member

Right. We are not really trying to use it. We are trying to work around it’s use in ufunc-like functions from NumPy. The latter are causing us issues when applied to other array types not from NumPy (e.g. CuPy). Does that make sense?

Comment thread dask/array/ufunc.py Resolved
Comment thread dask/array/ufunc.py Resolved
Comment thread dask/array/ufunc.py
not_equal = ufunc(np.not_equal)
equal = ufunc(np.equal)
isneginf = partial(equal, -np.inf)
isposinf = partial(equal, np.inf)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remind me why these are needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]: False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse doesn't implement __array_function__ or doesn't implement isneginf and isposinf?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info. Have raised issue ( pydata/sparse#252 ) on that point. Happy to carry this workaround for now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@jakirkham jakirkham May 13, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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). 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jakirkham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question above. Otherwise LGTM (assuming CI passes). Planning on merging in 24hrs if no comments.

Comment thread dask/array/ufunc.py Resolved
@jakirkham

Copy link
Copy Markdown
Member

Thanks @pentschev! Also thanks everyone for the reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants