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 / ORACLE.md
| 1 | # Omega LLM oracle (v0 and v1) |
| 2 | |
| 3 | `oracle.py` is a referee for `docs/omegallm/CONTRACT.md` (v0) and `docs/omegallm/CONTRACT-V1.md` (v1). It uses only the Python 3 standard library (`math.exp`, `math.log`, `math.sqrt` on f64 floats; no numpy). It was written from the contracts, not ported from `native/omegallm/*.om`. It is for verification only and is never part of the build. The v1 part is described in [v1 referee](#v1-referee-contract-v1); everything before it is v0 and unchanged. |
| 4 | |
| 5 | ## What it implements |
| 6 | |
| 7 | - `OLLMCK01` and `OLLMGR01` read and write, including FNV-1a 64 and the refusals. |
| 8 | - The §5 init. |
| 9 | - The §4 batch offsets: splitmix64 with every product masked to 2^64. |
| 10 | - The §3 forward pass and a hand-written analytic backward pass for the whole model. |
| 11 | - One Adam step with the clip and the LR schedule. |
| 12 | - §4 `prep`, §4 held-out `eval`, and the §7 `base` add-one unigram and bigram models. |
| 13 | |
| 14 | ## Commands |
| 15 | |
| 16 | Run everything on core 8 at nice 19. Core 8 is a Cortex-X925 on the GB10 Grace (MIDR `0x410fd851` in `/sys/devices/system/cpu/cpu8/regs/identification/midr_el1`). |
| 17 | |
| 18 | ```sh |
| 19 | taskset -c 8 nice -n 19 python3 tests/omegallm/oracle.py selftest |
| 20 | taskset -c 8 nice -n 19 python3 tests/omegallm/oracle.py prep IN TRAIN HELD # prints P ... |
| 21 | taskset -c 8 nice -n 19 python3 tests/omegallm/oracle.py base TRAIN HELD # prints B ... |
| 22 | # Cross-checks against an ollm binary. Each writes a JSON receipt with argv, rc, stdout, stderr, |
| 23 | # seconds, the binary sha256 and per-check results. |
| 24 | oracle.py xprep --ollm BIN --data IN --outdir DIR [--receipt R.json] # sha256 of TRAIN, HELD + stdout |
| 25 | oracle.py xbase --ollm BIN --train TRAIN --held HELD # exact micro values |
| 26 | oracle.py xinit --ollm BIN [--cfg small|v0] # params bit-for-bit, whole file sha256 |
| 27 | oracle.py xgrad --ollm BIN --data DATA [--cfg small|v0] [--batch B] # loss + every gradient entry |
| 28 | oracle.py xstep --ollm BIN --data DATA [--cfg small|v0] # train s -> s+1: params, m, v, step |
| 29 | oracle.py xeval --ollm BIN --held HELD [--cfg small|v0] # eval stdout |
| 30 | # All checks for one binary, with receipts under OUTDIR/LABEL (relative paths, so receipts name none): |
| 31 | DATA=data/TinyStoriesV2-GPT4-valid.txt WORK=scratch-oracle/work tests/omegallm/xcheck.sh BIN LABEL OUTDIR [v0] |
| 32 | ``` |
| 33 | |
| 34 | `selftest` checks: |
| 35 | |
| 36 | - The analytic gradient against central differences (h = 1e-5, relative error < 1e-5). It takes 3 to 4 coordinates in every tensor of the small config, at perturbed parameters and with B = 2. |
| 37 | - Known answers for FNV-1a 64, splitmix64, a batch offset and an init draw. |
| 38 | - A checkpoint round trip and the refusal of a corrupted checkpoint. |
| 39 | - The Adam step-0 value and the clip. |
| 40 | - Known answers for prep and for base. |
| 41 | |
| 42 | Negative control: flipping the dK sign in the attention backward, or the sign of the one-hot term in the cross-entropy gradient, makes `selftest` fail. See `evidence/OMEGALLM/oracle/README.md`. |
| 43 | |
| 44 | The cases in `xgrad` and `xstep`: |
| 45 | |
| 46 | - `xgrad`: |
| 47 | - `init`: a fresh init at step 0; |
| 48 | - `perturbed`: init plus seeded Gaussian noise of σ 0.3. |
| 49 | |
| 50 | Offsets come from the §4 batch rule on DATA. |
| 51 | - `xstep`: |
| 52 | - `step0-init`; |
| 53 | - `step1-chain`: from the binary's own step-1 output, so m and v are nonzero; |
| 54 | - `step2-unclipped`: gf = 0.1, so the gradient norm stays below 1; |
| 55 | - `step150-synthetic-clip`: the post-warm-up decay branch, with nonzero m and v. |
| 56 | |
| 57 | Each case asserts that it covers the clip branch it names. |
| 58 | |
| 59 | ## Known answers |
| 60 | |
| 61 | The values were computed with Wolfram (query W1): |
| 62 | |
| 63 | ``` |
| 64 | fnv[s_]:=Fold[Mod[BitXor[#1,#2]*1099511628211,2^64]&,14695981039346656037,ToCharacterCode[s]]; |
| 65 | sm[x_]:=Module[{z=Mod[x+16^^9E3779B97F4A7C15,2^64]},z=Mod[BitXor[z,BitShiftRight[z,30]]*16^^BF58476D1CE4E5B9,2^64]; |
| 66 | z=Mod[BitXor[z,BitShiftRight[z,27]]*16^^94D049BB133111EB,2^64];BitXor[z,BitShiftRight[z,31]]]; |
| 67 | {fnv[""],fnv["a"],fnv["foobar"],fnv["OLLMCK01"],sm[0],sm[1],sm[2^64-1],sm[42], |
| 68 | Mod[sm[BitXor[7,Mod[7*16^^9E3779B97F4A7C15,2^64]]],1000], BitShiftRight[sm[47],11]} |
| 69 | -> cbf29ce484222325 af63dc4c8601ec8c 85944171f73967e8 29c48bb36358f164 |
| 70 | e220a8397b1dcdaf 910a2dec89025cc1 e4d971771b652c20 bdd732262feb6e95 533 4352384759967643 |
| 71 | ``` |
| 72 | |
| 73 | ## Tolerance |
| 74 | |
| 75 | `xgrad` and `xstep` compare each entry with |
| 76 | |
| 77 | err = |got − ref| / max(|ref|, 10⁻³ · max|ref in that tensor|), and require err ≤ 10⁻⁹. |
| 78 | |
| 79 | Both sides are f64, but their summation orders differ. The longest reduction behind any gradient entry is at most n = 4096 terms: B·T·(the inner dimension) stays below that in the small config, and the per-entry accumulation over B·T = 1024 positions in v0 does too. The classical worst-case relative bound is γₙ = n·u/(1 − n·u). |
| 80 | |
| 81 | The floor of 10⁻³ of the tensor maximum caps the amplification of cancellation in entries near zero at 10³. Wolfram (query W2): |
| 82 | |
| 83 | `u=2^-53; g[n_]:=n u/(1-n u); {N[u],N[g[4096]],N[10^3 g[4096]],N[10^-9/(10^3 g[4096])]} -> {1.11*10^-16, 4.547*10^-13, 4.547*10^-10, 2.2}` |
| 84 | |
| 85 | So 10⁻⁹ is 2.2 times the heuristic bound 10³·γ₄₀₉₆. This is a forward-error heuristic, not a proof; the receipts give the observed maximum per tensor. |
| 86 | |
| 87 | The loss must also agree to a relative 10⁻⁹, and the printed `loss_micro` must match exactly. Init must agree bit for bit. Prep must be byte-identical. Base and eval must print the same micro integers. |
| 88 | |
| 89 | ## Contract readings agreed with the core lane |
| 90 | |
| 91 | These readings were agreed with OllmCore and are written into CONTRACT.md by that lane: |
| 92 | |
| 93 | - A1: the batch input is `S xor ((s·B + b)·0x9E3779B97F4A7C15 mod 2^64)`. |
| 94 | - A2: `wo` and `w2` use `((2r−1)·0.0346…)·(1.0/sqrt(2L))`. |
| 95 | - A3, the clip: `n = sqrt(Σg²)`; if n > 1, then `g ← g·(1/n)`. |
| 96 | - A3, Adam: |
| 97 | - m = β1·m + (1−β1)·g and v = β2·v + (1−β2)·g², with (1−β) computed in f64; |
| 98 | - the powers β^(s+1) come from repeated multiplication; |
| 99 | - p ← p − (lr_s·m̂)/(sqrt(v̂)+ε), where m̂ = m/(1−β1^(s+1)) and v̂ = v/(1−β2^(s+1)); |
| 100 | - lr_s = (lr·(s+1))/100, then lr·(1 − (0.9·(s−100))/max(1, TOTAL−100)). |
| 101 | - A4, `OLLMGR01`: |
| 102 | - the header copies CK, with step = B; |
| 103 | - OFFS holds exactly 8·B bytes, and every o ≤ N−T−1. |
| 104 | - A5, `base`: |
| 105 | - the unigram counts every TRAIN byte; |
| 106 | - the bigram counts every adjacent TRAIN pair, crossing the 0 separators, and its row denominator is the pair-row sum + 256; |
| 107 | - both are scored on the M−1 HELD transitions, as mean −log2 p. |
| 108 | - A6, `prep` stdout: `P total train held train_bytes held_bytes`, where the byte counts include the 0 terminators. |
| 109 | - A7, `*_micro`: round half away from zero of the single f64 product v·1e6. |
| 110 | - A7, `train`: prints `T s+1 loss`, using the loss at the pre-update parameters. |
| 111 | - A8: `lr = f64(LRMICRO)/1000000.0`, a single division. For 5784 of the values 1..20000 it differs in bits from `LRMICRO·1e-6`, for example 5, 10 and 15. For 3000 the two agree. |
| 112 | |
| 113 | ## v1 referee (CONTRACT-V1) |
| 114 | |
| 115 | ### What it implements |
| 116 | |
| 117 | - §2 tokenizer: |
| 118 | - the chunk rule; |
| 119 | - `bpefit`, including MAXBYTES cut back to the last 0, the tie rule (smallest a, then smallest b) and overlapping pair counts; |
| 120 | - `OLLMBPE1` write and load, with exits 26 and 27; |
| 121 | - `bpeenc` and `bpedec`, u16 token files, exit 21 for an odd file and 25 for a token ≥ V. |
| 122 | - §4 v1 shapes and the over-maximum exit 12. |
| 123 | - §5 G gradient blocks, and the K check (exit 29). |
| 124 | - §6 `OLLMCK02` and `OLLMGR01` with G. |
| 125 | - `train`, `grad`, `eval` with the §7.1 bits per byte, and `sample` (§7.2) with the MERGES argument. |
| 126 | |
| 127 | The model arithmetic reuses the v0 forward, backward and Adam code. Only the block grouping, the shapes and the file formats are new. |
| 128 | |
| 129 | ### Commands |
| 130 | |
| 131 | ```sh |
| 132 | taskset -c 8 nice -n 19 python3 tests/omegallm/oracle.py selftest-v1 |
| 133 | # The ollm command line of CONTRACT-V1 §7; same stdout, files and exit codes as the product: |
| 134 | python3 tests/omegallm/oracle.py ollm bpefit TEXT MERGES V [MAXBYTES] |
| 135 | python3 tests/omegallm/oracle.py ollm train CK DATA UNTIL EVERY [K] # etc., every §7 command |
| 136 | # Compare a product file with a referee file: |
| 137 | python3 tests/omegallm/oracle.py cmp ck|gr GOT REF # header bytes exact, payload within the tolerance below |
| 138 | python3 tests/omegallm/oracle.py cmp gline GOT REF # G line: loss_micro exact, loss bits within the tolerance |
| 139 | python3 tests/omegallm/oracle.py cmp tok|bytes GOT REF |
| 140 | # Every v1 case against a binary; one PASS/FAIL/NOT_RUN line per case, then SUMMARY: |
| 141 | BIN=path/to/ollm DATA=data WORK=scratch-v1-oracle sh tests/omegallm/v1-oracle.sh |
| 142 | ``` |
| 143 | |
| 144 | `selftest-v1` checks: |
| 145 | |
| 146 | - known answers for the chunk rule, an overlapping count ((a,a) = 2 in `aaa`), the tie rule and exit 28; |
| 147 | - that rank-order encoding equals applying the merges in sequence; |
| 148 | - `decode(encode(text)) == text`; |
| 149 | - an `OLLMBPE1` round trip with prefix use, and exit 26 for a corrupted hash; |
| 150 | - the CK02 round trip; |
| 151 | - that a G = 2 gradient equals the G = 1 gradient within the tolerance. |
| 152 | |
| 153 | `v1-oracle.sh` reads `DATA/TinyStoriesV2-GPT4-valid.txt` and runs everything on `CORE` (default 8) at nice 19, the script itself included. Its cases: |
| 154 | |
| 155 | - Tokenizer (§9.2), all byte-exact: |
| 156 | - prep; |
| 157 | - a V = 512 fit on TRAIN, and a V = 300 fit with MAXBYTES 1000000; |
| 158 | - the tie known answer `ab cd ef\0ab cd ef\0` at V = 261, whose merges must be (32,99) (32,101) (97,98) (256,100) (257,102). This is checked even without BIN. |
| 159 | - V = 262 on the same text → 28 with no file left; MAXBYTES with no 0 → 21; |
| 160 | - an edge text: a run of more than 64 letters, UTF-8, doubled spaces, tabs and CR; |
| 161 | - encodings at V = 512 and V = 300 of TRAIN and HELD; `bpedec`; and `decode(encode(TRAIN)) == TRAIN`; |
| 162 | - the altered-pair negative control: merge 100 is replaced and the file rehashed, and the tokens must change; |
| 163 | - exits 26 (hash), 27 (V over the merges, and `bpeenc` at V = 256), 21 (odd token file) and 25 (token ≥ V). |
| 164 | - Model (§9.3 to §9.5), small config `V=300 T=8 D=16 H=2 F=32 L=2 B=4 S=5 TOTAL=50 LRMICRO=3000`, data = the V = 300 encoding of TRAIN: |
| 165 | - `init` at G = 2, G = 4 and with no G (CK01), bit for bit; |
| 166 | - G = 3 → 12, and each of the seven shapes one over its maximum → 12; |
| 167 | - `grad` at OFFS 0, 777777, 5000000, 13000000 for G = 2 and G = 4: the G line, then the gradient within the tolerance; |
| 168 | - a 20-step chain, one `train` call per step from the binary's own checkpoint: T lines exact and the checkpoint within the tolerance at every step; |
| 169 | - one-process 20-step runs with K = 1 and K = 2: byte-identical to each other and to the chain, and their T lines equal the referee's own 20-step run; |
| 170 | - K = 3 and K = 0 → 29; |
| 171 | - `eval` W = 4 with merges512 on the V = 300 HELD encoding, exact; |
| 172 | - `eval` and `sample` without MERGES at V = 300 → 2; W too big → 23; |
| 173 | - `sample` at TEMPMILLI 0 and 800, compared byte for byte; |
| 174 | - data refusals: odd → 21, shorter than T + 1 → 20, a token ≥ V inside a read row → 25 (and exit 0 with a gradient when the bad token is in no row read), an offset ≥ N − T → 22, an OFFS file of the wrong size → 21. |
| 175 | - With `V0BIN` set: a V = 256 CK01 from the v1 binary equals the v0 binary's, at init and after 20 steps (§9.4). |
| 176 | - With `EXT_MERGES EXT_V EXT_TEXT EXT_TOK` set: the referee encodes `EXT_TEXT` and compares the result with the product's `EXT_TOK` (the Tok lane's merges4096 and held.tok check). |
| 177 | |
| 178 | The referee's own exit code is checked even without BIN. A referee that refuses where the contract expects success is a FAIL, not a NOT_RUN. |
| 179 | |
| 180 | ### Tolerance |
| 181 | |
| 182 | §9.3 (amended) sets these comparison rules: |
| 183 | |
| 184 | - the model is checked at the v0 tolerance above, entry by entry, err ≤ 10⁻⁹; |
| 185 | - init is compared bit for bit; |
| 186 | - the printed micro integers and the CK02 header must match exactly; |
| 187 | - tokenizer outputs must be byte-identical. |
| 188 | |
| 189 | The longest reduction behind any entry in the small config is far below the 4096 terms the v0 bound assumes: |
| 190 | |
| 191 | - B·T = 32 positions; |
| 192 | - the inner dimensions D = 16 and F = 32; |
| 193 | - a softmax over V = 300; |
| 194 | - G − 1 block additions. |
| 195 | |
| 196 | So the W2 bound carries over unchanged. |
| 197 | |
| 198 | `sample` and `eval` are compared byte for byte. That goes beyond §9.3; a mismatch there is reported with the first differing byte. |
| 199 | |
| 200 | ### Negative controls |
| 201 | |
| 202 | `OLLM_ORACLE_MUTANT=NAME`, or `MUTANT=NAME` for the script, plants a defect in the referee. With an honest product, each mutant must produce FAIL lines. |
| 203 | |
| 204 | | Mutant | Defect | Cases that FAIL | |
| 205 | |---|---|---| |
| 206 | | `tiemaxa` | ties go to the largest a | referee-selftest-v1, fit-tie, fit-tie-kat, fit-edge, enc-edge | |
| 207 | | `chunk63` | long chunks are cut into 63-byte pieces instead of 64 | referee-selftest-v1, fit-edge, enc-edge | |
| 208 | | `blockscale` | block gradients are scaled by 1/(T·R) instead of 1/(T·B) | referee-selftest-v1, grad-g2, grad-g4, data-lazy | |
| 209 | | `bpbv0` | eval at V > 256 prints v0's loss/ln 2 instead of the §7.1 bits per byte | eval-w4-bpb | |
| 210 | |
| 211 | `blockscale` passes the train cases. Adam's update m̂/(√v̂ + ε) does not change when the gradient is scaled, apart from ε. So a gradient-scale defect is caught by `grad`, not by `train`. |
| 212 | |
| 213 | Reversing the order in which blocks are summed (`blockrev`) changes the result only at rounding level. It is invisible within the tolerance, so it is not a mutant. Only .om against .om comparisons, which are bit-exact (§9.4 K identity), can catch it. |
| 214 | |
| 215 | ### Readings |
| 216 | |
| 217 | These are the referee's literal readings of points that CONTRACT-V1 leaves implicit: |
| 218 | |
| 219 | - MAXBYTES ≥ the text length still cuts back to the last 0. |
| 220 | - The merges loader checks the hash (26) before V (27). `bpeenc` with V ≤ 256 loads first, then exits 27. |
| 221 | - `bpefit` with V outside 257..4096 exits 2. This is unspecified and not tested. |
| 222 | - At V = 256, `eval` and `sample` accept an extra MERGES argument and ignore it. `sample` at V = 256 is v0's (context 4096, no merges). |
| 223 | - Data checks: an odd file exits 21. N < T + 1 exits 20. The offset check (22) comes before the token check, and tokens ≥ V are checked lazily (25), only in the rows actually read. |
| 224 | - The v1 `prep` is v0's `prep`. Its outputs are byte-identical to `data/train.bin` and `data/held.bin`. |
| 225 | |
| 226 | ### Measured |
| 227 | |
| 228 | All runs below were on GB10 Grace core 8 (Cortex-X925), at nice 19, using `/usr/bin/time`. |
| 229 | |
| 230 | | Step | Wall time | Peak RSS | Output | |
| 231 | |---|---|---|---| |
| 232 | | `ollm prep` of the valid text | 0.09 s | | `P 27630 24867 2763 19883834 2220914` | |
| 233 | | `ollm bpefit` TRAIN V = 512 | 0.94 s | 145 MB | `M 512 13055 4975460 19883834` | |
| 234 | | `ollm bpeenc` TRAIN V = 512 | 1.06 s | | `K 19883834 8320463` | |
| 235 | | the whole `v1-oracle.sh`, with the referee itself as BIN | 21.5 s | 788 MB | `SUMMARY pass=53 fail=0 not_run=1` | |
| 236 | |
| 237 | In the full-script run, the one NOT_RUN case is `v0-v1-ck01`, which needs `V0BIN`. |