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
31 changes: 31 additions & 0 deletions experiments/nautilus_30d_matched_ess/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Independent 30D evidence at matched ESS

At the registered ESS endpoint, seeds 1 and 2 gave evidence below the analytic value. Seed 3 gave evidence above it. Signed log-evidence errors ran from -0.018021 to +0.005755 against analytic log evidence -36.767688153. The continuations reached their endpoints in 49.5 minutes of shared wall time. [Phase protocol](https://github.com/PyAutoLabs/autofit_profiling/issues/34).

| Independent seed | Prior checkpoint error in evidence | Final log evidence | Final logZ error | Final error in evidence | Final fresh ESS | Additional calls |
|---:|---:|---:|---:|---:|---:|---:|
| 1 | +0.42% | -36.770081 | -0.002393 | -0.24% | 10,000.1 | 174,400 |
| 2 | -1.76% | -36.785710 | -0.018021 | -1.79% | 10,003.0 | 7,500 |
| 3 | -0.35% | -36.761933 | +0.005755 | +0.58% | 10,001.0 | 624,100 |

Two estimates changed sign between the original time cap and the ESS endpoint. Seed 1 moved from above to below the analytic value, and seed 3 from below to above. The mean signed log-evidence error was -0.004887, with sample standard deviation 0.012083 across three independent runs. These describe this fixture and these seeds. Relative evidence error is exp(estimated logZ − analytic logZ) − 1.

## Comparison with exploration and the shared-bound experiment

At the end of exploration the same three seeds had log-evidence errors of −0.8924, −0.9557 and −0.8886. Fresh-only sampling reduced the absolute discrepancy for every seed. The earlier 40-minute attempt ended at ESS 8,147, 9,904 and 6,920 and was kept as a separate partial result. Its files were read only. [Previous independent exploration attempt](../nautilus_30d_independent/README.md).

The historical seed-0 study gave errors of −0.0290, −0.0481 and −0.0278 across three continuations sharing a single exploration. Here each reported endpoint uses separately learned bounds and its own continuing random stream. One continuation per exploration mixes variation from learning bounds with variation in fresh sampling. The sample of three is too small to calibrate an evidence uncertainty or establish a general bias. [Conditional seed-0 comparison](../nautilus_30d_evidence_check/README.md).

## Verification

The source hashes and copied starting estimates matched PR #33. Exploration exclusion remained enabled, no new bounds were built, the per-shell exploration cutoffs remained fixed and all three original hashes matched after the run. Each native HDF5 checkpoint reopened with exact agreement on ordered posterior, evidence, ESS and moments. A separate reader verified the analytic integral by eigendecomposition, rebuilt log evidence and ESS from 3,906,700 fresh likelihood rows and stored shell volumes, and checked one fresh likelihood in each shell. [Independent verification](verification.json) · [Per-seed receipts, hashes and runtime pins](result.json).

## Reproduce

Use the PyAutoFit and PyAutoNerves checkouts and numerical package versions named in the receipt. Place both checkouts on PYTHONPATH. Pass the raw directory from PR #33 containing seed1, seed2 and seed3, along with a fresh output path. The program verifies each input checkpoint hash before advancing a copy. The exact continuation requires the retained source HDF5 files.

```sh
python experiments/nautilus_30d_matched_ess/continue_runs.py RAW_FROM_PR_33 FRESH_OUTPUT_DIRECTORY
```

The three spawned local CPU processes had one numerical thread each, native update cadence 400, four neural networks and a 4 GiB sampled RSS cap per process. The supervisor shared a single 3,600-second deadline and reserved 30 seconds for shutdown. Each seed stopped at its first native ESS ≥10,000 with at least 100 fresh samples per shell. The original timeout attempts retain their own receipts. The wider phase 0b dimension and workload campaign remains open. [Canonical profiling task](https://github.com/PyAutoLabs/PyAutoPulse/blob/main/tasks/autofit_profiling_bootstrap.md).
214 changes: 214 additions & 0 deletions experiments/nautilus_30d_matched_ess/continue_runs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
"""Continue the three PR #33 terminal checkpoints within one local hour."""

import argparse
import json
import os
import shutil
import subprocess
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch

ROOT = Path(__file__).resolve().parents[2]
PREDECESSOR = ROOT / "experiments/nautilus_30d_independent"
sys.path.insert(0, str(PREDECESSOR))
from profiling.nautilus_diagnostics import estimate, fresh_counts, sha256
from profiling.nautilus_manifest import RunConfig
from profiling.nautilus_resources import atomic_json, supervise
from read_saved import inspect_checkpoint
from run import MEMORY_BYTES, THREADS, geometry, load

BUDGET_S = 3600
GRACE_S = 30


def continue_seed(source, output, row, truth, deadline):
import numpy as np
from autofit.non_linear.search.nest.nautilus.search import _large_attribute_hdf5
from nautilus import Sampler
from profiling.nautilus_target import CorrelatedGaussianTarget

name = row["stream"]
source_file = source / name / "checkpoint.hdf5"
path = output / "checkpoint.hdf5"
if path.exists():
raise FileExistsError(path)
shutil.copyfile(source_file, path)
if sha256(path) != row["checkpoint_sha256"]:
raise ValueError(f"Wrong source checkpoint for {name}")
exploration = output / "exploration.json"
atomic_json(exploration, row["exploration"])
before = inspect_checkpoint(path, exploration, truth)
if before != row["estimate"]:
raise ValueError("Copied checkpoint differs from archived audit")
seed = int(name.removeprefix("seed"))
target = CorrelatedGaussianTarget(30)
if abs(truth - target.log_evidence) > 1e-12:
raise ValueError("Analytic evidence changed")
config = RunConfig(dimension=30, n_live=400, seed=seed, trace=False).constructor()

def reopen():
return Sampler(target.prior, target, filepath=str(path), **config)

with _large_attribute_hdf5():
sampler = reopen()
cuts = sampler.shell_end_exp.copy()
if (
not sampler.discard_exploration
or not sampler.explored
or sampler.n_like != before["total_calls"]
):
raise ValueError("Resume state mismatch")
update = sampler.write_shell_update
last = 0.0

def persist(*args, **kwargs):
nonlocal last
update(*args, **kwargs)
now = time.monotonic()
if now - last >= 30:
atomic_json(
output / "progress.json",
{
"elapsed_from_launch_s": now - (deadline - BUDGET_S),
"n_like": int(sampler.n_like),
"ess": float(sampler.n_eff),
},
)
last = now

remaining = max(0.0, deadline - time.monotonic() - GRACE_S)
with (
patch.object(sampler, "add_bound", side_effect=ValueError("Unexpected bound update")),
patch.object(sampler, "write_shell_update", persist),
):
done = sampler.run(
n_eff=10000, n_shell=100, discard_exploration=True, verbose=False, timeout=remaining
)
final = estimate(sampler, target)
completed = bool(done and sampler.n_eff >= 10000 and min(fresh_counts(sampler)) >= 100)
native = reopen()
if (
not native.discard_exploration
or not np.array_equal(cuts, native.shell_end_exp)
or estimate(native, target) != final
or geometry(path) != row["exploration"]["geometry"]
or final["selected_samples"] != native.n_like - row["exploration"]["estimate"]["n_like"]
):
raise ValueError("Saved result failed native reopening or fixed-bound verification")
atomic_json(
output / "outcome.json",
{
"seed": seed,
"completed": completed,
"initial": before,
"final": final,
"target_logZ": truth,
"additional_calls": final["n_like"] - before["total_calls"],
"geometry": row["exploration"]["geometry"],
"native_reopen_exact": True,
},
)


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("source", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--child", choices=("seed1", "seed2", "seed3"))
parser.add_argument("--deadline", type=float)
args = parser.parse_args()
source, output = args.source.resolve(), args.output.resolve()
prior = load(PREDECESSOR / "checkpoints.json")
rows = prior["seeds"]
if args.child:
row = next(r for r in rows if r["stream"] == args.child)
continue_seed(source, output, row, prior["analytic_logZ"], args.deadline)
return
from profiling.nautilus_baseline import provenance
from profiling.nautilus_campaign import environment_key
from verify import verify

start = time.monotonic()
deadline = start + BUDGET_S
expected = load(PREDECESSOR / "result.json")["environment"]
actual = environment_key(provenance())
for key in ("packages", "nautilus_source_sha256", "python"):
if actual[key] != expected[key]:
raise ValueError(f"Runtime mismatch: {key}")
for repo in ("PyAutoFit", "PyAutoNerves"):
if actual["sources"][repo] != expected["sources"][repo]:
raise ValueError(f"Library source mismatch: {repo}")
output.mkdir(parents=True, exist_ok=False)

def launch(row):
name = row["stream"]
folder = output / name
folder.mkdir()
cmd = [
sys.executable,
__file__,
str(source),
str(folder),
"--child",
name,
"--deadline",
str(deadline),
]
with (folder / "run.log").open("w") as log:
proc = subprocess.Popen(
cmd,
stdout=log,
stderr=subprocess.STDOUT,
start_new_session=True,
env={**os.environ, **dict.fromkeys(THREADS, "1")},
)
supervision = supervise(proc, deadline - time.monotonic(), MEMORY_BYTES, interval=0.25)
elapsed = time.monotonic() - start
atomic_json(folder / "supervision.json", supervision)
result = folder / "outcome.json"
return {
"stream": name,
"elapsed_from_shared_start_s": elapsed,
"supervision": supervision,
"result": load(result) if result.exists() else None,
}

with ThreadPoolExecutor(max_workers=3) as executor:
runs = list(executor.map(launch, rows))
elapsed_s = time.monotonic() - start
unchanged = all(
sha256(source / r["stream"] / "checkpoint.hdf5") == r["checkpoint_sha256"] for r in rows
)
success = unchanged and all(
r["supervision"]["returncode"] == 0 and r["result"] and r["result"]["completed"]
for r in runs
)
atomic_json(
output / "result.json",
{
"status": "complete" if success else "incomplete",
"runs": runs,
"source_hashes": {r["stream"]: r["checkpoint_sha256"] for r in rows},
"inputs_unchanged": unchanged,
"environment": actual,
"shared_budget_s": BUDGET_S,
"supervised_elapsed_s": elapsed_s,
"grace_s": GRACE_S,
"memory_limit_bytes_per_child": MEMORY_BYTES,
},
)
if success:
atomic_json(
output / "verification.json", verify(output, streams=[r["stream"] for r in runs])
)
else:
raise SystemExit(
"One or more streams incomplete or unverified; inspect their saved checkpoints."
)


if __name__ == "__main__":
main()
Loading
Loading