"""RULE.md 1: the port's numbers on the reference's own frames (teacher forcing).""" import json, sys, time from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) import numpy as np import port as P name, threads = sys.argv[1], 4 p = P.Port(decoder=name, threads=threads); ids = json.load(open(P.ROOT / "data" / "ids.json")); REF = P.ROOT / "runs" / "ref" tot = heads = match = 0; absd = 0.0; maxd = 0.0; stops = stop_ok = 0; kl = []; t0 = time.perf_counter() for i, seq in enumerate(ids): r = np.load(REF / f"{i:02d}.npz"); n = min(len(r["codes"]), 600) # runaways: the first 600 frames h, k, v = p.prefill(seq) for t in range(n): lg, st = p.out(h); ref = r["logits"][t]; at = 0 absd += float(np.abs(lg - ref).mean()); maxd = max(maxd, float(np.abs(lg - ref).max())); tot += 1 for m in P.LEVELS: match += int(lg[at:at + m].argmax() == ref[at:at + m].argmax()); heads += 1 a = ref[at:at + m].astype(np.float64); b = lg[at:at + m].astype(np.float64); pa = np.exp(a - a.max()); pa /= pa.sum(); pb = np.exp(b - b.max()); pb /= pb.sum() kl.append(float((pa * np.log((pa + 1e-12) / (pb + 1e-12))).sum())); at += m stops += 1; stop_ok += int((st > 0) == (r["stop"][t] > 0)) h, k, v = p.step(r["codes"][t], k, v) print(f"{name}: frames {tot}, heads x frames {heads}, same most likely value {100 * match / heads:.3f}%, mean abs logit diff {absd / tot:.4f}, max {maxd:.2f}, mean KL {np.mean(kl):.5f} nats, stop decision same {100 * stop_ok / stops:.2f}%, {time.perf_counter() - t0:.0f} s") json.dump(dict(name=name, frames=tot, heads=heads, same=match / heads, mad=absd / tot, maxd=maxd, kl=float(np.mean(kl)), stop=stop_ok / stops), open(P.ROOT / "runs" / f"teacher-{name}.json", "w"))