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/aien-train/blob/main/native/si19/select.om # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-train / native / si19 / select.om` 809 lines ยท 15975 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note aien-train#25 ORIG-2: an original candidate-selection scorer for the omexp-v1 records 3 note (docs/si19/ORIG1-CONTRACT.md), trained from a seeded random initialization with plain 4 note SGD on the squared error of the score against the normalized relative cost y. Two 5 note kinds: 1 affine (one 12-weight row per arm, 96 params; the constant feature x0 is 6 note the bias) and 2 one-hidden-layer ReLU (20 inputs = 12 state + 8 arm one-hot, H=32, 7 note 705 params). Every binary32 value is a bit pattern in a ulong; arithmetic goes only 8 note through add32/mul32; negation is xor with the sign bit. No float words. 9 note Trainer state (SS): 0 kind, 1 H, 2 seed, 3 lr bits (0 = no-update control), 4 epoch, 10 note 5 cursor into SR, 6 updates, 7 rng state, 8 mu16 (wsenc), 9 sh, 10 ntrain, 11 data 11 note fnv (RC[2] at init), 12 rng state at the start of the current epoch (SR is rebuilt from 12 note it), 13 best DEV loss sum bits (+inf until the first epoch), 14 epochs since the DEV 13 note loss improved, 15 nparams, 16 control (0 normal, 1 shuffled targets, 2 no-update), 14 note 17 last TRAIN loss sum bits, 18 best epoch, 19 FNV-1a over the initial params, 15 note 20..31 reserved 0. SP current params, SB params of the best DEV epoch (the init until 16 note the first epoch), SG gradients, SZ inputs (SZ[20] = last score), SH/SA hidden 17 note pre-activations/activations, SX shuffled-target permutation over pair = r*8 + a, 18 note SR epoch order, SI checkpoint image bytes, SE evaluation totals. 19 note Everything a resumed run needs is in SS, SP and SB: SR comes from SS[12], SX from the 20 note seed and the dataset. The scorer ranks admissible arms only; it never widens them. 21 global SP ulong 1024 22 global SG ulong 1024 23 global SB ulong 1024 24 global SZ ulong 24 25 global SH ulong 64 26 global SA ulong 64 27 global SS ulong 32 28 global SI uchar 32768 29 global SE ulong 64 30 global SX ulong 16384 31 global SR ulong 2048 32 33 fn slnext s ulong to ulong 34 note One step of the trainer LCG (the wsrand recurrence) on a caller-held state. 35 ret and add mul s `1664525` `1013904223` `4294967295` 36 end 37 38 fn slrand to ulong 39 n = slnext get SS 7 40 put SS 7 n 41 ret shr n 8 42 end 43 44 fn slkey x ulong to long 45 note Order key of a finite binary32 bit pattern: integer order equals value order. 46 if ne and x `2147483648` 0 47 ret neg long and x `2147483647` 48 end 49 ret long x 50 end 51 52 fn sllt a ulong b ulong to bool 53 ret lt slkey a slkey b 54 end 55 56 fn slnpar kind ulong to ulong 57 if eq kind 1 58 ret 96 59 end 60 ret 705 61 end 62 63 fn sldraw to ulong 64 note One weight: uniform in {-16..15}/128, exact binary32 bits. 65 ret wmbits sub long rem slrand 32 16 7 66 end 67 68 fn slweights to void 69 note Affine: 96 weights. MLP: W1 (640) and W2 (32) drawn in that order, b1 and b2 zero. 70 if eq get SS 0 1 71 for i ulong 96 72 put SP i sldraw 73 end 74 ret 75 end 76 for i ulong 640 77 put SP i sldraw 78 end 79 for h ulong 32 80 put SP add 672 h sldraw 81 end 82 end 83 84 fn slndraw to ulong 85 note Number of LCG draws slweights makes for the current kind. 86 if eq get SS 0 1 87 ret 96 88 end 89 ret 672 90 end 91 92 fn slfnvp n ulong to ulong 93 note FNV-1a 64 over the words SP[0..n). 94 h = ulong `14695981039346656037` 95 for i ulong n 96 h = mul xor h get SP i `1099511628211` 97 end 98 ret h 99 end 100 101 fn slfnvb n ulong to ulong 102 h = ulong `14695981039346656037` 103 for i ulong n 104 h = mul xor h get SB i `1099511628211` 105 end 106 ret h 107 end 108 109 fn slpairok r ulong a ulong to bool 110 note (r, a) is a TRAIN pair: an admitted TRAIN record and an admissible arm. 111 if ge r get RC 0 112 ret false 113 end 114 if ne oxpart r 0 115 ret false 116 end 117 ret oxcand r a 118 end 119 120 fn slxpack to ulong 121 note Compact the TRAIN pairs r*8+a in record order into SX[0..np); returns np. 122 k = ulong 0 123 for r ulong get RC 0 124 for a ulong 8 125 if slpairok r a 126 put SX k add mul r 8 a 127 k = add k 1 128 end 129 end 130 end 131 ret k 132 end 133 134 fn slxmix s ulong np ulong to ulong 135 note Fisher-Yates over SX[0..np) driven by the caller-held LCG state; returns the state. 136 i = np 137 while gt i 1 bound 16384 138 i = sub i 1 139 s = slnext s 140 j = rem shr s 8 add i 1 141 t = get SX i 142 put SX i get SX j 143 put SX j t 144 end 145 ret s 146 end 147 148 fn slxspread np ulong to void 149 note Spread the shuffled list: pair p of rank k gets SX[p] = list[k]; other p map to p. 150 note Walking p downward never overwrites a list entry that is still needed (k <= p). 151 k = np 152 p = ulong 16384 153 while gt p 0 bound 16384 154 p = sub p 1 155 if slpairok div p 8 rem p 8 156 k = sub k 1 157 put SX p get SX k 158 else 159 put SX p p 160 end 161 end 162 end 163 164 fn slshuf s ulong to ulong 165 note Shuffled-target control: a permutation of the TRAIN pairs from the trainer LCG. 166 np = slxpack 167 s = slxmix s np 168 slxspread np 169 ret s 170 end 171 172 fn slinit kind ulong seed ulong lr ulong ctrl ulong to void 173 note Declared initialization: seeded LCG weights, zero biases, SB = SP, no data prior. 174 for i ulong 32 175 put SS i 0 176 end 177 for i ulong 1024 178 put SP i 0 179 put SG i 0 180 put SB i 0 181 end 182 put SS 0 kind 183 put SS 1 32 184 put SS 2 seed 185 put SS 3 lr 186 put SS 7 seed 187 put SS 11 get RC 2 188 put SS 13 `2139095040` 189 put SS 15 slnpar kind 190 put SS 16 ctrl 191 slweights 192 put SS 19 slfnvp get SS 15 193 for i ulong 1024 194 put SB i get SP i 195 end 196 if eq ctrl 1 197 put SS 7 slshuf get SS 7 198 end 199 end 200 201 fn slfloor s long c long to long 202 q = div s c 203 if and lt s 0 ne mul q c s 204 q = sub q 1 205 end 206 ret q 207 end 208 209 fn slisqrt v long to long 210 r = long 0 211 while le mul add r 1 add r 1 v bound 100000 212 r = add r 1 213 end 214 ret r 215 end 216 217 fn slsh mu long c long to ulong 218 note sh = max(0, bitlen(s16) - 5), s16 the integer sqrt of the mean square deviation. 219 v = long 0 220 for r ulong get RC 0 221 for a ulong 8 222 if slpairok r a 223 d = sub oxt16 r a mu 224 v = add v mul d d 225 end 226 end 227 end 228 s = slisqrt div v c 229 b = ulong 0 230 while gt s 0 bound 64 231 s = shr s 1 232 b = add b 1 233 end 234 if le b 5 235 ret 0 236 end 237 ret sub b 5 238 end 239 240 fn slnorm to void 241 note mu16 = floor of the mean t16 over TRAIN pairs; sh per contract s.4; ntrain records. 242 s = long 0 243 c = long 0 244 nt = ulong 0 245 for r ulong get RC 0 246 if eq oxpart r 0 247 nt = add nt 1 248 end 249 for a ulong 8 250 if slpairok r a 251 s = add s oxt16 r a 252 c = add c 1 253 end 254 end 255 end 256 if eq c 0 257 exit 84 258 end 259 mu = slfloor s c 260 put SS 8 wsenc mu 261 put SS 9 slsh mu c 262 put SS 10 nt 263 end 264 265 fn sltarget r ulong a ulong to ulong 266 note y = (t16 - mu16) / 2^(4 + sh) as exact binary32; control 1 reads the pair through SX. 267 if eq get SS 16 1 268 p = get SX add mul r 8 a 269 r = div p 8 270 a = rem p 8 271 end 272 ret wmbits sub oxt16 r a wsdec get SS 8 add 4 get SS 9 273 end 274 275 fn slinput r ulong a ulong to void 276 note SZ[0..11] = x / 16 exactly, SZ[12..19] = arm one-hot. 277 oxfeat r 278 for j ulong 12 279 put SZ j wmbits get XF j 4 280 end 281 for k ulong 8 282 put SZ add 12 k 0 283 end 284 put SZ add 12 a `1065353216` 285 end 286 287 fn slaff a ulong to ulong 288 acc = ulong 0 289 b = mul a 12 290 for j ulong 12 291 acc = add32 acc mul32 get SP add b j get SZ j 292 end 293 ret acc 294 end 295 296 fn slhid h ulong to ulong 297 acc = get SP add 640 h 298 for i ulong 20 299 acc = add32 acc mul32 get SP add mul i 32 h get SZ i 300 end 301 ret acc 302 end 303 304 fn slpos x ulong to bool 305 note Strictly positive binary32 (the ReLU gate; +0, -0 and negatives are closed). 306 ret and eq and x `2147483648` 0 ne x 0 307 end 308 309 fn slmlp to ulong 310 nh = get SS 1 311 for h ulong nh 312 z = slhid h 313 put SH h z 314 if slpos z 315 put SA h z 316 else 317 put SA h 0 318 end 319 end 320 acc = get SP 704 321 for h ulong nh 322 acc = add32 acc mul32 get SP add 672 h get SA h 323 end 324 ret acc 325 end 326 327 fn slfwd r ulong a ulong to ulong 328 note Score of arm a in record r; keeps SZ, SH, SA and SZ[20] = score for slback. 329 slinput r a 330 out = ulong 0 331 if eq get SS 0 1 332 out = slaff a 333 else 334 out = slmlp 335 end 336 put SZ 20 out 337 ret out 338 end 339 340 fn slbhid h ulong dh ulong to void 341 k = add 640 h 342 put SG k add32 get SG k dh 343 for i ulong 20 344 k = add mul i 32 h 345 put SG k add32 get SG k mul32 dh get SZ i 346 end 347 end 348 349 fn slbmlp g ulong to void 350 note Backward through W2 and the ReLU gate: units at pre-activation exactly 0 pass nothing. 351 put SG 704 add32 get SG 704 g 352 nh = get SS 1 353 for h ulong nh 354 k = add 672 h 355 put SG k add32 get SG k mul32 g get SA h 356 if slpos get SH h 357 slbhid h mul32 g get SP k 358 end 359 end 360 end 361 362 fn slback r ulong a ulong y ulong to void 363 note dL/dtheta of L = (score - y)^2 for the (r, a) of the last slfwd, accumulated into SG. 364 d = add32 get SZ 20 xor y `2147483648` 365 g = add32 d d 366 if eq get SS 0 1 367 b = mul a 12 368 for j ulong 12 369 k = add b j 370 put SG k add32 get SG k mul32 g get SZ j 371 end 372 ret 373 end 374 slbmlp g 375 end 376 377 fn slstep to void 378 note SP = SP + (-(lr * SG)); SG cleared; a non-finite parameter stops the program (86). 379 lr = get SS 3 380 n = get SS 15 381 for i ulong n 382 v = add32 get SP i xor mul32 lr get SG i `2147483648` 383 if not affinite v 384 exit 86 385 end 386 put SP i v 387 put SG i 0 388 end 389 end 390 391 fn slpair r ulong a ulong to void 392 y = sltarget r a 393 out = slfwd r a 394 slback r a y 395 slstep 396 put SS 6 add get SS 6 1 397 end 398 399 fn slperm s ulong to ulong 400 note SR[0..ntrain) = TRAIN record indices, Fisher-Yates from the caller-held LCG state. 401 k = ulong 0 402 for r ulong get RC 0 403 if eq oxpart r 0 404 put SR k r 405 k = add k 1 406 end 407 end 408 i = k 409 while gt i 1 bound 2048 410 i = sub i 1 411 s = slnext s 412 j = rem shr s 8 add i 1 413 t = get SR i 414 put SR i get SR j 415 put SR j t 416 end 417 ret s 418 end 419 420 fn slrun n ulong to void 421 note Up to n TRAIN records from the cursor, every admissible arm one update. The epoch 422 note order is rebuilt from SS[12]; SS[12] is fixed (and the rng advanced) only at cursor 0 423 note and only when there is work to do, so a zero-length run leaves the state untouched. 424 if or eq n 0 ge get SS 5 get SS 10 425 ret 426 end 427 if eq get SS 5 0 428 put SS 12 get SS 7 429 put SS 7 slperm get SS 12 430 else 431 s = slperm get SS 12 432 end 433 c = get SS 5 434 e = add c n 435 if gt e get SS 10 436 e = get SS 10 437 end 438 while lt c e bound 2048 439 r = get SR c 440 for a ulong 8 441 if oxcand r a 442 slpair r a 443 end 444 end 445 c = add c 1 446 put SS 5 c 447 end 448 end 449 450 fn slrloss r ulong to ulong 451 note Squared error summed over the admissible arms of record r. 452 s = ulong 0 453 for a ulong 8 454 if oxcand r a 455 d = add32 slfwd r a xor sltarget r a `2147483648` 456 s = add32 s mul32 d d 457 end 458 end 459 ret s 460 end 461 462 fn slloss part ulong to ulong 463 s = ulong 0 464 for r ulong get RC 0 465 if eq oxpart r part 466 s = add32 s slrloss r 467 end 468 end 469 ret s 470 end 471 472 fn slcheck to void 473 note DEV loss after an epoch: early-stop bookkeeping and the SB snapshot when improved. 474 dev = slloss 1 475 put SS 17 slloss 0 476 if lt dev get SS 13 477 put SS 13 dev 478 put SS 14 0 479 put SS 18 get SS 4 480 for i ulong 1024 481 put SB i get SP i 482 end 483 else 484 put SS 14 add get SS 14 1 485 end 486 end 487 488 fn slepoch to void 489 slrun get SS 10 490 put SS 4 add get SS 4 1 491 put SS 5 0 492 put SS 12 get SS 7 493 slcheck 494 end 495 496 fn slgo maxep ulong patience ulong stop ulong to bool 497 e = get SS 4 498 if ge e maxep 499 ret false 500 end 501 if ge get SS 14 patience 502 ret false 503 end 504 if and ne stop 0 ge e stop 505 ret false 506 end 507 ret true 508 end 509 510 fn sltrain maxep ulong patience ulong stop ulong to void 511 note Epochs until maxep, until DEV has not improved for patience epochs, or until the 512 note epoch counter reaches stop (0 = no partial stop). 513 while slgo maxep patience stop bound 65 514 slepoch 515 end 516 end 517 518 fn slbest to void 519 note Install the best-on-DEV parameters as the current ones (evaluation only). 520 for i ulong 1024 521 put SP i get SB i 522 end 523 end 524 525 fn siput pos ulong v ulong to void 526 for i ulong 8 527 put SI add pos i uchar and shr v mul i 8 255 528 end 529 end 530 531 fn siget pos ulong to ulong 532 v = ulong 0 533 for i ulong 8 534 v = or v shl ulong get SI add pos i mul i 8 535 end 536 ret v 537 end 538 539 fn sifnv base ulong n ulong to ulong 540 note FNV-1a 64 over SI[base..base+n): integrity of the image, not authentication. 541 h = ulong `14695981039346656037` 542 for i ulong n 543 h = mul xor h ulong get SI add base i `1099511628211` 544 end 545 ret h 546 end 547 548 fn sllen kind ulong to ulong 549 note Image length: 16 header + 32 SS words + SP + SB + 8 fnv = 280 + 16 nparams. 550 ret add 280 mul 16 slnpar kind 551 end 552 553 fn slsave base ulong to ulong 554 note Image: magic "SI19SLCK", version 1, SS[0..31], SP[0..n), SB[0..n), FNV-1a 64. 555 n = get SS 15 556 siput base `5423262296609737043` 557 siput add base 8 1 558 for i ulong 32 559 siput add add base 16 mul i 8 get SS i 560 end 561 p = add base 272 562 for i ulong n 563 siput add p mul i 8 get SP i 564 end 565 p = add p mul n 8 566 for i ulong n 567 siput add p mul i 8 get SB i 568 end 569 p = add p mul n 8 570 siput p sifnv base sub p base 571 ret add 8 sub p base 572 end 573 574 fn sllrok lr ulong to bool 575 note Admitted learning rates: 0 (no-update control), 2^-8, 2^-10, 2^-12 (contract s.8). 576 if or eq lr 0 eq lr wmbits 1 8 577 ret true 578 end 579 ret or eq lr wmbits 1 10 eq lr wmbits 1 12 580 end 581 582 fn slrange base ulong to int 583 note State ranges of an image: epoch, cursor, updates, lr, control, ntrain. 584 if gt siget add base 48 64 585 ret -6 586 end 587 if gt siget add base 56 siget add base 96 588 ret -6 589 end 590 if gt siget add base 64 `1048576` 591 ret -6 592 end 593 if not sllrok siget add base 40 594 ret -6 595 end 596 if gt siget add base 144 2 597 ret -6 598 end 599 if gt siget add base 176 1 600 ret -6 601 end 602 if gt siget add base 96 2048 603 ret -6 604 end 605 ret 0 606 end 607 608 fn slfinite base ulong n ulong to bool 609 note Every SP and SB word of the image is a finite binary32. 610 c = mul 2 n 611 for i ulong c 612 if not affinite siget add add base 272 mul i 8 613 ret false 614 end 615 end 616 ret true 617 end 618 619 fn slxseed to ulong 620 note The rng state right after slweights: the state slinit built SX from. 621 s = get SS 2 622 k = slndraw 623 for i ulong k 624 s = slnext s 625 end 626 ret s 627 end 628 629 fn slcopy base ulong n ulong to void 630 for i ulong 32 631 put SS i siget add add base 16 mul i 8 632 end 633 for i ulong 1024 634 put SP i 0 635 put SB i 0 636 put SG i 0 637 end 638 p = add base 272 639 for i ulong n 640 put SP i siget add p mul i 8 641 end 642 p = add p mul n 8 643 for i ulong n 644 put SB i siget add p mul i 8 645 end 646 if eq get SS 16 1 647 p = slshuf slxseed 648 end 649 end 650 651 fn slverify base ulong n ulong to int 652 note Every image check except the training dataset binding; replaces nothing. 653 if and ne n 1816 ne n 11560 654 ret -1 655 end 656 if ne siget base `5423262296609737043` 657 ret -2 658 end 659 if ne siget add base 8 1 660 ret -3 661 end 662 kind = siget add base 16 663 if or lt kind 1 gt kind 2 664 ret -4 665 end 666 if ne n sllen kind 667 ret -4 668 end 669 if ne siget add base 24 32 670 ret -4 671 end 672 if ne siget add base 136 slnpar kind 673 ret -4 674 end 675 if ne sifnv base sub n 8 siget add base sub n 8 676 ret -5 677 end 678 st = slrange base 679 if ne st 0 680 ret st 681 end 682 if not slfinite base slnpar kind 683 ret -7 684 end 685 ret 0 686 end 687 688 fn sladmit base ulong n ulong to int 689 note Admission of SI[base..base+n) for inference and evaluation over any admitted 690 note dataset: every check except the training dataset binding; nothing is replaced 691 note before every check passed. The bound dataset hash stays readable at base+104. 692 st = slverify base n 693 if ne st 0 694 ret st 695 end 696 slcopy base slnpar siget add base 16 697 ret 0 698 end 699 700 fn slbound base ulong to bool 701 note Does the image's training dataset hash equal the admitted dataset's? 702 ret eq siget add base 104 get RC 2 703 end 704 705 fn slload base ulong n ulong to int 706 note Admission for resumed training: slverify plus the dataset binding (-8), checked 707 note before anything is replaced. 708 st = slverify base n 709 if ne st 0 710 ret st 711 end 712 if not slbound base 713 ret -8 714 end 715 slcopy base slnpar siget add base 16 716 ret 0 717 end 718 719 fn slpick r ulong to ulong 720 note The model's choice: admissible arm with the smallest score, lowest index on a tie. 721 best = ulong 0 722 bs = slfwd r 0 723 for k ulong 7 724 a = add k 1 725 if oxcand r a 726 s = slfwd r a 727 if sllt s bs 728 bs = s 729 best = a 730 end 731 end 732 end 733 ret best 734 end 735 736 fn slstat r ulong to ulong 737 note Best-static baseline arm of the record's core class, reference when not admissible. 738 a = oxstatic oxcore r 739 if oxcand r a 740 ret a 741 end 742 ret 0 743 end 744 745 fn slnear r ulong to ulong 746 note Retrieval baseline: the nearest TRAIN record's oracle arm, reference when not admissible. 747 k = oxnear r 748 if ge k `1048576` 749 ret 0 750 end 751 a = oxbest k 752 if oxcand r a 753 ret a 754 end 755 ret 0 756 end 757 758 fn slwl r ulong cm ulong a ulong k ulong to void 759 note Paired bookkeeping against arm a: SE[k] wins (model cheaper), SE[k+1] losses. 760 c = oxcost r a 761 if lt cm c 762 put SE k add get SE k 1 763 end 764 if gt cm c 765 put SE add k 1 add get SE add k 1 1 766 end 767 end 768 769 fn slevalr r ulong to void 770 m = slpick r 771 cm = oxcost r m 772 put SE 0 add get SE 0 1 773 put SE 1 add get SE 1 cm 774 co = oxcost r oxbest r 775 put SE 2 add get SE 2 co 776 a = oxrule r 777 put SE 3 add get SE 3 oxcost r a 778 slwl r cm a 9 779 put SE 4 add get SE 4 oxcost r 0 780 slwl r cm 0 15 781 a = slstat r 782 put SE 5 add get SE 5 oxcost r a 783 slwl r cm a 13 784 a = slnear r 785 put SE 6 add get SE 6 oxcost r a 786 slwl r cm a 11 787 if eq cm co 788 put SE 8 add get SE 8 1 789 end 790 end 791 792 fn slevalc part ulong core ulong to void 793 note Totals over the records of a partition into SE[0..16] (spec layout); core 3 = all 794 note core classes. Top-1 agreement counts a choice whose cost equals the oracle's. 795 for i ulong 17 796 put SE i 0 797 end 798 for r ulong get RC 0 799 if eq oxpart r part 800 if or eq core 3 eq oxcore r core 801 slevalr r 802 end 803 end 804 end 805 end 806 807 fn sleval part ulong to void 808 slevalc part 3 809 end