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 / omegallm / v1-oracle.sh

336 lines · 16173 bytes Raw Blame History
1#!/bin/sh
2# Omega LLM v1 referee checks (CONTRACT-V1 §9.2-§9.5): the `ollm` binary against oracle.py.
3#
4# BIN=path/to/ollm sh tests/omegallm/v1-oracle.sh
5#
6# Environment:
7# BIN aarch64 v1 ollm binary; unset = referee outputs only, binary cases NOT_RUN
8# DATA directory holding TinyStoriesV2-GPT4-valid.txt (default: ../../data from here)
9# WORK scratch directory (default: scratch-v1-oracle beside the clone); ref/ and bin/ are rebuilt
10# CORE core for taskset (default 8); every process runs at nice 19
11# MUTANT planted referee defect (oracle.py MUTANTS); the run must then report FAIL lines
12# V0BIN optional v0 ollm binary for the V = 256 CK01 identity case (§9.4)
13# EXT_MERGES EXT_V EXT_TEXT EXT_TOK optional: referee-encode EXT_TEXT with EXT_MERGES at EXT_V
14# and compare with the product's token file EXT_TOK byte for byte
15#
16# One PASS/FAIL/NOT_RUN line per case, then `SUMMARY pass=N fail=M not_run=K`; exit 0 iff fail=0.
17# Tokenizer outputs and init are compared byte for byte; grad and one train step per entry within
18# the v0 tolerance (ORACLE.md "Tolerance"); printed micro integers exactly.
19set -u
20here=$(cd "$(dirname "$0")" && pwd)
21ORACLE="$here/oracle.py"
22DATA=${DATA:-$here/../../data}
23WORK=${WORK:-$here/../../../scratch-v1-oracle}
24CORE=${CORE:-8}
25MUTANT=${MUTANT:-}
26BIN=${BIN:-}
27V0BIN=${V0BIN:-}
28VALID="$DATA/TinyStoriesV2-GPT4-valid.txt"
29RUN="taskset -c $CORE nice -n 19"
30# every helper (cmp, python3 -c, sha256sum) runs on the same core too
31if [ -z "${V1_ORACLE_PINNED:-}" ]; then
32 V1_ORACLE_PINNED=1 exec taskset -c "$CORE" nice -n 19 sh "$0" "$@"
33fi
34export PYTHONDONTWRITEBYTECODE=1
35export OLLM_ORACLE_MUTANT="$MUTANT"
36
37[ -r "$VALID" ] || { echo "v1-oracle: cannot read $VALID (set DATA)" >&2; exit 2; }
38mkdir -p "$WORK"
39WORK=$(cd "$WORK" && pwd)
40REF="$WORK/ref"
41GOT="$WORK/bin"
42rm -rf "$REF" "$GOT" "$WORK/tmp"
43mkdir -p "$REF" "$GOT" "$WORK/tmp"
44export OLLM_TMP="$WORK/tmp"
45if [ -n "$BIN" ]; then
46 BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN")
47 [ -x "$BIN" ] || { echo "v1-oracle: BIN $BIN is not executable" >&2; exit 2; }
48fi
49if [ -n "$V0BIN" ]; then
50 V0BIN=$(cd "$(dirname "$V0BIN")" && pwd)/$(basename "$V0BIN")
51 [ -x "$V0BIN" ] || { echo "v1-oracle: V0BIN $V0BIN is not executable" >&2; exit 2; }
52fi
53echo "v1-oracle: bin=${BIN:-none} mutant=${MUTANT:-none} core=$CORE"
54
55pass=0 fail=0 notrun=0
56ok() { echo "PASS $1${2:+ ($2)}"; pass=$((pass + 1)); }
57ko() { echo "FAIL $1${2:+ ($2)}" | sed "s|$WORK/||g"; fail=$((fail + 1)); }
58nr() { echo "NOT_RUN $1${2:+ ($2)}"; notrun=$((notrun + 1)); }
59
60# r DIR TAG CMD...: run CMD in DIR; stdout -> TAG.out, stderr -> TAG.err, exit code -> TAG.rc
61r() {
62 _d=$1 _t=$2
63 shift 2
64 (cd "$_d" && $RUN "$@" >"$_t.out" 2>"$_t.err"; echo $? >"$_t.rc")
65}
66ref() { _t=$1; shift; r "$REF" "$_t" python3 "$ORACLE" ollm "$@"; }
67got() { _t=$1; shift; [ -n "$BIN" ] && r "$GOT" "$_t" "$BIN" "$@"; return 0; }
68both() { ref "$@"; got "$@"; }
69py() { $RUN python3 "$ORACLE" "$@"; }
70
71# chk NAME TAG RC [FILE|!FILE]...: the referee must exit RC; the binary must match its exit code,
72# stdout bytes and each FILE byte for byte (!FILE: must not exist on either side).
73chk() {
74 _n=$1 _t=$2 _rc=$3
75 shift 3
76 _rr=$(cat "$REF/$_t.rc")
77 [ "$_rr" = "$_rc" ] || { ko "$_n" "referee exit $_rr, want $_rc: $(cat "$REF/$_t.err" | tail -n 1)"; return; }
78 for _f; do
79 case $_f in !*) [ ! -e "$REF/${_f#!}" ] || { ko "$_n" "referee left ${_f#!}"; return; } ;; esac
80 done
81 [ -n "$BIN" ] || { nr "$_n" "no BIN"; return; }
82 _gr=$(cat "$GOT/$_t.rc")
83 [ "$_gr" = "$_rr" ] || { ko "$_n" "exit $_gr, want $_rr"; return; }
84 _d=$(cmp "$GOT/$_t.out" "$REF/$_t.out" 2>&1) || { ko "$_n" "stdout: $_d; got '$(head -c 80 "$GOT/$_t.out")' want '$(head -c 80 "$REF/$_t.out")'"; return; }
85 for _f; do
86 case $_f in
87 !*) [ ! -e "$GOT/${_f#!}" ] || { ko "$_n" "binary left ${_f#!}"; return; } ;;
88 *) _d=$(cmp "$GOT/$_f" "$REF/$_f" 2>&1) || { ko "$_n" "$_f: $_d"; return; } ;;
89 esac
90 done
91 ok "$_n" "exit $_rr${1:+, files checked}"
92}
93
94# tol NAME KIND GOTFILE REFFILE: oracle.py cmp (header exact, payload within rtol)
95tol() {
96 _o=$(py cmp "$2" "$3" "$4" 2>&1)
97 _l=$(printf '%s\n' "$_o" | tail -n 1)
98 case $_l in PASS*) ok "$1" "max relerr $(printf '%s\n' "$_o" | grep 'max relerr' | awk '{printf "%s=%s ", $2, $5}')" ;;
99 *) ko "$1" "$(printf '%s\n' "$_o" | tr '\n' ' ')" ;; esac
100}
101
102wr() { python3 -c "import sys,struct; open(sys.argv[1],'wb').write($2)" "$1"; }
103
104# ---------------------------------------------------------------- referee self-test
105o=$(py selftest-v1 2>&1)
106case $(printf '%s\n' "$o" | tail -n 1) in
107*PASS*) ok referee-selftest-v1 ;;
108*) ko referee-selftest-v1 "$(printf '%s\n' "$o" | grep FAIL | tr '\n' ';')" ;;
109esac
110
111# ---------------------------------------------------------------- tokenizer (§2, §9.2)
112both prep prep "$VALID" train.bin held.bin
113chk prep prep 0 train.bin held.bin
114both fit512 bpefit train.bin merges512.bin 512
115chk fit512 fit512 0 merges512.bin
116both fit300mb bpefit train.bin m300mb.bin 300 1000000
117chk fit300-maxbytes fit300mb 0 m300mb.bin
118for d in "$REF" "$GOT"; do
119 wr "$d/tie.txt" "b'ab cd ef\\0ab cd ef\\0'"
120 wr "$d/no0.txt" "b'abc def ghi'"
121 wr "$d/edge.txt" "b' x'+b'A'*150+b' '+'caf\\u00e9 na\\u00efve \\u65e5\\u672c'.encode()+b'!!? 9z 3.14\\t\\r\\n\\0'+b' the end\\0'*3"
122done
123both fittie bpefit tie.txt tie.bin 261
124chk fit-tie fittie 0 tie.bin
125# known answer, independent of the binary: pairs (32,99) (32,101) (97,98) (256,100) (257,102)
126want=$(python3 -c "import struct; b=struct.pack('<10H',32,99,32,101,97,98,256,100,257,102); print(b.hex())")
127have=$(python3 -c "import sys; print(open(sys.argv[1],'rb').read()[24:].hex())" "$REF/tie.bin" 2>/dev/null)
128[ "$want" = "$have" ] && ok fit-tie-kat "smallest a, then smallest b" || ko fit-tie-kat "referee merges $have, want $want"
129both fittie28 bpefit tie.txt tie28.bin 262
130chk fit-out-of-pairs fittie28 28 '!tie28.bin'
131both fit21 bpefit no0.txt no0.bin 300 5
132chk fit-maxbytes-no-0 fit21 21 '!no0.bin'
133both fitedge bpefit edge.txt edge.bin 280
134chk fit-edge fitedge 0 edge.bin
135both enc512 bpeenc merges512.bin 512 train.bin train512.tok
136chk enc512-train enc512 0 train512.tok
137both enc512h bpeenc merges512.bin 512 held.bin held512.tok
138chk enc512-held enc512h 0 held512.tok
139both enc300 bpeenc merges512.bin 300 train.bin train300.tok
140chk enc300-train enc300 0 train300.tok
141both enc300h bpeenc merges512.bin 300 held.bin held300.tok
142chk enc300-held enc300h 0 held300.tok
143both encedge bpeenc edge.bin 280 edge.txt edge.tok
144chk enc-edge encedge 0 edge.tok
145both dec512 bpedec merges512.bin train512.tok train.dec
146chk dec512-train dec512 0 train.dec
147for d in "$REF" "$GOT"; do [ -e "$d/train.dec" ] && ! cmp -s "$d/train.dec" "$d/train.bin" && echo " decode != text in $d"; done
148if cmp -s "$REF/train.dec" "$REF/train.bin" && { [ -z "$BIN" ] || cmp -s "$GOT/train.dec" "$GOT/train.bin"; }; then
149 ok roundtrip "decode(encode(train)) == train"
150else ko roundtrip "decode(encode(train)) != train"; fi
151# negative control (§9.2): one pair altered (merge 100 -> (101, 32)), file rehashed; tokens change
152for d in "$REF" "$GOT"; do
153 python3 -c "
154import sys, struct
155sys.path.insert(0, sys.argv[3]); import oracle
156raw = bytearray(open(sys.argv[1], 'rb').read())
157struct.pack_into('<2H', raw, 24 + 4 * 100, 101, 32)
158struct.pack_into('<Q', raw, 16, oracle.fnv1a64(bytes(raw[24:])))
159open(sys.argv[2], 'wb').write(raw)" "$REF/merges512.bin" "$d/alt512.bin" "$here"
160done
161both encalt bpeenc alt512.bin 512 held.bin heldalt.tok
162chk enc-altered-pair encalt 0 heldalt.tok
163if cmp -s "$REF/heldalt.tok" "$REF/held512.tok"; then ko altered-pair-changes-tokens "referee tokens unchanged"
164elif [ -n "$BIN" ] && cmp -s "$GOT/heldalt.tok" "$GOT/held512.tok"; then ko altered-pair-changes-tokens "binary tokens unchanged"
165else ok altered-pair-changes-tokens "$(cmp "$REF/heldalt.tok" "$REF/held512.tok" | sed 's/.*differ: //')"; fi
166# refusals
167for d in "$REF" "$GOT"; do
168 python3 -c "import sys; b=bytearray(open(sys.argv[1],'rb').read()); b[30]^=1; open(sys.argv[2],'wb').write(b)" "$REF/merges512.bin" "$d/badhash.bin"
169 head -c 101 "$REF/held512.tok" >"$d/odd.tok"
170 wr "$d/big.tok" "struct.pack('<4H', 72, 105, 600, 33)"
171done
172both r26 bpeenc badhash.bin 512 held.bin r26.tok
173chk merges-hash-26 r26 26 '!r26.tok'
174both r27 bpeenc merges512.bin 513 held.bin r27.tok
175chk v-over-merges-27 r27 27 '!r27.tok'
176both r27b bpeenc merges512.bin 256 held.bin r27b.tok
177chk bpeenc-v256-27 r27b 27 '!r27b.tok'
178both r21 bpedec merges512.bin odd.tok odd.dec
179chk odd-token-file-21 r21 21
180both r25 bpedec merges512.bin big.tok big.dec
181chk token-over-v-25 r25 25
182if [ -n "${EXT_MERGES:-}" ]; then
183 ref ext bpeenc "$EXT_MERGES" "$EXT_V" "$EXT_TEXT" ext.tok
184 if [ "$(cat "$REF/ext.rc")" != 0 ]; then ko ext-enc "referee exit $(cat "$REF/ext.rc")"
185 elif d=$(cmp "$EXT_TOK" "$REF/ext.tok" 2>&1); then ok ext-enc "$(cat "$REF/ext.out")"
186 else ko ext-enc "$d"; fi
187fi
188
189# ---------------------------------------------------------------- model (§4-§7, §9.3-§9.5)
190TR="$REF/train300.tok"
191HE="$REF/held300.tok"
192M5="$REF/merges512.bin"
193CFG="300 8 16 2 32 2 4 5 50 3000"
194both init2 init init2.ck $CFG 2
195chk init-ck02-g2 init2 0 init2.ck
196both init1 init init1.ck $CFG
197chk init-ck01 init1 0 init1.ck
198both init4 init init4.ck $CFG 4
199chk init-ck02-g4 init4 0 init4.ck
200both initg3 init g3.ck $CFG 3
201chk init-g3-12 initg3 12 '!g3.ck'
202# shape limits (CONTRACT-V2 §1, CONTRACT-V3 §1): one over each maximum is refused with 12; D and F
203# at their v2 maxima and L at its v3 maximum init byte-identically
204for s in "4097 8 16 2 32 2 4" "300 257 16 2 32 2 4" "300 8 386 2 32 2 4" "300 8 34 17 32 2 4" \
205 "300 8 16 2 1537 2 4" "300 8 16 2 32 17 4" "300 8 16 2 32 2 65"; do
206 both lim init lim.ck $s 5 50 3000
207 chk "limit-12 [$s]" lim 12 '!lim.ck'
208done
209both limx init limx.ck 300 8 384 12 1536 2 4 5 50 3000
210chk "limit-max-d384-f1536" limx 0 limx.ck
211both liml init liml.ck 300 8 16 2 32 16 4 5 50 3000
212chk "limit-max-l16" liml 0 liml.ck
213# the whole v3 maximum (P 29,995,392; 719,889,528-byte checkpoint, deleted after the compare)
214both limv3 init limv3.ck 4096 256 384 12 1536 16 64 1 30000 3000 16
215chk "limit-max-v3" limv3 0 limv3.ck
216rm -f "$REF/limv3.ck" "$GOT/limv3.ck"
217for d in "$REF" "$GOT"; do wr "$d/offs" "struct.pack('<4Q', 0, 777777, 5000000, 13000000)"; done
218for g in 2 4; do
219 both grad$g grad init$g.ck "$TR" offs g$g.gr
220 if [ "$(cat "$REF/grad$g.rc")" != 0 ]; then ko "grad-g$g" "referee exit $(cat "$REF/grad$g.rc")"
221 elif [ -z "$BIN" ]; then nr "grad-g$g" "no BIN"
222 elif [ "$(cat "$GOT/grad$g.rc")" != 0 ]; then ko "grad-g$g" "exit $(cat "$GOT/grad$g.rc")"
223 else
224 o=$(py cmp gline "$GOT/grad$g.out" "$REF/grad$g.out")
225 case $o in PASS*) tol "grad-g$g" gr "$GOT/g$g.gr" "$REF/g$g.gr" ;; *) ko "grad-g$g" "$o" ;; esac
226 fi
227done
228# referee-only run (deliverable): 20 steps EVERY 5 from the referee's init, eval and samples
229cp "$REF/init2.ck" "$REF/ref20.ck"
230ref tr20 train ref20.ck "$TR" 20 5
231ref ev20 eval ref20.ck "$HE" 4 "$M5"
232ref s0 sample ref20.ck 7 40 0 "Once upon a time" "$M5"
233ref s800 sample ref20.ck 7 40 800 "Once upon a time" "$M5"
234for t in tr20 ev20 s0 s800; do
235 [ "$(cat "$REF/$t.rc")" = 0 ] || ko "referee-$t" "exit $(cat "$REF/$t.rc")"
236done
237# §9.3: one train step per call from the binary's own checkpoints
238if [ -z "$BIN" ]; then nr train-chain-20 "no BIN"
239else
240 cp "$GOT/init2.ck" "$GOT/c0.ck"
241 bad=
242 s=1
243 while [ $s -le 20 ]; do
244 cp "$GOT/c$((s - 1)).ck" "$GOT/c$s.ck"
245 cp "$GOT/c$((s - 1)).ck" "$REF/c$s.ck"
246 both ts$s train c$s.ck "$TR" $s 1
247 if [ "$(cat "$GOT/ts$s.rc")" != 0 ] || ! cmp -s "$GOT/ts$s.out" "$REF/ts$s.out"; then
248 bad="step $s: got '$(cat "$GOT/ts$s.out")' rc $(cat "$GOT/ts$s.rc"), want '$(cat "$REF/ts$s.out")'"
249 break
250 fi
251 o=$(py cmp ck "$GOT/c$s.ck" "$REF/c$s.ck")
252 case $(printf '%s\n' "$o" | tail -n 1) in PASS*) ;; *) bad="step $s: $(printf '%s\n' "$o" | tr '\n' ' ')"; break ;; esac
253 s=$((s + 1))
254 done
255 [ -z "$bad" ] && ok train-chain-20 "T lines exact, ck within rtol at every step" || ko train-chain-20 "$bad"
256fi
257# §9.4 workers: one process, 20 steps, K = 1 and K = 2
258if [ -z "$BIN" ]; then nr workers-k1-k2 "no BIN"; nr workers-tlines "no BIN"
259else
260 cp "$GOT/init2.ck" "$GOT/k1.ck"
261 cp "$GOT/init2.ck" "$GOT/k2.ck"
262 got trk1 train k1.ck "$TR" 20 5 1
263 got trk2 train k2.ck "$TR" 20 5 2
264 if [ "$(cat "$GOT/trk1.rc")$(cat "$GOT/trk2.rc")" != 00 ]; then ko workers-k1-k2 "exit $(cat "$GOT/trk1.rc") $(cat "$GOT/trk2.rc")"
265 elif d=$(cmp "$GOT/k2.ck" "$GOT/k1.ck" 2>&1) && d=$(cmp "$GOT/k1.ck" "$GOT/c20.ck" 2>&1); then
266 ok workers-k1-k2 "K=1, K=2 and the 20-call chain byte-identical"
267 else ko workers-k1-k2 "$d"; fi
268 if cmp -s "$GOT/trk1.out" "$REF/tr20.out" && cmp -s "$GOT/trk2.out" "$REF/tr20.out"; then
269 ok workers-tlines "T lines = referee"
270 else ko workers-tlines "got '$(cat "$GOT/trk1.out" | tr '\n' ';')' want '$(cat "$REF/tr20.out" | tr '\n' ';')'"; fi
271fi
272both k3 train init2.ck "$TR" 1 1 3
273chk bad-k3-29 k3 29
274both k0 train init2.ck "$TR" 1 1 0
275chk bad-k0-29 k0 29
276# eval and sample on the binary's 20-step checkpoint (the referee's when no BIN)
277if [ -n "$BIN" ]; then cp "$GOT/c20.ck" "$GOT/b20.ck"; cp "$GOT/c20.ck" "$REF/b20.ck"
278else cp "$REF/ref20.ck" "$REF/b20.ck"; fi
279both evb eval b20.ck "$HE" 4 "$M5"
280chk eval-w4-bpb evb 0
281both evnom eval b20.ck "$HE" 4
282chk eval-no-merges-2 evnom 2
283both evw eval b20.ck "$HE" 999999 "$M5"
284chk eval-w-23 evw 23
285both sb0 sample b20.ck 7 40 0 "Once upon a time" "$M5"
286chk sample-t0 sb0 0
287both sb800 sample b20.ck 7 40 800 "Once upon a time" "$M5"
288chk sample-t800 sb800 0
289both snom sample b20.ck 7 40 0 "Once"
290chk sample-no-merges-2 snom 2
291# data refusals (tokens checked lazily: only rows actually read)
292for d in "$REF" "$GOT"; do
293 head -c 4001 "$TR" >"$d/odd300.tok"
294 head -c 16 "$TR" >"$d/short.tok"
295 wr "$d/bad.tok" "struct.pack('<200H', *[400 if i == 100 else 5 + i % 200 for i in range(200)])"
296 wr "$d/offs-bad" "struct.pack('<4Q', 0, 10, 20, 95)"
297 wr "$d/offs-good" "struct.pack('<4Q', 0, 10, 20, 30)"
298 wr "$d/offs-22" "struct.pack('<4Q', 0, 10, 20, 192)"
299 wr "$d/offs-21" "struct.pack('<3Q', 0, 10, 20)"
300done
301both d21 train init2.ck odd300.tok 1 1
302chk data-odd-21 d21 21
303both d20 train init2.ck short.tok 1 1
304chk data-short-20 d20 20
305both d25 grad init2.ck bad.tok offs-bad d25.gr
306chk data-token-25 d25 25 '!d25.gr'
307both dlazy grad init2.ck bad.tok offs-good dlazy.gr
308if [ "$(cat "$REF/dlazy.rc")" != 0 ]; then ko data-lazy "referee exit $(cat "$REF/dlazy.rc")"
309elif [ -z "$BIN" ]; then nr data-lazy "no BIN"
310elif [ "$(cat "$GOT/dlazy.rc")" != 0 ]; then ko data-lazy "exit $(cat "$GOT/dlazy.rc")"
311else tol data-lazy gr "$GOT/dlazy.gr" "$REF/dlazy.gr"; fi
312both o22 grad init2.ck bad.tok offs-22 o22.gr
313chk offs-22 o22 22
314both o21 grad init2.ck bad.tok offs-21 o21.gr
315chk offs-size-21 o21 21
316# §9.4 V = 256: a CK01 trained by the v1 binary equals the v0 binary's after 20 steps
317if [ -z "$BIN" ] || [ -z "$V0BIN" ]; then nr v0-v1-ck01 "needs BIN and V0BIN"
318else
319 r "$GOT" v0i "$V0BIN" init v0.ck 256 8 16 2 32 2 2 5 50 3000
320 got v1i init v1.ck 256 8 16 2 32 2 2 5 50 3000
321 if ! d=$(cmp "$GOT/v1.ck" "$GOT/v0.ck" 2>&1); then ko v0-v1-ck01 "init: $d"
322 else
323 r "$GOT" v0t "$V0BIN" train v0.ck "$REF/held.bin" 20 5
324 got v1t train v1.ck "$REF/held.bin" 20 5
325 if d=$(cmp "$GOT/v1.ck" "$GOT/v0.ck" 2>&1) && cmp -s "$GOT/v1t.out" "$GOT/v0t.out"; then
326 ok v0-v1-ck01 "init and 20 steps byte-identical, T lines equal"
327 else ko v0-v1-ck01 "${d:-T lines differ}"; fi
328 fi
329fi
330
331# referee outputs and their hashes
332(cd "$REF" && sha256sum prep.out train.bin held.bin fit512.out merges512.bin train512.tok held512.tok \
333 train300.tok held300.tok m300mb.bin tie.bin edge.bin edge.tok init2.ck init1.ck init4.ck \
334 offs grad2.out g2.gr grad4.out g4.gr tr20.out ref20.ck ev20.out s0.out s800.out >SHA256SUMS 2>/dev/null)
335echo "SUMMARY pass=$pass fail=$fail not_run=$notrun"
336[ "$fail" = 0 ]