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.cpp # 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.cpp` 244 lines ยท 10640 bytes Raw Blame History 1 // Differential test of DeepInfer FlopsEstimator::floor_prefix, capped_prefix and pairs. Three sides: 2 // Up: the pinned upstream bytes (csrc/engine/flops_estimator.hpp lines 237-257, extracted verbatim by 3 // run.sh into upstream.inc and checked against its sha256), compiled here with GCC UBSan; 4 // Om: the Omega unit examples/deepinfer/flops_pairs.om through the generated omega_native.h; 5 // an oracle: direct enumeration (sum of min(t / ratio, cap) term by term) for the grid, and for large 6 // values a step model in __int128 (does a C++ step leave int64_t?) plus a second closed form, 7 // ratio*b*(b-1)/2 + b*(s+1) with n = ratio*b + s (evidence/deepinfer-flops-pairs/wolfram.md). 8 // Built -std=c++20 -fsanitize=undefined -fno-sanitize-recover=all: a case the step model calls defined 9 // that is undefined in the upstream code aborts the run. Modes: 10 // diff grid exhaustive n, begin, end in 0..300, ratio 1..40, cap 0..40 11 // diff random SEED N N seeded random large in-domain cases (both overflow classes) 12 // diff up FN ARG... the upstream function alone (run per boundary case: UBSan may abort) 13 // diff om FN ARG... the Omega function alone: "status S value V" 14 // FN is fp, cp or pairs. Every Omega declaration comes from omega_native.h. 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 23 #include "omega_native.h" 24 25 struct Up { 26 #include "upstream.inc" 27 static int64_t fp(int64_t n, int64_t r) { return floor_prefix(n, r); } 28 static int64_t cp(int64_t n, int64_t r, int64_t c) { return capped_prefix(n, r, c); } 29 static int64_t pr(int64_t b, int64_t e, int64_t r, int64_t c) { return pairs(b, e, r, c); } 30 }; 31 32 static const uint64_t SENTINEL = 0x5555555555555555u; 33 struct Om { 34 uint32_t st; 35 int64_t v; 36 bool kept; // result left unchanged on a trap 37 }; 38 static Om om_fp(int64_t n, int64_t r) { 39 uint64_t x = SENTINEL; 40 const uint32_t st = omega2_floor_prefix(&x, static_cast(n), static_cast(r)); 41 return {st, static_cast(x), st == 0 || x == SENTINEL}; 42 } 43 static Om om_cp(int64_t n, int64_t r, int64_t c) { 44 uint64_t x = SENTINEL; 45 const uint32_t st = omega2_capped_prefix(&x, static_cast(n), static_cast(r), static_cast(c)); 46 return {st, static_cast(x), st == 0 || x == SENTINEL}; 47 } 48 static Om om_pr(int64_t b, int64_t e, int64_t r, int64_t c) { 49 uint64_t x = SENTINEL; 50 const uint32_t st = omega2_pairs(&x, static_cast(b), static_cast(e), static_cast(r), 51 static_cast(c)); 52 return {st, static_cast(x), st == 0 || x == SENTINEL}; 53 } 54 55 // Step model: each C++ step of the upstream expression in __int128; ovf when any step leaves int64_t. 56 using i128 = __int128; 57 static bool fits(i128 x) { return x >= INT64_MIN && x <= INT64_MAX; } 58 struct M { 59 bool ovf; 60 int64_t v; 61 }; 62 static M m_fp(int64_t n, int64_t r) { 63 const i128 b = n / r, n1 = i128(n) + 1; 64 if (!fits(n1)) return {true, 0}; 65 const i128 a = b * n1; 66 if (!fits(a)) return {true, 0}; 67 const i128 rb = i128(r) * b; 68 if (!fits(rb)) return {true, 0}; 69 const i128 b1 = b + 1; 70 if (!fits(b1)) return {true, 0}; 71 const i128 t = rb * b1; 72 if (!fits(t)) return {true, 0}; 73 const i128 v = a - t / 2; 74 if (!fits(v)) return {true, 0}; 75 return {false, int64_t(v)}; 76 } 77 static M m_cp(int64_t n, int64_t r, int64_t c) { 78 const i128 rc = i128(r) * c; 79 if (!fits(rc)) return {true, 0}; 80 const int64_t stop = std::min(n, int64_t(rc)); 81 const M f = m_fp(stop, r); 82 if (f.ovf) return f; 83 const i128 d = i128(n) - stop; 84 if (!fits(d)) return {true, 0}; 85 const i128 dc = d * c; 86 if (!fits(dc)) return {true, 0}; 87 const i128 v = f.v + dc; 88 if (!fits(v)) return {true, 0}; 89 return {false, int64_t(v)}; 90 } 91 static M m_pr(int64_t b, int64_t e, int64_t r, int64_t c) { 92 const M x = c == 0 ? m_fp(e, r) : m_cp(e, r, c); 93 const M y = c == 0 ? m_fp(b, r) : m_cp(b, r, c); 94 if (x.ovf || y.ovf) return {true, 0}; 95 const i128 v = i128(x.v) - y.v; 96 if (!fits(v)) return {true, 0}; 97 return {false, int64_t(v)}; 98 } 99 // Second closed form, exact in __int128 for every n, ratio, cap in int64_t's nonnegative range. 100 static i128 alt_fp(i128 n, i128 r) { 101 const i128 b = n / r, s = n - r * b; 102 return r * b * (b - 1) / 2 + b * (s + 1); 103 } 104 static i128 alt_cp(i128 n, i128 r, i128 c) { 105 const i128 s = std::min(n, r * c); 106 return alt_fp(s, r) + (n - s) * c; 107 } 108 109 static long fails = 0; 110 static void bad(const char *what, const char *fn, const int64_t *a, int k) { 111 if (++fails <= 20) { 112 printf("MISMATCH %s %s", what, fn); 113 for (int i = 0; i < k; i++) printf(" %" PRId64, a[i]); 114 printf("\n"); 115 } 116 } 117 118 static int grid() { 119 long cases[3] = {0, 0, 0}, mis[3] = {0, 0, 0}; 120 for (int64_t r = 1; r <= 40; r++) { 121 int64_t acc = 0; 122 for (int64_t n = 0; n <= 300; n++) { 123 if (n > 0) acc += n / r; 124 const Om o = om_fp(n, r); 125 cases[0]++; 126 if (o.st != 0 || o.v != acc || Up::fp(n, r) != acc) { 127 mis[0]++; 128 const int64_t a[] = {n, r}; 129 bad("grid", "floor_prefix", a, 2); 130 } 131 } 132 for (int64_t c = 0; c <= 40; c++) { 133 acc = 0; 134 for (int64_t n = 0; n <= 300; n++) { 135 if (n > 0) acc += std::min(n / r, c); 136 const Om o = om_cp(n, r, c); 137 cases[1]++; 138 if (o.st != 0 || o.v != acc || Up::cp(n, r, c) != acc) { 139 mis[1]++; 140 const int64_t a[] = {n, r, c}; 141 bad("grid", "capped_prefix", a, 3); 142 } 143 } 144 for (int64_t b = 0; b <= 300; b++) { 145 acc = 0; // sum over t = b+1 .. e, term by term (cap 0: uncapped) 146 for (int64_t e = b; e <= 300; e++) { 147 if (e > b) acc += c == 0 ? e / r : std::min(e / r, c); 148 const Om o = om_pr(b, e, r, c); 149 cases[2]++; 150 if (o.st != 0 || o.v != acc || Up::pr(b, e, r, c) != acc) { 151 mis[2]++; 152 const int64_t a[] = {b, e, r, c}; 153 bad("grid", "pairs", a, 4); 154 } 155 } 156 } 157 } 158 } 159 const char *nm[] = {"floor_prefix", "capped_prefix", "pairs"}; 160 for (int i = 0; i < 3; i++) printf("GRID %s cases=%ld mismatches=%ld\n", nm[i], cases[i], mis[i]); 161 return mis[0] + mis[1] + mis[2] == 0 ? 0 : 1; 162 } 163 164 // A nonnegative value of random magnitude: bit length uniform in 0..63, so small and large both appear. 165 static int64_t mag(std::mt19937_64 &g, int lo) { 166 const int bits = int(g() % 64); 167 int64_t v = bits == 0 ? 0 : int64_t(g() >> (64 - bits)); 168 return std::max(v, lo); 169 } 170 171 static int random_cases(uint64_t seed, long count) { 172 std::mt19937_64 g(seed); 173 long defined[3] = {0, 0, 0}, ovf[3] = {0, 0, 0}, mis[3] = {0, 0, 0}; 174 for (long i = 0; i < count; i++) { 175 const int f = int(i % 3); 176 int64_t n = mag(g, 0), b = mag(g, 0); 177 const int64_t r = mag(g, 1), c = mag(g, 0); 178 if (b > n) std::swap(b, n); 179 M m; 180 Om o; 181 i128 alt = 0; 182 if (f == 0) { 183 m = m_fp(n, r), o = om_fp(n, r), alt = alt_fp(n, r); 184 } else if (f == 1) { 185 m = m_cp(n, r, c), o = om_cp(n, r, c), alt = alt_cp(n, r, c); 186 } else { 187 const int64_t cc = (g() & 3) == 0 ? 0 : c; // a quarter uncapped 188 m = m_pr(b, n, r, cc), o = om_pr(b, n, r, cc); 189 alt = cc == 0 ? alt_fp(n, r) - alt_fp(b, r) : alt_cp(n, r, cc) - alt_cp(b, r, cc); 190 const int64_t a[] = {b, n, r, cc}; 191 if (m.ovf) { 192 ovf[2]++; 193 if (o.st != 1 || !o.kept) mis[2]++, bad("random-ovf", "pairs", a, 4); 194 } else { 195 defined[2]++; 196 if (o.st != 0 || o.v != m.v || i128(o.v) != alt || Up::pr(b, n, r, cc) != o.v) mis[2]++, bad("random", "pairs", a, 4); 197 } 198 continue; 199 } 200 const int64_t a[] = {n, r, c}; 201 const char *nm = f == 0 ? "floor_prefix" : "capped_prefix"; 202 if (m.ovf) { 203 ovf[f]++; 204 if (o.st != 1 || !o.kept) mis[f]++, bad("random-ovf", nm, a, f == 0 ? 2 : 3); 205 } else { 206 defined[f]++; 207 const int64_t up = f == 0 ? Up::fp(n, r) : Up::cp(n, r, c); 208 if (o.st != 0 || o.v != m.v || i128(o.v) != alt || up != o.v) mis[f]++, bad("random", nm, a, f == 0 ? 2 : 3); 209 } 210 } 211 const char *nm[] = {"floor_prefix", "capped_prefix", "pairs"}; 212 for (int i = 0; i < 3; i++) 213 printf("RANDOM seed=%" PRIu64 " %s defined=%ld overflow=%ld mismatches=%ld\n", seed, nm[i], defined[i], ovf[i], mis[i]); 214 return mis[0] + mis[1] + mis[2] == 0 && defined[0] && defined[1] && defined[2] && ovf[0] && ovf[1] && ovf[2] ? 0 : 1; 215 } 216 217 int main(int argc, char **argv) { 218 if (argc >= 2 && !strcmp(argv[1], "grid")) return grid(); 219 if (argc == 4 && !strcmp(argv[1], "random")) return random_cases(strtoull(argv[2], nullptr, 10), strtol(argv[3], nullptr, 10)); 220 if (argc >= 4 && (!strcmp(argv[1], "up") || !strcmp(argv[1], "om"))) { 221 int64_t a[4] = {0, 0, 0, 0}; 222 const int k = argc - 3; 223 for (int i = 0; i < k && i < 4; i++) a[i] = strtoll(argv[3 + i], nullptr, 10); 224 const char *fn = argv[2]; 225 const bool up = argv[1][0] == 'u'; 226 if (!strcmp(fn, "fp") && k == 2) { 227 if (up) return printf("value %" PRId64 "\n", Up::fp(a[0], a[1])) < 0; 228 const Om o = om_fp(a[0], a[1]); 229 return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; 230 } 231 if (!strcmp(fn, "cp") && k == 3) { 232 if (up) return printf("value %" PRId64 "\n", Up::cp(a[0], a[1], a[2])) < 0; 233 const Om o = om_cp(a[0], a[1], a[2]); 234 return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; 235 } 236 if (!strcmp(fn, "pairs") && k == 4) { 237 if (up) return printf("value %" PRId64 "\n", Up::pr(a[0], a[1], a[2], a[3])) < 0; 238 const Om o = om_pr(a[0], a[1], a[2], a[3]); 239 return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; 240 } 241 } 242 fprintf(stderr, "usage: diff grid | random SEED N | up|om fp|cp|pairs ARG...\n"); 243 return 2; 244 }