Skip to content

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

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

seberg
Copy link
Member

@seberg seberg commented Dec 19, 2024

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

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.
@lesteve
Copy link
Contributor

lesteve commented Dec 20, 2024

Thanks a lot for looking at this! I can confirm that this PR fixes scikit-learn/scikit-learn#30509.

Copy link
Contributor

@mhvk mhvk left a 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.

"""
num = 100000000
value = 1.00000000002
res = np.einsum("i->", np.full(num, value)) / num
Copy link
Contributor

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).

Copy link
Member Author

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|
Copy link
Contributor

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...

Copy link
Member Author

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.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super!

@mhvk mhvk merged commit 9aa5cda into numpy:main Dec 20, 2024
66 checks passed
@lesteve
Copy link
Contributor

lesteve commented Dec 21, 2024

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 😅.

@seberg seberg deleted the einsum-always-chunk branch December 21, 2024 11:02
@seberg
Copy link
Member Author

seberg commented Dec 21, 2024

Maybe one day I will manage to understand these tricky floating point things

Conceptually, (in decimals) it comes down to:
Say 10. + 1.0001 == 11.0000 in floating point math, but for smaller values 9 + 1.0001 was still exact (not true, but...).
Then sum([1.0001] * 10) might give you 10.001 exactly, but sum([1.0001] * 20) will be the reach 10.001 and after that ignore the 0.0001 part and return 20.001 rather than 20.002.

But if you chunk it up into batches of 10 sum([1.0001] * 10) + sum([1.0001] * 10) you get 10.001 + 10.001 which is precise: It is more precise to add similar values and chunking/ or pairwise summation ((a0 + a1) + (a2 + a3)) + ... tends to add more similar values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants