Skip to content

perf: memoise prior_count bottom-up in AbstractPriorModel.parameterization - #1637

Merged
Jammy2211 merged 1 commit into
mainfrom
claude/parameterization-prior-count-blowup-jzsrj0
Sep 17, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
claude/parameterization-prior-count-blowup-jzsrj0

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #1635

Summary

AbstractPriorModel.parameterization (the (N=k) header block of every model.info) resolved every prefix of every leaf path and asked each model node for its own prior_count. prior_count is a full recursive subtree walk, so the root and every ancestor were re-walked once per leaf beneath them: O(leaves × subtree). On autogalaxy_workspace_test mge_group.py the two .info prints cost 7.25 s of a 19.4 s warm smoke run (763,555 recursive path_instances_of_class calls for model.info, 2,438,255 for global_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 traversal path_instances_of_class(obj, Prior, ignore_children=True) performs (the chain behind prior_count), so the counts are the same by construction. The leaf listing, the prefix enumeration and the order entries reach find_groups are 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 parameterization call):

wall time path_instances_of_class calls
before (fa2d540) 6.2 s 3,244,174
after 0.012 s 2,374

The prompt's timing witness on mge_group.py (model.info and global_prior_model.info byte-identical; warm smoke run 19.3 s → ≤ 14 s) needs autogalaxy_workspace_test and is for the local re-run / smoke gate; it could not run in this session.

API Changes

None. parameterization, prior_count, unique_prior_tuples and path_instances_of_class keep their signatures and results; the only new symbol is the module-private helper _subtree_unique_priors in autofit/mapper/prior_model/abstract.py. prior_count itself and model_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)
  • New oracle test: the pre-change algorithm kept verbatim as _reference_parameterization and 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)
  • New test_shared_prior_counted_once: a shared prior counts once at the parent (N=5, not 6)
  • New test_parameterization_walks_each_node_once: path_instances_of_class calls during parameterization equal those of a single leaf walk (118 = 118)
  • Full suite test_autofit/: 2813 passed, 48 skipped (Python 3.12, this container)
  • Human: re-run autogalaxy_workspace_test scripts/multi_dataset/jax_likelihood/mge_group.py smoke profile and confirm the .info text is unchanged and the warm run is ≤ 14 s

Validation checklist (--auto run — plan was not pre-approved)

  • Effective level: safe (header: safe, cap: refactor → safe); prompt declares Unattended: ready
  • Plan: on the issue (perf: bottom-up prior_count in parameterization (O(N·depth) walk) #1635), written at start, unmodified since
  • Gate: tests 2813 passed / 48 skipped (full test_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-heart unavailable in this web session; the full suite stood in as the gate)
  • Human: plan sound in hindsight?
  • Human: diff matches plan (no scope creep)?
  • Human: merge, amend, or reject — then log the outcome
Full API Changes (for automation & release notes)

Removed

  • none

Added

  • autofit.mapper.prior_model.abstract._subtree_unique_priors(obj, memo) — module-private; the distinct priors under a node, memoised by object identity across one parameterization call

Migration

  • none required; behaviour is byte-identical

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TSJ72sveM7GrTcddMXzUkb


Generated by Claude Code

…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 Jammy2211 added the pending-release PR queued for the next release build label Sep 17, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit 7c0e79a into main Sep 17, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the claude/parameterization-prior-count-blowup-jzsrj0 branch September 17, 2026 16:56
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
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.

perf: bottom-up prior_count in parameterization (O(N·depth) walk)

2 participants