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
8 changes: 7 additions & 1 deletion autogalaxy/aggregator/agg_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ def dataset_with_setup_from(fit: af.Fit, dataset):
- a search with no ``setup.json`` (no ``setup`` was given) returns the dataset unchanged;
- an array-free dataset (``dataset.data is None``, rebuilt by ``Interferometer.from_sparse_terms``)
already carries its operator and is returned unchanged;
- on success one INFO line says so (`Compute setup: reapplied <setup_id> from the search's setup.json`;
``apply_setup`` itself logs nothing);
- if the setup cannot be applied here (e.g. a JAX sparse setup reloaded without JAX installed, or an
unreadable file), one warning is logged and the dataset is returned unapplied.

Expand Down Expand Up @@ -278,11 +280,15 @@ def dataset_with_setup_from(fit: af.Fit, dataset):
setup_id = setup_dict.get("setup_id") if isinstance(setup_dict, dict) else setup_dict

try:
return dataset.apply_setup(setup_id)
dataset = dataset.apply_setup(setup_id)
except Exception as e:
logger.warning(
f"Aggregator: the search's compute setup {setup_id!r} could not be reapplied to the "
f"reloaded dataset ({type(e).__name__}: {e}); the dataset is reloaded without it, so a fit "
f"of it may take a different operator route from the search."
)
return dataset

logger.info(f"Compute setup: reapplied {setup_id} from the search's setup.json")

return dataset
8 changes: 6 additions & 2 deletions autogalaxy/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
"""

# The setup is resolved first: applying it may rebuild the dataset, so the grids below are
# preloaded on the dataset the likelihood will use.
# preloaded on the dataset the likelihood will use. Its log lines are kept until the end, so
# anything the preloading warns comes before them and they sit with the fit's `Parallel:` /
# `Estimate:` lines.

self.resolve_setup(model=model)
self.resolve_setup(model=model, log_banner=False)

# Ensures preloaded in memory so repeated calls are fast

Expand All @@ -141,6 +143,8 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):

super().modify_before_fit(paths=paths, model=model)

self.log_setup_banner()

return self

@staticmethod
Expand Down
46 changes: 32 additions & 14 deletions autogalaxy/analysis/analysis/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _apply_resolved_setup(self, setup: aa.Setup):
default does nothing.
"""

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

Expand All @@ -87,17 +87,25 @@ def resolve_setup(self, model: af.Collection):
the dataset and checks it against the backend, so a mismatch raises here rather than rejecting
every sample);
- pins the thread pools (`setup.apply_threads()`);
- is logged as the setup banner line and stored as `self.setup`.
- is logged as the header and `Setup:` lines (two records) and stored as `self.setup`.

Parameters
----------
model
The model fitted by the search, which decides the rule variant (e.g. pixelization mesh or MGE).
log_banner
Whether to log the setup lines now. `False` keeps them for `log_setup_banner`, so a caller that
does more work after resolving (e.g. `AnalysisDataset` preloading its grids, which can warn) logs
them last, next to the fit's `Parallel:` / `Estimate:` lines.
"""
setup_input = getattr(self, "setup_input", None)

self._setup_banner_lines = []

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

note = None
Expand All @@ -121,18 +129,31 @@ def resolve_setup(self, model: af.Collection):
self.setup = setup
self.setup_chosen_by = chosen_by

logger.info(
setup_util.setup_banner_from(
setup=setup,
chosen_by=chosen_by,
environment=environment,
note=note,
)
# The header and `Setup:` line are logged after the operator build and checks above (or
# later still, by `log_setup_banner`), so they sit with the fit's `Parallel:` / `Estimate:` lines.
self._setup_banner_lines = setup_util.setup_banner_lines_from(
setup=setup,
chosen_by=chosen_by,
environment=environment,
note=note,
)

if log_banner:
self.log_setup_banner()

def log_setup_banner(self):
"""
Log the setup lines `resolve_setup` prepared (the header and `Setup:` line, or the one "not set"
line), each as its own record, once.
"""
for line in getattr(self, "_setup_banner_lines", None) or []:
logger.info(line)
self._setup_banner_lines = []

def _log_setup_auto_would_choose(self, model: af.Collection):
"""
Log one line reporting the setup `setup="auto"` would choose, applying nothing.
Prepare the one line reporting the setup `setup="auto"` would choose (logged by
`log_setup_banner`), applying nothing.
"""
try:
key, _ = self.setup_key_and_note_from(model)
Expand All @@ -143,10 +164,7 @@ def _log_setup_auto_would_choose(self, model: af.Collection):
)
return

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

def save_setup(self, paths: af.DirectoryPaths):
"""
Expand Down
205 changes: 178 additions & 27 deletions autogalaxy/analysis/setup_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
`use_jax` (honouring `PYAUTO_DISABLE_JAX` and a missing JAX install).
- `check_setup_consistency`: run the inversion factory's operator / setup / backend check
once, outside the likelihood, so a mismatch raises before sampling.
- `setup_banner_from` / `setup_dict_from`: the banner line logged at fit start and the
contents of `files/setup.json`.
- `setup_header_from` / `setup_banner_from` / `setup_dict_from`: the header and `Setup:`
lines logged at fit start, and the contents of `files/setup.json`.
- `setup_text_from`: the readable setup (`numba · CPU · sparse operator · fp64`) those
lines, the "not set" line and the aggregator notice share.
- `setup_environment`: the detected hardware `aa.Setup.auto` chooses for.

The resolution itself is `autogalaxy.analysis.analysis.setup.AnalysisSetupMixin`, shared by every
Expand All @@ -33,7 +35,16 @@

import autoarray as aa

from autoarray.setup.setup import EXPLICIT_RULE_ID, Setup, rules, setup_from
from autoarray.setup.setup import (
EXPLICIT_RULE_ID,
GPU_A100_NAME,
HARDWARE_CLASS_CPU,
HARDWARE_CLASS_GPU,
MANY_CORE_MIN_CORES,
Setup,
rules,
setup_from,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -299,8 +310,8 @@ def chosen_by_from(setup: Setup, auto: bool) -> str:

def hardware_description_from(setup: Setup, environment=None) -> Optional[str]:
"""
A short description of the hardware a setup runs on, for the banner: the GPU name for
a GPU setup, the core count for a CPU setup, or `None` if unknown.
A short description of the hardware a setup runs on: the GPU name for a GPU setup,
the core count for a CPU setup, or `None` if unknown.
"""
if environment is None:
return None
Expand All @@ -310,44 +321,184 @@ def hardware_description_from(setup: Setup, environment=None) -> Optional[str]:
return f"{cores} CPU cores" if cores else None


_BACKEND_TEXT = {"numba": "numba", "jax": "JAX", "numpy": "numpy"}
_OPERATOR_TEXT = {"sparse": "sparse operator", "dense": "dense", "na": "no operator"}
_PRECISION_TEXT = {"mp": "mixed precision"}
_TRANSFORMER_TEXT = {"wtilde": "w-tilde", "nufft": "NUFFT", "dft": "DFT"}

# The `Setup:` / `Parallel:` / `Estimate:` labels of the fit-start block share one width.
LABEL_WIDTH = len("Parallel: ")


def setup_text_from(setup: Setup) -> str:
"""
The setup tuple in readable tokens, e.g. `numba · CPU · sparse operator · fp64` or
`numba · CPU · sparse operator · fp64 · w-tilde`.
"""
tokens = [
_BACKEND_TEXT.get(setup.backend, setup.backend),
setup.device.upper(),
_OPERATOR_TEXT.get(setup.operator, f"{setup.operator} operator"),
_PRECISION_TEXT.get(setup.precision, setup.precision),
]
if setup.transformer is not None:
tokens.append(_TRANSFORMER_TEXT.get(setup.transformer, setup.transformer))
return " · ".join(tokens)


def rule_likelihood_from(setup: Setup) -> Optional[str]:
"""
The `<dataset_type>/<variant>` likelihood of the rule that chose a setup (looked up
by its `decision_id` in the rule table), or `None` when it was not chosen by a rule.
"""
decision_id = setup.decision_id or setup.rule_id
if not decision_id or decision_id == EXPLICIT_RULE_ID:
return None
for row in rules():
if row.get("decision_id") == decision_id:
return row["likelihood"]
return None


def _intended_hardware_class(environment) -> str:
"""
The hardware class `aa.Setup.auto` aimed for: `gpu_a100` when `PYAUTO_SETUP=gpu`, or
when it is unset / `auto` and a GPU is detected; otherwise `cpu_many_core`.
"""
value = (os.environ.get("PYAUTO_SETUP") or "auto").strip().lower()
if value == "gpu":
return HARDWARE_CLASS_GPU
if value == "cpu":
return HARDWARE_CLASS_CPU
return HARDWARE_CLASS_GPU if getattr(environment, "gpu", False) else HARDWARE_CLASS_CPU


_CLASS_TEXT = {HARDWARE_CLASS_CPU: "CPU", HARDWARE_CLASS_GPU: "GPU"}


def extrapolated_reason_from(setup: Setup, environment=None) -> Optional[str]:
"""
Why an automatically chosen setup is marked extrapolated (`None` when it is not):

- a CPU rule on fewer than `MANY_CORE_MIN_CORES` cores:
`8 cores, rules measured on ≥16-core nodes`;
- an A100 rule on another GPU: `<GPU name>, rules measured on A100`, or on a machine
with no GPU: `no GPU detected, rules measured on A100`;
- no rule for the intended hardware class:
`no GPU rule for this likelihood, the CPU rule is used`.
"""
if not setup.extrapolated:
return None

if environment is not None and setup.hardware_class is not None:
intended = _intended_hardware_class(environment)
if intended != setup.hardware_class:
return (
f"no {_CLASS_TEXT.get(intended, intended)} rule for this likelihood, "
f"the {_CLASS_TEXT.get(setup.hardware_class, setup.hardware_class)} "
f"rule is used"
)

if setup.hardware_class == HARDWARE_CLASS_GPU:
if environment is None:
return f"rules measured on {GPU_A100_NAME}"
gpu_name = getattr(environment, "gpu_name", None)
if not getattr(environment, "gpu", False):
return f"no GPU detected, rules measured on {GPU_A100_NAME}"
return f"{gpu_name or 'GPU'}, rules measured on {GPU_A100_NAME}"

cores = getattr(environment, "cores", None) if environment is not None else None
measured = f"rules measured on ≥{MANY_CORE_MIN_CORES}-core nodes"
return f"{cores} cores, {measured}" if cores else measured


def setup_header_from(setup: Setup, chosen_by: str) -> str:
"""
The header line logged before the `Setup:` line at fit start, saying how the setup
was chosen:

- `"auto"`: `Automatic compute setup (CPU/GPU choice from setup="auto"; override with
PYAUTO_SETUP or setup=...):`
- `"PYAUTO_SETUP"`: `Compute setup (set by PYAUTO_SETUP=<setup_id>):`
- `"explicit"`: `Compute setup (set explicitly by setup=...):`
"""
if chosen_by == CHOSEN_BY_AUTO:
return (
'Automatic compute setup (CPU/GPU choice from setup="auto"; '
"override with PYAUTO_SETUP or setup=...):"
)
if chosen_by == CHOSEN_BY_ENV:
return f"Compute setup (set by PYAUTO_SETUP={setup.setup_id}):"
return "Compute setup (set explicitly by setup=...):"


def setup_banner_from(
setup: Setup,
chosen_by: str,
environment=None,
note: Optional[str] = None,
) -> str:
"""
The setup banner line logged at fit start, e.g.
The `Setup:` line logged at fit start (after `setup_header_from`), e.g.

`Setup: jax/gpu/dense/fp64 (NVIDIA A100-SXM4-80GB) · rule imaging-rectangular-standard-gpu-a100 ·
optimal (decision imaging-rectangular-standard-gpu-a100) · override: PYAUTO_SETUP`
"""
tokens = [setup.backend, setup.device, setup.operator, setup.precision]
if setup.transformer is not None:
tokens.append(setup.transformer)
`Setup: numba · CPU · sparse operator · fp64 (rule imaging/rectangular ·
extrapolated: 8 cores, rules measured on ≥16-core nodes)`

line = f"Setup: {'/'.join(tokens)}"

hardware = hardware_description_from(setup=setup, environment=environment)
if hardware:
line += f" ({hardware})"
An automatically chosen setup names its rule (`<dataset_type>/<variant>`) and, when
extrapolated, why (`extrapolated_reason_from`); `note` (e.g. that a fallback rule was
used) is appended inside the parentheses. An explicit or `PYAUTO_SETUP` setup has
only the note, if any.
"""
line = f"{'Setup:':<{LABEL_WIDTH}}{setup_text_from(setup)}"

details = []
if chosen_by == CHOSEN_BY_AUTO:
line += f" · rule {setup.rule_id}"
quality = "extrapolated" if setup.extrapolated else "optimal"
line += f" · {quality} (decision {setup.decision_id})"
line += " · override: PYAUTO_SETUP"
elif chosen_by == CHOSEN_BY_ENV:
line += " · rule explicit (PYAUTO_SETUP)"
else:
line += " · rule explicit (setup passed to the analysis)"

likelihood = rule_likelihood_from(setup)
details.append(f"rule {likelihood or setup.rule_id}")
reason = extrapolated_reason_from(setup=setup, environment=environment)
if reason:
details.append(f"extrapolated: {reason}")
if note:
line += f" · {note}"
details.append(note)

if details:
line += f" ({' · '.join(details)})"

return line


def setup_banner_lines_from(
setup: Setup,
chosen_by: str,
environment=None,
note: Optional[str] = None,
) -> list:
"""
The header and `Setup:` lines logged at fit start, as two lines logged as two
consecutive records (so each carries the log prefix and the labels align with the
`Parallel:` / `Estimate:` lines that follow).
"""
return [
setup_header_from(setup=setup, chosen_by=chosen_by),
setup_banner_from(
setup=setup, chosen_by=chosen_by, environment=environment, note=note
),
]


def setup_not_set_line_from(setup: Setup) -> str:
"""
The one line logged when an analysis has no `setup`, e.g.

`Compute setup: not set (auto would choose numba · CPU · sparse operator · fp64;
pass setup="auto")`
"""
return (
f"Compute setup: not set (auto would choose {setup_text_from(setup)}; "
f'pass setup="auto")'
)


def setup_dict_from(setup: Setup, chosen_by: str) -> dict:
"""
The contents of `files/setup.json`: the setup_id, the setup tuple, its provenance and
Expand Down
Loading
Loading