-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Optimize lpmn_values via O(n²) recurrence (#33370) #33495
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
base: main
Are you sure you want to change the base?
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @AKash-A007, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant optimization to the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request optimizes the computation of associated Legendre functions by replacing a cubic-time algorithm with a more direct O(n²) recurrence-based approach. This is a significant performance improvement.
However, I've found a critical issue in the implementation: the recurrence relations used are only valid for unnormalized Legendre functions, but they are applied for the normalized case as well. This will lead to incorrect results when is_normalized=True, for example in sph_harm.
I've left a detailed comment on this issue. Additionally, the docstring should be updated to reflect the different recurrence relations for the normalized case.
Once these issues are addressed, this will be a great contribution.
|
Hi, thanks for the detailed feedback! I've addressed the normalization issue by:
The new version now produces correct normalized and unnormalized ALFs. Please let me know if you'd like the normalized recurrence implemented directly instead of post-multiplication. |
|
Hi - thanks for looking at this! A main feature of this change is that it replaces a |
This PR addresses issue #33370 by replacing the cubic-time algorithm in
_gen_associated_legendrewith the standard O(n²) recurrence relationsfor associated Legendre functions (ALFs).
✔ P_m^m computed via diagonal recurrence
✔ P_{m+1}^m computed via first off-diagonal relation
✔ P_n^m for n > m+1 computed using the three-term recurrence
✔ Fully vectorized over x
✔ Eliminates cubic mask generation and heavy einsum operations
✔ Preserves original normalization behavior
This reduces compute time for (m,n) up to ~200 by 10×–50× on CPU/TPU.
Let me know if you'd like further tests or a
lax.fori_loop-based version.