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

156 lines · 7975 bytes Raw Blame History
1#!/bin/sh
2# Omega LLM v1 tokenizer check (CONTRACT-V1 §2, §7): bpefit, bpeenc and bpedec of the ollm binary
3# against the referee tests/omegallm/oracle.py on small generated texts, every tokenizer exit code,
4# and two negative controls. Needs no data directory; runs in seconds.
5#
6# BIN=path/to/ollm sh tests/omegallm/bpe-check.sh
7#
8# Environment: BIN (required; an x86linux binary runs through binfmt), WORK (scratch, default
9# scratch-bpe-check beside the clone), CORE (taskset core, default 6; everything runs at nice 19).
10# One PASS/FAIL line per case, then `SUMMARY pass=N fail=M`; exit 0 iff fail=0.
11# Negative controls: a merges file with one pair altered (rehashed) must change the tokens, and a
12# merges file with one flipped payload byte must be refused with 26. A third control runs the
13# comparison itself on a deliberately different token file and must see the difference.
14set -u
15here=$(cd "$(dirname "$0")" && pwd)
16ORACLE="$here/oracle.py"
17WORK=${WORK:-$here/../../../scratch-bpe-check}
18CORE=${CORE:-6}
19BIN=${BIN:?set BIN to the ollm binary}
20if [ -z "${BPE_CHECK_PINNED:-}" ]; then
21 BPE_CHECK_PINNED=1 exec taskset -c "$CORE" nice -n 19 sh "$0" "$@"
22fi
23export PYTHONDONTWRITEBYTECODE=1
24BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN")
25[ -x "$BIN" ] || { echo "bpe-check: BIN $BIN is not executable" >&2; exit 2; }
26mkdir -p "$WORK"
27WORK=$(cd "$WORK" && pwd)
28REF="$WORK/ref" GOT="$WORK/bin"
29rm -rf "$REF" "$GOT" "$WORK/tmp"
30mkdir -p "$REF" "$GOT" "$WORK/tmp"
31export OLLM_TMP="$WORK/tmp"
32echo "bpe-check: bin=$(basename "$BIN") sha256=$(sha256sum "$BIN" | cut -c1-16)"
33
34pass=0 fail=0
35ok() { echo "PASS $1${2:+ ($2)}"; pass=$((pass + 1)); }
36ko() { echo "FAIL $1${2:+ ($2)}" | sed "s|$WORK/||g"; fail=$((fail + 1)); }
37r() { _d=$1 _t=$2; shift 2; (cd "$_d" && "$@" >"$_t.out" 2>"$_t.err"; echo $? >"$_t.rc"); }
38both() { _t=$1; shift; r "$REF" "$_t" python3 "$ORACLE" ollm "$@"; r "$GOT" "$_t" "$BIN" "$@"; }
39gotonly() { _t=$1; shift; r "$GOT" "$_t" "$BIN" "$@"; }
40# chk NAME TAG RC [FILE|!FILE]...: referee exit must be RC; binary exit, stdout and FILEs equal
41chk() {
42 _n=$1 _t=$2 _rc=$3; shift 3
43 _rr=$(cat "$REF/$_t.rc") _gr=$(cat "$GOT/$_t.rc")
44 [ "$_rr" = "$_rc" ] || { ko "$_n" "referee exit $_rr, want $_rc"; return; }
45 [ "$_gr" = "$_rc" ] || { ko "$_n" "exit $_gr, want $_rc"; return; }
46 _d=$(cmp "$GOT/$_t.out" "$REF/$_t.out" 2>&1) || { ko "$_n" "stdout: $_d"; return; }
47 for _f; do
48 case $_f in
49 !*) [ ! -e "$GOT/${_f#!}" ] && [ ! -e "$GOT/${_f#!}.tmp" ] || { ko "$_n" "binary left ${_f#!}"; return; } ;;
50 *) _d=$(cmp "$GOT/$_f" "$REF/$_f" 2>&1) || { ko "$_n" "$_f: $_d"; return; } ;;
51 esac
52 done
53 ok "$_n" "exit $_rc, $(tr -d '\n' <"$GOT/$_t.out")"
54}
55# rc NAME TAG RC [!FILE]: binary-only exit code (usage refusals the referee does not model)
56rc() {
57 _n=$1 _t=$2 _rc=$3; shift 3
58 _gr=$(cat "$GOT/$_t.rc")
59 [ "$_gr" = "$_rc" ] || { ko "$_n" "exit $_gr, want $_rc"; return; }
60 for _f; do [ ! -e "$GOT/${_f#!}" ] || { ko "$_n" "binary left ${_f#!}"; return; }; done
61 ok "$_n" "exit $_rc"
62}
63wr() { python3 -c "import sys,struct; open(sys.argv[1],'wb').write($2)" "$1"; }
64
65# Texts: a story mix with every chunk rule (A runs, space+A, single bytes, pieces of 64, UTF-8,
66# controls, byte 0 terminators), the tie text, one without byte 0, an empty one.
67for d in "$REF" "$GOT"; do
68 wr "$d/mix.txt" "b''.join(b'Once upon a time, %d little cats sat on the mat. The cat said: \"hi!\"\\n It was 3.14 o\\'clock.\\0' % i for i in range(60))+b' x'+b'A'*150+b' '+'caf\\u00e9 na\\u00efve \\u65e5\\u672c'.encode()+b'!!? 9z\\t\\r\\n\\0'+bytes(range(256))+b'\\0'"
69 wr "$d/tie.txt" "b'ab cd ef\\0ab cd ef\\0'"
70 wr "$d/no0.txt" "b'abc def ghi'"
71 wr "$d/empty.txt" "b''"
72done
73
74both fit256 bpefit mix.txt m256.bin 256; rc fit-v256-2 fit256 2 '!m256.bin'
75both fit4097 bpefit mix.txt m4097.bin 4097; rc fit-v4097-2 fit4097 2 '!m4097.bin'
76both fit bpefit mix.txt mix.bin 400
77chk fit-mix fit 0 mix.bin
78[ ! -e "$GOT/mix.bin.tmp" ] && ok fit-no-tmp-left || ko fit-no-tmp-left
79both fitmax bpefit mix.txt mixmax.bin 300 2000
80chk fit-maxbytes-cut fitmax 0 mixmax.bin
81both fittie bpefit tie.txt tie.bin 261
82chk fit-tie fittie 0 tie.bin
83want=$(python3 -c "import struct; print(struct.pack('<10H',32,99,32,101,97,98,256,100,257,102).hex())")
84have=$(python3 -c "import sys; print(open(sys.argv[1],'rb').read()[24:].hex())" "$GOT/tie.bin")
85[ "$want" = "$have" ] && ok fit-tie-kat "binary merges = known answer" || ko fit-tie-kat "$have"
86both fit28 bpefit tie.txt tie28.bin 262
87chk fit-out-of-pairs-28 fit28 28 '!tie28.bin'
88both fit21 bpefit no0.txt no0.bin 300 5
89chk fit-maxbytes-no-0-21 fit21 21 '!no0.bin'
90both fitempty bpefit empty.txt e.bin 300
91chk fit-empty-28 fitempty 28 '!e.bin'
92
93for v in 400 300 257; do
94 both enc$v bpeenc mix.bin $v mix.txt mix$v.tok
95 chk enc-mix-v$v enc$v 0 mix$v.tok
96done
97both encempty bpeenc mix.bin 400 empty.txt empty.tok
98chk enc-empty encempty 0 empty.tok
99both dec bpedec mix.bin mix400.tok mix.dec
100chk dec-mix dec 0 mix.dec
101cmp -s "$GOT/mix.dec" "$GOT/mix.txt" && ok roundtrip-mix "decode(encode(mix)) == mix" || ko roundtrip-mix
102both decempty bpedec mix.bin empty.tok empty.dec
103chk dec-empty decempty 0 empty.dec
104
105# negative control 1: merge 20 altered to (101, 32), rehashed; the tokens must change
106for d in "$REF" "$GOT"; do
107 python3 -c "
108import sys, struct
109sys.path.insert(0, sys.argv[3]); import oracle
110raw = bytearray(open(sys.argv[1], 'rb').read())
111struct.pack_into('<2H', raw, 24 + 4 * 20, 101, 32)
112struct.pack_into('<Q', raw, 16, oracle.fnv1a64(bytes(raw[24:])))
113open(sys.argv[2], 'wb').write(raw)" "$REF/mix.bin" "$d/alt.bin" "$here"
114done
115both encalt bpeenc alt.bin 400 mix.txt alt.tok
116chk enc-altered-pair encalt 0 alt.tok
117if cmp -s "$GOT/alt.tok" "$GOT/mix400.tok"; then ko neg-altered-pair-changes-tokens "tokens unchanged"
118else ok neg-altered-pair-changes-tokens "$(cmp "$GOT/alt.tok" "$GOT/mix400.tok" | sed 's/.*differ: //')"; fi
119# negative control 2: one flipped payload byte -> 26, nothing written (enc and dec)
120for d in "$REF" "$GOT"; do
121 python3 -c "import sys; b=bytearray(open(sys.argv[1],'rb').read()); b[30]^=1; open(sys.argv[2],'wb').write(b)" "$REF/mix.bin" "$d/flip.bin"
122 python3 -c "import sys; b=bytearray(open(sys.argv[1],'rb').read()); b[0]^=1; open(sys.argv[2],'wb').write(b)" "$REF/mix.bin" "$d/magic.bin"
123 python3 -c "import sys; b=open(sys.argv[1],'rb').read(); open(sys.argv[2],'wb').write(b[:-4])" "$REF/mix.bin" "$d/short.bin"
124 head -c 101 "$REF/mix400.tok" >"$d/odd.tok"
125 wr "$d/big.tok" "struct.pack('<4H', 72, 105, 400, 33)"
126 wr "$d/bigodd.tok" "struct.pack('<4H', 72, 105, 400, 33)+b'x'"
127done
128both r26 bpeenc flip.bin 400 mix.txt r26.tok
129chk neg-flipped-byte-26 r26 26 '!r26.tok'
130both r26d bpedec flip.bin mix400.tok r26.dec
131chk dec-flipped-byte-26 r26d 26 '!r26.dec'
132both r26m bpeenc magic.bin 400 mix.txt r26m.tok
133chk bad-magic-26 r26m 26 '!r26m.tok'
134both r26s bpeenc short.bin 400 mix.txt r26s.tok
135chk short-file-26 r26s 26 '!r26s.tok'
136both r27 bpeenc mix.bin 401 mix.txt r27.tok
137chk v-over-merges-27 r27 27 '!r27.tok'
138both r27b bpeenc mix.bin 256 mix.txt r27b.tok
139chk enc-v256-27 r27b 27 '!r27b.tok'
140both r2627 bpeenc flip.bin 401 mix.txt r2627.tok
141chk order-26-before-27 r2627 26 '!r2627.tok'
142both r21 bpedec mix.bin odd.tok odd.dec
143chk odd-token-file-21 r21 21 '!odd.dec'
144both r25 bpedec mix.bin big.tok big.dec
145chk token-over-v-25 r25 25 '!big.dec'
146both r2125 bpedec mix.bin bigodd.tok bigodd.dec
147chk odd-before-25 r2125 21 '!bigodd.dec'
148gotonly u1 bpeenc mix.bin 400 mix.txt; rc enc-usage-2 u1 2
149gotonly u2 bpedec mix.bin mix400.tok; rc dec-usage-2 u2 2
150gotonly u3 bpefit mix.txt; rc fit-usage-2 u3 2
151# control of the comparison: a different token file must be seen as different
152cp "$REF/mix300.tok" "$WORK/tmp/c.tok"
153if cmp -s "$WORK/tmp/c.tok" "$REF/mix400.tok"; then ko neg-cmp-sees-difference; else ok neg-cmp-sees-difference; fi
154
155echo "SUMMARY pass=$pass fail=$fail"
156[ "$fail" = 0 ]