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 / train / omega-llm / native / ckpt.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm ckpt.om (aien-train#52): checkpoint OLLMCK01 / OLLMCK02 and gradient OLLMGR01 files |
| 3 | note (CONTRACT section 6, CONTRACT-V1 section 6). Needs math.om, data.om, model.om, adam.om before it. |
| 4 | note Files are streamed: a save encodes the f64 payload little-endian in chunks of OCKC values into |
| 5 | note ock, hashes each chunk (FNV-1a 64) and writes it at its file position through the platform |
| 6 | note unit (ocwopen, ocwput, ocwend; lio.om writes PATH.tmp, fsync, rename; eio.om the file in |
| 7 | note place), then writes the header at position 0. A load reads the header and then the payload in |
| 8 | note chunks through oprd into opw, hashing and decoding into the pools in one pass. |
| 9 | note CK01 header 112 bytes (FNV at 104); CK02 120 bytes (G at 104, FNV at 112); CK03 144 bytes (G at |
| 10 | note 104, the loop LP, LC, NP at 112, 120, 128, FNV at 136; docs/LOOP.md); GR01 112 bytes, step |
| 11 | note field = B, one payload. A model with a loop (NP > 1) is always CK03 and a CK03 file always |
| 12 | note has NP > 1, so a model without a loop keeps its CK01 or CK02 bytes. Largest file at the v3 |
| 13 | note maxima: 144 + 24 P with P = 29995392 (Wolfram: 144+24*29995392 -> 719889552); a file over |
| 14 | note OCKCAP = 719889553 bytes is refused with 15 before its header is read, as v0 refused a file |
| 15 | note over its image (exactly OCKCAP bytes goes to the header checks, then 15). |
| 16 | note Magic as a little-endian u64 (Wolfram FromDigits[Reverse[ToCharacterCode["OLLMCK01"]],256] |
| 17 | note -> 3544415659172318287; "OLLMGR01" -> 3544423372933581903; "OLLMCK02" -> 3616473253210246223; |
| 18 | note "OLLMCK03" -> 3688530847248174159). |
| 19 | note Load refusals (exit codes): 10 file shorter than its header or than header + 24 P, 11 bad magic, |
| 20 | note 12 a shape zero or over the maximum, V < 256, D mod H != 0, TOTAL > 999999, (CK02, CK03) G = 0, |
| 21 | note G > B or B mod G != 0, or (CK03) NP < 2, LC = 0, LP + LC > L or a loop the pools cannot hold, |
| 22 | note 13 P disagrees with the shapes, 14 FNV-1a mismatch, 15 file longer than header + 24 P, 17 a |
| 23 | note CK03 file given to a command that does not run loops (it loads with ockload, not ockloop); |
| 24 | note 70 I/O. |
| 25 | const OCKCAP ulong 719889553 |
| 26 | const OCKC ulong 65536 |
| 27 | global ock uchar 524288 |
| 28 | |
| 29 | fn ocput8 o ulong v ulong to void |
| 30 | for k ulong 8 |
| 31 | put ock add o k uchar and shr v mul k 8 255 |
| 32 | end |
| 33 | end |
| 34 | |
| 35 | |
| 36 | note little-endian u64 at ock[o .. o + 8) |
| 37 | fn ocget8 o ulong to ulong |
| 38 | v = ulong 0 |
| 39 | for k ulong 8 |
| 40 | v = or v shl ulong get ock add o k mul k 8 |
| 41 | end |
| 42 | ret v |
| 43 | end |
| 44 | |
| 45 | note decode m values from ock into gradient pool [o .. o + m) (a worker's gradient block, lio.om) |
| 46 | fn ocdecb o ulong m ulong to void |
| 47 | for j ulong m |
| 48 | ogrp add o j bitcast f64 ocget8 mul j 8 |
| 49 | end |
| 50 | end |
| 51 | note little-endian u64 at opw[o .. o + 8) |
| 52 | fn opw8 o ulong to ulong |
| 53 | v = ulong 0 |
| 54 | for k ulong 8 |
| 55 | v = or v shl ulong get opw add o k mul k 8 |
| 56 | end |
| 57 | ret v |
| 58 | end |
| 59 | |
| 60 | note FNV-1a 64 step over ock[0 .. n) from state h (basis cbf29ce484222325, prime 100000001b3) |
| 61 | fn ocfnvk h ulong n ulong to ulong |
| 62 | x = h |
| 63 | for i ulong n |
| 64 | x = mul xor x ulong get ock i 1099511628211 |
| 65 | end |
| 66 | ret x |
| 67 | end |
| 68 | |
| 69 | fn ocfnvw h ulong n ulong to ulong |
| 70 | x = h |
| 71 | for i ulong n |
| 72 | x = mul xor x ulong get opw i 1099511628211 |
| 73 | end |
| 74 | ret x |
| 75 | end |
| 76 | |
| 77 | note header fields from og into ock[0 .. 104); magic mg, step field st |
| 78 | fn ochead mg ulong st ulong to void |
| 79 | ocput8 0 mg |
| 80 | for i ulong 7 |
| 81 | ocput8 add 8 mul i 8 get og i |
| 82 | end |
| 83 | ocput8 64 get og 40 |
| 84 | ocput8 72 st |
| 85 | ocput8 80 get og 42 |
| 86 | ocput8 88 get og 43 |
| 87 | ocput8 96 get og 8 |
| 88 | end |
| 89 | |
| 90 | note encode m values of pool k (0 params, 1 m, 2 v, 3 GRD, 4 BLK) from value index i into ock |
| 91 | fn ocenc k ulong i ulong m ulong to void |
| 92 | if eq k 0 |
| 93 | for j ulong m |
| 94 | ocput8 mul j 8 bitcast ulong owg add i j |
| 95 | end |
| 96 | ret |
| 97 | end |
| 98 | if ge k 3 |
| 99 | o = add get og 10 i |
| 100 | if eq k 4 |
| 101 | o = add get og 8 i |
| 102 | end |
| 103 | for j ulong m |
| 104 | ocput8 mul j 8 bitcast ulong ogrg add o j |
| 105 | end |
| 106 | ret |
| 107 | end |
| 108 | o = add get og add 10 k i |
| 109 | for j ulong m |
| 110 | ocput8 mul j 8 bitcast ulong omvg add o j |
| 111 | end |
| 112 | end |
| 113 | |
| 114 | note decode m values from opw into pool k (0 params, 1 m, 2 v) from value index i |
| 115 | fn ocdec k ulong i ulong m ulong to void |
| 116 | if eq k 0 |
| 117 | for j ulong m |
| 118 | owp add i j bitcast f64 opw8 mul j 8 |
| 119 | end |
| 120 | ret |
| 121 | end |
| 122 | o = add get og add 10 k i |
| 123 | for j ulong m |
| 124 | omvp add o j bitcast f64 opw8 mul j 8 |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note stream pools ks .. ke-1 (ocenc numbering) after a header of hl bytes; returns the FNV-1a |
| 129 | fn ocbody ks ulong ke ulong hl ulong to ulong |
| 130 | p = get og 8 |
| 131 | h = ulong 14695981039346656037 |
| 132 | pos = hl |
| 133 | nc = div add p sub OCKC 1 OCKC |
| 134 | for k ulong sub ke ks |
| 135 | for c ulong nc |
| 136 | i = mul c OCKC |
| 137 | m = sub p i |
| 138 | if gt m OCKC |
| 139 | m = OCKC |
| 140 | end |
| 141 | ocenc add ks k i m |
| 142 | nb = mul m 8 |
| 143 | h = ocfnvk h nb |
| 144 | ocwput pos nb |
| 145 | pos = add pos nb |
| 146 | end |
| 147 | end |
| 148 | ret h |
| 149 | end |
| 150 | |
| 151 | note write checkpoint (params, m, v) to argv[ai]: OLLMCK03 when the model has a loop (og 59 > 1), |
| 152 | note else with the loaded or initialised magic (og 46) |
| 153 | fn ocksave ai ulong to void |
| 154 | ocwopen ai |
| 155 | hl = ulong 112 |
| 156 | if eq get og 46 2 |
| 157 | hl = 120 |
| 158 | end |
| 159 | if gt get og 59 1 |
| 160 | hl = 144 |
| 161 | end |
| 162 | h = ocbody 0 3 hl |
| 163 | if eq hl 112 |
| 164 | ochead 3544415659172318287 get og 41 |
| 165 | ocput8 104 h |
| 166 | end |
| 167 | if eq hl 120 |
| 168 | ochead 3616473253210246223 get og 41 |
| 169 | ocput8 104 get og 44 |
| 170 | ocput8 112 h |
| 171 | end |
| 172 | if eq hl 144 |
| 173 | ochead 3688530847248174159 get og 41 |
| 174 | ocput8 104 get og 44 |
| 175 | ocput8 112 get og 57 |
| 176 | ocput8 120 get og 58 |
| 177 | ocput8 128 get og 59 |
| 178 | ocput8 136 h |
| 179 | end |
| 180 | ocwput 0 hl |
| 181 | ocwend ai |
| 182 | end |
| 183 | |
| 184 | note write gradient file GR01: header with step = B, payload GRD (for either checkpoint magic) |
| 185 | fn ogrsave ai ulong to void |
| 186 | ocwopen ai |
| 187 | h = ocbody 3 4 112 |
| 188 | ochead 3544423372933581903 get og 6 |
| 189 | ocput8 104 h |
| 190 | ocwput 0 112 |
| 191 | ocwend ai |
| 192 | end |
| 193 | |
| 194 | note load the checkpoint named by argv[ai] for a command that runs loops (train, eval, sample, |
| 195 | note grad, gcheck, tune, kvcmp, serve on the CPU) |
| 196 | fn ockloop ai ulong to void |
| 197 | put og 60 1 |
| 198 | ockload ai |
| 199 | end |
| 200 | |
| 201 | note the magic mg of a checkpoint header: its length (112, 120 or 144) and og 46; exit 11 otherwise |
| 202 | fn ockmag mg ulong to ulong |
| 203 | if eq mg 3544415659172318287 |
| 204 | put og 46 1 |
| 205 | ret 112 |
| 206 | end |
| 207 | if eq mg 3616473253210246223 |
| 208 | put og 46 2 |
| 209 | ret 120 |
| 210 | end |
| 211 | if ne mg 3688530847248174159 |
| 212 | exit 11 |
| 213 | end |
| 214 | put og 46 3 |
| 215 | ret 144 |
| 216 | end |
| 217 | |
| 218 | note load the checkpoint named by argv[ai] into og, ow (params) and omv (m, v) |
| 219 | fn ockload ai ulong to void |
| 220 | n = ofsz ai |
| 221 | if gt n OCKCAP |
| 222 | exit 15 |
| 223 | end |
| 224 | hn = oprd ai 0 0 144 |
| 225 | if lt n 112 |
| 226 | exit 10 |
| 227 | end |
| 228 | hl = ockmag opw8 0 |
| 229 | if lt n hl |
| 230 | exit 10 |
| 231 | end |
| 232 | for i ulong 7 |
| 233 | put og i opw8 add 8 mul i 8 |
| 234 | end |
| 235 | put og 57 0 |
| 236 | put og 58 0 |
| 237 | put og 59 0 |
| 238 | if eq hl 144 |
| 239 | if eq get og 60 0 |
| 240 | exit 17 |
| 241 | end |
| 242 | put og 57 opw8 112 |
| 243 | put og 58 opw8 120 |
| 244 | put og 59 opw8 128 |
| 245 | if lt get og 59 2 |
| 246 | exit 12 |
| 247 | end |
| 248 | end |
| 249 | if not ogeom |
| 250 | exit 12 |
| 251 | end |
| 252 | put og 40 opw8 64 |
| 253 | put og 41 opw8 72 |
| 254 | put og 42 opw8 80 |
| 255 | put og 43 opw8 88 |
| 256 | if gt get og 42 999999 |
| 257 | exit 12 |
| 258 | end |
| 259 | g = ulong 1 |
| 260 | if ge hl 120 |
| 261 | g = opw8 104 |
| 262 | end |
| 263 | if not ogok2 g |
| 264 | exit 12 |
| 265 | end |
| 266 | put og 44 g |
| 267 | put og 45 1 |
| 268 | p = get og 8 |
| 269 | if ne opw8 96 p |
| 270 | exit 13 |
| 271 | end |
| 272 | hs = opw8 sub hl 8 |
| 273 | pb = mul 24 p |
| 274 | if lt n add hl pb |
| 275 | exit 10 |
| 276 | end |
| 277 | if gt n add hl pb |
| 278 | exit 15 |
| 279 | end |
| 280 | if ne ocklp ai hl p hs 1 |
| 281 | exit 14 |
| 282 | end |
| 283 | end |
| 284 | |
| 285 | note one pass over the payload at hl: read, hash and decode each chunk; exit 70 on a short read. |
| 286 | note Returns 1 when the payload FNV equals hs, else 0. |
| 287 | fn ocklp ai ulong hl ulong p ulong hs ulong to ulong |
| 288 | h = ulong 14695981039346656037 |
| 289 | nc = div add p sub OCKC 1 OCKC |
| 290 | pos = hl |
| 291 | for k ulong 3 |
| 292 | for c ulong nc |
| 293 | i = mul c OCKC |
| 294 | m = sub p i |
| 295 | if gt m OCKC |
| 296 | m = OCKC |
| 297 | end |
| 298 | nb = mul m 8 |
| 299 | if ne oprd ai pos 0 nb nb |
| 300 | exit 70 |
| 301 | end |
| 302 | h = ocfnvw h nb |
| 303 | ocdec k i m |
| 304 | pos = add pos nb |
| 305 | end |
| 306 | end |
| 307 | if eq h hs |
| 308 | ret 1 |
| 309 | end |
| 310 | ret 0 |
| 311 | end |
| 312 | |
| 313 | note G against the loaded shapes: 1 <= G <= B and B mod G = 0 |
| 314 | fn ogok2 g ulong to bool |
| 315 | b = get og 6 |
| 316 | if or eq g 0 gt g b |
| 317 | ret false |
| 318 | end |
| 319 | ret eq rem b g 0 |
| 320 | end |