diff --git a/autolens/analysis/analysis/dataset.py b/autolens/analysis/analysis/dataset.py index 992a157f9..af5b32cc7 100644 --- a/autolens/analysis/analysis/dataset.py +++ b/autolens/analysis/analysis/dataset.py @@ -25,6 +25,7 @@ import autoarray as aa import autogalaxy as ag +from autogalaxy.analysis.setup_util import SETUP_DEFAULT from autogalaxy.analysis.analysis.dataset import AnalysisDataset as AgAnalysisDataset from autolens.analysis.analysis.lens import AnalysisLens @@ -66,8 +67,8 @@ def __init__( settings: aa.Settings = None, raise_inversion_positions_likelihood_exception: bool = True, title_prefix: str = None, - use_jax: bool = True, - setup=None, + use_jax: Optional[bool] = None, + setup=SETUP_DEFAULT, **kwargs, ): """ @@ -102,16 +103,18 @@ def __init__( of this. This exception is not raised if this input is False, allowing the user to perform the model-fit anyway. use_jax - Whether the likelihood is evaluated with JAX. It is honoured as given when `setup` is `None`; a - resolved `setup` sets the backend instead. + Whether the likelihood is evaluated with JAX. Passing it with no `setup` opts out of `setup="auto"`: + the backend is `use_jax`, as with `setup=None`. With a `setup` the resolved setup sets the backend. setup - The compute setup of the likelihood: `None` (the default, nothing changes), `"auto"` (the designated - setup for the likelihood and hardware, chosen at fit start from the model), an `aa.Setup` or a - setup_id string such as `"numba_cpu_sparse_fp64"`. It is resolved first in `modify_before_fit` by the - **PyAutoGalaxy** base class (see `autogalaxy.analysis.analysis.dataset.AnalysisDataset`), or before - the fit by `prepare_for_model(model)` (e.g. via `print_vram_use(model=...)`). Evaluating the - likelihood before the setup is resolved raises an `exc.SetupException` instead of fitting with the - setup silently ignored. + The compute setup of the likelihood: `"auto"` (the default when neither `setup` nor `use_jax` is + passed: the designated setup for the likelihood and hardware, chosen at fit start from the model), + `None` (the manual opt-out: backend `use_jax`, plus one log line saying what `"auto"` would + choose), an `aa.Setup` or a setup_id string such as `"numba_cpu_sparse_fp64"`. It is resolved first + in `modify_before_fit` by the **PyAutoGalaxy** base class (see + `autogalaxy.analysis.analysis.dataset.AnalysisDataset`), or before the fit by + `prepare_for_model(model)` (e.g. via `print_vram_use(model=...)`). Evaluating the likelihood before + an explicitly passed setup is resolved raises an `exc.SetupException`; before the default `"auto"` + is resolved it runs with the constructor backend. """ super().__init__( diff --git a/autolens/analysis/analysis/setup.py b/autolens/analysis/analysis/setup.py index d5c92f668..18fcb9ee2 100644 --- a/autolens/analysis/analysis/setup.py +++ b/autolens/analysis/analysis/setup.py @@ -55,7 +55,11 @@ def raise_if_setup_unresolved(analysis): """ - Raise an `exc.SetupException` when an analysis was given a `setup` that was never resolved. + Raise an `exc.SetupException` when an analysis was explicitly given a `setup` that was never resolved. + + The default `setup="auto"` (an analysis constructed with neither `setup` nor `use_jax`) does not raise: + before it resolves, the likelihood runs with the constructor backend, so direct pre-fit calls such as + `analysis.log_likelihood_function(instance)` keep working. A setup is resolved in `modify_before_fit`, which a non-linear search calls before sampling (and a `FactorGraphModel` forwards to each factor), or by `prepare_for_model(model)` on pre-fit calls that @@ -72,6 +76,11 @@ def raise_if_setup_unresolved(analysis): if setup_input is None or getattr(analysis, "setup", None) is not None: return + # The default `setup="auto"` (neither `setup` nor `use_jax` passed) does not stop a pre-fit evaluation: + # it runs with the constructor backend, as the analyses did before `"auto"` became the default. + if getattr(analysis, "setup_defaulted", False): + return + if isinstance(setup_input, aa.Setup): setup_input = setup_input.setup_id diff --git a/autolens/imaging/model/analysis.py b/autolens/imaging/model/analysis.py index 4886474ea..098bf41d9 100644 --- a/autolens/imaging/model/analysis.py +++ b/autolens/imaging/model/analysis.py @@ -13,6 +13,7 @@ (``VisualizerImaging``), and position-based priors via ``PositionLikelihood``. """ import logging +from typing import Optional import autoarray as aa import autofit as af @@ -20,6 +21,8 @@ from autonerves.fitsable import hdu_list_for_output_from +from autogalaxy.analysis.setup_util import SETUP_DEFAULT + from autolens.analysis.analysis.dataset import AnalysisDataset from autolens.analysis.analysis.setup import raise_if_setup_unresolved from autolens.analysis.exceptions import raise_fit_exception @@ -51,9 +54,9 @@ def __init__( settings=None, raise_inversion_positions_likelihood_exception: bool = True, title_prefix: str = None, - use_jax: bool = True, + use_jax: Optional[bool] = None, shared_preloads: bool = False, - setup=None, + setup=SETUP_DEFAULT, **kwargs, ): """ @@ -62,10 +65,13 @@ def __init__( Parameters ---------- + use_jax + Whether the likelihood is evaluated with JAX. Passing it with no `setup` opts out of `"auto"` (the + manual backend, as `setup=None`); with a `setup`, the resolved setup sets the backend. setup - The compute setup of the likelihood: `None` (default, nothing changes), `"auto"` (the designated - setup for the likelihood and hardware, chosen at fit start from the model), an `aa.Setup` or a - setup_id string. See `AnalysisDataset`. + The compute setup of the likelihood: `"auto"` (the default: the designated setup for the likelihood + and hardware, chosen at fit start from the model), `None` (the manual opt-out, backend `use_jax`), + an `aa.Setup` or a setup_id string. See `AnalysisDataset`. shared_preloads Opts this analysis into the cross-factor shared-state mechanism of a `FactorGraphModel` (see `shared_state_from`). Set this to `True` only when this analysis is one of many diff --git a/autolens/interferometer/model/analysis.py b/autolens/interferometer/model/analysis.py index 4fea4642c..d35fbc965 100644 --- a/autolens/interferometer/model/analysis.py +++ b/autolens/interferometer/model/analysis.py @@ -23,6 +23,8 @@ import autogalaxy as ag from autogalaxy.interferometer.model.analysis import interferometer_hdu_list_from +from autogalaxy.analysis.setup_util import SETUP_DEFAULT + from autolens.analysis.analysis.dataset import AnalysisDataset from autolens.analysis.analysis.setup import raise_if_setup_unresolved from autolens.analysis.exceptions import raise_fit_exception @@ -52,9 +54,9 @@ def __init__( settings: aa.Settings = None, raise_inversion_positions_likelihood_exception: bool = True, title_prefix: str = None, - use_jax: bool = True, + use_jax: Optional[bool] = None, shared_preloads: bool = False, - setup=None, + setup=SETUP_DEFAULT, **kwargs, ): """ @@ -97,10 +99,13 @@ def __init__( title_prefix A string that is added before the title of all figures output by visualization, for example to put the name of the dataset and galaxy in the title. + use_jax + Whether the likelihood is evaluated with JAX. Passing it with no `setup` opts out of `"auto"` (the + manual backend, as `setup=None`); with a `setup`, the resolved setup sets the backend. setup - The compute setup of the likelihood: `None` (default, nothing changes), `"auto"` (the designated - setup for the likelihood and hardware, chosen at fit start from the model), an `aa.Setup` or a - setup_id string. See `AnalysisDataset`. + The compute setup of the likelihood: `"auto"` (the default: the designated setup for the likelihood + and hardware, chosen at fit start from the model), `None` (the manual opt-out, backend `use_jax`), + an `aa.Setup` or a setup_id string. See `AnalysisDataset`. shared_preloads Opts this analysis into the cross-factor shared-state mechanism of a `FactorGraphModel` (see `shared_state_from`). Set this to `True` only when this analysis is one of many datacube channels diff --git a/autolens/point/model/analysis.py b/autolens/point/model/analysis.py index 68ee3edb4..1eb85834e 100644 --- a/autolens/point/model/analysis.py +++ b/autolens/point/model/analysis.py @@ -16,6 +16,7 @@ """ import logging +from typing import Optional import os import numpy as np @@ -27,6 +28,8 @@ from autogalaxy.analysis.analysis.analysis import Analysis as AgAnalysis from autolens.analysis.analysis.lens import AnalysisLens +from autogalaxy.analysis.setup_util import SETUP_DEFAULT, setup_and_use_jax_from + from autolens.analysis.analysis.setup import ( AnalysisSetupNoOperator, raise_if_setup_unresolved, @@ -70,10 +73,10 @@ def __init__( image=None, cosmology: ag.cosmo.LensingCosmology = None, title_prefix: str = None, - use_jax: bool = True, + use_jax: Optional[bool] = None, fit_flux_cls=FitFluxes, fit_time_delays_cls=FitTimeDelays, - setup=None, + setup=SETUP_DEFAULT, **kwargs, ): """ @@ -121,18 +124,21 @@ def __init__( A string that is added before the title of all figures output by visualization, for example to put the name of the dataset and galaxy in the title. use_jax - Whether the likelihood is evaluated with JAX. It is honoured as given when `setup` is `None`; a - resolved `setup` sets the backend instead. + Whether the likelihood is evaluated with JAX. Passing it with no `setup` opts out of `setup="auto"`: + the backend is `use_jax`, as with `setup=None`. With a `setup` the resolved setup sets the backend. setup - The compute setup of the likelihood: `None` (the default, nothing changes), `"auto"`, an `aa.Setup` - or a setup_id string such as `"jax_cpu_na_fp64"`. It is resolved in `modify_before_fit`. With - `"auto"` the rule key is `point_source_source/source_plane` when `fit_positions_cls` is a - source-plane fit (`FitPositionsSource` or a subclass) and `point_source_image/image_plane` otherwise - (see `setup_key_from`). A resolved setup sets the backend, pins the thread pools, is logged as a - banner line and is written to `files/setup.json`; a point dataset has no operator to attach. - Evaluating the likelihood before the setup is resolved (by the fit or `prepare_for_model(model)`) - raises an `exc.SetupException`. + The compute setup of the likelihood: `"auto"` (the default when neither `setup` nor `use_jax` is + passed), `None` (the manual opt-out: backend `use_jax`, default `True`, plus one log line + saying what `"auto"` would choose), an `aa.Setup` or a setup_id string. It is resolved in + `modify_before_fit`; with `"auto"` the rule key is `point_source_source/source_plane` when `fit_positions_cls` is a source-plane fit (`FitPositionsSource` or a subclass) and `point_source_image/image_plane` otherwise (see `setup_key_from`). A resolved setup sets the backend, pins the thread pools, is + logged as a banner line and is written to `files/setup.json`. Evaluating the likelihood before + an explicitly passed setup is resolved (by the fit or `prepare_for_model(model)`) raises an + `exc.SetupException`; before the default `"auto"` is resolved it runs with the constructor backend. """ + setup, use_jax, setup_defaulted = setup_and_use_jax_from( + setup=setup, use_jax=use_jax, default_use_jax=True + ) + super().__init__(cosmology=cosmology, use_jax=use_jax, **kwargs) # `super().__init__` (af.Analysis) is the single reader of the @@ -148,7 +154,7 @@ def __init__( self.fit_flux_cls = fit_flux_cls self.fit_time_delays_cls = fit_time_delays_cls self.title_prefix = title_prefix - self._init_setup(setup) + self._init_setup(setup, setup_defaulted=setup_defaulted) self._check_solver_extent() def setup_key_from(self, model: af.Collection = None): diff --git a/autolens/weak/model/analysis.py b/autolens/weak/model/analysis.py index 0dd10e96b..30847a86a 100644 --- a/autolens/weak/model/analysis.py +++ b/autolens/weak/model/analysis.py @@ -13,12 +13,16 @@ It also manages result output (``ResultWeak``) and on-the-fly visualisation (``VisualizerWeak``). """ +from typing import Optional + import autofit as af import autogalaxy as ag from autogalaxy.analysis.analysis.analysis import Analysis as AgAnalysis +from autogalaxy.analysis.setup_util import SETUP_DEFAULT, setup_and_use_jax_from from autolens.analysis.analysis.lens import AnalysisLens + from autolens.analysis.analysis.setup import ( AnalysisSetupNoOperator, raise_if_setup_unresolved, @@ -41,8 +45,8 @@ def __init__( dataset: WeakDataset, cosmology: ag.cosmo.LensingCosmology = None, title_prefix: str = None, - use_jax: bool = False, - setup=None, + use_jax: Optional[bool] = None, + setup=SETUP_DEFAULT, **kwargs, ): """ @@ -59,9 +63,10 @@ def __init__( contributes two independent shear measurements (gamma_1 and gamma_2), which `FitWeak` compares against the model shear field of the `Tracer`. - `use_jax` defaults to `False`, the conservative choice for the newest Analysis class; pass - `use_jax=True` to run the `xp`-threaded fit path with `FitWeak` pytree registration (validated by - the `autolens_workspace_test` weak vmap-parity script). + By default the compute setup is `setup="auto"`, resolved at fit start. On the manual opt-out + (`setup=None` or an explicit `use_jax`) the backend defaults to NumPy (`use_jax=False`), the + conservative choice for the newest Analysis class; `use_jax=True` runs the `xp`-threaded fit path with + `FitWeak` pytree registration (validated by the `autolens_workspace_test` weak vmap-parity script). Parameters ---------- @@ -74,16 +79,21 @@ def __init__( A string that is added before the title of all figures output by visualization, for example to put the name of the dataset and galaxy in the title. use_jax - Whether the likelihood is evaluated with JAX. It is honoured as given when `setup` is `None`; a - resolved `setup` sets the backend instead. + Whether the likelihood is evaluated with JAX. Passing it with no `setup` opts out of `setup="auto"`: + the backend is `use_jax`, as with `setup=None`. With a `setup` the resolved setup sets the backend. setup - The compute setup of the likelihood: `None` (the default, nothing changes), `"auto"`, an `aa.Setup` - or a setup_id string such as `"jax_cpu_dense_fp64"`. It is resolved in `modify_before_fit`; with - `"auto"` the rule key is `weak/isothermal` (see `setup_key_from`). A resolved setup sets the backend, - pins the thread pools, is logged as a banner line and is written to `files/setup.json`; a shear - catalogue has no operator to attach. Evaluating the likelihood before the setup is resolved (by the - fit or `prepare_for_model(model)`) raises an `exc.SetupException`. + The compute setup of the likelihood: `"auto"` (the default when neither `setup` nor `use_jax` is + passed), `None` (the manual opt-out: backend `use_jax`, default `False`, plus one log line + saying what `"auto"` would choose), an `aa.Setup` or a setup_id string. It is resolved in + `modify_before_fit`; with `"auto"` the rule key is `weak/isothermal` (see `setup_key_from`). A shear catalogue has no operator to attach. A resolved setup sets the backend, pins the thread pools, is + logged as a banner line and is written to `files/setup.json`. Evaluating the likelihood before + an explicitly passed setup is resolved (by the fit or `prepare_for_model(model)`) raises an + `exc.SetupException`; before the default `"auto"` is resolved it runs with the constructor backend. """ + setup, use_jax, setup_defaulted = setup_and_use_jax_from( + setup=setup, use_jax=use_jax, default_use_jax=False + ) + super().__init__(cosmology=cosmology, use_jax=use_jax, **kwargs) # `super().__init__` (af.Analysis) is the single reader of the @@ -95,7 +105,7 @@ def __init__( self.title_prefix = title_prefix - self._init_setup(setup) + self._init_setup(setup, setup_defaulted=setup_defaulted) def setup_key_from(self, model: af.Collection = None): """ diff --git a/test_autolens/aggregator/test_fit_dataset_setup.py b/test_autolens/aggregator/test_fit_dataset_setup.py index 65d27d632..c0427f797 100644 --- a/test_autolens/aggregator/test_fit_dataset_setup.py +++ b/test_autolens/aggregator/test_fit_dataset_setup.py @@ -79,8 +79,9 @@ def test__fit_imaging_from__setup_json__reapplied(masked_imaging_7x7, monkeypatc fit_reloaded_no_setup = _fit_imaging_from(fit=_fake_fit({"dataset": hdu_list}))[0] + # With no setup.json nothing is attached; the lazy sparse operator (on by default) is built on use. assert fit_reloaded_no_setup.dataset.setup is None - assert fit_reloaded_no_setup.sparse_operator is None + assert fit_reloaded_no_setup.dataset.sparse_operator is None fit_reloaded = _fit_imaging_from( fit=_fake_fit( @@ -110,8 +111,9 @@ def test__fit_interferometer_from__setup_json__reapplied(interferometer_7): fit_reloaded_no_setup = _fit_interferometer_from(fit=_fake_fit(values))[0] + # With no setup.json nothing is attached; the lazy sparse operator (on by default) is built on use. assert fit_reloaded_no_setup.dataset.setup is None - assert fit_reloaded_no_setup.sparse_operator is None + assert fit_reloaded_no_setup.dataset.sparse_operator is None values["setup"] = _setup_dict("numba_cpu_sparse_fp64_wtilde") diff --git a/test_autolens/imaging/model/test_analysis_imaging_setup.py b/test_autolens/imaging/model/test_analysis_imaging_setup.py index 740d78cb1..e7d5ee1c6 100644 --- a/test_autolens/imaging/model/test_analysis_imaging_setup.py +++ b/test_autolens/imaging/model/test_analysis_imaging_setup.py @@ -80,8 +80,14 @@ def _analysis(dataset, **kwargs): def test__setup_kwarg__threaded_to_galaxy_base(masked_imaging_7x7): - assert _analysis(masked_imaging_7x7).setup_input is None + # Neither `setup` nor `use_jax`: the default `setup="auto"`. + assert _analysis(masked_imaging_7x7).setup_input == "auto" + assert _analysis(masked_imaging_7x7).setup_defaulted is True + # An explicit `use_jax` with no `setup`, or `setup=None`, opts out. + assert _analysis(masked_imaging_7x7, use_jax=False).setup_input is None + assert _analysis(masked_imaging_7x7, setup=None).setup_input is None assert _analysis(masked_imaging_7x7, setup="AUTO").setup_input == "auto" + assert _analysis(masked_imaging_7x7, setup="AUTO").setup_defaulted is False analysis = _analysis(masked_imaging_7x7, setup="numba_cpu_dense_fp64") @@ -317,3 +323,98 @@ def apply_setup_keeping_operator(self, setup, **kwargs): assert not isinstance(e.value, af.exc.FitException) assert analysis._use_jax is False + + +# The default `setup="auto"` (likelihood-lifecycle R6). + + +def test__default__resolves_auto_at_fit_start_with_banner( + masked_imaging_7x7, monkeypatch, caplog +): + _detect(monkeypatch, MANY_CORE) + + analysis = _analysis(masked_imaging_7x7) + + assert analysis.setup_input == "auto" + assert analysis.setup_defaulted is True + + with caplog.at_level(logging.INFO): + analysis.modify_before_fit(paths=af.DirectoryPaths(), model=_rectangular_model()) + + assert analysis.setup.setup_id == "numba_cpu_sparse_fp64" + assert analysis._use_jax is False + assert analysis._xp is np + assert any( + r.message.startswith("Automatic compute setup (") for r in caplog.records + ) + + +def test__default__explicit_use_jax_false__manual_numpy_would_choose_banner( + masked_imaging_7x7, monkeypatch, caplog +): + _detect(monkeypatch, MANY_CORE) + + analysis = _analysis(masked_imaging_7x7, use_jax=False) + + with caplog.at_level(logging.INFO): + analysis.modify_before_fit(paths=af.DirectoryPaths(), model=_rectangular_model()) + + assert analysis.setup is None + assert analysis._use_jax is False + assert analysis.dataset is masked_imaging_7x7 + assert any("auto would choose" in r.message for r in caplog.records) + assert not any(r.message.startswith("Automatic") for r in caplog.records) + + +def test__default__pre_fit_likelihood_runs_without_setup_exception(masked_imaging_7x7): + instance = _light_model().instance_from_unit_vector([]) + + # The default `"auto"` does not stop a pre-fit evaluation: it runs on the constructor backend. + analysis = _analysis(masked_imaging_7x7) + + assert np.isfinite(float(analysis.log_likelihood_function(instance=instance))) + + # An explicitly passed setup still raises until it is resolved. + with pytest.raises(exc.SetupException): + _analysis(masked_imaging_7x7, setup="auto").log_likelihood_function( + instance=instance + ) + + +def test__default__factor_graph__unchanged(masked_imaging_7x7): + factors = [ + af.AnalysisFactor(prior_model=_light_model(), analysis=_analysis(masked_imaging_7x7)) + for _ in range(2) + ] + + factor_graph = af.FactorGraphModel(*factors) + instance = factor_graph.global_prior_model.instance_from_unit_vector([]) + + assert np.isfinite(float(factor_graph.log_likelihood_function(instance))) + + +def test__default__search_default_number_of_cores_auto_resolves_to_1_for_jax( + masked_imaging_7x7, monkeypatch +): + pytest.importorskip("jax") + + from autofit.non_linear.parallel.pool import resolve_number_of_cores + + _detect(monkeypatch, MANY_CORE) + + search = af.Nautilus(name="r6_default_cores") + + assert search.number_of_cores == "auto" + + analysis = _analysis(masked_imaging_7x7) + analysis.modify_before_fit(paths=af.DirectoryPaths(), model=_light_model()) + + # The imaging/mge rule on a many-core CPU designates JAX. + assert analysis.setup.backend == "jax" + + resolution = resolve_number_of_cores( + requested=search.number_of_cores, analysis=analysis + ) + + assert resolution.number_of_cores == 1 + assert resolution.reason == "jax" diff --git a/test_autolens/imaging/test_fit_imaging.py b/test_autolens/imaging/test_fit_imaging.py index eac2c8d06..a13fd58ef 100644 --- a/test_autolens/imaging/test_fit_imaging.py +++ b/test_autolens/imaging/test_fit_imaging.py @@ -1220,6 +1220,7 @@ def test__galaxy_image_dicts__add_ordinary_and_linear_light(masked_imaging_7x7): # which the lens fit's `tracer_to_inversion` passes to every per-plane `DatasetInterface`. LAZY_ON = aa.Settings(lazy_sparse_operator="auto") +LAZY_OFF = aa.Settings(lazy_sparse_operator="off") @pytest.fixture(name="no_ambient_setup") @@ -1251,7 +1252,9 @@ def test__sparse_operator__lazy_policy_on__pixelized_source__matches_dense_built tracer = _lens_pixelized_source_tracer() - fit_dense = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer) + fit_dense = al.FitImaging( + dataset=masked_imaging_7x7, tracer=tracer, settings=LAZY_OFF + ) assert fit_dense.sparse_operator is None assert isinstance(fit_dense.inversion, aa.InversionImagingMapping) @@ -1396,3 +1399,36 @@ def test__sparse_operator__linear_light_only__attached_operator_unchanged( assert fit.figure_of_merit == pytest.approx( fit_dense.figure_of_merit, rel=1.0e-6 ) + + +def test__sparse_operator__lazy_default__two_pixelized_sources_match_dense( + masked_imaging_7x7, no_ambient_setup +): + """ + The lazy sparse operator is on by default, so a NumPy fit with two pixelized source planes takes the + numba multi-mapper route (`InversionImagingSparseNumba._data_vector_multi_mapper`), which must match the + dense mapping-matrix route. + """ + pytest.importorskip("numba") + + pixelization = al.Pixelization( + mesh=al.mesh.RectangularUniform(shape=(3, 3)), + regularization=al.reg.Constant(coefficient=1.0), + ) + + tracer = al.Tracer( + galaxies=[ + al.Galaxy(redshift=0.5, mass=al.mp.IsothermalSph(einstein_radius=0.2)), + al.Galaxy(redshift=1.0, pixelization=pixelization), + al.Galaxy(redshift=2.0, pixelization=pixelization), + ] + ) + + masked_imaging_7x7._lazy_sparse_operator = None + + fit = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer) + fit_dense = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer, settings=LAZY_OFF) + + assert fit.sparse_operator is not None + assert isinstance(fit_dense.inversion, aa.InversionImagingMapping) + assert fit.figure_of_merit == pytest.approx(fit_dense.figure_of_merit, rel=1.0e-8) diff --git a/test_autolens/interferometer/model/test_analysis_interferometer.py b/test_autolens/interferometer/model/test_analysis_interferometer.py index 50e6d3c78..231e4786e 100644 --- a/test_autolens/interferometer/model/test_analysis_interferometer.py +++ b/test_autolens/interferometer/model/test_analysis_interferometer.py @@ -435,8 +435,10 @@ def _light_model(): def test__setup_kwarg__threaded_to_galaxy_base(interferometer_7, unset_setup_env): + assert al.AnalysisInterferometer(dataset=interferometer_7).setup_input == "auto" assert ( - al.AnalysisInterferometer(dataset=interferometer_7).setup_input is None + al.AnalysisInterferometer(dataset=interferometer_7, use_jax=False).setup_input + is None ) analysis = al.AnalysisInterferometer( diff --git a/test_autolens/interferometer/test_fit_interferometer.py b/test_autolens/interferometer/test_fit_interferometer.py index 9ed0afbc3..0ce1b954f 100644 --- a/test_autolens/interferometer/test_fit_interferometer.py +++ b/test_autolens/interferometer/test_fit_interferometer.py @@ -4,6 +4,9 @@ import autoarray as aa import autolens as al +# The dense mapping-matrix reference fits switch the lazy sparse operator (on by default) off. +LAZY_OFF = aa.Settings(lazy_sparse_operator="off") + def test__model_visibilities(interferometer_7): g0 = al.Galaxy(redshift=0.5, bulge=al.m.MockLightProfile(image_2d=np.ones(9))) @@ -583,7 +586,7 @@ def test__fit_figure_of_merit__sparse_operator__pixelization_only__data_term_sca tracer = _pixelized_source_tracer() - fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) + fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer, settings=LAZY_OFF) fit_sparse = al.FitInterferometer(dataset=dataset_sparse, tracer=tracer) assert isinstance(fit.inversion, aa.InversionInterferometerMapping) @@ -868,7 +871,7 @@ def test__fit_figure_of_merit__array_free_dataset__lens_light_profile__matches_d tracer = _lens_light_tracer(source=source) - fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) + fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer, settings=LAZY_OFF) fit_array_free = al.FitInterferometer(dataset=dataset_array_free, tracer=tracer) assert fit_array_free._uses_precomputed_data_term @@ -1290,7 +1293,8 @@ def test__sparse_operator__lazy_policy_off__dense_and_nothing_built( interferometer_7, no_ambient_setup ): fit = al.FitInterferometer( - dataset=interferometer_7, tracer=_lens_pixelized_source_tracer(lens_light=False) + dataset=interferometer_7, tracer=_lens_pixelized_source_tracer(lens_light=False), + settings=LAZY_OFF, ) assert fit.sparse_operator is None @@ -1305,7 +1309,7 @@ def test__sparse_operator__lazy_policy_on__pixelized_source__matches_dense_built tracer = _lens_pixelized_source_tracer(lens_light=False) - fit_dense = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) + fit_dense = al.FitInterferometer(dataset=interferometer_7, tracer=tracer, settings=LAZY_OFF) fit = al.FitInterferometer( dataset=interferometer_7, tracer=tracer, settings=LAZY_ON @@ -1341,7 +1345,7 @@ def test__sparse_operator__lazy_policy_on__pixelized_source_and_lens_light__dirt tracer = _lens_pixelized_source_tracer(lens_light=True) - fit_dense = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) + fit_dense = al.FitInterferometer(dataset=interferometer_7, tracer=tracer, settings=LAZY_OFF) fit = al.FitInterferometer( dataset=interferometer_7, tracer=tracer, settings=LAZY_ON diff --git a/test_autolens/interferometer/test_simulate_and_fit_interferometer.py b/test_autolens/interferometer/test_simulate_and_fit_interferometer.py index 34c4c80b0..46e1d7a1b 100644 --- a/test_autolens/interferometer/test_simulate_and_fit_interferometer.py +++ b/test_autolens/interferometer/test_simulate_and_fit_interferometer.py @@ -291,9 +291,13 @@ def test__simulate_interferometer_data_and_fit__linear_light_profiles_and_pixeli tracer_linear = al.Tracer(galaxies=[lens_galaxy_linear, source_galaxy_pix]) + # The regression values are of the dense mapping-matrix route, so the lazy sparse operator (on by + # default) is switched off: on this near-degenerate 2-visibility dataset the positive-only + # reconstruction of the sparse route differs beyond the tolerance. fit_linear = al.FitInterferometer( dataset=dataset, tracer=tracer_linear, + settings=al.Settings(lazy_sparse_operator="off"), ) assert fit_linear.inversion.reconstruction == pytest.approx( diff --git a/test_autolens/point/model/test_analysis_point_setup.py b/test_autolens/point/model/test_analysis_point_setup.py index b9615b0aa..603468a0f 100644 --- a/test_autolens/point/model/test_analysis_point_setup.py +++ b/test_autolens/point/model/test_analysis_point_setup.py @@ -81,7 +81,10 @@ def test__setup_key_from__follows_fit_positions_cls( def test__setup_kwarg__stored(point_dataset): - assert _analysis(point_dataset).setup_input is None + assert _analysis(point_dataset).setup_input == "auto" + assert _analysis(point_dataset).setup_defaulted is True + assert _analysis(point_dataset, use_jax=True).setup_input is None + assert _analysis(point_dataset, setup=None).setup_input is None assert _analysis(point_dataset, setup="auto").setup_input == "auto" analysis = _analysis(point_dataset, setup="jax_cpu_na_fp64") diff --git a/test_autolens/weak/test_analysis.py b/test_autolens/weak/test_analysis.py index cd3d96ae9..cdf734fea 100644 --- a/test_autolens/weak/test_analysis.py +++ b/test_autolens/weak/test_analysis.py @@ -196,10 +196,13 @@ def test__setup_auto__pyauto_setup_env_wins__weak(detect_many_core, monkeypatch) assert analysis._xp is np -def test__no_setup__weak_unchanged(detect_many_core, caplog, tmp_path): +@pytest.mark.parametrize("kwargs", [{"setup": None}, {"use_jax": False}]) +def test__no_setup__weak_unchanged(detect_many_core, caplog, tmp_path, kwargs): dataset = _make_dataset() - analysis = al.AnalysisWeak(dataset=dataset) + # `setup=None` or an explicit `use_jax` opts out of the default `setup="auto"`; the manual + # backend keeps AnalysisWeak's old default, NumPy. + analysis = al.AnalysisWeak(dataset=dataset, **kwargs) with caplog.at_level(logging.INFO): analysis.modify_before_fit(paths=af.DirectoryPaths(), model=_lens_model())