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
16 changes: 16 additions & 0 deletions autolens/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(
raise_inversion_positions_likelihood_exception: bool = True,
title_prefix: str = None,
use_jax: bool = True,
setup=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -99,6 +100,16 @@ def __init__(
be inferred, in which case an Exception is raised before the model-fit begins to inform the user
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.
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`). A setup is
not supported inside a factor graph, which does not call `modify_before_fit`: the likelihood raises
an `exc.SetupException` instead of fitting with the setup silently ignored.
"""

super().__init__(
Expand All @@ -108,6 +119,7 @@ def __init__(
settings=settings,
title_prefix=title_prefix,
use_jax=use_jax,
setup=setup,
**kwargs,
)

Expand Down Expand Up @@ -136,6 +148,10 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):

This function:

- Resolves and applies the compute `setup` (via the **PyAutoGalaxy** base class, which does this first,
before the grids are preloaded), keeping the backend (`_use_jax`, and `_xp`, which derives from it) in
step with the resolved setup.

- Checks that the adapt-dataset is consistent with previous adapt-datasets if the model-fit is being
resumed from a previous run.

Expand Down
195 changes: 195 additions & 0 deletions autolens/analysis/analysis/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
"""
The compute `setup` of the lens analyses that have no dataset operator (point-source and weak lensing),
and the guard that stops a setup being silently ignored inside a factor graph.

The imaging and interferometer analyses resolve their setup through the **PyAutoGalaxy** base class
(`autogalaxy.analysis.analysis.dataset.AnalysisDataset.resolve_setup`), which also applies it to the
dataset (attaching the sparse operator it names). `AnalysisPoint` and `AnalysisWeak` do not fit an
`aa.Imaging` / `aa.Interferometer`, so their setup has no operator to attach (operator `"na"` or
`"dense"`); `AnalysisSetupNoOperator` resolves and applies the rest of it (backend, threads, banner,
`files/setup.json`), reusing the helpers of `autogalaxy.analysis.setup_util`.

`raise_if_setup_unresolved` is called at the top of every lens `log_likelihood_function`. A setup is
resolved in `modify_before_fit`, which a `FactorGraphModel` does not call on its factors, so a setup
given to an analysis fitted inside a factor graph would otherwise never be applied.
"""

import logging
from typing import Optional, Tuple

import autofit as af
import autoarray as aa

from autogalaxy.analysis import setup_util

from autolens import exc

logger = logging.getLogger(__name__)

logger.setLevel(level="INFO")


SETUP_UNRESOLVED_MESSAGE = """
The {cls} was given `setup={setup_input!r}`, but the setup was never resolved, because
`modify_before_fit` was not called before the likelihood was evaluated.

This happens when the analysis is fitted inside a factor graph (`af.FactorGraphModel` of
`af.AnalysisFactor`s): the factor graph does not call each factor's `modify_before_fit`, so the
setup would silently not be applied. Setups are not yet supported inside factor graphs (this is
planned for the PyAutoFit phase of the likelihood lifecycle, 5D).

Instead, apply the setup to each dataset yourself and choose the backend explicitly, e.g.

dataset = dataset.apply_setup("numba_cpu_sparse_fp64")
analysis = al.AnalysisImaging(dataset=dataset, use_jax=False)

(`use_jax=True` for a `jax_*` setup), and do not pass `setup=` to the analysis.
"""


def raise_if_setup_unresolved(analysis):
"""
Raise an `exc.SetupException` when an analysis was given a `setup` that was never resolved.

A setup is resolved in `modify_before_fit`, which a non-linear search calls before sampling. A
`FactorGraphModel` does not call it on its factors, so a factor given `setup=` would otherwise be
fitted with its constructor backend and the setup silently ignored.

This is a cheap Python attribute check, safe under JAX tracing. It is called at the top of each
`log_likelihood_function`, outside the `raise_fit_exception` wrapper, so it stops the fit instead of
being turned into a resample. It is not called by `fit_from`, so visualization, results and the
aggregator are unaffected.
"""
setup_input = getattr(analysis, "setup_input", None)

if setup_input is None or getattr(analysis, "setup", None) is not None:
return

if isinstance(setup_input, aa.Setup):
setup_input = setup_input.setup_id

raise exc.SetupException(
SETUP_UNRESOLVED_MESSAGE.format(
cls=type(analysis).__name__, setup_input=setup_input
)
)


def setup_environment():
"""
The detected hardware (`autonerves.environment.Environment`), or `None` if detection fails.
"""
try:
from autonerves.environment import Environment

return Environment.detect()
except Exception as e:
logger.debug(f"Setup: hardware detection failed: {e}")
return None


class AnalysisSetupNoOperator:
"""
Mixin resolving the compute `setup` of an analysis whose dataset has no operator to attach
(`AnalysisPoint`, `AnalysisWeak`).

The subclass calls `_init_setup(setup)` in its constructor, `resolve_setup(model)` first in its
`modify_before_fit`, `save_setup(paths)` in its `save_attributes`, and implements `setup_key_from`
(the `(dataset_type, variant)` rule key of `aa.Setup.auto`).
"""

def _init_setup(self, setup):
"""
Store the normalised `setup` input (`None`, `"auto"` or an `aa.Setup`, see
`setup_util.setup_input_from`); the resolved setup is set by `resolve_setup`.
"""
self.setup_input = setup_util.setup_input_from(setup)
self.setup = None
self.setup_chosen_by = None

def setup_key_from(self, model: af.Collection) -> Tuple[str, str]:
"""
The `(dataset_type, variant)` rule key of `aa.Setup.auto` for this analysis and model.
"""
raise NotImplementedError

def resolve_setup(self, model: af.Collection):
"""
Resolve the `setup` input of the analysis against the model and apply it.

With no `setup` input nothing changes: one log line reports what `setup="auto"` would choose (any
error working that out is ignored).

With `setup="auto"` the setup is chosen by `aa.Setup.auto` from `setup_key_from(model)`;
`PYAUTO_SETUP=<setup_id>` overrides it. An explicit `aa.Setup` or setup_id is used as given. The
resolved setup then:

- sets the analysis backend (`_use_jax = setup.backend == "jax"`, falling back to numpy when JAX is
disabled or not installed, as `af.Analysis` does); `_xp` follows, as it derives from `_use_jax`;
- pins the thread pools (`setup.apply_threads()`);
- is logged as the setup banner line and stored as `self.setup`.

There is no dataset operator to attach or check.
"""
setup_input = getattr(self, "setup_input", None)

if setup_input is None:
self._log_setup_auto_would_choose(model=model)
return

environment = setup_environment()

if setup_input == setup_util.SETUP_AUTO:
setup = aa.Setup.auto(*self.setup_key_from(model), environment=environment)
chosen_by = setup_util.chosen_by_from(setup=setup, auto=True)
else:
setup = setup_input
chosen_by = setup_util.chosen_by_from(setup=setup, auto=False)

self._use_jax = setup_util.use_jax_from(setup.backend == "jax")

setup.apply_threads()

self.setup = setup
self.setup_chosen_by = chosen_by

logger.info(
setup_util.setup_banner_from(
setup=setup,
chosen_by=chosen_by,
environment=environment,
)
)

def _log_setup_auto_would_choose(self, model: af.Collection):
"""
Log one line reporting the setup `setup="auto"` would choose, applying nothing.
"""
try:
setup = aa.Setup.auto(
*self.setup_key_from(model), environment=setup_environment()
)
except Exception as e:
logger.debug(
f"Setup: could not work out what setup='auto' would choose: {e}"
)
return

logger.info(
f"Setup: auto would choose {setup.setup_id} (rule {setup.rule_id}); "
f'pass setup="auto" to use it'
)

def save_setup(self, paths: af.DirectoryPaths):
"""
Write the resolved setup to `files/setup.json`, when a setup was resolved.
"""
setup: Optional[aa.Setup] = getattr(self, "setup", None)

if setup is not None:
paths.save_json(
name="setup",
object_dict=setup_util.setup_dict_from(
setup=setup, chosen_by=self.setup_chosen_by
),
)
13 changes: 13 additions & 0 deletions autolens/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ class PointProfileMismatchException(Exception):
absent dataset components (its long-standing name-pairing semantics), and profile/fit mismatches must never
be silently skipped — they invalidate the composed model.
"""


class SetupException(AnalysisException):
"""
Raised when the compute `setup` of an analysis cannot be honoured, for example when an analysis given
`setup=` is fitted inside a factor graph, where its `modify_before_fit` (which resolves and applies the
setup) is never called.

Deliberately NOT a subclass of `af.exc.FitException`: it is a configuration error, so it must stop the
model-fit with a clear message rather than be swallowed into a resample on every likelihood call.
"""

pass
9 changes: 9 additions & 0 deletions autolens/imaging/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from autonerves.fitsable import hdu_list_for_output_from

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
from autolens.analysis.latent import LatentLens
from autolens.imaging.model.result import ResultImaging
Expand Down Expand Up @@ -52,6 +53,7 @@ def __init__(
title_prefix: str = None,
use_jax: bool = True,
shared_preloads: bool = False,
setup=None,
**kwargs,
):
"""
Expand All @@ -60,6 +62,10 @@ def __init__(

Parameters
----------
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`.
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 All @@ -77,6 +83,7 @@ def __init__(
raise_inversion_positions_likelihood_exception=raise_inversion_positions_likelihood_exception,
title_prefix=title_prefix,
use_jax=use_jax,
setup=setup,
**kwargs,
)

Expand Down Expand Up @@ -126,6 +133,8 @@ def log_likelihood_function(self, instance: af.ModelInstance, shared=None) -> fl
The log likelihood indicating how well this model instance fitted the imaging data.
"""

raise_if_setup_unresolved(analysis=self)

log_likelihood_penalty = self.log_likelihood_penalty_from(
instance=instance,
)
Expand Down
9 changes: 9 additions & 0 deletions autolens/interferometer/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from autogalaxy.interferometer.model.analysis import interferometer_hdu_list_from

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
from autolens.analysis.positions import PositionsLH
from autolens.interferometer.model.result import ResultInterferometer
Expand Down Expand Up @@ -53,6 +54,7 @@ def __init__(
title_prefix: str = None,
use_jax: bool = True,
shared_preloads: bool = False,
setup=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -95,6 +97,10 @@ 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.
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`.
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 All @@ -111,6 +117,7 @@ def __init__(
raise_inversion_positions_likelihood_exception=raise_inversion_positions_likelihood_exception,
title_prefix=title_prefix,
use_jax=use_jax,
setup=setup,
**kwargs,
)

Expand Down Expand Up @@ -164,6 +171,8 @@ def log_likelihood_function(self, instance, shared=None):
The log likelihood indicating how well this model instance fitted the interferometer data.
"""

raise_if_setup_unresolved(analysis=self)

log_likelihood_penalty = self.log_likelihood_penalty_from(
instance=instance,
)
Expand Down
Loading
Loading