Skip to content
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

Avoid copy in background model evaluation #3592

Merged
merged 4 commits into from Nov 18, 2021
Merged

Conversation

QRemy
Copy link
Contributor

@QRemy QRemy commented Nov 16, 2021

Avoid copy in background model evaluation in order to slightly improve performance.

@QRemy QRemy requested a review from adonath November 16, 2021 14:20
@QRemy QRemy added cleanup performance Performance improvement labels Nov 16, 2021
@codecov
Copy link

codecov bot commented Nov 16, 2021

Codecov Report

Merging #3592 (dc996bc) into master (12a87b2) will decrease coverage by 0.15%.
The diff coverage is 68.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3592      +/-   ##
==========================================
- Coverage   93.85%   93.69%   -0.16%     
==========================================
  Files         162      162              
  Lines       19345    19406      +61     
==========================================
+ Hits        18156    18183      +27     
- Misses       1189     1223      +34     
Impacted Files Coverage Δ
gammapy/datasets/map.py 93.14% <68.75%> (-0.54%) ⬇️
gammapy/irf/background.py 78.91% <0.00%> (-17.86%) ⬇️
gammapy/estimators/map/core.py 96.80% <0.00%> (-0.22%) ⬇️
gammapy/modeling/parameter.py 96.30% <0.00%> (-0.03%) ⬇️
gammapy/estimators/points/core.py 91.24% <0.00%> (+1.84%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 12a87b2...dc996bc. Read the comment docs.

@QRemy QRemy added this to In progress in gammapy.modeling via automation Nov 16, 2021
@QRemy QRemy added this to the v0.19 milestone Nov 16, 2021
if self.background_model and background:
values = self.background_model.evaluate_geom(geom=self.background.geom)
background = background * values
if self._background_cached is None:
self._background_cached = background * values
Copy link
Member

Choose a reason for hiding this comment

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

This caching does not make sense to me. Don't you want to check on the model parameters and avoid the recomputation if not needed?

Copy link
Contributor Author

@QRemy QRemy Nov 16, 2021

Choose a reason for hiding this comment

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

yes that could be added, for now I just did this as most of the time is spend in copy during background = background * values just avoiding this allow to reduce npred_background computation by about 40%.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@@ -767,8 +768,9 @@ def evaluate(self):
Background evaluated on the Map
"""
value = self.spectral_model(self.energy_center).value
back_values = self.map.data * value
return self.map.copy(data=back_values)
self._evaluation_cache.data = self.map.data * value
Copy link
Member

Choose a reason for hiding this comment

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

I'd say we don't need the caching on the .evaluate() level, this is only used for independent evaluation and not used during fitting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for the TemplateNPredModel the evaluator call .evaluate()

npred = self.model.evaluate()

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but we have caching on the MapEvaluator, so the effect on the performance should be completely negligible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

@adonath
Copy link
Member

adonath commented Nov 17, 2021

Thanks @QRemy, do you have a rough estimate how much this improve the performance in our benchmarks?

@QRemy
Copy link
Contributor Author

QRemy commented Nov 17, 2021

Thanks @QRemy, do you have a rough estimate how much this improve the performance in our benchmarks?

I tested it only on the analysis_3d_joint benchmark, it reduces npred_background computation time by about 50% (34s to 17s on my machine)

Copy link
Member

@adonath adonath left a comment

Choose a reason for hiding this comment

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

Thanks @QRemy, no further comments from my side.

@adonath adonath merged commit 3f7306a into gammapy:master Nov 18, 2021
gammapy.modeling automation moved this from In progress to Done Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup performance Performance improvement
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants