diff --git a/README.md b/README.md index 09e3fd7..8cd8a48 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ OMEGA_NATIVE_OMC= tools/native build/native tests/native/madd.om That writes `libomega_native.so` and, from the same Omega signatures, `omega_native.h` (C and C++), `omega_native.py`, `omega_native.rs`, `omega_native.zig` and `omega_native.go`; a program includes or imports its file and calls `omega_madd`, nothing is declared by hand. One generator, [src/native.om](src/native.om), writes all six. Contract, limits and the qualification run: [native call ABI](docs/NATIVE-ABI.md). +A worked integration: [examples/deepinfer](examples/deepinfer/README.md) replaces DeepInfer's `FlopsEstimator` pair-count helpers with an OM-2 unit called through these bindings, differential-tested against the pinned upstream code. + `f32` and `ushort` array kinds (4-byte and 2-byte elements) are qualified by `OMEGA_NATIVE_OMC= sh tests/native/kinds.sh`. The contract is in [native call ABI](docs/NATIVE-ABI.md). The native target defaults to `aarch64linux`. Hosted x86-64 SysV Linux bindings diff --git a/evidence/deepinfer-flops-pairs/.crumb b/evidence/deepinfer-flops-pairs/.crumb new file mode 100644 index 0000000..ff94d90 --- /dev/null +++ b/evidence/deepinfer-flops-pairs/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-31T00:23:54Z", + "compiler": "crumb-compile/1", + "digest": "sha256:579eb72ebd1a11bcd3304f4b4a896f8e52dfa65e8b193feaeaea469d24844050", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 0, + "generated_at_commit": "1958a1895279c0d4869bab39eaa20f070b174a6c", + "source_tree": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + "layer": "evidence", + "name": "deepinfer-flops-evidence", + "purpose": "Run log, overflow measurement, Wolfram checks and hashes for examples/deepinfer", + "schema_version": "1.0.0" +} diff --git a/evidence/deepinfer-flops-pairs/files.sha256 b/evidence/deepinfer-flops-pairs/files.sha256 new file mode 100644 index 0000000..6211645 --- /dev/null +++ b/evidence/deepinfer-flops-pairs/files.sha256 @@ -0,0 +1,24 @@ +b65d98b7af2aa584bb2f4fbb86b3cde210a74346ab5e38913ec4985a0782c17a examples/deepinfer/README.md +a9d6c99adf4b78a9f4b40e7e6d7146fa3beae32602183511378b8f5be9e1515e examples/deepinfer/boundary.txt +86a4ffde0d0c8322fd8257cf5c9d17c5356a65991f753faf0cd6f59619a6f57c examples/deepinfer/diff.cpp +5138b58ac198c10baa5048fcd5eaad7a6d5092111b3ccc63f0a0f5a81416532f examples/deepinfer/diff.py +bf46c87adea5833c3fb4caf775c415d8c0d7e44892bcdc1b169aaed15fa43842 examples/deepinfer/flops_estimator-omega.patch +95965d57d441cc3d2a78b666e2eb52beac0b0bd97893dcc1564c9f39cec4a55f examples/deepinfer/flops_pairs.om +33ea19e64a0bbda6994f6e15d04ec9edc3ca93a7c995ed9bb72a09429e6b7f97 examples/deepinfer/overflow.sh +8ef76345a17d10ed519ae6bd303de78b0eea386b21ba5e5fe9273a4d97266e6f examples/deepinfer/patched.cpp +22bd7f9893ba002036bb8d8f6fdfbe10eb82df50e53e31050b95b524c31ed24f examples/deepinfer/run.sh +4acb46c3c2c1965e249bd8fc03f576805bb8b055a3a979d38aa099518832f52a examples/deepinfer/callers/caller.c +b7b125a6d3ea03b29747d2be21eaae25d6fb206e5aa86bf21b0aeea5b69a8634 examples/deepinfer/callers/caller.cpp +929bf487b40de8c999bc7bd4f3522cc7f2cd9605ea05ebfd2316b9a335eb8322 examples/deepinfer/callers/caller.go +3ae5b31a0ee3d9a7f4440e5206c920994f551dcd4c79a0bab792808945e5819f examples/deepinfer/callers/caller.py +387da0472939a7acaffdfe6217a675947bac78d05926ee7f957ca92bf9ea07dc examples/deepinfer/callers/caller.rs +436342dce798285862e78f4b6ba8a59374e06f0fece21d8e6c3acb2ca5e3d308 examples/deepinfer/callers/caller.zig +1226e74bdad044415a9d30ef887145f20f07348cbe95710bfc8aa560fe63a0c5 build/deepinfer/libomega_native.so +3267c35004c8e87aa6e8f1a31633134d68eaa7b6d51c9560a14c828d26f54623 build/deepinfer/omega_native.c +870464dddb6552c96a2197e5913fbcba2ea16ba7150053aa55a41e3a7c372d39 build/deepinfer/omega_native.go +30e5474cfe0fb66822e20fca64bd8efb015ab52fbfe5994711c64c1e28f0600f build/deepinfer/omega_native.h +37ba27aab504aaf16ed21032dfff28aaf8e02686965bda1d94db8cb4436a0aee build/deepinfer/omega_native.py +4c0d468e9b9bdb0a604839c30074c75cf52e3b471991f9cb5973bd8621f2e9b8 build/deepinfer/omega_native.rs +06054f85a253f8f53e12f9b165b2266f851a8ab88e8bc3acee39132d380e0e76 build/deepinfer/omega_native.zig +95b16b3dafffb46ac3a3f57f41d9d296436f905e6d0ce9814e9a5ace8cb1c611 build/deepinfer/upstream.inc +9bb948f30bd984c1adb6afb7fe624c05c1fbf9fbfd7c12239f39617fce5d2b75 build/deepinfer/patch/b/flops_estimator.hpp diff --git a/evidence/deepinfer-flops-pairs/overflow-measurement.log b/evidence/deepinfer-flops-pairs/overflow-measurement.log new file mode 100644 index 0000000..4892dae --- /dev/null +++ b/evidence/deepinfer-flops-pairs/overflow-measurement.log @@ -0,0 +1,42 @@ +INFO compiler /tmp/omc-734f0a39 sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 +734f0a39 OM-1 add 2^63-1 + 1: 101 (trap: 101) +734f0a39 OM-1 sub -(2^63-1) - 2: 101 (trap: 101) +734f0a39 OM-1 mul 2^62 * 2: 101 (trap: 101) +734f0a39 OM-1 mul 3037000500 * 3037000500: 101 (trap: 101) +734f0a39 OM-1 control add 2^63-1 + 0: 0 (0 = Mod[2^63-1,7]) +734f0a39 OM-1 control mul (2^62-1) * 2: 6 (6 = Mod[2^63-2,7]) +734f0a39 OM-1 control mul 3037000499 * 3037000500: 2 (2) +734f0a39 OM-1 mul through a function's parameters 3037000500^2: 101 (trap: 101) +734f0a39 OM-1 requires ge x 0 called with -5: 252 (trap 9: 109; a return of -4 is int -4 = 252) +734f0a39 OM-1 parameter named end: refused: omc: line 1: error 16 (refused) +734f0a39 OM-2 add 2^63-1 + 1: refused: omc: line 1: error 16 (trap: 101) +734f0a39 OM-2 sub -(2^63-1) - 2: refused: omc: line 1: error 16 (trap: 101) +734f0a39 OM-2 mul 2^62 * 2: refused: omc: line 1: error 16 (trap: 101) +734f0a39 OM-2 mul 3037000500 * 3037000500: refused: omc: line 1: error 16 (trap: 101) +734f0a39 OM-2 control add 2^63-1 + 0: refused: omc: line 1: error 16 (0 = Mod[2^63-1,7]) +734f0a39 OM-2 control mul (2^62-1) * 2: refused: omc: line 1: error 16 (6 = Mod[2^63-2,7]) +734f0a39 OM-2 control mul 3037000499 * 3037000500: refused: omc: line 1: error 16 (2) +734f0a39 OM-2 mul through a function's parameters 3037000500^2: refused: omc: line 1: error 16 (trap: 101) +734f0a39 OM-2 requires ge x 0 called with -5: refused: omc: line 1: error 16 (trap 9: 109; a return of -4 is int -4 = 252) +734f0a39 OM-2 parameter named end: refused: omc: line 2: error 16 (refused) +INFO compiler ~/workspace/kflow/seed/out/omc sha256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f +2c824805 OM-1 add 2^63-1 + 1: 101 (trap: 101) +2c824805 OM-1 sub -(2^63-1) - 2: 101 (trap: 101) +2c824805 OM-1 mul 2^62 * 2: 101 (trap: 101) +2c824805 OM-1 mul 3037000500 * 3037000500: 101 (trap: 101) +2c824805 OM-1 control add 2^63-1 + 0: 0 (0 = Mod[2^63-1,7]) +2c824805 OM-1 control mul (2^62-1) * 2: 6 (6 = Mod[2^63-2,7]) +2c824805 OM-1 control mul 3037000499 * 3037000500: 2 (2) +2c824805 OM-1 mul through a function's parameters 3037000500^2: 101 (trap: 101) +2c824805 OM-1 requires ge x 0 called with -5: 252 (trap 9: 109; a return of -4 is int -4 = 252) +2c824805 OM-1 parameter named end: refused: omc: line 1: error 16 (refused) +2c824805 OM-2 add 2^63-1 + 1: 101 (trap: 101) +2c824805 OM-2 sub -(2^63-1) - 2: 101 (trap: 101) +2c824805 OM-2 mul 2^62 * 2: 101 (trap: 101) +2c824805 OM-2 mul 3037000500 * 3037000500: 101 (trap: 101) +2c824805 OM-2 control add 2^63-1 + 0: 0 (0 = Mod[2^63-1,7]) +2c824805 OM-2 control mul (2^62-1) * 2: 6 (6 = Mod[2^63-2,7]) +2c824805 OM-2 control mul 3037000499 * 3037000500: 2 (2) +2c824805 OM-2 mul through a function's parameters 3037000500^2: 101 (trap: 101) +2c824805 OM-2 requires ge x 0 called with -5: 109 (trap 9: 109; a return of -4 is int -4 = 252) +2c824805 OM-2 parameter named end: refused: omc: line 2: error 16 (refused) diff --git a/evidence/deepinfer-flops-pairs/run.log b/evidence/deepinfer-flops-pairs/run.log new file mode 100644 index 0000000..ba2a608 --- /dev/null +++ b/evidence/deepinfer-flops-pairs/run.log @@ -0,0 +1,78 @@ +INFO DeepInfer 553741557a57df44a46fb3ab72b6aafeaf945337 csrc/engine/flops_estimator.hpp sha256 89fbefa41ef0f1ee79be19bb92700d446de38c3781d18227bb0044eb20e7b4f2; lines 237-257 sha256 95b16b3dafffb46ac3a3f57f41d9d296436f905e6d0ce9814e9a5ace8cb1c611 +INFO omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61; OM-2 compiler sha256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f; flops_pairs.om sha256 95965d57d441cc3d2a78b666e2eb52beac0b0bd97893dcc1564c9f39cec4a55f +INFO g++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +PASS tools/native: flops_pairs.om (OM-2) -> libomega_native.so sha256 1226e74bdad044415a9d30ef887145f20f07348cbe95710bfc8aa560fe63a0c5 and six bindings +INFO omega-ident 1 unit=flops_pairs abi=2 profile=OM-2 src=95965d57d441cc3d2a78b666e2eb52beac0b0bd97893dcc1564c9f39cec4a55f omc=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f +PASS C++ grid (upstream = Omega = enumeration) + GRID floor_prefix cases=12040 mismatches=0 + GRID capped_prefix cases=493640 mismatches=0 + GRID pairs cases=74539640 mismatches=0 +PASS C++ random (upstream = Omega = step model = second closed form; overflow -> trap 1) + RANDOM seed=20261031 floor_prefix defined=198088 overflow=135246 mismatches=0 + RANDOM seed=20261031 capped_prefix defined=112770 overflow=220563 mismatches=0 + RANDOM seed=20261031 pairs defined=131671 overflow=201662 mismatches=0 + BOUNDARY ok fp 3037000499 1 | want 4611686016981624750 | omega: status 0 value 4611686016981624750 kept 1 | upstream: value 4611686016981624750 + BOUNDARY ok fp 3037000500 1 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 3037000500 * 3037000501 cannot be represented in type 'long int' + BOUNDARY ok fp 9223372036854775807 1 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long int' + BOUNDARY ok fp 9223372036854775807 9223372036854775807 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 9223372036854775807 * 2 cannot be represented in type 'long int' + BOUNDARY ok fp 0 9223372036854775807 | want 0 | omega: status 0 value 0 kept 1 | upstream: value 0 + BOUNDARY ok fp 9 2 | want 20 | omega: status 0 value 20 kept 1 | upstream: value 20 + BOUNDARY ok cp 4611686018427387904 1 2 | want 9223372036854775807 | omega: status 0 value 9223372036854775807 kept 1 | upstream: value 9223372036854775807 + BOUNDARY ok cp 4611686018427387905 1 2 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 9223372036854775806 + 3 cannot be represented in type 'long int' + BOUNDARY ok cp 0 4294967296 2147483648 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 4294967296 * 2147483648 cannot be represented in type 'long int' + BOUNDARY ok cp 5 4294967296 2147483647 | want 0 | omega: status 0 value 0 kept 1 | upstream: value 0 + BOUNDARY ok cp 9223372036854775807 1 1 | want 9223372036854775807 | omega: status 0 value 9223372036854775807 kept 1 | upstream: value 9223372036854775807 + BOUNDARY ok cp 9 2 3 | want 18 | omega: status 0 value 18 kept 1 | upstream: value 18 + BOUNDARY ok pairs 0 3037000499 1 0 | want 4611686016981624750 | omega: status 0 value 4611686016981624750 kept 1 | upstream: value 4611686016981624750 + BOUNDARY ok pairs 0 3037000500 1 0 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 3037000500 * 3037000501 cannot be represented in type 'long int' + BOUNDARY ok pairs 3037000499 3037000499 1 0 | want 0 | omega: status 0 value 0 kept 1 | upstream: value 0 + BOUNDARY ok pairs 0 9223372036854775807 1 1 | want 9223372036854775807 | omega: status 0 value 9223372036854775807 kept 1 | upstream: value 9223372036854775807 + BOUNDARY ok pairs 1 4611686018427387905 1 2 | want ovf | omega: status 1 value 0 kept 1 | upstream: runtime error: signed integer overflow: 9223372036854775806 + 3 cannot be represented in type 'long int' + BOUNDARY ok pairs 3 9 2 3 | want 16 | omega: status 0 value 16 kept 1 | upstream: value 16 + BOUNDARY ok pairs 0 4096 1 512 | want 1966336 | omega: status 0 value 1966336 kept 1 | upstream: value 1966336 + BOUNDARY ok fp -1 1 | want dom | omega: status 9 value 0 kept 1 | upstream: value 0 + BOUNDARY ok fp 5 0 | want dom | omega: status 9 value 0 kept 1 | upstream: runtime error: division by zero + BOUNDARY ok fp 5 -1 | want dom | omega: status 9 value 0 kept 1 | upstream: value -20 + BOUNDARY ok fp -9223372036854775808 -1 | want dom | omega: status 9 value 0 kept 1 | upstream: runtime error: division of -9223372036854775808 by -1 cannot be represented in type 'long int' + BOUNDARY ok cp -1 1 1 | want dom | omega: status 9 value 0 kept 1 | upstream: value 0 + BOUNDARY ok cp 5 0 3 | want dom | omega: status 9 value 0 kept 1 | upstream: runtime error: division by zero + BOUNDARY ok cp 5 1 -1 | want dom | omega: status 9 value 0 kept 1 | upstream: value -6 + BOUNDARY ok pairs -1 3 1 0 | want dom | omega: status 9 value 0 kept 1 | upstream: value 6 + BOUNDARY ok pairs 5 3 1 0 | want dom | omega: status 9 value 0 kept 1 | upstream: value -9 + BOUNDARY ok pairs 0 3 0 0 | want dom | omega: status 9 value 0 kept 1 | upstream: runtime error: division by zero + BOUNDARY ok pairs 0 3 1 -1 | want dom | omega: status 9 value 0 kept 1 | upstream: value -3 +PASS boundary: 30 cases (values equal; ovf: Omega trap 1 and UBSan signed integer overflow; dom: Omega trap 9) +PASS Python binding differential + PY-IDENT omega-ident 1 unit=flops_pairs abi=2 profile=OM-2 src=95965d57d441cc3d2a78b666e2eb52beac0b0bd97893dcc1564c9f39cec4a55f omc=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f + PY-GRID floor_prefix n 0..300 ratio 1..40 cases=12040; capped_prefix cap 0..40 cases=493640; pairs begin<=end 0..300 cases=74539640; mismatches=0 + PY-RANDOM seed=20261031 defined=88464 overflow=111536 mismatches=0 + PY-BOUNDARY cases=30 mismatches=0 + PY-TOTAL mismatches=0 +PASS flops_estimator-omega.patch applies to the pinned header (patched sha256 9bb948f30bd984c1adb6afb7fe624c05c1fbf9fbfd7c12239f39617fce5d2b75) +INFO extracted regions: 15+42 original lines, 15+53 patched lines +PASS patched header = original on PairTerm::count and query_ranges; Omega traps reach TORCH_CHECK + PATCH PairTerm::count cases=47625600 mismatches=0 + PATCH query_ranges cases=14473800 refused-by-TORCH_CHECK=2215800 mismatches=0 + PATCH trap pairs(5, 3, 1, 0) -> "pairs: Omega trap 9" + PATCH trap pairs(0, 3, 1, -1) -> "pairs: Omega trap 9" + PATCH trap floor_prefix(5, 0) -> "floor_prefix: Omega trap 9" + PATCH trap floor_prefix(3037000500, 1) -> "floor_prefix: Omega trap 1" + PATCH trap capped_prefix(0, 4294967296, 2147483648) -> "capped_prefix: Omega trap 1" + PATCH traps cases=5 mismatches=0 + PATCH total mismatches=0 +PASS whole original header compiles against torch headers (~/.venv/lib/python3.12/site-packages/torch/include) +PASS whole patched header compiles against torch headers (~/.venv/lib/python3.12/site-packages/torch/include) +PASS mutant M1 caught: grid mismatches=56293075 boundary failures=11 patch-harness mismatches=39773816 +PASS mutant M2 caught: grid mismatches=72607516 boundary failures=7 patch-harness mismatches=NOT_RUN +PASS mutant M3 caught: grid mismatches=0 boundary failures=2 patch-harness mismatches=NOT_RUN +PASS no caller declares an Omega function by hand +PASS c caller (16 checks) +PASS c-bti caller (16 checks) +PASS cpp caller (16 checks) +PASS python caller (17 checks) +PASS rust caller (16 checks) +PASS zig caller (16 checks) +PASS go caller (16 checks) +NATIVE-CALL PASS (c c-bti cpp python rust zig go) +RESULT failures=0 NOT_RUN: none +DEEPINFER-FLOPS PASS diff --git a/evidence/deepinfer-flops-pairs/toolchain.txt b/evidence/deepinfer-flops-pairs/toolchain.txt new file mode 100644 index 0000000..4fcbbdc --- /dev/null +++ b/evidence/deepinfer-flops-pairs/toolchain.txt @@ -0,0 +1,8 @@ +g++: g++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +cc: cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +python3: Python 3.12.3 +rustc: rustc 1.98.1 (48a229cea 2026-09-01) +zig: 0.13.0 +go: go version go1.27.3 linux/arm64 +torch headers: ~/.venv torch 2.14.0+cu130 +uname: Linux 7.0.0-1019-nvidia aarch64 diff --git a/evidence/deepinfer-flops-pairs/wolfram.md b/evidence/deepinfer-flops-pairs/wolfram.md new file mode 100644 index 0000000..42b24a0 --- /dev/null +++ b/evidence/deepinfer-flops-pairs/wolfram.md @@ -0,0 +1,53 @@ +# Wolfram checks for examples/deepinfer + +Every identity, bound and expected value used by `examples/deepinfer` (flops_pairs.om, boundary.txt, +the callers) came from the Wolfram Language evaluator (MCP `WolframLanguageEvaluator`), 2026-10-31. +Each line is `query -> result`. Notation: n = ratio*b + s with b = Quotient[n, ratio], 0 <= s < ratio; +m = 2^63 - 1. + +## Closed forms (the upstream code is a closed form of a sum) + +- Sum of floor(t/r) over t = 1..n, split into whole blocks and the partial block: + `Simplify[Sum[j r,{j,0,b-1}]+b (s+1) == b (b r+s+1)-r b (b+1)/2]` -> `True` + (the left sum simplifies to `((-1 + b)*b*r)/2 + b*(1 + s)`, the second closed form diff.cpp and + diff.py compare against). +- floor_prefix equals the enumeration on the test grid, with fp[n,r] = b (n+1) - r b (b+1)/2: + `AllTrue[Table[fp[n,r]==Sum[Floor[t/r],{t,1,n}],{n,0,300},{r,1,40}]]` -> `True` +- The `/ 2` is exact: `Resolve[ForAll[b, b∈Integers, Mod[b (b+1),2]==0]]` -> `True` +- capped_prefix: for t >= r*c, floor(t/r) >= c, and for t < r*c, floor(t/r) <= c, so + sum min(floor(t/r), c) = floor_prefix(min(n, r c), r) + (n - min(n, r c)) c: + `Simplify[Floor[(r c+d)/r]>=c, r>0&&c>=0&&d>=1&&integers]` -> `True`; + `Simplify[Floor[(r c-d)/r]<=c, r>0&&c>=0&&d>=0&&integers]` -> `True`; + the capped form against an `Accumulate` of min(floor(t/r), c) over n 0..300, r 1..40, c 0..40 + (493640 cases) -> `True`. +- pairs(begin, end) = prefix(end) - prefix(begin) is the sum over t = begin+1..end by definition of + the prefix sum (no query needed). + +## Overflow boundaries (where an upstream step leaves int64_t) + +- `Floor[(Sqrt[4 m+1]-1)/2]` -> `3037000499` (n0, largest n with n (n+1) <= m); + `n0 (n0+1) <= m` -> `True`; `(n0+1)(n0+2) > m` -> `True`; `n0 (n0+1)/2` -> `4611686016981624750`. +- `Sum[t,{t,1,3037000500}] <= m` -> `True` and `3037000500*3037000501 > m` -> `True`: the true + floor_prefix(3037000500, 1) fits in int64_t but the upstream step `blocks * (n + 1)` overflows; + Omega traps 1 at the same step (boundary `fp 3037000500 1 | ovf`). +- `2^32*2^31 > m` -> `True` (capped_prefix `ratio * cap` overflows at ratio 2^32, cap 2^31); + `4294967296*2147483648 > 2^63-1` -> `True`; `2^32*2147483647 <= m` -> `True`. +- `2^62` -> `4611686018427387904`; `(2^62-2)*2+3 == m` -> `True`; `(2^62-1)*2 <= m` -> `True`; + `(2^62-1)*2+3 > m` -> `True`; `Sum[Min[t,2],{t,1,2^62+1}] > m` -> `True`. + +## Expected values (boundary.txt and callers) + +- `Sum[Floor[t/2],{t,1,9}]` -> `20` (fp 9 2) +- `Sum[Min[Floor[t/2],3],{t,1,9}]` -> `18` (cp 9 2 3) +- `Sum[Min[Floor[t/2],3],{t,4,9}]` -> `16` (pairs 3 9 2 3); `Sum[Min[Floor[t/2],3],{t,1,3}]` -> `2` +- `Sum[Floor[t/2],{t,4,9}]` -> `18`, and `Sum[Floor[t/2],{t,1,9}]-Sum[Floor[t/2],{t,1,3}]` -> `18` (pairs 3 9 2 0) +- `Sum[Min[t,512],{t,1,4096}]` -> `1966336` (pairs 0 4096 1 512: a 512-token SWA window over 4096 queries) +- `Sum[t,{t,1,3037000499}]` -> `4611686016981624750`; `Sum[Floor[t/1],{t,1,3037000499}]` -> `4611686016981624750` +- `Sum[Min[t,2],{t,1,2^62}]` -> `9223372036854775807` (cp 2^62 1 2 = m exactly) +- `Sum[Min[t,1],{t,1,2^63-1}]==2^63-1` -> `True` (cp m 1 1, pairs 0 m 1 1) +- `Sum[Min[Floor[t/2^32],2147483647],{t,1,5}]` -> `0`; `Sum[Min[Floor[t/4294967296],2147483647],{t,1,5}]` -> `0` + +## Overflow measurement controls (overflow-measurement.log) + +- `Mod[2^63-1,7]` -> `0`; `Mod[2^63-2,7]` -> `6`; `Mod[3037000499*3037000500,7]` -> `2`; + `3037000500^2 > 2^63-1` -> `True`. diff --git a/examples/deepinfer/.crumb b/examples/deepinfer/.crumb new file mode 100644 index 0000000..be6a45a --- /dev/null +++ b/examples/deepinfer/.crumb @@ -0,0 +1,27 @@ +{ + "extensions": { + "generated": { + "children": [ + { + "digest": "0930c059d4792c524ead954ccfa16e3dab282f13db861da3cd00a446883eac4f", + "last_commit": "", + "name": "callers", + "purpose": "c, c-bti, cpp, python, rust, zig and go callers of flops_pairs.om through the generated bindings" + } + ], + "children_root": "sha256:55b6c520fea9b88868f31919ffa3761fe965588bf9f0f006a9e52928ab6d2a4a", + "compiled_at": "2026-10-31T00:23:54Z", + "compiler": "crumb-compile/1", + "digest": "sha256:d0a686f1689207de43dda23ac1769a106851e542d55b240b3512ed5b0b8294d3", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 0, + "generated_at_commit": "1958a1895279c0d4869bab39eaa20f070b174a6c", + "source_tree": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + "layer": "examples", + "name": "deepinfer-flops-pairs", + "purpose": "DeepInfer FlopsEstimator floor_prefix/capped_prefix/pairs as an OM-2 Omega unit: differential harness, header patch, native callers", + "schema_version": "1.0.0" +} diff --git a/examples/deepinfer/NOTICE b/examples/deepinfer/NOTICE new file mode 100644 index 0000000..1e16d82 --- /dev/null +++ b/examples/deepinfer/NOTICE @@ -0,0 +1,11 @@ +Third-party material in this directory + +flops_estimator-omega.patch is a modification of csrc/engine/flops_estimator.hpp +from deepseek-ai/DeepInfer (https://github.com/deepseek-ai/DeepInfer), commit +553741557a57df44a46fb3ab72b6aafeaf945337, licensed under the Apache License, +Version 2.0 (copy in LICENSE at the root of this repository). Its context lines +are upstream text. Its added lines replace the pair-count arithmetic with calls +to the Omega unit in flops_pairs.om. Upstream ships no NOTICE file at that commit. + +run.sh and diff.cpp read the pinned upstream header at test time (DEEPINFER_SRC) +and check its sha256; no other upstream file is copied into this repository. diff --git a/examples/deepinfer/README.md b/examples/deepinfer/README.md new file mode 100644 index 0000000..9ebb6d4 --- /dev/null +++ b/examples/deepinfer/README.md @@ -0,0 +1,39 @@ +# DeepInfer FlopsEstimator pair counts in Omega + +`flops_pairs.om` is `FlopsEstimator::floor_prefix`, `capped_prefix` and `pairs` from +[deepseek-ai/DeepInfer](https://github.com/deepseek-ai/DeepInfer) `553741557a57df44a46fb3ab72b6aafeaf945337` +`csrc/engine/flops_estimator.hpp` lines 237-257 (Apache-2.0), as an OM-2 unit bound by `tools/native` +into `omega2_floor_prefix`, `omega2_capped_prefix` and `omega2_pairs`. + +- Each step is the C++ expression's step in the same order, so a step that leaves `long` traps 1 + exactly where the C++ is undefined behaviour (the pinned compilers trap on long add, sub and mul: + `overflow.sh`, `evidence/deepinfer-flops-pairs/overflow-measurement.log`). +- Outside n >= 0, ratio > 0, cap >= 0, 0 <= begin <= end a `requires` clause traps 9. OM-2 is used + because the pinned compilers do not check an OM-1 `requires` (same log). +- The C++ parameter `end` is `finish`: `end` is an Omega keyword (error 16). + +Files: + +| File | Role | +|---|---| +| `flops_pairs.om` | the unit | +| `diff.cpp` | upstream bytes (extracted, sha256-checked, GCC UBSan) vs Omega vs enumeration / step model / second closed form | +| `diff.py` | the generated Python binding vs Python oracles | +| `boundary.txt` | overflow and domain edges with Wolfram-checked values | +| `flops_estimator-omega.patch` | the pinned header with the three bodies calling `omega_native.h`; a trap becomes `TORCH_CHECK` | +| `patched.cpp` | original vs patched header on `PairTerm::count` and `query_ranges`, stub `TORCH_CHECK` | +| `callers/` | c (also BTI-enforced), cpp, python, rust, zig and go callers using only the generated bindings | +| `run.sh` | everything above plus three mutants that must be caught | +| `overflow.sh` | long overflow, `requires` and `end` probes per compiler | + +Run (host only, no GPU): + +```sh +DEEPINFER_SRC= OMEGA_NATIVE_OMC= \ +OMEGA_NATIVE_OMC2= [TORCH_INC=] sh examples/deepinfer/run.sh +``` + +Applying the patch to DeepInfer also needs `omega_native.h` on its include path and +`libomega_native.so` linked into `deepinfer._C`; that build and the upstream pytest +`tests/engine/test_flops_estimator.py` (needs the model config) were not run. Evidence: +`evidence/deepinfer-flops-pairs/`. diff --git a/examples/deepinfer/boundary.txt b/examples/deepinfer/boundary.txt new file mode 100644 index 0000000..621aefe --- /dev/null +++ b/examples/deepinfer/boundary.txt @@ -0,0 +1,34 @@ +# Boundary cases for examples/deepinfer/run.sh, read by diff.cpp, diff.py and run.sh. +# fn arg... | expect: a value, ovf (a C++ step leaves int64_t: Omega traps 1, UBSan reports) or dom +# (outside n >= 0, ratio > 0, cap >= 0, 0 <= begin <= end: Omega traps 9). The values come from the +# Wolfram queries in evidence/deepinfer-flops-pairs/wolfram.md. +fp 3037000499 1 | 4611686016981624750 +fp 3037000500 1 | ovf +fp 9223372036854775807 1 | ovf +fp 9223372036854775807 9223372036854775807 | ovf +fp 0 9223372036854775807 | 0 +fp 9 2 | 20 +cp 4611686018427387904 1 2 | 9223372036854775807 +cp 4611686018427387905 1 2 | ovf +cp 0 4294967296 2147483648 | ovf +cp 5 4294967296 2147483647 | 0 +cp 9223372036854775807 1 1 | 9223372036854775807 +cp 9 2 3 | 18 +pairs 0 3037000499 1 0 | 4611686016981624750 +pairs 0 3037000500 1 0 | ovf +pairs 3037000499 3037000499 1 0 | 0 +pairs 0 9223372036854775807 1 1 | 9223372036854775807 +pairs 1 4611686018427387905 1 2 | ovf +pairs 3 9 2 3 | 16 +pairs 0 4096 1 512 | 1966336 +fp -1 1 | dom +fp 5 0 | dom +fp 5 -1 | dom +fp -9223372036854775808 -1 | dom +cp -1 1 1 | dom +cp 5 0 3 | dom +cp 5 1 -1 | dom +pairs -1 3 1 0 | dom +pairs 5 3 1 0 | dom +pairs 0 3 0 0 | dom +pairs 0 3 1 -1 | dom diff --git a/examples/deepinfer/callers/.crumb b/examples/deepinfer/callers/.crumb new file mode 100644 index 0000000..1d6198e --- /dev/null +++ b/examples/deepinfer/callers/.crumb @@ -0,0 +1,20 @@ +{ + "extensions": { + "generated": { + "children": [], + "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "compiled_at": "2026-10-31T00:23:54Z", + "compiler": "crumb-compile/1", + "digest": "sha256:0930c059d4792c524ead954ccfa16e3dab282f13db861da3cd00a446883eac4f", + "evidence_files": 0, + "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "files": 0, + "generated_at_commit": "1958a1895279c0d4869bab39eaa20f070b174a6c", + "source_tree": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + "layer": "examples", + "name": "deepinfer-flops-callers", + "purpose": "c, c-bti, cpp, python, rust, zig and go callers of flops_pairs.om through the generated bindings", + "schema_version": "1.0.0" +} diff --git a/examples/deepinfer/callers/caller.c b/examples/deepinfer/callers/caller.c new file mode 100644 index 0000000..0bbe5f5 --- /dev/null +++ b/examples/deepinfer/callers/caller.c @@ -0,0 +1,63 @@ +/* C caller for examples/deepinfer/flops_pairs.om (run.sh builds it plain and with BTI enforced). + * Every Omega declaration comes from the generated omega_native.h; nothing is typed by hand here. + * Expected values: evidence/deepinfer-flops-pairs/wolfram.md. */ +#include +#include +#include + +#include "omega_native.h" + +static int fails; +static void check(const char *name, int ok) { + printf("%s %s\n", ok ? "PASS" : "FAIL", name); + if (!ok) fails++; +} + +enum { FP, CP, PR }; +struct v { + int fn; + int64_t a[4]; + uint32_t status; + int64_t want; +}; + +static const struct v vs[] = { + {FP, {9, 2}, 0, 20}, + {CP, {9, 2, 3}, 0, 18}, + {PR, {3, 9, 2, 3}, 0, 16}, + {PR, {3, 9, 2, 0}, 0, 18}, + {PR, {0, 4096, 1, 512}, 0, 1966336}, + {FP, {3037000499, 1}, 0, 4611686016981624750}, + {CP, {4611686018427387904, 1, 2}, 0, 9223372036854775807}, + {FP, {3037000500, 1}, 1, 0}, + {CP, {4611686018427387905, 1, 2}, 1, 0}, + {CP, {0, 4294967296, 2147483648}, 1, 0}, + {FP, {-1, 1}, 9, 0}, + {FP, {5, 0}, 9, 0}, + {PR, {5, 3, 1, 0}, 9, 0}, + {PR, {0, 3, 1, -1}, 9, 0}, +}; + +static uint32_t call(const struct v *x, uint64_t *r) { + const uint64_t *u = (const uint64_t *)x->a; + if (x->fn == FP) return omega2_floor_prefix(r, u[0], u[1]); + if (x->fn == CP) return omega2_capped_prefix(r, u[0], u[1], u[2]); + return omega2_pairs(r, u[0], u[1], u[2], u[3]); +} + +int main(void) { + static const char *names[] = {"floor_prefix", "capped_prefix", "pairs"}; + const uint64_t sentinel = 0x5a5a5a5a5a5a5a5aull; + check("ident: library == header", strcmp(omega2_ident_flops_pairs(), OMEGA2_IDENT_flops_pairs) == 0); + for (size_t i = 0; i < sizeof vs / sizeof vs[0]; i++) { + uint64_t r = sentinel; + const uint32_t st = call(&vs[i], &r); + const int ok = st == vs[i].status && (st == 0 ? (int64_t)r == vs[i].want : r == sentinel); + char name[160]; + snprintf(name, sizeof name, "%s %lld %lld %lld %lld -> status %u", names[vs[i].fn], (long long)vs[i].a[0], + (long long)vs[i].a[1], (long long)vs[i].a[2], (long long)vs[i].a[3], st); + check(name, ok); + } + check("null result refused 32", omega2_pairs(NULL, 0, 3, 1, 0) == OMEGA_BAD_ARG); + return fails ? 1 : 0; +} diff --git a/examples/deepinfer/callers/caller.cpp b/examples/deepinfer/callers/caller.cpp new file mode 100644 index 0000000..788d110 --- /dev/null +++ b/examples/deepinfer/callers/caller.cpp @@ -0,0 +1,61 @@ +// C++ caller for examples/deepinfer/flops_pairs.om. Every Omega declaration comes from the generated +// omega_native.h; nothing is typed by hand here. Expected values: evidence/deepinfer-flops-pairs/wolfram.md. +#include +#include +#include +#include +#include + +#include "omega_native.h" + +namespace { +int fails = 0; +void check(const std::string &name, bool ok) { + std::printf("%s %s\n", ok ? "PASS" : "FAIL", name.c_str()); + if (!ok) fails++; +} +struct V { + const char *fn; + std::vector a; + uint32_t status; + int64_t want; +}; +uint32_t call(const V &x, uint64_t *r) { + std::vector u; + for (int64_t v : x.a) u.push_back(static_cast(v)); + if (!std::strcmp(x.fn, "floor_prefix")) return omega2_floor_prefix(r, u[0], u[1]); + if (!std::strcmp(x.fn, "capped_prefix")) return omega2_capped_prefix(r, u[0], u[1], u[2]); + return omega2_pairs(r, u[0], u[1], u[2], u[3]); +} +} // namespace + +int main() { + const std::vector vs = { + {"floor_prefix", {9, 2}, 0, 20}, + {"capped_prefix", {9, 2, 3}, 0, 18}, + {"pairs", {3, 9, 2, 3}, 0, 16}, + {"pairs", {3, 9, 2, 0}, 0, 18}, + {"pairs", {0, 4096, 1, 512}, 0, 1966336}, + {"floor_prefix", {3037000499, 1}, 0, 4611686016981624750}, + {"capped_prefix", {4611686018427387904, 1, 2}, 0, 9223372036854775807}, + {"floor_prefix", {3037000500, 1}, 1, 0}, + {"capped_prefix", {4611686018427387905, 1, 2}, 1, 0}, + {"capped_prefix", {0, 4294967296, 2147483648}, 1, 0}, + {"floor_prefix", {-1, 1}, 9, 0}, + {"floor_prefix", {5, 0}, 9, 0}, + {"pairs", {5, 3, 1, 0}, 9, 0}, + {"pairs", {0, 3, 1, -1}, 9, 0}, + }; + const uint64_t sentinel = 0x5a5a5a5a5a5a5a5aull; + check("ident: library == header", std::strcmp(omega2_ident_flops_pairs(), OMEGA2_IDENT_flops_pairs) == 0); + for (const V &x : vs) { + uint64_t r = sentinel; + const uint32_t st = call(x, &r); + std::string name = x.fn; + for (int64_t v : x.a) name += " " + std::to_string(v); + name += " -> status " + std::to_string(st); + check(name, st == x.status && (st == OMEGA_OK ? static_cast(r) == x.want : r == sentinel)); + } + check("null result refused 32", omega2_pairs(nullptr, 0, 3, 1, 0) == OMEGA_BAD_ARG); + return fails ? 1 : 0; +} diff --git a/examples/deepinfer/callers/caller.go b/examples/deepinfer/callers/caller.go new file mode 100644 index 0000000..f4930fa --- /dev/null +++ b/examples/deepinfer/callers/caller.go @@ -0,0 +1,79 @@ +// Go caller for examples/deepinfer/flops_pairs.om. Every Omega declaration comes from the omeganative +// package (omega_native.go, generated by tools/native); this file has no cgo of its own. Expected values: +// evidence/deepinfer-flops-pairs/wolfram.md. +package main + +import ( + "fmt" + "os" + + on "omegacaller/omeganative" +) + +var fails int + +func check(name string, ok bool) { + if ok { + fmt.Println("PASS", name) + } else { + fmt.Println("FAIL", name) + fails++ + } +} + +type v struct { + f string + a []int64 + status uint32 + want int64 +} + +func call(x v, r *uint64) uint32 { + u := make([]uint64, len(x.a)) + for i, a := range x.a { + u[i] = uint64(a) + } + switch x.f { + case "floor_prefix": + return on.Omega2_floor_prefix(r, u[0], u[1]) + case "capped_prefix": + return on.Omega2_capped_prefix(r, u[0], u[1], u[2]) + } + return on.Omega2_pairs(r, u[0], u[1], u[2], u[3]) +} + +func main() { + vs := []v{ + {"floor_prefix", []int64{9, 2}, 0, 20}, + {"capped_prefix", []int64{9, 2, 3}, 0, 18}, + {"pairs", []int64{3, 9, 2, 3}, 0, 16}, + {"pairs", []int64{3, 9, 2, 0}, 0, 18}, + {"pairs", []int64{0, 4096, 1, 512}, 0, 1966336}, + {"floor_prefix", []int64{3037000499, 1}, 0, 4611686016981624750}, + {"capped_prefix", []int64{4611686018427387904, 1, 2}, 0, 9223372036854775807}, + {"floor_prefix", []int64{3037000500, 1}, 1, 0}, + {"capped_prefix", []int64{4611686018427387905, 1, 2}, 1, 0}, + {"capped_prefix", []int64{0, 4294967296, 2147483648}, 1, 0}, + {"floor_prefix", []int64{-1, 1}, 9, 0}, + {"floor_prefix", []int64{5, 0}, 9, 0}, + {"pairs", []int64{5, 3, 1, 0}, 9, 0}, + {"pairs", []int64{0, 3, 1, -1}, 9, 0}, + } + const sentinel = uint64(0x5a5a5a5a5a5a5a5a) + check("ident: library == binding", on.Omega2_ident_flops_pairs() == on.Omega2Ident_flops_pairs) + for _, x := range vs { + r := sentinel + st := call(x, &r) + ok := st == x.status + if st == on.OmegaOK { + ok = ok && int64(r) == x.want + } else { + ok = ok && r == sentinel + } + check(fmt.Sprintf("%s %v -> status %d", x.f, x.a, st), ok) + } + check("null result refused 32", on.Omega2_pairs(nil, 0, 3, 1, 0) == on.OmegaBadArg) + if fails != 0 { + os.Exit(1) + } +} diff --git a/examples/deepinfer/callers/caller.py b/examples/deepinfer/callers/caller.py new file mode 100644 index 0000000..eb37548 --- /dev/null +++ b/examples/deepinfer/callers/caller.py @@ -0,0 +1,50 @@ +"""Python caller for examples/deepinfer/flops_pairs.om: python3 -I caller.py BUILD_DIR. Every Omega +declaration comes from the generated omega_native.py; nothing is typed by hand here. Expected values: +evidence/deepinfer-flops-pairs/wolfram.md.""" +import ctypes +import sys + +sys.path.insert(0, sys.argv[1]) +import omega_native as om # noqa: E402 + +FAILS = 0 + + +def check(name, ok): + global FAILS + print("PASS" if ok else "FAIL", name) + FAILS += 0 if ok else 1 + + +VS = [ + ("floor_prefix", (9, 2), 0, 20), + ("capped_prefix", (9, 2, 3), 0, 18), + ("pairs", (3, 9, 2, 3), 0, 16), + ("pairs", (3, 9, 2, 0), 0, 18), + ("pairs", (0, 4096, 1, 512), 0, 1966336), + ("floor_prefix", (3037000499, 1), 0, 4611686016981624750), + ("capped_prefix", (4611686018427387904, 1, 2), 0, 9223372036854775807), + ("floor_prefix", (3037000500, 1), 1, 0), + ("capped_prefix", (4611686018427387905, 1, 2), 1, 0), + ("capped_prefix", (0, 4294967296, 2147483648), 1, 0), + ("floor_prefix", (-1, 1), 9, 0), + ("floor_prefix", (5, 0), 9, 0), + ("pairs", (5, 3, 1, 0), 9, 0), + ("pairs", (0, 3, 1, -1), 9, 0), +] +SENTINEL = 0x5A5A5A5A5A5A5A5A + +check("ident: library == binding", om.omega2_ident_flops_pairs() == om.OMEGA2_IDENT_flops_pairs) +for fn, args, status, want in VS: + r = ctypes.c_uint64(SENTINEL) + st = getattr(om, "omega2_" + fn)(ctypes.byref(r), *(a & 0xFFFFFFFFFFFFFFFF for a in args)) + got = r.value - (1 << 64) if r.value >> 63 else r.value + check("%s %s -> status %d" % (fn, " ".join(map(str, args)), st), + st == status and (got == want if st == om.OMEGA_OK else r.value == SENTINEL)) +check("null result refused 32", om.omega2_pairs(None, 0, 3, 1, 0) == om.OMEGA_BAD_ARG) +try: + om.omega2_pairs(ctypes.byref(ctypes.c_uint64()), -1, 3, 1, 0) + check("negative int refused by the binding", False) +except ctypes.ArgumentError: + check("negative int refused by the binding (a long is passed as its two's-complement bits)", True) +sys.exit(1 if FAILS else 0) diff --git a/examples/deepinfer/callers/caller.rs b/examples/deepinfer/callers/caller.rs new file mode 100644 index 0000000..14cc3a2 --- /dev/null +++ b/examples/deepinfer/callers/caller.rs @@ -0,0 +1,54 @@ +// Rust caller for examples/deepinfer/flops_pairs.om (no crates). Every Omega declaration comes from the +// generated omega_native.rs; nothing is typed by hand here. Expected values: +// evidence/deepinfer-flops-pairs/wolfram.md. +mod omega_native; +use omega_native::*; + +static mut FAILS: u32 = 0; +fn check(name: &str, ok: bool) { + println!("{} {}", if ok { "PASS" } else { "FAIL" }, name); + if !ok { + unsafe { FAILS += 1 }; + } +} + +fn call(f: &str, a: &[i64], r: *mut u64) -> u32 { + let u: Vec = a.iter().map(|&v| v as u64).collect(); + unsafe { + match f { + "floor_prefix" => omega2_floor_prefix(r, u[0], u[1]), + "capped_prefix" => omega2_capped_prefix(r, u[0], u[1], u[2]), + _ => omega2_pairs(r, u[0], u[1], u[2], u[3]), + } + } +} + +fn main() { + let vs: [(&str, &[i64], u32, i64); 14] = [ + ("floor_prefix", &[9, 2], 0, 20), + ("capped_prefix", &[9, 2, 3], 0, 18), + ("pairs", &[3, 9, 2, 3], 0, 16), + ("pairs", &[3, 9, 2, 0], 0, 18), + ("pairs", &[0, 4096, 1, 512], 0, 1966336), + ("floor_prefix", &[3037000499, 1], 0, 4611686016981624750), + ("capped_prefix", &[4611686018427387904, 1, 2], 0, 9223372036854775807), + ("floor_prefix", &[3037000500, 1], 1, 0), + ("capped_prefix", &[4611686018427387905, 1, 2], 1, 0), + ("capped_prefix", &[0, 4294967296, 2147483648], 1, 0), + ("floor_prefix", &[-1, 1], 9, 0), + ("floor_prefix", &[5, 0], 9, 0), + ("pairs", &[5, 3, 1, 0], 9, 0), + ("pairs", &[0, 3, 1, -1], 9, 0), + ]; + const SENTINEL: u64 = 0x5a5a5a5a5a5a5a5a; + let id = unsafe { std::ffi::CStr::from_ptr(omega2_ident_flops_pairs() as *const std::ffi::c_char) }; + check("ident: library == binding", id.to_bytes() == OMEGA2_IDENT_flops_pairs.as_bytes()); + for (f, a, status, want) in vs.iter() { + let mut r: u64 = SENTINEL; + let st = call(f, a, &mut r); + let ok = st == *status && if st == OMEGA_OK { r as i64 == *want } else { r == SENTINEL }; + check(&format!("{} {:?} -> status {}", f, a, st), ok); + } + check("null result refused 32", unsafe { omega2_pairs(std::ptr::null_mut(), 0, 3, 1, 0) } == OMEGA_BAD_ARG); + std::process::exit(if unsafe { FAILS } == 0 { 0 } else { 1 }); +} diff --git a/examples/deepinfer/callers/caller.zig b/examples/deepinfer/callers/caller.zig new file mode 100644 index 0000000..66aaaa3 --- /dev/null +++ b/examples/deepinfer/callers/caller.zig @@ -0,0 +1,54 @@ +// Zig 0.13 caller for examples/deepinfer/flops_pairs.om. Every Omega declaration comes from the generated +// omega_native.zig; nothing is typed by hand here. Expected values: evidence/deepinfer-flops-pairs/wolfram.md. +const std = @import("std"); +const om = @import("omega_native.zig"); + +var fails: u32 = 0; +fn check(name: []const u8, ok: bool) void { + std.debug.print("{s} {s}\n", .{ if (ok) "PASS" else "FAIL", name }); + if (!ok) fails += 1; +} + +const Fn = enum { floor_prefix, capped_prefix, pairs }; +const V = struct { f: Fn, a: [4]i64, status: u32, want: i64 }; + +fn call(x: V, r: ?*u64) u32 { + var u: [4]u64 = undefined; + for (x.a, 0..) |v, i| u[i] = @bitCast(v); + return switch (x.f) { + .floor_prefix => om.omega2_floor_prefix(r, u[0], u[1]), + .capped_prefix => om.omega2_capped_prefix(r, u[0], u[1], u[2]), + .pairs => om.omega2_pairs(r, u[0], u[1], u[2], u[3]), + }; +} + +pub fn main() void { + const vs = [_]V{ + .{ .f = .floor_prefix, .a = .{ 9, 2, 0, 0 }, .status = 0, .want = 20 }, + .{ .f = .capped_prefix, .a = .{ 9, 2, 3, 0 }, .status = 0, .want = 18 }, + .{ .f = .pairs, .a = .{ 3, 9, 2, 3 }, .status = 0, .want = 16 }, + .{ .f = .pairs, .a = .{ 3, 9, 2, 0 }, .status = 0, .want = 18 }, + .{ .f = .pairs, .a = .{ 0, 4096, 1, 512 }, .status = 0, .want = 1966336 }, + .{ .f = .floor_prefix, .a = .{ 3037000499, 1, 0, 0 }, .status = 0, .want = 4611686016981624750 }, + .{ .f = .capped_prefix, .a = .{ 4611686018427387904, 1, 2, 0 }, .status = 0, .want = 9223372036854775807 }, + .{ .f = .floor_prefix, .a = .{ 3037000500, 1, 0, 0 }, .status = 1, .want = 0 }, + .{ .f = .capped_prefix, .a = .{ 4611686018427387905, 1, 2, 0 }, .status = 1, .want = 0 }, + .{ .f = .capped_prefix, .a = .{ 0, 4294967296, 2147483648, 0 }, .status = 1, .want = 0 }, + .{ .f = .floor_prefix, .a = .{ -1, 1, 0, 0 }, .status = 9, .want = 0 }, + .{ .f = .floor_prefix, .a = .{ 5, 0, 0, 0 }, .status = 9, .want = 0 }, + .{ .f = .pairs, .a = .{ 5, 3, 1, 0 }, .status = 9, .want = 0 }, + .{ .f = .pairs, .a = .{ 0, 3, 1, -1 }, .status = 9, .want = 0 }, + }; + const sentinel: u64 = 0x5a5a5a5a5a5a5a5a; + check("ident: library == binding", std.mem.eql(u8, std.mem.span(om.omega2_ident_flops_pairs()), om.OMEGA2_IDENT_flops_pairs)); + var buf: [160]u8 = undefined; + for (vs) |x| { + var r: u64 = sentinel; + const st = call(x, &r); + const ok = st == x.status and (if (st == om.OMEGA_OK) @as(i64, @bitCast(r)) == x.want else r == sentinel); + const name = std.fmt.bufPrint(&buf, "{s} {any} -> status {d}", .{ @tagName(x.f), x.a, st }) catch "vector"; + check(name, ok); + } + check("null result refused 32", om.omega2_pairs(null, 0, 3, 1, 0) == om.OMEGA_BAD_ARG); + std.process.exit(if (fails == 0) 0 else 1); +} diff --git a/examples/deepinfer/diff.cpp b/examples/deepinfer/diff.cpp new file mode 100644 index 0000000..96f4ebd --- /dev/null +++ b/examples/deepinfer/diff.cpp @@ -0,0 +1,244 @@ +// Differential test of DeepInfer FlopsEstimator::floor_prefix, capped_prefix and pairs. Three sides: +// Up: the pinned upstream bytes (csrc/engine/flops_estimator.hpp lines 237-257, extracted verbatim by +// run.sh into upstream.inc and checked against its sha256), compiled here with GCC UBSan; +// Om: the Omega unit examples/deepinfer/flops_pairs.om through the generated omega_native.h; +// an oracle: direct enumeration (sum of min(t / ratio, cap) term by term) for the grid, and for large +// values a step model in __int128 (does a C++ step leave int64_t?) plus a second closed form, +// ratio*b*(b-1)/2 + b*(s+1) with n = ratio*b + s (evidence/deepinfer-flops-pairs/wolfram.md). +// Built -std=c++20 -fsanitize=undefined -fno-sanitize-recover=all: a case the step model calls defined +// that is undefined in the upstream code aborts the run. Modes: +// diff grid exhaustive n, begin, end in 0..300, ratio 1..40, cap 0..40 +// diff random SEED N N seeded random large in-domain cases (both overflow classes) +// diff up FN ARG... the upstream function alone (run per boundary case: UBSan may abort) +// diff om FN ARG... the Omega function alone: "status S value V" +// FN is fp, cp or pairs. Every Omega declaration comes from omega_native.h. +#include +#include +#include +#include +#include +#include +#include + +#include "omega_native.h" + +struct Up { +#include "upstream.inc" + static int64_t fp(int64_t n, int64_t r) { return floor_prefix(n, r); } + static int64_t cp(int64_t n, int64_t r, int64_t c) { return capped_prefix(n, r, c); } + static int64_t pr(int64_t b, int64_t e, int64_t r, int64_t c) { return pairs(b, e, r, c); } +}; + +static const uint64_t SENTINEL = 0x5555555555555555u; +struct Om { + uint32_t st; + int64_t v; + bool kept; // result left unchanged on a trap +}; +static Om om_fp(int64_t n, int64_t r) { + uint64_t x = SENTINEL; + const uint32_t st = omega2_floor_prefix(&x, static_cast(n), static_cast(r)); + return {st, static_cast(x), st == 0 || x == SENTINEL}; +} +static Om om_cp(int64_t n, int64_t r, int64_t c) { + uint64_t x = SENTINEL; + const uint32_t st = omega2_capped_prefix(&x, static_cast(n), static_cast(r), static_cast(c)); + return {st, static_cast(x), st == 0 || x == SENTINEL}; +} +static Om om_pr(int64_t b, int64_t e, int64_t r, int64_t c) { + uint64_t x = SENTINEL; + const uint32_t st = omega2_pairs(&x, static_cast(b), static_cast(e), static_cast(r), + static_cast(c)); + return {st, static_cast(x), st == 0 || x == SENTINEL}; +} + +// Step model: each C++ step of the upstream expression in __int128; ovf when any step leaves int64_t. +using i128 = __int128; +static bool fits(i128 x) { return x >= INT64_MIN && x <= INT64_MAX; } +struct M { + bool ovf; + int64_t v; +}; +static M m_fp(int64_t n, int64_t r) { + const i128 b = n / r, n1 = i128(n) + 1; + if (!fits(n1)) return {true, 0}; + const i128 a = b * n1; + if (!fits(a)) return {true, 0}; + const i128 rb = i128(r) * b; + if (!fits(rb)) return {true, 0}; + const i128 b1 = b + 1; + if (!fits(b1)) return {true, 0}; + const i128 t = rb * b1; + if (!fits(t)) return {true, 0}; + const i128 v = a - t / 2; + if (!fits(v)) return {true, 0}; + return {false, int64_t(v)}; +} +static M m_cp(int64_t n, int64_t r, int64_t c) { + const i128 rc = i128(r) * c; + if (!fits(rc)) return {true, 0}; + const int64_t stop = std::min(n, int64_t(rc)); + const M f = m_fp(stop, r); + if (f.ovf) return f; + const i128 d = i128(n) - stop; + if (!fits(d)) return {true, 0}; + const i128 dc = d * c; + if (!fits(dc)) return {true, 0}; + const i128 v = f.v + dc; + if (!fits(v)) return {true, 0}; + return {false, int64_t(v)}; +} +static M m_pr(int64_t b, int64_t e, int64_t r, int64_t c) { + const M x = c == 0 ? m_fp(e, r) : m_cp(e, r, c); + const M y = c == 0 ? m_fp(b, r) : m_cp(b, r, c); + if (x.ovf || y.ovf) return {true, 0}; + const i128 v = i128(x.v) - y.v; + if (!fits(v)) return {true, 0}; + return {false, int64_t(v)}; +} +// Second closed form, exact in __int128 for every n, ratio, cap in int64_t's nonnegative range. +static i128 alt_fp(i128 n, i128 r) { + const i128 b = n / r, s = n - r * b; + return r * b * (b - 1) / 2 + b * (s + 1); +} +static i128 alt_cp(i128 n, i128 r, i128 c) { + const i128 s = std::min(n, r * c); + return alt_fp(s, r) + (n - s) * c; +} + +static long fails = 0; +static void bad(const char *what, const char *fn, const int64_t *a, int k) { + if (++fails <= 20) { + printf("MISMATCH %s %s", what, fn); + for (int i = 0; i < k; i++) printf(" %" PRId64, a[i]); + printf("\n"); + } +} + +static int grid() { + long cases[3] = {0, 0, 0}, mis[3] = {0, 0, 0}; + for (int64_t r = 1; r <= 40; r++) { + int64_t acc = 0; + for (int64_t n = 0; n <= 300; n++) { + if (n > 0) acc += n / r; + const Om o = om_fp(n, r); + cases[0]++; + if (o.st != 0 || o.v != acc || Up::fp(n, r) != acc) { + mis[0]++; + const int64_t a[] = {n, r}; + bad("grid", "floor_prefix", a, 2); + } + } + for (int64_t c = 0; c <= 40; c++) { + acc = 0; + for (int64_t n = 0; n <= 300; n++) { + if (n > 0) acc += std::min(n / r, c); + const Om o = om_cp(n, r, c); + cases[1]++; + if (o.st != 0 || o.v != acc || Up::cp(n, r, c) != acc) { + mis[1]++; + const int64_t a[] = {n, r, c}; + bad("grid", "capped_prefix", a, 3); + } + } + for (int64_t b = 0; b <= 300; b++) { + acc = 0; // sum over t = b+1 .. e, term by term (cap 0: uncapped) + for (int64_t e = b; e <= 300; e++) { + if (e > b) acc += c == 0 ? e / r : std::min(e / r, c); + const Om o = om_pr(b, e, r, c); + cases[2]++; + if (o.st != 0 || o.v != acc || Up::pr(b, e, r, c) != acc) { + mis[2]++; + const int64_t a[] = {b, e, r, c}; + bad("grid", "pairs", a, 4); + } + } + } + } + } + const char *nm[] = {"floor_prefix", "capped_prefix", "pairs"}; + for (int i = 0; i < 3; i++) printf("GRID %s cases=%ld mismatches=%ld\n", nm[i], cases[i], mis[i]); + return mis[0] + mis[1] + mis[2] == 0 ? 0 : 1; +} + +// A nonnegative value of random magnitude: bit length uniform in 0..63, so small and large both appear. +static int64_t mag(std::mt19937_64 &g, int lo) { + const int bits = int(g() % 64); + int64_t v = bits == 0 ? 0 : int64_t(g() >> (64 - bits)); + return std::max(v, lo); +} + +static int random_cases(uint64_t seed, long count) { + std::mt19937_64 g(seed); + long defined[3] = {0, 0, 0}, ovf[3] = {0, 0, 0}, mis[3] = {0, 0, 0}; + for (long i = 0; i < count; i++) { + const int f = int(i % 3); + int64_t n = mag(g, 0), b = mag(g, 0); + const int64_t r = mag(g, 1), c = mag(g, 0); + if (b > n) std::swap(b, n); + M m; + Om o; + i128 alt = 0; + if (f == 0) { + m = m_fp(n, r), o = om_fp(n, r), alt = alt_fp(n, r); + } else if (f == 1) { + m = m_cp(n, r, c), o = om_cp(n, r, c), alt = alt_cp(n, r, c); + } else { + const int64_t cc = (g() & 3) == 0 ? 0 : c; // a quarter uncapped + m = m_pr(b, n, r, cc), o = om_pr(b, n, r, cc); + alt = cc == 0 ? alt_fp(n, r) - alt_fp(b, r) : alt_cp(n, r, cc) - alt_cp(b, r, cc); + const int64_t a[] = {b, n, r, cc}; + if (m.ovf) { + ovf[2]++; + if (o.st != 1 || !o.kept) mis[2]++, bad("random-ovf", "pairs", a, 4); + } else { + defined[2]++; + 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); + } + continue; + } + const int64_t a[] = {n, r, c}; + const char *nm = f == 0 ? "floor_prefix" : "capped_prefix"; + if (m.ovf) { + ovf[f]++; + if (o.st != 1 || !o.kept) mis[f]++, bad("random-ovf", nm, a, f == 0 ? 2 : 3); + } else { + defined[f]++; + const int64_t up = f == 0 ? Up::fp(n, r) : Up::cp(n, r, c); + 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); + } + } + const char *nm[] = {"floor_prefix", "capped_prefix", "pairs"}; + for (int i = 0; i < 3; i++) + printf("RANDOM seed=%" PRIu64 " %s defined=%ld overflow=%ld mismatches=%ld\n", seed, nm[i], defined[i], ovf[i], mis[i]); + return mis[0] + mis[1] + mis[2] == 0 && defined[0] && defined[1] && defined[2] && ovf[0] && ovf[1] && ovf[2] ? 0 : 1; +} + +int main(int argc, char **argv) { + if (argc >= 2 && !strcmp(argv[1], "grid")) return grid(); + if (argc == 4 && !strcmp(argv[1], "random")) return random_cases(strtoull(argv[2], nullptr, 10), strtol(argv[3], nullptr, 10)); + if (argc >= 4 && (!strcmp(argv[1], "up") || !strcmp(argv[1], "om"))) { + int64_t a[4] = {0, 0, 0, 0}; + const int k = argc - 3; + for (int i = 0; i < k && i < 4; i++) a[i] = strtoll(argv[3 + i], nullptr, 10); + const char *fn = argv[2]; + const bool up = argv[1][0] == 'u'; + if (!strcmp(fn, "fp") && k == 2) { + if (up) return printf("value %" PRId64 "\n", Up::fp(a[0], a[1])) < 0; + const Om o = om_fp(a[0], a[1]); + return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; + } + if (!strcmp(fn, "cp") && k == 3) { + if (up) return printf("value %" PRId64 "\n", Up::cp(a[0], a[1], a[2])) < 0; + const Om o = om_cp(a[0], a[1], a[2]); + return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; + } + if (!strcmp(fn, "pairs") && k == 4) { + if (up) return printf("value %" PRId64 "\n", Up::pr(a[0], a[1], a[2], a[3])) < 0; + const Om o = om_pr(a[0], a[1], a[2], a[3]); + return printf("status %u value %" PRId64 " kept %d\n", o.st, o.st ? 0 : o.v, o.kept) < 0; + } + } + fprintf(stderr, "usage: diff grid | random SEED N | up|om fp|cp|pairs ARG...\n"); + return 2; +} diff --git a/examples/deepinfer/diff.py b/examples/deepinfer/diff.py new file mode 100644 index 0000000..314bb49 --- /dev/null +++ b/examples/deepinfer/diff.py @@ -0,0 +1,202 @@ +"""Differential test of the Omega unit examples/deepinfer/flops_pairs.om through the generated Python +binding (omega_native.py, ctypes) against Python oracles: term-by-term enumeration of +sum(min(t // ratio, cap)) for the grid, and for large values a step model in Python integers (does a +step of the upstream C++ expression leave int64_t?) plus the second closed form +ratio*b*(b-1)//2 + b*(s+1), n = ratio*b + s. Run by run.sh as +python3 -I diff.py BUILD_DIR BOUNDARY_FILE SEED COUNT; prints one line per section and exits 0 only when +nothing mismatched. The grid is diff.cpp's: n, begin, end in 0..300, ratio 1..40, cap 0..40.""" +import ctypes +import random +import sys + +sys.path.insert(0, sys.argv[1]) +import omega_native as om # noqa: E402 every Omega declaration comes from the generated binding + +LO, HI = -(1 << 63), (1 << 63) - 1 +SENTINEL = 0x5555555555555555 +FAILS = [] + + +def bits(v): + return v & 0xFFFFFFFFFFFFFFFF + + +def signed(u): + return u - (1 << 64) if u >> 63 else u + + +def call(fn, *args): + r = ctypes.c_uint64(SENTINEL) + st = fn(ctypes.byref(r), *(bits(a) for a in args)) + return st, signed(r.value), st == 0 or r.value == SENTINEL + + +FN = {"fp": om.omega2_floor_prefix, "cp": om.omega2_capped_prefix, "pairs": om.omega2_pairs} + + +def trunc_div(a, b): # C++ / on integers + q = abs(a) // abs(b) + return q if (a >= 0) == (b >= 0) else -q + + +class Ovf(Exception): + pass + + +def chk(v): + if not LO <= v <= HI: + raise Ovf + return v + + +def m_fp(n, r): + b = trunc_div(n, r) + a = chk(b * chk(n + 1)) + t = chk(chk(r * b) * chk(b + 1)) + return chk(a - trunc_div(t, 2)) + + +def m_cp(n, r, c): + stop = min(n, chk(r * c)) + return chk(m_fp(stop, r) + chk(chk(n - stop) * c)) + + +def m_pr(b, e, r, c): + if c == 0: + return chk(m_fp(e, r) - m_fp(b, r)) + return chk(m_cp(e, r, c) - m_cp(b, r, c)) + + +def model(fn, a): + """None when a step leaves int64_t (C++ undefined behaviour), else the value.""" + try: + return {"fp": m_fp, "cp": m_cp, "pairs": m_pr}[fn](*a) + except Ovf: + return None + + +def alt_fp(n, r): + b, s = divmod(n, r) + return r * b * (b - 1) // 2 + b * (s + 1) + + +def alt_cp(n, r, c): + s = min(n, r * c) + return alt_fp(s, r) + (n - s) * c + + +def alt(fn, a): + if fn == "fp": + return alt_fp(*a) + if fn == "cp": + return alt_cp(*a) + b, e, r, c = a + return alt_fp(e, r) - alt_fp(b, r) if c == 0 else alt_cp(e, r, c) - alt_cp(b, r, c) + + +def bad(what, fn, a, got): + FAILS.append((what, fn, a, got)) + if len(FAILS) <= 20: + print("MISMATCH", what, fn, *a, got) + + +def grid(): + n_fp = n_cp = n_pr = 0 + m0 = len(FAILS) + for r in range(1, 41): + acc = 0 + for n in range(301): + acc += n // r + n_fp += 1 + got = call(FN["fp"], n, r) + if got[:2] != (0, acc): + bad("grid", "fp", (n, r), got) + for c in range(41): + acc = 0 + for n in range(301): + acc += min(n // r, c) + n_cp += 1 + got = call(FN["cp"], n, r, c) + if got[:2] != (0, acc): + bad("grid", "cp", (n, r, c), got) + for r in range(1, 41): + for c in range(41): + for b in range(301): + acc = 0 + for e in range(b, 301): + if e > b: + acc += e // r if c == 0 else min(e // r, c) + n_pr += 1 + got = call(FN["pairs"], b, e, r, c) + if got[:2] != (0, acc): + bad("grid", "pairs", (b, e, r, c), got) + print("PY-GRID floor_prefix n 0..300 ratio 1..40 cases=%d; capped_prefix cap 0..40 cases=%d; " + "pairs begin<=end 0..300 cases=%d; mismatches=%d" % (n_fp, n_cp, n_pr, len(FAILS) - m0)) + + +def mag(g, lo): + k = g.randrange(64) + return max(g.getrandbits(k) if k else 0, lo) + + +def rand(seed, count): + g = random.Random(seed) + m0 = len(FAILS) + defined = ovf = 0 + for i in range(count): + n, b, r, c = mag(g, 0), mag(g, 0), mag(g, 1), mag(g, 0) + b, n = min(b, n), max(b, n) + fn = ("fp", "cp", "pairs")[i % 3] + a = {"fp": (n, r), "cp": (n, r, c), "pairs": (b, n, r, c if g.randrange(4) else 0)}[fn] + want = model(fn, a) + got = call(FN[fn], *a) + if want is None: + ovf += 1 + if got[0] != 1 or not got[2]: + bad("random-ovf", fn, a, got) + else: + defined += 1 + if got[:2] != (0, want) or want != alt(fn, a): + bad("random", fn, a, got) + print("PY-RANDOM seed=%d defined=%d overflow=%d mismatches=%d" % (seed, defined, ovf, len(FAILS) - m0)) + if not defined or not ovf: + FAILS.append(("random", "one class empty", (), ())) + + +def boundary(path): + m0 = len(FAILS) + k = 0 + for line in open(path): + line = line.split("#")[0].strip() + if not line: + continue + lhs, expect = (s.strip() for s in line.split("|")) + fn, *args = lhs.split() + a = tuple(int(x) for x in args) + got = call(FN[fn], *a) + k += 1 + if expect == "dom": + ok = got[0] == 9 and got[2] + elif expect == "ovf": + ok = got[0] == 1 and got[2] and model(fn, a) is None + else: + ok = got[:2] == (0, int(expect)) and model(fn, a) == int(expect) + if not ok: + bad("boundary", fn, a, got) + print("PY-BOUNDARY cases=%d mismatches=%d" % (k, len(FAILS) - m0)) + + +def main(): + ident = om.omega2_ident_flops_pairs() + print("PY-IDENT", ident.decode()) + if ident != om.OMEGA2_IDENT_flops_pairs: + FAILS.append(("ident", ident)) + grid() + rand(int(sys.argv[3]), int(sys.argv[4])) + boundary(sys.argv[2]) + print("PY-TOTAL mismatches=%d" % len(FAILS)) + return 1 if FAILS else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/deepinfer/flops_estimator-omega.patch b/examples/deepinfer/flops_estimator-omega.patch new file mode 100644 index 0000000..3bb197b --- /dev/null +++ b/examples/deepinfer/flops_estimator-omega.patch @@ -0,0 +1,55 @@ +--- a/csrc/engine/flops_estimator.hpp ++++ b/csrc/engine/flops_estimator.hpp +@@ -10,6 +10,10 @@ + #include + #include + ++// floor_prefix, capped_prefix and pairs run the Omega unit flops_pairs.om (aien-dev/omega-bridge ++// examples/deepinfer), bound by omega-bridge tools/native into libomega_native. ++#include "omega_native.h" ++ + // GEMM coefficients include multiply-add (2 FLOPs) and the FP8-equivalent weight. + namespace flops_coef { + constexpr int64_t FP4 = 1; +@@ -237,23 +241,34 @@ + // Sum floor(t / ratio), t=1..n: compressed query-key pairs without a cap. + // Used for dense core attention and full-context index attention. + static int64_t floor_prefix(int64_t n, int64_t ratio) { +- const int64_t blocks = n / ratio; +- return blocks * (n + 1) - ratio * blocks * (blocks + 1) / 2; ++ uint64_t bits = 0; ++ const uint32_t status = omega2_floor_prefix(&bits, static_cast(n), static_cast(ratio)); ++ return omega_result(status, bits, "floor_prefix"); + } + + // Sum min(floor(t / ratio), cap), t=1..n. + // Used for topk/candidate limits and SWA windows (ratio=1). + static int64_t capped_prefix(int64_t n, int64_t ratio, int64_t cap) { +- const int64_t stop = std::min(n, ratio * cap); +- return floor_prefix(stop, ratio) + (n - stop) * cap; ++ uint64_t bits = 0; ++ const uint32_t status = ++ omega2_capped_prefix(&bits, static_cast(n), static_cast(ratio), static_cast(cap)); ++ return omega_result(status, bits, "capped_prefix"); + } + + // Query-key pairs for positions [begin, end), via prefix differences. + // Used for full/consumer query ranges; cap=0 means uncapped. + static int64_t pairs(int64_t begin, int64_t end, int64_t ratio, int64_t cap = 0) { +- if (cap == 0) +- return floor_prefix(end, ratio) - floor_prefix(begin, ratio); +- return capped_prefix(end, ratio, cap) - capped_prefix(begin, ratio, cap); ++ uint64_t bits = 0; ++ const uint32_t status = omega2_pairs(&bits, static_cast(begin), static_cast(end), ++ static_cast(ratio), static_cast(cap)); ++ return omega_result(status, bits, "pairs"); ++ } ++ ++ // An Omega status is 0 when done; 1 when a step leaves int64_t (where the former C++ expression was ++ // undefined behaviour); 9 outside n >= 0, ratio > 0, cap >= 0, 0 <= begin <= end. ++ static int64_t omega_result(uint32_t status, uint64_t bits, const char *fn) { ++ TORCH_CHECK(status == OMEGA_OK, fn, ": Omega trap ", status); ++ return static_cast(bits); + } + + std::pair query_ranges(int q, int k, bool is_prompt, int full_len, int swa_valid_start) const { diff --git a/examples/deepinfer/flops_pairs.om b/examples/deepinfer/flops_pairs.om new file mode 100644 index 0000000..4d2778a --- /dev/null +++ b/examples/deepinfer/flops_pairs.om @@ -0,0 +1,41 @@ +note flops_pairs.om: DeepInfer FlopsEstimator::floor_prefix, capped_prefix and pairs in Omega +note (deepseek-ai/DeepInfer 553741557a57df44a46fb3ab72b6aafeaf945337 csrc/engine/flops_estimator.hpp +note lines 237-257, Apache-2.0). Each step is the C++ expression's step in the same order, so a +note step that leaves long traps 1 exactly where the C++ would be undefined behaviour. div truncates +note toward zero like C++ /. Valid domain (the callers clamp with std::max(0, ...)): n >= 0, +note ratio > 0, cap >= 0, 0 <= begin <= end; outside it a requires clause traps 9 instead of +note returning a value. Bound through tools/native as omega2_floor_prefix, omega2_capped_prefix and +note omega2_pairs (examples/deepinfer/README.md). +om2 +note sum floor(t / ratio), t = 1 .. n +export fn floor_prefix n long ratio long to long +requires ge n 0 +requires gt ratio 0 +blocks = div n ratio +a = mul blocks add n 1 +b = div mul mul ratio blocks add blocks 1 2 +ret sub a b +end +note sum min(floor(t / ratio), cap), t = 1 .. n +export fn capped_prefix n long ratio long cap long to long +requires ge n 0 +requires gt ratio 0 +requires ge cap 0 +stop = mul ratio cap +if lt n stop +stop = n +end +ret add floor_prefix stop ratio mul sub n stop cap +end +note query-key pairs for positions [begin, finish) by prefix differences; cap 0 means uncapped +note (C++ names the second parameter end, which is an Omega keyword) +export fn pairs begin long finish long ratio long cap long to long +requires ge begin 0 +requires le begin finish +requires gt ratio 0 +requires ge cap 0 +if eq cap 0 +ret sub floor_prefix finish ratio floor_prefix begin ratio +end +ret sub capped_prefix finish ratio cap capped_prefix begin ratio cap +end diff --git a/examples/deepinfer/overflow.sh b/examples/deepinfer/overflow.sh new file mode 100755 index 0000000..6ab4132 --- /dev/null +++ b/examples/deepinfer/overflow.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# Does a 64-bit long add, sub or mul that leaves the long range trap or wrap? Measured on each compiler +# named on the command line (sh overflow.sh OMC...), in program mode (exit status 101 = trap 1, Omega +# exit_status) for OM-1 and, when the compiler accepts it, OM-2. Also records two facts flops_pairs.om +# depends on: an OM-1 requires clause and an OM-2 requires clause on a violated precondition, and a +# parameter named end. Each probe prints " : ()". +# The non-overflowing controls exit with the value mod 7 (evidence/deepinfer-flops-pairs/wolfram.md). +set -eu +T=$(mktemp -d); trap 'rm -rf "$T"' EXIT +probe() { # omc profile name expected program + omc=$1; prof=$2; name=$3; want=$4; shift 4 + { [ "$prof" = OM-2 ] && echo om2; printf '%s\n' "$@"; } > "$T/p.om" + if "$omc" < "$T/p.om" > "$T/p.bin" 2> "$T/p.err"; then + chmod +x "$T/p.bin"; rc=0; "$T/p.bin" > /dev/null 2>&1 || rc=$? + else rc="refused: $(head -c 120 "$T/p.err")"; fi + echo "$(sha256sum < "$omc" | cut -c1-8) $prof $name: $rc ($want)" +} +two() { # omc profile name expected op a b + probe "$1" "$2" "$3" "$4" "fn main to int" "a = long $6" "b = long $7" "q = $5 a b" "ret int rem q 7" "end" +} +for omc in "$@"; do + echo "INFO compiler $omc sha256 $(sha256sum < "$omc" | cut -d' ' -f1)" + for prof in OM-1 OM-2; do + two "$omc" $prof "add 2^63-1 + 1" "trap: 101" add 9223372036854775807 1 + two "$omc" $prof "sub -(2^63-1) - 2" "trap: 101" sub -9223372036854775807 2 + two "$omc" $prof "mul 2^62 * 2" "trap: 101" mul 4611686018427387904 2 + two "$omc" $prof "mul 3037000500 * 3037000500" "trap: 101" mul 3037000500 3037000500 + two "$omc" $prof "control add 2^63-1 + 0" "0 = Mod[2^63-1,7]" add 9223372036854775807 0 + two "$omc" $prof "control mul (2^62-1) * 2" "6 = Mod[2^63-2,7]" mul 4611686018427387903 2 + two "$omc" $prof "control mul 3037000499 * 3037000500" "2" mul 3037000499 3037000500 + probe "$omc" $prof "mul through a function's parameters 3037000500^2" "trap: 101" \ + "fn f a long b long to long" "ret mul a b" "end" "fn main to int" "ret int rem f 3037000500 3037000500 7" "end" + probe "$omc" $prof "requires ge x 0 called with -5" "trap 9: 109; a return of -4 is int -4 = 252" \ + "fn step x long to long" "requires ge x 0" "ret add x 1" "end" "fn main to int" "ret int step -5" "end" + probe "$omc" $prof "parameter named end" "refused" \ + "fn f begin long end long to long" "ret sub end begin" "end" "fn main to int" "ret int f 1 3" "end" + done +done diff --git a/examples/deepinfer/patched.cpp b/examples/deepinfer/patched.cpp new file mode 100644 index 0000000..73228ab --- /dev/null +++ b/examples/deepinfer/patched.cpp @@ -0,0 +1,139 @@ +// Standalone check of flops_estimator-omega.patch without torch. run.sh applies the patch to a copy of +// the pinned header and extracts the same two regions from the original and the patched header: +// *-types.inc struct QueryRange and struct PairTerm (whose count() calls pairs); +// *-fns.inc floor_prefix, capped_prefix, pairs (and the patch's omega_result) and query_ranges. +// Each pair of regions becomes a struct with the members query_ranges reads; TORCH_CHECK is a stub +// that throws std::runtime_error with the same message parts. Built -std=c++20 with GCC UBSan (the +// original side may not hit undefined behaviour). The patched side calls the Omega library through +// omega_native.h. Prints one line per section; exit 0 only when every compared result is identical. +#include +#include +#include +#include +#include +#include +#include +#include + +#include "omega_native.h" + +template static std::string torch_msg(const A &...a) { + std::ostringstream s; + (s << ... << a); + return s.str(); +} +#define TORCH_CHECK(cond, ...) \ + do { \ + if (!(cond)) throw std::runtime_error(torch_msg(__VA_ARGS__)); \ + } while (0) + +struct Orig { + bool compact = false; + int keep_tokens = 0; + int window_size = 0; + bool has_truncated_swa = false; +#include "orig-types.inc" +#include "orig-fns.inc" +}; +struct Patched { + bool compact = false; + int keep_tokens = 0; + int window_size = 0; + bool has_truncated_swa = false; +#include "patched-types.inc" +#include "patched-fns.inc" +}; + +static long fails = 0; +static void bad(const char *what, long a, long b, long c, long d) { + if (++fails <= 20) printf("MISMATCH %s %ld %ld %ld %ld\n", what, a, b, c, d); +} + +// The exception message (or "" when none) and the result of f. +template static std::pair run(F f) { + try { + return {"", f()}; + } catch (const std::runtime_error &e) { + return {e.what(), 0}; + } +} + +int main() { + // 1. PairTerm::count, the path calc_attn_flops uses: shift = ratio * (block - 1), block 1..4. + long n1 = 0; + for (int64_t ratio = 1; ratio <= 40; ratio++) + for (int cap = 0; cap <= 40; cap++) + for (int64_t block = 1; block <= 4; block++) + for (int b = 0; b <= 120; b++) + for (int e = b + 1; e <= 120; e++) { + const Orig::PairTerm ot{ratio, cap, ratio * (block - 1), 0, 0}; + const Patched::PairTerm pt{ratio, cap, ratio * (block - 1), 0, 0}; + const Orig::QueryRange oq{b, e, 0}; + const Patched::QueryRange pq{b, e, 0}; + n1++; + if (ot.count(oq) != pt.count(pq)) bad("PairTerm::count", ratio, cap, b, e); + } + printf("PATCH PairTerm::count cases=%ld mismatches=%ld\n", n1, fails); + + // 2. query_ranges (pairs with ratio 1, cap window_size), including its TORCH_CHECK refusals. + long n2 = 0, refused = 0; + const long f1 = fails; + const int windows[] = {0, 1, 7, 64, 128}; + const int keeps[] = {0, 5, 140}; + for (int w : windows) + for (int flags = 0; flags < 4; flags++) + for (int keep : keeps) { + Orig o; + Patched p; + o.window_size = p.window_size = w; + o.compact = p.compact = flags & 1; + o.has_truncated_swa = p.has_truncated_swa = flags & 2; + o.keep_tokens = p.keep_tokens = keep; + for (int q = 0; q <= 24; q++) + for (int k = q; k <= 160; k += 3) + for (int prompt = 0; prompt < 2; prompt++) + for (int fl : {-1, k, k + 3, k + 200, k - 1}) + for (int sv = 0; sv <= k - q + 1; sv += 4) { + n2++; + const auto a = run([&] { + const auto [f, c] = o.query_ranges(q, k, prompt, fl, sv); + return f.begin * 1000003LL + f.end * 1009LL + f.swa_pairs * 7919LL + c.begin * 31LL + + c.end * 17LL + c.swa_pairs; + }); + const auto b = run([&] { + const auto [f, c] = p.query_ranges(q, k, prompt, fl, sv); + return f.begin * 1000003LL + f.end * 1009LL + f.swa_pairs * 7919LL + c.begin * 31LL + + c.end * 17LL + c.swa_pairs; + }); + if (!a.first.empty()) refused++; + if (a != b) bad("query_ranges", q, k, fl, sv); + } + } + printf("PATCH query_ranges cases=%ld refused-by-TORCH_CHECK=%ld mismatches=%ld\n", n2, refused, fails - f1); + + // 3. Omega traps reach TORCH_CHECK. The original has no refusal: these inputs are outside its + // domain (ratio 0 divides by zero; 3037000500 overflows a step), so only the patched side runs. + struct T { + const char *name; + std::pair got; + const char *want; + }; + const T traps[] = { + {"pairs(5, 3, 1, 0)", run([] { return Patched::pairs(5, 3, 1, 0); }), "pairs: Omega trap 9"}, + {"pairs(0, 3, 1, -1)", run([] { return Patched::pairs(0, 3, 1, -1); }), "pairs: Omega trap 9"}, + {"floor_prefix(5, 0)", run([] { return Patched::floor_prefix(5, 0); }), "floor_prefix: Omega trap 9"}, + {"floor_prefix(3037000500, 1)", run([] { return Patched::floor_prefix(3037000500, 1); }), "floor_prefix: Omega trap 1"}, + {"capped_prefix(0, 4294967296, 2147483648)", run([] { return Patched::capped_prefix(0, 4294967296LL, 2147483648LL); }), + "capped_prefix: Omega trap 1"}, + }; + long n3 = 0; + const long f2 = fails; + for (const T &t : traps) { + n3++; + printf("PATCH trap %s -> \"%s\"\n", t.name, t.got.first.c_str()); + if (t.got.first != t.want) bad(t.name, 0, 0, 0, 0); + } + printf("PATCH traps cases=%ld mismatches=%ld\n", n3, fails - f2); + printf("PATCH total mismatches=%ld\n", fails); + return fails == 0 ? 0 : 1; +} diff --git a/examples/deepinfer/run.sh b/examples/deepinfer/run.sh new file mode 100755 index 0000000..89edf9f --- /dev/null +++ b/examples/deepinfer/run.sh @@ -0,0 +1,181 @@ +#!/bin/sh +# DeepInfer FlopsEstimator::floor_prefix, capped_prefix and pairs as the Omega unit flops_pairs.om: +# differential test against the pinned upstream code, mutants, the flops_estimator-omega.patch harness +# and the native callers (examples/deepinfer/README.md). Environment: +# DEEPINFER_SRC a deepseek-ai/DeepInfer checkout at 553741557a57df44a46fb3ab72b6aafeaf945337 (read only) +# OMEGA_NATIVE_OMC the seeded omc pinned by tools/native +# OMEGA_NATIVE_OMC2 the OM-2 compiler (recorded run: omega-mv e5b32567 seed/out/omc, sha256 2c824805...) +# SEED, RANDOM_N random-case seed and count for diff.cpp (default 20261031, 1000000) +# Output goes to build/deepinfer. A missing toolchain is NOT_RUN, never PASS. Exit 0 only when every +# check that ran passed. +set -eu +ROOT=$(cd "$(dirname "$0")/../.." && pwd) +D=$ROOT/examples/deepinfer +O=$ROOT/build/deepinfer +SRC=${DEEPINFER_SRC:?set DEEPINFER_SRC to a DeepInfer checkout at 553741557a57df44a46fb3ab72b6aafeaf945337} +SEED=${SEED:-20261031} +RANDOM_N=${RANDOM_N:-1000000} +PIN_REV=553741557a57df44a46fb3ab72b6aafeaf945337 +PIN_HDR=89fbefa41ef0f1ee79be19bb92700d446de38c3781d18227bb0044eb20e7b4f2 +PIN_INC=95b16b3dafffb46ac3a3f57f41d9d296436f905e6d0ce9814e9a5ace8cb1c611 +OMC=${OMEGA_NATIVE_OMC:?set OMEGA_NATIVE_OMC to the seeded omc pinned by tools/native} +OMC2=${OMEGA_NATIVE_OMC2:?set OMEGA_NATIVE_OMC2 to the OM-2 compiler} +export OMEGA_NATIVE_OMC OMEGA_NATIVE_OMC2 PYTHONDONTWRITEBYTECODE=1 +export UBSAN_OPTIONS=print_stacktrace=0 +fails=0; notrun="" +pass() { echo "PASS $1"; } +fail() { echo "FAIL $1"; fails=$((fails + 1)); } +sha() { sha256sum < "$1" | cut -d' ' -f1; } +rm -rf "$O"; mkdir -p "$O" + +# 0. Inputs: the upstream revision and bytes, and the compilers. +HDR=$SRC/csrc/engine/flops_estimator.hpp +rev=$(git -C "$SRC" rev-parse HEAD 2> /dev/null || echo unknown) +[ "$rev" = "$PIN_REV" ] || { echo "FAIL DeepInfer HEAD $rev, pinned $PIN_REV"; exit 1; } +[ "$(sha "$HDR")" = "$PIN_HDR" ] || { echo "FAIL flops_estimator.hpp sha256 $(sha "$HDR"), pinned $PIN_HDR"; exit 1; } +sed -n 237,257p "$HDR" > "$O/upstream.inc" +[ "$(sha "$O/upstream.inc")" = "$PIN_INC" ] || { echo "FAIL upstream lines 237-257 sha256 $(sha "$O/upstream.inc")"; exit 1; } +echo "INFO DeepInfer $rev csrc/engine/flops_estimator.hpp sha256 $PIN_HDR; lines 237-257 sha256 $PIN_INC" +echo "INFO omc sha256 $(sha "$OMC"); OM-2 compiler sha256 $(sha "$OMC2"); flops_pairs.om sha256 $(sha "$D/flops_pairs.om")" +echo "INFO $(g++ --version | head -n 1)" + +# 1. The library and its bindings. +if "$ROOT/tools/native" "$O" "$D/flops_pairs.om" > "$O/tools-native.log" 2>&1; then + pass "tools/native: flops_pairs.om (OM-2) -> libomega_native.so sha256 $(sha "$O/libomega_native.so") and six bindings" +else fail "tools/native"; cat "$O/tools-native.log"; exit 1; fi +grep -o 'omega-ident [^"]*' "$O/omega_native.h" | head -n 1 | sed 's/^/INFO /' + +# 2. Upstream (UBSan) against Omega against the oracles. +UBSAN="-fsanitize=undefined,signed-integer-overflow -fno-sanitize-recover=all" +diffbin() { g++ -std=c++20 -O1 -Wall -Wextra -Werror $UBSAN -I "$1" -I "$O" "$D/diff.cpp" -L"$1" -lomega_native -Wl,-rpath,"$1" -o "$1/diff"; } +diffbin "$O" +if "$O/diff" grid > "$O/grid.log" 2>&1; then pass "C++ grid (upstream = Omega = enumeration)"; else fail "C++ grid"; fi +sed 's/^/ /' "$O/grid.log" +if "$O/diff" random "$SEED" "$RANDOM_N" > "$O/random.log" 2>&1; then pass "C++ random (upstream = Omega = step model = second closed form; overflow -> trap 1)" +else fail "C++ random"; fi +sed 's/^/ /' "$O/random.log" + +# boundary BIN: the Omega side of each boundary case; prints the number of failing cases. +boundary() { + bin=$1; bad=0 + while IFS='|' read -r lhs want; do + case "$lhs" in '#'* | '') continue;; esac + want=$(echo $want) + got=$("$bin" om $lhs 2>&1 || true) + case "$want" in + ovf) exp="status 1 value 0 kept 1";; + dom) exp="status 9 value 0 kept 1";; + *) exp="status 0 value $want kept 1";; + esac + [ "$got" = "$exp" ] || bad=$((bad + 1)) + done + echo $bad +} +bfails=0; bn=0 +while IFS='|' read -r lhs want; do + case "$lhs" in '#'* | '') continue;; esac + want=$(echo $want); lhs=$(echo $lhs); bn=$((bn + 1)) + om=$("$O/diff" om $lhs 2>&1 || true) + up=$("$O/diff" up $lhs 2>&1) && urc=0 || urc=$? + upline=$(printf '%s\n' "$up" | grep -m 1 -o 'runtime error: .*\|^value .*' || echo "exit $urc") + case "$want" in + ovf) ok=0; [ "$om" = "status 1 value 0 kept 1" ] && [ $urc != 0 ] && printf '%s' "$up" | grep -q 'signed integer overflow' && ok=1;; + dom) ok=0; [ "$om" = "status 9 value 0 kept 1" ] && ok=1;; + *) ok=0; [ "$om" = "status 0 value $want kept 1" ] && [ "$up" = "value $want" ] && ok=1;; + esac + [ $ok = 1 ] || bfails=$((bfails + 1)) + echo " BOUNDARY $([ $ok = 1 ] && echo ok || echo BAD) $lhs | want $want | omega: $om | upstream: $upline" +done < "$D/boundary.txt" +if [ $bfails = 0 ]; then pass "boundary: $bn cases (values equal; ovf: Omega trap 1 and UBSan signed integer overflow; dom: Omega trap 9)" +else fail "boundary: $bfails of $bn cases"; fi + +# 3. The generated Python binding against Python oracles. +if command -v python3 > /dev/null; then + if python3 -I "$D/diff.py" "$O" "$D/boundary.txt" "$SEED" 200000 > "$O/py.log" 2>&1; then pass "Python binding differential"; else fail "Python binding differential"; fi + sed 's/^/ /' "$O/py.log" +else notrun="$notrun python-differential"; fi + +# 4. flops_estimator-omega.patch: the patched header against the original (no torch: stub TORCH_CHECK). +P=$O/patch; mkdir -p "$P/a" "$P/b" +cp "$HDR" "$P/a/flops_estimator.hpp"; cp "$HDR" "$P/b/flops_estimator.hpp"; chmod u+w "$P/b/flops_estimator.hpp" +if patch -s -p3 -d "$P/b" < "$D/flops_estimator-omega.patch"; then pass "flops_estimator-omega.patch applies to the pinned header (patched sha256 $(sha "$P/b/flops_estimator.hpp"))" +else fail "patch does not apply"; fi +for s in a b; do + n=$([ $s = a ] && echo orig || echo patched) + awk '/^ struct QueryRange \{/{on=1} on{print} on&&/int64_t count\(/{c=1} c&&/^ \};/{exit}' "$P/$s/flops_estimator.hpp" > "$P/$n-types.inc" + awk '/^ \/\/ Sum floor\(t \/ ratio\)/{on=1} on{print} on&&/^ return \{full, consumer\};/{r=1; next} r&&/^ }$/{exit}' "$P/$s/flops_estimator.hpp" > "$P/$n-fns.inc" +done +echo "INFO extracted regions: $(wc -l < "$P/orig-types.inc")+$(wc -l < "$P/orig-fns.inc") original lines, $(wc -l < "$P/patched-types.inc")+$(wc -l < "$P/patched-fns.inc") patched lines" +patchbin() { g++ -std=c++20 -O1 -Wall -Wextra -Werror $UBSAN -I "$P" -I "$1" "$D/patched.cpp" -L"$1" -lomega_native -Wl,-rpath,"$1" -o "$1/patched"; } +patchbin "$O" +if "$O/patched" > "$P/patched.log" 2>&1; then pass "patched header = original on PairTerm::count and query_ranges; Omega traps reach TORCH_CHECK" +else fail "patched header harness"; fi +sed 's/^/ /' "$P/patched.log" +# The whole patched header against the torch C++ headers (syntax only), when a torch install is given. +TORCH_INC=${TORCH_INC:-} +if [ -n "$TORCH_INC" ] && [ -f "$TORCH_INC/torch/extension.h" ]; then + PYINC=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["include"])') + for s in a b; do + if g++ -std=c++20 -fsyntax-only -w -x c++ -I "$P/$s" -I "$O" -I "$TORCH_INC" -I "$TORCH_INC/torch/csrc/api/include" -I "$PYINC" \ + - < /dev/null -include "$P/$s/flops_estimator.hpp" > "$P/torch-$s.log" 2>&1; then + pass "whole $([ $s = a ] && echo original || echo patched) header compiles against torch headers ($TORCH_INC)" + else fail "whole $s header against torch headers"; cat "$P/torch-$s.log"; fi + done +else notrun="$notrun torch-header-compile"; fi + +# 5. Mutants: each must be caught. M1 floor_prefix a = blocks * n; M2 capped_prefix max for min; +# M3 floor_prefix without requires gt ratio 0. Same unit name, own folder. +mutant() { + M=$O/mut-$1; mkdir -p "$M/src" + sed "$2" "$D/flops_pairs.om" > "$M/src/flops_pairs.om" + if cmp -s "$D/flops_pairs.om" "$M/src/flops_pairs.om"; then fail "mutant $1 did not change the unit"; return; fi + "$ROOT/tools/native" "$M" "$M/src/flops_pairs.om" > "$M/tools-native.log" 2>&1 || { fail "mutant $1 build"; cat "$M/tools-native.log"; return; } + diffbin "$M" + "$M/diff" grid > "$M/grid.log" 2>&1 || true + gm=$(sed -n 's/.*mismatches=//p' "$M/grid.log" | awk '{s+=$1} END {print s+0}') + bm=$(boundary "$M/diff" < "$D/boundary.txt") + pm=NOT_RUN + if [ "$1" = M1 ]; then patchbin "$M"; "$M/patched" > "$M/patched.log" 2>&1 || true; pm=$(sed -n 's/^PATCH total mismatches=//p' "$M/patched.log"); fi + if [ $((gm + bm)) -gt 0 ]; then pass "mutant $1 caught: grid mismatches=$gm boundary failures=$bm patch-harness mismatches=$pm" + else fail "mutant $1 not caught (grid $gm, boundary $bm)"; fi +} +mutant M1 's/^a = mul blocks add n 1$/a = mul blocks n/' +mutant M2 's/^if lt n stop$/if gt n stop/' +mutant M3 '13{/^requires gt ratio 0$/d}' + +# 6. Native callers. No caller declares an Omega function by hand. +C=$D/callers +if grep -n -E 'uint32_t omega|fn omega|argtypes|CDLL|import "C"' "$C"/caller.* "$D/diff.cpp" "$D/patched.cpp" "$D/diff.py" > "$O/hand.log"; then + fail "a caller declares an Omega function by hand"; cat "$O/hand.log" +else pass "no caller declares an Omega function by hand"; fi +ran="" +run() { lang=$1; shift; if "$@" > "$O/$lang.log" 2>&1; then pass "$lang caller ($(grep -c '^PASS' "$O/$lang.log") checks)"; ran="$ran $lang"; else fail "$lang caller"; cat "$O/$lang.log"; fi; } +bld() { lang=$1; shift; if "$@" > "$O/$lang-build.log" 2>&1; then return 0; fi; fail "$lang build"; cat "$O/$lang-build.log"; return 1; } +cp "$C"/caller.* "$O/" +if command -v cc > /dev/null; then + bld c cc -std=gnu11 -O2 -Wall -Wextra -Werror -I "$O" "$O/caller.c" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_c" && run c "$O/caller_c" + if OMEGA_NATIVE_CFLAGS="-nostartfiles -mbranch-protection=standard -Wl,-z,force-bti" "$ROOT/tools/native" "$O/bti" "$D/flops_pairs.om" > "$O/bti-native.log" 2>&1 \ + && bld c-bti cc -std=gnu11 -O2 -Wall -Wextra -Werror -I "$O/bti" "$O/caller.c" -L"$O/bti" -lomega_native -Wl,-rpath,"$O/bti" -o "$O/bti/caller_c"; then + if readelf -n "$O/bti/libomega_native.so" | grep -q "feature: BTI"; then run c-bti "$O/bti/caller_c"; else fail "c-bti library carries no BTI property"; fi + else fail "c-bti library build"; cat "$O/bti-native.log"; fi +else notrun="$notrun c c-bti"; fi +if command -v c++ > /dev/null; then + bld cpp c++ -std=c++17 -O2 -Wall -Wextra -Werror -I "$O" "$O/caller.cpp" -L"$O" -lomega_native -Wl,-rpath,"$O" -o "$O/caller_cpp" && run cpp "$O/caller_cpp" +else notrun="$notrun cpp"; fi +if command -v python3 > /dev/null; then run python python3 -I "$O/caller.py" "$O"; else notrun="$notrun python"; fi +if command -v rustc > /dev/null; then + bld rust rustc -O --edition 2021 -D warnings "$O/caller.rs" -L "$O" -C link-arg=-Wl,-rpath,"$O" -o "$O/caller_rs" && run rust "$O/caller_rs" +else notrun="$notrun rust"; fi +if command -v zig > /dev/null; then + bld zig sh -c 'cd "$1" && zig build-exe caller.zig -L. -lomega_native -lc -rpath "$1" -femit-bin=caller_zig' _ "$O" && run zig "$O/caller_zig" +else notrun="$notrun zig"; fi +if command -v go > /dev/null; then + G=$O/go; mkdir -p "$G/omeganative" + cp "$O/omega_native.go" "$O/omega_native.h" "$O/libomega_native.so" "$G/omeganative/"; cp "$O/caller.go" "$G/" + bld go sh -c 'cd "$1" && go mod init omegacaller > /dev/null 2>&1 && CGO_LDFLAGS="-Wl,-rpath,$1/omeganative" go build -o caller_go .' _ "$G" && run go "$G/caller_go" +else notrun="$notrun go"; fi +[ "$(echo $ran)" = "c c-bti cpp python rust zig go" ] && echo "NATIVE-CALL PASS (c c-bti cpp python rust zig go)" + +echo "RESULT failures=$fails NOT_RUN:${notrun:- none}" +[ $fails = 0 ] && echo "DEEPINFER-FLOPS PASS" || echo "DEEPINFER-FLOPS FAIL" +exit $fails