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
7 changes: 4 additions & 3 deletions autolens/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ def __init__(
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.
**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.
"""

super().__init__(
Expand Down
33 changes: 19 additions & 14 deletions autolens/analysis/analysis/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
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.
and the guard that stops a likelihood being evaluated before its setup has been resolved.

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
Expand All @@ -10,9 +10,11 @@
`files/setup.json`) through PyAutoGalaxy's shared `AnalysisSetupMixin`
(`autogalaxy.analysis.analysis.setup`).

`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.
`raise_if_setup_unresolved` is called at the top of every lens `log_likelihood_function`. A setup
resolves when the fit starts (`modify_before_fit`, which a search calls on the analysis and a
`FactorGraphModel` forwards to each factor) or on a pre-fit call that receives a model
(`analysis.prepare_for_model(model)`, called by e.g. `print_vram_use(model=...)`). A likelihood evaluated
before either would otherwise run with the constructor backend and the setup silently ignored.
"""

import logging
Expand All @@ -32,15 +34,17 @@


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.
The {cls} was given `setup={setup_input!r}`, but its likelihood was evaluated before the setup was
resolved.

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).
The setup resolves when the fit starts (`search.fit` calls `modify_before_fit`, which a factor graph
forwards to each factor's analysis) or on a pre-fit call that receives a model, e.g.
`analysis.print_vram_use(model=model, batch_size=...)`. The likelihood was evaluated before the fit
started and before either.

Instead, apply the setup to each dataset yourself and choose the backend explicitly, e.g.
Call `analysis.prepare_for_model(model)` first (it resolves the setup from the model you will fit),
or run the fit. Alternatively, apply the setup to the 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)
Expand All @@ -53,9 +57,10 @@ 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.
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
receive a model (e.g. `print_vram_use`). A likelihood evaluated before either would otherwise use the
constructor backend with 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
Expand Down
202 changes: 190 additions & 12 deletions autolens/analysis/runtime_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import yaml

import autoarray as aa
from autoarray.setup.setup import setup_from

from autolens.analysis import wall_model

Expand Down Expand Up @@ -146,6 +145,45 @@ class RuntimeEstimate:
expected_evals_band: Optional[Tuple[int, int]] = None
band_across: Optional[str] = None
supported_search: bool = True
has_profiling_cell: bool = True
fallback_reason: Optional[str] = None

@classmethod
def no_profiling_cell(
cls,
setup_id: str,
backend: Optional[str],
number_of_cores: int,
search=None,
fallback_reason: Optional[str] = None,
) -> "RuntimeEstimate":
"""
The estimate of a setup the run-time table has no row for (e.g. `numba_cpu_dense_fp64`): every
number is `None` and `line()` reads `Estimate: no estimate for <setup_id> (no profiling cell)`.

`fallback_reason` (e.g. `"JAX disabled"`) marks a `jax` setup that runs on numpy instead: the
table has rows for the JAX designation only, so the fit that actually runs is not priced, and
`line()` says why.
"""
return cls(
ms_per_eval=None,
record_id=None,
instrument=None,
matched_by="none",
expected_evals=None,
evals_model="none",
parallel_efficiency=None,
number_of_cores=number_of_cores,
wall_seconds=None,
wall_band=None,
calibrated=False,
note="no profiling cell",
setup_id=setup_id,
backend=backend,
search_name=None if search is None else type(search).__name__,
has_profiling_cell=False,
fallback_reason=fallback_reason,
)

def line(self) -> str:
"""
Expand All @@ -158,8 +196,21 @@ def line(self) -> str:
fit has `1 worker (JAX)` in place of `η`; an uncalibrated estimate ends its details with
`uncalibrated (held-out 0.2–0.5×)`; a search other than Nautilus reads
`Estimate: none for Emcee (the run-time model covers Nautilus only) (697 ms/eval, hst cell)`.
The worker count is not repeated (the fit's `Parallel:` line reports it).
The worker count is not repeated (the fit's `Parallel:` line reports it). A setup with no row in
the run-time table reads `Estimate: no estimate for numba_cpu_dense_fp64 (no profiling cell)`.
"""
if not self.has_profiling_cell and self.fallback_reason is not None:
return _estimate_line(
f"no estimate for {self.setup_id} (designated JAX; {self.fallback_reason}, "
f"the numpy fallback has no profiling cell)",
[],
)

if not self.has_profiling_cell:
return _estimate_line(
f"no estimate for {self.setup_id} (no profiling cell)", []
)

details = []

if self.ms_per_eval_band is not None:
Expand Down Expand Up @@ -478,12 +529,67 @@ def matched_rows_for(
return candidates, matched, matched_by


def _rule_key_from(dataset, model, likelihood: Optional[str]) -> Tuple[str, str]:
"""
The `(dataset_type, variant)` rule key of `aa.Setup.auto` for `setup="auto"`: the one the analyses
use (`autogalaxy.analysis.setup_util.setup_key_and_note_from` for imaging and interferometer,
`weak/isothermal` for weak lensing), or the `likelihood` the caller gives.

Raises
------
ValueError
When the key cannot be worked out (a point dataset needs `likelihood=`, as its rule depends on
the analysis's positions fit).
"""
if likelihood is not None:
dataset_type, _, variant = likelihood.partition("/")
return dataset_type, variant

dataset_type = dataset_type_from(dataset)

if dataset_type in ("imaging", "interferometer"):
from autogalaxy.analysis import setup_util

key, _ = setup_util.setup_key_and_note_from(dataset=dataset, model=model)
return key

if dataset_type == "weak":
return "weak", "isothermal"

raise ValueError(
"setup='auto' cannot work out the setup rule for this dataset"
+ (
": a point dataset's rule depends on its positions fit, so pass likelihood="
"'point_source_image/image_plane' or 'point_source_source/source_plane'."
if dataset_type == POINT_DATASET_TYPE
else f" ({type(dataset).__name__}); pass likelihood='<dataset_type>/<variant>'."
)
)


def resolve_setup_from(dataset, model, setup, likelihood: Optional[str] = None):
"""
The compute setup of `estimate_runtime`: `None`, an `aa.Setup` or setup_id string as given, or for
`"auto"` the setup a fit's `setup="auto"` resolves to, `aa.Setup.auto` with the analyses' rule key
(`_rule_key_from`) on the detected hardware (`PYAUTO_SETUP` overrides it, as in a fit).
"""
from autogalaxy.analysis import setup_util

setup = setup_util.setup_input_from(setup)

if setup != setup_util.SETUP_AUTO:
return setup

key = _rule_key_from(dataset=dataset, model=model, likelihood=likelihood)
return aa.Setup.auto(*key, environment=setup_util.setup_environment())


def estimate_runtime(
dataset,
model,
search,
setup,
number_of_cores: int = 1,
number_of_cores=1,
likelihood: Optional[str] = None,
) -> Optional[RuntimeEstimate]:
"""
Expand All @@ -501,19 +607,26 @@ def estimate_runtime(
The non-linear search: only `af.Nautilus` gets an estimate (its `n_live`); any other search gets
an estimate whose line says there is none.
setup
The compute setup, an `al.Setup` or a setup_id string (e.g. `"numba_cpu_sparse_fp64"`).
The compute setup: an `al.Setup`, a setup_id string (e.g. `"numba_cpu_sparse_fp64"`) or `"auto"`,
resolved as a fit's `setup="auto"` is (`aa.Setup.auto` with the analyses' rule key for the dataset
and model on the detected hardware; `PYAUTO_SETUP` overrides it).
number_of_cores
The worker count of the fit. For a numba setup the per-call is divided by
The worker count of the fit, an int or `"auto"` (resolved as a fit resolves it, by PyAutoFit's
`resolve_number_of_cores`: 1 for a JAX backend, else the detected cores capped by the matched row's
`cpus_per_eval` / `peak_rss_gb`). For a numba setup the per-call is divided by
`cores * efficiency(cores)`; a JAX setup runs one worker and uses its per-call as is.
likelihood
The `<dataset_type>/<variant>` likelihood, when the caller knows it (the analyses pass their setup
rule key); otherwise it is worked out from the dataset and model.

Returns
-------
A `RuntimeEstimate`, or `None` when there is no setup or the table has no row for it.
A `RuntimeEstimate`, or `None` when there is no setup. A setup the table has no row for gives an
estimate whose `line()` reads `no estimate for <setup_id> (no profiling cell)`.
"""
setup = setup_from(setup)
setup = resolve_setup_from(
dataset=dataset, model=model, setup=setup, likelihood=likelihood
)
if setup is None:
return None

Expand All @@ -527,12 +640,37 @@ def estimate_runtime(
likelihood=likelihood,
rows=table["rows"],
)
if not candidates:
return None

backend = setup.backend
is_jax = backend == "jax"
cores = 1 if is_jax else max(1, int(number_of_cores or 1))
cores = _number_of_cores_from(
number_of_cores=number_of_cores,
setup=setup,
resources=resources_per_eval_from(matched, matched_by),
)

from autogalaxy.analysis import setup_util

# A `jax` setup run on numpy (`PYAUTO_DISABLE_JAX=1` or JAX not installed) is not what the JAX
# rows measured, so it is not priced by them (the `Setup:` line reports the same fallback).
fallback_reason = setup_util.jax_fallback_reason_from(
setup=setup, use_jax=setup_util.use_jax_from(backend == "jax")
)
if fallback_reason is not None:
return RuntimeEstimate.no_profiling_cell(
setup_id=setup.setup_id,
backend="numpy",
number_of_cores=cores,
search=search,
fallback_reason=fallback_reason,
)

if not candidates:
return RuntimeEstimate.no_profiling_cell(
setup_id=setup.setup_id,
backend=backend,
number_of_cores=cores,
search=search,
)

efficiency = (
wall_model.parallel_efficiency(cores, curve=curve)
Expand Down Expand Up @@ -617,6 +755,39 @@ def estimate_runtime(
return estimate


def _number_of_cores_from(number_of_cores, setup: aa.Setup, resources: Optional[dict]) -> int:
"""
The worker count `estimate_runtime` prices a fit by: 1 for a setup whose backend resolves to JAX
(after `PYAUTO_DISABLE_JAX` and a missing JAX install, as in a fit), else the request, with `"auto"`
resolved by PyAutoFit's `resolve_number_of_cores` exactly as a fit resolves it (the detected cores,
capped by the matched row's `cpus_per_eval` / `peak_rss_gb`).
"""
from types import SimpleNamespace

from autofit.non_linear.parallel.pool import resolve_number_of_cores
from autofit.non_linear.parallel.resources import is_auto
from autogalaxy.analysis import setup_util

is_jax = setup_util.use_jax_from(setup.backend == "jax")

if is_auto(number_of_cores):
# A stand-in analysis carrying what `resolve_number_of_cores` duck-types: the setup and the
# matched row's resources.
analysis = SimpleNamespace(
setup=setup, is_jax=is_jax, resources_per_eval=lambda: resources
)
return resolve_number_of_cores(
requested=number_of_cores,
analysis=analysis,
is_jax=is_jax,
where="estimate_runtime",
).number_of_cores

if is_jax:
return 1
return max(1, int(number_of_cores or 1))


def resources_per_eval_from(rows: List[dict], matched_by: str) -> Optional[dict]:
"""
`{"cpus_per_eval", "peak_rss_gb"}` of the matched row, or for a band the largest of each across the
Expand Down Expand Up @@ -699,7 +870,11 @@ def runtime_estimate(self, search, model=None, number_of_cores=1) -> Optional[st
except Exception as e:
logger.debug(f"Run-time estimate failed and was ignored: {e!r}")
return None
return None if estimate is None else estimate.line()
# A setup with no profiling cell logs nothing at fit time (as before); only a direct
# `al.estimate_runtime` call reports "no estimate for <setup_id> (no profiling cell)".
if estimate is None or not estimate.has_profiling_cell:
return None
return estimate.line()

def resources_per_eval(self) -> Optional[dict]:
"""
Expand All @@ -710,6 +885,9 @@ def resources_per_eval(self) -> Optional[dict]:
setup = getattr(self, "setup", None)
if setup is None:
return None
# A `jax` setup run on numpy is not what the JAX rows measured (see `estimate_runtime`).
if setup.backend == "jax" and not self.is_jax:
return None
model = getattr(self, "_runtime_estimate_model", None)
_, matched, matched_by = matched_rows_for(
dataset=getattr(self, "dataset", None),
Expand Down
6 changes: 3 additions & 3 deletions autolens/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class PointProfileMismatchException(Exception):

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.
Raised when the compute `setup` of an analysis cannot be honoured, for example when the likelihood of an
analysis given `setup=` is evaluated before the setup was resolved (by `modify_before_fit` when the fit
starts, or by `prepare_for_model(model)` before it).

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.
Expand Down
5 changes: 3 additions & 2 deletions autolens/point/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ def __init__(
`"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. A setup
is not supported inside a factor graph: the likelihood raises an `exc.SetupException` instead.
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`.
"""
super().__init__(cosmology=cosmology, use_jax=use_jax, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions autolens/weak/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def __init__(
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. A setup is not supported inside a factor graph: the likelihood
raises an `exc.SetupException` instead.
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`.
"""
super().__init__(cosmology=cosmology, use_jax=use_jax, **kwargs)

Expand Down
Loading
Loading