Improve fitting performance of most non-linear fitters#16673
Improve fitting performance of most non-linear fitters#16673nden merged 4 commits intoastropy:mainfrom
Conversation
|
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.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
f9855e9 to
f7cd5ec
Compare
|
The test fails are due to #16675 |
|
I've split out some of the changes here that relate to the Model API into #16677 |
b98648e to
65c65c7
Compare
|
Please use the new |
|
@pllim already done :) |
|
I think you need to rebase because the bot does not know about "perf" here. |
…de the objective function and avoiding modifying a model instance at every iteration.
f02719c to
390780d
Compare
|
My kingdom for that update and rebase button on the github web ui |
|
The asv benchmarks seem to be working! The one regression is a fluke. |
|
For convenience: |
|
The fitting results are all genuine and reliable (they are on the >1ms level). Not sure what's up with the units results but they are on the microsecond level so maybe just can't measure that in CI? |
|
Does this comment need to be resolved? 👉 #16673 (comment) |
|
No I was just commenting that the units asv tests were unreliable but not related to the changes here. This is good to go. |
|
This PR is breaking code that relies on documented behavior, specifically: https://docs.astropy.org/en/stable/api/astropy.modeling.fitting.LevMarLSQFitter.html#astropy.modeling.fitting.LevMarLSQFitter.objective_function It introduces additional parameters to |
|
Oh no! We should either
|
|
I have an idea to fix this, can take a look this morning unless @Cadair beats me to it. |
|
Thanks for the heads up @astrofrog ! I was about to give it a try myself, glad we could avoid another race condition :) |
|
Is #16982 @Cadair beating @astrofrog to it? |
|
Yes |
Add a changelog about the API change to objective_function in #16673
This PR significantly speeds up fitting of models for most of the non-linear fitters. When profiling this, we found that a lot of the overhead was in the fact that the fitters evaluate the model using call, which includes several time-consuming pre- and post-evaluation steps. However, in the non-linear fitters, the models always have scalar parameters and we have control over the inputs to the models, so these checks are unnecessary. We can therefore call Model.evaluate directly which speeds things up a lot. For compound models, this can speed up the fitting by factors of 4x or more.
For a simple 1D Gaussian unconstrained fit:
Fit example
This takes the run time for me from 3.02s on main to 2.19s on this PR.
With
use_min_max_constraintsand bounds set it goes from 3.64s to 2.2s.If you change the model being fit to
g = Gaussian1D() + Gaussian1D()then it takes 27.9s on main and 9.4s on this PR.