Skip to content

perf: cache immutable @property on Fit classes #340

Description

@Jammy2211

Overview

37 @property methods across FitDataset, FitImaging, and FitInterferometer recompute on every access despite depending only on immutable constructor args. The worst case is FitImaging.model_data which rebuilds the entire inversion (~5-20s for Delaunay sources) on every access — and residual_map, chi_squared_map, log_likelihood all access it indirectly, creating a cascade of redundant recomputation.

Discovered during quick-update rendering profiling: accessing fit.model_data + fit.subtracted_images_of_planes_list cost 13.7s from two redundant inversion recomputations. Changing these to @cached_property eliminates the cascade.

Plan

  • Sweep all @property methods on FitDataset (PyAutoArray), FitImaging / FitInterferometer (PyAutoGalaxy + PyAutoLens) and change expensive ones to @cached_property
  • Focus on the recomputation cascades: model_dataresidual_mapchi_squared_maplog_likelihood, and blurred_imageprofile_subtracted_imagemodel_data
  • Verify no pickling or JAX pytree issues (audit confirms none of these classes use __getstate__ or pytree registration on Fit objects)
  • Leave cheap leaf-access properties (data, noise_map, mask, _xp) uncached
  • Run full test suites across all three repos + profiling scripts to confirm speedup
Detailed implementation plan

Affected Repositories

  • PyAutoArray (primary — FitDataset base class)
  • PyAutoGalaxy (FitImaging, FitInterferometer)
  • PyAutoLens (FitImaging override)

Branch Survey

Repository Current Branch Dirty?
PyAutoArray main clean
PyAutoGalaxy main CLAUDE.md
PyAutoLens main CLAUDE.md, README

Suggested branch: feature/cache-fit-properties

Properties to cache (37 total)

FitDataset (PyAutoArray) — 12 properties:
chi_squared, chi_squared_map, log_likelihood, log_evidence, log_likelihood_with_regularization, normalized_residual_map, noise_normalization, reduced_chi_squared, residual_map, residual_flux_fraction_map, signal_to_noise_map, figure_of_merit, grids

FitImaging (PyAutoGalaxy) — 8 properties:
blurred_image, profile_subtracted_image, model_data, galaxy_image_dict, galaxy_model_image_dict, model_images_of_galaxies_list, subtracted_images_of_galaxies_dict, subtracted_images_of_galaxies_list

FitImaging (PyAutoLens) — 5 properties:
blurred_image, profile_subtracted_image, model_data, subtracted_images_of_galaxies_dict, subtracted_signal_to_noise_maps_of_galaxies_dict, subtracted_images_of_planes_list

FitInterferometer (PyAutoGalaxy) — 6 properties:
profile_visibilities, profile_subtracted_visibilities, model_data, galaxy_image_dict, galaxy_model_visibilities_dict, model_visibilities_of_galaxies_list

Already cached (no change needed): inversion (AG+AL), galaxy_model_image_dict (AL), model_images_of_planes_list (AL)

Key Files

  • PyAutoArray/autoarray/fit/fit_dataset.py — base class, 12 changes
  • PyAutoGalaxy/autogalaxy/imaging/fit_imaging.py — AG FitImaging, 8 changes
  • PyAutoGalaxy/autogalaxy/interferometer/fit_interferometer.py — 6 changes
  • PyAutoLens/autolens/imaging/fit_imaging.py — AL FitImaging, 5 changes

Testing

  • pytest test_autoarray/ + pytest test_autogalaxy/ + pytest test_autolens/
  • autolens_profiling/quick_update/imaging.py and imaging_delaunay.py
  • Smoke tests across workspaces

Original Prompt

Click to expand starting prompt

Audit uncached @Property on Fit classes — cache immutable computed results

FitImaging.model_data is a plain @property that recomputes the entire inversion pipeline on every access. Any code that touches fit.model_data more than once (residuals, chi-squared, visualization, aggregator scripts) pays the full cost again. For a Delaunay source with 1500 mesh pixels, each recomputation is ~5-20s.

This was discovered during quick-update rendering profiling: accessing fit.model_data once and fit.subtracted_images_of_planes_list once (which internally accesses model_data again) cost 13.7s just from redundant recomputation.

FitImaging is constructed once and never mutated — the tracer, dataset, and settings are fixed at construction time. All computed properties should be safe to cache.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions