omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / docs / train / M22_SUBSTRATE.md
| 1 | # M22 optimizer substrate and E5 training provenance (Lane 21) |
| 2 | |
| 3 | Status: **M22 NOT QUALIFIED.** This is the transactional parameter-state |
| 4 | substrate and an SGD path over plain byte buffers. There is no tensor or |
| 5 | autodiff integration yet; that waits for M20 OMEGA_TENSOR and M21 |
| 6 | OMEGA_AUTODIFF. Plan reference: aien-architecture `CURRENT_EXECUTION_PLAN.md` |
| 7 | sections E4 (M22) and E5 (training provenance). |
| 8 | |
| 9 | ## What exists |
| 10 | |
| 11 | | Piece | File | Plan item | |
| 12 | |---|---|---| |
| 13 | | Generation store (shadow, validate, one atomic switch, recovery) | `src/train/tg_store.[ch]` | E4: shadow state, validation before one atomic switch, OLD or NEW | |
| 14 | | SGD updater (float32, optional heavy-ball momentum) | `src/train/tg_sgd.[ch]` | E4: SGD first | |
| 15 | | Two-tier provenance | `src/train/tg_store.[ch]` | E5 | |
| 16 | | Exit tests, plain + ASan/UBSan | `tests/train/test_train.c`, `make test-train` | E4/E5 exit gate (substrate level) | |
| 17 | | Receipt | `tests/train/train_receipt.sh`, `make train-receipt` -> `evidence/M22/receipts/` | | |
| 18 | |
| 19 | ## On-disk layout (one directory per store) |
| 20 | |
| 21 | - `gen-<20 digits>.bin`: one immutable generation. 152-byte little-endian |
| 22 | header (magic `OMTRGEN1`, version, generation, step, parameter bytes, |
| 23 | optimizer bytes, dispatch-log length and chain head at commit, previous |
| 24 | generation digest, digest) followed by parameter bytes and optimizer bytes. |
| 25 | Digest = SHA-256 over the first 120 header bytes + parameters + optimizer. |
| 26 | - `CURRENT`: the pointer, exactly 95 bytes: `OMTRCUR1 <generation> <digest hex>\n`. |
| 27 | A reader checks the generation file's digest against the pointer. |
| 28 | - `dispatch.log`: tier (a) records, 192 bytes each, hash-chained. |
| 29 | - `refusals.log`: one line per refused operation (`REFUSE <code> gen=<g> <why>`). |
| 30 | - `LOCK`: `flock` for the single writer. |
| 31 | |
| 32 | ## Commit protocol |
| 33 | |
| 34 | 1. Refuse if the shadow is not open (double switch), its base is not the |
| 35 | current generation (stale), or sizes differ. Run the validation callback; |
| 36 | refuse on reject. Every refusal is appended to `refusals.log`. |
| 37 | 2. Append the shadow's dispatch records to `dispatch.log`, chained, `fsync`. |
| 38 | 3. Compute the full state digest (tier b; the only place the model is hashed). |
| 39 | 4. Write `shadow-N.tmp`, `fsync`, read it back and verify the digest. |
| 40 | 5. `rename` to `gen-N.bin`, `fsync` the directory. |
| 41 | 6. Write `CURRENT.tmp`, `fsync`, `rename` to `CURRENT` (the one atomic |
| 42 | switch), `fsync` the directory. |
| 43 | |
| 44 | Recovery (`tg_open`): verify the generation `CURRENT` names (size, digest, |
| 45 | pointer digest), delete `*.tmp` and generation files newer than `CURRENT`, |
| 46 | truncate `dispatch.log` to the committed length and verify its chain against |
| 47 | the committed head. A generation that does not verify is refused, never |
| 48 | silently rolled back. |
| 49 | |
| 50 | ## Provenance tiers (E5) |
| 51 | |
| 52 | - (a) Per dispatch: step, op id, two scalar arguments (SGD: learning rate and |
| 53 | momentum bit patterns; gradient reference id), up to three input references |
| 54 | (buffer id, offset, length) and the base generation's digest, by reference. |
| 55 | Cost is one SHA-256 over 192 bytes per record, independent of model size |
| 56 | (tested at 1 Ki and 256 Ki parameters). |
| 57 | - (b) Full parameter + optimizer digest, only at committed generations, |
| 58 | linked by previous-digest into a verifiable history (`tg_verify_history`). |
| 59 | |
| 60 | ## What the tests prove (host, single core) |
| 61 | |
| 62 | - Crash at each of 8 injected points inside commit (child process `_exit`): |
| 63 | points before the pointer rename recover to exactly OLD; points after it |
| 64 | recover to exactly NEW; digests verified; redoing the step from OLD gives |
| 65 | the same NEW digest. Torn-file variants (not-yet-current files truncated |
| 66 | after the crash) also recover to OLD. |
| 67 | - A lock-free reader racing 150 commits never sees a torn or unverified state |
| 68 | and never sees the generation go backwards. |
| 69 | - Refusals: corrupted shadow, truncated generation (payload and header), |
| 70 | payload digest mismatch, pointer digest mismatch, validation reject, double |
| 71 | switch, stale shadow, tampered and truncated dispatch log, second writer, |
| 72 | re-create, bad SGD arguments. |
| 73 | - Replay: the same 25-step SGD sequence from G0 gives a byte-identical G25 |
| 74 | digest and dispatch chain head, in two stores and across the -O2 and |
| 75 | ASan/UBSan -O1 builds; each committed step re-derived from its base |
| 76 | generation plus its dispatch record matches the committed payload byte for |
| 77 | byte. |
| 78 | - SGD (plain and momentum) cuts a fixed quadratic loss by more than 10x in |
| 79 | 40 committed steps. |
| 80 | |
| 81 | ## Not done / limits |
| 82 | |
| 83 | - **Not a power-loss test.** Faults are process crashes; `fsync` ordering is |
| 84 | by construction and code review, not by cutting power. |
| 85 | - **Gradients are hand-written** (quadratic loss). No autodiff (M21), no |
| 86 | tensor API (M20); no GPU path. |
| 87 | - **Adam/AdamW deferred** (plan E4: "SGD first; Adam/AdamW after"). The |
| 88 | optimizer state is opaque bytes, so they fit the same substrate later. |
| 89 | - Reproducibility is claimed for the same binary and for the two test builds |
| 90 | on this host (AArch64, `-ffp-contract=off`), not across ISAs or compilers. |
| 91 | - No garbage collection of old generations (all are kept; history is |
| 92 | verifiable end to end). |
| 93 | - Dispatch records are held with the shadow and written at commit, so work |
| 94 | that is never committed leaves no tier (a) record. |
| 95 | - If the pointer rename succeeds but the directory `fsync` after it fails, |
| 96 | the commit returns `E_IO`, readers already see NEW, and the writer refuses |
| 97 | every further commit until the store is reopened (recovery decides). |
| 98 | This path is not exercised by a test (it needs a failing `fsync`). |