-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
BUG: no way to override matmul/@ if __array_ufunc__ is set #9028
Comments
Probably the simplest thing to do is to make |
The whole reason binops care about |
Agreed, this is also totally reasonable.
Yes, this would be my concern as well. We only get one chance to introduce |
The |
I think the opt-out is dual use, it basically tells arrays to "leave me alone". If arrays weren't such greedy bastards and only ate other arrays, we wouldn't need this solution. |
Arguably easiest would be to get |
Alternatively, could we just create some |
Note that the basic matrix multiply What would seem to be needed is:
@seberg, @eric-wieser - since you last looked at that file -- is this possible? |
I think it would be great to have matmul be a If you write |
I don't think the call needs to go directly to the gufunc, there can be preparation before and after the call, the override should still work as long as there is only one call to the gufunc. And the return can be reshaped. Multiple calls would get dicey. |
BTW, I don't think this needs to be in 1.13, we already have too much stuff. |
But it would be good to solve the issue here one way or another, of not being able to override |
Plus vecvec. In general, one would like to reshape (..., i, j) dot (j,) to (n,j) dot (j,) for efficiency in multiplication, and then unreshape, same with various other combinations, plus BLAS has contiguity requirements, so some preliminary work is always required. Timewise, there doesn't seem to be much difference between the special cases and reshaping and having just one function may make things easier for the user who wants to override. |
Note that |
If we do have a gufunc matmat and want to use BLAS, then we should also raise an error if the passed arguments do not meet contiguity requirements. BLAS makes everything more difficult to pull off and we want to make things as easy for users as we can. @eric-wieser's suggestion of some sort of wrapper may be the best way forward. |
I assume gufuncs already make some continuity copies just to work with LAPACK, but that needs to be checked. |
More generally, we don't really want to be passing private internal gufuncs (like the two used by A "ufunc like" interface that defines most of that standard ufunc API (at least the attributes, if not all the methods) seems like it could be pretty safe to use with
That said, there's lots to sort out for what this interface should look like, so it's definitely not something to block for 1.13. |
I'm going to kick this on to 1.14 for solution. |
This issue just surfaced in dask when implementing Per @mrocklin (dask/dask#2438 (comment)), it appears that the result does not necessarily raise an error and can return some sort of malformed or eagerly evaluated numpy array. This is more problematic than I thought... |
|
@charris, when you refer to "some kind of wrapper" do you mean this comment?
Is this still considered the best way forward, or is there more consensus around making |
@mattip - I don't think there was consensus! For
I think (1) might be the ideal solution and (2) could lead to that fairly easily, so perhaps that is indeed the right way forward. But as @charris noted, an advantage of (3) is that those would be good to have anyway. |
Let me propose one more option:
This option would allow for potential divergence between I would be happy with either of @mhvk's options (1) or (2), or my option (4). My concern with (3) is that it will impose some requirements on the other argument, namely that it must have |
I vote for (4) in the short term and (3) in the long term. I can think of cases where (for multidimensional arrays) I'd like to do Allowing multiple signatures is ideal in principle, but will cause a lot of corner cases and problems while supporting/maintaining in practice (at least according to what I predict). |
@hameerabbasi - I should perhaps have been clear that there is no reason not to implement |
An option that falls short of full arbitrary multi-signature dispatch, would be to add support to gufuncs specifically for the way matmul does signatures. For example, syntax like |
Yes, that sounds like a good solution! I do somewhat worry that this (like, e.g., #5015), will need some tweaking to the ufunc structure and thus break C-API. Right now all we pass on from each separate element/character of the signature is a simple index But perhaps the right way to proceed is to try and add a set of flags or so for every element (anything that's expandable...). This would allow #5015 as well. |
We're going to have to rewrite a lot of ufunc internals anyway to progress on both the duck array and the user-defined dtypes work. A few years ago I did a lot of checking and found that there was no code on GitHub or in Debian that touched the internals of the ufunc struct, except numba and dynd. Dynd is defunct now, and we can coordinate with numba. (Probably should double check that there situation hasn't changed too.) So I think opaqueifying the ufunc internals is viable. |
Sorry, between poor phrasing and autocorrect this came out kind of garbled. What I meant to say is, since my checking was a few years ago now, we should probably rerun it, in case any new users have appeared. |
If I understand how @njs 's proposal would play out, it means implementing @njs do you remember how you did the search? Whenever I try to search GitHub for NumPy C-API consumers, I end up getting many false-positives for repos that fork/copy NumPy code |
Yeah, that's the problem :-/ Hence: https://github.com/njsmith/codetrawl (BTW @njs is, sadly, someone else.) |
@njsmith Thanks for the tool Did I understand your proposed changes correctly, implementing |
Yeah, it's (1) with the additional note that it's not adding generic "multi-signature" support, just a specific narrow form of multi-signature support. |
Closing. matmul is now implemented as a ufunc. What is the status of |
With NumPy master, it is currently impossible to override
ndarray @ other
ifother
also implements__array_ufunc__
.Consider:
This is problematic. We definitely need a way to allow for overriding
@
from the second argument, whether than means breaking the rules on__array_ufunc__
(allowingmatmul
to be passed even though it isn't a ufunc) or breaking the rules for__matmul__
(to call__rmatmul__
or allow__rmatmul__
to be called by Python, even when__array_ufunc__
is implemented on the other argument).The text was updated successfully, but these errors were encountered: