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 / native / omegallm / lio.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words. |
| 3 | note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both |
| 4 | note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit, |
| 5 | note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is |
| 6 | note free of host words. |
| 7 | global owfd int 2 |
| 8 | global opth uchar 2100 |
| 9 | global opk uchar 4200 |
| 10 | global ocfd int 1 |
| 11 | global opp uchar 8 |
| 12 | global ost uchar 8 |
| 13 | global owrf int 64 |
| 14 | global owpd int 64 |
| 15 | |
| 16 | note the argument count (argv[0] included) |
| 17 | fn oargc to ulong |
| 18 | ret ulong argcnt |
| 19 | end |
| 20 | |
| 21 | note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument) |
| 22 | fn oargr i ulong to long |
| 23 | ret argread i oarg 0 4096 |
| 24 | end |
| 25 | |
| 26 | fn main to int |
| 27 | if lt oargc 2 |
| 28 | exit 2 |
| 29 | end |
| 30 | ret ocmd |
| 31 | end |
| 32 | |
| 33 | note onsnow: monotonic nanoseconds (kvcmp's step timing in cli.om) |
| 34 | fn onsnow to ulong |
| 35 | ret ulong monons |
| 36 | end |
| 37 | |
| 38 | fn oflush to void |
| 39 | n = get oon 0 |
| 40 | p = ulong 0 |
| 41 | while lt p n bound 4097 |
| 42 | w = write 1 oout p sub n p |
| 43 | if le w 0 |
| 44 | exit 70 |
| 45 | end |
| 46 | p = add p ulong w |
| 47 | end |
| 48 | put oon 0 0 |
| 49 | end |
| 50 | |
| 51 | note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied) |
| 52 | fn ocwopen ai ulong to void |
| 53 | pl = oargp ai |
| 54 | if gt pl 2000 |
| 55 | exit 2 |
| 56 | end |
| 57 | for i ulong pl |
| 58 | put opth i get oarg i |
| 59 | end |
| 60 | put opth pl 46 |
| 61 | put opth add pl 1 116 |
| 62 | put opth add pl 2 109 |
| 63 | put opth add pl 3 112 |
| 64 | fd = int fopen opth 0 add pl 4 577 420 |
| 65 | if lt fd 0 |
| 66 | exit 70 |
| 67 | end |
| 68 | put ocfd 0 fd |
| 69 | end |
| 70 | |
| 71 | note write ock[0 .. n) at byte position pos of the open PATH.tmp |
| 72 | fn ocwput pos ulong n ulong to void |
| 73 | fd = get ocfd 0 |
| 74 | p = ulong 0 |
| 75 | while lt p n bound 999999 |
| 76 | w = pwrite fd ock p sub n p long add pos p |
| 77 | if le w 0 |
| 78 | exit 70 |
| 79 | end |
| 80 | p = add p ulong w |
| 81 | end |
| 82 | end |
| 83 | |
| 84 | note fsync and close PATH.tmp, then rename it onto the path in argv[ai] |
| 85 | fn ocwend ai ulong to void |
| 86 | fd = get ocfd 0 |
| 87 | if ne fsync fd 0 |
| 88 | exit 70 |
| 89 | end |
| 90 | if ne fclose fd 0 |
| 91 | exit 70 |
| 92 | end |
| 93 | ocrename oargp ai |
| 94 | end |
| 95 | |
| 96 | note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list |
| 97 | fn ocrename pl ulong to void |
| 98 | put opk 0 2 |
| 99 | put opk 1 0 |
| 100 | put opk 2 uchar and add pl 4 255 |
| 101 | put opk 3 uchar shr add pl 4 8 |
| 102 | for i ulong add pl 4 |
| 103 | put opk add 4 i get opth i |
| 104 | end |
| 105 | q = add 8 pl |
| 106 | put opk q uchar and pl 255 |
| 107 | put opk add q 1 uchar shr pl 8 |
| 108 | for j ulong pl |
| 109 | put opk add add q 2 j get opth j |
| 110 | end |
| 111 | r = renames opk 0 add add q 2 pl 0 |
| 112 | if ne r 0 |
| 113 | exit 70 |
| 114 | end |
| 115 | end |
| 116 | |
| 117 | note size in bytes of the file named by argv[ai] (seek to its end) |
| 118 | fn ofsz ai ulong to ulong |
| 119 | n = oargp ai |
| 120 | fd = int fopen oarg 0 n 0 0 |
| 121 | if lt fd 0 |
| 122 | exit 70 |
| 123 | end |
| 124 | sz = fseek fd 0 2 |
| 125 | if lt sz 0 |
| 126 | exit 70 |
| 127 | end |
| 128 | if ne fclose fd 0 |
| 129 | exit 70 |
| 130 | end |
| 131 | ret ulong sz |
| 132 | end |
| 133 | |
| 134 | note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3 |
| 135 | note maxima (3 29995392 -> 89986176 values, 719889408 bytes), in one private anonymous mapping made |
| 136 | note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited |
| 137 | note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70). |
| 138 | const OCSN ulong 89986176 |
| 139 | global ocsh long 1 |
| 140 | global ocsf ulong 1 |
| 141 | |
| 142 | note capacity of the cold store in f64 values (maps it on the first call) |
| 143 | fn ocsinit to ulong |
| 144 | if eq get ocsf 0 0 |
| 145 | h = mapnew int -1 0 mul OCSN 8 3 34 |
| 146 | if lt h 0 |
| 147 | exit 70 |
| 148 | end |
| 149 | put ocsh 0 h |
| 150 | put ocsf 0 1 |
| 151 | end |
| 152 | ret OCSN |
| 153 | end |
| 154 | |
| 155 | fn ocsg i ulong to f64 |
| 156 | ret bitcast f64 mapget get ocsh 0 mul i 8 8 |
| 157 | end |
| 158 | |
| 159 | fn ocsp i ulong v f64 to void |
| 160 | x = mapset get ocsh 0 mul i 8 8 bitcast ulong v |
| 161 | end |
| 162 | |
| 163 | note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is |
| 164 | note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w |
| 165 | note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each |
| 166 | note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes |
| 167 | note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD |
| 168 | note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the |
| 169 | note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that |
| 170 | note does not exit 0 is an I/O refusal (exit 70). |
| 171 | fn ogradw to f64 |
| 172 | k = get og 45 |
| 173 | if le k 1 |
| 174 | ret ograd |
| 175 | end |
| 176 | per = div get og 44 k |
| 177 | oflush |
| 178 | for w ulong sub k 1 |
| 179 | ofork1 w per |
| 180 | end |
| 181 | for j ulong per |
| 182 | oblkc j |
| 183 | ogfold eq j 0 |
| 184 | end |
| 185 | for w ulong sub k 1 |
| 186 | ojoin1 w per |
| 187 | end |
| 188 | ret olsum |
| 189 | end |
| 190 | |
| 191 | note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid |
| 192 | fn ofork1 w ulong per ulong to void |
| 193 | r = pipefds opp 0 8 0 |
| 194 | if ne r 0 |
| 195 | exit 70 |
| 196 | end |
| 197 | rf = int or ulong get opp 0 shl ulong get opp 1 8 |
| 198 | wf = int or ulong get opp 4 shl ulong get opp 5 8 |
| 199 | pid = fork |
| 200 | if lt pid 0 |
| 201 | exit 70 |
| 202 | end |
| 203 | if eq pid 0 |
| 204 | c = fclose rf |
| 205 | owork wf mul add w 1 per per |
| 206 | exit 0 |
| 207 | end |
| 208 | c2 = fclose wf |
| 209 | put owrf w rf |
| 210 | put owpd w int pid |
| 211 | end |
| 212 | |
| 213 | note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it |
| 214 | fn ojoin1 w ulong per ulong to void |
| 215 | rf = get owrf w |
| 216 | for j ulong per |
| 217 | orecvb rf |
| 218 | ogadd |
| 219 | end |
| 220 | r = div get og 6 get og 44 |
| 221 | m = mul per r |
| 222 | orecvl rf mul add w 1 m m |
| 223 | c = fclose rf |
| 224 | pd = get owpd w |
| 225 | x = waitpid pd ost 0 4 0 |
| 226 | if ne x long pd |
| 227 | exit 70 |
| 228 | end |
| 229 | s0 = get ost 0 |
| 230 | s1 = get ost 1 |
| 231 | if ne or s0 s1 0 |
| 232 | exit 70 |
| 233 | end |
| 234 | end |
| 235 | |
| 236 | note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then |
| 237 | note the losses of its sequences |
| 238 | fn owork fd int j0 ulong n ulong to void |
| 239 | for t ulong n |
| 240 | oblkc add j0 t |
| 241 | osendb fd |
| 242 | end |
| 243 | r = div get og 6 get og 44 |
| 244 | b0 = mul j0 r |
| 245 | m = mul n r |
| 246 | for i ulong m |
| 247 | ocput8 mul i 8 bitcast ulong get olos add b0 i |
| 248 | end |
| 249 | owall fd mul m 8 |
| 250 | end |
| 251 | |
| 252 | fn owall fd int n ulong to void |
| 253 | p = ulong 0 |
| 254 | while lt p n bound 999999 |
| 255 | w = write fd ock p sub n p |
| 256 | if le w 0 |
| 257 | exit 70 |
| 258 | end |
| 259 | p = add p ulong w |
| 260 | end |
| 261 | end |
| 262 | |
| 263 | fn ordall fd int n ulong to void |
| 264 | p = ulong 0 |
| 265 | while lt p n bound 999999 |
| 266 | r = read fd ock p sub n p |
| 267 | if le r 0 |
| 268 | exit 70 |
| 269 | end |
| 270 | p = add p ulong r |
| 271 | end |
| 272 | end |
| 273 | |
| 274 | note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values |
| 275 | fn osendb fd int to void |
| 276 | p = get og 8 |
| 277 | nc = div add p sub OCKC 1 OCKC |
| 278 | for c ulong nc |
| 279 | i = mul c OCKC |
| 280 | m = sub p i |
| 281 | if gt m OCKC |
| 282 | m = OCKC |
| 283 | end |
| 284 | ocenc 4 i m |
| 285 | owall fd mul m 8 |
| 286 | end |
| 287 | end |
| 288 | |
| 289 | note receive one block into BLK (gradient pool [P .. 2P)) |
| 290 | fn orecvb fd int to void |
| 291 | p = get og 8 |
| 292 | nc = div add p sub OCKC 1 OCKC |
| 293 | for c ulong nc |
| 294 | i = mul c OCKC |
| 295 | m = sub p i |
| 296 | if gt m OCKC |
| 297 | m = OCKC |
| 298 | end |
| 299 | ordall fd mul m 8 |
| 300 | ocdecb add p i m |
| 301 | end |
| 302 | end |
| 303 | |
| 304 | note receive n losses into olos[b0 ..) |
| 305 | fn orecvl fd int b0 ulong n ulong to void |
| 306 | ordall fd mul n 8 |
| 307 | for i ulong n |
| 308 | put olos add b0 i bitcast f64 ocget8 mul i 8 |
| 309 | end |
| 310 | end |
| 311 | |
| 312 | note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21). |
| 313 | note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes |
| 314 | note of at most 999999 inner passes each cover 2^25 + 2. |
| 315 | fn odread i ulong to ulong |
| 316 | n = oargp i |
| 317 | fd = int fopen oarg 0 n 0 0 |
| 318 | if lt fd 0 |
| 319 | exit 70 |
| 320 | end |
| 321 | p = ulong 0 |
| 322 | dn = false |
| 323 | for o ulong 64 |
| 324 | q = ulong 0 |
| 325 | while and not dn lt q 999999 bound 999999 |
| 326 | q = add q 1 |
| 327 | r = long 0 |
| 328 | if lt p ODHALF |
| 329 | r = read fd od0 p sub ODHALF p |
| 330 | else |
| 331 | if lt p ODCAP |
| 332 | r = read fd od1 sub p ODHALF sub ODCAP p |
| 333 | else |
| 334 | r = read fd ochk 0 1 |
| 335 | end |
| 336 | end |
| 337 | if lt r 0 |
| 338 | exit 70 |
| 339 | end |
| 340 | if eq r 0 |
| 341 | dn = true |
| 342 | else |
| 343 | p = add p ulong r |
| 344 | if gt p ODCAP |
| 345 | exit 21 |
| 346 | end |
| 347 | end |
| 348 | end |
| 349 | if dn |
| 350 | break |
| 351 | end |
| 352 | end |
| 353 | if not dn |
| 354 | exit 70 |
| 355 | end |
| 356 | c = fclose fd |
| 357 | put odn 0 p |
| 358 | ret p |
| 359 | end |
| 360 | |
| 361 | note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD). |
| 362 | fn owflush k ulong to void |
| 363 | n = get owbn k |
| 364 | fd = get owfd k |
| 365 | p = ulong 0 |
| 366 | while lt p n bound 65537 |
| 367 | w = long 0 |
| 368 | if eq k 0 |
| 369 | w = write fd owb0 p sub n p |
| 370 | else |
| 371 | w = write fd owb1 p sub n p |
| 372 | end |
| 373 | if le w 0 |
| 374 | exit 70 |
| 375 | end |
| 376 | p = add p ulong w |
| 377 | end |
| 378 | put owbn k 0 |
| 379 | end |
| 380 | |
| 381 | fn owopen k ulong i ulong to void |
| 382 | n = oargp i |
| 383 | fd = int fopen oarg 0 n 577 420 |
| 384 | if lt fd 0 |
| 385 | exit 70 |
| 386 | end |
| 387 | put owfd k fd |
| 388 | put owbn k 0 |
| 389 | end |
| 390 | |
| 391 | fn owclose k ulong to void |
| 392 | owflush k |
| 393 | fd = get owfd k |
| 394 | if ne fsync fd 0 |
| 395 | exit 70 |
| 396 | end |
| 397 | if ne fclose fd 0 |
| 398 | exit 70 |
| 399 | end |
| 400 | end |
| 401 | |
| 402 | note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes |
| 403 | fn ooffrd ai ulong to void |
| 404 | n = oargp ai |
| 405 | fd = int fopen oarg 0 n 0 0 |
| 406 | if lt fd 0 |
| 407 | exit 70 |
| 408 | end |
| 409 | p = ulong 0 |
| 410 | dn = false |
| 411 | while not dn bound 70000 |
| 412 | r = read fd ochk p sub 65536 p |
| 413 | if lt r 0 |
| 414 | exit 70 |
| 415 | end |
| 416 | if eq r 0 |
| 417 | dn = true |
| 418 | else |
| 419 | p = add p ulong r |
| 420 | if ge p 65536 |
| 421 | exit 21 |
| 422 | end |
| 423 | end |
| 424 | end |
| 425 | c = fclose fd |
| 426 | if ne p mul 8 get og 6 |
| 427 | exit 21 |
| 428 | end |
| 429 | end |
| 430 | |
| 431 | |
| 432 | note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai], |
| 433 | note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns |
| 434 | note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an |
| 435 | note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one |
| 436 | note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones. |
| 437 | fn oprd ai ulong pos ulong off ulong n ulong to ulong |
| 438 | pl = oargp ai |
| 439 | fd = int fopen oarg 0 pl 0 0 |
| 440 | if lt fd 0 |
| 441 | exit 70 |
| 442 | end |
| 443 | p = ulong 0 |
| 444 | dn = false |
| 445 | for o ulong 3 |
| 446 | while and not dn lt p n bound 999999 |
| 447 | r = pread fd opw add off p sub n p long add pos p |
| 448 | if lt r 0 |
| 449 | exit 70 |
| 450 | end |
| 451 | if eq r 0 |
| 452 | dn = true |
| 453 | else |
| 454 | p = add p ulong r |
| 455 | end |
| 456 | end |
| 457 | end |
| 458 | if ne fclose fd 0 |
| 459 | exit 70 |
| 460 | end |
| 461 | ret p |
| 462 | end |
| 463 | |
| 464 | note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does: |
| 465 | note PATH.tmp, fsync, rename |
| 466 | fn obmsave ai ulong n ulong to void |
| 467 | pl = oargp ai |
| 468 | if gt pl 2000 |
| 469 | exit 2 |
| 470 | end |
| 471 | for i ulong pl |
| 472 | put opth i get oarg i |
| 473 | end |
| 474 | put opth pl 46 |
| 475 | put opth add pl 1 116 |
| 476 | put opth add pl 2 109 |
| 477 | put opth add pl 3 112 |
| 478 | fd = int fopen opth 0 add pl 4 577 420 |
| 479 | if lt fd 0 |
| 480 | exit 70 |
| 481 | end |
| 482 | p = ulong 0 |
| 483 | while lt p n bound 999999 |
| 484 | w = write fd obm p sub n p |
| 485 | if le w 0 |
| 486 | exit 70 |
| 487 | end |
| 488 | p = add p ulong w |
| 489 | end |
| 490 | if ne fsync fd 0 |
| 491 | exit 70 |
| 492 | end |
| 493 | if ne fclose fd 0 |
| 494 | exit 70 |
| 495 | end |
| 496 | ocrename pl |
| 497 | end |