"""Job #2053 triage of route 88: is another bounded experiment warranted, and what does it cost? FALSIFIER FIRST (fixed before any number below was computed, and reported as-is if it fires): F1 Anchor. For any step (P# -> P'#) with Q = the primes entering (P, P'], the one-slot cell is N_1 = D_P * (NCOPY - prod_{q in Q}(q - 2)), with NCOPY = prod_{q in Q} q and D_P the number of level-P slots per period of P#. Recomputed here from the prime list alone, at the three frozen steps (must reproduce the committed 105221160 / 3691273410 / 2524470300: a CONTROL on this script) and then, NEW, at the two priced chain cells 19#->43# and 23#->43#, where the value is not in the record and serves as a cheap pre-registered falsifier for those runs. F2 Slot count. D_P must equal prod_{p <= P}(p - 2) -- the admissible twin-slot count. Checked against the corpus's own six values (3, 15, 135, 1485, 22275, 378675) and used to derive D_23, the value the pinned engine needs and which no frozen step exercises. F3 Census identity. hist[l] = N_l - 2*N_{l+1} + N_{l+2} is the pre-registration's own conversion from the N_k curve to the census of maximal killed runs of length exactly l. In particular hist[K*] = N_{K*}, since N_{K*+1} = N_{K*+2} = 0. Checked on all three committed curves; disagreement is reported, not smoothed. F4 Cost model. The engine's work is D * |Q| * kmax * 2^kmax elementary mask updates (one shape table per slot start, |Q| or-transforms per bit). If that model is right, the three frozen steps' wall times must lie on one line through the origin; leave-one-out prediction error is reported. If the model is wrong, the price band below is wrong and is so labelled. SCOPE. Nothing here re-decides the pinned convention (job #2048 did, and this script only USES the pinned engine), adds or removes a certificate, or re-implements the engine. It prices the route's own next step and pre-registers the anchors that step must hit. """ import argparse import importlib.util import json import os import sys import time HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) # The pinned engine, by the sibling run that pinned it (job #2048). Imported, not rewritten: # its sha256 is recorded in the report so the numbers below name the exact file they time. # RUN is <...>/runs/, so the sibling run sits next to it under <...>/runs/. ENGINE = os.path.join(os.path.dirname(RUN), "bf3-d485361a5ead560d", "evidence", "job2048", "kstar-engine-check.py") PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73] D_EXPECT = {5: 3, 7: 15, 11: 135, 13: 1485, 17: 22275, 19: 378675, 23: 7952175} FROZEN = [(13, 29, 10), (13, 31, 17), (17, 31, 13)] PRICED = [(19, 43), (23, 43)] COMMITTED = { (13, 29): [105221160, 29114520, 6942634, 1470674, 285422, 52048, 9456, 1712, 270, 36, 0], (13, 31): [3691273410, 1208132640, 357506328, 98048374, 25570726, 6426266, 1601992, 403958, 103170, 25738, 5568, 1180, 296, 80, 18, 6, 2, 0], (17, 31): [2524470300, 607806150, 128515868, 25793636, 5037414, 929548, 167144, 29148, 4894, 710, 138, 22, 6, 0], } # The route's own pricing of the two chain cells, quoted in route 88's text ("section 3 of the # run document"): 19#->43# at ~2.5e10 and 23#->43# at ~1-2e10. Read here as a RANGE of kmax, # because that is what a price in `D * 2^kmax` units means once D is known. ROUTE_PRICE_UNITS = {(19, 43): (2.5e10, 2.5e10), (23, 43): (1e10, 2e10)} BUDGETS_S = {"lane_2h": 2 * 3600, "lane_3h": 3 * 3600, "assignment_cap_4h": 4 * 3600} def d_of(P): """D_P = prod_{5 <= p <= P}(p - 2), the admissible twin-slot count per period of P#. The product runs from p = 5, not from p = 2: the level-5 word is the 3 residues {11,17,29} mod 30, so D_5 = 3 = (5 - 2) and the factors p - 2 for p = 2, 3 are not in the word's construction. Checked against the corpus's own values below (3, 15, 135, 1485, ...); the first version of this function started at p = 2 and returned 0, which is exactly what the slot-count control exists to catch. """ v = 1 for p in PRIMES: if p < 5: continue if p > P: break v *= (p - 2) return v def Q_of(P, Pp): return [q for q in PRIMES if P < q <= Pp] def f1(P, Pp): Q = Q_of(P, Pp) ncopy = 1 prod2 = 1 for q in Q: ncopy *= q prod2 *= (q - 2) D = d_of(P) return D, Q, ncopy, prod2, D * (ncopy - prod2) def load_engine(): if not os.path.exists(ENGINE): return None, None spec = importlib.util.spec_from_file_location("pinned_engine", ENGINE) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod, ENGINE def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", required=True) ap.add_argument("--skip-timing", action="store_true", help="arithmetic only (no engine runs): the anchors and the price model " "are computed without measuring the frozen steps again") ap.add_argument("--time-steps", default="0,1,2", dest="time_steps", help="which frozen steps to time, by index (0 = 13#->29#, 1 = 13#->31# " "the 260 s one, 2 = 17#->31#); the steps not timed fall back to the " "recorded times, labelled, and are then predicted leave-one-out") a = ap.parse_args() checks = [] def chk(name, ok, detail): checks.append({"name": name, "ok": bool(ok), "detail": detail}) print("%-58s %s %s" % (name, "PASS" if ok else "FAIL", detail)) # ---------------------------------------------------------------- F2: slot counts print("== F2 slot counts D_P = prod_{p<=P}(p-2) ==") for P, exp in sorted(D_EXPECT.items()): got = d_of(P) chk("F2.slotcount P=%d" % P, got == exp, "derived %d vs corpus %d" % (got, exp)) # the corpus's own list stops at 23; the two steps it does NOT carry are derived the same way d_more = {P: d_of(P) for P in (29, 31, 37, 41, 43)} print(" derived beyond the corpus list:", json.dumps(d_more)) # ---------------------------------------------------------------- F1: anchors print("== F1 one-slot anchor, frozen steps (CONTROL) ==") anchors = {} for (P, Pp, kstar) in FROZEN: D, Q, ncopy, prod2, n1 = f1(P, Pp) committed = COMMITTED[(P, Pp)][0] chk("F1.control %d#->%d#" % (P, Pp), n1 == committed, "N_1 = %d, committed %d (D=%d Q=%s NCOPY=%d)" % (n1, committed, D, Q, ncopy)) anchors["%d#->%d#" % (P, Pp)] = {"D": D, "Q": Q, "NCOPY": ncopy, "prod_q_minus_2": prod2, "N_1": n1, "committed": committed} print("== F1 anchors at the two PRICED cells (NEW: not in the record) ==") for (P, Pp) in PRICED: D, Q, ncopy, prod2, n1 = f1(P, Pp) windows = D * ncopy anchors["%d#->%d#" % (P, Pp)] = {"D": D, "Q": Q, "NCOPY": ncopy, "prod_q_minus_2": prod2, "N_1": n1, "windows_per_period": windows} print(" %d#->%d#: D=%d |Q|=%d NCOPY=%d prod(q-2)=%d N_1 = %d (windows/period %d)" % (P, Pp, D, len(Q), ncopy, prod2, n1, windows)) chk("F1.anchor %d#->%d# positive" % (P, Pp), n1 > 0, "N_1 = %d > 0" % n1) # ---------------------------------------------------------------- F3: census identity print("== F3 census identity hist[K*] = N_{K*} on the committed curves ==") for (P, Pp, kstar) in FROZEN: curve = COMMITTED[(P, Pp)] nk, nk1, nk2 = curve[kstar - 1], curve[kstar], (curve[kstar + 1] if kstar + 1 < len(curve) else 0) hist = nk - 2 * nk1 + nk2 chk("F3.hist[K*] %d#->%d#" % (P, Pp), hist == nk and hist >= 1, "hist[%d] = N_%d - 2N_%d + N_%d = %d (N_{K*+1}=%d) so the census of maximal %d-runs " "is %d" % (kstar, kstar, kstar + 1, kstar + 2, hist, nk1, kstar, hist)) # ---------------------------------------------------------------- F4: cost model print("== F4 cost model, fitted on the three frozen steps ==") engine, engine_path = (None, None) if a.skip_timing else load_engine() import hashlib esha = None if engine_path: h = hashlib.sha256() with open(engine_path, "rb") as fh: for c in iter(lambda: fh.read(65536), b""): h.update(c) esha = h.hexdigest() measured = [] want = set(int(x) for x in a.time_steps.split(",") if x.strip()) if engine is not None: for si, (P, Pp, kstar) in enumerate(FROZEN): if si not in want: D = d_of(P) ext = {(13, 29): 1.2, (13, 31): 260.5, (17, 31): 105.3}[(P, Pp)] measured.append({"step": "%d#->%d#" % (P, Pp), "D": D, "Q": len(Q_of(P, Pp)), "kmax": len(COMMITTED[(P, Pp)]), "seconds": ext, "units": D * len(Q_of(P, Pp)) * len(COMMITTED[(P, Pp)]) * (1 << len(COMMITTED[(P, Pp)])), "source": "recorded by job #2048's log, not timed in this run"}) continue slots, Psharp = engine.slot_word(P) D = len(slots) Q = Q_of(P, Pp) kmax = len(COMMITTED[(P, Pp)]) t0 = time.time() got = engine.nk_curve(slots, Psharp, Q, kmax, D) dt = time.time() - t0 ok = got == COMMITTED[(P, Pp)] chk("F4.rerun %d#->%d# matches committed" % (P, Pp), ok, "%.1f s, kmax=%d |Q|=%d D=%d, mismatches %s" % (dt, kmax, len(Q), D, [(k + 1, got[k], COMMITTED[(P, Pp)][k]) for k in range(kmax) if got[k] != COMMITTED[(P, Pp)][k]][:3])) measured.append({"step": "%d#->%d#" % (P, Pp), "D": D, "Q": len(Q), "kmax": kmax, "seconds": dt, "units": D * len(Q) * kmax * (1 << kmax), "source": "timed in this run, in-process"}) else: # No engine run in this mode: fall back to the times recorded by job #2048's own log, # labelled externally observed, so the model still has three points. ext = {(13, 29): 1.2, (13, 31): 260.5, (17, 31): 105.3} for (P, Pp, kstar) in FROZEN: D = d_of(P) Q = Q_of(P, Pp) kmax = len(COMMITTED[(P, Pp)]) measured.append({"step": "%d#->%d#" % (P, Pp), "D": D, "Q": len(Q), "kmax": kmax, "seconds": ext[(P, Pp)], "units": D * len(Q) * kmax * (1 << kmax), "source": "externally observed (job #2048's own log), not re-measured"}) for m in measured: print(" %-10s D=%-8d |Q|=%d kmax=%-3d units=%.3e %.1fs -> %.3e s/unit" % (m["step"], m["D"], m["Q"], m["kmax"], m["units"], m["seconds"], m["seconds"] / m["units"])) rates = [m["seconds"] / m["units"] for m in measured] c_lo, c_hi = min(rates), max(rates) c_mid = sum(rates) / len(rates) # leave-one-out: predict each point from the other two (what the price band must survive) loo = [] for i, m in enumerate(measured): others = [rates[j] for j in range(len(measured)) if j != i] c = sum(others) / len(others) pred = c * m["units"] loo.append({"step": m["step"], "predicted_s": round(pred, 2), "observed_s": m["seconds"], "ratio": round(pred / m["seconds"], 3)}) print(" rate band %.3e .. %.3e s/unit (mean %.3e); leave-one-out %s" % (c_lo, c_hi, c_mid, json.dumps(loo))) chk("F4.one_line_through_origin", max(l["ratio"] for l in loo) <= 2.0 and min(l["ratio"] for l in loo) >= 0.5, "leave-one-out predictions within a factor 2 of the measured times: %s" % [(l["step"], l["ratio"]) for l in loo]) print("== price of the two chain cells ==") prices = {} for (P, Pp) in PRICED: D = d_of(P) Q = Q_of(P, Pp) units_txt = ROUTE_PRICE_UNITS[(P, Pp)] k_from_route = sorted(set(int(round(__import__("math").log2(u / D))) for u in units_txt)) rows = [] for kmax in sorted(set(k_from_route + list(range(10, 22)))): units = D * len(Q) * kmax * (1 << kmax) rows.append({"kmax": kmax, "units": units, "seconds_lo": units * c_lo, "seconds_mid": units * c_mid, "seconds_hi": units * c_hi, "cpu_hours_mid": units * c_mid / 3600.0, "certificate_if_N_kmax_zero": "C2 <= %d" % kmax}) fits = {name: max([r["kmax"] for r in rows if r["seconds_mid"] <= lim] or [0]) for name, lim in BUDGETS_S.items()} prices["%d#->%d#" % (P, Pp)] = { "D": D, "Q": Q, "kmax_implied_by_route_pricing": k_from_route, "table": [{"kmax": r["kmax"], "seconds_mid": round(r["seconds_mid"], 1), "cpu_hours_mid": round(r["cpu_hours_mid"], 2), "fits_2h": r["seconds_mid"] <= 2 * 3600, "fits_4h_cap": r["seconds_mid"] <= 4 * 3600} for r in rows], "max_kmax_fitting": fits, } print(" %d#->%d#: kmax implied by the route's own price %s; largest kmax that fits " "2h/3h/4h = %d/%d/%d" % (P, Pp, k_from_route, fits["lane_2h"], fits["lane_3h"], fits["assignment_cap_4h"])) for r in rows: if r["kmax"] in k_from_route or r["kmax"] in (10, 12, 14, 16, 18, 20): print(" kmax=%-3d %8.1f s = %5.2f CPU-h (certifies C2 <= %d if " "N_kmax = 0)" % (r["kmax"], r["seconds_mid"], r["cpu_hours_mid"], r["kmax"])) # the decisive triage question: does the route's own next step fit ONE bounded assignment? worst = [] for key, p in prices.items(): k = p["kmax_implied_by_route_pricing"][-1] row = [r for r in p["table"] if r["kmax"] == k][0] worst.append((key, k, row["cpu_hours_mid"], row["fits_4h_cap"])) chk("D.priced cells fit one 4 CPU-h assignment at the route's own kmax", all(w[3] for w in worst), "; ".join("%s kmax=%d -> %.2f CPU-h" % (w[0], w[1], w[2]) for w in worst)) res = {"tool": "check-2053.py", "job": 2053, "route": 88, "engine_imported": engine_path, "engine_sha256": esha, "timing_mode": "skipped (arithmetic only)" if a.skip_timing else "three frozen steps re-run", "anchors": anchors, "measured": measured, "rate_s_per_unit": {"lo": c_lo, "mid": c_mid, "hi": c_hi}, "leave_one_out": loo, "prices": prices, "checks": checks, "summary": {"pass": sum(1 for c in checks if c["ok"]), "fail": sum(1 for c in checks if not c["ok"]), "of": len(checks)}} with open(a.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) print(json.dumps(res["summary"])) return 0 if res["summary"]["fail"] == 0 else 1 if __name__ == "__main__": sys.exit(main())