Skip to content

Implement Analytical Derivatives on CompoundModel - #17034

Merged
neutrinoceros merged 6 commits into
astropy:mainfrom
Cadair:compound_analytical_deriv
Oct 25, 2024
Merged

neutrinoceros merged 6 commits into
astropy:mainfrom
Cadair:compound_analytical_deriv

Conversation

@Cadair

@Cadair Cadair commented Sep 18, 2024 •

Copy link
Copy Markdown
Member

Description

This pull request adds support for computing analytical derivatives on CompoundModel for the +, -, * and / operators.
Others are possible, but haven't been attempted yet.

The main motivation for this PR is to substantially improve the performance of fitting these models. The profiling script we are using which fits a Const + Gaussian1D model in parallel went from 28.1s to 19.8s so a 1.4x speedup.

Probably Fixes #3783, but that might have a slightly larger scope

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@Cadair
Cadair requested a review from a team as a code owner September 18, 2024 14:56
@Cadair
Cadair marked this pull request as draft September 18, 2024 14:56
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@Cadair Cadair added this to the v7.0.0 milestone Sep 18, 2024
@Cadair Cadair changed the title Implement Analytical Derivatives on CompoundModel Implement Analytical Derivatives on CompoundModel Sep 18, 2024
@pllim pllim added Performance benchmark Run benchmarks for a PR labels Sep 18, 2024
Comment thread astropy/modeling/tests/test_compound.py Outdated
@astrofrog
astrofrog force-pushed the compound_analytical_deriv branch from 12b9ee7 to 33abdb9 Compare September 19, 2024 10:37
@astrofrog

Copy link
Copy Markdown
Member

Ok so just for the record as I am going to stop working on this for today: one of the remaining issues is related to fit_deriv for Polynomial2D which always returns a 2-D array as it flattens multi-dimensional input, whereas other models such as Gaussian2D don't. We need to be more robust to this in CompoundModel.fit_deriv.

@astrofrog
astrofrog force-pushed the compound_analytical_deriv branch from 8b1db7a to 9001005 Compare September 19, 2024 13:53
@astrofrog

Copy link
Copy Markdown
Member

The Polynomial2D thing was a red herring, fitters will just call fit_deriv with 1D arrays even when there are multiple dimensions, so this won't matter. We are down to three test failures: one because there is a difference of NaN vs Inf for the numerical vs analytical derivatives, and the other one is a slight numerical difference in the final model, which might just be a numerical issue, so might be safe to increase the relative tolerance?

@Cadair
Cadair force-pushed the compound_analytical_deriv branch from 9001005 to ce0ff74 Compare October 23, 2024 09:29
@Cadair
Cadair marked this pull request as ready for review October 23, 2024 12:22

@astrofrog astrofrog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me! Presumably @nden or @perrygreenfield should review too.

@WilliamJamieson WilliamJamieson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These look good to me.

One comment, we should look into supporting the computation through function composition. This should be doable as that is just chain rule.

@Cadair

Cadair commented Oct 24, 2024

Copy link
Copy Markdown
Member Author

Yes I would like to add more operators to this but these are the most critical and I don't have time to get others in for feature freeze.

@neutrinoceros

Copy link
Copy Markdown
Contributor

Looks to me that this can now go in. I've opened #17250 as a reminder of @WilliamJamieson's suggestion to expand the scope later on.
If no one complains about it I intend to merge this later today.

@astrofrog

Copy link
Copy Markdown
Member

On one hand it is 'just' the chain rule but on the other hand I think it requires implementing derivatives with respect to x for all functions if I recall? (As current derivatives are relative to parameters). @Cadair and I did start looking into it but it seemed non trivial so decided to leave it for later.

@Cadair

Cadair commented Oct 25, 2024

Copy link
Copy Markdown
Member Author

@neutrinoceros :shipit:❓

@neutrinoceros

Copy link
Copy Markdown
Contributor

Screenshot 2024-10-25 at 20 16 22

@neutrinoceros
neutrinoceros merged commit 2f5805a into astropy:main Oct 25, 2024
@neutrinoceros

Copy link
Copy Markdown
Contributor

This PR caused a regression in 7.0.0: #17509
Lessons learned:

  • I should remember to squash-merge more often
  • I should refrain from merging sizeable features literal hours ahead of feature freeze 🥶

@Cadair

Cadair commented Dec 6, 2024

Copy link
Copy Markdown
Member Author

I should refrain from merging sizeable features literal hours ahead of feature freeze 🥶

Better than hours afterwards ;)

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.

Implement deriv and fit_deriv for compound models

5 participants