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
25 changes: 14 additions & 11 deletions autolens/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
):
"""
Expand Down Expand Up @@ -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__(
Expand Down
11 changes: 10 additions & 1 deletion autolens/analysis/analysis/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
16 changes: 11 additions & 5 deletions autolens/imaging/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
(``VisualizerImaging``), and position-based priors via ``PositionLikelihood``.
"""
import logging
from typing import Optional

import autoarray as aa
import autofit as af
import autogalaxy as ag

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
Expand Down Expand Up @@ -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,
):
"""
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions autolens/interferometer/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
):
"""
Expand Down Expand Up @@ -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
Expand Down
32 changes: 19 additions & 13 deletions autolens/point/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import logging
from typing import Optional
import os

import numpy as np
Expand All @@ -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,
Expand Down Expand Up @@ -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,
):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
38 changes: 24 additions & 14 deletions autolens/weak/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
):
"""
Expand All @@ -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
----------
Expand All @@ -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
Expand All @@ -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):
"""
Expand Down
6 changes: 4 additions & 2 deletions test_autolens/aggregator/test_fit_dataset_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")

Expand Down
Loading
Loading