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
omega-mv / seed / nki / gate-irnki.sh
| 1 | #!/bin/sh |
| 2 | # Host-only verification of seed/nki/irnki.om, the OSC IR v1 -> AWS NKI (nki.isa) emitter. |
| 3 | # The NKI CPU simulator (nki.simulate, nki 0.7.0, Apache-2.0) is an interop oracle, not |
| 4 | # Omega-native, and never generates kernels: irnki.om writes every kernel and a native |
| 5 | # Omega program computes every expected lane. Simulator results are not hardware |
| 6 | # qualification; the Trainium/Inferentia hardware receipt stays NOT_RUN. |
| 7 | # NKI_ORACLE_DIR names a directory prepared by seed/nki/setup-oracle.sh (x86_64 CPython + |
| 8 | # nki wheel run under qemu-user, because the nki wheel is published for x86_64 only). |
| 9 | # Optional: OMC, NKI_TARGETS (default "trn1 trn2 trn3"). |
| 10 | # Exit 0 PASS, 1 FAIL, 3 NOT_RUN. Every artifact stays under build/seed/nki. |
| 11 | set -eu |
| 12 | cd "$(dirname "$0")/../.." |
| 13 | B=build/seed/nki |
| 14 | mkdir -p "$B" |
| 15 | find "$B" -mindepth 1 -maxdepth 1 -exec rm -rf {} + |
| 16 | : > "$B/gate.log" |
| 17 | say() { printf '%s\n' "$*" | tee -a "$B/gate.log"; } |
| 18 | fail() { say "FAIL $*"; exit 1; } |
| 19 | notrun() { say "NOT_RUN $*"; exit 3; } |
| 20 | [ "$(uname -m)" = aarch64 ] || notrun 'requires AArch64 Linux (seed/out/omc emits AArch64)' |
| 21 | OMC=${OMC:-seed/out/omc} |
| 22 | pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991 |
| 23 | [ -f "$OMC" ] || fail "compiler $OMC missing" |
| 24 | [ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs' |
| 25 | D=${NKI_ORACLE_DIR:-} |
| 26 | [ -n "$D" ] || notrun 'NKI_ORACLE_DIR unset; prepare it with seed/nki/setup-oracle.sh DIR' |
| 27 | for f in qemu/usr/bin/qemu-x86_64 python/bin/python3.12 sysroot/usr/lib/x86_64-linux-gnu/libc.so.6 site/nki/__init__.py; do |
| 28 | [ -e "$D/$f" ] || notrun "oracle file $D/$f missing; rerun seed/nki/setup-oracle.sh" |
| 29 | done |
| 30 | D=$(cd "$D" && pwd) |
| 31 | TARGETS=${NKI_TARGETS:-trn1 trn2 trn3} |
| 32 | oracle() { |
| 33 | target=$1 |
| 34 | shift |
| 35 | env -i PATH=/usr/bin:/bin PYTHONPATH="$D/site" NEURON_LOGICAL_NC_CONFIG=1 \ |
| 36 | NEURON_PLATFORM_TARGET_OVERRIDE="$target" \ |
| 37 | nice -n 19 "$D/qemu/usr/bin/qemu-x86_64" -L "$D/sysroot" \ |
| 38 | "$D/python/bin/python3.12" -s -S seed/nki/oracle.py "$@" |
| 39 | } |
| 40 | version=$(oracle trn3 version 2> "$B/oracle-version.err") || |
| 41 | { cat "$B/oracle-version.err"; notrun 'NKI simulator does not start'; } |
| 42 | want='nki 0.7.0+37713544100.gfede04bb numpy 2.2.6 python 3.12.16' |
| 43 | [ "$version" = "$want" ] || notrun "oracle is '$version', pinned '$want'" |
| 44 | say "SOURCE $(git rev-parse HEAD)" |
| 45 | say "COMPILER $OMC sha256=$pin" |
| 46 | say "ORACLE interop oracle, not Omega-native: nki.simulate ($version) targets=$TARGETS" |
| 47 | say "ORACLE_HOST qemu-x86_64 $("$D/qemu/usr/bin/qemu-x86_64" --version | sed -n 1p) sha256=$(sha256sum "$D/qemu/usr/bin/qemu-x86_64" | cut -d ' ' -f 1)" |
| 48 | say "ORACLE_DRIVER oracle.py sha256=$(sha256sum seed/nki/oracle.py | cut -d ' ' -f 1)" |
| 49 | say "PLATFORM $(uname -srm)" |
| 50 | nice -n 19 "$OMC" < seed/nki/irnki.om > "$B/irnki" 2> "$B/compile.err" |
| 51 | [ ! -s "$B/compile.err" ] || fail 'emitter compile stderr' |
| 52 | chmod +x "$B/irnki" |
| 53 | # lift UNIT LANE OUT: IR v1 bytes of a unit, then the NKI module, emitted twice. |
| 54 | lift() { |
| 55 | { printf 'ir\n'; cat "seed/nki/$1.unit.om"; } | nice -n 19 "$OMC" > "$B/$1.ir" 2> "$B/$1.ir.err" |
| 56 | [ ! -s "$B/$1.ir.err" ] || fail "$1 IR stderr" |
| 57 | if [ "$2" = float32 ]; then mode=f32; else mode=; fi |
| 58 | # shellcheck disable=SC2086 # empty mode means no argument |
| 59 | "$B/irnki" $mode < "$B/$1.ir" > "$B/$3.py" 2> "$B/$3.err" || fail "irnki $1 $2 exit $?" |
| 60 | # shellcheck disable=SC2086 |
| 61 | "$B/irnki" $mode < "$B/$1.ir" > "$B/$3.again.py" |
| 62 | cmp -s "$B/$3.py" "$B/$3.again.py" || fail "$3 emission is not deterministic" |
| 63 | [ ! -s "$B/$3.err" ] || fail "$3 emitter stderr" |
| 64 | say "EMIT $3 from $1.unit.om lane=$2 deterministic sha256=$(sha256sum "$B/$3.py" | cut -d ' ' -f 1)" |
| 65 | } |
| 66 | # ref UNIT LANE OUT: one native AArch64 Omega program per function computes the expected |
| 67 | # lanes from the same unit source. float32 rereads the unit with uint as f32, the same |
| 68 | # retyping irnki applies to the lanes. |
| 69 | ref() { |
| 70 | mkdir -p "$B/$3.ref" |
| 71 | for name in $(sed -n 's/^fn \([A-Za-z0-9_]*\) .*/\1/p' "seed/nki/$1.unit.om"); do |
| 72 | p=$B/$3.ref/$name |
| 73 | if [ "$2" = float32 ]; then |
| 74 | sed '/^fn /s/ uint/ f32/g' "seed/nki/$1.unit.om" > "$p.om" |
| 75 | call="r = ulong bitcast uint $name bitcast f32 a bitcast f32 b" |
| 76 | else |
| 77 | cat "seed/nki/$1.unit.om" > "$p.om" |
| 78 | call="r = ulong $name a b" |
| 79 | fi |
| 80 | cat >> "$p.om" <<EOF |
| 81 | note gate driver: stdin is 262144 a lanes then 262144 b lanes (LE u32); stdout the results. |
| 82 | global inb uchar 2097152 |
| 83 | global outb uchar 1048576 |
| 84 | global tailb uchar 1 |
| 85 | fn rdall to void |
| 86 | n = ulong 0 |
| 87 | for t ulong 2097152 |
| 88 | if eq n len inb |
| 89 | break |
| 90 | end |
| 91 | k = read 0 inb n sub len inb n |
| 92 | if le k 0 |
| 93 | exit 91 |
| 94 | end |
| 95 | n = add n ulong k |
| 96 | end |
| 97 | if ne n len inb |
| 98 | exit 91 |
| 99 | end |
| 100 | k = read 0 tailb 0 1 |
| 101 | if ne k 0 |
| 102 | exit 92 |
| 103 | end |
| 104 | end |
| 105 | fn lane o ulong to uint |
| 106 | v = ulong 0 |
| 107 | for j ulong 4 |
| 108 | v = or v shl ulong get inb add o j mul j 8 |
| 109 | end |
| 110 | ret uint v |
| 111 | end |
| 112 | fn main to int |
| 113 | rdall |
| 114 | for i ulong 262144 |
| 115 | a = lane mul i 4 |
| 116 | b = lane add 1048576 mul i 4 |
| 117 | $call |
| 118 | for j ulong 4 |
| 119 | put outb add mul i 4 j uchar and shr r mul j 8 255 |
| 120 | end |
| 121 | end |
| 122 | p = ulong 0 |
| 123 | for t ulong 1048577 |
| 124 | if eq p len outb |
| 125 | break |
| 126 | end |
| 127 | k = write 1 outb p sub len outb p |
| 128 | if le k 0 |
| 129 | exit 93 |
| 130 | end |
| 131 | p = add p ulong k |
| 132 | end |
| 133 | ret 0 |
| 134 | end |
| 135 | EOF |
| 136 | nice -n 19 "$OMC" < "$p.om" > "$p.bin.exe" 2> "$p.err" || { cat "$p.err"; fail "native reference $name compile"; } |
| 137 | [ ! -s "$p.err" ] || { cat "$p.err"; fail "native reference $name compile stderr"; } |
| 138 | chmod +x "$p.bin.exe" |
| 139 | nice -n 19 "$p.bin.exe" < "$B/$2.in" > "$B/$3.ref/$name.bin" || fail "native reference $name exit $?" |
| 140 | done |
| 141 | names=$(sed -n 's/^fn \([A-Za-z0-9_]*\) .*/\1/p' "seed/nki/$1.unit.om" | tr '\n' ' ') |
| 142 | say "REFERENCE $3 native Omega lanes for: $names" |
| 143 | } |
| 144 | # check ID LANE EXPECTREF: run every kernel of ID.py on every target. |
| 145 | check() { |
| 146 | for t in $TARGETS; do |
| 147 | # shellcheck disable=SC2086 # names are [A-Za-z0-9_] words |
| 148 | oracle "$t" check "$B/$1.py" "$B/$2.in" "$B/$3" $names > "$B/$1.$t.log" 2>&1 || return 1 |
| 149 | tee -a "$B/gate.log" < "$B/$1.$t.log" |
| 150 | done |
| 151 | } |
| 152 | for lane in uint32 float32; do |
| 153 | oracle trn3 inputs "$lane" > "$B/$lane.in" 2> "$B/$lane.in.err" || { cat "$B/$lane.in.err"; fail "inputs $lane"; } |
| 154 | say "INPUT $lane 2x262144 lanes sha256=$(sha256sum "$B/$lane.in" | cut -d ' ' -f 1)" |
| 155 | done |
| 156 | lift vecadd float32 vecadd-f32 |
| 157 | lift vecadd uint32 vecadd-u32 |
| 158 | lift ops uint32 ops-u32 |
| 159 | grep -q '^ nisa.tensor_tensor(dst=tc, data1=ta, data2=tb, op=nl.add)$' "$B/vecadd-f32.py" || |
| 160 | fail 'vecadd kernel does not use nisa.tensor_tensor add' |
| 161 | ref vecadd float32 vecadd-f32 |
| 162 | ref vecadd uint32 vecadd-u32 |
| 163 | ref ops uint32 ops-u32 |
| 164 | names=vecadd |
| 165 | check vecadd-f32 float32 vecadd-f32.ref || { cat "$B"/vecadd-f32.*.log; fail 'vecadd float32 differs from native Omega'; } |
| 166 | check vecadd-u32 uint32 vecadd-u32.ref || { cat "$B"/vecadd-u32.*.log; fail 'vecadd uint32 differs from native Omega'; } |
| 167 | names=$(sed -n 's/^fn \([A-Za-z0-9_]*\) .*/\1/p' seed/nki/ops.unit.om | tr '\n' ' ') |
| 168 | check ops-u32 uint32 ops-u32.ref || { cat "$B"/ops-u32.*.log; fail 'ops uint32 differ from native Omega'; } |
| 169 | oracle trn3 limit "$B/vecadd-f32.py" vecadd > "$B/limit.log" 2>&1 || { cat "$B/limit.log"; fail 'kernel accepted a shape outside 128x512 tiles'; } |
| 170 | say "CONTROL limit $(sed -n 1p "$B/limit.log")" |
| 171 | |
| 172 | # Negative controls: each must make the oracle comparison FAIL. |
| 173 | names=vecadd |
| 174 | ALL=$TARGETS |
| 175 | TARGETS=trn3 |
| 176 | neg() { |
| 177 | if check "$1" "$2" "$3"; then fail "negative control $1 passed"; fi |
| 178 | say "CONTROL $1 rejected: $(grep -m 1 '^FAIL\|oracle:' "$B/$1.trn3.log")" |
| 179 | } |
| 180 | # (a) the emitted kernel text changed from add to subtract |
| 181 | sed 's/op=nl.add)/op=nl.subtract)/' "$B/vecadd-f32.py" > "$B/mut-text.py" |
| 182 | cmp -s "$B/vecadd-f32.py" "$B/mut-text.py" && fail 'text mutant unchanged' |
| 183 | neg mut-text float32 vecadd-f32.ref |
| 184 | # (b) the IR BIN subcode changed from add (1) to sub (2) before lifting |
| 185 | [ "$(od -An -j 38 -N 2 -t u1 "$B/vecadd.ir" | tr -s ' ')" = ' 3 1' ] || fail 'vecadd IR BIN is not at offset 38' |
| 186 | cp "$B/vecadd.ir" "$B/mut-ir.ir" |
| 187 | printf '\002' | dd of="$B/mut-ir.ir" bs=1 seek=39 conv=notrunc 2> /dev/null |
| 188 | "$B/irnki" f32 < "$B/mut-ir.ir" > "$B/mut-ir.py" |
| 189 | neg mut-ir float32 vecadd-f32.ref |
| 190 | # (c) one expected lane flipped by one bit |
| 191 | mkdir -p "$B/mut-ref" |
| 192 | cp "$B/vecadd-f32.ref/vecadd.bin" "$B/mut-ref/vecadd.bin" |
| 193 | b0=$(od -An -j 4000 -N 1 -t u1 "$B/mut-ref/vecadd.bin" | tr -d ' ') |
| 194 | printf "\\$(printf '%03o' $((b0 ^ 1)))" | dd of="$B/mut-ref/vecadd.bin" bs=1 seek=4000 conv=notrunc 2> /dev/null |
| 195 | cp "$B/vecadd-f32.py" "$B/mut-ref.py" |
| 196 | neg mut-ref float32 mut-ref |
| 197 | # (d) a kernel set that drops a function |
| 198 | sed '/^ "vecadd": /d' "$B/vecadd-f32.py" > "$B/mut-set.py" |
| 199 | neg mut-set float32 vecadd-f32.ref |
| 200 | # Refusals: exact exit code, the error text on stderr and nothing on stdout. |
| 201 | refuse() { |
| 202 | want=$1; label=$2; shift 2 |
| 203 | set +e |
| 204 | "$B/irnki" "$@" < "$B/r.ir" > "$B/r.out" 2> "$B/r.err" |
| 205 | got=$? |
| 206 | set -e |
| 207 | [ "$got" = "$want" ] || fail "refusal $label exit $got, expected $want" |
| 208 | [ ! -s "$B/r.out" ] || fail "refusal $label wrote stdout" |
| 209 | grep -q '^irnki: ' "$B/r.err" || fail "refusal $label without message" |
| 210 | say "REFUSE $label exit $got $(cat "$B/r.err")" |
| 211 | } |
| 212 | unit() { { printf 'ir\n'; printf '%s\n' "$@"; } | nice -n 19 "$OMC" > "$B/r.ir" || fail "unit $1 IR"; } |
| 213 | cp "$B/vecadd.ir" "$B/r.ir"; refuse 30 usage-extra-arg f32 x |
| 214 | cp "$B/ops.ir" "$B/r.ir"; refuse 24 f32-xor f32 |
| 215 | cp "$B/vecadd.ir" "$B/r.ir"; refuse 30 usage-bad-lane f64 |
| 216 | unit 'fn q a uint b uint to uint' 'ret div a b' 'end'; refuse 24 div |
| 217 | unit 'fn q a uint b uint to uint' 'ret shl a b' 'end'; refuse 24 shl |
| 218 | unit 'fn q a int b int to int' 'ret add a b' 'end'; refuse 25 int-tag |
| 219 | unit 'fn q a ulong b ulong to ulong' 'ret add a b' 'end'; refuse 25 ulong-tag |
| 220 | unit 'fn q a uint b uint to uint' 'ret add mul a b a' 'end'; refuse 29 two-ops |
| 221 | unit 'fn q a uint to uint' 'ret add a a' 'end'; refuse 29 one-param |
| 222 | printf 'OSC1IR\000\002\001\000' > "$B/r.ir"; refuse 21 version |
| 223 | printf 'OSC2IR\000\001\001\000' > "$B/r.ir"; refuse 20 magic |
| 224 | head -c 30 "$B/vecadd.ir" > "$B/r.ir"; refuse 22 truncated |
| 225 | { cat "$B/vecadd.ir"; printf '\000'; } > "$B/r.ir"; refuse 27 trailing |
| 226 | printf 'OSC1IR\000\001\000\000' > "$B/r.ir"; refuse 26 no-functions |
| 227 | say "HARDWARE NOT_RUN: no Trainium/Inferentia instance; simulator results are not hardware qualification" |
| 228 | say "PASS irnki: kernels from Omega equal native Omega lanes on nki.simulate targets=$ALL; controls on trn3" |
| 229 | sha256sum seed/nki/irnki.om seed/nki/oracle.py seed/nki/*.unit.om "$B/irnki" "$B"/*.py "$B"/*.ir | tee -a "$B/gate.log" |