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 / tests / omegese45 / core / oracle.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: AGPL-3.0-only |
| 3 | # Independent test oracle for tests/omegese45/core/run-core.sh (aien-train #45, pipeline |
| 4 | # lane). Python stdlib only; it is a referee for the .om code, never part of the pipeline. |
| 5 | # oracle.py mask VOCAB.tsv EPISODES.oe45 K expected `M e t target mask` lines |
| 6 | # oracle.py policy EPISODES.oe45 reference-policy check (rc 0 / 1) |
| 7 | # oracle.py prefixes EPISODES.oe45 K OUT EXPECT decode prefixes and expected next messages |
| 8 | import sys |
| 9 | |
| 10 | HDR = "oe45 v1" |
| 11 | |
| 12 | |
| 13 | def episodes(path): |
| 14 | eps, cur = [], None |
| 15 | with open(path, encoding="ascii") as f: |
| 16 | for line in f.read().split("\n")[:-1]: |
| 17 | if line == HDR: |
| 18 | cur = [] |
| 19 | eps.append(cur) |
| 20 | else: |
| 21 | cur.append(line.split(" ")) |
| 22 | return eps |
| 23 | |
| 24 | |
| 25 | def ids_of(words, vocab): |
| 26 | out = [] |
| 27 | for w in words: |
| 28 | if w.startswith("0x"): |
| 29 | out.append(vocab["0x"]) |
| 30 | out.extend(vocab[c] for c in w[2:]) |
| 31 | else: |
| 32 | out.append(vocab[w]) |
| 33 | return out |
| 34 | |
| 35 | |
| 36 | def cmd_mask(vocab_path, ep_path, k): |
| 37 | vocab = {} |
| 38 | with open(vocab_path, encoding="ascii") as f: |
| 39 | for line in f: |
| 40 | i, tok = line.split("\t")[:2] |
| 41 | vocab[tok] = int(i) |
| 42 | for e, msgs in enumerate(episodes(ep_path)[: int(k)]): |
| 43 | t = 0 |
| 44 | for m in msgs: |
| 45 | ap = 1 if m[0] in ("act", "rep") else 0 |
| 46 | for i in ids_of(m, vocab): |
| 47 | print(f"M {e} {t} {i} {ap}") |
| 48 | t += 1 |
| 49 | return 0 |
| 50 | |
| 51 | |
| 52 | def check_policy(msgs): |
| 53 | g = msgs[0] |
| 54 | if g[0] != "goal": |
| 55 | return "no goal" |
| 56 | kind, dims = g[1], g[2:8] |
| 57 | seeds = [g[9]] if kind == "mm" else [g[9], g[11]] |
| 58 | vals = [int(dims[i], 16) for i in (1, 3, 5)] |
| 59 | rest = msgs[1:] |
| 60 | if any(v < 1 or v > 256 for v in vals): |
| 61 | want = [["act", "refuse", "bound", "end"], ["rep", "refused", "bound", "end"]] |
| 62 | return None if rest == want else "bound" |
| 63 | pos = 0 |
| 64 | for s in seeds: |
| 65 | tries = 0 |
| 66 | while True: |
| 67 | a = ["act", "job", "mm"] + dims + ["ulong", s, "end"] |
| 68 | if rest[pos] != a: |
| 69 | return "act" |
| 70 | r = rest[pos + 1] |
| 71 | pos += 2 |
| 72 | if r == ["res", "ok", "end"]: |
| 73 | break |
| 74 | if r == ["res", "fail", "transport", "end"] and tries == 0: |
| 75 | tries = 1 |
| 76 | continue |
| 77 | want = [["rep", r[1], r[2], "end"]] |
| 78 | return None if rest[pos:] == want else "stop" |
| 79 | want = [["rep", "ok"] + g[1:]] |
| 80 | return None if rest[pos:] == want else "report" |
| 81 | |
| 82 | |
| 83 | def cmd_policy(ep_path): |
| 84 | bad = 0 |
| 85 | eps = episodes(ep_path) |
| 86 | for e, msgs in enumerate(eps): |
| 87 | why = check_policy(msgs) |
| 88 | if why: |
| 89 | bad += 1 |
| 90 | print(f"POLICY_BAD {e} {why}") |
| 91 | print(f"POLICY {len(eps)} {bad}") |
| 92 | return 1 if bad else 0 |
| 93 | |
| 94 | |
| 95 | def cmd_prefixes(ep_path, k, out_path, exp_path): |
| 96 | with open(out_path, "w", encoding="ascii") as out, open(exp_path, "w", encoding="ascii") as exp: |
| 97 | for msgs in episodes(ep_path)[: int(k)]: |
| 98 | for i in range(1, len(msgs)): |
| 99 | prev = msgs[i - 1] |
| 100 | # Every A/P position: after G or R, and the P that follows `act refuse` (no R). |
| 101 | if msgs[i][0] in ("act", "rep") and (prev[0] in ("goal", "res") or prev[:2] == ["act", "refuse"]): |
| 102 | out.write(HDR + "\n" + "".join(" ".join(m) + "\n" for m in msgs[:i])) |
| 103 | exp.write(" ".join(msgs[i]) + "\n") |
| 104 | return 0 |
| 105 | |
| 106 | |
| 107 | def main(argv): |
| 108 | if len(argv) == 5 and argv[1] == "mask": |
| 109 | return cmd_mask(argv[2], argv[3], argv[4]) |
| 110 | if len(argv) == 3 and argv[1] == "policy": |
| 111 | return cmd_policy(argv[2]) |
| 112 | if len(argv) == 6 and argv[1] == "prefixes": |
| 113 | return cmd_prefixes(argv[2], argv[3], argv[4], argv[5]) |
| 114 | print("usage: oracle.py mask VOCAB EPS K | policy EPS | prefixes EPS K OUT EXPECT", file=sys.stderr) |
| 115 | return 2 |
| 116 | |
| 117 | |
| 118 | if __name__ == "__main__": |
| 119 | sys.exit(main(sys.argv)) |