"""NS callback implementations for monitoring, checkpointing, and I/O.
These are plugged into the NS outer loop via the callbacks parameter.
Callbacks receive NSState objects (not dicts).
"""
from __future__ import annotations
import collections
import logging
import time
from pathlib import Path
from typing import Any
import jax
import jax.numpy as jnp
import numpy as np
import jaxrens._jax_init # noqa: F401 -- pins jax_enable_x64=False before any JAX op
from jaxrens.io.formats import population_record
from jaxrens.state.ns import NSState
from jaxrens.state.walker import WalkerState
from jaxrens.utils.cell import get_volume
logger = logging.getLogger(__name__)
def _format_elapsed(seconds: float) -> str:
"""Render a duration as ``Dd HHh MMm SSs``, dropping leading zero units."""
s = int(round(seconds))
days, s = divmod(s, 86400)
hours, s = divmod(s, 3600)
minutes, s = divmod(s, 60)
parts = []
if days:
parts.append(f"{days}d")
if hours or parts:
parts.append(f"{hours}h")
if minutes or parts:
parts.append(f"{minutes}m")
parts.append(f"{s}s")
return " ".join(parts)
def _ns_state_to_checkpoint_dict(ns_state: NSState) -> dict:
"""Convert NSState to a dict suitable for save_checkpoint.
Live-walker state + scalars only. Dead-point history is *not* in the
checkpoint — the canonical record lives in ``<prefix>.energies`` (text
file appended each iteration by ``EnergyLogger``) and
``<prefix>.traj.<format>`` (per-iter dead-point trajectory written by
``TrajectoryCallback`` / ``BatchedTrajectoryCallback``). Postprocessing
(``postprocess.Monitor.from_directory``) reads from those streamed
artifacts; the HDF5 checkpoint is the restart anchor for live state.
Handles single-run scalar shapes (``iteration`` / ``n_dead`` as Python
ints) and batched multi-run shapes (``(G, P)`` or ``(n_runs,)``) by
keeping batched scalars as arrays; ``save_checkpoint`` is batched-safe.
"""
pop = ns_state.population
ep = (
ns_state.population.ensemble_params
if hasattr(ns_state.population, "ensemble_params")
else {}
)
is_npt = isinstance(ep, dict) and "pressure" in ep
it_arr = jnp.asarray(ns_state.iteration)
iteration = int(it_arr) if it_arr.ndim == 0 else it_arr
# n_dead ≡ iteration (both increment per ns_step) — kept as a separate
# field for backward compat with consumers that read it.
n_dead = iteration
result = population_record(
pop.positions,
pop.types,
pop.energy,
pop.cell,
step_sizes=pop.step_sizes,
log_evidence=ns_state.log_evidence,
iteration=iteration,
emax=ns_state.emax,
n_dead=n_dead,
n_walkers=ns_state.n_walkers,
)
if is_npt:
# Vectorized volume: vmap over all leading axes by flattening then reshape.
cell = pop.cell
flat = cell.reshape(-1, 3, 3)
vols = jax.vmap(get_volume)(flat)
result["live_volumes"] = vols.reshape(cell.shape[:-2])
else:
result["live_volumes"] = None
return result
def _format_reject_breakdown(
counts: Any,
reasons_used: "frozenset[str] | None" = None,
) -> str:
"""Format reject-reason counts: [n_accepted, n_energy, n_cell, n_prior].
Args:
counts: Array-like of length 4: [n_accepted, n_energy, n_cell, n_prior].
reasons_used: When provided, only include columns for reasons in this
set. Valid values: ``"energy"``, ``"cell"``, ``"prior"``.
When ``None`` (backward compat), fall back to current behaviour:
print all three columns if there were any rejects.
Returns:
Empty string when there were no rejects (all accepted) or no moves.
Otherwise a compact ``" reject: E=x% C=y% P=z%"`` suffix with
columns filtered to ``reasons_used``. If there were rejects but none
fell in the declared ``reasons_used`` set (indicates a code bug),
returns ``" reject: ???=<n>"`` as a visible signal.
"""
# Bucket index map: name -> column index in counts
_BUCKET_IDX = {"energy": 1, "cell": 2, "prior": 3}
_BUCKET_LABEL = {"energy": "E", "cell": "C", "prior": "P"}
_ALL_REASONS = ("energy", "cell", "prior")
c = np.asarray(counts).astype(np.int64)
if c.sum() == 0:
return ""
n_total = int(c.sum())
n_acc = int(c[0])
if n_acc == n_total:
return ""
n_rej = n_total - n_acc
if n_rej == 0:
return ""
if reasons_used is None:
# Backward-compat: print all three columns
pct_e = 100 * int(c[1]) / n_rej
pct_c = 100 * int(c[2]) / n_rej
pct_p = 100 * int(c[3]) / n_rej
return f" reject: E={pct_e:>3.0f}% C={pct_c:>3.0f}% P={pct_p:>3.0f}%"
# Filter to declared reasons; build ordered parts list
parts = []
for reason in _ALL_REASONS:
if reason in reasons_used:
idx = _BUCKET_IDX[reason]
label = _BUCKET_LABEL[reason]
pct = 100 * int(c[idx]) / n_rej
parts.append(f"{label}={pct:>3.0f}%")
if not parts:
# reasons_used is non-empty but none match our known reasons —
# or reasons_used is an empty frozenset; shouldn't happen normally.
return f" reject: ???={n_rej}"
# Check if the declared reasons account for all rejects; if not, something
# emitted an undeclared reason — flag it without hiding the data.
n_accounted = sum(
int(c[_BUCKET_IDX[r]]) for r in reasons_used if r in _BUCKET_IDX
)
if n_accounted < n_rej:
parts.append(f"???={n_rej - n_accounted}")
return " reject: " + " ".join(parts)
def _is_batched(ns_state: Any, info: "dict | None" = None) -> bool:
"""Return True when ns_state holds multiple parallel runs.
Prefers ``info["_batcher"].is_batched`` when the *info* dict is provided
and the key is present (commit 4+). Falls back to the legacy ndim-sniff
on ``ns_state.log_evidence`` for backward compatibility with callers that
do not pass an info dict.
Args:
ns_state: ``NSState`` or result dict.
info: Optional info dict from the NS outer loop. When present and
contains ``"_batcher"``, the batcher's ``is_batched`` property
is authoritative.
Returns:
``True`` for multi-run (batched) NS runs.
"""
if info is not None:
batcher = info.get("_batcher")
if batcher is not None:
return batcher.is_batched
# Fallback: recover the batcher from log_evidence's shape prefix.
from jaxrens.sampling.batch_descriptor import from_shape_prefix
if isinstance(ns_state, NSState):
log_ev = jnp.asarray(ns_state.log_evidence)
else:
log_ev = jnp.asarray(ns_state.get("log_evidence", jnp.array(0.0)))
return from_shape_prefix(log_ev.shape).is_batched
[docs]
class ProgressCallback:
"""Prints a multi-line per-move adaptation summary at each info interval.
Canonical single location for the periodic human-readable NS summary.
The duplicate ``logger.info`` call in ``nested_sampling.py`` around line 543
has been removed; this callback is the only summary log source.
Single-run format::
iter=500 Emax=-102.3 log_Z=99.0 dt=0.8s
galilean ss=5.000 acc=0.62
volume ss=0.011 acc=0.10
Batched format (n_runs > 1)::
iter=500 Emax=[-102.3..-101.8] log_Z=[99.0..99.3] dt=0.8s
galilean ss=5.000±0.08 acc=0.62±0.03
volume ss=0.011±0.001 acc=0.10±0.02
When ``info`` does not carry ``step_sizes_per_move`` (no full_auto), the
per-move block is omitted and only the header line is printed.
"""
def __init__(self, info_interval: int = 100):
self.info_interval = info_interval
self._start_time = time.time()
self._last_print_time = self._start_time
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if iteration % self.info_interval != 0 and iteration != 0:
return
dt = time.time() - self._last_print_time
self._last_print_time = time.time()
batched = _is_batched(ns_state, info)
# ---- header line ------------------------------------------------
# Evaluation counter suffix (backward-compat: absent for callers that
# don't populate cumulative_n_evaluations_per_move)
cum_evals_arr = info.get("cumulative_n_evaluations_per_move")
cum_grad_arr = info.get("cumulative_n_grad_evaluations_per_move")
if cum_evals_arr is not None and cum_grad_arr is not None:
cum_e = float(np.asarray(cum_evals_arr).sum())
cum_g = float(np.asarray(cum_grad_arr).sum())
eval_suffix = f" nE={cum_e:.2e} nG={cum_g:.2e}"
else:
eval_suffix = ""
if batched:
emax_arr = jnp.asarray(info.get("emax", 0))
log_z_arr = jnp.asarray(
ns_state.log_evidence
if isinstance(ns_state, NSState)
else info.get("log_evidence", float("-inf"))
)
header = (
f"iter={iteration}"
f" Emax=[{float(jnp.min(emax_arr)):.4g}..{float(jnp.max(emax_arr)):.4g}]"
f" log_Z=[{float(jnp.min(log_z_arr)):.4f}..{float(jnp.max(log_z_arr)):.4f}]"
f" dt={dt:.1f}s"
f"{eval_suffix}"
)
else:
log_z = float(
ns_state.log_evidence
if isinstance(ns_state, NSState)
else info.get("log_evidence", float("-inf"))
)
header = (
f"iter={iteration}"
f" Emax={float(info.get('emax', 0)):.6g}"
f" log_Z={log_z:.4f}"
f" dt={dt:.1f}s"
f"{eval_suffix}"
)
# ---- per-move rows -----------------------------------------------
ss_per_move = info.get(
"step_sizes_per_move"
) # (n_moves,) or (n_runs, n_moves)
move_names = info.get("move_names")
# Tuple of frozensets: which reject-reason buckets each move can emit.
# None when not provided (e.g. legacy callers or run_ns_parallel).
move_reject_reasons = info.get(
"move_reject_reasons"
) # tuple[frozenset] | None
# Chain-level acceptance: always present from ns_step (preferred).
# Fall back to trial-phase acceptance_rates_per_move for backward compat.
n_accepted_per_move = info.get(
"n_accepted_per_move"
) # (n_moves,) int32
n_proposed_per_move = info.get(
"n_proposed_per_move"
) # (n_moves,) int32
# reject_reason_counts_per_move[:, 0] = accepted,
# [:, 1] = energy reject, [:, 2] = cell reject, [:, 3] = prior reject.
rr_counts_per_move = info.get(
"reject_reason_counts_per_move"
) # (n_moves, 4) or None
lines = [header]
if ss_per_move is not None:
ss = jnp.asarray(ss_per_move)
n_moves = ss.shape[-1] if ss.ndim >= 1 else 1
if move_names is None:
move_names = [f"move_{i}" for i in range(n_moves)]
# Build per-move acceptance rates from chain-level counts when available.
if (
n_accepted_per_move is not None
and n_proposed_per_move is not None
):
n_acc = jnp.asarray(n_accepted_per_move)
n_prop = jnp.asarray(n_proposed_per_move)
acc_per_move = n_acc / jnp.maximum(n_prop, 1)
rc = (
jnp.asarray(rr_counts_per_move)
if rr_counts_per_move is not None
else None
)
else:
# Backward compat: trial-phase rates from the adapt step.
trial_acc = info.get("acceptance_rates_per_move")
if trial_acc is None:
acc_per_move = None
rc = None
else:
acc_per_move = trial_acc
reject_counts = info.get("reject_counts_per_move")
rc = (
jnp.asarray(reject_counts)
if reject_counts is not None
else None
)
if acc_per_move is not None:
acc = jnp.asarray(acc_per_move)
# Flatten any leading batch dims (ndim >= 2) into one run axis so
# the batched-stats branch sees shape (n_runs_flat, n_moves).
# This handles both VmapRuns (ndim==2) and PmapVmapRuns (ndim==3,
# shape (G,P,n_moves)).
if batched and ss.ndim >= 2:
# Flatten leading shape-prefix dims to a single (n_runs,) axis.
# Prefer the batcher when present; legacy callers without
# info["_batcher"] fall back to a plain reshape.
batcher = (
info.get("_batcher") if info is not None else None
)
if batcher is not None:
ss_flat = batcher.flatten(ss)
acc_flat = batcher.flatten(acc)
rc_flat = (
batcher.flatten(jnp.asarray(rc))
if rc is not None
else None
)
else:
n_moves_here = ss.shape[-1]
ss_flat = ss.reshape(-1, n_moves_here)
acc_flat = acc.reshape(-1, n_moves_here)
rc_flat = (
jnp.asarray(rc).reshape(-1, n_moves_here, 4)
if rc is not None
else None
)
ss_mean = jnp.mean(ss_flat, axis=0)
ss_std = jnp.std(ss_flat, axis=0)
acc_mean = jnp.mean(acc_flat, axis=0)
acc_std = jnp.std(acc_flat, axis=0)
rc_sum = (
jnp.sum(rc_flat, axis=0)
if rc_flat is not None
else None
)
for k, name in enumerate(move_names):
row = (
f" {name:<16} ss={float(ss_mean[k]):>9.3e}±{float(ss_std[k]):>8.2e}"
f" acc={float(acc_mean[k]):>4.2f}±{float(acc_std[k]):>4.2f}"
)
if rc_sum is not None:
reasons_k = (
move_reject_reasons[k]
if move_reject_reasons is not None
else None
)
row += _format_reject_breakdown(
rc_sum[k], reasons_used=reasons_k
)
lines.append(row)
else:
# single-run: ss shape (n_moves,)
if ss.ndim == 2:
ss = ss[0]
acc = acc[0] if acc.ndim == 2 else acc
for k, name in enumerate(move_names):
row = (
f" {name:<16} ss={float(ss[k]):>9.3e}"
f" acc={float(acc[k]):>4.2f}"
)
if rc is not None:
reasons_k = (
move_reject_reasons[k]
if move_reject_reasons is not None
else None
)
if rc.ndim == 3:
row += _format_reject_breakdown(
rc[0, k], reasons_used=reasons_k
)
else:
row += _format_reject_breakdown(
rc[k], reasons_used=reasons_k
)
lines.append(row)
# ---- inter-RE stats row (when present) ----
re_stats = info.get("inter_re_stats")
if re_stats is not None:
n_att = re_stats.get("n_swap_pairs_attempted", 0)
n_acc = re_stats.get("n_swap_pairs_accepted", 0)
acc = n_acc / max(n_att, 1)
re_evals = re_stats.get("n_energy_evals", 0)
# Per-pair acc rates: shape (n_pairs,) = (n_runs - 1,). Mean±std
# across replica-pair indices exposes spread along the ladder
# (e.g. low-P pairs accepting more than high-P pairs).
per_pair_str = ""
n_acc_pp = re_stats.get("n_accepted_per_pair")
n_att_pp = re_stats.get("n_attempted_per_pair")
if n_acc_pp is not None and n_att_pp is not None:
n_acc_arr = np.asarray(n_acc_pp, dtype=np.float64)
n_att_arr = np.asarray(n_att_pp, dtype=np.float64)
if n_acc_arr.size > 0:
rates = np.where(
n_att_arr > 0,
n_acc_arr / np.maximum(n_att_arr, 1.0),
0.0,
)
per_pair_str = (
f" per_pair={rates.mean():.2f}±{rates.std():.2f}"
)
lines.append(
f" {'inter_re':<16} n_pairs={n_att:>3d} acc={acc:.2f}"
f"{per_pair_str} evals={re_evals}"
)
logger.info("\n".join(lines))
def on_finish(self, ns_state: Any) -> None:
elapsed = time.time() - self._start_time
if isinstance(ns_state, NSState):
iteration_arr = jnp.asarray(ns_state.iteration)
log_z_arr = jnp.asarray(ns_state.log_evidence)
else:
log_z_arr = jnp.asarray(
ns_state.get("log_evidence", float("-inf"))
)
iteration_arr = jnp.asarray(ns_state.get("iteration", 0))
# Iterations stay in lock-step across replicas (outer Python loop
# advances all runs by 1 per tick), so a single int suffices.
iter_n = int(jnp.max(iteration_arr))
elapsed_str = _format_elapsed(elapsed)
if log_z_arr.ndim > 0:
logger.info(
"NS finished: %d iterations, log_Z=[%.4f..%.4f], elapsed=%s",
iter_n,
float(jnp.min(log_z_arr)),
float(jnp.max(log_z_arr)),
elapsed_str,
)
else:
logger.info(
"NS finished: %d iterations, log_Z=%.4f, elapsed=%s",
iter_n,
float(log_z_arr),
elapsed_str,
)
[docs]
class AdaptationCallback:
"""Writes per-move adaptation data to an HDF5 trace file.
Wraps an :class:`~jaxrens.io.adaptation_log.AdaptationLogger` and calls
``write_entry`` on every iteration where ``info["step_sizes_per_move"]``
is present. Iterations between adaptation adjustments are skipped (the
info key is absent).
Also surfaces two adapter-health WARNINGs to the textual log on top of
the silent h5 trace:
* First-hit floor warning — fires once per move the first time any
replica's bisection clamps the step size to the configured floor.
Signals "the move can't shrink further; remaining rejections are
not informative for the adapter".
* Stall guard — fires once (then suppressed for ``_STALL_SUPPRESS_EVENTS``
adjust events) when a move shows mean-trial-acceptance < 0.01 AND
any replica at the floor AND no replica detecting a bracket, for
``_STALL_HISTORY_LEN`` consecutive adjust events.
Args:
logger_obj: A ready-to-use ``AdaptationLogger`` instance.
"""
_STALL_HISTORY_LEN = 5
_STALL_SUPPRESS_EVENTS = 50
def __init__(self, logger_obj: Any) -> None:
self._adaptation_logger = logger_obj
n_moves = logger_obj.n_moves
self._floor_warned: set[int] = set()
self._stall_history: list[collections.deque] = [
collections.deque(maxlen=self._STALL_HISTORY_LEN)
for _ in range(n_moves)
]
self._stall_cooldown: list[int] = [0] * n_moves
[docs]
def on_start(self, ns_state: Any, start_info: dict | None = None) -> None:
"""Write the mandatory iter-0 baseline row.
This pins the initial step sizes for post-hoc reconstruction:
active ss at iter k = last row whose ``iter <= k``. Without
this row, a non-``full_auto`` run would produce an empty file
(no adjustment events ever fire), and a ``full_auto`` run
would mis-attribute the pre-first-adjust period to the first
adjustment event's step size.
Adjustment-event-only fields (``n_rounds``, ``cap_hits``,
``floor_hits``, ``converged``, ``bracket_detected``,
``reject_reason_counts``) are written as zeros / ``True`` to
signal "no bisection ran for this row". Evaluation counters
are zero too.
Acceptance rates default to 0 here: real chain rates are
absent at iter 0 (no NS step has run yet), and the trial-phase
rates from bisection don't exist either. Downstream readers
should ignore acc rates on the iter-0 row.
"""
if start_info is None:
return
ss = start_info.get("step_sizes_per_move")
if ss is None:
return
ss_arr = jnp.asarray(ss)
batcher = start_info.get("_batcher")
if batcher is not None:
ss_flat = batcher.flatten(ss_arr)
else:
ss_flat = ss_arr[None, :] if ss_arr.ndim == 1 else ss_arr
n_runs = self._adaptation_logger.n_runs
n_moves = self._adaptation_logger.n_moves
ss_np = np.asarray(ss_flat, dtype=np.float32)
acc_np = np.zeros((n_runs, n_moves), dtype=np.float32)
zero_int = np.zeros((n_runs, n_moves), dtype=np.int32)
zero_int64 = np.zeros((n_runs, n_moves), dtype=np.int64)
baseline_adj_stats = {
"n_rounds": zero_int,
# converged=True signals "no bisection needed / not run".
"converged": np.ones((n_runs, n_moves), dtype=bool),
"cap_hits": zero_int,
"floor_hits": zero_int,
"bracket_detected": np.zeros((n_runs, n_moves), dtype=bool),
"reject_reason_counts": np.zeros(
(n_runs, n_moves, 4), dtype=np.int32
),
}
# Label the baseline with the run's starting global iteration: 0 for
# a fresh run, the restored checkpoint iteration on restart. This
# keeps the row monotonic with the rest of the log so truncate-on-
# restart (drop iter >= restart_iteration) leaves exactly one
# baseline at the resume boundary instead of a duplicate at 0.
baseline_iter = (
0
if ns_state is None
else int(jnp.asarray(ns_state.iteration).reshape(-1)[0])
)
self._adaptation_logger.write_entry(
iteration=baseline_iter,
step_sizes=ss_np,
acceptance_rates=acc_np,
adjustment_stats=baseline_adj_stats,
n_evaluations=zero_int64,
n_grad_evaluations=zero_int64,
)
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
ss = info.get("step_sizes_per_move")
acc = info.get("acceptance_rates_per_move")
if ss is None or acc is None:
return
ss_np = jnp.asarray(ss)
acc_np = jnp.asarray(acc)
# Ensure (n_runs, n_moves) shape via the batcher when available;
# legacy callers (no info["_batcher"]) fall back to ndim-based reshape.
batcher = info.get("_batcher")
if batcher is not None:
ss_np = batcher.flatten(ss_np)
acc_np = batcher.flatten(acc_np)
else:
if ss_np.ndim == 1:
ss_np = ss_np[None, :]
acc_np = acc_np[None, :]
elif ss_np.ndim >= 3:
n_moves_here = ss_np.shape[-1]
ss_np = ss_np.reshape(-1, n_moves_here)
acc_np = acc_np.reshape(-1, n_moves_here)
# Collect per-adjust-call diagnostic stats when present (v2 schema).
# All values arrive shaped ``(*shape_prefix, n_moves[, 4])`` straight
# from ``adapt_step`` — for PmapVmapRuns(G, P) that's
# ``(G, P, n_moves[, 4])``. Coerce to the flat ``(n_runs, ...)``
# layout the HDF5 schema expects using the same ``batcher.flatten``
# the ss/acc path uses above. Without this the dataset is created
# at the correct shape on the iter-0 baseline row (which is already
# flat) but real adjust events would fail with a broadcast error.
def _flatten_for_hdf5(arr):
arr = np.asarray(arr)
if batcher is not None:
return np.asarray(batcher.flatten(arr))
# No batcher available: fall back to ndim-based reshape.
if arr.ndim == 1:
return arr[None, :]
if arr.ndim >= 3:
# Last 1 or 2 axes are payload (n_moves[, 4]); rest is prefix.
# Heuristic: reject_reason_counts has trailing 4, others trail at n_moves.
tail = arr.shape[-2:] if arr.shape[-1] == 4 else arr.shape[-1:]
return arr.reshape((-1,) + tail)
return arr
adjustment_stats: "dict[str, np.ndarray] | None" = None
_adj_keys = (
"adjustment_n_rounds",
"adjustment_converged",
"adjustment_cap_hits",
"adjustment_floor_hits",
"adjustment_bracket_detected",
"reject_counts_per_move",
)
_adj_rename = {
"adjustment_n_rounds": "n_rounds",
"adjustment_converged": "converged",
"adjustment_cap_hits": "cap_hits",
"adjustment_floor_hits": "floor_hits",
"adjustment_bracket_detected": "bracket_detected",
"reject_counts_per_move": "reject_reason_counts",
}
for info_key in _adj_keys:
val = info.get(info_key)
if val is not None:
if adjustment_stats is None:
adjustment_stats = {}
adjustment_stats[_adj_rename[info_key]] = _flatten_for_hdf5(
val
)
# Collect per-iter evaluation counts for v3 schema (shape (n_runs, n_moves))
n_evals_raw = info.get("n_evaluations_per_move")
n_grad_evals_raw = info.get("n_grad_evaluations_per_move")
n_evals_np: "np.ndarray | None" = None
n_grad_evals_np: "np.ndarray | None" = None
if n_evals_raw is not None:
n_evals_np = np.asarray(
_flatten_for_hdf5(n_evals_raw),
dtype=np.int64,
)
if n_grad_evals_raw is not None:
n_grad_evals_np = np.asarray(
_flatten_for_hdf5(n_grad_evals_raw),
dtype=np.int64,
)
self._adaptation_logger.write_entry(
iteration=iteration,
step_sizes=np.asarray(ss_np),
acceptance_rates=np.asarray(acc_np),
adjustment_stats=adjustment_stats,
n_evaluations=n_evals_np,
n_grad_evaluations=n_grad_evals_np,
)
self._check_adapter_health(
iteration=iteration,
ss_np=np.asarray(ss_np),
acc_np=np.asarray(acc_np),
adjustment_stats=adjustment_stats,
)
def _check_adapter_health(
self,
iteration: int,
ss_np: np.ndarray,
acc_np: np.ndarray,
adjustment_stats: "dict[str, np.ndarray] | None",
) -> None:
"""Emit WARNINGs when adapter diagnostics indicate a dying move.
Reads the same arrays the h5 logger just wrote (``(n_runs, n_moves)``
layout) and applies two cheap heuristics:
* first-hit floor — set membership so each move warns at most once;
* stall guard — fixed-length deque of bool flags per move, warning
when every entry is ``True`` and the cooldown counter has elapsed.
``adjustment_stats == None`` is the non-``full_auto`` case (no
bisection ever ran). Floor / bracket info is absent, so we silently
return — there's nothing to diagnose.
"""
if adjustment_stats is None:
return
floor_hits = adjustment_stats.get("floor_hits")
if floor_hits is None:
return
floor_hits = np.asarray(floor_hits) # (n_runs, n_moves) int32
bracket_detected = adjustment_stats.get("bracket_detected")
bracket_detected = (
np.asarray(bracket_detected)
if bracket_detected is not None
else np.zeros_like(floor_hits, dtype=bool)
)
move_names = self._adaptation_logger.move_names
n_runs, n_moves = floor_hits.shape
for m in range(n_moves):
name = move_names[m]
floor_runs = int((floor_hits[:, m] > 0).sum())
bracketed_runs = int(bracket_detected[:, m].sum())
mean_acc = float(acc_np[:, m].mean())
mean_ss = float(ss_np[:, m].mean())
if floor_runs > 0 and m not in self._floor_warned:
logger.warning(
"[adapt] iter=%d move=%s hit step-size floor on %d/%d "
"replicas (ss=%.2e, mean trial acc=%.3f). Further "
"shrinking is a no-op; if acc stays ~0 the move is "
"functionally dead.",
iteration,
name,
floor_runs,
n_runs,
mean_ss,
mean_acc,
)
self._floor_warned.add(m)
is_pathological = (
mean_acc < 0.01 and floor_runs > 0 and bracketed_runs == 0
)
self._stall_history[m].append(is_pathological)
if self._stall_cooldown[m] > 0:
self._stall_cooldown[m] -= 1
continue
hist = self._stall_history[m]
if len(hist) == hist.maxlen and all(hist):
logger.warning(
"[adapt] iter=%d move=%s STALLED: mean trial acc<0.01, "
"ss=%.2e at floor on %d/%d replicas, bracket never "
"detected across last %d adjustment events. This move's "
"DOFs are effectively frozen; subsequent dead points are "
"correlated clones in this DOF.",
iteration,
name,
mean_ss,
floor_runs,
n_runs,
self._STALL_HISTORY_LEN,
)
self._stall_cooldown[m] = self._STALL_SUPPRESS_EVENTS
def on_finish(self, ns_state: Any) -> None:
self._adaptation_logger.close()
[docs]
class AccRatesCallback:
"""Writes per-iteration chain-phase per-move acceptance counts to HDF5.
Independent of ``full_auto``: the chain counters ``n_accepted_per_move``
and ``n_proposed_per_move`` are populated by ``ns_step`` on every
iteration, so this callback fires regardless of whether bisection
adaptation is active. ``AdaptationCallback`` complements this by
capturing the *sparse* step-size event series; this callback captures
the *dense* per-iter chain-rate series.
Stores raw counts (not derived rates) so downstream code can
re-aggregate over arbitrary windows.
Args:
logger_obj: A ready-to-use ``AccRatesLogger`` instance.
interval: Fire every ``interval`` iterations. Default ``1``
(every iter); set higher to reduce I/O on long runs.
"""
def __init__(self, logger_obj: Any, interval: int = 1) -> None:
self._logger = logger_obj
self._interval = max(1, int(interval))
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if iteration % self._interval != 0:
return
n_acc = info.get("n_accepted_per_move")
n_prop = info.get("n_proposed_per_move")
if n_acc is None or n_prop is None:
return
n_acc_arr = jnp.asarray(n_acc)
n_prop_arr = jnp.asarray(n_prop)
batcher = info.get("_batcher")
if batcher is not None:
n_acc_arr = batcher.flatten(n_acc_arr)
n_prop_arr = batcher.flatten(n_prop_arr)
else:
if n_acc_arr.ndim == 1:
n_acc_arr = n_acc_arr[None, :]
n_prop_arr = n_prop_arr[None, :]
elif n_acc_arr.ndim >= 3:
n_moves_here = n_acc_arr.shape[-1]
n_acc_arr = n_acc_arr.reshape(-1, n_moves_here)
n_prop_arr = n_prop_arr.reshape(-1, n_moves_here)
self._logger.write_entry(
iteration=iteration,
n_accepted=np.asarray(n_acc_arr, dtype=np.int64),
n_proposed=np.asarray(n_prop_arr, dtype=np.int64),
)
def on_finish(self, ns_state: Any) -> None:
self._logger.close()
[docs]
class MaxNeighborsCallback:
"""Writes per-iteration neighbor-bucket diagnostics to HDF5.
Captures the full per-walker ``max_neighbor_count`` distribution at
each iteration, alongside the current bucket size and the overflow
flag — enough to plot the per-iteration distribution of neighbor
counts vs the current bucket boundary, and to diagnose bucket
growth/shrink dynamics.
Reads directly off the post-step ``ns_state``: no info-dict keys
are required. For sharded runs, the redundant leading-G axis on
info is collapsed upstream by ``_run_loop``, but the population
leaves are already gathered to ``(K, ...)`` via
``_gather_sharded_ns_state``.
Args:
logger_obj: A ready-to-use ``MaxNeighborsLogger`` instance.
interval: Fire every ``interval`` iterations. Default ``1``.
"""
def __init__(self, logger_obj: Any, interval: int = 1) -> None:
self._logger = logger_obj
self._interval = max(1, int(interval))
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if iteration % self._interval != 0:
return
pop = ns_state.population
counts = jnp.asarray(pop.max_neighbor_count)
# ``max_neighbors`` is a static int shared across every replica/walker
# in this run. Same value across the population, so pass the scalar
# and let the logger broadcast to ``(n_runs,)``.
bucket_scalar = int(pop.max_neighbors)
# ``overflow`` is per-walker (shape ``(*B, n_walkers)``). The
# logger stores a per-run flag, so reduce across walkers via
# ``any`` after flattening the batch prefix.
of = jnp.asarray(pop.overflow)
batcher = info.get("_batcher")
if batcher is not None:
counts = batcher.flatten(counts)
of = batcher.flatten(of)
else:
if counts.ndim == 1:
counts = counts[None, :]
of_per_run = jnp.any(of, axis=-1) if of.ndim > 0 else of
self._logger.write_entry(
iteration=iteration,
max_neighbor_count=np.asarray(counts, dtype=np.int32),
bucket_size=np.int32(bucket_scalar),
overflow=np.asarray(of_per_run, dtype=np.bool_),
)
def on_finish(self, ns_state: Any) -> None:
self._logger.close()
[docs]
class RECallback:
"""Writes per-fire inter-RE swap counts to HDF5.
Reads ``info["inter_re_stats"]`` (populated by ``run_loop`` when
the manager fires) and forwards the per-pair counts to an
:class:`~jaxrens.io.re_stats_log.RELogger`. Skips iterations on
which the manager did not fire (info key absent or
``n_attempted_per_pair`` is empty / sums to zero) — the resulting
file's iteration index is therefore the authoritative record of
when the manager actually did work.
Args:
logger_obj: A ready-to-use ``RELogger`` instance.
"""
def __init__(self, logger_obj: Any) -> None:
self._logger = logger_obj
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
re_stats = info.get("inter_re_stats")
if re_stats is None:
return
n_att = re_stats.get("n_attempted_per_pair")
if n_att is None:
return
n_att_arr = np.asarray(n_att, dtype=np.int32)
if n_att_arr.size == 0 or int(n_att_arr.sum()) == 0:
# Manager didn't fire this iteration (or fired with no
# valid pairs). Skip — keeps the file iteration-indexed
# by actual fires only.
return
n_acc_arr = np.asarray(re_stats["n_accepted_per_pair"], dtype=np.int32)
self._logger.write_entry(
iteration=iteration,
n_accepted_per_pair=n_acc_arr,
n_attempted_per_pair=n_att_arr,
)
def on_finish(self, ns_state: Any) -> None:
self._logger.close()
[docs]
class EnergyCheckCallback:
"""Warns if any replica's Emax fails to decrease between iterations.
Compares ``info["emax"]`` element-wise against the previous iteration; a
global ``max``-reduction would let one descending replica mask another
replica's non-monotonic contour (the failure mode that previously hid
`https://github.com/.../pressure-rens-writeback-bug`). Works for any
batched shape — scalars (``SingleRun``), 1-D ``(R,)`` (``VmapRuns``),
2-D ``(G, P)`` (``PmapVmapRuns``).
"""
def __init__(self):
self._prev_emax: np.ndarray | None = None
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
emax_arr = np.asarray(info.get("emax", 0.0), dtype=np.float64)
if iteration > 0 and self._prev_emax is not None:
offenders = emax_arr > self._prev_emax
if bool(np.any(offenders)):
# Build per-replica diagnostic for the offending indices.
idxs = np.argwhere(offenders)
deltas = (emax_arr - self._prev_emax)[offenders]
# Report up to 4 worst, sorted by delta descending.
order = np.argsort(-deltas)
def _fmt_idx(coords: np.ndarray) -> str:
parts = tuple(int(c) for c in coords)
return (
str(parts[0])
if len(parts) == 1
else (",".join(str(p) for p in parts))
)
worst = [
(_fmt_idx(idxs[k]), float(deltas[k])) for k in order[:4]
]
summary = ", ".join(
f"replica[{i}]: Δ=+{d:.4g}" for i, d in worst
)
logger.warning(
"iter=%d: Emax non-monotonic on %d replica(s) (worst: %s)",
iteration,
int(offenders.sum()),
summary,
)
self._prev_emax = emax_arr
def on_finish(self, ns_state: Any) -> None:
pass
def _min_pair_distance_pbc(
positions: jnp.ndarray, cell: jnp.ndarray
) -> jnp.ndarray:
"""Minimum interatomic distance under PBC, vmapped over (n_runs, K).
``positions``: ``(n_runs, K, N, 3)``. ``cell``: ``(n_runs, K, 3, 3)``.
Returns ``(n_runs, K)`` minimum pair distance per walker, using the
minimum-image convention.
"""
def _one(p, c):
cell_inv = jnp.linalg.inv(c)
frac = p @ cell_inv
diff_frac = frac[:, None, :] - frac[None, :, :]
diff_frac = diff_frac - jnp.round(diff_frac)
diff = diff_frac @ c
d2 = jnp.sum(diff**2, axis=-1)
n = p.shape[0]
d2 = jnp.where(jnp.eye(n, dtype=bool), jnp.inf, d2)
return jnp.sqrt(jnp.min(d2))
return jax.vmap(jax.vmap(_one))(positions, cell)
def _min_pair_distance_open(positions: jnp.ndarray) -> jnp.ndarray:
"""All-pairs minimum distance (no PBC), vmapped over (n_runs, K).
``positions``: ``(n_runs, K, N, 3)``. Returns ``(n_runs, K)``.
"""
def _one(p):
diff = p[:, None, :] - p[None, :, :]
d2 = jnp.sum(diff**2, axis=-1)
n = p.shape[0]
d2 = jnp.where(jnp.eye(n, dtype=bool), jnp.inf, d2)
return jnp.sqrt(jnp.min(d2))
return jax.vmap(jax.vmap(_one))(positions)
[docs]
class CollisionCheckCallback:
"""Periodically scans walkers for unphysically close atom pairs.
Computes the minimum interatomic distance per walker using the
minimum-image convention when ``population.cell`` is present, and
plain all-pairs otherwise. When any walker falls below
``threshold``, a warning is logged identifying the worst-offender
replicas + walkers and their minimum pair distance.
Diagnostic only — never feeds back into the run. Works for
SingleRun, VmapRuns, and PmapVmapRuns shape conventions via
``info["_batcher"]``.
Args:
threshold: Pair distance below which a warning fires. Backend
length units (typically Å).
interval: Fire every ``interval`` iterations. Default 100 —
this is a diagnostic, not a per-iter signal.
max_offenders: Cap on the number of (run, walker) entries
included in the warning message. Default 4.
"""
def __init__(
self,
threshold: float,
interval: int = 100,
max_offenders: int = 4,
) -> None:
self._threshold = float(threshold)
self._interval = max(1, int(interval))
self._max_offenders = max(1, int(max_offenders))
self._jit_pbc = jax.jit(_min_pair_distance_pbc)
self._jit_open = jax.jit(_min_pair_distance_open)
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if iteration % self._interval != 0:
return
pop = ns_state.population
positions = jnp.asarray(pop.positions)
cell = pop.cell
batcher = info.get("_batcher")
if batcher is not None:
positions_flat = batcher.flatten(positions)
cell_flat = batcher.flatten(cell) if cell is not None else None
else:
positions_flat = (
positions[None, ...] if positions.ndim == 3 else positions
)
if cell is not None:
cell_flat = cell[None, ...] if cell.ndim == 3 else cell
else:
cell_flat = None
if cell_flat is not None:
mins = self._jit_pbc(positions_flat, jnp.asarray(cell_flat))
else:
mins = self._jit_open(positions_flat)
mins_np = np.asarray(mins)
offenders = mins_np < self._threshold
if not bool(np.any(offenders)):
return
idxs = np.argwhere(offenders)
offending_d = mins_np[offenders]
order = np.argsort(offending_d)
worst = [
(int(idxs[k, 0]), int(idxs[k, 1]), float(offending_d[k]))
for k in order[: self._max_offenders]
]
summary = ", ".join(
f"run[{r}].walker[{w}]: d_min={d:.4g}" for r, w, d in worst
)
logger.warning(
"iter=%d: %d walker(s) have atom pairs below %.4g " "(worst: %s)",
iteration,
int(offenders.sum()),
self._threshold,
summary,
)
def on_finish(self, ns_state: Any) -> None:
pass
[docs]
class TemperatureCallback:
"""Finite-difference temperature estimator (Baldock et al., PRE 96, 043311, 2017).
Maintains a per-replica FIFO of the last ``lag`` ``info["emax"]`` values and
reports ``T = 1 / (k_B * beta)`` with
``beta = (L - 1) * log_alpha / (E_last - E_first)`` and
``log_alpha = log((n_live + 1 - n_cull) / (n_live + 1))``.
Diagnostic only — never feeds back into the run. Works for SingleRun,
VmapRuns, and PmapVmapRuns shape conventions via ``info["_batcher"]``.
Returns NaN when the deque is shorter than 2 samples or when
``E_last == E_first`` (avoids ``ZeroDivisionError``); downstream
aggregation uses ``np.nan{min,max,median}``.
Args:
n_live: Number of live walkers (from ``NSConfig.n_live``).
n_cull: Number of culled walkers per iteration (from ``NSConfig.n_cull``).
lag: Length of the Emax FIFO (window size for the finite difference).
interval: Emit a log line every ``interval`` iterations.
kB: Boltzmann constant in the backend's energy units. Default
``8.6173324e-5`` eV/K (ASE convention). Set ``1.0`` for
reduced-unit backends (LJ, harmonic).
"""
def __init__(
self,
n_live: int,
n_cull: int = 1,
lag: int = 100,
interval: int = 100,
kB: float = 8.6173324e-5,
) -> None:
self._lag = int(lag)
self._interval = max(1, int(interval))
self._kB = float(kB)
self._log_alpha = float(np.log((n_live + 1 - n_cull) / (n_live + 1)))
self._histories: list[collections.deque] | None = None
def on_start(self, ns_state: Any, start_info: dict | None = None) -> None:
batcher = (start_info or {}).get("_batcher") if start_info else None
n_runs = batcher.n_runs if batcher is not None else 1
self._histories = [
collections.deque(maxlen=self._lag) for _ in range(n_runs)
]
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if self._histories is None:
# on_start was not called (legacy/test caller) — lazy-init from info.
batcher = info.get("_batcher")
n_runs = batcher.n_runs if batcher is not None else 1
self._histories = [
collections.deque(maxlen=self._lag) for _ in range(n_runs)
]
emax_arr = jnp.asarray(info.get("emax", 0.0))
batcher = info.get("_batcher")
if batcher is not None:
flat = batcher.flatten(emax_arr)
elif emax_arr.ndim == 0:
flat = emax_arr[None]
else:
flat = emax_arr
flat_np = np.asarray(flat).reshape(-1)
if flat_np.shape[0] != len(self._histories):
# Resize the per-replica list if the run topology changed since
# on_start (e.g. tests that swap batchers mid-run).
self._histories = [
collections.deque(maxlen=self._lag)
for _ in range(flat_np.shape[0])
]
for k, e in enumerate(flat_np):
self._histories[k].append(float(e))
if iteration % self._interval != 0:
return
T = np.array(
[self._compute_T(h) for h in self._histories], dtype=np.float64
)
# All replicas still warming up — emit a pending line instead of
# letting nanmin/nanmax fire All-NaN RuntimeWarnings.
if np.all(np.isnan(T)):
logger.info("iter=%d T_est=pending (deque < 2)", iteration)
return
if T.size == 1:
logger.info("iter=%d T_est=%.3f K", iteration, float(T[0]))
else:
logger.info(
"iter=%d T_est=[%.3f..%.3f] K median=%.3f",
iteration,
float(np.nanmin(T)),
float(np.nanmax(T)),
float(np.nanmedian(T)),
)
def _compute_T(self, history: collections.deque) -> float:
if len(history) < 2:
return float("nan")
dE = history[-1] - history[0]
if dE == 0.0:
return float("nan")
beta = (len(history) - 1) * self._log_alpha / dE
return 1.0 / (self._kB * beta)
def on_finish(self, ns_state: Any) -> None:
pass
[docs]
class CheckpointCallback:
"""Saves checkpoints at configured intervals."""
def __init__(
self,
working_dir: Path | str,
interval: int = 100,
prefix: str = "ns",
symbol_map: dict[int, str] | None = None,
):
self.working_dir = Path(working_dir)
self.interval = interval
self.prefix = prefix
self.symbol_map = symbol_map
def on_start(self, ns_state: Any, start_info: dict | None = None) -> None:
from jaxrens.io.checkpoint import save_checkpoint
path = self.working_dir / f"{self.prefix}.initial.checkpoint.h5"
state_dict = (
_ns_state_to_checkpoint_dict(ns_state)
if isinstance(ns_state, NSState)
else ns_state
)
save_checkpoint(path, state_dict, self.symbol_map)
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if iteration > 0 and iteration % self.interval == 0:
from jaxrens.io.checkpoint import save_checkpoint
path = self.working_dir / f"{self.prefix}.checkpoint.h5"
state_dict = (
_ns_state_to_checkpoint_dict(ns_state)
if isinstance(ns_state, NSState)
else ns_state
)
save_checkpoint(path, state_dict, self.symbol_map)
def on_finish(self, ns_state: Any) -> None:
from jaxrens.io.checkpoint import save_checkpoint
path = self.working_dir / f"{self.prefix}.final.checkpoint.h5"
state_dict = (
_ns_state_to_checkpoint_dict(ns_state)
if isinstance(ns_state, NSState)
else ns_state
)
save_checkpoint(path, state_dict, self.symbol_map)
[docs]
class MemProfileCallback:
"""One-shot device-memory snapshot on the first post-compile iteration.
Intended as a debug aid when the NS loop OOMs or when you want to see
which HLO op owns the peak allocation (typically the vmap'd backward
pass through the energy backend inside the galilean/HMC kernels).
Enabled only when the ``JAXRENS_MEMPROF`` env var names the output
filename; otherwise the callback is not constructed and the run pays
nothing. Inspect the result with ``pprof -top -cum <file.prof.gz>``.
Fires twice at most: once from ``on_start`` with the ``.baseline``
suffix (captures post-init state before any NS compile) and once from
the first ``on_iteration`` after the first step completes (captures
the steady-state live set). The baseline snapshot is the only one
that survives when iter 0 itself OOMs — which is the common failure
mode we're trying to diagnose.
"""
def __init__(self, out_path: Path | str):
self.out_path = str(out_path)
self.iter_done = False
def _baseline_path(self) -> str:
p = Path(self.out_path)
return str(p.with_name(p.stem + ".baseline" + p.suffix))
def on_start(self, ns_state: Any, start_info: dict | None = None) -> None:
import jax
jax.profiler.save_device_memory_profile(self._baseline_path())
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
if self.iter_done:
return
import jax
jax.profiler.save_device_memory_profile(self.out_path)
self.iter_done = True
[docs]
class TrajectoryCallback:
"""Writes dead points and snapshots at configured intervals."""
def __init__(
self,
writer: Any,
energy_logger: Any | None = None,
traj_interval: int = 1,
snapshot_interval: int = 100,
):
self.writer = writer
self.energy_logger = energy_logger
self.traj_interval = traj_interval
self.snapshot_interval = snapshot_interval
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
# ``ns_step`` surfaces this iteration's dead points as one
# ``WalkerState`` pytree with a leading ``(n_cull,)`` axis in
# descending energy under ``info["dead_walkers"]``. Older callers
# (and hand-built test infos) only carry the single ``dead_walker``.
dead_ws = info.get("dead_walkers")
if dead_ws is None:
dead_ws = jax.tree.map(lambda x: x[None], info["dead_walker"])
dead_cells = np.asarray(dead_ws.cell)
dead_energies = np.asarray(dead_ws.energy, dtype=np.float64).reshape(
-1
)
n_cull = dead_energies.shape[0]
if n_cull == 1:
# Single cull: record the NS contour exactly as before (identical
# to the walker's own energy).
dead_energies = np.asarray(
[float(info.get("emax", dead_energies[0]))]
)
for k in range(n_cull):
dead_cell = dead_cells[k]
if iteration % self.traj_interval == 0:
# Carry each dead point's energy on its WalkerState so the
# writer records it. Drop a degenerate (all-zero) cell to
# None so it serializes as non-periodic.
cell = (
jnp.asarray(dead_cell) if np.any(dead_cell != 0) else None
)
dead_walker = jax.tree.map(lambda x: x[k], dead_ws).set(
energy=jnp.asarray(dead_energies[k]), cell=cell
)
self.writer.write_dead_point(
iteration,
dead_walker,
float(dead_energies[k]),
cull_rank=k if n_cull > 1 else None,
)
if self.energy_logger is not None:
# One ``.energies`` row per dead point (n_cull per iteration,
# descending energy — the pymatnest layout), with the volume
# derived from the dead walker's own cell.
self.energy_logger.write_entry(
iteration,
float(dead_energies[k]),
volume=float(abs(np.linalg.det(dead_cell))),
)
if (
self.snapshot_interval
and iteration > 0
and iteration % self.snapshot_interval == 0
):
if isinstance(ns_state, NSState):
snapshot_dict = _ns_state_to_checkpoint_dict(ns_state)
self.writer.write_walker_snapshot(iteration, snapshot_dict)
else:
self.writer.write_walker_snapshot(iteration, ns_state)
def on_finish(self, ns_state: Any) -> None:
self.writer.close()
if self.energy_logger is not None:
self.energy_logger.close()
[docs]
class BatchedTrajectoryCallback:
"""Trajectory + energy logging for multi-run NS.
Holds one writer and one (optional) energy logger per replica. On each
iteration, iterates flat replica indices and writes the per-replica dead
walker + energy entry. Snapshots follow the same pattern.
For ``PmapVmapRuns`` the state arrays have leading axes ``(G, P, ...)``;
this callback flattens them to ``(G*P, ...)`` for per-replica slicing.
"""
def __init__(
self,
writers: list,
energy_loggers: list | None = None,
traj_interval: int = 1,
snapshot_interval: int = 100,
):
self.writers = list(writers)
self.energy_loggers = (
list(energy_loggers) if energy_loggers is not None else None
)
if self.energy_loggers is not None and len(self.energy_loggers) != len(
self.writers
):
raise ValueError(
f"BatchedTrajectoryCallback: len(writers)={len(self.writers)} vs "
f"len(energy_loggers)={len(self.energy_loggers)}."
)
self.traj_interval = traj_interval
self.snapshot_interval = snapshot_interval
def on_iteration(self, iteration: int, ns_state: Any, info: dict) -> None:
# Use the batcher (when present in info) to flatten ``(*shape_prefix, …)``
# arrays to ``(n_runs, …)`` for per-replica indexing. Fall back to the
# legacy ndim-sniff when callers don't pass info["_batcher"].
batcher = info.get("_batcher") if info is not None else None
if batcher is not None:
n_runs = batcher.n_runs
flatten = batcher.flatten
else:
le = jnp.asarray(ns_state.log_evidence)
n_batch_axes = max(1, int(le.ndim))
n_runs = int(np.prod(le.shape)) if le.ndim >= 1 else 1
def flatten(x):
arr = x if isinstance(x, np.ndarray) else jnp.asarray(x)
if n_batch_axes == 1:
return arr
return arr.reshape((-1,) + arr.shape[n_batch_axes:])
if n_runs != len(self.writers):
raise RuntimeError(
f"BatchedTrajectoryCallback: ns_state has {n_runs} replicas "
f"but {len(self.writers)} writers were registered."
)
pop = ns_state.population
# ``info["emax"]`` is (*shape_prefix,) when batched; flatten via the
# batcher (or by ravel for the legacy fallback).
emax_arr = jnp.asarray(info.get("emax", 0.0))
if batcher is not None:
emax_flat = batcher.flatten(emax_arr)
else:
emax_flat = (
emax_arr.reshape(-1) if emax_arr.ndim >= 1 else emax_arr[None]
)
# Dead walkers are one ``WalkerState`` pytree from ns_step with leaves
# ``(*shape_prefix, n_cull, ...)`` (descending energy). Fall back to
# the single ``dead_walker`` (no cull axis) for legacy infos. Flatten
# each leaf over the replica axis once; per-replica slicing then uses
# simple integer indexing.
dead_ws = info.get("dead_walkers")
has_cull_axis = dead_ws is not None
if not has_cull_axis:
dead_ws = info["dead_walker"]
dead_pos_flat = np.asarray(flatten(dead_ws.positions))
dead_types_flat = np.asarray(flatten(dead_ws.types))
dead_cell_flat = np.asarray(flatten(dead_ws.cell))
dead_e_flat = np.asarray(flatten(dead_ws.energy), dtype=np.float64)
if not has_cull_axis:
dead_pos_flat = dead_pos_flat[:, None]
dead_types_flat = dead_types_flat[:, None]
dead_cell_flat = dead_cell_flat[:, None]
dead_e_flat = dead_e_flat[:, None]
n_cull = int(dead_pos_flat.shape[1])
n_atoms = int(dead_pos_flat.shape[2])
if n_cull == 1:
# Single cull: record the NS contour exactly as before.
dead_e_flat = np.asarray(emax_flat, dtype=np.float64).reshape(
-1, 1
)
for r in range(n_runs):
for k in range(n_cull):
cell_rk = dead_cell_flat[r, k]
if iteration % self.traj_interval == 0:
# Build the per-replica dead walker as a typed WalkerState
# (jnp arrays satisfy the jaxtyped fields). A degenerate
# all-zero cell serializes as non-periodic (cell=None).
dead_walker = WalkerState(
positions=jnp.asarray(dead_pos_flat[r, k]),
types=jnp.asarray(dead_types_flat[r, k]),
energy=jnp.asarray(dead_e_flat[r, k]),
cell=(
jnp.asarray(cell_rk)
if np.any(cell_rk != 0)
else None
),
n_atoms=n_atoms,
)
self.writers[r].write_dead_point(
iteration,
dead_walker,
float(dead_e_flat[r, k]),
cull_rank=k if n_cull > 1 else None,
)
if self.energy_loggers is not None:
# Per-replica volume derived from the dead walker's cell.
self.energy_loggers[r].write_entry(
iteration,
float(dead_e_flat[r, k]),
volume=float(abs(np.linalg.det(cell_rk))),
)
if (
self.snapshot_interval
and iteration > 0
and iteration % self.snapshot_interval == 0
):
# Live-walker snapshot — separate from the dead-point write
# path; needs the full per-replica live population, not the
# culled walker.
positions_flat = flatten(pop.positions)
energies_flat = flatten(pop.energy)
types_flat = flatten(pop.types)
cells_flat = flatten(pop.cell)
for r in range(n_runs):
snap = population_record(
positions_flat[r],
types_flat[r] if types_flat.ndim >= 2 else types_flat,
energies_flat[r],
cells_flat[r] if cells_flat.ndim >= 3 else cells_flat,
iteration=iteration,
n_dead=iteration,
)
self.writers[r].write_walker_snapshot(iteration, snap)
def on_finish(self, ns_state: Any) -> None:
for w in self.writers:
w.close()
if self.energy_loggers is not None:
for e in self.energy_loggers:
e.close()