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
Implement most ufunc attributes and ufunc.reduce #9123
Implement most ufunc attributes and ufunc.reduce #9123
Conversation
@kc611, could you please take a look at this PR once you have a chance? I'd like to discuss possible ways to speedup ufunc.reduce |
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.
Cool. It would be nice if numba could do reduce ufuncs.
# But this fails as it cannot a weak reference to an ufunc due to NumPy | ||
# not setting the "tp_weaklistoffset" field. See: | ||
# https://github.com/numpy/numpy/blob/7fc72776b972bfbfdb909e4b15feb0308cf8adba/numpy/core/src/umath/ufunc_object.c#L6968-L6983 # noqa: E501 | ||
|
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.
Does NumPy need to explicitly set the field? From reading the documentation I would think it inherits from the object
type.
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.
When attempting to create a weak reference to an ufunc, CPython raises an error:
>>> import weakref
>>> import numpy as np
>>> weakref.ref(np.minimum)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create weak reference to 'numpy.ufunc' object
numba/np/ufunc/dufunc.py
Outdated
return s | ||
|
||
@intrinsic | ||
def compute_flat_idx__(typingctx, strides, itemsize, idx, axis): |
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.
Question: The compute_flat_idx__
is not being used anywhere. Is that intended?
And is it overall faster or slower than compute_flat_idx
?
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.
compute_flat_idx__
implements the same code as compute_flat_idx
but it is just a bit switch table where each case is computed by hand beforehand. In short, it looks something like this:
def compute_flat_idx(axis, ...):
if axis == 0:
# compute flat idx when axis == 0
else axis == 1:
# compute flat idx when axis == 1
...
And is it overall faster or slower than compute_flat_idx?
Faster
# https://github.com/numpy/numpy/blob/7fc72776b972bfbfdb909e4b15feb0308cf8adba/numpy/core/src/umath/ufunc_object.c#L6968-L6983 # noqa: E501 | ||
|
||
at = types.Function(template) | ||
attributes = ('nin', 'nout', 'nargs', # 'ntypes', # 'types', |
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.
The incapability of accessing types can be opened as a separate issue, to be fixed once there is a solution upstream. Otherwise if we don't expect a solution in future, might be a good idea to remove the commented code.
@guilhermeleobas Thanks for the patch. This PR mostly looks good. Some nitpicks and questions above before we can mark this as RTM. Edit: This also needs a documentation entry that support for |
CI failure doesn't seem to be related to this PR. |
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.
LGTM!
gpuci run tests |
@kc611 Is this good to be RTM (assuming it passes the gpuCI run I just started)? |
I think it's good to go. (Unless someone objects) |
gpuCI failed one CUDA version for some kind of Conda error. I expected if this was going to break CUDA, it'd break for all configurations, so I'm happy to ignore that. |
Thanks for the review, folks. I'll create the issues to track future work:
|
This PR exposes most ufunc attributes and implements
ufunc.reduce
for ufuncs created with@vectorize
None
)Implementing ufunc.ntypes or ufunc.types is not possible as Numba needs to call the NumPy ufunc attribute at runtime. However, this is not possible as NumPy doesn't allow creating weak references to an ufunc, which is required by
types.BoundFunction
.To-do:
Reduce is implemented for the following set of arguments:
keeping #1269 (comment) as a reference here.
Code I used to benchmark: