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 Numpy-like arrays #397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

mrocklin
Copy link

@mrocklin mrocklin commented May 9, 2018

Several libraries exist that support a broad subset of the Numpy API,
but with different underlying data structures:

  • CuPy for GPU arrays
  • Sparse arrays
  • Dask array for parallel and distributed arrays

With the __array_ufunc__ protocol these can support numpy functions
like np.sin directly.

Previously these would not work with autograd because their types and
functions were not registered.

However, to the extent that they support the numpy API directly (which
is increasingly becoming the case) we can just hijack the existing
function mappings for Numpy functions.

This commit replaces two explicit type checks with more flexible tests
that check for "array-like" behavior. If so then they return the
corresponding numpy function.

There are several things wrong with this commit as it stands:

  1. introduces dask as a testing dependency
  2. is far from a comprehensive test
  3. introduces a function name typeof which can probably be improved

My initial goal here is just to start conversation and see if this is of interest.

Several libraries exist that support a broad subset of the Numpy API,
but with different underlying data structures:

-  CuPy for GPU arrays
-  Sparse arrays
-  Dask array for parallel and distributed arrays

These all support numpy functions like np.sin directly

Previously these would not work with autograd because their types and
functions were not registered.

However, to the extent that they support the numpy API directly (which
is increasingly becoming the case) we can just hijack the existing
function mappings for Numpy functions.

This commit replaces two explicit type checks with more flexible tests
that check for "array-like" behavior.  If so then they return the
corresponding numpy function.
@j-towns
Copy link
Collaborator

j-towns commented Jun 1, 2018

Boxes and VSpaces are designed to be extensible via their .register method. For example, the Box mapping for ndarray is registered here. Can't we just use this system to register the extra array-like types?

I will try to code up an example of how this might look for Dask arrays.

@mrocklin
Copy link
Author

mrocklin commented Jun 1, 2018 via email

@mrocklin
Copy link
Author

mrocklin commented Jun 4, 2018

There is now a NEP and an associated mailing list discussion. Engagement from this community on that mailing list would be quite welcome: http://numpy-discussion.10968.n7.nabble.com/NEP-Dispatch-Mechanism-for-NumPy-s-high-level-API-td45638.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants