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_sealgen.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Independent sealed-set generator for aien-train #45 (eval lane). |
| 3 | |
| 4 | Usage: oe_sealgen.py SEEDFILE OUTFILE |
| 5 | |
| 6 | SEEDFILE holds one line: 64 lowercase hex characters (the eval lineage seed). |
| 7 | OUTFILE receives the sealed bundle: for each episode a line |
| 8 | `@ <id> <stratum>` followed by the OE45 v1 episode lines (`oe45 v1`, messages). |
| 9 | |
| 10 | Written from interface v1.1 sections 2, 3 and 7 only. It does not import or |
| 11 | read the pipeline generator, the translator or the contract lane's code. |
| 12 | Randomness is a SHA-256 counter-mode stream over the seed (no dependence on |
| 13 | the Python `random` module's version-specific algorithms). Strata and the |
| 14 | outcome distribution are declared in docs/omegese45/EVAL-CONTRACT.md §3. |
| 15 | """ |
| 16 | import hashlib |
| 17 | import sys |
| 18 | |
| 19 | if __package__ in (None, ""): |
| 20 | sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__))) |
| 21 | from oe45lib import (ACT_REFUSE, EPISODE_HEADER, RCODES, RUNTIME_CLASS, admitted, |
| 22 | fmt_act, fmt_goal, fmt_rep, fmt_rep_ok, fmt_res) |
| 23 | |
| 24 | GENERATOR_ID = "oe_sealgen v1" |
| 25 | |
| 26 | |
| 27 | class Stream: |
| 28 | """Deterministic integer stream: SHA-256(seed || label || counter).""" |
| 29 | |
| 30 | def __init__(self, seed_bytes, label): |
| 31 | self.key = seed_bytes + b"|" + label.encode() |
| 32 | self.ctr = 0 |
| 33 | self.buf = b"" |
| 34 | |
| 35 | def _bytes(self, n): |
| 36 | while len(self.buf) < n: |
| 37 | self.buf += hashlib.sha256(self.key + self.ctr.to_bytes(8, "big")).digest() |
| 38 | self.ctr += 1 |
| 39 | out, self.buf = self.buf[:n], self.buf[n:] |
| 40 | return out |
| 41 | |
| 42 | def below(self, n): |
| 43 | """Uniform integer in [0, n) by rejection sampling over 32-bit draws.""" |
| 44 | assert 0 < n <= 1 << 32 |
| 45 | lim = (1 << 32) - ((1 << 32) % n) |
| 46 | while True: |
| 47 | v = int.from_bytes(self._bytes(4), "big") |
| 48 | if v < lim: |
| 49 | return v % n |
| 50 | |
| 51 | def rng(self, lo, hi): |
| 52 | return lo + self.below(hi - lo + 1) |
| 53 | |
| 54 | def pick(self, seq): |
| 55 | return seq[self.below(len(seq))] |
| 56 | |
| 57 | |
| 58 | NONT = [c for c in RCODES if c != "transport"] # 7 non-transport C' codes |
| 59 | |
| 60 | |
| 61 | def res_for(code): |
| 62 | """Runtime R for a C' code, with its aienet0-run class.""" |
| 63 | return ("ok", None) if code == "ok" else (RUNTIME_CLASS[code], code) |
| 64 | |
| 65 | |
| 66 | def build_episode(kind, dims, seeds, script): |
| 67 | """Write one episode from a per-job outcome script. |
| 68 | |
| 69 | script: list (one entry per job) of attempt lists; each attempt is an R as |
| 70 | (cls, code). Attempt patterns follow interface §3.2: [ok], [fail transport, |
| 71 | ok], [fail transport, fail transport], [X], [fail transport, X]. |
| 72 | Returns the list of message lines (without the header). |
| 73 | """ |
| 74 | lines = [fmt_goal(kind, dims, seeds)] |
| 75 | if any(not admitted(d) for d in dims): |
| 76 | return lines + [ACT_REFUSE, fmt_rep("refused", "bound")] |
| 77 | for j, attempts in enumerate(script): |
| 78 | for a, (cls, code) in enumerate(attempts): |
| 79 | lines.append(fmt_act(dims, seeds[j])) |
| 80 | lines.append(fmt_res(cls, code)) |
| 81 | if cls == "ok": |
| 82 | break |
| 83 | if cls == "fail" and code == "transport" and a == 0: |
| 84 | continue # first transport failure on this job: retry once |
| 85 | return lines + [fmt_rep(cls, code)] |
| 86 | return lines + [fmt_rep_ok(kind, dims, seeds)] |
| 87 | |
| 88 | |
| 89 | def check_script(kind, script): |
| 90 | njobs = 1 if kind == "mm" else 2 |
| 91 | assert 1 <= len(script) <= njobs |
| 92 | for j, att in enumerate(script): |
| 93 | assert 1 <= len(att) <= 2 |
| 94 | if len(att) == 2: |
| 95 | assert att[0] == ("fail", "transport") |
| 96 | last = att[-1] |
| 97 | stopped = last != ("ok", None) |
| 98 | if stopped: |
| 99 | assert j == len(script) - 1, "no job after a stop" |
| 100 | if len(script) < njobs: |
| 101 | assert script[-1][-1] != ("ok", None) |
| 102 | |
| 103 | |
| 104 | def T(): |
| 105 | return ("fail", "transport") |
| 106 | |
| 107 | |
| 108 | OK = ("ok", None) |
| 109 | |
| 110 | |
| 111 | def gen(seed_hex): |
| 112 | seed = bytes.fromhex(seed_hex) |
| 113 | assert len(seed) == 32 |
| 114 | eps = [] |
| 115 | |
| 116 | def add(stratum, kind, dims, seeds, script): |
| 117 | if all(admitted(d) for d in dims): |
| 118 | check_script(kind, script) |
| 119 | lines = build_episode(kind, dims, seeds, script) |
| 120 | eps.append((stratum, lines)) |
| 121 | |
| 122 | def adm_dims(s): |
| 123 | return (s.rng(1, 256), s.rng(1, 256), s.rng(1, 256)) |
| 124 | |
| 125 | def seeds_for(s, kind): |
| 126 | if kind == "mm": |
| 127 | return (s.below(65536),) |
| 128 | a = s.below(65536) |
| 129 | b = s.below(65535) |
| 130 | if b >= a: |
| 131 | b += 1 # distinct seeds in every stratum except PAIRREPLAY |
| 132 | return (a, b) |
| 133 | |
| 134 | # EDGE: every dim position x {0x000, 0x001, 0x100, 0x101, 0xfff} x kind. |
| 135 | s = Stream(seed, "EDGE") |
| 136 | for kind in ("mm", "pair"): |
| 137 | for pos in range(3): |
| 138 | for v in (0x000, 0x001, 0x100, 0x101, 0xfff): |
| 139 | dims = list(adm_dims(s)) |
| 140 | dims[pos] = v |
| 141 | sd = seeds_for(s, kind) |
| 142 | script = [[OK]] * (1 if kind == "mm" else 2) |
| 143 | add("EDGE", kind, tuple(dims), sd, script) |
| 144 | |
| 145 | # CODE: every non-transport C' code, mm, and pair at job 1 and job 2. |
| 146 | s = Stream(seed, "CODE") |
| 147 | for code in NONT: |
| 148 | add("CODE", "mm", adm_dims(s), seeds_for(s, "mm"), [[res_for(code)]]) |
| 149 | add("CODE", "pair", adm_dims(s), seeds_for(s, "pair"), [[res_for(code)]]) |
| 150 | add("CODE", "pair", adm_dims(s), seeds_for(s, "pair"), [[OK], [res_for(code)]]) |
| 151 | |
| 152 | # RETRY: transport retry patterns, three replicas each. |
| 153 | s = Stream(seed, "RETRY") |
| 154 | for _rep in range(3): |
| 155 | mm_pats = [[[T(), OK]], [[T(), T()]]] + [[[T(), res_for(c)]] for c in NONT] |
| 156 | for pat in mm_pats: |
| 157 | add("RETRY", "mm", adm_dims(s), seeds_for(s, "mm"), pat) |
| 158 | x1, x2 = s.pick(NONT), s.pick(NONT) |
| 159 | pair_pats = [ |
| 160 | [[T(), OK], [OK]], |
| 161 | [[OK], [T(), OK]], |
| 162 | [[T(), OK], [T(), OK]], |
| 163 | [[T(), T()]], |
| 164 | [[OK], [T(), T()]], |
| 165 | [[T(), OK], [T(), T()]], |
| 166 | [[T(), res_for(x1)]], |
| 167 | [[OK], [T(), res_for(x2)]], |
| 168 | ] |
| 169 | for pat in pair_pats: |
| 170 | add("RETRY", "pair", adm_dims(s), seeds_for(s, "pair"), pat) |
| 171 | |
| 172 | # PAIRREPLAY: pair with equal seeds; job 2 is the same document -> refused replay. |
| 173 | s = Stream(seed, "PAIRREPLAY") |
| 174 | for _ in range(5): |
| 175 | sv = s.below(65536) |
| 176 | add("PAIRREPLAY", "pair", adm_dims(s), (sv, sv), [[OK], [res_for("replay")]]) |
| 177 | |
| 178 | # XCLASS: grammar-legal R with the class the runtime never pairs with the code |
| 179 | # (e.g. `res fail replay`, `res refused transport`). Policy: mirror; only |
| 180 | # `res fail transport` triggers a retry. Reported separately, not thresholded. |
| 181 | s = Stream(seed, "XCLASS") |
| 182 | for i in range(10): |
| 183 | code = RCODES[i % len(RCODES)] |
| 184 | cls = "refused" if RUNTIME_CLASS[code] == "fail" else "fail" |
| 185 | kind = "mm" if i % 2 == 0 else "pair" |
| 186 | add("XCLASS", kind, adm_dims(s), seeds_for(s, kind), [[(cls, code)]]) |
| 187 | |
| 188 | # RAND: 400 episodes from the declared distribution (EVAL-CONTRACT §3). |
| 189 | s = Stream(seed, "RAND") |
| 190 | for _ in range(400): |
| 191 | kind = "mm" if s.below(2) == 0 else "pair" |
| 192 | dims = list(adm_dims(s)) |
| 193 | if s.below(5) == 0: # p = 0.2: out-of-bound goal |
| 194 | nbad = 1 if s.below(4) else 2 # 3/4 one bad dim, 1/4 two |
| 195 | positions = [0, 1, 2] |
| 196 | for _b in range(nbad): |
| 197 | pos = positions.pop(s.below(len(positions))) |
| 198 | dims[pos] = 0 if s.below(8) == 0 else s.rng(257, 4095) |
| 199 | sd = seeds_for(s, kind) |
| 200 | script = [] |
| 201 | for _j in range(1 if kind == "mm" else 2): |
| 202 | att = [] |
| 203 | for a in range(2): |
| 204 | r = s.below(20) # ok 12/20, transport 3/20, other 5/20 |
| 205 | if r < 12: |
| 206 | att.append(OK) |
| 207 | elif r < 15: |
| 208 | att.append(T()) |
| 209 | else: |
| 210 | att.append(res_for(s.pick(NONT))) |
| 211 | if att[-1] != T() or a == 1: |
| 212 | break |
| 213 | script.append(att) |
| 214 | if att[-1] != OK: |
| 215 | break |
| 216 | add("RAND", kind, tuple(dims), sd, script) |
| 217 | return eps |
| 218 | |
| 219 | |
| 220 | def render(eps): |
| 221 | out = [] |
| 222 | for i, (stratum, lines) in enumerate(eps): |
| 223 | out.append("@ s%04d %s" % (i, stratum)) |
| 224 | out.append(EPISODE_HEADER) |
| 225 | out.extend(lines) |
| 226 | return "\n".join(out) + "\n" |
| 227 | |
| 228 | |
| 229 | def read_bundle(text): |
| 230 | """Sealed bundle text -> list of (id, stratum, [message lines]).""" |
| 231 | eps = [] |
| 232 | for ln in text.rstrip("\n").split("\n"): |
| 233 | if ln.startswith("@ "): |
| 234 | _, eid, stratum = ln.split(" ") |
| 235 | eps.append((eid, stratum, [])) |
| 236 | elif ln == EPISODE_HEADER: |
| 237 | continue |
| 238 | else: |
| 239 | eps[-1][2].append(ln) |
| 240 | return eps |
| 241 | |
| 242 | |
| 243 | def main(argv): |
| 244 | if len(argv) != 3: |
| 245 | sys.stderr.write("usage: oe_sealgen.py SEEDFILE OUTFILE\n") |
| 246 | return 2 |
| 247 | seed_hex = open(argv[1]).read().strip() |
| 248 | if len(seed_hex) != 64 or any(c not in "0123456789abcdef" for c in seed_hex): |
| 249 | sys.stderr.write("seed must be 64 lowercase hex chars\n") |
| 250 | return 2 |
| 251 | text = render(gen(seed_hex)) |
| 252 | with open(argv[2], "w", newline="\n") as f: |
| 253 | f.write(text) |
| 254 | print("%s episodes=%d sha256=%s" % (GENERATOR_ID, text.count("\n@ ") + 1, |
| 255 | hashlib.sha256(text.encode()).hexdigest())) |
| 256 | return 0 |
| 257 | |
| 258 | |
| 259 | if __name__ == "__main__": |
| 260 | sys.exit(main(sys.argv)) |