interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / stablehlo / gate-irstablehlo.sh

202 lines · 11902 bytes Raw Blame History
1#!/bin/sh
2# Host-only verification of seed/stablehlo/irstablehlo.om on AArch64 Linux.
3# IREE (iree-compile, iree-run-module) and the XLA CPU PJRT client in jaxlib are
4# interop oracles, not Omega-native: they only read the module Omega wrote and
5# never generate kernels. No result here is hardware qualification.
6# Exit 0 PASS, 1 FAIL, 3 NOT_RUN (an oracle or the host is absent).
7# Oracle locations: IREE_COMPILE, IREE_RUN, PYTHON (defaults from PATH).
8set -eu
9cd "$(dirname "$0")/../.."
10B=build/seed/stablehlo
11D=seed/stablehlo
12fail() { printf 'FAIL %s\n' "$*"; exit 1; }
13notrun() { printf 'NOT_RUN %s\n' "$*"; exit 3; }
14[ "$(uname -sm)" = 'Linux aarch64' ] || notrun 'requires AArch64 Linux'
15IREE_COMPILE=${IREE_COMPILE:-$(command -v iree-compile || true)}
16IREE_RUN=${IREE_RUN:-$(command -v iree-run-module || true)}
17PYTHON=${PYTHON:-$(command -v python3 || true)}
18[ -n "$IREE_COMPILE" ] && [ -x "$IREE_COMPILE" ] || notrun 'requires iree-compile oracle'
19[ -n "$IREE_RUN" ] && [ -x "$IREE_RUN" ] || notrun 'requires iree-run-module oracle'
20[ -n "$PYTHON" ] && "$PYTHON" -c 'import jax, jaxlib, numpy' 2>/dev/null || notrun 'requires jaxlib PJRT CPU oracle'
21OMC=seed/out/omc
22pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991
23[ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs'
24rm -rf "$B"
25mkdir -p "$B"
26printf 'SOURCE %s\n' "$(git rev-parse HEAD)"
27printf 'COMPILER %s\n' "$pin"
28printf 'ORACLE iree-compile %s\n' "$("$IREE_COMPILE" --version 2>&1 | sed -n 's/^ *IREE compiler version //p' | head -n 1)"
29printf 'ORACLE iree-run-module %s\n' "$(sha256sum "$IREE_RUN" | cut -d ' ' -f 1)"
30printf 'ORACLE pjrt %s\n' "$(JAX_PLATFORMS=cpu "$PYTHON" -c 'import jax, jaxlib; print("jax", jax.__version__, "jaxlib", jaxlib.__version__)')"
31uname -sm
32
33nice -n 19 "$OMC" < "$D/irstablehlo.om" > "$B/irstablehlo" 2> "$B/compile.err" || fail 'translator compile'
34[ ! -s "$B/compile.err" ] || fail 'translator compiler stderr'
35chmod +x "$B/irstablehlo"
36T="$B/irstablehlo"
37for unit in vecadd refuse; do
38 { printf 'ir\n'; cat "$D/$unit.unit.om"; } | nice -n 19 "$OMC" > "$B/$unit.ir" || fail "$unit ir"
39done
40# Wire v7 fixtures (spec/one-layer-format.md section 10): omc ir refuses float
41# words at this pin, so the f32/f64 scalar IR is written byte by byte here.
42# Header: magic OSC1IR\0, version, nstructs 0, nregions 0, nfuncs 1. Function:
43# name, nparams 2, return tag, nvregs 3 with tags, one block (first 0, count 2),
44# BIN add r2 = r0 + r1, RET r2, empty requires and ensures.
45bytes() { for b in "$@"; do printf "\\$(printf %03o "$b")"; done; }
46fixture() {
47 ver=$1 tag=$2; shift 2
48 bytes 79 83 67 49 73 82 0 "$ver"
49 [ "$ver" = 7 ] && bytes 0 0 0
50 bytes 1 0 "$#"; bytes "$@"
51 bytes 2 "$tag" 3 0 "$tag" "$tag" "$tag" 1 0 0 0 0 0 2 0 0 0 2 0 0 0
52 bytes 3 1 2 0 0 0 1 0 15 2 0 0 0 0 0
53}
54fixture 7 13 118 97 100 100 102 51 50 > "$B/f32.ir"
55fixture 7 14 118 97 100 100 102 54 52 > "$B/f64.ir"
56fixture 1 13 118 97 100 100 102 51 50 > "$B/v1f32.ir"
57
58iree_compile() { nice -n 19 "$IREE_COMPILE" --iree-hal-target-device=local \
59 --iree-hal-local-target-device-backends=llvm-cpu --iree-input-type=stablehlo --iree-input-demote-f64-to-f32=false \
60 --iree-input-demote-i64-to-i32=false "$1" -o "$2"; }
61# iree_check MODULE.vmfb TYPE EXPECTED INPUT...; byte-equality compare.
62iree_check() {
63 vm=$1 ty=$2 ex=$3; shift 3
64 set -- "$@"
65 args=
66 for x in "$@"; do args="$args --input=8x$ty=$x"; done
67 # shellcheck disable=SC2086
68 nice -n 19 "$IREE_RUN" --device=local-task --module="$vm" --function=main $args \
69 --expected_output="8x$ty=$ex" --equality_mode=exact
70}
71pjrt() { PJRT_ORACLE_PLATFORM=cpu nice -n 19 "$PYTHON" "$D/pjrt-oracle.py" "$@" 2>> "$B/pjrt.stderr"; }
72otype() { case "$1" in i32) echo int ;; ui32) echo uint ;; i64) echo long ;; *) echo - ;; esac; }
73
74i=0
75tab=$(printf '\t')
76while IFS="$tab" read -r fx fn ty ex in1 in2 in3; do
77 case "$fx" in ''|'#'*) continue ;; esac
78 i=$((i+1))
79 id="case-$i-$fn"
80 nice -n 19 "$T" 8 "$fn" < "$B/$fx.ir" > "$B/$id.mlir" || fail "$id translate"
81 nice -n 19 "$T" 8 "$fn" < "$B/$fx.ir" > "$B/$id.again.mlir" || fail "$id translate again"
82 cmp -s "$B/$id.mlir" "$B/$id.again.mlir" || fail "$id nondeterministic"
83 set -- "$in1"
84 [ -z "$in2" ] || set -- "$@" "$in2"
85 [ -z "$in3" ] || set -- "$@" "$in3"
86 iree_compile "$B/$id.mlir" "$B/$id.vmfb" 2> "$B/$id.iree-compile.err" || fail "$id iree-compile refused Omega module"
87 iree_check "$B/$id.vmfb" "$ty" "$ex" "$@" > "$B/$id.iree.out" 2>&1 || fail "$id IREE mismatch: $(grep -m1 FAILED "$B/$id.iree.out")"
88 grep -q '^\[SUCCESS\]' "$B/$id.iree.out" || fail "$id IREE gave no SUCCESS line"
89 pjrt "$B/$id.mlir" "$ty" 8 "$ex" "$@" > "$B/$id.pjrt.out" || fail "$id PJRT: $(tail -n 1 "$B/$id.pjrt.out")"
90 # Omega-native differential: the same scalar function, compiled natively by
91 # omc, must give the expected value for every element (integer rows).
92 ot=$(otype "$ty")
93 if [ "$ot" != - ]; then
94 { cat "$D/vecadd.unit.om"; printf 'fn main to int\n'
95 for k in 1 2 3 4 5 6 7 8; do
96 call="$fn"
97 for v in "$@"; do call="$call $ot $(printf '%s' "$v" | cut -d , -f "$k")"; done
98 printf 'if ne %s %s %s\nexit 1\nend\n' "$call" "$ot" "$(printf '%s' "$ex" | cut -d , -f "$k")"
99 done
100 printf 'ret 0\nend\n'; } > "$B/$id.native.om"
101 nice -n 19 "$OMC" < "$B/$id.native.om" > "$B/$id.native" 2> "$B/$id.native.err" || fail "$id native compile"
102 chmod +x "$B/$id.native"
103 nice -n 19 "$B/$id.native" || fail "$id Omega-native result differs from table"
104 native=native
105 else
106 native='native NOT_RUN (omc ir has no float words at this pin)'
107 fi
108 printf 'PASS %s %s deterministic, IREE exact match, PJRT bitwise match, %s\n' "$id" "$ty" "$native"
109done < "$D/cases.tsv"
110[ "$i" = 7 ] || fail "expected 7 case rows, ran $i"
111
112# Negative controls: each must be caught.
113neg=0
114expect_mismatch() {
115 label=$1 mod=$2 ty=$3 ex=$4; shift 4
116 iree_compile "$mod" "$mod.vmfb" 2> "$mod.iree-compile.err" || fail "$label: iree-compile refused instead of mismatching"
117 if iree_check "$mod.vmfb" "$ty" "$ex" "$@" > "$mod.iree.out" 2>&1; then fail "$label: IREE accepted"; fi
118 grep -q '^\[FAILED\]' "$mod.iree.out" || fail "$label: IREE failed without a mismatch line"
119 set +e; pjrt "$mod" "$ty" 8 "$ex" "$@" > "$mod.pjrt.out"; rc=$?; set -e
120 [ "$rc" = 1 ] || fail "$label: PJRT rc $rc, expected 1 (mismatch)"
121 neg=$((neg+1))
122 printf 'PASS negative %s: IREE [FAILED], PJRT MISMATCH\n' "$label"
123}
124expect_refused() {
125 label=$1 mod=$2
126 if iree_compile "$mod" "$mod.vmfb" 2> "$mod.iree-compile.err"; then fail "$label: iree-compile accepted"; fi
127 set +e; pjrt "$mod" i32 8 0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0 > "$mod.pjrt.out"; rc=$?; set -e
128 [ "$rc" = 2 ] || fail "$label: PJRT rc $rc, expected 2 (module refused)"
129 neg=$((neg+1))
130 printf 'PASS negative %s: iree-compile and PJRT refused\n' "$label"
131}
132I32A=0,1,-1,2147483646,-2147483647,123456,-98765,7
133I32B=0,2,-1,1,-1,654321,98765,-10
134I32E=0,3,-2,2147483647,-2147483648,777777,0,-3
135F32A=1.5,0.25,-2,1048576,0.1,16777216,1e38,-7.75
136F32B=2.25,0.5,2,0.5,0.2,1,1e38,0.125
137sed 's/stablehlo\.add/stablehlo.subtract/' "$B/case-1-vaddi32.mlir" > "$B/neg-wrongop.mlir"
138cmp -s "$B/neg-wrongop.mlir" "$B/case-1-vaddi32.mlir" && fail 'mutant identical'
139expect_mismatch 'wrong op (add->subtract) i32' "$B/neg-wrongop.mlir" i32 "$I32E" "$I32A" "$I32B"
140sed 's/stablehlo\.add/stablehlo.multiply/' "$B/case-6-vaddf32.mlir" > "$B/neg-wrongop-f32.mlir"
141expect_mismatch 'wrong op (add->multiply) f32' "$B/neg-wrongop-f32.mlir" f32 \
142 3.75,0.75,0,1048576.5,0.300000012,16777216,1.99999994e38,-7.625 "$F32A" "$F32B"
143cp "$B/case-1-vaddi32.mlir" "$B/neg-wrongexp.mlir"
144expect_mismatch 'wrong expected (element 0 = 1) i32' "$B/neg-wrongexp.mlir" i32 1,3,-2,2147483647,-2147483648,777777,0,-3 "$I32A" "$I32B"
145cp "$B/case-6-vaddf32.mlir" "$B/neg-wrongexp-f32.mlir"
146expect_mismatch 'wrong expected (element 4 one ulp up) f32' "$B/neg-wrongexp-f32.mlir" f32 \
147 3.75,0.75,0,1048576.5,0.300000042,16777216,1.99999994e38,-7.625 "$F32A" "$F32B"
148sed '$d' "$B/case-1-vaddi32.mlir" > "$B/neg-nobrace.mlir"
149expect_refused 'malformed (closing brace dropped)' "$B/neg-nobrace.mlir"
150sed 's/stablehlo\.add/stablehlo.addx/' "$B/case-1-vaddi32.mlir" > "$B/neg-badop.mlir"
151expect_refused 'malformed (unknown op stablehlo.addx)' "$B/neg-badop.mlir"
152sed 's/-> tensor<8xi32>/-> tensor<8xf32>/' "$B/case-1-vaddi32.mlir" > "$B/neg-badtype.mlir"
153expect_refused 'malformed (return type f32 vs i32 value)' "$B/neg-badtype.mlir"
154
155# Documented divergence: Omega signed add traps on overflow; the lifted module does not.
156{ cat "$D/vecadd.unit.om"; printf 'fn main to int\nx = vaddi32 int 2147483647 int 1\nret 0\nend\n'; } > "$B/overflow.native.om"
157nice -n 19 "$OMC" < "$B/overflow.native.om" > "$B/overflow.native" 2> "$B/overflow.native.err" || fail 'overflow native compile'
158chmod +x "$B/overflow.native"
159set +e; nice -n 19 "$B/overflow.native" 2> /dev/null; nrc=$?; set -e
160[ "$nrc" != 0 ] || fail 'Omega native signed overflow did not trap'
161ONES=1,1,1,1,1,1,1,1
162MAXS=2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647
163MINS=-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648,-2147483648
164iree_check "$B/case-1-vaddi32.vmfb" i32 "$MINS" "$MAXS" "$ONES" > "$B/overflow.iree.out" 2>&1 || fail 'IREE overflow result was not the wrapped value'
165pjrt "$B/case-1-vaddi32.mlir" i32 8 "$MINS" "$MAXS" "$ONES" > "$B/overflow.pjrt.out" || fail 'PJRT overflow result was not the wrapped value'
166printf 'DIVERGENCE signed overflow: Omega native exit %s (trap); IREE and PJRT return -2147483648 (StableHLO: implementation-defined)\n' "$nrc"
167
168# Translator refusals: exact class on stderr, nonzero status, nothing on stdout.
169refuse() {
170 label=$1 class=$2 input=$3; shift 3
171 set +e; nice -n 19 "$T" "$@" < "$input" > "$B/refuse.out" 2> "$B/refuse.err"; rc=$?; set -e
172 [ "$rc" != 0 ] || fail "refusal $label exited 0"
173 [ ! -s "$B/refuse.out" ] || fail "refusal $label wrote stdout"
174 [ "$(cat "$B/refuse.err")" = "irstablehlo: $class" ] || fail "refusal $label said $(cat "$B/refuse.err")"
175 neg=$((neg+1))
176 printf 'PASS refusal %s -> %s (exit %s)\n' "$label" "$class" "$rc"
177}
178refuse 'integer div' UNSUPPORTED_OP "$B/refuse.ir" 8 divi32
179refuse 'integer shl' UNSUPPORTED_OP "$B/refuse.ir" 8 shlu32
180refuse 'branches' UNSUPPORTED_OP "$B/refuse.ir" 8 maxi32
181refuse 'mixed element types' UNSUPPORTED_TAG "$B/refuse.ir" 8 widen
182refuse 'bool result' UNSUPPORTED_TAG "$B/refuse.ir" 8 isodd
183refuse 'f32 tag in wire v1' UNSUPPORTED_TAG "$B/v1f32.ir" 8 vaddf32
184refuse 'unknown function' NO_FUNCTION "$B/vecadd.ir" 8 nosuch
185refuse 'N zero' USAGE "$B/vecadd.ir" 0 vaddi32
186refuse 'N leading zero' USAGE "$B/vecadd.ir" 08 vaddi32
187refuse 'N above 16777216' USAGE "$B/vecadd.ir" 16777217 vaddi32
188refuse 'N not decimal' USAGE "$B/vecadd.ir" 8x vaddi32
189refuse 'missing function argument' USAGE "$B/vecadd.ir" 8
190{ printf 'X'; tail -c +2 "$B/vecadd.ir"; } > "$B/bad-magic.ir"
191refuse 'bad magic' MAGIC "$B/bad-magic.ir" 8 vaddi32
192{ head -c 7 "$B/vecadd.ir"; bytes 2; tail -c +9 "$B/vecadd.ir"; } > "$B/bad-version.ir"
193refuse 'wire version 2' VERSION "$B/bad-version.ir" 8 vaddi32
194size=$(wc -c < "$B/vecadd.ir")
195head -c $((size-1)) "$B/vecadd.ir" > "$B/truncated.ir"
196refuse 'truncated' TRUNCATED "$B/truncated.ir" 8 vaddi32
197{ cat "$B/vecadd.ir"; bytes 0; } > "$B/trailing.ir"
198refuse 'trailing byte' TRAILING "$B/trailing.ir" 8 vaddi32
199[ "$neg" = 23 ] || fail "expected 23 negative controls and refusals, ran $neg"
200
201sha256sum "$B/irstablehlo" "$B"/case-*.mlir | sed 's/^/SHA256 /'
202printf 'PASS irstablehlo: %s cases on two interop oracles (not Omega-native, not hardware), %s negatives caught\n' "$i" "$neg"