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 / run-adtrain.sh

159 lines · 7908 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: AGPL-3.0-only
3# adtrain suite: native tape training example, gates 1-4 and the host arm of 5,
4# plus the golden receipts of PR #42 (docs/adtrain/golden.json: T trajectory,
5# step-120 checkpoint, G rows), so a self-consistent but different trainer fails.
6# OMC=<pinned seed/out/omc> OMEGA=<omega-mv checkout> OUT=<scratch dir> sh tests/adtrain/run-adtrain.sh
7# OMEGA must contain the commits pinned in docs/adtrain/dependencies.json; the
8# fixture host arm runs `make test-autodiff-fixture` in OMEGA as checked out.
9# Host glue only (concatenation, file copies, byte flips, comparisons); every
10# computation is in .om. Writes each rc/stdout/stderr under $OUT/run.
11# Last line ADTRAIN_SUITE_PASS (exit 0) or ADTRAIN_SUITE_FAIL <gate> (exit 1).
12set -u
13ROOT=$(cd "$(dirname "$0")/../.." && pwd)
14: "${OMC:?set OMC to the pinned seed/out/omc}"
15: "${OMEGA:?set OMEGA to an omega-mv checkout}"
16: "${OUT:?set OUT to a scratch directory}"
17DEPS=$ROOT/docs/adtrain/dependencies.json
18mkdir -p "$OUT/deps" "$OUT/run" "$OUT/work" "$OUT/mutants"
19OUT=$(cd "$OUT" && pwd)
20fail() { echo "ADTRAIN_SUITE_FAIL $1"; exit 1; }
21
22# 1. Pinned compiler and dependency files from their exact Git objects.
23want=$(jq -r .compiler.binary_sha256 "$DEPS")
24have=$(sha256sum "$OMC" | cut -d' ' -f1)
25echo "$have omc" > "$OUT/deps/omc.sha256"
26[ "$have" = "$want" ] || fail "compiler-sha256 ($have)"
27: > "$OUT/deps/files"
28n=$(jq '.dependencies | length' "$DEPS")
29i=0
30while [ "$i" -lt "$n" ]; do
31 commit=$(jq -r ".dependencies[$i].commit" "$DEPS")
32 path=$(jq -r ".dependencies[$i].path" "$DEPS")
33 sha=$(jq -r ".dependencies[$i].sha256" "$DEPS")
34 dst=$OUT/deps/$i-$(basename "$path")
35 git -C "$OMEGA" show "$commit:$path" > "$dst" 2> "$OUT/deps/$i.stderr" || fail "dependency-missing $commit:$path"
36 echo "$sha $dst" | sha256sum -c --status || fail "dependency-sha256 $path"
37 echo "$dst" >> "$OUT/deps/files"
38 i=$((i + 1))
39done
40DEPFILES=$(cat "$OUT/deps/files")
41
42build() { # $1 unit.om $2 out.elf $3 tag
43 "$OMC" < "$1" > "$2" 2> "$OUT/run/$3.compile.stderr"
44 echo $? > "$OUT/run/$3.compile.rc"
45 [ "$(cat "$OUT/run/$3.compile.rc")" = 0 ] || return 1
46 [ "$(od -An -tx1 -N4 "$2" | tr -d ' ')" = 7f454c46 ] || return 1
47 chmod 700 "$2"
48}
49# run TAG ELF MODE: run one driver mode inside $OUT/work.
50run() {
51 (cd "$OUT/work" && printf '%s' "$3" | "$2" > "$OUT/run/$1.stdout" 2> "$OUT/run/$1.stderr")
52 echo $? > "$OUT/run/$1.rc"
53 cat "$OUT/run/$1.rc"
54}
55# golden GATE FILE: FILE must hash to the sha256 recorded for GATE in
56# docs/adtrain/golden.json (PR #42's receipts); a missing entry also fails.
57GOLD=$ROOT/docs/adtrain/golden.json
58golden() {
59 g=$(jq -r --arg g "$1" '[.receipts[] | select(.gate == $g)] | if length == 1 then .[0].sha256 else "" end' "$GOLD")
60 [ -n "$g" ] || fail "$1 (no single golden.json entry)"
61 h=$(sha256sum "$2" | cut -d' ' -f1)
62 echo "$1 want=$g have=$h" >> "$OUT/run/golden.txt"
63 [ "$h" = "$g" ] || fail "$1 ($h)"
64}
65: > "$OUT/run/golden.txt"
66flip() { # flip FILE OFFSET: xor one byte with 1
67 b=$(od -An -tu1 -j"$2" -N1 "$1" | tr -d ' ')
68 printf "\\$(printf %o $((b ^ 1)))" | dd of="$1" bs=1 seek="$2" conv=notrunc 2> /dev/null
69}
70
71# shellcheck disable=SC2086
72cat $DEPFILES "$ROOT/native/adtrain/mlp.om" "$ROOT/tests/adtrain/main.om" > "$OUT/unit.om"
73build "$OUT/unit.om" "$OUT/unit.elf" unit || fail compile
74E=$OUT/unit.elf
75CK=$OUT/work/adtrain.ck
76
77# 2. Gates 1 and 3: forward, loss, updates that reduce loss (T).
78[ "$(run T "$E" T)" = 0 ] || fail train
79grep -qx ADTRAIN_T_PASS "$OUT/run/T.stdout" || fail train-output
80[ "$(grep -c '^L ' "$OUT/run/T.stdout")" = 201 ] || fail train-L-rows
81[ "$(grep -c '^P ' "$OUT/run/T.stdout")" = 58 ] || fail train-P-rows
82l0=$(awk '$1=="L"&&$2==0{print $3}' "$OUT/run/T.stdout")
83l100=$(awk '$1=="L"&&$2==100{print $3}' "$OUT/run/T.stdout")
84l200=$(awk '$1=="L"&&$2==200{print $3}' "$OUT/run/T.stdout")
85# loss >= 0, so binary32 bit patterns order like the values
86[ "$l200" -lt "$l0" ] && [ "$l200" -lt "$l100" ] || fail loss-gate
87echo "L0=$l0 L100=$l100 L200=$l200" > "$OUT/run/loss-gate.txt"
88# Golden trajectory: every L and P row bit for bit as recorded by PR #42.
89golden golden-T-stdout "$OUT/run/T.stdout"
90
91# 3. Gate 2: tape gradients against central differences (G).
92[ "$(run G "$E" G)" = 0 ] || fail gradcheck
93ok=$(grep -c '^GC [0-9]* [0-9]* 1$' "$OUT/run/G.stdout")
94bad=$(grep -c '^GC [0-9]* [0-9]* 0$' "$OUT/run/G.stdout")
95[ "$ok" -ge 50 ] && [ "$bad" = 0 ] || fail "gradcheck-rows ok=$ok bad=$bad"
96golden golden-G-stdout "$OUT/run/G.stdout"
97
98# 4. Fixture for the C oracle (D).
99[ "$(run D "$E" D)" = 0 ] || fail fixture
100[ "$(wc -c < "$OUT/work/adtrain.fix")" = 748 ] || fail fixture-size
101cp "$OUT/work/adtrain.fix" "$OUT/adtrain.fix"
102
103# 5. Gate 4: interrupted at 120, resumed to 200, identical to uninterrupted.
104[ "$(run S "$E" S)" = 0 ] || fail save
105[ "$(wc -c < "$CK")" = 536 ] || fail checkpoint-size
106cp "$CK" "$OUT/adtrain.ck.good"
107# Golden checkpoint: the step-120 image byte for byte as recorded by PR #42.
108golden golden-ck120 "$OUT/adtrain.ck.good"
109[ "$(run R "$E" R)" = 0 ] || fail resume
110grep -qx ADTRAIN_R_PASS "$OUT/run/R.stdout" || fail resume-output
111awk '($1=="L"&&$2>=120)||$1=="P"' "$OUT/run/T.stdout" > "$OUT/run/T.tail"
112awk '$1=="L"||$1=="P"' "$OUT/run/R.stdout" > "$OUT/run/R.tail"
113[ "$(grep -c '^L ' "$OUT/run/R.tail")" = 81 ] || fail resume-L-rows
114cmp -s "$OUT/run/T.tail" "$OUT/run/R.tail" || fail resume-identity
115
116# 6. Checkpoint refusals: other data (66), truncated (65), flipped bytes (65).
117[ "$(run Z "$E" Z)" = 0 ] || fail save-other-data
118cp "$CK" "$OUT/adtrain.ck.seed29"
119[ "$(run R-binding "$E" R)" = 66 ] || fail binding-refusal
120head -c 535 "$OUT/adtrain.ck.good" > "$CK"
121[ "$(run R-truncated "$E" R)" = 65 ] || fail truncation-refusal
122cp "$OUT/adtrain.ck.good" "$OUT/adtrain.ck.flip96"
123flip "$OUT/adtrain.ck.flip96" 96
124cp "$OUT/adtrain.ck.flip96" "$CK"
125[ "$(run R-flip96 "$E" R)" = 65 ] || fail checksum-refusal
126cp "$OUT/adtrain.ck.good" "$CK"
127flip "$CK" 100
128[ "$(run R-flip100 "$E" R)" = 65 ] || fail padding-refusal
129
130# 7. Compiled mutants of mlp.om; each must be caught.
131build "$ROOT/tests/adtrain/mutate.om" "$OUT/mutate.elf" mutate || fail mutate-compile
132for mode in 0 1 2; do
133 { printf '%s' "$mode"; cat "$ROOT/native/adtrain/mlp.om"; } | "$OUT/mutate.elf" > "$OUT/mutants/mlp$mode.om"
134 echo $? > "$OUT/mutants/mlp$mode.mutate.rc"
135 [ "$(cat "$OUT/mutants/mlp$mode.mutate.rc")" = 0 ] || fail "mutant$mode-inject"
136 # shellcheck disable=SC2086
137 cat $DEPFILES "$OUT/mutants/mlp$mode.om" "$ROOT/tests/adtrain/main.om" > "$OUT/mutants/unit$mode.om"
138 build "$OUT/mutants/unit$mode.om" "$OUT/mutants/unit$mode.elf" "mutant$mode" || fail "mutant$mode-compile"
139done
140rc=$(run mutant0-T "$OUT/mutants/unit0.elf" T)
141[ "$rc" = 86 ] || [ "$rc" = 88 ] || fail "mutant0-survived rc=$rc"
142cp "$OUT/adtrain.ck.seed29" "$CK"
143[ "$(run mutant1-R-binding "$OUT/mutants/unit1.elf" R)" = 0 ] || fail mutant1-not-load-bearing
144cp "$OUT/adtrain.ck.flip96" "$CK"
145[ "$(run mutant2-R-flip96 "$OUT/mutants/unit2.elf" R)" = 0 ] || fail mutant2-not-load-bearing
146
147# 8. Gate 5 host arm: the fixture replayed on the C M21 tape (CPU table).
148git -C "$OMEGA" rev-parse HEAD > "$OUT/run/fixture-host.omega-head"
149make -s -C "$OMEGA" test-autodiff-fixture FIXTURE="$OUT/adtrain.fix" \
150 > "$OUT/run/fixture-host.stdout" 2> "$OUT/run/fixture-host.stderr"
151echo $? > "$OUT/run/fixture-host.rc"
152[ "$(cat "$OUT/run/fixture-host.rc")" = 0 ] || fail fixture-host
153grep -Eq '^AD_FIXTURE_HOST: exact=[0-9]+/75 max_ulp=[0-9]+ verdict=(PASS|PASS_TOL)$' \
154 "$OUT/run/fixture-host.stdout" || fail fixture-host-verdict
155grep -q '^AD_FIXTURE_HOST: .* verdict=FAIL (mutant-sign)$' "$OUT/run/fixture-host.stdout" \
156 || fail fixture-host-control
157
158(cd "$OUT" && sha256sum unit.om unit.elf adtrain.fix adtrain.ck.good run/*.stdout) > "$OUT/artifacts.sha256"
159echo ADTRAIN_SUITE_PASS