Repository navigation
perf: memoise prior_count bottom-up in AbstractPriorModel.parameterization - #1637
Merged
Jammy2211 merged 1 commit intoSep 17, 2026
Merged
Conversation
…ation (#1635) parameterization resolved every prefix of every leaf path and asked each model node for its own prior_count, which is a full recursive subtree walk, so the root and every ancestor were re-walked once per leaf beneath them: O(leaves x subtree). On a 561-prior nested model that was 3,244,174 path_instances_of_class calls and 6.2 s for six lines of text; on autogalaxy_workspace_test mge_group.py the two .info prints cost 7.25 s of a 19.4 s warm smoke run. A module-private _subtree_unique_priors computes each node's distinct-prior set once, bottom-up, memoised by object identity for the duration of the call, mirroring the traversal path_instances_of_class(obj, Prior, ignore_children=True) performs. The leaf listing, prefix enumeration and entry order handed to find_groups are unchanged, so the rendered text is byte-identical; each unique prefix is resolved once. Same model afterwards: 2,374 calls and 0.012 s. Tests: the pre-change algorithm is kept verbatim as an oracle and compared byte-for-byte over nested collections, shared priors, list children, tuple priors, constants and floats, zero-dimension children and integer attributes; a call-count test pins parameterization to one path_instances_of_class call per node. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TSJ72sveM7GrTcddMXzUkb
Jammy2211
deleted the
claude/parameterization-prior-count-blowup-jzsrj0
branch
September 17, 2026 16:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1635
Summary
AbstractPriorModel.parameterization(the(N=k)header block of everymodel.info) resolved every prefix of every leaf path and asked each model node for its ownprior_count.prior_countis a full recursive subtree walk, so the root and every ancestor were re-walked once per leaf beneath them: O(leaves × subtree). Onautogalaxy_workspace_testmge_group.pythe two.infoprints cost 7.25 s of a 19.4 s warm smoke run (763,555 recursivepath_instances_of_classcalls formodel.info, 2,438,255 forglobal_prior_model.info), and 25 of that gate's 39 smoke entries print an.info.This PR adds a module-private
_subtree_unique_priors(obj, memo)that computes each node's distinct-prior set once, bottom-up, memoised by object identity for the duration of the call. It mirrors exactly the traversalpath_instances_of_class(obj, Prior, ignore_children=True)performs (the chain behindprior_count), so the counts are the same by construction. The leaf listing, the prefix enumeration and the order entries reachfind_groupsare untouched, so the rendered text is byte-identical; each unique prefix is additionally resolved once instead of once per leaf.Micro-benchmark (synthetic nested model, 561 priors, 8 groups × 5 galaxies × 5 Gaussians with one shared prior; one cold
parameterizationcall):path_instances_of_classcallsfa2d540)The prompt's timing witness on
mge_group.py(model.infoandglobal_prior_model.infobyte-identical; warm smoke run 19.3 s → ≤ 14 s) needsautogalaxy_workspace_testand is for the local re-run / smoke gate; it could not run in this session.API Changes
None.
parameterization,prior_count,unique_prior_tuplesandpath_instances_of_classkeep their signatures and results; the only new symbol is the module-private helper_subtree_unique_priorsinautofit/mapper/prior_model/abstract.py.prior_countitself andmodel_tuples_with_type(which calls it once per model) are deliberately unchanged.See full details below.
Test Plan
test_autofit/mapper/test_parameterization.py: existing byte-exact expectations unchanged and passing (23 passed)_reference_parameterizationand compared byte-for-byte over eight model shapes (nested collections, a prior shared between two models, list children, tuple priors, a constant and a float beside a model, a zero-dimension child, an integer attribute, and the benchmark model)test_shared_prior_counted_once: a shared prior counts once at the parent (N=5, not 6)test_parameterization_walks_each_node_once:path_instances_of_classcalls duringparameterizationequal those of a single leaf walk (118 = 118)test_autofit/: 2813 passed, 48 skipped (Python 3.12, this container)autogalaxy_workspace_testscripts/multi_dataset/jax_likelihood/mge_group.pysmoke profile and confirm the.infotext is unchanged and the warm run is ≤ 14 sValidation checklist (--auto run — plan was not pre-approved)
Unattended: readytest_autofit/) · smoke n/a (no workspace in this session; the mge_group.py witness is the human's re-run) · review n/a (review faculty not run; the oracle and call-count tests are the equivalence evidence) · Heart n/a (pyauto-heartunavailable in this web session; the full suite stood in as the gate)Full API Changes (for automation & release notes)
Removed
Added
autofit.mapper.prior_model.abstract._subtree_unique_priors(obj, memo)— module-private; the distinct priors under a node, memoised by object identity across oneparameterizationcallMigration
Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code
https://claude.ai/code/session_01TSJ72sveM7GrTcddMXzUkb
Generated by Claude Code