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
aien-train / tests / omegallm / bpe-check.sh
| 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. |
| 14 | set -u |
| 15 | here=$(cd "$(dirname "$0")" && pwd) |
| 16 | ORACLE="$here/oracle.py" |
| 17 | WORK=${WORK:-$here/../../../scratch-bpe-check} |
| 18 | CORE=${CORE:-6} |
| 19 | BIN=${BIN:?set BIN to the ollm binary} |
| 20 | if [ -z "${BPE_CHECK_PINNED:-}" ]; then |
| 21 | BPE_CHECK_PINNED=1 exec taskset -c "$CORE" nice -n 19 sh "$0" "$@" |
| 22 | fi |
| 23 | export PYTHONDONTWRITEBYTECODE=1 |
| 24 | BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN") |
| 25 | [ -x "$BIN" ] || { echo "bpe-check: BIN $BIN is not executable" >&2; exit 2; } |
| 26 | mkdir -p "$WORK" |
| 27 | WORK=$(cd "$WORK" && pwd) |
| 28 | REF="$WORK/ref" GOT="$WORK/bin" |
| 29 | rm -rf "$REF" "$GOT" "$WORK/tmp" |
| 30 | mkdir -p "$REF" "$GOT" "$WORK/tmp" |
| 31 | export OLLM_TMP="$WORK/tmp" |
| 32 | echo "bpe-check: bin=$(basename "$BIN") sha256=$(sha256sum "$BIN" | cut -c1-16)" |
| 33 | |
| 34 | pass=0 fail=0 |
| 35 | ok() { echo "PASS $1${2:+ ($2)}"; pass=$((pass + 1)); } |
| 36 | ko() { echo "FAIL $1${2:+ ($2)}" | sed "s|$WORK/||g"; fail=$((fail + 1)); } |
| 37 | r() { _d=$1 _t=$2; shift 2; (cd "$_d" && "$@" >"$_t.out" 2>"$_t.err"; echo $? >"$_t.rc"); } |
| 38 | both() { _t=$1; shift; r "$REF" "$_t" python3 "$ORACLE" ollm "$@"; r "$GOT" "$_t" "$BIN" "$@"; } |
| 39 | gotonly() { _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 |
| 41 | chk() { |
| 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) |
| 56 | rc() { |
| 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 | } |
| 63 | wr() { 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. |
| 67 | for 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''" |
| 72 | done |
| 73 | |
| 74 | both fit256 bpefit mix.txt m256.bin 256; rc fit-v256-2 fit256 2 '!m256.bin' |
| 75 | both fit4097 bpefit mix.txt m4097.bin 4097; rc fit-v4097-2 fit4097 2 '!m4097.bin' |
| 76 | both fit bpefit mix.txt mix.bin 400 |
| 77 | chk fit-mix fit 0 mix.bin |
| 78 | [ ! -e "$GOT/mix.bin.tmp" ] && ok fit-no-tmp-left || ko fit-no-tmp-left |
| 79 | both fitmax bpefit mix.txt mixmax.bin 300 2000 |
| 80 | chk fit-maxbytes-cut fitmax 0 mixmax.bin |
| 81 | both fittie bpefit tie.txt tie.bin 261 |
| 82 | chk fit-tie fittie 0 tie.bin |
| 83 | want=$(python3 -c "import struct; print(struct.pack('<10H',32,99,32,101,97,98,256,100,257,102).hex())") |
| 84 | have=$(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" |
| 86 | both fit28 bpefit tie.txt tie28.bin 262 |
| 87 | chk fit-out-of-pairs-28 fit28 28 '!tie28.bin' |
| 88 | both fit21 bpefit no0.txt no0.bin 300 5 |
| 89 | chk fit-maxbytes-no-0-21 fit21 21 '!no0.bin' |
| 90 | both fitempty bpefit empty.txt e.bin 300 |
| 91 | chk fit-empty-28 fitempty 28 '!e.bin' |
| 92 | |
| 93 | for 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 |
| 96 | done |
| 97 | both encempty bpeenc mix.bin 400 empty.txt empty.tok |
| 98 | chk enc-empty encempty 0 empty.tok |
| 99 | both dec bpedec mix.bin mix400.tok mix.dec |
| 100 | chk dec-mix dec 0 mix.dec |
| 101 | cmp -s "$GOT/mix.dec" "$GOT/mix.txt" && ok roundtrip-mix "decode(encode(mix)) == mix" || ko roundtrip-mix |
| 102 | both decempty bpedec mix.bin empty.tok empty.dec |
| 103 | chk dec-empty decempty 0 empty.dec |
| 104 | |
| 105 | # negative control 1: merge 20 altered to (101, 32), rehashed; the tokens must change |
| 106 | for d in "$REF" "$GOT"; do |
| 107 | python3 -c " |
| 108 | import sys, struct |
| 109 | sys.path.insert(0, sys.argv[3]); import oracle |
| 110 | raw = bytearray(open(sys.argv[1], 'rb').read()) |
| 111 | struct.pack_into('<2H', raw, 24 + 4 * 20, 101, 32) |
| 112 | struct.pack_into('<Q', raw, 16, oracle.fnv1a64(bytes(raw[24:]))) |
| 113 | open(sys.argv[2], 'wb').write(raw)" "$REF/mix.bin" "$d/alt.bin" "$here" |
| 114 | done |
| 115 | both encalt bpeenc alt.bin 400 mix.txt alt.tok |
| 116 | chk enc-altered-pair encalt 0 alt.tok |
| 117 | if cmp -s "$GOT/alt.tok" "$GOT/mix400.tok"; then ko neg-altered-pair-changes-tokens "tokens unchanged" |
| 118 | else 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) |
| 120 | for 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'" |
| 127 | done |
| 128 | both r26 bpeenc flip.bin 400 mix.txt r26.tok |
| 129 | chk neg-flipped-byte-26 r26 26 '!r26.tok' |
| 130 | both r26d bpedec flip.bin mix400.tok r26.dec |
| 131 | chk dec-flipped-byte-26 r26d 26 '!r26.dec' |
| 132 | both r26m bpeenc magic.bin 400 mix.txt r26m.tok |
| 133 | chk bad-magic-26 r26m 26 '!r26m.tok' |
| 134 | both r26s bpeenc short.bin 400 mix.txt r26s.tok |
| 135 | chk short-file-26 r26s 26 '!r26s.tok' |
| 136 | both r27 bpeenc mix.bin 401 mix.txt r27.tok |
| 137 | chk v-over-merges-27 r27 27 '!r27.tok' |
| 138 | both r27b bpeenc mix.bin 256 mix.txt r27b.tok |
| 139 | chk enc-v256-27 r27b 27 '!r27b.tok' |
| 140 | both r2627 bpeenc flip.bin 401 mix.txt r2627.tok |
| 141 | chk order-26-before-27 r2627 26 '!r2627.tok' |
| 142 | both r21 bpedec mix.bin odd.tok odd.dec |
| 143 | chk odd-token-file-21 r21 21 '!odd.dec' |
| 144 | both r25 bpedec mix.bin big.tok big.dec |
| 145 | chk token-over-v-25 r25 25 '!big.dec' |
| 146 | both r2125 bpedec mix.bin bigodd.tok bigodd.dec |
| 147 | chk odd-before-25 r2125 21 '!bigodd.dec' |
| 148 | gotonly u1 bpeenc mix.bin 400 mix.txt; rc enc-usage-2 u1 2 |
| 149 | gotonly u2 bpedec mix.bin mix400.tok; rc dec-usage-2 u2 2 |
| 150 | gotonly u3 bpefit mix.txt; rc fit-usage-2 u3 2 |
| 151 | # control of the comparison: a different token file must be seen as different |
| 152 | cp "$REF/mix300.tok" "$WORK/tmp/c.tok" |
| 153 | if cmp -s "$WORK/tmp/c.tok" "$REF/mix400.tok"; then ko neg-cmp-sees-difference; else ok neg-cmp-sees-difference; fi |
| 154 | |
| 155 | echo "SUMMARY pass=$pass fail=$fail" |
| 156 | [ "$fail" = 0 ] |