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 / nki / gate-irnki.sh

229 lines · 10432 bytes Raw Blame History
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.
11set -eu
12cd "$(dirname "$0")/../.."
13B=build/seed/nki
14mkdir -p "$B"
15find "$B" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
16: > "$B/gate.log"
17say() { printf '%s\n' "$*" | tee -a "$B/gate.log"; }
18fail() { say "FAIL $*"; exit 1; }
19notrun() { say "NOT_RUN $*"; exit 3; }
20[ "$(uname -m)" = aarch64 ] || notrun 'requires AArch64 Linux (seed/out/omc emits AArch64)'
21OMC=${OMC:-seed/out/omc}
22pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991
23[ -f "$OMC" ] || fail "compiler $OMC missing"
24[ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs'
25D=${NKI_ORACLE_DIR:-}
26[ -n "$D" ] || notrun 'NKI_ORACLE_DIR unset; prepare it with seed/nki/setup-oracle.sh DIR'
27for 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"
29done
30D=$(cd "$D" && pwd)
31TARGETS=${NKI_TARGETS:-trn1 trn2 trn3}
32oracle() {
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}
40version=$(oracle trn3 version 2> "$B/oracle-version.err") ||
41 { cat "$B/oracle-version.err"; notrun 'NKI simulator does not start'; }
42want='nki 0.7.0+37713544100.gfede04bb numpy 2.2.6 python 3.12.16'
43[ "$version" = "$want" ] || notrun "oracle is '$version', pinned '$want'"
44say "SOURCE $(git rev-parse HEAD)"
45say "COMPILER $OMC sha256=$pin"
46say "ORACLE interop oracle, not Omega-native: nki.simulate ($version) targets=$TARGETS"
47say "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)"
48say "ORACLE_DRIVER oracle.py sha256=$(sha256sum seed/nki/oracle.py | cut -d ' ' -f 1)"
49say "PLATFORM $(uname -srm)"
50nice -n 19 "$OMC" < seed/nki/irnki.om > "$B/irnki" 2> "$B/compile.err"
51[ ! -s "$B/compile.err" ] || fail 'emitter compile stderr'
52chmod +x "$B/irnki"
53# lift UNIT LANE OUT: IR v1 bytes of a unit, then the NKI module, emitted twice.
54lift() {
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.
69ref() {
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
81note gate driver: stdin is 262144 a lanes then 262144 b lanes (LE u32); stdout the results.
82global inb uchar 2097152
83global outb uchar 1048576
84global tailb uchar 1
85fn rdall to void
86n = ulong 0
87for t ulong 2097152
88if eq n len inb
89break
90end
91k = read 0 inb n sub len inb n
92if le k 0
93exit 91
94end
95n = add n ulong k
96end
97if ne n len inb
98exit 91
99end
100k = read 0 tailb 0 1
101if ne k 0
102exit 92
103end
104end
105fn lane o ulong to uint
106v = ulong 0
107for j ulong 4
108v = or v shl ulong get inb add o j mul j 8
109end
110ret uint v
111end
112fn main to int
113rdall
114for i ulong 262144
115a = lane mul i 4
116b = lane add 1048576 mul i 4
117$call
118for j ulong 4
119put outb add mul i 4 j uchar and shr r mul j 8 255
120end
121end
122p = ulong 0
123for t ulong 1048577
124if eq p len outb
125break
126end
127k = write 1 outb p sub len outb p
128if le k 0
129exit 93
130end
131p = add p ulong k
132end
133ret 0
134end
135EOF
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.
145check() {
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}
152for 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)"
155done
156lift vecadd float32 vecadd-f32
157lift vecadd uint32 vecadd-u32
158lift ops uint32 ops-u32
159grep -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'
161ref vecadd float32 vecadd-f32
162ref vecadd uint32 vecadd-u32
163ref ops uint32 ops-u32
164names=vecadd
165check vecadd-f32 float32 vecadd-f32.ref || { cat "$B"/vecadd-f32.*.log; fail 'vecadd float32 differs from native Omega'; }
166check vecadd-u32 uint32 vecadd-u32.ref || { cat "$B"/vecadd-u32.*.log; fail 'vecadd uint32 differs from native Omega'; }
167names=$(sed -n 's/^fn \([A-Za-z0-9_]*\) .*/\1/p' seed/nki/ops.unit.om | tr '\n' ' ')
168check ops-u32 uint32 ops-u32.ref || { cat "$B"/ops-u32.*.log; fail 'ops uint32 differ from native Omega'; }
169oracle trn3 limit "$B/vecadd-f32.py" vecadd > "$B/limit.log" 2>&1 || { cat "$B/limit.log"; fail 'kernel accepted a shape outside 128x512 tiles'; }
170say "CONTROL limit $(sed -n 1p "$B/limit.log")"
171
172# Negative controls: each must make the oracle comparison FAIL.
173names=vecadd
174ALL=$TARGETS
175TARGETS=trn3
176neg() {
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
181sed 's/op=nl.add)/op=nl.subtract)/' "$B/vecadd-f32.py" > "$B/mut-text.py"
182cmp -s "$B/vecadd-f32.py" "$B/mut-text.py" && fail 'text mutant unchanged'
183neg 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'
186cp "$B/vecadd.ir" "$B/mut-ir.ir"
187printf '\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"
189neg mut-ir float32 vecadd-f32.ref
190# (c) one expected lane flipped by one bit
191mkdir -p "$B/mut-ref"
192cp "$B/vecadd-f32.ref/vecadd.bin" "$B/mut-ref/vecadd.bin"
193b0=$(od -An -j 4000 -N 1 -t u1 "$B/mut-ref/vecadd.bin" | tr -d ' ')
194printf "\\$(printf '%03o' $((b0 ^ 1)))" | dd of="$B/mut-ref/vecadd.bin" bs=1 seek=4000 conv=notrunc 2> /dev/null
195cp "$B/vecadd-f32.py" "$B/mut-ref.py"
196neg mut-ref float32 mut-ref
197# (d) a kernel set that drops a function
198sed '/^ "vecadd": /d' "$B/vecadd-f32.py" > "$B/mut-set.py"
199neg mut-set float32 vecadd-f32.ref
200# Refusals: exact exit code, the error text on stderr and nothing on stdout.
201refuse() {
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}
212unit() { { printf 'ir\n'; printf '%s\n' "$@"; } | nice -n 19 "$OMC" > "$B/r.ir" || fail "unit $1 IR"; }
213cp "$B/vecadd.ir" "$B/r.ir"; refuse 30 usage-extra-arg f32 x
214cp "$B/ops.ir" "$B/r.ir"; refuse 24 f32-xor f32
215cp "$B/vecadd.ir" "$B/r.ir"; refuse 30 usage-bad-lane f64
216unit 'fn q a uint b uint to uint' 'ret div a b' 'end'; refuse 24 div
217unit 'fn q a uint b uint to uint' 'ret shl a b' 'end'; refuse 24 shl
218unit 'fn q a int b int to int' 'ret add a b' 'end'; refuse 25 int-tag
219unit 'fn q a ulong b ulong to ulong' 'ret add a b' 'end'; refuse 25 ulong-tag
220unit 'fn q a uint b uint to uint' 'ret add mul a b a' 'end'; refuse 29 two-ops
221unit 'fn q a uint to uint' 'ret add a a' 'end'; refuse 29 one-param
222printf 'OSC1IR\000\002\001\000' > "$B/r.ir"; refuse 21 version
223printf 'OSC2IR\000\001\001\000' > "$B/r.ir"; refuse 20 magic
224head -c 30 "$B/vecadd.ir" > "$B/r.ir"; refuse 22 truncated
225{ cat "$B/vecadd.ir"; printf '\000'; } > "$B/r.ir"; refuse 27 trailing
226printf 'OSC1IR\000\001\000\000' > "$B/r.ir"; refuse 26 no-functions
227say "HARDWARE NOT_RUN: no Trainium/Inferentia instance; simulator results are not hardware qualification"
228say "PASS irnki: kernels from Omega equal native Omega lanes on nki.simulate targets=$ALL; controls on trn3"
229sha256sum seed/nki/irnki.om seed/nki/oracle.py seed/nki/*.unit.om "$B/irnki" "$B"/*.py "$B"/*.ir | tee -a "$B/gate.log"