Skip to content

change __array_function__ implementation for backwards compat - #5043

Merged
mrocklin merged 17 commits into
dask:masterfrom
rgommers:amin-arrayfunction
Jul 5, 2019
Merged

mrocklin merged 17 commits into
dask:masterfrom
rgommers:amin-arrayfunction

Conversation

@rgommers

@rgommers rgommers commented Jul 2, 2019 •

Copy link
Copy Markdown
Contributor
  • Tests added / passed
  • Passes black dask / flake8 dask

This adds a fix and regression tests for gh-5031, and is an alternative to gh-5032. This PR is not complete yet (hence the WIP label), but enough to compare with gh-5031 and make a decision on whether this is the right approach. If so, more @implements decorators need to be added.

It's based on the discussion in #5031 (comment), and follows the @implements decorator suggestion in NEP 18. It solves two problems:

  1. The issue with some aliases (np.min is np.amin) reported in dask array failing with numpy 1.17.0rc1 and np.min #5031
  2. The backwards compatibility break for all numpy functions handled with __array_function__ that do not have a dask.array equivalent.

To explain (2) in more detail:

In [1]: np.__version__                                                                         
Out[1]: '1.16.4'

In [2]: import dask.array as da                                                                

In [3]: image = da.from_array(np.array([[0, 1], [1, 2]]), chunks=(1, 2))                       

In [4]: np.median(image)                                                                       
Out[4]: 1.0

With dask master:

In [1]: np.__version__                                                                         
Out[1]: '1.17.0.dev0+f0e64a1'

In [2]: import dask.array as da                                                                

In [3]: image = da.from_array(np.array([[0, 1], [1, 2]]), chunks=(1, 2))                       

In [4]: np.median(image)                                                                       
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-a6d36c3a2fe3> in <module>
----> 1 np.median(image)

<__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'>]

With this PR:

In [1]: np.__version__                                                                         
Out[1]: '1.17.0.dev0+f0e64a1'

In [2]: import dask.array as da                                                                

In [3]: image = da.from_array(np.array([[0, 1], [1, 2]]), chunks=(1, 2))                       

In [4]: np.median(image)                                                                       
/Users/rgommers/Code/dask/dask/array/core.py:1226: UserWarning: `median` is not implemented by dask, explicitly coerce your array (e.g. with numpy.asarray) to silence this warning
  "to silence this warning".format(func.__name__)
Out[4]: 1.0

rgommers added 2 commits July 1, 2019 17:39
See daskgh-5031 for discussion. Note that decorating all functions is not yet
complete; this is enough to show tests in `test_numpy_compat.py` passing.
Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
@jakirkham jakirkham added the array label Jul 2, 2019
Comment thread dask/array/core.py Resolved
@rgommers rgommers changed the title WIP: change __array_function__ implementation for backwards compat change __array_function__ implementation for backwards compat Jul 2, 2019
@rgommers

rgommers commented Jul 2, 2019

Copy link
Copy Markdown
Contributor Author

Okay, this should be complete, passes all tests in dask/array/tests/ locally for me, except for 4 failures that I also see on master (will see about fixing those or reporting them separately).

Comment thread dask/array/tests/test_ufunc.py Resolved
Comment thread dask/array/reductions.py Resolved
@rgommers

rgommers commented Jul 3, 2019 •

Copy link
Copy Markdown
Contributor Author

Hmm, the only failures are because warnings are inconsistently showing. On Travis:

E           UserWarning: `imag` is not implemented by dask, explicitly coerce your array (e.g. with numpy.asarray) to silence this warning

and on Appveyor:

E               Failed: DID NOT WARN. No warnings of type (<class 'UserWarning'>,) was emitted. The list of emitted warnings is: [].

This may depend on test execution order if warnings aren't set to "always". Before I dig into CI warning settings, may be good to know if this PR is okay otherwise. Perhaps the warnings should be removed completely, not sure - that's a taste question I'd say.

Comment thread dask/array/core.py Resolved
Comment thread dask/array/core.py Resolved
@rgommers
rgommers force-pushed the amin-arrayfunction branch from dff89bc to 5e06685 Compare July 3, 2019 02:07
@rgommers

rgommers commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

Only one failure left, that is consistent between all test runs:

        image = da.from_array(np.array([[0, 1], [1, 2]]), chunks=(1, 2))
        with pytest.warns(FutureWarning, match="`numpy.median` is not implemented by dask"):
>           assert int(np.median(image)) == 1
E           Failed: DID NOT WARN. No warnings of type (<class 'FutureWarning'>,) was emitted. The list of emitted warnings is: [].

That definitely gives a warning when run standalone, so it's due to not always raising a warning or not turning warnings to errors. Can be fixed with CI settings or by removing the warning filter. I'd suggest the former, but some input would be nice on what you prefer.

@jrbourbeau

Copy link
Copy Markdown
Member

Right now we only have NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1 for the Python 3.7 build on Travis. There's a IS_NEP18_ACTIVE boolean in dask/array/utils.py that could be used to skip test_median_func accordingly. E.g. something like:

@pytest.mark.skipif(
    not IS_NEP18_ACTIVE, reason="NEP-18 support is not available in NumPy"
)
def test_median_func():
    ...

Then again, if this is testing __array_function__ behavior then perhaps it makes sense for it to be in dask/array/tests/test_array_function.py

@rgommers

rgommers commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

Thanks @jrbourbeau, good catch. Updated and moved to test_array_function.py

Comment thread dask/array/core.py Resolved
@mrocklin

mrocklin commented Jul 3, 2019

Copy link
Copy Markdown
Member

I think I'm somewhat against having to explicitly mark all current and future dask array functions with implements.

@pentschev

Copy link
Copy Markdown
Member

I totally understand @mrocklin's concern on decorating functions with implements, but I don't have a strong objection to that either. So ultimately, I will defer the decision whether to integrate this or #5032 to him.

@mrocklin

mrocklin commented Jul 3, 2019

Copy link
Copy Markdown
Member

Maybe we leave things as they are if they match the numpy API already, and we optionally have a dict for things that don't match?

@rgommers

rgommers commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

I think that's feasible, and a good idea. IIRC I saw a few functions that had the same name but didn't match (need to check again), but those could be added to a skip list within __array_function__

@mrocklin

mrocklin commented Jul 3, 2019

Copy link
Copy Markdown
Member

I think that's feasible, and a good idea. IIRC I saw a few functions that had the same name but didn't match (need to check again), but those could be added to a skip list within array_function

Thanks for doing this! I think that that was my main concern.

It looks like the test test_array_notimpl_function_dask[<lambda>0] is currently failing. We may want to make the match condition less precise to be more flexible to future changes here.

In principle I'm fine with what's here. I suspect that others may have more technical concerns though. If there are no further comments and if tests pass then I'll plan to merge in 24 hours.

@rgommers
rgommers force-pushed the amin-arrayfunction branch from 70361d7 to 8c8bfc1 Compare July 3, 2019 20:46
Comment thread dask/array/tests/test_ufunc.py Resolved
@pentschev

Copy link
Copy Markdown
Member

LGTM. Thanks a lot for the work here @rgommers!

@pentschev

Copy link
Copy Markdown
Member

Btw, the Travis errors are just linting complaints from black.

@rgommers
rgommers force-pushed the amin-arrayfunction branch from 8c8bfc1 to 9474b8c Compare July 3, 2019 23:52
@rgommers

rgommers commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

Btw, the Travis errors are just linting complaints from black.

Grr, each time I forget to check there's a tiny tweak it wants to do. Guess you use a post-commit hook for that?

@jrbourbeau

Copy link
Copy Markdown
Member

You should be able to configure pre-commit hooks with

cd /path/to/dask
pip install pre-commit
pre-commit install

@mrocklin
mrocklin merged commit 2f55cf4 into dask:master Jul 5, 2019
@mrocklin

mrocklin commented Jul 5, 2019

Copy link
Copy Markdown
Member

This is in. Thanks @rgommers for the fix and @pentschev , @shoyer , @jrbourbeau for the review!

@rgommers
rgommers deleted the amin-arrayfunction branch July 5, 2019 15:56
@rgommers

rgommers commented Jul 5, 2019

Copy link
Copy Markdown
Contributor Author

Thanks for the help and reviews everyone!

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.

6 participants