omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / train / omega-llm / tools / combined / experiment / engines.py
| 1 | """Subprocess boundary for the CPU experiment. No references or final labels enter this module.""" |
| 2 | from pathlib import Path |
| 3 | import subprocess |
| 4 | |
| 5 | |
| 6 | class ComponentFailure(RuntimeError): |
| 7 | def __init__(self, argv, result): |
| 8 | self.details = {"component": Path(argv[0]).name, "exit": result.returncode, |
| 9 | "stderr": result.stderr.decode(errors="replace")[-500:]} |
| 10 | super().__init__(self.details) |
| 11 | |
| 12 | |
| 13 | def invoke(argv, permitted): |
| 14 | timeout = 2400 if Path(argv[0]).name == "solve" else 240 |
| 15 | result = subprocess.run(argv, capture_output=True, timeout=timeout) |
| 16 | if result.returncode not in permitted: |
| 17 | raise ComponentFailure(argv, result) |
| 18 | return result |
| 19 | |
| 20 | |
| 21 | class Engines: |
| 22 | def __init__(self, kit, scratch, prompt): |
| 23 | self.kit = Path(kit).resolve() |
| 24 | self.scratch = Path(scratch).resolve() |
| 25 | self.prompt = self.scratch / "task.prompt" |
| 26 | self.prompt.write_text(prompt) |
| 27 | self.header = prompt.splitlines()[1] |
| 28 | self.shape = (5 if " edit " in self.header else 4 if " view " in self.header |
| 29 | else self.header.count(" ulong") - 1) |
| 30 | self.cache = {} |
| 31 | |
| 32 | def baseline(self, observations): |
| 33 | examples = self.scratch / "examples" |
| 34 | examples.write_text("".join(" ".join(map(str, (*x, *y)))+"\n" for x,y in observations)) |
| 35 | r = invoke([self.kit/"solve", self.prompt, examples], (0, 1)) |
| 36 | if r.returncode == 1: |
| 37 | if r.stdout or r.stderr.strip() != b"combined: no candidate passed the supplied examples": |
| 38 | raise RuntimeError(("unexpected abstention", r.stderr)) |
| 39 | return None, "abstain" |
| 40 | return r.stdout, r.stderr.decode().strip() |
| 41 | |
| 42 | def predict(self, body, xs): |
| 43 | cache = self.cache.setdefault(body, {}) |
| 44 | missing = list(dict.fromkeys(tuple(x) for x in xs if tuple(x) not in cache)) |
| 45 | for start in range(0, len(missing), 24): |
| 46 | self._predict_batch(body, missing[start:start+24], cache) |
| 47 | return [cache[tuple(x)] for x in xs] |
| 48 | |
| 49 | def _predict_batch(self, body, xs, cache): |
| 50 | bodyfile, inputs = self.scratch/"body", self.scratch/"arguments" |
| 51 | bodyfile.write_bytes(body) |
| 52 | inputs.write_text("".join(" ".join(map(str, x))+"\n" for x in xs)) |
| 53 | result = invoke([self.kit/"predict", self.kit, self.scratch, self.kit/"omc", |
| 54 | self.prompt, bodyfile, inputs], (0, 1, 2)) |
| 55 | if result.returncode == 2 and len(xs) > 1: |
| 56 | middle = len(xs)//2 |
| 57 | self._predict_batch(body, xs[:middle], cache) |
| 58 | self._predict_batch(body, xs[middle:], cache) |
| 59 | elif result.returncode: |
| 60 | cache.update((x, None) for x in xs) |
| 61 | else: |
| 62 | lines = (self.scratch/"bench-out").read_text().splitlines() |
| 63 | values = [tuple(map(int, line.split())) for line in lines] |
| 64 | if len(values) != len(xs) or any(len(y) != (8 if self.shape == 5 else 1) for y in values): |
| 65 | raise RuntimeError("incomplete sandbox predictions") |
| 66 | cache.update(zip(xs, values)) |
| 67 | |
| 68 | def examples_spec(self, observations, constants=(0, 1, 2, 3)): |
| 69 | spec = [f"arity {self.shape}", "depth 2", "consts " + " ".join(map(str, constants))] |
| 70 | for x, y in observations: |
| 71 | if self.shape == 4: |
| 72 | row = ("fold", x[0], *x[1:1+x[0]], *y) |
| 73 | else: |
| 74 | row = ("rew" if self.shape == 5 else "case", *x, *y) |
| 75 | spec.append(" ".join(map(str, row))) |
| 76 | return ("\n".join(spec)+"\n").encode() |
| 77 | |
| 78 | def formal_spec(self): |
| 79 | path = self.scratch / "english.spec" |
| 80 | r = invoke([self.kit/"om_formalize", "formalize", self.prompt, path], (0, 1)) |
| 81 | if r.returncode: |
| 82 | if not r.stdout.startswith(b"FORMALIZE_ABSTAIN"): |
| 83 | raise RuntimeError(("formalizer", r.stdout, r.stderr)) |
| 84 | return None |
| 85 | return path.read_bytes() |
| 86 | |
| 87 | def symbolic(self, generator, spec): |
| 88 | path = self.scratch / "examples.spec" |
| 89 | path.write_bytes(spec) |
| 90 | target = self.scratch / "function.om" |
| 91 | r = invoke([self.kit/generator, "solve", path, target], (0, 1)) |
| 92 | if r.returncode: |
| 93 | expected = b"SKETCH_FAIL" if generator == "om_sketch" else b"SYNTH_FAIL" |
| 94 | if not r.stdout.startswith(expected): |
| 95 | raise RuntimeError((generator, r.stdout, r.stderr)) |
| 96 | return None |
| 97 | header, body = target.read_bytes().split(b"\n", 1) |
| 98 | if header != self.header.encode(): |
| 99 | return None |
| 100 | return body |