-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Ensure einsum uses chunking (now that nditer doesn't) #28043
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
Conversation
The nditer refactor enabled the `GROWINNER` for reductions, this was not the case before (I am not sure about cases where the reduction is in an outer axis, but I don't think so). Either way, as the test says, chunking always improves the precision of large sorts if their mean is nonzero and sklearn noticed lower precision. We could only remove growinner if there is a reduction, but it seems like a 1% performance hit for the simplest (non-trivial) case where GROWINNER had done something before.
Thanks a lot for looking at this! I can confirm that this PR fixes scikit-learn/scikit-learn#30509. |
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.
Seems a sensible solution for now. Two small comments inline.
numpy/_core/tests/test_einsum.py
Outdated
""" | ||
num = 100000000 | ||
value = 1.00000000002 | ||
res = np.einsum("i->", np.full(num, value)) / num |
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.
I'd tend to use float32
and fewer elements, just to keep the test fast (now 147 ms on my machine).
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.
Doesn't work :). Einsum uses double for the accumulation, so for float32, the results are actually better without chunking...
But, let me use broadcasting and make the number smaller, certainly doesn't need this much.
@@ -1035,7 +1035,6 @@ PyArray_EinsteinSum(char *subscripts, npy_intp nop, | |||
iter_flags = NPY_ITER_EXTERNAL_LOOP| | |||
NPY_ITER_BUFFERED| | |||
NPY_ITER_DELAY_BUFALLOC| | |||
NPY_ITER_GROWINNER| |
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.
How about adding a comment here? Partially as a reminder that a pair-wise sum might be a good idea...
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.
Yeah, didn't think of the "let's do pairwise" spin, so wasn't sure.
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.
Super!
Great, thanks a lot for the investigation and the fix! Maybe one day I will manage to understand these tricky floating point things, maybe one day 😅. |
Conceptually, (in decimals) it comes down to: But if you chunk it up into batches of 10 |
The nditer refactor enabled the
GROWINNER
for reductions, this was not the case before (I am not sure about cases where the reduction is in an outer axis, but I don't think so).Either way, as the test says, chunking always improves the precision of large sums if their mean is nonzero and sklearn noticed lower precision.
We could only remove growinner if there is a reduction, but it seems like a 1% performance hit for the simplest (non-trivial) case where GROWINNER had done something before.
I suspect this is a pragmatic thing (little downside, better precision for large arrays). An even better thing would be pairwise summation, within einsum I suppose.
I.e. the reason for the chunking used to be that the iterator didn't manage to optimize this (an explicit code comment). But now, it became a choice to help the sum precision a bit...
See also scikit-learn/scikit-learn#30509, ping @lesteve