# SPDX-FileCopyrightText: 2023-present Rohit Goswami <rog32@hi.is>
#
# SPDX-License-Identifier: MIT
"""Producer-independent records of surrogate-assisted saddle searches.
A surrogate-assisted search spends calls on an expensive *oracle* (the true
potential) and answers the rest from a fitted surrogate. The classes here
hold what a plot needs, whichever code produced it. NaN marks a quantity the
producer did not record.
Units: energy in eV, force in eV/A, length in A, curvature in eV/A^2,
time in s.
.. versionadded:: 1.10.0
"""
from __future__ import annotations
from dataclasses import dataclass, field
import numpy as np
[docs]
def _arr(x) -> np.ndarray:
return np.asarray(x, dtype=float)
[docs]
@dataclass
class BandSnapshot:
"""A path of images at one outer iteration.
``coordinate`` is the path coordinate in A (0 at the reactant);
``sigma`` and ``true_*`` hold NaN where not recorded or not evaluated.
"""
coordinate: np.ndarray
energy: np.ndarray
sigma: np.ndarray | None = None
true_energy: np.ndarray | None = None
true_force: np.ndarray | None = None
climbing: int | None = None
outer: int = -1
oracle_calls: int = -1
positions: np.ndarray | None = None
[docs]
def __post_init__(self):
self.coordinate = _arr(self.coordinate)
self.energy = _arr(self.energy)
n = len(self.coordinate)
for name in ("sigma", "true_energy", "true_force"):
v = getattr(self, name)
if v is not None:
v = _arr(v)
if v.shape != (n,):
msg = f"{name} has shape {v.shape}, expected ({n},)"
raise ValueError(msg)
setattr(self, name, v)
if self.energy.shape != (n,):
msg = "energy and coordinate differ in length"
raise ValueError(msg)
@property
def n_images(self) -> int:
return len(self.coordinate)
@property
def evaluated(self) -> np.ndarray:
if self.true_energy is None:
return np.zeros(self.n_images, dtype=bool)
return np.isfinite(self.true_energy)
[docs]
@dataclass
class EvaluatedPoints:
"""Oracle observations in acquisition order."""
energy: np.ndarray
max_force: np.ndarray | None = None
positions: np.ndarray | None = None
coordinate: np.ndarray | None = None
# Cartesian distance (A) of each observation from the final path.
distance: np.ndarray | None = None
# Cartesian energy gradients (eV/A), one row per observation.
gradients: np.ndarray | None = None
n_calls: int | None = None
[docs]
def __post_init__(self):
self.energy = _arr(self.energy)
[docs]
def __len__(self) -> int:
return len(self.energy)
[docs]
@dataclass
class AcquisitionEvent:
"""One decision to call the oracle."""
oracle_calls: int
kind: str
outer: int = -1
image: int = -1
detail: str = ""
[docs]
@dataclass
class BandHistory:
"""Two-ended search: path snapshots, observations and decisions."""
final: BandSnapshot
snapshots: list[BandSnapshot] = field(default_factory=list)
points: EvaluatedPoints | None = None
reference: BandSnapshot | None = None
events: list[AcquisitionEvent] = field(default_factory=list)
# Geometry the search reports as its saddle (flat Cartesian, A) and whether
# it passed the producer's acceptance checks.
saddle: np.ndarray | None = None
saddle_certified: bool | None = None
# Atomic numbers of the atoms (same order as the positions), where they came
# from, and the files looked at when none was found.
numbers: np.ndarray | None = None
numbers_source: str = ""
numbers_looked_for: list[str] = field(default_factory=list)
[docs]
@dataclass
class SearchHistory:
"""Per-outer (or per-call) series of a search.
``series`` keys use the standard names ``oracle_calls``, ``outer``,
``true_force``, ``surrogate_force``, ``surrogate_force_sigma``, ``energy``,
``n_train``, ``magnitude_sigma2``, ``noise_sigma2``, ``length_scale_min``,
``length_scale_max``, ``time_refit``, ``time_predict``, ``time_inner``,
``rss_mb``. Missing quantities are absent, never zero-filled.
"""
series: dict[str, np.ndarray]
force_tolerance: float | None = None
converged: bool | None = None
events: list[AcquisitionEvent] = field(default_factory=list)
retained_cap: int | None = None
meta: dict = field(default_factory=dict)
[docs]
def __post_init__(self):
self.series = {k: _arr(v) for k, v in self.series.items()}
[docs]
def __getitem__(self, key: str) -> np.ndarray:
return self.series[key]
[docs]
def get(self, key: str) -> np.ndarray | None:
return self.series.get(key)
@property
def oracle_calls(self) -> np.ndarray:
return self.series["oracle_calls"]
[docs]
def to_frame(self, method: str = "search"):
"""Long-form polars frame (columns ``oracle_calls``, ``max_fatom``,
``kind``, ``method``), one row per oracle call and force kind.
"""
import polars as pl
rows = []
for key, kind in (("true_force", "true"), ("surrogate_force", "surrogate")):
v = self.series.get(key)
if v is None:
continue
rows.append(
pl.DataFrame(
{
"oracle_calls": self.oracle_calls,
"max_fatom": v,
"kind": kind,
"method": f"{method} ({kind})",
}
)
)
return pl.concat(rows)
[docs]
@dataclass
class SingleEndedHistory:
"""Dimer-type search against oracle calls."""
oracle_calls: np.ndarray
force: np.ndarray
curvature: np.ndarray
curvature_measured: np.ndarray | None = None
curvature_surrogate: np.ndarray | None = None
rotation_angle: np.ndarray | None = None
escape: list[AcquisitionEvent] = field(default_factory=list)
force_tolerance: float | None = None
converged: bool | None = None
[docs]
def __post_init__(self):
self.oracle_calls = _arr(self.oracle_calls)
self.force = _arr(self.force)
self.curvature = _arr(self.curvature)
for name in ("curvature_measured", "curvature_surrogate", "rotation_angle"):
v = getattr(self, name)
if v is not None:
setattr(self, name, _arr(v))
[docs]
@dataclass
class CellRecord:
"""One reaction of a campaign."""
label: str
set: str = ""
method: str = ""
status: str = ""
converged: bool | None = None
passed: bool | None = None
index: int | None = None
search_calls: int | None = None
validation_calls: int | None = None
total_calls: int | None = None
wall: dict[str, float] = field(default_factory=dict)
energy_delta: float | None = None
ledger: dict[str, int] = field(default_factory=dict)
extras: dict = field(default_factory=dict)
[docs]
@dataclass
class CampaignTable:
"""One row per reaction, plus calls of comparison methods."""
name: str
cells: list[CellRecord]
baselines: dict[str, dict[str, float]] = field(default_factory=dict)
tolerance: float | None = None
[docs]
def by_label(self) -> dict[str, CellRecord]:
return {c.label: c for c in self.cells}
[docs]
def to_frame(self):
"""One polars row per cell; the wall times become ``wall_<stage>``."""
import polars as pl
return pl.DataFrame(
[
{
"label": c.label,
"set": c.set,
"method": c.method,
"status": c.status,
"converged": c.converged,
"passed": c.passed,
"index": c.index,
"search_calls": c.search_calls,
"validation_calls": c.validation_calls,
"total_calls": c.total_calls,
**{f"wall_{k}": v for k, v in c.wall.items()},
}
for c in self.cells
]
)
[docs]
@dataclass
class ScalingTable:
"""Time against worker count for several series.
``reference`` names the series whose first point defines speedup 1 for all
series; without it each series is measured against its own first point.
"""
series: dict[str, tuple[np.ndarray, np.ndarray]]
reference: str | None = None
# Optional per-point extras, keyed like ``series``: slowest/fastest
# repetition (s), oracle calls of the run, and runs that hit a cap.
time_min: dict[str, np.ndarray] = field(default_factory=dict)
time_max: dict[str, np.ndarray] = field(default_factory=dict)
calls: dict[str, np.ndarray] = field(default_factory=dict)
capped: dict[str, np.ndarray] = field(default_factory=dict)
# ``ranks x threads`` label of each point, keyed like ``series``.
layouts: dict[str, list[str]] = field(default_factory=dict)
[docs]
def speedup(self, name: str) -> tuple[np.ndarray, np.ndarray]:
"""Speedup of ``name`` against its own first point.
With ``reference`` set, every series is measured against the first
point of that series instead, so series that split one total (search
and validation of a pipeline) share a baseline.
"""
w, t = self.series[name]
w, t = _arr(w), _arr(t)
_, rt = self.series[self.reference or name]
return w, _arr(rt)[0] / t
[docs]
def speedup_range(self, name: str) -> tuple[np.ndarray, np.ndarray] | None:
"""(low, high) speedup from the slowest and fastest repetition."""
if name not in self.time_min or name not in self.time_max:
return None
_, rt = self.series[self.reference or name]
r0 = _arr(rt)[0]
return r0 / _arr(self.time_max[name]), r0 / _arr(self.time_min[name])
[docs]
@dataclass
class SurrogateSearch:
"""All records of one search, with provenance."""
label: str
producer: str
band: BandHistory | None = None
search: SearchHistory | None = None
single_ended: SingleEndedHistory | None = None
cell: CellRecord | None = None
provenance: dict = field(default_factory=dict)
[docs]
@dataclass
class PopTable:
"""Fixed-work efficiency factors of one cell, one row per layout.
``layouts`` are ``ranks x threads`` labels sorted by cores then threads,
``cores`` the matching core counts, ``time`` the wall of each layout and
``metrics`` one array per factor name, every value a fraction in [0, 1].
Speedup and fixed-work (global) efficiency follow from ``time`` against
the layout with the fewest cores.
"""
layouts: list[str]
cores: np.ndarray
time: np.ndarray
metrics: dict[str, np.ndarray]
cell: str | None = None
# what ``time`` measures, for the axis label (process wall, fit, ...)
quantity: str = "process wall"
[docs]
def speedup(self) -> np.ndarray:
"""``T(reference) / T(layout)``, the reference being the fewest cores."""
t = _arr(self.time)
return t[int(np.argmin(self.cores))] / t
[docs]
def efficiency(self) -> np.ndarray:
"""Speedup per core ratio against the reference layout."""
c = _arr(self.cores)
return self.speedup() / (c / c[int(np.argmin(c))])
[docs]
@dataclass
class ComponentTable:
"""Median seconds per component of the wall, per cell and layout.
``components`` keeps the order of the file; ``parts[(cell, layout)]`` maps
each component to its median seconds over repetitions and
``total[(cell, layout)]`` holds the whole wall. The part of the total no
component accounts for is ``unattributed``.
"""
components: list[str]
cells: list[str]
layouts: dict[str, list[str]]
parts: dict[tuple[str, str], dict[str, float]]
total: dict[tuple[str, str], float]
[docs]
def unattributed(self, cell: str, layout: str) -> float:
key = (cell, layout)
return max(self.total[key] - sum(self.parts[key].values()), 0.0)
[docs]
def fractions(self, cell: str, layout: str) -> dict[str, float]:
"""Share of the total per component, plus ``unattributed``."""
key = (cell, layout)
tot = self.total[key]
out = {c: v / tot for c, v in self.parts[key].items()}
out["unattributed"] = self.unattributed(cell, layout) / tot
return out
[docs]
def amdahl_bounds(self, cell: str, layout: str) -> dict[str, float]:
"""Largest whole-run speedup if one component alone took no time.
With the component's share ``f`` of the wall, the rest of the run
bounds the speedup at ``1 / (1 - f)``.
"""
return {
c: (1.0 / (1.0 - f) if f < 1.0 else float("inf"))
for c, f in self.fractions(cell, layout).items()
}