diff --git a/autofit/non_linear/search/nest/nautilus/checkpoint_policy.py b/autofit/non_linear/search/nest/nautilus/checkpoint_policy.py new file mode 100644 index 000000000..409fdeebd --- /dev/null +++ b/autofit/non_linear/search/nest/nautilus/checkpoint_policy.py @@ -0,0 +1,84 @@ +"""Experimental checkpoint batching for the native Nautilus 1.0.5 schema. + +Only persistence changes. Full native snapshots include every dirty shell, +bound, cache and RNG state; incremental global metadata is never published over +older shell data. Atomic replacement protects the previous checkpoint against +process interruption. This is not a power-loss or network-filesystem guarantee. +""" + +from importlib.metadata import version +from numbers import Integral +import os +from pathlib import Path +import tempfile + +from nautilus import Sampler + + +def validate_interval(interval): + if isinstance(interval, bool) or not isinstance(interval, Integral) or interval < 1: + raise ValueError("checkpoint_interval_batches must be a positive integer") + return int(interval) + + +class BatchedCheckpointSampler(Sampler): + """Defer incremental writes, retaining native full-write boundaries. + + An interruption can lose completed batches since the last published + snapshot plus work in flight. The interval counts native shell-update + notifications, not wall time or PyAutoFit output updates. No sampler RNG + calls are made by this policy. + """ + + def __init__(self, *args, checkpoint_interval_batches=5, **kwargs): + self.checkpoint_interval_batches = validate_interval(checkpoint_interval_batches) + if self.checkpoint_interval_batches == 1: + raise ValueError("Use the native Sampler for checkpoint interval 1") + if version("nautilus-sampler") != "1.0.5": + raise ValueError("Checkpoint batching is validated only for nautilus-sampler 1.0.5") + self._pending_checkpoint_batches = 0 + super().__init__(*args, **kwargs) + + def write(self, filepath, overwrite=False): + """Publish a complete native snapshot without unlinking the good file.""" + filepath = Path(filepath) + if filepath.suffix not in (".h5", ".hdf5"): + raise ValueError("File ending must be '.h5' or '.hdf5'.") + if filepath.exists() and not overwrite: + raise RuntimeError(f"File {filepath} already exists.") + filepath.parent.mkdir(parents=True, exist_ok=True) + fd, name = tempfile.mkstemp(prefix=f".{filepath.name}.", suffix=".hdf5", dir=filepath.parent) + os.close(fd) + temporary = Path(name) + # Native write creates its file exclusively. Reserve a unique name, + # then let the native writer own creation and HDF5 close/flush. + temporary.unlink() + try: + super().write(temporary) + with temporary.open("rb") as stream: + os.fsync(stream.fileno()) + os.replace(temporary, filepath) + finally: + temporary.unlink(missing_ok=True) + if self.filepath is not None and filepath.absolute() == Path(self.filepath).absolute(): + self._pending_checkpoint_batches = 0 + + def write_shell_update(self, filepath, shell): + # Never write a single shell alongside global metadata newer than the + # other deferred shells. An explicit export to another path is immediate. + if self.filepath is None or Path(filepath).absolute() != Path(self.filepath).absolute(): + return self.write(filepath, overwrite=True) + self._pending_checkpoint_batches += 1 + if self._pending_checkpoint_batches >= self.checkpoint_interval_batches: + self.write(filepath, overwrite=True) + + def flush_checkpoint(self): + if self.filepath is not None and self._pending_checkpoint_batches: + self.write(self.filepath, overwrite=True) + + def run(self, *args, **kwargs): + result = super().run(*args, **kwargs) + # Deliberately not a finally block: exceptions may leave half-mutated + # in-memory state. Resume from the last complete published file instead. + self.flush_checkpoint() + return result diff --git a/autofit/non_linear/search/nest/nautilus/search.py b/autofit/non_linear/search/nest/nautilus/search.py index 2408b215c..d6bd3eaee 100644 --- a/autofit/non_linear/search/nest/nautilus/search.py +++ b/autofit/non_linear/search/nest/nautilus/search.py @@ -346,6 +346,7 @@ def __init__( session: Optional[sa.orm.Session] = None, use_jax_vmap: Optional[bool] = None, checkpoint: bool = True, + checkpoint_interval_batches: int = 1, **kwargs, ): """ @@ -414,8 +415,25 @@ def __init__( normally: the search writes the file in the HDF5 1.8 format, because nautilus 1.0.5's default format cannot store their bounds (``_large_attribute_hdf5``). + checkpoint_interval_batches + Experimental persistence policy. The default 1 keeps native Nautilus + checkpointing. Larger positive integers batch shell updates into + complete atomic snapshots, retaining native full-write points and + flushing on normal run returns. An interruption can lose recent + batches plus work in flight. Requires checkpoint=True and Nautilus + 1.0.5; does not change sampling settings or the search identifier. """ + from numbers import Integral + + if (isinstance(checkpoint_interval_batches, bool) + or not isinstance(checkpoint_interval_batches, Integral) + or checkpoint_interval_batches < 1): + raise ValueError("checkpoint_interval_batches must be a positive integer") + if not checkpoint and checkpoint_interval_batches != 1: + raise ValueError("checkpoint_interval_batches > 1 requires checkpoint=True") + self.checkpoint_interval_batches = int(checkpoint_interval_batches) + super().__init__( name=name, path_prefix=path_prefix, @@ -586,6 +604,14 @@ def sampler_cls(self): try: from nautilus import Sampler + if getattr(self, "checkpoint_interval_batches", 1) > 1: + from functools import partial + from .checkpoint_policy import BatchedCheckpointSampler + + return partial( + BatchedCheckpointSampler, + checkpoint_interval_batches=self.checkpoint_interval_batches, + ) return Sampler except ModuleNotFoundError: raise ModuleNotFoundError( diff --git a/test_autofit/non_linear/search/conformance_roster.py b/test_autofit/non_linear/search/conformance_roster.py index d02f69666..e9ebdcde8 100644 --- a/test_autofit/non_linear/search/conformance_roster.py +++ b/test_autofit/non_linear/search/conformance_roster.py @@ -483,6 +483,7 @@ class Golden: "session", "number_of_cores", "checkpoint", + "checkpoint_interval_batches", "discard_exploration", "enlarge_per_dim", "f_live", diff --git a/test_autofit/non_linear/search/nest/test_nautilus.py b/test_autofit/non_linear/search/nest/test_nautilus.py index b3ee0ef1a..ed9571e99 100644 --- a/test_autofit/non_linear/search/nest/test_nautilus.py +++ b/test_autofit/non_linear/search/nest/test_nautilus.py @@ -685,7 +685,8 @@ def update(self, group): @requires_nautilus -def test__d100_fit_writes_a_checkpoint_that_nautilus_resumes(tmp_path, monkeypatch): +@pytest.mark.parametrize("checkpoint_interval", [1, 5]) +def test__d100_fit_writes_a_checkpoint_that_nautilus_resumes(tmp_path, monkeypatch, checkpoint_interval): """ A D = 100 fit reaches its first checkpoint write after an ellipsoid bound (nautilus-sampler 1.0.5 raised ``object header message is too large`` there), @@ -711,6 +712,7 @@ def test__d100_fit_writes_a_checkpoint_that_nautilus_resumes(tmp_path, monkeypat search = af.Nautilus( name="nautilus_d100_checkpoint", + checkpoint_interval_batches=checkpoint_interval, n_live=101, n_networks=1, n_batch=101, diff --git a/test_autofit/non_linear/search/nest/test_nautilus_checkpoint_policy.py b/test_autofit/non_linear/search/nest/test_nautilus_checkpoint_policy.py new file mode 100644 index 000000000..98a022387 --- /dev/null +++ b/test_autofit/non_linear/search/nest/test_nautilus_checkpoint_policy.py @@ -0,0 +1,194 @@ +"""Native schema, trajectory, atomicity and normal-return witnesses.""" +import copy + +import numpy as np +import pytest + +import autofit as af + +nautilus = pytest.importorskip("nautilus") +from autofit.non_linear.search.nest.nautilus import checkpoint_policy as policy +from autofit.non_linear.search.nest.nautilus.search import _large_attribute_hdf5 + + +def prior(x): + return x + + +def likelihood(x): + return -np.sum((x - .5)**2) / .02 + + +def blob_likelihood(x): + return likelihood(x), x[0], x[1] + + +def sampler(path, *, batch=True, blobs=False): + cls = policy.BatchedCheckpointSampler if batch else nautilus.Sampler + return cls(prior, blob_likelihood if blobs else likelihood, n_dim=2, + n_live=30, n_points_min=10, n_batch=20, n_networks=4, + neural_network_kwargs={"hidden_layer_sizes": (8,), "max_iter": 50}, + seed=3, filepath=path) + + +def assert_native_equal(a, b): + assert a.n_like == b.n_like + assert a.rng.bit_generator.state == b.rng.bit_generator.state + assert a.explored == b.explored + assert a.discard_exploration == b.discard_exploration + for key in ("shell_n", "shell_end_exp", "shell_n_sample_exp", "points_t", "log_l_t", "shell_t", "blobs_t"): + np.testing.assert_array_equal(getattr(a, key), getattr(b, key)) + for key in ("points", "log_l", "blobs"): + left, right = getattr(a, key), getattr(b, key) + if left is None: + assert right is None + else: + for x, y in zip(left, right): + np.testing.assert_array_equal(x, y) + for x, y in zip(a.posterior(), b.posterior()): + np.testing.assert_array_equal(x, y) + + +@pytest.mark.parametrize("bad", [0, -1, True, 1.5, "5", None]) +def test_invalid_intervals(bad): + with pytest.raises(ValueError, match="positive integer"): + af.Nautilus(checkpoint_interval_batches=bad) + with pytest.raises(ValueError, match="positive integer"): + policy.validate_interval(bad) + + +def test_default_native_path_and_disabled_checkpoints(): + assert af.Nautilus().sampler_cls is nautilus.Sampler + assert af.Nautilus(checkpoint=False).sampler_cls is nautilus.Sampler + assert af.Nautilus(checkpoint=False).checkpoint_file is None + with pytest.raises(ValueError, match="requires checkpoint=True"): + af.Nautilus(checkpoint=False, checkpoint_interval_batches=5) + assert "checkpoint_interval_batches" not in af.Nautilus.__identifier_fields__ + assert af.Nautilus(checkpoint_interval_batches=5).sampler_cls.func is policy.BatchedCheckpointSampler + + +def test_version_guard(monkeypatch, tmp_path): + monkeypatch.setattr(policy, "version", lambda name: "1.0.6") + with pytest.raises(ValueError, match="1.0.5"): + sampler(tmp_path / "checkpoint.hdf5") + + +@pytest.mark.parametrize("blobs", [False, True]) +def test_exact_trajectory_all_shells_caches_and_native_reopen(tmp_path, blobs): + with _large_attribute_hdf5(): + original = sampler(tmp_path / "original.hdf5", batch=False, blobs=blobs) + candidate = sampler(tmp_path / "candidate.hdf5", blobs=blobs) + boundaries = [] + for obj in (original, candidate): + events = [] + write = obj.write + def observed(*args, **kwargs): + events.append((obj.n_like, obj.explored, len(obj.bounds))) + return write(*args, **kwargs) + obj.write = observed + assert obj.run(n_eff=50, n_shell=2, f_live=.2, n_like_max=3000, + timeout=15, discard_exploration=True, verbose=False) + obj.write = write + boundaries.append(set(events)) + assert boundaries[0] <= boundaries[1] + assert len(candidate.bounds) > 1 + assert_native_equal(original, candidate) + reopened = sampler(tmp_path / "candidate.hdf5", batch=False, blobs=blobs) + assert_native_equal(candidate, reopened) + assert candidate._pending_checkpoint_batches == 0 + state = copy.deepcopy(candidate.rng.bit_generator.state) + candidate.write(tmp_path / "extra.hdf5") + assert candidate.rng.bit_generator.state == state + assert_native_equal(candidate, sampler(tmp_path / "extra.hdf5", batch=False, blobs=blobs)) + + +@pytest.mark.parametrize("limit", [60, 140]) +def test_normal_budget_return_flushes_pending_batches(tmp_path, limit): + with _large_attribute_hdf5(): + obj = sampler(tmp_path / "checkpoint.hdf5") + assert not obj.run(n_like_max=limit, verbose=False) + restored = sampler(tmp_path / "checkpoint.hdf5", batch=False) + assert_native_equal(obj, restored) + assert obj._pending_checkpoint_batches == 0 + + +def test_timeout_normal_return_flushes_without_new_sampling(tmp_path, monkeypatch): + obj = sampler(tmp_path / "checkpoint.hdf5") + obj.run(n_like_max=40, verbose=False) + obj.add_samples(-1) + obj.write_shell_update(obj.filepath, -1) + assert obj._pending_checkpoint_batches == 1 + obj.run(timeout=0, verbose=False) + assert_native_equal(obj, sampler(obj.filepath, batch=False)) + + +@pytest.mark.parametrize("failure", ["native_write", "replace"]) +def test_failed_publication_keeps_previous_file_and_cleans_temp(tmp_path, monkeypatch, failure): + path = tmp_path / "checkpoint.hdf5" + obj = sampler(path) + obj.run(n_like_max=40, verbose=False) + before, rng = path.read_bytes(), copy.deepcopy(obj.rng.bit_generator.state) + obj._pending_checkpoint_batches = 2 + def fail(*args, **kwargs): + raise OSError("injected failure") + if failure == "native_write": + def partial(self, filepath, **kwargs): + filepath.write_bytes(b"incomplete") + raise OSError("injected failure") + monkeypatch.setattr(nautilus.Sampler, "write", partial) + else: + monkeypatch.setattr(policy.os, "replace", fail) + with pytest.raises(OSError, match="injected failure"): + obj.flush_checkpoint() + assert path.read_bytes() == before + assert obj.rng.bit_generator.state == rng + assert obj._pending_checkpoint_batches == 2 + assert not list(tmp_path.glob(".*.hdf5")) + assert sampler(path, batch=False).n_like == 40 + + +def test_exception_does_not_flush_half_mutated_state(tmp_path, monkeypatch): + path = tmp_path / "checkpoint.hdf5" + obj = sampler(path) + obj.run(n_like_max=40, verbose=False) + before = path.read_bytes() + def broken_run(self, **kwargs): + self._pending_checkpoint_batches = 2 + self.n_like += 99 + raise RuntimeError("broken batch") + monkeypatch.setattr(nautilus.Sampler, "run", broken_run) + with pytest.raises(RuntimeError, match="broken batch"): + obj.run() + assert path.read_bytes() == before + assert sampler(path, batch=False).n_like == 40 + + +def test_interval_publishes_all_dirty_shells_together(tmp_path): + path = tmp_path / "checkpoint.hdf5" + with _large_attribute_hdf5(): + obj = sampler(path, blobs=True) + assert obj.run(n_eff=50, n_shell=2, f_live=.2, n_like_max=3000, + timeout=15, discard_exploration=True, verbose=False) + assert len(obj.bounds) > 1 + before = path.read_bytes() + for index in range(5): + shell = index % len(obj.bounds) + obj.add_samples(shell) + obj.write_shell_update(path, shell) + if index < 4: + assert path.read_bytes() == before + assert obj._pending_checkpoint_batches == 0 + assert_native_equal(obj, sampler(path, batch=False, blobs=True)) + + +def test_export_does_not_clear_unpublished_primary_updates(tmp_path): + path = tmp_path / "checkpoint.hdf5" + obj = sampler(path) + obj.run(n_like_max=40, verbose=False) + obj.add_samples(-1) + obj.write_shell_update(path, -1) + obj.write(tmp_path / "export.hdf5") + assert obj._pending_checkpoint_batches == 1 + assert sampler(path, batch=False).n_like == 40 + obj.flush_checkpoint() + assert sampler(path, batch=False).n_like == obj.n_like