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
Performance Improvements #442
Comments
These would be great. Another low-hanging fruit improvement would be to not recomputing apec (and other xspec models) shape if only the normalization parameter was changed. We are fitting a sum of 10 apecs with the temperatures fixed in logarithmic steps, where the normalisations are linked to a formula whose parameters are varied. There is no real reason why this should be slow. So I'd propose model caching, not to recompute models with unchanged parameters. This can be achieved with memoization. I placed a practical benchmark here: https://github.com/JohannesBuchner/xray-fit-benchmark |
In general, models are cached in Sherpa using the Line 373 in 80b22a8
I'm not sure what @dtnguyen2 had in mind when he wrote that, but the first bullet should be solved by now. I agree with
Unfortunately, that's a little harder to implement in a general sense: For all XSPEC models, we pass all model parameters to the XSPEC function. There is no special markup which of those is the norm and which of those require a re-compute. We could make an informed guess that any parameter with the substring "norm" in it may not require a recomputation, but that's seem brittle (it would work for APEC, though). However, you should be able to achieve the same effect by changing your model definitions; instead of
Because there are no changes to any of the parameters for apec1 and apec2 in the fitting loop now, it should not be eveluated again. |
Thanks @hamogu!
Maybe sherpa could maintain a list of additive models for which "norm" exists and this is safe to do, and automatically wrap those, i.e., add a scale1d.norm and keep xspec.norm=1. With your workaround, the speed increased from 15 model evaluations per second to 20 model evaluations per second. I could not apply
|
Also see #1782
…On Fri, Nov 24, 2023 at 12:59 PM Johannes Buchner ***@***.***> wrote:
Thanks @hamogu <https://github.com/hamogu>!
Unfortunately, that's a little harder to implement in a general sense: For
all XSPEC models, we pass all model parameters to the XSPEC function. There
is no special markup which of those is the norm and which of those require
a re-compute. We could make an informed guess that any parameter with the
substring "norm" in it may not require a recomputation, but that's seem
brittle (it would work for APEC, though).
Maybe sherpa could maintain a list of additive models for which "norm"
exists and this is safe to do, and automatically wrap those, i.e., add a
scale1d.norm and keep xspec.norm=1.
With your workaround, the speed increased from 15 model evaluations per
second to 20 model evaluations per second.
I could not apply modelCacher1d to the xspec table model:
Traceback (most recent call last):
File "generateconst.py", line 28, in <module>
mymodel = xstbabs.galabso * (scale1d.torusnorm * modelCacher1d(torus) + scale1d.omninorm * modelCacher1d(omni))
File "/home/user/bin/ciao-4.13/lib/python3.7/site-packages/sherpa-4.13.0-py3.7-linux-x86_64.egg/sherpa/models/model.py", line 86, in modelCacher1d
cache_model.__name__ = func.__name__
File "/home/user/bin/ciao-4.13/lib/python3.7/site-packages/sherpa-4.13.0-py3.7-linux-x86_64.egg/sherpa/models/model.py", line 203, in __getattr__
NoNewAttributesAfterInit.__getattribute__(self, name)
AttributeError: 'XSTableModel' object has no attribute '__name__'
—
Reply to this email directly, view it on GitHub
<#442 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABW27R22GK3KDYGXTI2V5LYGCK4BAVCNFSM4E2JEHXKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBSGU3DIMZZHE3Q>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
@JohannesBuchner The caching for XSPEC models should already be applied without you doing things. @DougBurke is the master of knowing how it's done, but the goal is that, as a user, you don't have to do anything because we apply sensible caching as default. |
So, the cache code is complicated to understand as there's some complexities there I don't quite understand and have never had time to track down what's going on (last time I tried to look at how well the cache was working the results didn't make sense to me; perhaps it's time to look again). One thing I will say, and it echoes the suggestion in #442 (comment) is that the current cache code works at the python model class instance level - that is, if you have mdl1 = XSapec("m1")
mdl2 = Xsapec("m2") then there are caches for
|
Even with independent caches, since my model keeps all parameters except for the normalisation the same, it should be so slow. I cannot find where If that would wrap some selected models and handle norm internally, it would be solved. Another idea I had is that projection through the response could potentially be cached, if one applied a distributing transformation to the model tree ( Is there a way to use sherpa in a pedestrian fashion? Can I do something like:
What is the right way for that last line? |
FYI I don't have much time to spend time on so it'll take some time for me to respond. |
It took me some time to count how many times the word "time" can be used in a sentence with < 20 words.. |
But seriously, there is a trade-off between developer time and run-time. The suggestion in #442 (comment) should work in principle, but it's not simple in any sense. For that, it would be good to actually write down a few end-to-end workflows that we can benchmark and profile to identify where we should focus our efforts. Is it caching as discussed here? Parallelization? Avoiding temporary copies of big arrays? Move more Python Code to C? Avoid overhead of calling C by moving more code to Python/Numpy? Just-in-time compilation of Python code with Numba / JAX / ...? Find better optimizers / use analytical gradiends/ autograd / to reduce the number of model evaluations needed to get a good fit? Any of those can help in specific cases, but without profiling a set of representative use cases, I don't know which of those are high or low priority or have a high or low likelihood of success. |
Model evaluation
- Model caching, not to recompute models with frozen parameters
- Paralellization with intelligent parsing of the model expression
Slowness in fitting X-ray spectra
- issues in simultaneous fitting of multiple data (more general) - do not reevaluate the models with the
same parameters multiple times
Parallelization for MonCar and Nelder-Mead methods:
The text was updated successfully, but these errors were encountered: