interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / tests / adtrain / replay / run-replay.sh

252 lines · 14119 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: AGPL-3.0-only
3# DRA-14 independent replay of the bounded native autodiff-to-training path:
4# checkpoint refusals with valid checksums, one-check-removed load mutants,
5# independent semantic mutants of the native tape and of the trainer, and the
6# negative controls of the suite's golden gates (mutants-golden.txt).
7# OMC=<pinned omc 62775d2a...> OMEGA=<omega-mv checkout> OUT=<new empty dir> \
8# sh tests/adtrain/replay/run-replay.sh
9# Host glue only (concatenation, copies, comparisons); the mutant injection
10# (subst.om), the image forging (ckforge.om) and everything measured is .om.
11# Prints one line per case to stdout and $OUT/replay.txt. Last line:
12# REPLAY_PASS ... (exit 0) every refusal and load-bearing case as declared
13# REPLAY_FAIL ... (exit 1) otherwise, or any invalid mutant setup
14# Mutants that no existing gate kills are reported as GAP lines; they are
15# findings about the suites, not failures of this replay.
16set -u
17ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
18HERE=$ROOT/tests/adtrain/replay
19: "${OMC:?set OMC to the pinned seed/out/omc}"
20: "${OMEGA:?set OMEGA to an omega-mv checkout}"
21: "${OUT:?set OUT to a new empty directory}"
22mkdir -p "$OUT"
23[ -z "$(ls -A "$OUT")" ] || { echo "REPLAY_FAIL OUT not empty"; exit 1; }
24OUT=$(cd "$OUT" && pwd)
25DEPS=$ROOT/docs/adtrain/dependencies.json
26EV=$ROOT/evidence/ADTRAIN
27mkdir -p "$OUT/deps" "$OUT/b" "$OUT/w" "$OUT/m"
28REP=$OUT/replay.txt
29: > "$REP"
30NFAIL=0; NGAP=0; NINV=0
31say() { echo "$*" | tee -a "$REP"; }
32bad() { NFAIL=$((NFAIL + 1)); say "$* FAIL"; }
33
34# 0. Pinned compiler and dependencies, as tests/adtrain/run-adtrain.sh does.
35want=$(jq -r .compiler.binary_sha256 "$DEPS")
36have=$(sha256sum "$OMC" | cut -d' ' -f1)
37[ "$have" = "$want" ] || { say "REPLAY_FAIL compiler-sha256 $have"; exit 1; }
38say "omc $have"
39n=$(jq '.dependencies | length' "$DEPS")
40i=0
41while [ "$i" -lt "$n" ]; do
42 commit=$(jq -r ".dependencies[$i].commit" "$DEPS")
43 path=$(jq -r ".dependencies[$i].path" "$DEPS")
44 sha=$(jq -r ".dependencies[$i].sha256" "$DEPS")
45 git -C "$OMEGA" show "$commit:$path" > "$OUT/deps/$i.om" 2> /dev/null || { say "REPLAY_FAIL dependency-missing $commit:$path"; exit 1; }
46 echo "$sha $OUT/deps/$i.om" | sha256sum -c --status || { say "REPLAY_FAIL dependency-sha256 $path"; exit 1; }
47 say "dep $commit $path $sha"
48 i=$((i + 1))
49done
50META=$OUT/deps/0.om
51TAPE=$OUT/deps/1.om
52TCOMMIT=$(jq -r '.dependencies[1].commit' "$DEPS")
53git -C "$OMEGA" show "$TCOMMIT:tests/autodiff664/tape-test.om" > "$OUT/deps/tape-test.om" || { say "REPLAY_FAIL tape-test missing"; exit 1; }
54say "dep $TCOMMIT tests/autodiff664/tape-test.om $(sha256sum "$OUT/deps/tape-test.om" | cut -d' ' -f1)"
55MLP=$ROOT/native/adtrain/mlp.om
56MAIN=$ROOT/tests/adtrain/main.om
57
58build() { # $1 unit.om $2 out.elf ; rc of the compiler, no stale output kept
59 "$OMC" < "$1" > "$2" 2> "$2.stderr"
60 r=$?
61 if [ "$r" != 0 ] || [ "$(od -An -tx1 -N4 "$2" | tr -d ' ')" != 7f454c46 ]; then
62 : > "$2"; return 1
63 fi
64 chmod 700 "$2"
65}
66runm() { # $1 elf $2 mode $3 tag ; prints rc; runs in $OUT/w
67 (cd "$OUT/w" && printf '%s' "$2" | "$1" > "$OUT/b/$3.stdout" 2> "$OUT/b/$3.stderr")
68 echo $?
69}
70mut() { # $1 source $2 pattern $3 replacement $4 out ; exact-once substitution
71 { printf '%b' "$2"; printf '\0'; printf '%b' "$3"; printf '\0'; cat "$1"; } | "$OUT/subst.elf" > "$4"
72}
73tailof() { awk '($1=="L"&&$2>=120)||$1=="P"' "$1"; }
74rtail() { awk '$1=="L"||$1=="P"' "$1"; }
75
76build "$HERE/subst.om" "$OUT/subst.elf" || { say "REPLAY_FAIL subst compile"; exit 1; }
77build "$HERE/ckforge.om" "$OUT/ckforge.elf" || { say "REPLAY_FAIL ckforge compile"; exit 1; }
78cat "$META" "$TAPE" "$MLP" "$MAIN" > "$OUT/ref.om"
79build "$OUT/ref.om" "$OUT/ref.elf" || { say "REPLAY_FAIL reference compile"; exit 1; }
80E=$OUT/ref.elf
81CK=$OUT/w/adtrain.ck
82
83# 1. Reference runs and the golden receipts of PR #42.
84[ "$(runm "$E" T ref-T)" = 0 ] || { say "REPLAY_FAIL reference T"; exit 1; }
85[ "$(runm "$E" S ref-S)" = 0 ] || { say "REPLAY_FAIL reference S"; exit 1; }
86cp "$CK" "$OUT/good.ck"
87[ "$(runm "$E" G ref-G)" = 0 ] || { say "REPLAY_FAIL reference G"; exit 1; }
88tailof "$OUT/b/ref-T.stdout" > "$OUT/ref-T.tail"
89for pair in "b/ref-T.stdout:run/T.stdout" "good.ck:frozen/adtrain.ck.good" "b/ref-G.stdout:run/G.stdout"; do
90 a=${pair%%:*}; g=${pair#*:}
91 if [ -f "$EV/$g" ] && cmp -s "$OUT/$a" "$EV/$g"; then say "GOLDEN $a == evidence/ADTRAIN/$g PASS"
92 else bad "GOLDEN $a vs evidence/ADTRAIN/$g"; fi
93done
94# The suite's golden.json must name exactly these receipts and their sha256.
95GJ=$ROOT/docs/adtrain/golden.json
96for gate in golden-T-stdout golden-ck120 golden-G-stdout; do
97 ev=$(jq -r --arg g "$gate" '[.receipts[] | select(.gate == $g)] | if length == 1 then .[0].evidence else "" end' "$GJ")
98 gs=$(jq -r --arg g "$gate" '[.receipts[] | select(.gate == $g)] | if length == 1 then .[0].sha256 else "" end' "$GJ")
99 if [ -n "$ev" ] && [ -f "$ROOT/$ev" ] && [ "$(sha256sum "$ROOT/$ev" | cut -d' ' -f1)" = "$gs" ]; then
100 say "GOLDEN-PIN $gate == sha256($ev) $gs PASS"
101 else bad "GOLDEN-PIN $gate ($ev $gs)"; fi
102done
103[ "$(jq '.receipts | length' "$GJ")" = 3 ] && say "GOLDEN-PIN golden.json has 3 receipts PASS" || bad "GOLDEN-PIN receipt count"
104
105# 2. Checkpoint refusals: every structural check reached with a valid checksum.
106forge() { # $1 mode $2 out
107 { printf '%s' "$1"; cat "$OUT/good.ck"; } | "$OUT/ckforge.elf" > "$2"
108}
109image() { # $1 case -> $CK ; returns 1 when the forger fails
110 case "$1" in
111 size537) { cat "$OUT/good.ck"; printf '\0'; } > "$CK" ;;
112 size0) : > "$CK" ;;
113 missing) mv "$CK" "$OUT/w/gone.ck" 2> /dev/null; [ ! -e "$CK" ] ;;
114 size535) head -c 535 "$OUT/good.ck" > "$CK" ;;
115 *) forge "$1" "$CK" ;;
116 esac
117}
118forge 0 "$OUT/f0.ck" && cmp -s "$OUT/f0.ck" "$OUT/good.ck" \
119 && say "CKFORGE mode 0 reproduces the image byte for byte (FNV-1a 64 agrees) PASS" \
120 || bad "CKFORGE mode 0 identity"
121for spec in 0:0 S:0 m:65 v:65 s:65 c:65 l:65 p:65 u:65 n:65 i:65 x:65 h:66 \
122 size537:65 size0:65 size535:65 missing:65; do
123 c=${spec%%:*}; e=${spec#*:}
124 if ! image "$c"; then NINV=$((NINV + 1)); say "CK $c INVALID (forger)"; continue; fi
125 cp "$CK" "$OUT/b/ck-$c.img" 2> /dev/null
126 r=$(runm "$E" R "ck-$c")
127 if [ "$r" = "$e" ]; then say "CK $c expect=$e rc=$r PASS"; else bad "CK $c expect=$e rc=$r"; fi
128 if [ "$c" = 0 ] && [ "$r" = 0 ]; then
129 rtail "$OUT/b/ck-0.stdout" > "$OUT/ck0.tail"
130 cmp -s "$OUT/ck0.tail" "$OUT/ref-T.tail" && say "CK 0 resumed tail == uninterrupted T PASS" || bad "CK 0 resumed tail"
131 fi
132done
133
134# 3. One checkpoint check removed at a time: the matching image must stop
135# giving the refusal code, the good image must still load.
136grep -v '^#' "$HERE/mutants-ck.txt" | while IFS=' ' read -r id pat rep img code; do
137 [ -n "$id" ] || continue
138 src=$OUT/m/$id.mlp.om
139 mut "$MLP" "$pat" "$rep" "$src"; r=$?
140 if [ "$r" != 0 ]; then echo "CKMUT $id INVALID subst=$r" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
141 cat "$META" "$TAPE" "$src" "$MAIN" > "$OUT/m/$id.unit.om"
142 if ! build "$OUT/m/$id.unit.om" "$OUT/m/$id.elf"; then echo "CKMUT $id INVALID compile" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
143 cp "$OUT/good.ck" "$CK"
144 g=$(runm "$OUT/m/$id.elf" R "ckmut-$id-good")
145 image "$img" || { echo "CKMUT $id INVALID image" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; }
146 f=$(runm "$OUT/m/$id.elf" R "ckmut-$id-$img")
147 if [ "$g" = 0 ] && [ "$f" != "$code" ]; then
148 echo "CKMUT $id image=$img good_rc=$g forged_rc=$f (was $code) LOADBEARING PASS" | tee -a "$REP"
149 else
150 echo "CKMUT $id image=$img good_rc=$g forged_rc=$f (was $code) NOT_LOADBEARING FAIL" | tee -a "$REP"; echo x >> "$OUT/fail"
151 fi
152done
153
154# 4. Independent tape mutants: omega-mv tape-test.om, and the adtrain T stdout.
155grep -v '^#' "$HERE/mutants-tape.txt" | while IFS=' ' read -r id pat rep; do
156 [ -n "$id" ] || continue
157 src=$OUT/m/$id.tape.om
158 mut "$TAPE" "$pat" "$rep" "$src"; r=$?
159 if [ "$r" != 0 ]; then echo "TAPEMUT $id INVALID subst=$r" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
160 cat "$META" "$src" "$OUT/deps/tape-test.om" > "$OUT/m/$id.tt.om"
161 if ! build "$OUT/m/$id.tt.om" "$OUT/m/$id.tt.elf"; then echo "TAPEMUT $id INVALID compile" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
162 (cd "$OUT/w" && "$OUT/m/$id.tt.elf" > "$OUT/b/tt-$id.stdout" 2> "$OUT/b/tt-$id.stderr")
163 t=$?
164 cat "$META" "$src" "$MLP" "$MAIN" > "$OUT/m/$id.ad.om"
165 if ! build "$OUT/m/$id.ad.om" "$OUT/m/$id.ad.elf"; then echo "TAPEMUT $id INVALID adtrain-compile" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
166 a=$(runm "$OUT/m/$id.ad.elf" T "ad-$id")
167 if [ "$a" = 0 ] && cmp -s "$OUT/b/ad-$id.stdout" "$OUT/b/ref-T.stdout"; then d=SAME; else d=DIFFER; fi
168 if [ "$t" != 0 ]; then k=KILLED; else k=SURVIVED; fi
169 line="TAPEMUT $id tape_test_rc=$t $k $(head -n 1 "$OUT/b/tt-$id.stdout" | cut -c1-60) adtrain_T_rc=$a $d"
170 if [ "$t" = 0 ]; then line="$line GAP"; echo x >> "$OUT/gap"; fi
171 echo "$line" | tee -a "$REP"
172done
173
174# 4b. Is t-e1-pad-pos0 an equivalent mutant? padprobe.om sums four -0 leaves.
175cat "$META" "$TAPE" "$HERE/padprobe.om" > "$OUT/m/padprobe.om"
176pm=$OUT/m/t-e1-pad-pos0.tape.om
177if build "$OUT/m/padprobe.om" "$OUT/m/padprobe.elf" && [ -s "$pm" ] \
178 && cat "$META" "$pm" "$HERE/padprobe.om" > "$OUT/m/padprobe-mut.om" \
179 && build "$OUT/m/padprobe-mut.om" "$OUT/m/padprobe-mut.elf"; then
180 "$OUT/m/padprobe.elf"; po=$?
181 "$OUT/m/padprobe-mut.elf"; pq=$?
182 if [ "$po" = 11 ]; then say "PADPROBE pinned tape: sum of four -0 is -0 (rc=$po) PASS"; else bad "PADPROBE pinned tape rc=$po"; fi
183 case "$pq" in
184 10) say "PADPROBE t-e1-pad-pos0: sum of four -0 is +0 (rc=$pq): not equivalent, a real GAP of tape-test.om" ;;
185 11) say "PADPROBE t-e1-pad-pos0: sum of four -0 is -0 (rc=$pq): equivalent on this probe" ;;
186 *) bad "PADPROBE t-e1-pad-pos0 rc=$pq" ;;
187 esac
188else
189 NINV=$((NINV + 1)); say "PADPROBE INVALID build"
190fi
191
192# 5. Independent trainer mutants: the full adtrain suite on a copy of the tree
193# with the mutant mlp.om, plus the golden receipts of PR #42.
194grep -v '^#' "$HERE/mutants-mlp.txt" | while IFS=' ' read -r id pat rep; do
195 [ -n "$id" ] || continue
196 tree=$OUT/m/tree-$id
197 mkdir -p "$tree/docs" "$tree/native" "$tree/tests"
198 cp -R "$ROOT/docs/adtrain" "$tree/docs/" && cp -R "$ROOT/native/adtrain" "$tree/native/" \
199 && cp -R "$ROOT/tests/adtrain" "$tree/tests/" || { echo "MLPMUT $id INVALID copy" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; }
200 mut "$MLP" "$pat" "$rep" "$tree/native/adtrain/mlp.om"; r=$?
201 if [ "$r" != 0 ]; then echo "MLPMUT $id INVALID subst=$r" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
202 (cd "$tree" && OMC=$OMC OMEGA=$OMEGA OUT=$OUT/m/suite-$id sh tests/adtrain/run-adtrain.sh) > "$OUT/b/suite-$id.log" 2>&1
203 s=$(tail -n 1 "$OUT/b/suite-$id.log")
204 # A failure of the suite's own mutant injection (its pattern no longer
205 # matches the mutated mlp.om) is a setup collision, never a kill.
206 case "$s" in ADTRAIN_SUITE_PASS) k=SURVIVED ;; *-inject) k=UNKNOWN ;; ADTRAIN_SUITE_FAIL*) k=KILLED ;; *) k=UNKNOWN ;; esac
207 if [ "$k" = UNKNOWN ]; then echo "MLPMUT $id INVALID suite-output ($s)" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
208 cat "$META" "$TAPE" "$tree/native/adtrain/mlp.om" "$MAIN" > "$OUT/m/$id.om"
209 if ! build "$OUT/m/$id.om" "$OUT/m/$id.elf"; then gt=COMPILE_REFUSED; gc=COMPILE_REFUSED; gg=COMPILE_REFUSED
210 else
211 a=$(runm "$OUT/m/$id.elf" T "mlp-$id-T")
212 if [ "$a" = 0 ] && cmp -s "$OUT/b/mlp-$id-T.stdout" "$EV/run/T.stdout"; then gt=SAME; else gt="DIFFER(rc=$a)"; fi
213 b=$(runm "$OUT/m/$id.elf" S "mlp-$id-S")
214 if [ "$b" = 0 ] && cmp -s "$CK" "$EV/frozen/adtrain.ck.good"; then gc=SAME; else gc="DIFFER(rc=$b)"; fi
215 c=$(runm "$OUT/m/$id.elf" G "mlp-$id-G")
216 if [ "$c" = 0 ] && cmp -s "$OUT/b/mlp-$id-G.stdout" "$EV/run/G.stdout"; then gg=SAME; else gg="DIFFER(rc=$c)"; fi
217 fi
218 line="MLPMUT $id suite=$k ($s) golden_T=$gt golden_ck=$gc golden_G=$gg"
219 if [ "$k" = SURVIVED ]; then line="$line GAP"; echo x >> "$OUT/gap"; fi
220 echo "$line" | tee -a "$REP"
221done
222
223# 6. Negative controls of the golden gates and of dgeps: the suite on a copy of
224# the tree with one declared edit must end with the declared line.
225grep -v '^#' "$HERE/mutants-golden.txt" | while IFS=' ' read -r id f pat rep want pat2 rep2; do
226 [ -n "$id" ] || continue
227 tree=$OUT/m/gtree-$id
228 mkdir -p "$tree/docs" "$tree/native" "$tree/tests"
229 cp -R "$ROOT/docs/adtrain" "$tree/docs/" && cp -R "$ROOT/native/adtrain" "$tree/native/" \
230 && cp -R "$ROOT/tests/adtrain" "$tree/tests/" || { echo "GOLDCTL $id INVALID copy" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; }
231 mut "$ROOT/$f" "$pat" "$rep" "$tree/$f"; r=$?
232 if [ "$r" = 0 ] && [ -n "$pat2" ]; then
233 cp "$tree/$f" "$OUT/m/$id.first"
234 mut "$OUT/m/$id.first" "$pat2" "$rep2" "$tree/$f"; r=$?
235 fi
236 if [ "$r" != 0 ]; then echo "GOLDCTL $id INVALID subst=$r" | tee -a "$REP"; echo x >> "$OUT/invalid"; continue; fi
237 (cd "$tree" && OMC=$OMC OMEGA=$OMEGA OUT=$OUT/m/gsuite-$id sh tests/adtrain/run-adtrain.sh) > "$OUT/b/gsuite-$id.log" 2>&1
238 s=$(tail -n 1 "$OUT/b/gsuite-$id.log")
239 case "$s" in
240 "$want"*) echo "GOLDCTL $id ($s) PASS" | tee -a "$REP" ;;
241 *) echo "GOLDCTL $id want=($want) got=($s) FAIL" | tee -a "$REP"; echo x >> "$OUT/fail" ;;
242 esac
243done
244
245[ -f "$OUT/fail" ] && NFAIL=$((NFAIL + $(wc -l < "$OUT/fail")))
246[ -f "$OUT/invalid" ] && NINV=$((NINV + $(wc -l < "$OUT/invalid")))
247[ -f "$OUT/gap" ] && NGAP=$(wc -l < "$OUT/gap")
248(cd "$OUT" && sha256sum ref.om ref.elf good.ck subst.elf ckforge.elf deps/* b/*.stdout b/*.img) > "$OUT/artifacts.sha256" 2> /dev/null
249if [ "$NFAIL" = 0 ] && [ "$NINV" = 0 ]; then
250 say "REPLAY_PASS fail=0 invalid=0 gap=$NGAP"; exit 0
251fi
252say "REPLAY_FAIL fail=$NFAIL invalid=$NINV gap=$NGAP"; exit 1