lorapop / code /tools /check_ckpt_equiv.py
bryan7264's picture
Add files using upload-large-folder tool
a4054b7 verified
Raw History Blame Contribute Delete
1.39 kB
# MIT License, Bryan Cheng, 2026.
"""Equivalence check for D-041: two forecast_competitors.py outputs (with and without activation checkpointing, same
seed, sizes, K and D, same GPU) must agree. Exit status 0 if they do.
python tools/check_ckpt_equiv.py on.json off.json [--tol 1e-3]
"""
import argparse
import json
import sys
import numpy as np
def main():
ap = argparse.ArgumentParser()
ap.add_argument("on")
ap.add_argument("off")
ap.add_argument("--tol", type=float, default=1e-3)
a = ap.parse_args()
A, B = json.load(open(a.on)), json.load(open(a.off))
ok = A["traits"] == B["traits"] and A["n_items"] == B["n_items"]
rep = {"traits_equal": A["traits"] == B["traits"], "n_items_equal": A["n_items"] == B["n_items"]}
if rep["traits_equal"]:
for k in ("R_grad", "R_rep"):
rep[f"{k}_max_abs_diff"] = float(np.abs(np.array(A[k]) - np.array(B[k])).max())
ok &= rep[f"{k}_max_abs_diff"] <= a.tol
g1, g2 = np.array(A["grad_norm"]), np.array(B["grad_norm"])
rep["grad_norm_max_rel_diff"] = float((np.abs(g1 - g2) / np.maximum(np.abs(g2), 1e-30)).max())
ok &= rep["grad_norm_max_rel_diff"] <= a.tol
rep["seconds_on_off"] = [A.get("seconds"), B.get("seconds")]
rep["equivalent"] = bool(ok)
print(json.dumps(rep))
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()