Skip to content

Faster DistributionMaker._set_rescaled_free_params for fits with many free parameters; iminuit strategy option - #981

Open
jpyanez wants to merge 3 commits into
icecube:masterfrom
jpyanez:faster-free-param-setting
Open

jpyanez wants to merge 3 commits into
icecube:masterfrom
jpyanez:faster-free-param-setting

Conversation

@jpyanez

@jpyanez jpyanez commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • DistributionMaker._set_rescaled_free_params cost grows as N³ in the number of free parameters N. With 142 free
    parameters it took 0.44 s per minimizer call, ~85 % of the call on our cluster's worker nodes. This PR makes it
    ~linear: 0.44 s → 6 ms. The fit result does not change (see "Why this is exact").
  • New iminuit option strategy in method_kwargs (default unchanged, Minuit's 1). With many free parameters,
    strategy 0 saves the full HESSE that MIGRAD runs at the end with strategy 1.
  • New unit test test_set_rescaled_free_params.

Context

We fit the IceCube/DeepCore 2023 public data release (PRD 108, 012014) with the daemonflux v2 flux model: 129 free
flux parameters + 13 oscillation/detector parameters = 142 free parameters. One octant took 6.1 h for ~30k
minimizer calls. Profiling showed that most of each call went into setting the parameter values, not into the
pipelines.

The problem

For every free parameter, the method evaluates pipeline.params up to three times
(pipeline.params.free.names, pipeline.params[name], pipeline.params.names). Pipeline.params is a property
that builds a new ParamSet from all stages. Each extend → update call looks up self.names, which is a
tuple built over all params. That is O(N²) per pipeline.params access, so O(N³) per call. For N = 142 this is
~1.2e8 generator steps per minimizer call (cProfile: param.py:1492(<genexpr>) called 116 M times in 31 calls).

The change

  1. pipeline.params is collected once per pipeline per call, and its free and all names are put in sets.
    The loop is then O(N).
  2. A param is skipped if this method set it to the same rescaled value in the previous call and its value
    object is still the one stored then. Minimizers move one parameter at a time in their numerical gradients,
    so most params are unchanged between calls. Setting a value costs ~0.2–0.4 ms (pint unit conversions and
    range checks), so this saves most of the remaining time when N is large.
  3. _fit_iminuit reads method_kwargs['strategy'] and sets Minuit.strategy. Nothing changes if the key is
    absent.

Why this is exact

  • (1) is a pure refactoring. Pipeline.params returns a new ParamSet that holds the same Param objects as the
    stages (ParamSet.update appends the objects, no copies). So setting _rescaled_value on the param of the set
    collected once is the same as setting it on a freshly collected set. The code path and the arithmetic are
    unchanged. The AttributeError for a param that is fixed in one pipeline and free in another is unchanged.
  • (2) only skips an assignment that would store the value the param already has. The skip needs both the same
    requested rescaled value and the same value object (is). Any other change of the value replaces that object:
    param.value = ..., reset_free(), update_params(), _rescaled_value = ... from elsewhere, or a new
    DistributionMaker. After such a change the param is set again. The cache is per (pipeline, param name), so
    params shared by pipelines are handled independently.
  • Checks:
    • chi2 identical to all printed digits (12) at random parameter points, before and after (142-param model);
    • chi2 at the published v1 best fit of the IceCube 2023 public data with daemonflux v1 (37 free params):
      207.077318459636 with master and with this branch;
    • test_DistributionMaker, test_analysis and test_constrained_minimization pass;
    • new test_set_rescaled_free_params: values reach every pipeline after a one-param step, after
      reset_free(), and after a param was set directly in one pipeline.

Timings

Login node (AMD EPYC 7452), 1 thread:

model free params _set_rescaled_free_params master this PR full minimizer call, master → PR
IceCube 2023 public data, daemonflux v1 37 48 ms 1.5 ms 120 → 103 ms (aeff_scale step)
same, daemonflux v2 sky map (local branch) 142 440 ms 6 ms (one param changed), 28 ms (all changed) 0.50 → 0.07 s (aeff_scale step)

Worker node (Intel Xeon E5-2623 v4), 142 free params, full minimizer call: 0.74 → 0.17 s on average over a
gradient. That figure also includes a small change in our local flux stage, not part of this PR; for an
aeff_scale step alone it is 0.62 → 0.08 s.

Whole fit, one theta23 octant, 142 free params, same worker type: 6.1 h with master (30194 calls, strategy 1).
With this PR and strategy: 0 (plus the small local flux-stage change above): 55.9 min (19245 calls), same minimum (χ² 200.2151 vs 200.2149, EDM 1.5e-5 against a
goal of 2e-5). Expected with this PR and strategy 1 (not run): ~1.4 h (30k calls × 0.17 s).

On the minimizer strategy: on a toy 142-parameter correlated quadratic with the same settings (limits, tol
1e-2), strategy 1 used 18580 calls, ~10800 of them for the final HESSE. Strategy 0 used 7711 calls and reached the
same minimum (EDM 1e-5), without an accurate covariance. HESSE can be run separately at the best fit when the
covariance is needed.

Notes

  • No change in behaviour or defaults for existing configs.
  • The 142-parameter numbers use the daemonflux v2 sky-map flux, which needs changes to the daemon_flux stage
    that are not in master yet. They are on a branch we will propose separately.

🤖 Generated with Claude Code

jpyanez and others added 3 commits November 26, 2026 03:54
…umber

_set_rescaled_free_params read pipeline.params (a new ParamSet collected from
all stages on every access) once or twice per free parameter, so its cost grew
with the cube of the number of params. Collect each pipeline's params once per
call, and skip params this method set to the same value last time (as long as
their value object is unchanged).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jpyanez
jpyanez marked this pull request as ready for review November 26, 2026 06:25

@JanWeldert JanWeldert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Apart from the one inline comment, this looks good to me.

# ~0.4 ms (unit conversions).
cached = self._rescaled_cache.get((ipipe, name))
if (cached is not None and cached[0] == rvalue
and cached[1] is param._value): # pylint: disable=protected-access

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For some reason cached[1] is param._value always fails for me even of the two are identical. Maybe switch to cached[1] == param._value? That worked for me and at the end you only want to check if the value is the same, not necessarily if the object is the same, right?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants