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 / fixture.py
| 1 | """Frozen CPU-only development and held-out tasks. Only the experiment judge imports ref().""" |
| 2 | import argparse |
| 3 | from collections import Counter |
| 4 | import hashlib |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import random |
| 8 | |
| 9 | U64 = (1 << 64) - 1 |
| 10 | MANIFEST = Path(__file__).with_name("manifest.json") |
| 11 | SIGNATURE = {1: "a ulong", 2: "a ulong b ulong", 3: "a ulong b ulong c ulong", |
| 12 | 4: "a view ulong 8 n ulong", 5: "a edit ulong 8 n ulong"} |
| 13 | |
| 14 | # Variants of a template always stay in the same split. K is interpolated in prose and |
| 15 | # passed to a separate reference implementation. Each evaluation family has four variants. |
| 16 | FAMILIES = [ |
| 17 | ("eval","branch","threshold-gap",2,"if the first argument exceeds the second by more than {k}, return their difference, otherwise return the second argument"), |
| 18 | ("eval","branch","equal-offset",2,"return the sum of the arguments when the first equals the second plus {k}, and the smaller argument otherwise"), |
| 19 | ("eval","branch","minimum-offset",3,"return the third argument when the first plus {k} is below the second, otherwise return the first"), |
| 20 | ("eval","branch","masked-choice",3,"return the second argument when the first has bit {k} set, and the third argument otherwise"), |
| 21 | ("eval","branch","residue-choice",2,"return the first argument if its remainder modulo {k} equals the second argument, and their sum otherwise"), |
| 22 | ("eval","arithmetic","scaled-sum",2,"return {k} times the first argument plus the second"), |
| 23 | ("eval","arithmetic","product-offset",2,"return the product of the first argument plus {k} and the second argument plus one"), |
| 24 | ("eval","arithmetic","floor-before-add",2,"return the first argument divided by {k}, rounded down, plus the second argument"), |
| 25 | ("eval","arithmetic","binary-mix",2,"return the bitwise exclusive or of the first argument and the second argument multiplied by {k}"), |
| 26 | ("eval","arithmetic","paired-residue",2,"return the sum of the two remainders after dividing each argument by {k}"), |
| 27 | ("eval","read","stride-total",4,"sum values at indexes divisible by {k} in the first n items"), |
| 28 | ("eval","read","larger-step-count",4,"count consecutive pairs in the first n items where the right value exceeds the left value by at least {k}"), |
| 29 | ("eval","read","scaled-alternates",4,"sum the first n items, multiplying each odd-indexed value by {k} first"), |
| 30 | ("eval","read","clipped-total",4,"sum the first n values after capping each at {k}"), |
| 31 | ("eval","read","index-class",4,"count values among the first n whose remainder modulo {k} equals their index modulo {k}"), |
| 32 | ("eval","rewrite","shifted-index-xor",5,"exclusive-or each of the first n items with its index plus {k}"), |
| 33 | ("eval","rewrite","alternating-scale",5,"multiply odd-indexed items among the first n by {k} and preserve the others"), |
| 34 | ("eval","rewrite","pairwise-swap-offset",5,"swap each complete adjacent pair among the first n and add {k} to the new left member; preserve any unpaired item"), |
| 35 | ("eval","rewrite","local-peaks",5,"add {k} to each strict interior local maximum of the original first n items; leave the endpoints alone"), |
| 36 | ("eval","rewrite","clipped-gap",5,"replace each of the first n items by its excess above {k}, or by zero if it does not exceed {k}"), |
| 37 | ("dev","branch","sum-cutoff",2,"return the larger argument if their sum is above {k} and zero otherwise"), |
| 38 | ("dev","branch","sign-choice",3,"if the first argument is smaller than the second return the third plus {k}, otherwise return the third"), |
| 39 | ("dev","arithmetic","unary-square",1,"return the square of the argument plus {k}"), |
| 40 | ("dev","arithmetic","squared-distance",2,"return the squared difference of the arguments plus {k}"), |
| 41 | ("dev","read","residue-total",4,"sum the remainders modulo {k} of the first n items"), |
| 42 | ("dev","read","large-item-count",4,"count the first n items that exceed {k}"), |
| 43 | ("dev","rewrite","adjacent-add",5,"add {k} times the next original item to each of the first n items except the last"), |
| 44 | ("dev","rewrite","anchor-add",5,"add {k} times the original first item to every item among the first n"), |
| 45 | ("dev","rewrite","constant-add",5,"add {k} to each of the first n items"), |
| 46 | ("dev","rewrite","constant-xor",5,"apply bitwise exclusive or with {k} to each of the first n items"), |
| 47 | ("dev","rewrite","constant-fill",5,"fill the first n items with {k}"), |
| 48 | ("dev","rewrite","prefix-high",5,"set each of the first n items to the largest original value up to that point plus {k}"), |
| 49 | ] |
| 50 | |
| 51 | |
| 52 | def scalar(f, k, x): |
| 53 | a, b, c = (*x, 0, 0)[:3] |
| 54 | if f == "threshold-gap": return a-b if a > b+k else b |
| 55 | if f == "equal-offset": return a+b if a == b+k else min(a,b) |
| 56 | if f == "minimum-offset": return c if a+k < b else a |
| 57 | if f == "masked-choice": return b if a & (1<<k) else c |
| 58 | if f == "residue-choice": return a if a%k == b else a+b |
| 59 | if f == "scaled-sum": return k*a+b |
| 60 | if f == "product-offset": return (a+k)*(b+1) |
| 61 | if f == "floor-before-add": return a//k+b |
| 62 | if f == "binary-mix": return a^(b*k) |
| 63 | if f == "paired-residue": return a%k+b%k |
| 64 | if f == "sum-cutoff": return max(a,b) if a+b>k else 0 |
| 65 | if f == "sign-choice": return c+k if a<b else c |
| 66 | if f == "unary-square": return a*a+k |
| 67 | if f == "squared-distance": return (a-b)**2+k |
| 68 | raise KeyError(f) |
| 69 | |
| 70 | |
| 71 | def array(f, k, x, rewrite): |
| 72 | n, *a = x |
| 73 | active = a[:n] |
| 74 | if not rewrite: |
| 75 | if f == "stride-total": return sum(active[::k]) |
| 76 | if f == "larger-step-count": return sum(right>=left+k for left,right in zip(active,active[1:])) |
| 77 | if f == "scaled-alternates": return sum(active[::2])+k*sum(active[1::2]) |
| 78 | if f == "clipped-total": return sum(min(v,k) for v in active) |
| 79 | if f == "index-class": return sum(v%k == i%k for i,v in enumerate(active)) |
| 80 | if f == "residue-total": return sum(v%k for v in active) |
| 81 | if f == "large-item-count": return sum(v>k for v in active) |
| 82 | raise KeyError(f) |
| 83 | out = list(a) |
| 84 | for i, v in enumerate(active): |
| 85 | if f == "shifted-index-xor": out[i] = v^(i+k) |
| 86 | elif f == "alternating-scale": out[i] = k*v if i%2 else v |
| 87 | elif f == "pairwise-swap-offset": |
| 88 | if i%2: out[i] = a[i-1] |
| 89 | elif i+1<n: out[i] = a[i+1]+k |
| 90 | elif f == "local-peaks": |
| 91 | if 0<i<n-1 and a[i-1]<v>a[i+1]: out[i] = v+k |
| 92 | elif f == "clipped-gap": out[i] = max(0,v-k) |
| 93 | elif f == "adjacent-add": out[i] = v+(k*a[i+1] if i+1<n else 0) |
| 94 | elif f == "anchor-add": out[i] = v+k*a[0] |
| 95 | elif f == "constant-add": out[i] = v+k |
| 96 | elif f == "constant-xor": out[i] = v^k |
| 97 | elif f == "constant-fill": out[i] = k |
| 98 | elif f == "prefix-high": out[i] = max(active[:i+1])+k |
| 99 | else: raise KeyError(f) |
| 100 | return tuple(v & U64 for v in out) |
| 101 | |
| 102 | |
| 103 | def ref(task, x): |
| 104 | f, k, shape = task["family"], task["k"], task["shape"] |
| 105 | y = scalar(f,k,x) if shape < 4 else array(f,k,x,shape == 5) |
| 106 | return y if shape == 5 else (y & U64,) |
| 107 | |
| 108 | |
| 109 | def inputs(shape): |
| 110 | rng = random.Random(20261123 + shape) |
| 111 | if shape == 1: |
| 112 | fixed = [(a,) for a in (23,18,22,27,35,54,81,210)] |
| 113 | pool = [(i,) for i in range(32)] |
| 114 | score = [(i,) for i in range(32,64)] |
| 115 | elif shape in (2,3): |
| 116 | fixed = [x[:shape] for x in [(2,11,5),(13,3,13),(11,11,11),(21,0,0), |
| 117 | (1,19,26),(32,7,14),(13,26,4),(20,4,1)]] |
| 118 | pool, score = [], [] |
| 119 | for stream, base in [(pool,0),(score,10)]: |
| 120 | for a,b in [(0,0),(0,1),(1,0),*((j,0) if base==0 else (0,j) for j in range(2,8)),*((base+j,base+j+d) for j in (6,8,11,16,31,32,33) for d in range(-6,7))]: |
| 121 | stream.append((a,b) if shape==2 else (a,b,(a+b+base)%13)) |
| 122 | stream.extend(tuple(rng.randrange(33) for _ in range(shape)) for _ in range(54)) |
| 123 | else: |
| 124 | a,b=(7,0,3,11,3,2,9,13),(9,2,3,2,1,12,0,9) |
| 125 | fixed = [(n,*v) for v in (a,b) for n in (0,2,5,8)] |
| 126 | pool,score=[],[] |
| 127 | for stream, offset in [(pool,0),(score,1)]: |
| 128 | arrays=[(offset,)*8,(offset,3,3,offset,0,5,5,1),tuple(range(offset,offset+8)), |
| 129 | (8,1,9,1,7,2,6,2+offset),tuple(rng.randrange(5) for _ in range(8))] |
| 130 | stream.extend((n,*v) for n in range(9) for v in arrays) |
| 131 | stream.extend((i%9,*(rng.randrange(12) for _ in range(8))) for i in range(30)) |
| 132 | pool = list(dict.fromkeys(pool + fixed)) |
| 133 | score = [x for x in dict.fromkeys(score) if x not in set(pool)] |
| 134 | return {"fixed":fixed,"pool":pool,"score":score} |
| 135 | |
| 136 | |
| 137 | def make(): |
| 138 | data = {"inputs":{str(s):inputs(s) for s in range(1,6)}, "tasks":[]} |
| 139 | for split,group,family,shape,description in FAMILIES: |
| 140 | for k in (2,3,4,5): |
| 141 | sig = "fn solve "+SIGNATURE[shape]+" to "+("void" if shape==5 else "ulong") |
| 142 | prompt = "note "+description.format(k=k)+"\n"+sig+"\n" |
| 143 | data["tasks"].append(dict(id=f"{split}-{family}-{k}", split=split, group=group, |
| 144 | family=family,k=k,shape=shape,prompt=prompt)) |
| 145 | return data |
| 146 | |
| 147 | |
| 148 | def tasks(split): |
| 149 | data=json.loads(MANIFEST.read_text()) |
| 150 | for t in data["tasks"]: |
| 151 | if t["split"] == split: |
| 152 | yield {**t, **{key:list(map(tuple,xs)) for key,xs in data["inputs"][str(t["shape"])].items()}} |
| 153 | |
| 154 | |
| 155 | def check(): |
| 156 | assert json.loads(MANIFEST.read_text()) == json.loads(json.dumps(make())) |
| 157 | dev,ev=list(tasks("dev")),list(tasks("eval")) |
| 158 | assert Counter(t["group"] for t in ev)==dict.fromkeys(("arithmetic","branch","read","rewrite"),20) |
| 159 | assert not {t["family"] for t in dev}&{t["family"] for t in ev} |
| 160 | observed=set() |
| 161 | for t in [*dev,*ev]: |
| 162 | assert len(set(t["fixed"]))==len(t["fixed"])==8 and set(t["fixed"])<=set(t["pool"]) |
| 163 | assert not set(t["pool"])&set(t["score"]) and len(t["score"])>=20 |
| 164 | probes=t["pool"]+t["score"] |
| 165 | signature=(t["shape"],tuple(ref(t,x) for x in probes)) |
| 166 | assert signature not in observed,t["id"] |
| 167 | observed.add(signature) |
| 168 | if t["shape"]==5: |
| 169 | assert {x[0] for x in t["score"]}==set(range(9)) |
| 170 | assert all(ref(t,x)[x[0]:]==x[1+x[0]:] for x in probes) |
| 171 | print("Verified",len(dev),"development and",len(ev),"evaluation tasks.", |
| 172 | "Manifest SHA-256",hashlib.sha256(MANIFEST.read_bytes()).hexdigest()) |
| 173 | |
| 174 | |
| 175 | if __name__=="__main__": |
| 176 | p=argparse.ArgumentParser() |
| 177 | p.add_argument("--create",action="store_true") |
| 178 | if p.parse_args().create: |
| 179 | with MANIFEST.open("x") as out: |
| 180 | json.dump(make(),out,separators=(",",":")) |
| 181 | out.write("\n") |
| 182 | check() |