interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / train / omega-llm / tools / combined / experiment / selection.py

102 lines · 4510 bytes Raw Blame History
1"""Experimental symbolic program selection. This module receives only answered queries."""
2from collections import Counter
3from itertools import permutations
4import hashlib
5import re
6
7
8def candidates(engine, rows):
9 xs, ys = zip(*rows)
10 seen, kept = set(), []
11 constants = tuple(dict.fromkeys([0,1,2,3] + [int(v) for v in
12 re.findall(r"\b[0-9]{1,2}\b", engine.prompt.read_text().splitlines()[0])]))
13 full = engine.examples_spec(rows, constants)
14 english = engine.formal_spec()
15
16 def admit(name, result):
17 if result is None:
18 return
19 body = b"\n".join(b" ".join(line.split()) for line in result.splitlines() if line.strip())+b"\n"
20 if body in seen:
21 return
22 seen.add(body)
23 if engine.predict(body, xs) == list(ys):
24 kept.append((name, body))
25
26 # Give both engines a seat before later proposals. Proposals from descriptions
27 # also have to pass every observed row in the seeded execution sandbox.
28 generators = ("om_sketch", "om_synth") if engine.shape != 5 else ("om_sketch",)
29 for spec in dict.fromkeys(s for s in (full, english) if s is not None):
30 for name in generators:
31 admit(name, engine.symbolic(name, spec))
32 ordinal = 1
33 while len(kept) < 8:
34 body = engine.symbolic("om_sketch", full+f"skip {ordinal}\n".encode())
35 if body is None:
36 break
37 admit("om_sketch", body)
38 ordinal += 1
39 if "om_synth" in generators:
40 # Observation-equivalent expressions merge inside the native synthesizer.
41 # Change its search order and project the examples to obtain alternatives.
42 # Every proposal is still checked on all rows.
43 for subset in (rows,rows[::2],rows[1::2],rows[:-1],rows[1:],rows[:2],rows[-2:]):
44 if len(kept) == 8:
45 break
46 spec = engine.examples_spec(subset, tuple(reversed(constants)))
47 admit("om_synth", engine.symbolic("om_synth", spec))
48 if engine.shape in (2,3):
49 # Argument order also determines which representative survives observation
50 # deduplication in the expression search. Undo that permutation on its body.
51 names = (b"a",b"b",b"c")[:engine.shape]
52 for order in permutations(range(engine.shape)):
53 if len(kept) == 8:
54 break
55 if order == tuple(range(engine.shape)):
56 continue
57 permuted = [(tuple(x[i] for i in order),y) for x,y in rows]
58 source = engine.symbolic("om_synth",engine.examples_spec(permuted,constants))
59 if source:
60 rename = dict(zip(names,(names[i] for i in order)))
61 restored = b"\n".join(b" ".join(rename.get(w,w) for w in line.split())
62 for line in source.splitlines())
63 admit("om_synth",restored)
64 return kept
65
66
67def disagreement(values):
68 total = len(values)
69 return total*(total-1)//2 - sum(n*(n-1)//2 for n in Counter(values).values())
70
71
72def choose(engine, fixed, pool, query, adaptive):
73 rows, rounds = [], []
74 for x in fixed[:4 if adaptive else 8]:
75 rows.append((x,query(x)))
76 survivors = candidates(engine, rows)
77 rounds.append({"answers":len(rows),"generated":len(survivors)})
78 while len(rows) < 8:
79 used = {x for x,_ in rows}
80 unused = [x for x in pool if x not in used]
81 choice = next(x for x in fixed if x not in used)
82 pairs = 0
83 if len(survivors) >= 2:
84 predictions = [engine.predict(body,unused) for _,body in survivors]
85 for x, values in zip(unused,zip(*predictions,strict=True),strict=True):
86 score = disagreement(values)
87 if score > pairs:
88 choice,pairs = x,score
89 y = query(choice)
90 rows.append((choice,y))
91 survivors = [(name,body) for name,body in survivors if engine.predict(body,[choice])==[y]]
92 record = {"answers":len(rows),"disagreeing_pairs":pairs,"survived":len(survivors)}
93 if not survivors:
94 survivors = candidates(engine,rows)
95 record["generated"] = len(survivors)
96 rounds.append(record)
97 rounds[-1]["finalists"] = [{"engine":name,"bytes":len(body),"sha256":hashlib.sha256(body).hexdigest()}
98 for name,body in survivors]
99 if not survivors:
100 return None,"abstain",rounds
101 name,body=min(survivors,key=lambda item:len(item[1]))
102 return body,name,rounds