Skip to content

Lagrange evaluation performance improvements#1409

Merged
akoshelev merged 3 commits intoprivate-attribution:mainfrom
akoshelev:lagrange-perf
Nov 12, 2024
Merged

Lagrange evaluation performance improvements#1409
akoshelev merged 3 commits intoprivate-attribution:mainfrom
akoshelev:lagrange-perf

Conversation

@akoshelev
Copy link
Copy Markdown
Collaborator

@akoshelev akoshelev commented Nov 7, 2024

rustc didn't optimize the eval function and ended up doing
13107232992 loop iteration per Lagrange compute.

For some unknown reason to me, optimizer works only if we operate
on integers, not on fields. Maybe modulo reduction is to blame

There is really no reason for taking an iterator, just makes things worse for the compiler
rustc didn't optimize the `eval` function and ended up doing
131072*32*992 loop iteration per Lagrange compute.

For some unknown reason to me, optimizer works only if we operate
on integers, not on fields. Maybe modulo reduction is to blame
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 7, 2024

Codecov Report

Attention: Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.

Project coverage is 93.14%. Comparing base (875e70a) to head (a0c6a78).
Report is 33 commits behind head on main.

Files with missing lines Patch % Lines
...rc/protocol/ipa_prf/malicious_security/lagrange.rs 87.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1409      +/-   ##
==========================================
- Coverage   93.46%   93.14%   -0.32%     
==========================================
  Files         223      225       +2     
  Lines       38456    38568     +112     
==========================================
- Hits        35944    35926      -18     
- Misses       2512     2642     +130     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@akoshelev
Copy link
Copy Markdown
Collaborator Author

Maybe we can take this PR as is and improve later. It gives another 50% boost:

4hrs vs 6hrs

@akoshelev akoshelev marked this pull request as ready for review November 8, 2024 01:11
@akoshelev akoshelev changed the title [DRAFT] Lagrange evaluation performance improvements Lagrange evaluation performance improvements Nov 8, 2024
Comment thread ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs
Copy link
Copy Markdown
Collaborator

@tyurek tyurek left a comment

Choose a reason for hiding this comment

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

Looks good!

Comment on lines +166 to +172
// Staying in integers allows rustc to optimize this code properly
// with any reasonable N, we won't run into overflow with dot product.
// (N can be as large as 2^32 and still no chance of overflow for 61 bit prime fields)
debug_assert!(
F::PRIME.into() < (1 << 64),
"The prime {} is too large for this dot product implementation",
F::PRIME.into()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We are multiplying two field values and then summing. I'd say that the conservative requirement is 2 * F::BITS + N.next_power_of_two().ilog2() <= u128::BITS.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

that's right, I think this assertion just assumes N being reasonable and the second argument of this expression negligible compared to F::BITS. I don't mind changing it, the reasoning behind this was just to optimize for readers

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't think what I said was correct here - I did go over the math again and we can't sustain $2^{32}$ additions for sure w/o risk of an overflow. @andyleiserson suggestion was correct

It turns out the original check was wrong estimating overflow conditions
@akoshelev akoshelev merged commit 0a34110 into private-attribution:main Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants