Skip to content

WIP: ENH: have ndarray subclass np.ndarray #1022

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

Closed
wants to merge 1 commit into from

Conversation

stsievert
Copy link

I've run into an error with CuPy where it wasn't recognized as a NumPy ndarray. That is, isinstance(cupy_array, np.ndarray) == False. This resolves that.

Specifically, I believe this would resolve numba/numba#2786 (though it's untested). This issue happens because numba checks for isinstance(arg, np.ndarray) at deviceufunc.py#L97.

I'm don't see why numba shouldn't work with CuPy. I'm a new user of CuPy, but this seems like it'd help with NumPy integration (but I'm not certain what expense this would come at).

I can devote more time to this, and test, but first I'd like to hear if this is reasonable.

@stsievert
Copy link
Author

The NumPy team is planning to make subclassing NumPy arrays easier: https://m.youtube.com/watch?v=fowHwlpGb34

Here’s some docs subclassing ndarrays: https://docs.scipy.org/doc/numpy/user/basics.subclassing.html

I would be curious to hear more on this.

@shoyer
Copy link

shoyer commented Mar 13, 2018

NumPy has support for subclassing, but I would not recommend it for new code. We are trying to make it easier to write "duck array" classes which mimic numpy arrays without actual subclassing.

Subclassing brings in a host a potential problems because subclassing APIs are often poorly defined, and numpy.ndarray in particular has a very large API surface. It makes some things magically work, but other things will fail in very mysterious ways.

I'm not quite sure how you got the message that we are trying to make subclassing easier. That was definitely not an intended message of Nathaniel's talk.

@stsievert
Copy link
Author

Whoops, I meant "duck arrays" instead of "subclassing". I agree, this PR will have unexpected behavior. I support the NEP behind identifying and coercing duck arrays, numpy/numpy#10706 (and I'll close this PR in favor of that)

@stsievert stsievert closed this Mar 13, 2018
@teoliphant
Copy link

The Numba team is interested in using CuPy.

However, making CuPy a subclass of np.ndarray is not likely the right way to do that. It could be, but would need quite a bit more evaluation and investigation.

This change would not help Numba much --- in fact it might hurt, because Numba needs to know the details of the class and if they are not the same, odd errors could occur.

What is needed is for Numba to use CuPy instead of it's own version of a gpu device-array that it currently uses. There is support for this on the Numba team.

In a few months, I'd like to see CuPy and Numba make use of Xnd (github.com/plures/xnd) as well as gumath and ndtypes for describing general, typed containers.

@kmaehashi kmaehashi added this to the Closed issues and PRs milestone Mar 15, 2018
@datnamer
Copy link

datnamer commented Mar 27, 2018

I think teaching numba about pep 0544, implementing an abstract parameterized array protocol that dovetails with numpy's efforts, that then maps to an ndtype abstract type would be the way to go.

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

Successfully merging this pull request may close these issues.

Argument has invalid type for CuPy array
6 participants