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
Allow libraries that implement __array_ufunc__ to override DUFunc.__c… #8995
Conversation
@jpivarski, Thank you for the effort. This PR is definitely in the right direction.
Yes, that would do. A minimal class to exercise the new code path in Dufunc would be sufficient.
I think we should keep this PR small and focus on In summary, I think all that's remaining for this PR are tests and a doc update. |
I added a test (we'll see what happens in CI), though I'm not sure what to say where for a doc update. I never saw any documentation that says that ufuncs made by Thanks! |
For docs, I think it's worth adding a note in https://github.com/numba/numba/blob/main/docs/source/reference/jit-compilation.rst#vectorized-functions-ufuncs-and-dufuncs to say what level of NEP13 is supported. |
Okay, I think that's it: the assertions are unittest-style and I've added a paragraph at the end of the jit-compilation.rst reference. |
Is this waiting on anything from me? As far as I'm aware, it's done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
This PR addresses a long-standing issue in which array-like objects that define an
__array_ufunc__
method (NEP-18) can't be used with ufuncs created bynp.vectorize
unless a signature is provided. (I should have done this years ago!)For instance, in Awkward Array,
but
and even if we pass
nopython=True
,Similarly, with the Sparse library,
but
and
However, with this PR, ufuncs created with
@nb.vectorize
and no type signature are recognized by Awkward Array:and Sparse:
It's because
__array_ufunc__
methods are usually implemented by pre- and post-processing the result of applying the ufunc to some underlying NumPy array. If an__array_ufunc__
method is implemented some other way, without Numba-compatible objects at any level, then@nb.vectorize
-decorated functions wouldn't work with it, anyway.This is a draft PR because:
Some thought will be needed to know what kinds of tests would be appropriate. This mostly affects libraries that use Numba. Maybe a little class that implements
__array_ufunc__
?It could be related to #6678, #4625, and/or #2979. For completeness, the PR in its current state only addresses ufunc's
__call__
method; perhaps we wantaccumulate
,at
,outer
,reduce
, andreduceat
as well. Also, it only addressesnp.vectorize
; maybe something similar is needed fornp.guvectorize
.I'm leaving it in this simple state so that I can get feedback about whether this is the right direction before proceeding to generalize it. (Also, which generalizations are important?)