Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-bridge/blob/main/examples/deepinfer/diff.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / examples / deepinfer / diff.py` 202 lines ยท 5917 bytes Raw Blame History 1 """Differential test of the Omega unit examples/deepinfer/flops_pairs.om through the generated Python 2 binding (omega_native.py, ctypes) against Python oracles: term-by-term enumeration of 3 sum(min(t // ratio, cap)) for the grid, and for large values a step model in Python integers (does a 4 step of the upstream C++ expression leave int64_t?) plus the second closed form 5 ratio*b*(b-1)//2 + b*(s+1), n = ratio*b + s. Run by run.sh as 6 python3 -I diff.py BUILD_DIR BOUNDARY_FILE SEED COUNT; prints one line per section and exits 0 only when 7 nothing mismatched. The grid is diff.cpp's: n, begin, end in 0..300, ratio 1..40, cap 0..40.""" 8 import ctypes 9 import random 10 import sys 11 12 sys.path.insert(0, sys.argv[1]) 13 import omega_native as om # noqa: E402 every Omega declaration comes from the generated binding 14 15 LO, HI = -(1 << 63), (1 << 63) - 1 16 SENTINEL = 0x5555555555555555 17 FAILS = [] 18 19 20 def bits(v): 21 return v & 0xFFFFFFFFFFFFFFFF 22 23 24 def signed(u): 25 return u - (1 << 64) if u >> 63 else u 26 27 28 def call(fn, *args): 29 r = ctypes.c_uint64(SENTINEL) 30 st = fn(ctypes.byref(r), *(bits(a) for a in args)) 31 return st, signed(r.value), st == 0 or r.value == SENTINEL 32 33 34 FN = {"fp": om.omega2_floor_prefix, "cp": om.omega2_capped_prefix, "pairs": om.omega2_pairs} 35 36 37 def trunc_div(a, b): # C++ / on integers 38 q = abs(a) // abs(b) 39 return q if (a >= 0) == (b >= 0) else -q 40 41 42 class Ovf(Exception): 43 pass 44 45 46 def chk(v): 47 if not LO <= v <= HI: 48 raise Ovf 49 return v 50 51 52 def m_fp(n, r): 53 b = trunc_div(n, r) 54 a = chk(b * chk(n + 1)) 55 t = chk(chk(r * b) * chk(b + 1)) 56 return chk(a - trunc_div(t, 2)) 57 58 59 def m_cp(n, r, c): 60 stop = min(n, chk(r * c)) 61 return chk(m_fp(stop, r) + chk(chk(n - stop) * c)) 62 63 64 def m_pr(b, e, r, c): 65 if c == 0: 66 return chk(m_fp(e, r) - m_fp(b, r)) 67 return chk(m_cp(e, r, c) - m_cp(b, r, c)) 68 69 70 def model(fn, a): 71 """None when a step leaves int64_t (C++ undefined behaviour), else the value.""" 72 try: 73 return {"fp": m_fp, "cp": m_cp, "pairs": m_pr}[fn](*a) 74 except Ovf: 75 return None 76 77 78 def alt_fp(n, r): 79 b, s = divmod(n, r) 80 return r * b * (b - 1) // 2 + b * (s + 1) 81 82 83 def alt_cp(n, r, c): 84 s = min(n, r * c) 85 return alt_fp(s, r) + (n - s) * c 86 87 88 def alt(fn, a): 89 if fn == "fp": 90 return alt_fp(*a) 91 if fn == "cp": 92 return alt_cp(*a) 93 b, e, r, c = a 94 return alt_fp(e, r) - alt_fp(b, r) if c == 0 else alt_cp(e, r, c) - alt_cp(b, r, c) 95 96 97 def bad(what, fn, a, got): 98 FAILS.append((what, fn, a, got)) 99 if len(FAILS) <= 20: 100 print("MISMATCH", what, fn, *a, got) 101 102 103 def grid(): 104 n_fp = n_cp = n_pr = 0 105 m0 = len(FAILS) 106 for r in range(1, 41): 107 acc = 0 108 for n in range(301): 109 acc += n // r 110 n_fp += 1 111 got = call(FN["fp"], n, r) 112 if got[:2] != (0, acc): 113 bad("grid", "fp", (n, r), got) 114 for c in range(41): 115 acc = 0 116 for n in range(301): 117 acc += min(n // r, c) 118 n_cp += 1 119 got = call(FN["cp"], n, r, c) 120 if got[:2] != (0, acc): 121 bad("grid", "cp", (n, r, c), got) 122 for r in range(1, 41): 123 for c in range(41): 124 for b in range(301): 125 acc = 0 126 for e in range(b, 301): 127 if e > b: 128 acc += e // r if c == 0 else min(e // r, c) 129 n_pr += 1 130 got = call(FN["pairs"], b, e, r, c) 131 if got[:2] != (0, acc): 132 bad("grid", "pairs", (b, e, r, c), got) 133 print("PY-GRID floor_prefix n 0..300 ratio 1..40 cases=%d; capped_prefix cap 0..40 cases=%d; " 134 "pairs begin<=end 0..300 cases=%d; mismatches=%d" % (n_fp, n_cp, n_pr, len(FAILS) - m0)) 135 136 137 def mag(g, lo): 138 k = g.randrange(64) 139 return max(g.getrandbits(k) if k else 0, lo) 140 141 142 def rand(seed, count): 143 g = random.Random(seed) 144 m0 = len(FAILS) 145 defined = ovf = 0 146 for i in range(count): 147 n, b, r, c = mag(g, 0), mag(g, 0), mag(g, 1), mag(g, 0) 148 b, n = min(b, n), max(b, n) 149 fn = ("fp", "cp", "pairs")[i % 3] 150 a = {"fp": (n, r), "cp": (n, r, c), "pairs": (b, n, r, c if g.randrange(4) else 0)}[fn] 151 want = model(fn, a) 152 got = call(FN[fn], *a) 153 if want is None: 154 ovf += 1 155 if got[0] != 1 or not got[2]: 156 bad("random-ovf", fn, a, got) 157 else: 158 defined += 1 159 if got[:2] != (0, want) or want != alt(fn, a): 160 bad("random", fn, a, got) 161 print("PY-RANDOM seed=%d defined=%d overflow=%d mismatches=%d" % (seed, defined, ovf, len(FAILS) - m0)) 162 if not defined or not ovf: 163 FAILS.append(("random", "one class empty", (), ())) 164 165 166 def boundary(path): 167 m0 = len(FAILS) 168 k = 0 169 for line in open(path): 170 line = line.split("#")[0].strip() 171 if not line: 172 continue 173 lhs, expect = (s.strip() for s in line.split("|")) 174 fn, *args = lhs.split() 175 a = tuple(int(x) for x in args) 176 got = call(FN[fn], *a) 177 k += 1 178 if expect == "dom": 179 ok = got[0] == 9 and got[2] 180 elif expect == "ovf": 181 ok = got[0] == 1 and got[2] and model(fn, a) is None 182 else: 183 ok = got[:2] == (0, int(expect)) and model(fn, a) == int(expect) 184 if not ok: 185 bad("boundary", fn, a, got) 186 print("PY-BOUNDARY cases=%d mismatches=%d" % (k, len(FAILS) - m0)) 187 188 189 def main(): 190 ident = om.omega2_ident_flops_pairs() 191 print("PY-IDENT", ident.decode()) 192 if ident != om.OMEGA2_IDENT_flops_pairs: 193 FAILS.append(("ident", ident)) 194 grid() 195 rand(int(sys.argv[3]), int(sys.argv[4])) 196 boundary(sys.argv[2]) 197 print("PY-TOTAL mismatches=%d" % len(FAILS)) 198 return 1 if FAILS else 0 199 200 201 if __name__ == "__main__": 202 sys.exit(main())