Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-helix/blob/main/native/lio.om # omega-helix This project has not described itself yet. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-helix / native / lio.om` 615 lines ยท 11210 bytes Raw Blame History 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 global ommap_sz_g ulong 1 16 17 note the argument count (argv[0] included) 18 fn oargc to ulong 19 ret ulong argcnt 20 end 21 22 note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument) 23 fn oargr i ulong to long 24 ret argread i oarg 0 4096 25 end 26 27 fn main to int 28 if lt oargc 2 29 exit 2 30 end 31 ret ocmd 32 end 33 34 note onsnow: monotonic nanoseconds (kvcmp's step timing in cli.om) 35 fn onsnow to ulong 36 ret ulong monons 37 end 38 39 fn oflush to void 40 n = get oon 0 41 p = ulong 0 42 while lt p n bound 4097 43 w = write 1 oout p sub n p 44 if le w 0 45 exit 70 46 end 47 p = add p ulong w 48 end 49 put oon 0 0 50 end 51 52 note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied) 53 fn ocwopen ai ulong to void 54 pl = oargp ai 55 if gt pl 2000 56 exit 2 57 end 58 for i ulong pl 59 put opth i get oarg i 60 end 61 put opth pl 46 62 put opth add pl 1 116 63 put opth add pl 2 109 64 put opth add pl 3 112 65 fd = int fopen opth 0 add pl 4 577 420 66 if lt fd 0 67 exit 70 68 end 69 put ocfd 0 fd 70 end 71 72 note write ock[0 .. n) at byte position pos of the open PATH.tmp 73 fn ocwput pos ulong n ulong to void 74 fd = get ocfd 0 75 p = ulong 0 76 while lt p n bound 999999 77 w = pwrite fd ock p sub n p long add pos p 78 if le w 0 79 exit 70 80 end 81 p = add p ulong w 82 end 83 end 84 85 note fsync and close PATH.tmp, then rename it onto the path in argv[ai] 86 fn ocwend ai ulong to void 87 fd = get ocfd 0 88 if ne fsync fd 0 89 exit 70 90 end 91 if ne fclose fd 0 92 exit 70 93 end 94 ocrename oargp ai 95 end 96 97 note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list 98 fn ocrename pl ulong to void 99 put opk 0 2 100 put opk 1 0 101 put opk 2 uchar and add pl 4 255 102 put opk 3 uchar shr add pl 4 8 103 for i ulong add pl 4 104 put opk add 4 i get opth i 105 end 106 q = add 8 pl 107 put opk q uchar and pl 255 108 put opk add q 1 uchar shr pl 8 109 for j ulong pl 110 put opk add add q 2 j get opth j 111 end 112 r = renames opk 0 add add q 2 pl 0 113 if ne r 0 114 exit 70 115 end 116 end 117 118 note size in bytes of the file named by argv[ai] (seek to its end) 119 fn ofsz ai ulong to ulong 120 n = oargp ai 121 fd = int fopen oarg 0 n 0 0 122 if lt fd 0 123 exit 70 124 end 125 sz = fseek fd 0 2 126 if lt sz 0 127 exit 70 128 end 129 if ne fclose fd 0 130 exit 70 131 end 132 ret ulong sz 133 end 134 135 note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3 136 note maxima (3 `29995392` -> `89986176` values, `719889408` bytes), in one private anonymous mapping made 137 note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited 138 note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70). 139 const OCSN ulong `1500000000` 140 global ocsh long 1 141 global ocsf ulong 1 142 143 note capacity of the cold store in f64 values (maps it on the first call) 144 fn ocsinit to ulong 145 if eq get ocsf 0 0 146 h = mapnew int -1 0 mul OCSN 8 3 34 147 if lt h 0 148 exit 70 149 end 150 put ocsh 0 h 151 put ocsf 0 1 152 end 153 ret OCSN 154 end 155 156 fn ocsgrow need ulong to bool 157 if gt need OCSN 158 ret false 159 end 160 cap = ocsinit 161 ret le need cap 162 end 163 164 fn ocsg i ulong to f64 165 ret bitcast f64 mapget get ocsh 0 mul i 8 8 166 end 167 168 fn ocsp i ulong v f64 to void 169 x = mapset get ocsh 0 mul i 8 8 bitcast ulong v 170 end 171 172 note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is 173 note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w 174 note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each 175 note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes 176 note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD 177 note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the 178 note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that 179 note does not exit 0 is an I/O refusal (exit 70). 180 fn ogradw to f64 181 k = get og 45 182 if le k 1 183 ret ograd 184 end 185 per = div get og 44 k 186 oflush 187 for w ulong sub k 1 188 ofork1 w per 189 end 190 for j ulong per 191 oblkc j 192 ogfold eq j 0 193 end 194 for w ulong sub k 1 195 ojoin1 w per 196 end 197 ret olsum 198 end 199 200 note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid 201 fn ofork1 w ulong per ulong to void 202 r = pipefds opp 0 8 0 203 if ne r 0 204 exit 70 205 end 206 rf = int or ulong get opp 0 shl ulong get opp 1 8 207 wf = int or ulong get opp 4 shl ulong get opp 5 8 208 pid = fork 209 if lt pid 0 210 exit 70 211 end 212 if eq pid 0 213 c = fclose rf 214 owork wf mul add w 1 per per 215 exit 0 216 end 217 c2 = fclose wf 218 put owrf w rf 219 put owpd w int pid 220 end 221 222 note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it 223 fn ojoin1 w ulong per ulong to void 224 rf = get owrf w 225 for j ulong per 226 orecvb rf 227 ogadd 228 end 229 r = div get og 6 get og 44 230 m = mul per r 231 orecvl rf mul add w 1 m m 232 c = fclose rf 233 pd = get owpd w 234 x = waitpid pd ost 0 4 0 235 if ne x long pd 236 exit 70 237 end 238 s0 = get ost 0 239 s1 = get ost 1 240 if ne or s0 s1 0 241 exit 70 242 end 243 end 244 245 note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then 246 note the losses of its sequences 247 fn owork fd int j0 ulong n ulong to void 248 for t ulong n 249 oblkc add j0 t 250 osendb fd 251 end 252 r = div get og 6 get og 44 253 b0 = mul j0 r 254 m = mul n r 255 for i ulong m 256 ocput8 mul i 8 bitcast ulong get olos add b0 i 257 end 258 owall fd mul m 8 259 end 260 261 fn owall fd int n ulong to void 262 p = ulong 0 263 while lt p n bound 999999 264 w = write fd ock p sub n p 265 if le w 0 266 exit 70 267 end 268 p = add p ulong w 269 end 270 end 271 272 fn ordall fd int n ulong to void 273 p = ulong 0 274 while lt p n bound 999999 275 r = read fd ock p sub n p 276 if le r 0 277 exit 70 278 end 279 p = add p ulong r 280 end 281 end 282 283 note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values 284 fn osendb fd int to void 285 p = get og 8 286 nc = div add p sub OCKC 1 OCKC 287 for c ulong nc 288 i = mul c OCKC 289 m = sub p i 290 if gt m OCKC 291 m = OCKC 292 end 293 ocenc 4 i m 294 owall fd mul m 8 295 end 296 end 297 298 note receive one block into BLK (gradient pool [P .. 2P)) 299 fn orecvb fd int to void 300 p = get og 8 301 nc = div add p sub OCKC 1 OCKC 302 for c ulong nc 303 i = mul c OCKC 304 m = sub p i 305 if gt m OCKC 306 m = OCKC 307 end 308 ordall fd mul m 8 309 ocdecb add p i m 310 end 311 end 312 313 note receive n losses into olos[b0 ..) 314 fn orecvl fd int b0 ulong n ulong to void 315 ordall fd mul n 8 316 for i ulong n 317 put olos add b0 i bitcast f64 ocget8 mul i 8 318 end 319 end 320 321 note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21). 322 note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes 323 note of at most 999999 inner passes each cover 2^25 + 2. 324 fn odread i ulong to ulong 325 n = oargp i 326 fd = int fopen oarg 0 n 0 0 327 if lt fd 0 328 exit 70 329 end 330 p = ulong 0 331 dn = false 332 for o ulong 64 333 q = ulong 0 334 while and not dn lt q 999999 bound 999999 335 q = add q 1 336 r = long 0 337 if lt p ODHALF 338 r = read fd od0 p sub ODHALF p 339 else 340 if lt p ODCAP 341 r = read fd od1 sub p ODHALF sub ODCAP p 342 else 343 r = read fd ochk 0 1 344 end 345 end 346 if lt r 0 347 exit 70 348 end 349 if eq r 0 350 dn = true 351 else 352 p = add p ulong r 353 if gt p ODCAP 354 exit 21 355 end 356 end 357 end 358 if dn 359 break 360 end 361 end 362 if not dn 363 exit 70 364 end 365 c = fclose fd 366 put odn 0 p 367 ret p 368 end 369 370 note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD). 371 fn owflush k ulong to void 372 n = get owbn k 373 fd = get owfd k 374 p = ulong 0 375 while lt p n bound 65537 376 w = long 0 377 if eq k 0 378 w = write fd owb0 p sub n p 379 else 380 w = write fd owb1 p sub n p 381 end 382 if le w 0 383 exit 70 384 end 385 p = add p ulong w 386 end 387 put owbn k 0 388 end 389 390 fn owopen k ulong i ulong to void 391 n = oargp i 392 fd = int fopen oarg 0 n 577 420 393 if lt fd 0 394 exit 70 395 end 396 put owfd k fd 397 put owbn k 0 398 end 399 400 fn owclose k ulong to void 401 owflush k 402 fd = get owfd k 403 if ne fsync fd 0 404 exit 70 405 end 406 if ne fclose fd 0 407 exit 70 408 end 409 end 410 411 note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes 412 fn ooffrd ai ulong to void 413 n = oargp ai 414 fd = int fopen oarg 0 n 0 0 415 if lt fd 0 416 exit 70 417 end 418 p = ulong 0 419 dn = false 420 while not dn bound 70000 421 r = read fd ochk p sub 65536 p 422 if lt r 0 423 exit 70 424 end 425 if eq r 0 426 dn = true 427 else 428 p = add p ulong r 429 if ge p 65536 430 exit 21 431 end 432 end 433 end 434 c = fclose fd 435 if ne p mul 8 get og 6 436 exit 21 437 end 438 end 439 440 441 note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai], 442 note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns 443 note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an 444 note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one 445 note byte or ends, so n + 1 <= `2097153` passes suffice: 3 outer passes of at most 999999 inner ones. 446 fn oprd ai ulong pos ulong off ulong n ulong to ulong 447 pl = oargp ai 448 fd = int fopen oarg 0 pl 0 0 449 if lt fd 0 450 exit 70 451 end 452 p = ulong 0 453 dn = false 454 for o ulong 3 455 while and not dn lt p n bound 999999 456 r = pread fd opw add off p sub n p long add pos p 457 if lt r 0 458 exit 70 459 end 460 if eq r 0 461 dn = true 462 else 463 p = add p ulong r 464 end 465 end 466 end 467 if ne fclose fd 0 468 exit 70 469 end 470 ret p 471 end 472 473 note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does: 474 note PATH.tmp, fsync, rename 475 fn obmsave ai ulong n ulong to void 476 pl = oargp ai 477 if gt pl 2000 478 exit 2 479 end 480 for i ulong pl 481 put opth i get oarg i 482 end 483 put opth pl 46 484 put opth add pl 1 116 485 put opth add pl 2 109 486 put opth add pl 3 112 487 fd = int fopen opth 0 add pl 4 577 420 488 if lt fd 0 489 exit 70 490 end 491 p = ulong 0 492 while lt p n bound 999999 493 w = write fd obm p sub n p 494 if le w 0 495 exit 70 496 end 497 p = add p ulong w 498 end 499 if ne fsync fd 0 500 exit 70 501 end 502 if ne fclose fd 0 503 exit 70 504 end 505 ocrename pl 506 end 507 508 fn ocwput_opw pos ulong n ulong to void 509 fd = get ocfd 0 510 p = ulong 0 511 while lt p n bound 999999 512 w = pwrite fd opw p sub n p long add pos p 513 if le w 0 514 exit 70 515 end 516 p = add p ulong w 517 end 518 end 519 520 fn ommap_open ai ulong to long 521 pl = oargp ai 522 fd = int fopen oarg 0 pl 0 0 523 if lt fd 0 524 exit 70 525 end 526 sz = fseek fd 0 2 527 if lt sz 0 528 c = fclose fd 529 exit 70 530 end 531 put ommap_sz_g 0 ulong sz 532 if eq sz 0 533 zr = read fd ochk 0 1 534 if lt zr 0 535 c = fclose fd 536 exit 70 537 end 538 if ne fclose fd 0 539 exit 70 540 end 541 ret 0 542 end 543 h = mapnew fd 0 ulong sz 1 2 544 c = fclose fd 545 if ne c 0 546 if gt h 0 547 d = mapdrop h 548 end 549 exit 70 550 end 551 if le h 0 552 exit 70 553 end 554 ret h 555 end 556 557 fn ommap_sz to ulong 558 ret get ommap_sz_g 0 559 end 560 561 fn ommap_read h long off ulong n ulong to void 562 if eq n 0 563 ret 564 end 565 r = mapread h off opw 0 n 566 if ne r long n 567 exit 70 568 end 569 end 570 571 fn ommap_drop h long to void 572 if gt h 0 573 r = mapdrop h 574 if ne r 0 575 exit 70 576 end 577 end 578 end 579 580 fn ocswrite off ulong n ulong to bool 581 if eq get ocsf 0 0 582 ret false 583 end 584 if eq n 0 585 ret true 586 end 587 r = mapwrite get ocsh 0 off opw 0 n 588 if ne r long n 589 exit 70 590 end 591 ret true 592 end 593 594 fn ocsread off ulong n ulong to bool 595 if eq get ocsf 0 0 596 ret false 597 end 598 if eq n 0 599 ret true 600 end 601 r = mapread get ocsh 0 off ock 0 n 602 if ne r long n 603 exit 70 604 end 605 ret true 606 end 607 608 fn ommap_fnv64 h long off ulong nw ulong init_h ulong to ulong 609 x = init_h 610 for i ulong nw 611 w = mapget h add off mul i 8 8 612 x = mul xor x w `1099511628211` 613 end 614 ret x 615 end