aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / eval / omegese45 / oe_audit.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Audit an OE45 episode collection against interface v1.3 (eval-owned oracle). |
| 3 | |
| 4 | Usage: |
| 5 | oe_audit.py bundle FILE sealed bundle (`@ id stratum` + episodes) |
| 6 | oe_audit.py dir DIR directory of OE45 v1 episode files (*.oe45 or all files) |
| 7 | oe_audit.py files F1 F2 ... individual episode files |
| 8 | |
| 9 | For every episode: strict codec, grammar, sequence shape G (A R?)+ P, length |
| 10 | <= MAXLEN 176, and agreement of every model message with the eval reference |
| 11 | policy (policy_next) given the scripted R messages. Prints a coverage summary |
| 12 | and one line per disagreement. Exit 0 iff every episode passes. |
| 13 | Used both to self-check the sealed set and to check the pipeline generator's |
| 14 | train/dev output against the spec. |
| 15 | """ |
| 16 | import os |
| 17 | import sys |
| 18 | |
| 19 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 20 | from oe45lib import (CODES, EPISODE_HEADER, RCODES, admitted, |
| 21 | parse_episode, parse_line, policy_next, surface_to_ids) |
| 22 | from oe_episode import check_episode_shape |
| 23 | from oe_sealgen import read_bundle |
| 24 | |
| 25 | MAXLEN = 176 |
| 26 | |
| 27 | |
| 28 | def load(mode, args): |
| 29 | eps = [] |
| 30 | if mode == "bundle": |
| 31 | for eid, stratum, lines in read_bundle(open(args[0]).read()): |
| 32 | eps.append((eid, stratum, lines)) |
| 33 | else: |
| 34 | files = [] |
| 35 | if mode == "dir": |
| 36 | for root, _d, fs in os.walk(args[0]): |
| 37 | for f in sorted(fs): |
| 38 | files.append(os.path.join(root, f)) |
| 39 | files.sort() |
| 40 | else: |
| 41 | files = args |
| 42 | for p in files: |
| 43 | try: |
| 44 | lines = parse_episode(open(p, newline="").read()) |
| 45 | except (ValueError, UnicodeDecodeError) as e: |
| 46 | eps.append((p, "?", None, str(e))) |
| 47 | continue |
| 48 | eps.append((p, "-", lines)) |
| 49 | return eps |
| 50 | |
| 51 | |
| 52 | def audit_one(lines): |
| 53 | """Returns (ok, reason, info).""" |
| 54 | try: |
| 55 | msgs = [parse_line(l) for l in lines] |
| 56 | except ValueError as e: |
| 57 | return False, "grammar:%s" % e, None |
| 58 | shape = check_episode_shape(msgs) |
| 59 | if shape: |
| 60 | return False, "shape:" + shape, None |
| 61 | n = sum(len(surface_to_ids(l)) for l in lines) |
| 62 | if n > MAXLEN: |
| 63 | return False, "maxlen:%d" % n, None |
| 64 | for i, m in enumerate(msgs): |
| 65 | if m.kind in ("A", "P"): |
| 66 | want = policy_next(msgs[:i]) |
| 67 | if m.text != want: |
| 68 | return False, "policy@%d got[%s] want[%s]" % (i, m.text, want), None |
| 69 | return True, "ok", (msgs, n) |
| 70 | |
| 71 | |
| 72 | def main(argv): |
| 73 | if len(argv) < 3 or argv[1] not in ("bundle", "dir", "files"): |
| 74 | sys.stderr.write(__doc__) |
| 75 | return 2 |
| 76 | eps = load(argv[1], argv[2:]) |
| 77 | bad = 0 |
| 78 | cov = {"episodes": 0, "mm": 0, "pair": 0, "oob": 0, "retry": 0, "transport2": 0, |
| 79 | "rep_ok": 0, "maxlen_ids": 0} |
| 80 | rcov = {} |
| 81 | edge = set() |
| 82 | strata = {} |
| 83 | for e in eps: |
| 84 | eid, stratum, lines = e[0], e[1], e[2] |
| 85 | if lines is None: |
| 86 | bad += 1 |
| 87 | print("BAD %s format:%s" % (eid, e[3])) |
| 88 | continue |
| 89 | ok, why, info = audit_one(lines) |
| 90 | cov["episodes"] += 1 |
| 91 | strata[stratum] = strata.get(stratum, 0) + 1 |
| 92 | if not ok: |
| 93 | bad += 1 |
| 94 | print("BAD %s %s" % (eid, why)) |
| 95 | continue |
| 96 | msgs, n = info |
| 97 | g = msgs[0] |
| 98 | cov[g.jk] += 1 |
| 99 | cov["maxlen_ids"] = max(cov["maxlen_ids"], n) |
| 100 | for d in g.dims: |
| 101 | if d in (0x000, 0x001, 0x100, 0x101): |
| 102 | edge.add(d) |
| 103 | if any(not admitted(d) for d in g.dims): |
| 104 | cov["oob"] += 1 |
| 105 | nt = 0 |
| 106 | for m in msgs: |
| 107 | if m.kind == "R" and m.cls != "ok": |
| 108 | rcov["%s %s" % (m.cls, m.code)] = rcov.get("%s %s" % (m.cls, m.code), 0) + 1 |
| 109 | if m.code == "transport" and m.cls == "fail": |
| 110 | nt += 1 |
| 111 | if nt: |
| 112 | cov["retry"] += 1 |
| 113 | if any(m.kind == "R" and m.cls == "fail" and m.code == "transport" for m in msgs) and \ |
| 114 | msgs[-1].text == "rep fail transport end": |
| 115 | cov["transport2"] += 1 |
| 116 | if msgs[-1].cls == "ok": |
| 117 | cov["rep_ok"] += 1 |
| 118 | print("COVERAGE " + " ".join("%s=%d" % kv for kv in cov.items())) |
| 119 | print("STRATA " + " ".join("%s=%d" % kv for kv in sorted(strata.items()))) |
| 120 | print("RCODES " + " ".join("%s=%d" % kv for kv in sorted(rcov.items()))) |
| 121 | print("EDGES " + " ".join("0x%03x" % d for d in sorted(edge))) |
| 122 | missing = [c for c in RCODES if not any(k.endswith(" " + c) for k in rcov)] |
| 123 | print("MISSING_RCODES %s" % (" ".join(missing) if missing else "-")) |
| 124 | print("AUDIT %s episodes=%d bad=%d" % ("PASS" if bad == 0 else "FAIL", len(eps), bad)) |
| 125 | return 0 if bad == 0 else 1 |
| 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | sys.exit(main(sys.argv)) |