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 / tok / check.sh

209 lines · 11897 bytes Raw Blame History
1#!/bin/sh
2# Own BPE tokenizer check (tests/omegallm/tok/FORMAT.md): ollm-tok toktrain, tokenc, tokdec and
3# tokinfo against the test oracle tests/omegallm/tok/oracle.py (Python, verification only) on
4# synthetic texts and fixtures, exact round trips on random bytes and Unicode, determinism, every
5# exit code, and refusal of corrupted merges files. Needs no data directory; runs in about a minute.
6#
7# BIN=path/to/ollm-tok sh tests/omegallm/tok/check.sh
8#
9# Environment: BIN (required, from tests/omegallm/tok/build.sh), WORK (scratch, default
10# scratch-tok-check beside the clone), CORES (taskset core list, default 18-19). The whole script
11# runs at nice 10 inside bwrap with no network; only WORK is writable.
12# One PASS/FAIL line per case, then `SUMMARY pass=N fail=M`; exit 0 iff fail=0.
13# Negative controls: the comparison must see a deliberately changed token file, and every
14# corrupted merges file (flipped byte, wrong length, rehashed bad pair) must be refused with 26.
15set -u
16here=$(cd "$(dirname "$0")" && pwd)
17BIN=${BIN:?set BIN to the ollm-tok binary}
18BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN")
19WORK=${WORK:-$here/../../../../scratch-tok-check}
20CORES=${CORES:-18-19}
21if [ -z "${TOK_CHECK_PINNED:-}" ]; then
22 mkdir -p "$WORK"
23 WORK=$(cd "$WORK" && pwd)
24 export TOK_CHECK_PINNED=1 WORK BIN
25 exec taskset -c "$CORES" nice -n 10 bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /tmp \
26 --bind "$WORK" "$WORK" --unshare-net --die-with-parent -- sh "$0" "$@"
27fi
28export PYTHONDONTWRITEBYTECODE=1
29ORACLE="$here/oracle.py"
30[ -x "$BIN" ] || { echo "tok-check: BIN $BIN is not executable" >&2; exit 2; }
31IN="$WORK/in" REF="$WORK/ref" GOT="$WORK/bin"
32rm -rf "$IN" "$REF" "$GOT"
33mkdir -p "$IN" "$REF" "$GOT"
34echo "tok-check: bin sha256=$(sha256sum "$BIN" | cut -c1-16) cores=$CORES"
35
36pass=0 fail=0
37ok() { echo "PASS $1${2:+ ($2)}"; pass=$((pass + 1)); }
38ko() { echo "FAIL $1${2:+ ($2)}" | sed "s|$WORK/||g"; fail=$((fail + 1)); }
39# r DIR TAG CMD...: run CMD in DIR; TAG.out, TAG.err, TAG.rc
40r() { _d=$1 _t=$2; shift 2; (cd "$_d" && "$@" >"$_t.out" 2>"$_t.err"; echo $? >"$_t.rc"); }
41both() { _t=$1; shift; r "$REF" "$_t" python3 "$ORACLE" "$@"; r "$GOT" "$_t" "$BIN" "$@"; }
42rc() { cat "$1/$2.rc"; }
43# chk NAME TAG RC [FILE|!FILE]...: both exits must be RC, both stdouts equal (the binary's T line
44# cut to six fields), each FILE equal in both dirs, each !FILE absent from both dirs
45chk() {
46 _n=$1 _t=$2 _rc=$3; shift 3
47 _a=$(rc "$REF" "$_t") _b=$(rc "$GOT" "$_t")
48 if [ "$_a" != "$_rc" ] || [ "$_b" != "$_rc" ]; then ko "$_n" "exit oracle=$_a bin=$_b want $_rc"; return; fi
49 if [ "$(cat "$REF/$_t.out")" != "$(cut -d' ' -f1-6 "$GOT/$_t.out")" ]; then
50 ko "$_n" "stdout oracle='$(cat "$REF/$_t.out")' bin='$(cat "$GOT/$_t.out")'"; return; fi
51 for _f in "$@"; do
52 case $_f in
53 !*) _f=${_f#!}
54 if [ -e "$REF/$_f" ] || [ -e "$GOT/$_f" ]; then ko "$_n" "$_f left behind"; return; fi ;;
55 *) if ! cmp -s "$REF/$_f" "$GOT/$_f"; then ko "$_n" "$_f differs"; return; fi ;;
56 esac
57 done
58 ok "$_n" "$(cut -d' ' -f1-6 "$GOT/$_t.out" | tr -d '\n')"
59}
60# rt NAME MERGES MODE TEXT: binary encode then decode must give TEXT back byte for byte
61rt() {
62 _n=$1 _m=$2 _mode=$3 _x=$4
63 r "$GOT" rt "$BIN" tokenc "$_mode" "$_m" "$_x" rt.tok
64 r "$GOT" rtd "$BIN" tokdec "$_m" rt.tok rt.back
65 if [ "$(rc "$GOT" rt)" != 0 ] || [ "$(rc "$GOT" rtd)" != 0 ]; then
66 ko "$_n" "exit enc=$(rc "$GOT" rt) dec=$(rc "$GOT" rtd)"; return; fi
67 if cmp -s "$_x" "$GOT/rt.back"; then ok "$_n" "$(cat "$GOT/rt.out")"; else ko "$_n" "decoded bytes differ"; fi
68}
69# flip FILE OFFSET: xor the byte at OFFSET with 1
70flip() {
71 _v=$(od -An -tu1 -j"$2" -N1 "$1" | tr -d ' ')
72 printf "\\$(printf %03o $((_v ^ 1)))" | dd of="$1" bs=1 seek="$2" conv=notrunc 2>/dev/null
73}
74# spec FILE: how many u16 tokens of FILE are special ids (256 .. 271)
75spec() { od -An -tu2 -v "$1" | tr -s ' ' '\n' | awk '$1 >= 256 && $1 < 272 { n++ } END { print n + 0 }'; }
76
77python3 "$ORACLE" gen 400000 1 "$IN/g1.txt"
78python3 "$ORACLE" gen 3200000 2 "$IN/g2.txt"
79head -c 200000 /dev/urandom > "$IN/rand200k.bin"
80head -c 1048576 /dev/urandom > "$IN/rand1m.bin"
81cat "$here/chat.txt" "$IN/g1.txt" "$here/chat.txt" > "$IN/chatmix.txt"
82cat "$here/unicode.txt" > "$IN/uni.txt"
83printf '\377\376\303(\355\240\200\300\257 \360\237\230 end\200\n' >> "$IN/uni.txt"
84echo "tok-check: inputs rand1m sha256=$(sha256sum "$IN/rand1m.bin" | cut -c1-16) g2=$(wc -c < "$IN/g2.txt") bytes"
85
86# 1. usage and argument errors
87r "$GOT" u0 "$BIN"
88both u1 bogus
89both u2 toktrain raw "$IN/g1.txt" m.bin 1000
90both u3 toktrain RAW "$IN/g1.txt" m.bin 1000 0
91both u4 toktrain raw "$IN/g1.txt" m.bin 271 0
92both u5 toktrain raw "$IN/g1.txt" m.bin 32769 0
93both u6 toktrain raw "$IN/g1.txt" m.bin 10a0 0
94[ "$(rc "$GOT" u0)" = 2 ] && ok "usage: no command exits 2" || ko "usage: no command" "exit $(rc "$GOT" u0)"
95chk "usage: unknown command" u1 2
96chk "usage: wrong argument count" u2 2 '!m.bin'
97chk "usage: unknown mode" u3 2 '!m.bin'
98chk "usage: V 271 below 272" u4 2 '!m.bin'
99chk "usage: V 32769 above 32768" u5 2 '!m.bin'
100chk "usage: V not a number" u6 2 '!m.bin'
101r "$GOT" u7 "$BIN" tokinfo "$IN/absent.bin"
102[ "$(rc "$GOT" u7)" = 70 ] && ok "missing merges file exits 70" || ko "missing merges file" "exit $(rc "$GOT" u7)"
103
104# 2. against the oracle: training, encoding, decoding, raw and chat
105both tr1 toktrain raw "$IN/g1.txt" m1.bin 1500 0
106chk "train raw V=1500 on 400 kB synthetic: same merges file" tr1 0 m1.bin
107both tr2 toktrain chat "$IN/chatmix.txt" m2.bin 1200 0
108chk "train chat V=1200 on chat fixture + synthetic: same merges file" tr2 0 m2.bin
109both tr3 toktrain raw "$IN/g2.txt" m3.bin 800 250000
110chk "train raw MAXBYTES=250000 of a 3.2 MB text: same merges file" tr3 0 m3.bin
111both tr4 toktrain raw "$IN/rand200k.bin" m4.bin 600 0
112chk "train raw V=600 on 200 kB random bytes: same merges file" tr4 0 m4.bin
113both in1 tokinfo "$IN/g1.txt"
114chk "tokinfo refuses a text file" in1 26
115cp "$GOT/m1.bin" "$IN/m1.bin"; cp "$GOT/m2.bin" "$IN/m2.bin"; cp "$GOT/m4.bin" "$IN/m4.bin"
116both in2 tokinfo "$IN/m1.bin"
117chk "tokinfo V, merges, checksum" in2 0
118both en1 tokenc raw "$IN/m1.bin" "$IN/g2.txt" t1.tok
119chk "encode raw 3.2 MB (window refills): same tokens" en1 0 t1.tok
120both en2 tokenc chat "$IN/m2.bin" "$IN/chatmix.txt" t2.tok
121chk "encode chat: same tokens and markers" en2 0 t2.tok
122both en3 tokenc raw "$IN/m2.bin" "$IN/chatmix.txt" t3.tok
123chk "encode raw text holding markers: same tokens" en3 0 t3.tok
124both en4 tokenc raw "$IN/m4.bin" "$IN/rand1m.bin" t4.tok
125chk "encode raw 1 MiB random bytes: same tokens" en4 0 t4.tok
126both en5 tokenc chat "$IN/m1.bin" "$IN/uni.txt" t5.tok
127chk "encode chat Unicode + invalid UTF-8: same tokens" en5 0 t5.tok
128cp "$GOT/t1.tok" "$IN/t1.tok"; cp "$GOT/t2.tok" "$IN/t2.tok"
129both de1 tokdec "$IN/m1.bin" "$IN/t1.tok" d1.txt
130chk "decode raw 3.2 MB: same bytes" de1 0 d1.txt
131both de2 tokdec "$IN/m2.bin" "$IN/t2.tok" d2.txt
132chk "decode chat: same bytes" de2 0 d2.txt
133
134# 3. exact round trips
135rt "round trip raw 1 MiB random bytes (merges from random)" "$IN/m4.bin" raw "$IN/rand1m.bin"
136rt "round trip chat 1 MiB random bytes (merges from text)" "$IN/m2.bin" chat "$IN/rand1m.bin"
137rt "round trip raw Unicode + invalid UTF-8" "$IN/m1.bin" raw "$IN/uni.txt"
138rt "round trip chat Unicode + invalid UTF-8" "$IN/m2.bin" chat "$IN/uni.txt"
139rt "round trip chat fixture (chat mode)" "$IN/m2.bin" chat "$here/chat.txt"
140rt "round trip chat fixture (raw mode)" "$IN/m2.bin" raw "$here/chat.txt"
141rt "round trip 3.2 MB synthetic (chat mode)" "$IN/m1.bin" chat "$IN/g2.txt"
142: > "$IN/empty.txt"
143rt "round trip empty text" "$IN/m1.bin" raw "$IN/empty.txt"
144
145# 4. chat special tokens
146r "$GOT" sp1 "$BIN" tokenc chat "$IN/m2.bin" "$here/chat.txt" sp1.tok
147r "$GOT" sp2 "$BIN" tokenc raw "$IN/m2.bin" "$here/chat.txt" sp2.tok
148n1=$(spec "$GOT/sp1.tok") n2=$(spec "$GOT/sp2.tok") f1=$(cut -d' ' -f6 "$GOT/sp1.out") f2=$(cut -d' ' -f6 "$GOT/sp2.out")
149nm=$(python3 -c 'import sys; sys.path.insert(0, sys.argv[1]); import oracle; t = open(sys.argv[2], "rb").read(); print(sum(1 for s, c in oracle.pieces(t, True) if s))' "$here" "$here/chat.txt")
150if [ "$n1" = "$nm" ] && [ "$f1" = "$nm" ] && [ "$nm" -gt 20 ]; then ok "chat mode: every exact marker is one special token" "$n1 of $nm"; else ko "chat mode specials" "tokens=$n1 field=$f1 oracle=$nm"; fi
151if [ "$n2" = 0 ] && [ "$f2" = 0 ]; then ok "raw mode: no special token, markers stay text"; else ko "raw mode specials" "tokens=$n2 field=$f2"; fi
152od -An -tu2 -v "$GOT/sp1.tok" | tr -s ' ' '\n' | awk '$1 >= 256 && $1 < 264 { s[$1] = 1 } END { for (k = 256; k < 264; k++) if (!(k in s)) exit 1 }' \
153 && ok "chat fixture uses all eight markers 256..263" || ko "chat fixture lacks a marker id"
154
155# 5. determinism
156r "$GOT" dt1 "$BIN" toktrain chat "$IN/chatmix.txt" dt1.bin 1200 0
157r "$GOT" dt2 "$BIN" toktrain chat "$IN/chatmix.txt" dt2.bin 1200 0
158h1=$(sha256sum "$GOT/dt1.bin" | cut -d' ' -f1) h2=$(sha256sum "$GOT/dt2.bin" | cut -d' ' -f1) h0=$(sha256sum "$IN/m2.bin" | cut -d' ' -f1)
159[ "$h1" = "$h2" ] && [ "$h1" = "$h0" ] && ok "determinism: three trainings, one merges sha256" "${h1%"${h1#????????????????}"}" || ko "determinism" "$h0 $h1 $h2"
160r "$GOT" dt3 "$BIN" tokenc chat "$GOT/dt1.bin" "$IN/chatmix.txt" dt3.tok
161cmp -s "$GOT/dt3.tok" "$GOT/t2.tok" && ok "determinism: same tokens from a retrained file" || ko "determinism tokens"
162ls "$GOT"/*.tmp >/dev/null 2>&1 && ko "atomic save: a .tmp file was left" || ok "atomic save: no .tmp file left"
163
164# 6. refusals of corrupted merges files (each exit 26 from tokinfo, tokenc and tokdec, no output)
165bad() {
166 _n=$1 _f=$2
167 both b1 tokinfo "$_f"
168 both b2 tokenc raw "$_f" "$IN/g1.txt" b2.tok
169 both b3 tokdec "$_f" "$IN/t1.tok" b3.txt
170 for _t in b1 b2 b3; do
171 if [ "$(rc "$REF" $_t)" != 26 ] || [ "$(rc "$GOT" $_t)" != 26 ]; then
172 ko "refuse $_n" "$_t exit oracle=$(rc "$REF" $_t) bin=$(rc "$GOT" $_t)"; return; fi
173 done
174 if [ -e "$GOT/b2.tok" ] || [ -e "$GOT/b3.txt" ]; then ko "refuse $_n" "output left behind"; return; fi
175 ok "refuse $_n" "26 x3"
176}
177sz=$(wc -c < "$IN/m1.bin")
178for o in 0 8 16 24 32 $((sz / 2)) $((sz - 1)); do
179 cp "$IN/m1.bin" "$IN/f$o.bin"; flip "$IN/f$o.bin" "$o"; bad "flipped byte at $o" "$IN/f$o.bin"
180done
181head -c $((sz - 1)) "$IN/m1.bin" > "$IN/short1.bin"; bad "one byte short" "$IN/short1.bin"
182head -c $((sz - 4)) "$IN/m1.bin" > "$IN/short4.bin"; bad "one pair short" "$IN/short4.bin"
183head -c 31 "$IN/m1.bin" > "$IN/short31.bin"; bad "31-byte header" "$IN/short31.bin"
184cat "$IN/m1.bin" "$IN/empty.txt" > "$IN/extra.bin"; printf x >> "$IN/extra.bin"; bad "one extra byte" "$IN/extra.bin"
185bad "empty file" "$IN/empty.txt"
186for k in special reserved forward self dup long; do
187 python3 "$ORACLE" mkbad $k "$IN/m1.bin" "$IN/k$k.bin"; bad "rehashed bad pair: $k" "$IN/k$k.bin"
188done
189
190# 7. token file errors and an exhausted text
191printf '\010\001' > "$IN/tres.tok"
192printf '\334\005' > "$IN/tbig.tok"
193printf 'abc' > "$IN/todd.tok"
194both te1 tokdec "$IN/m1.bin" "$IN/tres.tok" te1.txt
195chk "decode refuses reserved id 264 (25, no output)" te1 25 '!te1.txt'
196both te2 tokdec "$IN/m1.bin" "$IN/tbig.tok" te2.txt
197chk "decode refuses id 1500 = V (25, no output)" te2 25 '!te2.txt'
198both te3 tokdec "$IN/m1.bin" "$IN/todd.tok" te3.txt
199chk "decode refuses an odd-length token file (21, no output)" te3 21 '!te3.txt'
200printf 'ab ab ab' > "$IN/tiny.txt"
201both te4 toktrain raw "$IN/tiny.txt" te4.bin 300 0
202chk "train exits 28 when no pair is left (no file)" te4 28 '!te4.bin' '!te4.bin.tmp'
203
204# 8. negative control: the comparison sees one changed token
205cp "$GOT/t1.tok" "$WORK/neg.tok"; flip "$WORK/neg.tok" 1000
206cmp -s "$WORK/neg.tok" "$REF/t1.tok" && ko "negative control: changed token not seen" || ok "negative control: changed token file differs from the oracle's"
207
208echo "SUMMARY pass=$pass fail=$fail"
209[ "$fail" = 0 ]