File size: 1,387 Bytes
a4054b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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()