Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

perf: drop use of awkward in yield uncertainty calculation #408

Merged
merged 19 commits into from
Jul 5, 2023

Conversation

ekauffma
Copy link
Contributor

@ekauffma ekauffma commented May 31, 2023

ak.sum is much more expensive than anticipated, so this changes the matrix operations in yield_stdev to use numpy instead, as per suggestion by @alexander-held

partially addresses #409, follows initial improvements done in #315

@alexander-held alexander-held changed the title fix: change awkward operations to numpy in yield_stdev perf: change awkward operations to numpy in yield_stdev May 31, 2023
@codecov
Copy link

codecov bot commented May 31, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (5064e38) 100.00% compared to head (ab4ceb5) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #408   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2069      2072    +3     
  Branches       334       337    +3     
=========================================
+ Hits          2069      2072    +3     
Impacted Files Coverage Δ
src/cabinetry/model_utils.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
ekauffma and others added 3 commits June 30, 2023 11:55
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
@agoose77
Copy link
Contributor

I had some perf suggestions after this popped up in my notifications:

Pre-convert the parameters, and use np.ufunc.at to avoid a temporary

# calculate the model distribution for every parameter varied up and down
# within the respective uncertainties
# ensure float for correct addition
float_parameters = parameters.astype(float)
for i_par in range(model.config.npars):
    # central parameter values, but one parameter varied within uncertainties
    up_pars = float_parameters.copy()
    np.add.at(up_pars, i_par, uncertainty)
    down_pars = float_parameters.copy()
    np.subtract.at(down_pars, i_par, uncertainty)

Pre-allocate stacked arrays and use in-place assignment (unsure of shapes here, so it's pseudo-code)

up_comb_next = np.empty(...)
up_comb_next[...] = up_comb
np.sum(up_comb, axis=0, out=up_comb_next[...])

Pre-allocate the up_variants and down_variations arrays, and as#-place

up_variations = np.empty(..., dtype=...)

for i_par in range(model.config.npars):
    ...
    up_variations[i] = up_yields

It might also be possible to do the above without the

up_yields = np.concatenate((up_comb, up_yields_channel_sum), axis=1)

step, i.e. directly assign the parts. I'm not sure.

ekauffma and others added 2 commits June 30, 2023 12:59
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
@alexander-held
Copy link
Member

Thanks a lot for the additional suggestions @agoose77! I moved them to #415. They look great, I would prefer that we address them in a separate PR to keep the changes a bit more atomic.

ekauffma and others added 3 commits July 4, 2023 16:31
ekauffma and others added 2 commits July 5, 2023 08:24
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
Co-authored-by: Alexander Held <45009355+alexander-held@users.noreply.github.com>
Copy link
Member

@alexander-held alexander-held left a comment

Choose a reason for hiding this comment

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

Looks all good to me, thanks a lot for getting this ready!

* drop use of awkward in yield uncertainty calculations
* reduced memory footprint and performance improvements for yield uncertainty calculations

@alexander-held alexander-held changed the title perf: change awkward operations to numpy in yield_stdev perf: drop use of awkward in yield uncertainty calculation Jul 5, 2023
@alexander-held alexander-held merged commit 7b9b6af into scikit-hep:master Jul 5, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants