Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Generalized Universal Functions #4675

Merged
merged 1 commit into from
Mar 18, 2021
Merged

Conversation

emcastillo
Copy link
Member

@emcastillo emcastillo commented Feb 17, 2021

This is a big nasty one.

Currently wip.

Adds a GUFunc class (all the axes and batching stuff has been copied straight from dask).

There are some heavy incompatibilities here with things such as matmul since all the batching is done in the relevant cublas routine, we should avoid doing repeated calls at any cost. So for some gufuncs we need to skip several of these steps.

Also, we need to find a way to blend this with the regular UniversalFunction, probably wrapping it and doing a straight call should be enough, but we will provide additional support for currently non-supported kwargs such as where.

This needs to be cythonized too.

Fixes:
#4549
#2552

@jakirkham
Copy link
Member

cc @seberg @pentschev (in case this is of interest 🙂)

@leofang
Copy link
Member

leofang commented Feb 17, 2021

Since @seberg its pinged, I'd like to know whether #4549 (which this PR is trying to fix) should be considered as a NumPy bug or not. NumPy made the gufuncs not dispatchable via __array_function__ IMHO, see #4549 (comment).

@seberg
Copy link
Contributor

seberg commented Feb 17, 2021

I am not sure I understand. Since __array_ufunc__ takes complete control, it doesn't matter whether the cupy version of matmul is implemented as a ufunc? IIRC the __array_function__ NEP basically says, __array_ufunc__ is preferable, and any function may be moved to that (i.e. be converted into a ufunc if applicable).

I admit it is a bit weird that some cases will actually dispatch to a ufunc from within a non-ufunc wrapped with __array_function__

@emcastillo
Copy link
Member Author

Hi!
in CuPy right now matmul is a regular function instead of a ufunc and does not support most of the ufunc kwargs, so we are not dispatching properly. The issue is entirely in CuPy function dispatcher.
This PR is adding a wrapper to be used by matmul and other ufuncs so that now dispatch will behave properly, and we also get arguments such as axes and others in routines that did not support them before.

@emcastillo
Copy link
Member Author

emcastillo commented Feb 23, 2021

Btw, although the code is 60% straight from dask I added support for stuff such as optional dimensions in signatures (m?, n), (n, k?) -> (m?, k?), broadcasting, out kwarg, and now I am working on supporting dtypes and type signatures. So hopefully we will have a numpy 1:1 GUFunc API.

How should I reference Dask here? All the matrix transposition credits goes to them, so I don't know if I have to reproduce their license in the code.

@pentschev
Copy link
Member

How should I reference Dask here? All the matrix transposition credits goes to them, so I don't know if I have to reproduce their license in the code.

Usually Dask itself adds a comment to where it was taken from with a GitHub permalink (see https://github.com/dask/dask/blob/91cee5b7944e47959f475eaebc42dc75be14bc21/dask/array/numpy_compat.py#L167-L208 for reference), so maybe do the same here?

@emcastillo emcastillo changed the title [WIP] Support for Generalized Universal Functions Support for Generalized Universal Functions Mar 2, 2021
@emcastillo
Copy link
Member Author

PTAL,
Still some work left for future PRs.

Cythonize -> better performance
Improve test coverage
Support multiple outputs
Support dtype, order and the rest of kwargs.

BTW, matmul dispatch works now, so Dask will not complain :)

@emcastillo emcastillo closed this Mar 4, 2021
@emcastillo emcastillo deleted the gufunc branch March 4, 2021 08:18
@emcastillo emcastillo restored the gufunc branch March 4, 2021 08:19
@emcastillo emcastillo reopened this Mar 4, 2021
Copy link
Member

@leofang leofang left a comment

Choose a reason for hiding this comment

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

I haven't finished a full pass and will do so asap 😅 Found two minor issues so far.

cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
@kmaehashi kmaehashi added the blocking Issue/pull-request is mandatory for the upcoming release label Mar 15, 2021
cupy/core/_routines_linalg.pyx Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
cupy/core/_gufuncs.py Outdated Show resolved Hide resolved
@emcastillo
Copy link
Member Author

PTAL!

cupy/core/core.pyx Outdated Show resolved Hide resolved
@asi1024
Copy link
Member

asi1024 commented Mar 16, 2021

@emcastillo Could you merge the master branch to pass CI?

@asi1024 asi1024 added this to the v9.0.0rc1 milestone Mar 16, 2021
@emcastillo emcastillo force-pushed the gufunc branch 3 times, most recently from 14afbde to 7a0ea54 Compare March 17, 2021 02:58
@emcastillo
Copy link
Member Author

rebased :)

@asi1024
Copy link
Member

asi1024 commented Mar 17, 2021

Jenkins, test this please.

@chainer-ci
Copy link
Member

Jenkins CI test (for commit b1bce6a, target branch master) failed with status FAILURE.

@leofang
Copy link
Member

leofang commented Mar 17, 2021

Jenkins, test this please

@chainer-ci
Copy link
Member

Jenkins CI test (for commit b1bce6a, target branch master) succeeded!

@asi1024
Copy link
Member

asi1024 commented Mar 18, 2021

LGTM!

@asi1024 asi1024 merged commit 33c6139 into cupy:master Mar 18, 2021
@emcastillo emcastillo deleted the gufunc branch March 18, 2021 06:34
asi1024 added a commit that referenced this pull request Dec 3, 2021
chainer-ci pushed a commit to chainer-ci/cupy that referenced this pull request Dec 3, 2021
toslunar added a commit that referenced this pull request Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocking Issue/pull-request is mandatory for the upcoming release cat:feature New features/APIs prio:medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants