diff --git a/autoarray/config/general.yaml b/autoarray/config/general.yaml index 781576325..bf028e1c0 100644 --- a/autoarray/config/general.yaml +++ b/autoarray/config/general.yaml @@ -21,7 +21,7 @@ inversion: interferometer_numba_nnz_per_source_max: null # How a NumPy (xp=np) single-mapper interferometer inversion chooses between the numba `direct_conv` curvature path (InversionInterferometerSparseNumba) and the FFT one (InversionInterferometerSparse). null (default) = cost model: predicts both routes' single-thread time from source pixels S, total mapping non-zeros and the extent rectangle (FFT ~ S x rfft2+irfft2 cost of the padded (2ny,2nx) extent, direct_conv ~ nnz x ny x nx) and picks the faster; calibrated on autolens_profiling#226 and the Aris 512x512 mask (PyAutoArray#628). A positive value is a hard gate in mean non-zeros per source column (mapper.pix_sizes_for_sub_slim_index.sum() / mapper.params): numba at or below it, FFT above (the pre-#628 behaviour, default was 60.0). Set 0 to disable the numba path entirely. interferometer_route: null # Force the interferometer curvature route for benchmarking: null (default) routes by interferometer_numba_nnz_per_source_max; "fft" always uses the FFT route; "numba" uses the numba direct_conv route whenever it can represent the inversion (NumPy, one mapper, no linear functions, no over-sampling), overriding the gate and the 0 kill switch. "matrix_free" uses InversionInterferometerMatrixFree, which never forms the dense curvature or regularization matrix (PCG / interior-point solve, stochastic Lanczos log-det); it needs NumPy, one RectangularUniform mapper with MaternSparse regularization, no linear functions and (positive-only) use_edge_zeroed_pixels false, raises on anything else, and has no dense attributes such as reconstruction_noise_map (PyAutoArray#657). interferometer_direct_conv_symmetric_max_work_fraction: 0.7 # When the serial (numba parallel: false) direct_conv curvature route runs, use the symmetric row-restricted kernel (curvature_direct_conv_symmetric: source columns ranked by their top extent row, each convolving only the extent rows later-ranked columns span and only each row's mask x-range, then a mirror pass) when its geometry work fraction (its convolution work / the full kernel's) is at or below this value. Same F to round-off (computed entries bit-identical). Synthetic Aris-512 geometry (compact columns): f ~0.41, 1.81-1.90x single-thread on RAL; Aris's real 512 Delaunay mesh (multiply imaged, tall columns): f ~0.51, ~1.46x measured (PyAutoArray#635). The route cost model scales t_dc by min(1, 1.32 x work fraction), so the kernel wins below f ~0.76; 0.7 keeps a predicted gain >= 1.08x. Set 0 to always use the full kernel. The parallel kernel is unaffected. - lazy_sparse_operator: "off" # Whether a dataset builds its sparse operator lazily on the first NumPy pixelized fit that needs one, when none is attached (Imaging/Interferometer.sparse_operator_for). "off" (default this release): nothing is built and such fits take the dense mapping-matrix route, as before. "auto": the numba sparse operator (imaging) or the NUFFT w-tilde operator (interferometer) is built once, cached privately on the dataset and reused; never under JAX, inside a multiprocessing worker, with an explicit dense setup, or (interferometer) when the real and imaginary noise differ. PYAUTO_SETUP=auto also turns it on. Modeling applies its setup eagerly before forking and does not rely on this. + lazy_sparse_operator: "auto" # Whether a dataset builds its sparse operator lazily on the first NumPy pixelized fit that needs one, when none is attached (Imaging/Interferometer.sparse_operator_for). "off": nothing is built and such fits take the dense mapping-matrix route. "auto" (default this release): the numba sparse operator (imaging) or the NUFFT w-tilde operator (interferometer) is built once, cached privately on the dataset and reused; never under JAX, inside a multiprocessing worker, with an explicit dense setup, or (interferometer) when the real and imaginary noise differ. PYAUTO_SETUP=auto also turns it on when this is "off". Modeling applies its setup eagerly before forking and does not rely on this. lazy_sparse_operator_visibility_budget: null # The visibility count above which the lazy interferometer build accumulates the operator chunk by chunk (apply_sparse_operator_from_chunks) instead of in one shot, bounding the NUFFT spreader buffer (~K x nspread^2 complex128: ~3 GB at 1e6 visibilities, ~15 GB at 5e6). null = 1e6. numba: use_numba: true # If True, use numba-accelerated mapper and Sibson interpolation paths when eligible. diff --git a/autoarray/dataset/imaging/dataset.py b/autoarray/dataset/imaging/dataset.py index e6895d067..7e8de5e62 100644 --- a/autoarray/dataset/imaging/dataset.py +++ b/autoarray/dataset/imaging/dataset.py @@ -816,9 +816,9 @@ def sparse_operator_for(self, xp=np, needs_operator: bool = True, settings=None) This lets calculations outside a model-fit -- a plain `FitImaging(dataset, ...)`, an aggregator reload, `analysis.fit_from(...)` before a search -- use the sparse operator modeling attaches eagerly (via `apply_setup()`), instead of the slow, memory-heavy dense - mapping-matrix route. The policy is off by default (see - `autoarray.dataset.lazy_operator.lazy_sparse_operator_enabled`), in which case this - returns the attached operator or `None`, exactly as before. + mapping-matrix route. The policy is on by default (see + `autoarray.dataset.lazy_operator.lazy_sparse_operator_enabled`); switched off + (`lazy_sparse_operator: "off"`), this returns the attached operator or `None`. Nothing is built, and `None` is returned, when `needs_operator` is False, the policy is off, `xp` is not NumPy or the noise-map is a JAX tracer, or the dataset has an explicit diff --git a/autoarray/dataset/interferometer/dataset.py b/autoarray/dataset/interferometer/dataset.py index 807645906..b20ff8d11 100644 --- a/autoarray/dataset/interferometer/dataset.py +++ b/autoarray/dataset/interferometer/dataset.py @@ -1116,9 +1116,9 @@ def sparse_operator_for(self, xp=np, needs_operator: bool = True, settings=None) This lets calculations outside a model-fit -- a plain `FitInterferometer(dataset, ...)`, an aggregator reload, `analysis.fit_from(...)` before a search -- use the sparse operator modeling attaches eagerly (via `apply_setup()`), instead of the slow, memory-heavy dense - mapping-matrix route. The policy is off by default (see - `autoarray.dataset.lazy_operator.lazy_sparse_operator_enabled`), in which case this - returns the attached operator or `None`, exactly as before. + mapping-matrix route. The policy is on by default (see + `autoarray.dataset.lazy_operator.lazy_sparse_operator_enabled`); switched off + (`lazy_sparse_operator: "off"`), this returns the attached operator or `None`. The build uses this dataset's own data and noise-map, as `apply_sparse_operator()` does (`method="nufft"`, `use_jax=False`, so it never imports JAX). Above the visibility budget diff --git a/autoarray/dataset/lazy_operator.py b/autoarray/dataset/lazy_operator.py index 9125a5f8f..06b79fdfc 100644 --- a/autoarray/dataset/lazy_operator.py +++ b/autoarray/dataset/lazy_operator.py @@ -46,9 +46,9 @@ def lazy_sparse_operator_enabled(settings=None) -> bool: - An explicit `Settings(lazy_sparse_operator=...)` decides on its own (``"auto"`` on, ``"off"`` off), so a script can switch the lazy build off whatever the environment says. - Otherwise the policy is on if `general.yaml` `inversion: lazy_sparse_operator` is ``"auto"``, - or the environment variable `PYAUTO_SETUP` is set to ``auto`` (parsed by + (the packaged default), or the environment variable `PYAUTO_SETUP` is set to ``auto`` (parsed by `autonerves.environment.parse_setup_intent`). An unset or empty `PYAUTO_SETUP` does not turn - it on: the default is off this release. + on a config of ``"off"``. Parameters ---------- diff --git a/autoarray/inversion/inversion/imaging_numba/sparse.py b/autoarray/inversion/inversion/imaging_numba/sparse.py index f360fe18e..27a54a359 100644 --- a/autoarray/inversion/inversion/imaging_numba/sparse.py +++ b/autoarray/inversion/inversion/imaging_numba/sparse.py @@ -262,15 +262,15 @@ def _data_vector_multi_mapper(self) -> np.ndarray: for mapper in self.cls_list_from(cls=Mapper): - rows, cols, vals = mapper.sparse_triplets_data - data_vector_mapper = ( inversion_imaging_numba_util.data_vector_via_psf_weighted_data_from( psf_weighted_data=self.psf_weighted_data, - rows=rows, - cols=cols, - vals=vals, - S=mapper.params, + data_to_pix_unique=np.array( + mapper.unique_mappings.data_to_pix_unique + ), + data_weights=np.array(mapper.unique_mappings.data_weights), + pix_lengths=np.array(mapper.unique_mappings.pix_lengths), + pix_pixels=mapper.params, ) ) diff --git a/autoarray/settings.py b/autoarray/settings.py index 02a333731..c9dc67619 100644 --- a/autoarray/settings.py +++ b/autoarray/settings.py @@ -278,13 +278,14 @@ def __init__( lazy_sparse_operator Whether a dataset builds its sparse operator lazily, on the first NumPy pixelized fit that needs one, when none is attached (`Imaging.sparse_operator_for` / - `Interferometer.sparse_operator_for`). `None` (default) reads the packaged value ``"off"``. - - - ``"off"`` (default) -- nothing is built; a dataset without an attached operator takes the - dense mapping-matrix route, as before. - - ``"auto"`` -- the dataset builds the numba sparse operator (imaging) or the NUFFT w-tilde - operator (interferometer) once, caches it privately and reuses it. `PYAUTO_SETUP=auto` also - turns this on, unless this argument is given explicitly (see + `Interferometer.sparse_operator_for`). `None` (default) reads the packaged value ``"auto"``. + + - ``"off"`` -- nothing is built; a dataset without an attached operator takes the dense + mapping-matrix route. + - ``"auto"`` (default) -- the dataset builds the numba sparse operator (imaging) or the NUFFT + w-tilde operator (interferometer) once, caches it privately and reuses it; never under JAX or + inside a multiprocessing worker. When the config is ``"off"``, `PYAUTO_SETUP=auto` turns it + on, unless this argument is given explicitly (see `autoarray.dataset.lazy_operator.lazy_sparse_operator_enabled`). """ self.use_mixed_precision = use_mixed_precision @@ -540,19 +541,19 @@ def interferometer_direct_conv_symmetric_max_work_fraction(self) -> float: @property def lazy_sparse_operator(self) -> str: """ - The lazy sparse operator policy: ``"off"`` (default) or ``"auto"``. + The lazy sparse operator policy: ``"auto"`` (default) or ``"off"``. See the constructor docstring. YAML 1.1 reads an unquoted ``off`` / ``on`` as a boolean, so `False` is accepted as ``"off"`` and `True` as ``"auto"``; `None` means the default. """ value = self._lazy_sparse_operator if value is None: - value = self._inversion_config_value("lazy_sparse_operator", "off") + value = self._inversion_config_value("lazy_sparse_operator", "auto") - if value is None or value is False: - value = "off" - elif value is True: + if value is None or value is True: value = "auto" + elif value is False: + value = "off" if value not in ("off", "auto"): raise ValueError( diff --git a/test_autoarray/inversion/inversion/test_lazy_operator.py b/test_autoarray/inversion/inversion/test_lazy_operator.py index a57d1bba5..7827a207e 100644 --- a/test_autoarray/inversion/inversion/test_lazy_operator.py +++ b/test_autoarray/inversion/inversion/test_lazy_operator.py @@ -13,6 +13,7 @@ INTERFEROMETER_LOGGER = "autoarray.dataset.interferometer.dataset" ON = aa.Settings(lazy_sparse_operator="auto") +OFF = aa.Settings(lazy_sparse_operator="off") @pytest.fixture(autouse=True) @@ -68,8 +69,9 @@ def _assert_interferometer_operator_close(operator, reference, rtol=1.0e-12): # Policy -def test__settings__lazy_sparse_operator__default_off_and_validated(): - assert aa.Settings().lazy_sparse_operator == "off" +def test__settings__lazy_sparse_operator__default_auto_and_validated(): + assert aa.Settings().lazy_sparse_operator == "auto" + assert aa.Settings(lazy_sparse_operator="off").lazy_sparse_operator == "off" assert aa.Settings(lazy_sparse_operator="auto").lazy_sparse_operator == "auto" assert aa.Settings(lazy_sparse_operator=False).lazy_sparse_operator == "off" @@ -78,7 +80,8 @@ def test__settings__lazy_sparse_operator__default_off_and_validated(): def test__policy__config_and_pyauto_setup(monkeypatch): - assert lazy_operator.lazy_sparse_operator_enabled() is False + # The packaged default is "auto". + assert lazy_operator.lazy_sparse_operator_enabled() is True monkeypatch.setattr( aa.Settings, @@ -97,6 +100,14 @@ def test__policy__config_and_pyauto_setup(monkeypatch): monkeypatch.undo() monkeypatch.delenv("PYAUTO_SETUP", raising=False) + # With the config "off", `PYAUTO_SETUP=auto` turns the policy on. + monkeypatch.setattr( + aa.Settings, + "_inversion_config_value", + lambda self, key, default: "off" if key == "lazy_sparse_operator" else default, + ) + assert lazy_operator.lazy_sparse_operator_enabled() is False + for value, enabled in ( ("auto", True), (" AUTO ", True), @@ -148,7 +159,10 @@ def test__imaging__policy_off__none_and_nothing_built(masked_imaging_7x7, monkey lambda self: called.append(1), ) - assert masked_imaging_7x7.sparse_operator_for(xp=np, needs_operator=True) is None + assert ( + masked_imaging_7x7.sparse_operator_for(xp=np, needs_operator=True, settings=OFF) + is None + ) assert masked_imaging_7x7._lazy_sparse_operator is None assert called == [] @@ -218,6 +232,13 @@ def test__imaging__policy_on_via_config_and_pyauto_setup( monkeypatch.undo() monkeypatch.delenv("PYAUTO_SETUP", raising=False) + # With the config "off", `PYAUTO_SETUP=auto` turns the lazy build on. + monkeypatch.setattr( + aa.Settings, + "_inversion_config_value", + lambda self, key, default: "off" if key == "lazy_sparse_operator" else default, + ) + dataset = masked_imaging_7x7.apply_over_sampling(over_sample_size_lp=2) dataset._lazy_sparse_operator = None assert dataset.sparse_operator_for(xp=np, needs_operator=True) is None @@ -346,7 +367,7 @@ def test__interferometer__policy_off__none_and_nothing_built(mask_2d_7x7, monkey lambda self, **kwargs: called.append(1), ) - assert dataset.sparse_operator_for(xp=np, needs_operator=True) is None + assert dataset.sparse_operator_for(xp=np, needs_operator=True, settings=OFF) is None assert dataset._lazy_sparse_operator is None assert called == []