ENH: Optimize and cleanup ufunc calls and ufunc CheckOverrides#15271
ENH: Optimize and cleanup ufunc calls and ufunc CheckOverrides#15271charris merged 12 commits intonumpy:mainfrom
Conversation
383139c to
261f7bf
Compare
|
Great! Would think it is useful to split up. In particular, the benchmarks should go in first, to establish a baseline. |
261f7bf to
cdb54ba
Compare
ceb04e3 to
f85ae19
Compare
f85ae19 to
3c73f86
Compare
|
Just to note, this one should be almost ready, but I should prod it a little bit, and test against astropy (and maybe dask) to make sure they are fine with the |
mhvk
left a comment
There was a problem hiding this comment.
I had a cursory look just to see what was up, and overall it seems a really nice change. I mostly looked at the ufunc code, which is considerably simplified. Great! If it is ready for more detailed review, I can try to make some time (though perhaps just focussing on the ufunc stuff; it is a lot, though I can see it is not so easy to separate).
numpy/core/src/umath/override.c
Outdated
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
The unpacking happens elsewhere, correct?
numpy/core/src/common/npy_argparse.h
Outdated
There was a problem hiding this comment.
Parenthesis after kwnames should not be there.
numpy/core/src/common/npy_argparse.h
Outdated
There was a problem hiding this comment.
I wondered quite a bit about whether this implementation perhaps should be shared a bit with the PREPARE macro, but in the end it does seem that this is quite simple and logical, keeping keyword names, their actions, and their destinations together, etc. So, I guess I ended with a 👍
There was a problem hiding this comment.
Oh, this should be in now, does it still show up in the diff? Let me rebase quickly to make it hopefully go away. (will make a note of the above comments if they disappear though.)
I tried for a while to do that, but I don't think it is possible. IIRC there was a gcc specific solution (not C99) to be able to give a return value and declare a variable.
7ca285b to
a701a62
Compare
|
@mhvk yeah, it would be great to move this along. Because I need to attack the ufunc code soon, and separating argument parsing from the rest of the code is pretty much a prerequisite to refactor more. It seems I messed up with the other PRs (picking a wrong commit during rebase), the whole |
a701a62 to
51a17ca
Compare
|
Rebased with only the ufunc related changes. I will put further fixups into their own commits for clarity (I don't really expect much, but maybe an occasional refcount problem in override-paths or so). |
mhvk
left a comment
There was a problem hiding this comment.
@seberg - this is very nice indeed!!!! I think the code is much cleaner now. My comments are basically all nitpicks, with perhaps the only one to be thought through being the one about the type of error to be raised when a tuple of outputs has the wrong size.
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
Add "on input" at the end (at least, that is how I understand it!)
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
It would seem we might as well leave this to the caller as well, since we ask for it to initialize the output objects to NULL too.
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
Name *_mps is not very instructive... Maybe "*_with_operand_pointers"? (not very important, obviously!)
There was a problem hiding this comment.
Renamed it to _with_operands, but you are right and I will make a PR after this to just remove the whole thing. Since nobody complained yet, I assume we can go ahead with disabling PyUFunc_GenericFunction for good.
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
Here perhaps ValueError is actually more logical? After all, the type is correct at this point (it being a tuple). Not sure. cc @eric-wieser who introduced this routine.
There was a problem hiding this comment.
Trying to turn it back into a ValueError. I don't remember if I had more reason to change it (will have to see if the tests need adapting later).
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
I realize this was here before, but shall we call the variable axis_obj in analogy with all the others? (Feel free to leave it as is, though!)
numpy/core/src/umath/override.c
Outdated
There was a problem hiding this comment.
Delete "An error ..." as this is done in ufuncobject.c
numpy/core/src/umath/override.c
Outdated
There was a problem hiding this comment.
This note was very confusing to me: maybe "note that any input and output arrays in args have already been stored in in_args and out_args"
The comment about len_args seems false (at least for reductions).
There was a problem hiding this comment.
Hmmmpf, this probably made a bit more sense in the old (more complicated) iteration. Adopting your comment.
numpy/core/src/umath/override.c
Outdated
numpy/core/src/umath/ufunc_object.c
Outdated
There was a problem hiding this comment.
Stupid and small, but perhaps assert(!(axes != NULL && axis != NULL))
|
Hmmmpf, there is a small "conflict" with gh-18670, in the |
mhvk
left a comment
There was a problem hiding this comment.
Only some nitpicks left, really... Might be good to get another pair of eyes, just in case.
There was a problem hiding this comment.
"This" is confusing - does it refer to what was the case previously, or what is the case now. Overall, my suggestion would be to just delete the sentence, as I think it is very unlikely someone relied on this behaviour.
There was a problem hiding this comment.
Sentence not grammatical! I think you want "where used for" -> "to check for"
There was a problem hiding this comment.
Add "array in a reduction such as np.add.reduce."
numpy/core/src/umath/override.c
Outdated
There was a problem hiding this comment.
Nit: double "checking" should be "before checking for overrides"
numpy/core/src/umath/ufunc_object.c
Outdated
b334712 to
d4ad09e
Compare
d4ad09e to
fe40b15
Compare
This is a rather large commit which clean up the ufunc override code for argument parsing. (I may attempt to split it up) There are two main things, argument parsing, especially for reductions is now much faster since `METH_FASTCALL` is used (when keyword arguments are being used). By moving the argument parsing and using more generic code this also simplifies the ufunc override checking especially when keyword arguments are present. Both of this decreases the argument parsing overhead quite a lot for ufunc calls, especially for small reductions. (Without double checking, I believe the speedup was up to around 30% for small reductions.) The downside is some added/annoyance due to the use of marcos to support both FASTCALL and no FASTCALL semantics. As a side note: vectorcall is likely not a huge factor for ufuncs since it is very common not to use any keyword arguments. OTOH, code that uses ``out=...`` a lot on small arrays should see a nice difference.
The TypeError TODO is also reflected in the tests I think, the other TODO seems pretty unnecessary (most users will go through `tp_vectorcall` anyway, so micro-optimizing that is not all that relevant).
This fixes the review comments by Marten, mainly small cleanups. The largest change is that it is now back to a ValueError if the out tuple has the wrong length. Co-Authored-By: Marten van Kerkwijk <mhvk@astro.utoronto.ca>
These functions do not require `NPY_NO_EXPORT`. One of them was (incorrectly) flagged by code coverage. Maybe this helps...
This is based on Marten's review again.
fe40b15 to
a1fc8c4
Compare
|
If there are two things going on in this PR it might be best to split them. |
a1fc8c4 to
10ee4e0
Compare
|
I had not written release notes for gh-15269 and gh-18642, since they should not have any compatibility issues. But as the PRs do similar things on different part of the code-base I thought it fits together in the release notes. Other than the release notes thing, this PR is a bit annoyingly large, but that is mostly the nature of the beast, unfortunately. There are some side battles here, but those won't be easy to split out I think. |
mhvk
left a comment
There was a problem hiding this comment.
Thanks. To me, this looks all OK!
|
|
||
| ``__array_ufunc__`` and additional positional arguments | ||
| ------------------------------------------------------- | ||
| Previously, all positionally passed arguments were check for |
There was a problem hiding this comment.
Sorry, missed a small mistake: check -> checked
There was a problem hiding this comment.
Thanks. Yeah I have to remember to add [skip CI] more often. Seems it didn't pan out, because I pushed before CI was finished I guess.
|
p.s. Minus the last spelling mistake, that is. Note that skipping CI on actions is now possible, by adding [skip CI] to the commit message - see https://github.blog/changelog/2021-02-08-github-actions-skip-pull-request-and-push-workflows-with-skip-ci/ |
[skip CI]
|
Thanks Sebastian. |
This is a rather large commit which clean up the ufunc override
code for argument parsing. (I may attempt to split it up)
There are two main things, argument parsing, especially for
reductions is now much faster since
METH_FASTCALLis used(when keyword arguments are being used).
By moving the argument parsing and using more generic code this
also simplifies the ufunc override checking especially when
keyword arguments are present.
Both of this decreases the argument parsing overhead quite a lot
for ufunc calls, especially for small reductions. (Without double
checking, I believe the speedup was up to around 30% for small
reductions.)
The downside is some added/annoyance due to the use of marcos to
support both FASTCALL and no FASTCALL semantics.
As a side note: vectorcall is likely not a huge factor for ufuncs
since it is very common not to use any keyword arguments. OTOH,
code that uses
out=...a lot on small arrays should see a nicedifference.
Only the last commit is new, the others are changes from gh-15269 and gh-15270
@mhvk the last commit here is about what you had once looked at for the override cleanup stuff.