Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions _profile_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ProfileCLI:
instrument: str | None
vmap_probe: bool
use_sparse_operator: bool
rect_mesh: str


def parse_profile_cli(default_config_name: str | None = None) -> ProfileCLI:
Expand Down Expand Up @@ -122,6 +123,25 @@ def parse_profile_cli(default_config_name: str | None = None) -> ProfileCLI:
),
)

parser.add_argument(
"--rect-mesh",
choices=("bilinear", "rtu"),
default="bilinear",
help=(
"Which adaptive rectangular mesh family the cell profiles "
"(PyAutoArray#462 split): 'bilinear' — "
"RectangularBilinearAdaptDensity/AdaptImage, the empirical "
"rank-CDF workspace default and CPU-speed campaign target — or "
"'rtu' — RectangularRTUAdaptDensity/AdaptImage, the kernel-CDF "
"advanced/GPU/gradient meshes (the pre-split behaviour, so "
"'--rect-mesh rtu' reproduces pre-split recorded results). "
"Cells that read this flag resolve the classes via "
"``rect_mesh_classes`` and embed ``rect_mesh`` in the result "
"JSON; '_rtu' is appended to the output basename so the two "
"families' results never clobber each other."
),
)

args, _unknown = parser.parse_known_args()
config_name = args.config_name or default_config_name
output_dir = Path(args.output_dir).resolve() if args.output_dir else None
Expand All @@ -132,6 +152,26 @@ def parse_profile_cli(default_config_name: str | None = None) -> ProfileCLI:
instrument=args.instrument,
vmap_probe=bool(args.vmap_probe),
use_sparse_operator=bool(args.sparse),
rect_mesh=args.rect_mesh,
)


def rect_mesh_classes(cli: ProfileCLI):
"""Resolve the explicit adaptive rectangular mesh classes for this run.

Returns ``(density_cls, image_cls)`` for ``cli.rect_mesh``:
``RectangularBilinearAdaptDensity/AdaptImage`` (rank-CDF, the workspace
default) or ``RectangularRTUAdaptDensity/AdaptImage`` (kernel-CDF, the
pre-split behaviour). Imports autolens lazily so ``_profile_cli`` stays
importable without the modelling stack.
"""
import autolens as al

if cli.rect_mesh == "rtu":
return al.mesh.RectangularRTUAdaptDensity, al.mesh.RectangularRTUAdaptImage
return (
al.mesh.RectangularBilinearAdaptDensity,
al.mesh.RectangularBilinearAdaptImage,
)


Expand Down Expand Up @@ -211,6 +251,10 @@ def resolve_output_paths(
basename = f"{cell}_{cli.config_name}"
if cli.use_sparse_operator:
basename = f"{basename}_sparse"
if cli.rect_mesh == "rtu":
# Keep the two rectangular-mesh families' results disjoint; the
# bilinear default keeps the unsuffixed (pre-split) filenames.
basename = f"{basename}_rtu"
return results_dir / f"{basename}.json", results_dir / f"{basename}.png"


Expand Down
12 changes: 7 additions & 5 deletions scripts/imaging/likelihood_breakdown/pixelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _profiling_root() -> _Path:
auto_simulate_if_missing,
device_info_dict,
parse_profile_cli,
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -270,12 +271,12 @@ def jit_profile(func, label, *args, n_repeats=10):

lens = af.Model(al.Galaxy, redshift=0.5, bulge=lens_bulge, mass=mass, shear=shear)

# ``RectangularAdaptImage`` weights mesh pixels by the lensed-source
# ``RectangularBilinearAdaptImage`` weights mesh pixels by the lensed-source
# adapt image — the production-grade alternative to the coordinate-
# density-only ``RectangularAdaptDensity``. Adapt image is loaded /
# density-only ``RectangularBilinearAdaptDensity``. Adapt image is loaded /
# cached below; the same shape and regularization are kept.
pixelization = al.Pixelization(
mesh=al.mesh.RectangularAdaptImage(shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
mesh=rect_mesh_classes(_cli)[1](shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
regularization=al.reg.Constant(coefficient=1.0),
)

Expand Down Expand Up @@ -324,7 +325,7 @@ def jit_profile(func, label, *args, n_repeats=10):

# ---------------------------------------------------------------------------
# 4. Adapt image — PSF-convolved lensed-source image used by
# ``RectangularAdaptImage`` to weight mesh pixels.
# ``RectangularBilinearAdaptImage`` to weight mesh pixels.
# ---------------------------------------------------------------------------

print("\n--- Adapt image (lensed source) ---")
Expand Down Expand Up @@ -557,7 +558,7 @@ def overlay_grid_raw_fn(relocated_grid_raw):
pixelization_obj = instance.galaxies.source.pixelization

with timer.section("interpolation_and_mapper"):
# ``RectangularAdaptImage.interpolator_from`` consumes ``adapt_data`` to
# ``RectangularBilinearAdaptImage.interpolator_from`` consumes ``adapt_data`` to
# build the per-pixel weight map; pass the lensed-source image through.
interpolator = pixelization_obj.mesh.interpolator_from(
source_plane_data_grid=relocated_grid,
Expand Down Expand Up @@ -973,6 +974,7 @@ def compute_log_evidence(
"image_pixels_masked": int(n_image_pixels),
"over_sampled_pixels": int(n_over_sampled_pixels),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"inversion_path": "sparse" if _cli.use_sparse_operator else "dense",
},
Expand Down
4 changes: 3 additions & 1 deletion scripts/imaging/likelihood_breakdown/pixelization_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _profiling_root() -> _Path:
device_info_dict,
parse_profile_cli,
record_pinned_check,
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -169,7 +170,7 @@ def _profiling_root() -> _Path:
lens = af.Model(al.Galaxy, redshift=0.5, bulge=lens_bulge, mass=mass, shear=shear)

pixelization = al.Pixelization(
mesh=al.mesh.RectangularAdaptDensity(shape=mesh_shape),
mesh=rect_mesh_classes(_cli)[0](shape=mesh_shape),
regularization=al.reg.Constant(coefficient=1.0),
)

Expand Down Expand Up @@ -339,6 +340,7 @@ def one_decomposed_evaluation() -> tuple[dict[str, float], float]:
"image_pixels_masked": int(n_image_pixels),
"over_sampled_pixels": int(n_over_sampled_pixels),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"inversion_path": "sparse_numba",
"use_jax": False,
Expand Down
13 changes: 8 additions & 5 deletions scripts/imaging/likelihood_runtime/pixelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def _profiling_root() -> _Path:
device_info_dict,
parse_profile_cli,
record_pinned_check, # noqa: E402
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -275,12 +276,12 @@ def jit_profile(func, label, *args, n_repeats=10):

lens = af.Model(al.Galaxy, redshift=0.5, bulge=lens_bulge, mass=mass, shear=shear)

# ``RectangularAdaptImage`` weights mesh pixels by the lensed-source
# ``RectangularBilinearAdaptImage`` weights mesh pixels by the lensed-source
# adapt image — the production-grade alternative to the coordinate-
# density-only ``RectangularAdaptDensity``. Adapt image is loaded /
# density-only ``RectangularBilinearAdaptDensity``. Adapt image is loaded /
# cached below; the same shape and regularization are kept.
pixelization = al.Pixelization(
mesh=al.mesh.RectangularAdaptImage(shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
mesh=rect_mesh_classes(_cli)[1](shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
regularization=al.reg.Constant(coefficient=1.0),
)

Expand Down Expand Up @@ -329,7 +330,7 @@ def jit_profile(func, label, *args, n_repeats=10):

# ---------------------------------------------------------------------------
# 4. Adapt image — PSF-convolved lensed-source image used by
# ``RectangularAdaptImage`` to weight mesh pixels. Loads ``lensed_source.fits``
# ``RectangularBilinearAdaptImage`` to weight mesh pixels. Loads ``lensed_source.fits``
# from the dataset directory if present, otherwise computes it from the
# truth tracer and caches the file for sibling scripts on the same
# instrument.
Expand Down Expand Up @@ -421,6 +422,7 @@ def full_pipeline_from_params(params_tree):
"image_pixels_masked": int(n_image_pixels),
"over_sampled_pixels": int(n_over_sampled_pixels),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"inversion_path": "sparse" if _cli.use_sparse_operator else "dense",
},
Expand Down Expand Up @@ -626,6 +628,7 @@ def full_pipeline_from_params(params_tree):
"image_pixels_masked": int(n_image_pixels),
"over_sampled_pixels": int(n_over_sampled_pixels),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"inversion_path": "sparse" if _cli.use_sparse_operator else "dense",
},
Expand Down Expand Up @@ -654,7 +657,7 @@ def full_pipeline_from_params(params_tree):
# Regression assertion — realistic-scale deterministic log-evidence
# ===================================================================
#
# RectangularAdaptImage at prior medians anchors the regression on the
# RectangularBilinearAdaptImage at prior medians anchors the regression on the
# *eager* FitImaging value (deterministic to fp64 noise). The full-pipeline
# single-JIT / vmap paths agree with eager to ~1e-3 only: adaptive mesh
# weighting amplifies fp accumulation in Cholesky / log_det on the bigger
Expand Down
7 changes: 5 additions & 2 deletions scripts/imaging/likelihood_runtime/pixelization_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

The model mirrors the production CPU-fast fiducial: MGE-60 lens light (linear,
held at prior medians), Isothermal + shear mass, and a rectangular
``RectangularAdaptDensity`` source pixelization with constant regularization
``RectangularBilinearAdaptDensity`` source pixelization with constant regularization
(or ``RectangularRTUAdaptDensity`` via ``--rect-mesh rtu`` — PyAutoArray#462 split)
(the workspace example's mesh; 28x28 = 784 source pixels).

Notes:
Expand Down Expand Up @@ -82,6 +83,7 @@ def _profiling_root() -> _Path:
device_info_dict,
parse_profile_cli,
record_pinned_check,
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -211,7 +213,7 @@ def section(self, label: str):
lens = af.Model(al.Galaxy, redshift=0.5, bulge=lens_bulge, mass=mass, shear=shear)

pixelization = al.Pixelization(
mesh=al.mesh.RectangularAdaptDensity(shape=mesh_shape),
mesh=rect_mesh_classes(_cli)[0](shape=mesh_shape),
regularization=al.reg.Constant(coefficient=1.0),
)

Expand Down Expand Up @@ -324,6 +326,7 @@ def section(self, label: str):
"image_pixels_masked": int(n_image_pixels),
"over_sampled_pixels": int(n_over_sampled_pixels),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"inversion_path": "sparse_numba",
"use_jax": False,
Expand Down
4 changes: 2 additions & 2 deletions scripts/imaging/parallel_scaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ submit scripts are `hpc/batch_cpu/submit_parallel_scaling_pixelization_numba_euc
harness deliberately excludes.

> **Fiducial note:** the Findings section below is for the Rectangular
> (28×28 `RectangularAdaptDensity`) fiducial. The production campaign fiducial
> (28×28 `RectangularBilinearAdaptDensity`) fiducial. The production campaign fiducial
> is Delaunay + Hilbert-1250 image-mesh (the `delaunay_numba` runtime/breakdown
> cells): run it with `--mesh delaunay` (artifacts land as
> `delaunay_numba_scaling_<instrument>`). First Delaunay pass (euclid, 4-core
Expand All @@ -54,7 +54,7 @@ submit scripts are `hpc/batch_cpu/submit_parallel_scaling_pixelization_numba_euc

## Findings — first pass, 2026-08-20 (v2026.8.17.1, local 8-core WSL box)

Fiducial: 28×28 `RectangularAdaptDensity` + `Constant` reg, MGE-60 linear lens
Fiducial: 28×28 `RectangularBilinearAdaptDensity` + `Constant` reg, MGE-60 linear lens
light, mask 3.5". Full JSONs under `results/{runtime,breakdown,parallel_scaling}/imaging/`.

**Per-evaluation cost** (`likelihood_runtime` cell):
Expand Down
8 changes: 5 additions & 3 deletions scripts/imaging/parallel_scaling/pixelization_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
------
``results/parallel_scaling/imaging/pixelization_numba_scaling_<instrument>_v<version>.{json,png}``

``--mesh`` selects the fiducial: ``rectangular`` (28x28 ``RectangularAdaptDensity``,
``--mesh`` selects the fiducial: ``rectangular`` (28x28 adaptive rectangular —
``RectangularBilinearAdaptDensity`` by default, ``--rect-mesh rtu`` for the kernel-CDF variant,
the ``pixelization_numba`` siblings) or ``delaunay`` (Hilbert-1250 Delaunay +
ConstantSplit, the ``delaunay_numba`` siblings — the production campaign
fiducial; artifacts are written as ``delaunay_numba_scaling_<instrument>``).
Expand Down Expand Up @@ -105,6 +106,7 @@ def _profiling_root() -> _Path:
auto_simulate_if_missing,
device_info_dict,
parse_profile_cli,
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -205,10 +207,10 @@ def _profiling_root() -> _Path:
mesh_pixels_yx = 28
mesh_shape = (mesh_pixels_yx, mesh_pixels_yx)
n_source_pixels = mesh_pixels_yx * mesh_pixels_yx
mesh_label = f"rectangular_adapt_density_{mesh_pixels_yx}x{mesh_pixels_yx}"
mesh_label = f"rectangular_{_cli.rect_mesh}_adapt_density_{mesh_pixels_yx}x{mesh_pixels_yx}"

pixelization = al.Pixelization(
mesh=al.mesh.RectangularAdaptDensity(shape=mesh_shape),
mesh=rect_mesh_classes(_cli)[0](shape=mesh_shape),
regularization=al.reg.Constant(coefficient=1.0),
)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Budget/batching lessons as per the ``knn`` cell: 16 starts, 3000-step budget
(a long plateau is a reg mode, not convergence), ``batch_size=4`` mandatory
(unbatched 16-start pixelized jvp ~58 GB). Model: MGE lens light + free broad
Isothermal/shear + ``RectangularAdaptImage`` source + free ``Constant`` reg —
Isothermal/shear + ``RectangularRTUAdaptImage`` source + free ``Constant`` reg —
the SLaM ``source_pix[1]`` shape (see ``_setup._pixelization_model``).
"""

Expand Down
2 changes: 1 addition & 1 deletion scripts/imaging/searches/nautilus/pixelization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""First-class af.Nautilus search profiling — imaging pixelization.

Drives a full ``af.Nautilus`` fit on an MGE lens + RectangularAdaptImage
Drives a full ``af.Nautilus`` fit on an MGE lens + RectangularRTUAdaptImage
pixelization source imaging model. Uses a truth-derived adapt image cached
next to the dataset. See ``searches/README.md`` for caveats.
"""
Expand Down
6 changes: 4 additions & 2 deletions scripts/interferometer/likelihood_runtime/pixelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _profiling_root() -> _Path:
device_info_dict,
parse_profile_cli,
record_pinned_check, # noqa: E402
rect_mesh_classes,
resolve_output_paths,
)

Expand Down Expand Up @@ -260,7 +261,7 @@ def _build_transformer(uv_wavelengths, real_space_mask):

pixelization = af.Model(
al.Pixelization,
mesh=al.mesh.RectangularAdaptImage(shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
mesh=rect_mesh_classes(_cli)[1](shape=mesh_shape, weight_power=1.0, weight_floor=0.0),
regularization=al.reg.Constant(coefficient=regularization_coefficient),
)

Expand Down Expand Up @@ -309,7 +310,7 @@ def _build_transformer(uv_wavelengths, real_space_mask):
print(f" Reg. coefficient: {regularization_coefficient}")

# ---------------------------------------------------------------------------
# 4b. Adapt image — drives ``RectangularAdaptImage`` mesh weighting
# 4b. Adapt image — drives ``RectangularBilinearAdaptImage`` mesh weighting
# ---------------------------------------------------------------------------

print("\n--- Adapt image (lensed source) ---")
Expand Down Expand Up @@ -561,6 +562,7 @@ def full_pipeline_from_params(params_tree):
"real_space_shape": list(real_space_shape),
"visibilities": int(n_visibilities),
"mesh_shape": list(mesh_shape),
"rect_mesh": _cli.rect_mesh,
"source_pixels": int(n_source_pixels),
"regularization_coefficient": regularization_coefficient,
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/misc/likelihood_breakdown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ If, on the other hand, the two numbers agree closely, the per-step bars are a fa
| Script | Dataset class | Source model | Notes |
|--------|--------------|--------------|-------|
| `imaging/mge.py` | Imaging | MGE linear bulge | Linear MGE source; 8-step pipeline. |
| `imaging/pixelization.py` | Imaging | RectangularAdaptImage | 13-step pipeline incl. mesh + regularisation. |
| `imaging/pixelization.py` | Imaging | RectangularBilinearAdaptImage (`--rect-mesh rtu` for the RTU variant) | 13-step pipeline incl. mesh + regularisation. |
| `imaging/delaunay.py` | Imaging | DelaunayBrightnessImage | 13-step pipeline; Hilbert-curve mesh. |
| `interferometer/delaunay.py` | Interferometer | DelaunayBrightnessImage + sparse-DFT | 11-step pipeline. The transform-mapping-matrix step is the interferometer-specific replacement for imaging's PSF convolution. |
| `datacube/delaunay.py` | Datacube | DelaunayBrightnessImage × N channels | 8-step pipeline. Channel-invariant steps profiled once; channel-variant steps profiled on channel 0 and multiplied by `N_channels` for the cube cost. |
Expand Down
2 changes: 1 addition & 1 deletion scripts/misc/likelihood_runtime/OPTIMIZATION_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ A "—" entry means the row failed or wasn't run; see the cell's notes.
---

## imaging/pixelization
*Rectangular `RectangularAdaptImage` mesh + Constant regularization. 35×35
*Rectangular `RectangularRTUAdaptImage` (kernel-CDF; measured pre-split as `RectangularAdaptImage`) mesh + Constant regularization. 35×35
source pixels = 1225 source mesh nodes. Default HST-resolution dataset
(0.05″/px, mask radius 3.5″, 15 361 masked pixels, 17 980 over-sampled).*

Expand Down
4 changes: 2 additions & 2 deletions scripts/misc/likelihood_runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ Empirical effect: mp on CPU consistently helps 7 – 23 %. On GPU it's more vari
| Script | Dataset class | Source model |
|--------|--------------|--------------|
| `imaging/mge.py` | Imaging | MGE linear bulge |
| `imaging/pixelization.py` | Imaging | RectangularAdaptImage |
| `imaging/pixelization.py` | Imaging | RectangularBilinearAdaptImage (`--rect-mesh rtu` for the kernel-CDF RTU variant) |
| `imaging/delaunay.py` | Imaging | DelaunayBrightnessImage |
| `interferometer/mge.py` | Interferometer | MGE linear bulge; `TransformerNUFFT` (nufftax) default, `--use-dft` opt-in |
| `interferometer/pixelization.py` | Interferometer | RectangularAdaptImage + `apply_sparse_operator(use_jax=True)` |
| `interferometer/pixelization.py` | Interferometer | RectangularBilinearAdaptImage (`--rect-mesh rtu` available) + `apply_sparse_operator(use_jax=True)` |
| `interferometer/delaunay.py` | Interferometer | DelaunayBrightnessImage + `apply_sparse_operator(use_jax=True)` |
| `datacube/delaunay.py` | Datacube (34-channel cube) | DelaunayBrightnessImage per channel, shared lens model |
| `point_source/image_plane.py` | Point source | Image-plane χ² via `PointSolver` |
Expand Down
4 changes: 2 additions & 2 deletions scripts/misc/pipeline_resume/slam_resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def source_pix_1(settings_search, dataset, source_lp_result, mesh_shape, n_live,
redshift=source_lp_result.instance.galaxies.source.redshift,
pixelization=af.Model(
al.Pixelization,
mesh=af.Model(al.mesh.RectangularAdaptDensity, shape=mesh_shape),
mesh=af.Model(al.mesh.RectangularBilinearAdaptDensity, shape=mesh_shape),
regularization=al.reg.Adapt,
),
),
Expand Down Expand Up @@ -321,7 +321,7 @@ def source_pix_2(
redshift=source_lp_result.instance.galaxies.source.redshift,
pixelization=af.Model(
al.Pixelization,
mesh=af.Model(al.mesh.RectangularAdaptImage, shape=mesh_shape),
mesh=af.Model(al.mesh.RectangularBilinearAdaptImage, shape=mesh_shape),
regularization=al.reg.Adapt,
),
),
Expand Down
Loading
Loading