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/patched.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 / patched.cpp` 139 lines ยท 6655 bytes Raw Blame History 1 // Standalone check of flops_estimator-omega.patch without torch. run.sh applies the patch to a copy of 2 // the pinned header and extracts the same two regions from the original and the patched header: 3 // *-types.inc struct QueryRange and struct PairTerm (whose count() calls pairs); 4 // *-fns.inc floor_prefix, capped_prefix, pairs (and the patch's omega_result) and query_ranges. 5 // Each pair of regions becomes a struct with the members query_ranges reads; TORCH_CHECK is a stub 6 // that throws std::runtime_error with the same message parts. Built -std=c++20 with GCC UBSan (the 7 // original side may not hit undefined behaviour). The patched side calls the Omega library through 8 // omega_native.h. Prints one line per section; exit 0 only when every compared result is identical. 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 18 #include "omega_native.h" 19 20 template static std::string torch_msg(const A &...a) { 21 std::ostringstream s; 22 (s << ... << a); 23 return s.str(); 24 } 25 #define TORCH_CHECK(cond, ...) \ 26 do { \ 27 if (!(cond)) throw std::runtime_error(torch_msg(__VA_ARGS__)); \ 28 } while (0) 29 30 struct Orig { 31 bool compact = false; 32 int keep_tokens = 0; 33 int window_size = 0; 34 bool has_truncated_swa = false; 35 #include "orig-types.inc" 36 #include "orig-fns.inc" 37 }; 38 struct Patched { 39 bool compact = false; 40 int keep_tokens = 0; 41 int window_size = 0; 42 bool has_truncated_swa = false; 43 #include "patched-types.inc" 44 #include "patched-fns.inc" 45 }; 46 47 static long fails = 0; 48 static void bad(const char *what, long a, long b, long c, long d) { 49 if (++fails <= 20) printf("MISMATCH %s %ld %ld %ld %ld\n", what, a, b, c, d); 50 } 51 52 // The exception message (or "" when none) and the result of f. 53 template static std::pair run(F f) { 54 try { 55 return {"", f()}; 56 } catch (const std::runtime_error &e) { 57 return {e.what(), 0}; 58 } 59 } 60 61 int main() { 62 // 1. PairTerm::count, the path calc_attn_flops uses: shift = ratio * (block - 1), block 1..4. 63 long n1 = 0; 64 for (int64_t ratio = 1; ratio <= 40; ratio++) 65 for (int cap = 0; cap <= 40; cap++) 66 for (int64_t block = 1; block <= 4; block++) 67 for (int b = 0; b <= 120; b++) 68 for (int e = b + 1; e <= 120; e++) { 69 const Orig::PairTerm ot{ratio, cap, ratio * (block - 1), 0, 0}; 70 const Patched::PairTerm pt{ratio, cap, ratio * (block - 1), 0, 0}; 71 const Orig::QueryRange oq{b, e, 0}; 72 const Patched::QueryRange pq{b, e, 0}; 73 n1++; 74 if (ot.count(oq) != pt.count(pq)) bad("PairTerm::count", ratio, cap, b, e); 75 } 76 printf("PATCH PairTerm::count cases=%ld mismatches=%ld\n", n1, fails); 77 78 // 2. query_ranges (pairs with ratio 1, cap window_size), including its TORCH_CHECK refusals. 79 long n2 = 0, refused = 0; 80 const long f1 = fails; 81 const int windows[] = {0, 1, 7, 64, 128}; 82 const int keeps[] = {0, 5, 140}; 83 for (int w : windows) 84 for (int flags = 0; flags < 4; flags++) 85 for (int keep : keeps) { 86 Orig o; 87 Patched p; 88 o.window_size = p.window_size = w; 89 o.compact = p.compact = flags & 1; 90 o.has_truncated_swa = p.has_truncated_swa = flags & 2; 91 o.keep_tokens = p.keep_tokens = keep; 92 for (int q = 0; q <= 24; q++) 93 for (int k = q; k <= 160; k += 3) 94 for (int prompt = 0; prompt < 2; prompt++) 95 for (int fl : {-1, k, k + 3, k + 200, k - 1}) 96 for (int sv = 0; sv <= k - q + 1; sv += 4) { 97 n2++; 98 const auto a = run([&] { 99 const auto [f, c] = o.query_ranges(q, k, prompt, fl, sv); 100 return f.begin * 1000003LL + f.end * 1009LL + f.swa_pairs * 7919LL + c.begin * 31LL + 101 c.end * 17LL + c.swa_pairs; 102 }); 103 const auto b = run([&] { 104 const auto [f, c] = p.query_ranges(q, k, prompt, fl, sv); 105 return f.begin * 1000003LL + f.end * 1009LL + f.swa_pairs * 7919LL + c.begin * 31LL + 106 c.end * 17LL + c.swa_pairs; 107 }); 108 if (!a.first.empty()) refused++; 109 if (a != b) bad("query_ranges", q, k, fl, sv); 110 } 111 } 112 printf("PATCH query_ranges cases=%ld refused-by-TORCH_CHECK=%ld mismatches=%ld\n", n2, refused, fails - f1); 113 114 // 3. Omega traps reach TORCH_CHECK. The original has no refusal: these inputs are outside its 115 // domain (ratio 0 divides by zero; `3037000500` overflows a step), so only the patched side runs. 116 struct T { 117 const char *name; 118 std::pair got; 119 const char *want; 120 }; 121 const T traps[] = { 122 {"pairs(5, 3, 1, 0)", run([] { return Patched::pairs(5, 3, 1, 0); }), "pairs: Omega trap 9"}, 123 {"pairs(0, 3, 1, -1)", run([] { return Patched::pairs(0, 3, 1, -1); }), "pairs: Omega trap 9"}, 124 {"floor_prefix(5, 0)", run([] { return Patched::floor_prefix(5, 0); }), "floor_prefix: Omega trap 9"}, 125 {"floor_prefix(`3037000500`, 1)", run([] { return Patched::floor_prefix(`3037000500`, 1); }), "floor_prefix: Omega trap 1"}, 126 {"capped_prefix(0, `4294967296`, `2147483648`)", run([] { return Patched::capped_prefix(0, 4294967296LL, 2147483648LL); }), 127 "capped_prefix: Omega trap 1"}, 128 }; 129 long n3 = 0; 130 const long f2 = fails; 131 for (const T &t : traps) { 132 n3++; 133 printf("PATCH trap %s -> \"%s\"\n", t.name, t.got.first.c_str()); 134 if (t.got.first != t.want) bad(t.name, 0, 0, 0, 0); 135 } 136 printf("PATCH traps cases=%ld mismatches=%ld\n", n3, fails - f2); 137 printf("PATCH total mismatches=%ld\n", fails); 138 return fails == 0 ? 0 : 1; 139 }