BurnyCoder's picture
Publish independent scientific audit and corrected study reports
f317f35 verified
Raw History Blame Contribute Delete
11.8 kB
"""Registered comparisons for third-derivative training on a fixed test corpus.
Seeds are independent experimental units. Positive deltas mean worse prediction for the
third-order method. The module consumes ordinary JSON-compatible result records so the
CLI, static report, and interactive explorer can share the same inference.
"""
import hashlib
import json
import re
from math import comb, isfinite
from statistics import median
from .records import HORIZONS, RATIOS, run_name
_METHODS = ("ce", "trace", "third")
_SEEDS = range(1, 9)
def _number(value):
"""Reject missing, boolean and nonfinite values before constructing measures or plots."""
return isinstance(value, (int, float)) and not isinstance(value, bool) and isfinite(value)
def _comparison(records, baseline):
"""Apply the registered paired sign test and symmetric distribution-free median interval."""
pairs = []
for seed in _SEEDS:
third = records[seed, "third"]["test_nll"]
other = records[seed, baseline]["test_nll"]
pairs.append({"seed": seed, "third_nll": third, "baseline_nll": other, "delta": third - other})
return paired_sign(pairs)
def paired_sign(pairs, *, larger=False):
"""Exact conditional paired sign test and symmetric order-statistic median interval."""
ordered = sorted(pair["delta"] for pair in pairs)
wins = sum(d > 0 if larger else d < 0 for d in ordered)
losses = sum(d < 0 if larger else d > 0 for d in ordered)
discordant = wins + losses
# Exact upper binomial tail under fair signs conditional on discordant pairs.
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binomtest.html
p_raw = sum(comb(discordant, k) for k in range(wins, discordant + 1)) / 2**discordant
# If m is a continuous population median, #{observations < m} has Bin(n, 1/2) law.
# Choose the narrowest symmetric order-statistic interval meeting the registered level.
# Quantile interval, eq. (30) with p=1/2: https://nvlpubs.nist.gov/nistpubs/TechnicalNotes/NIST.TN.2119.pdf#page=41
n = len(ordered)
rank = 0
discarded = 0
coverage = 1.0
for k in range(n // 2):
discarded += comb(n, k)
candidate = 1 - 2 * discarded / 2**n
if candidate < 0.95:
break
rank, coverage = k + 1, candidate
# The registered eight pairs admit a finite interval; expose unbounded ends for small n.
interval = [ordered[rank - 1], ordered[-rank]] if rank else [None, None]
return {
"pairs": pairs, "median_delta": median(ordered), "median_ci": interval,
"ci_coverage": coverage, "wins": wins, "ties": n - discordant, "losses": losses,
"p_raw": p_raw,
}
def holm(comparisons):
"""Adjust a prespecified family using Holm's monotone step-down procedure."""
maximum = 0.
for index,row in enumerate(sorted(comparisons.values(),key=lambda item:item["p_raw"])):
maximum = max(maximum,row["p_raw"]*(len(comparisons)-index))
row["p_holm"] = min(1.,maximum)
def _time_match(seed, records, clock="train_seconds"):
"""Look backward from the shortest complete training run to actual validation evaluations."""
runs = {method: records[seed, method] for method in _METHODS}
if any(not _number(run.get(clock)) or run[clock] <= 0 for run in runs.values()):
return None
cutoff = min(run[clock] for run in runs.values())
# Production logs evaluate fixed windows at each diagnostic, including the final step.
# Older homogeneous result files have a single dev_nll field for the same population.
fixed_windows = any("matched_dev_nll" in run for run in runs.values())
summary_field = "matched_dev_nll" if fixed_windows else "dev_nll"
matched = {}
for method, run in runs.items():
events = list(run.get("events") or [])
final = run.get("status") not in {"configured", "in progress", "training", "failed", "initializing", "scoring development"}
if final and _number(run.get(summary_field)):
events.append({
"tokens": run["tokens"], "train_seconds": run["train_seconds"], clock:run[clock],
"dev_nll": run[summary_field],
})
eligible = [
event for event in events
if _number(event.get(clock)) and 0 <= event[clock] <= cutoff
and _number(event.get("dev_nll")) and event["dev_nll"] >= 0
and _number(event.get("tokens")) and event["tokens"] >= 0
]
if not eligible:
return None
# max() takes the first equal timestamp, giving a real event priority over its summary.
event = max(eligible, key=lambda x: x[clock])
matched[method] = {key: event[key] for key in ("tokens", "train_seconds", "dev_nll")}
matched[method]["clock_seconds"] = event[clock]
matched[method]["staleness_seconds"] = cutoff - event[clock]
return {"seed": seed, "cutoff_seconds": cutoff, "by_method": matched}
def freeze_digest(freeze):
"""Recreate the original freeze identifier independent of file and platform line endings."""
payload = {key: value for key, value in freeze.items() if key != "freeze_id"}
return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest()
def _inconsistency(records, stage, freeze, dataset):
"""Bind all published scores to their preregistered checkpoint, training corpus and test denominator."""
if not freeze or freeze.get("stage") != stage or freeze.get("freeze_id") != freeze_digest(freeze):
return "Missing or inconsistent stage freeze."
if not dataset or dataset.get("stage") != stage:
return "Missing stage dataset manifest."
split = dataset.get("splits", {}).get("test", {})
if (not re.fullmatch(r"[a-f0-9]{64}", str(split.get("sha256"))) or
split["sha256"] != freeze.get("test_sha256") or type(split.get("tokens")) is not int or split["tokens"] < 2):
return "Frozen test split differs from the corpus manifest."
ratios = freeze.get("ratios", {})
if set(ratios) != {"third", "trace"} or any(value not in RATIOS for value in ratios.values()):
return "Invalid frozen ratios."
weights, initial = freeze.get("runs", {}), freeze.get("initializations", {})
if len(weights) != len(records) or set(initial) != {str(s) for s in _SEEDS}:
return "Incomplete checkpoint or initialization manifest."
# data.prepare publishes the sorted, indented UTF-8 manifest with one LF.
corpus = hashlib.sha256((json.dumps(dataset, indent=2, sort_keys=True) + "\n").encode()).hexdigest()
for (seed, method), row in records.items():
ratio = ratios.get(method, 0)
name = run_name(stage, method, "confirm", seed, ratio)
if not all(re.fullmatch(r"[a-f0-9]{64}", str(value)) for value in (weights.get(name), initial[str(seed)])):
return f"Missing checkpoint or initialization digest for {name}."
expected = {"id": name, "ratio": ratio, "weights_sha256": weights[name], "status": "complete",
"token_budget": HORIZONS[stage], "initial_trainable_sha256": initial[str(seed)],
"data_manifest_sha256": corpus, "test_tokens": split["tokens"] - 1,
"test_manifest_sha256": split["sha256"], "freeze_id": freeze["freeze_id"]}
for key, value in expected.items():
if row.get(key) != value:
return f"Unverified {key} for {name}."
return None
def analyze_stage(summaries: list[dict], stage: str, *, freeze=None, dataset=None) -> dict:
"""Analyze the fixed three-arm matrix after every registered checkpoint has a final score.
Partial matrices expose progress and eligible elapsed-time development comparisons.
Duplicate rows or incompatible provenance invalidate inference for that stage.
"""
horizon = HORIZONS[stage]
result = {
"stage": stage, "status": "pending", "complete": False, "success": None,
"n_expected_seeds": len(_SEEDS), "n_complete_seeds": 0, "missing": [],
"comparisons": {}, "matched_time": [], "matched_nonhost_time": [], "reason": "Awaiting confirmatory runs.",
}
records = {}
for row in summaries:
seed, method = row.get("seed"), row.get("method")
if (row.get("stage") != stage or row.get("phase") != "confirm" or method not in _METHODS
or type(seed) is not int or seed not in _SEEDS):
continue
key = (seed, method)
if key in records:
result.update(status="invalid", reason=f"Duplicate final-run records for seed {seed}, {method}.")
return result
if row.get("target_tokens") is not None and row["target_tokens"] != horizon:
result.update(status="invalid", reason=f"The configured {method} budget differs from registration.")
return result
records[key] = row
if not records:
return result
for seed in _SEEDS:
missing_methods = [
method for method in _METHODS if records.get((seed, method), {}).get("tokens") != horizon
]
if missing_methods:
result["missing"].extend(f"Seed {seed} {method} full horizon" for method in missing_methods)
else:
result["n_complete_seeds"] += 1
timing = _time_match(seed, records)
if timing is not None:
result["matched_time"].append(timing)
if stage=="abstracts":
gpu_interval = _time_match(seed,records,"training_seconds_excluding_host_probes")
if gpu_interval is not None:
result["matched_nonhost_time"].append(gpu_interval)
if result["missing"]:
result.update(status="incomplete", reason=f"All checkpoints must reach {horizon:,} target tokens.")
return result
# Inspect the test scores only after establishing complete training coverage.
for (seed, method), row in records.items():
score = row.get("test_nll")
if score is None:
result["missing"].append(f"Seed {seed} {method} final test score")
elif not _number(score) or score < 0:
result.update(status="invalid", reason=f"Invalid test NLL for seed {seed}, {method}.")
return result
if result["missing"]:
result.update(status="incomplete", reason="Awaiting the complete frozen set of final test scores.")
return result
issue = _inconsistency(records, stage, freeze, dataset)
if issue:
result.update(status="invalid", reason=issue)
return result
comparisons = {baseline: _comparison(records, baseline) for baseline in ("ce", "trace")}
# Holm step-down: adjusted p-values are monotone in raw-p rank; ties receive equal values.
# https://stat.ethz.ch/R-manual/R-devel/library/stats/html/p.adjust.html
holm(comparisons)
for stats in comparisons.values():
if stats["p_holm"] <= 0.05:
stats["interpretation"] = "improved"
elif stats["median_ci"][0] > 0:
stats["interpretation"] = "adverse"
else:
stats["interpretation"] = "inconclusive"
success = all(stats["p_holm"] <= 0.05 for stats in comparisons.values())
if success:
status, reason = "confirmed", "Both registered superiority comparisons passed."
elif any(stats["interpretation"] == "adverse" for stats in comparisons.values()):
status, reason = "adverse", "A median-difference interval favors a baseline."
else:
status, reason = "inconclusive", "The registered two-comparison superiority criterion was unmet."
result.update(status=status, complete=True, success=success, comparisons=comparisons, reason=reason)
return result