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/omegallm/model.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 / omegallm / model.om` 1358 lines ยท 30671 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note omegallm model.om (aien-train#52): geometry, forward pass and hand-derived backward pass 3 note (CONTRACT sections 1-3, CONTRACT-V1 section 1). Needs math.om and data.om before it. 4 note 5 note f64 values live in banked pools (one omc global holds at most `16777216` elements, and all 6 note globals together at most 2^30 bytes, omc error 23; CONTRACT-V3 section 2): 7 note ow parameter bank A: [0, SB) of the CONTRACT section 2 order, then a one-layer window of 8 note LSTR values at og[36] and the final gain gf at og[37]; oww (lpool.om/epool.om) bank B holds 9 note layers LA .. L-1 at (l - LA) LSTR. When P <= OWC, SB = GF, LA = L and ow is the v2 layout. 10 note Every index outside the kernels is virtual (the section 2 order); owg/owp map it, and opx 11 note maps a hot index of bank A or the staged layer (og[48]). 12 note the gradient pool [0, 2P): GRD [0, P) in the platform cold store (ocsg/ocsp), BLK [P, 2P) in 13 note ogr[j] below OGC and ogb[j - OGC] from OGC (j = i - P), reached through ogrg/ogrp/ogra 14 note the Adam pool [0, 2P) (omvg/omvp): m at 0, v at P, at cold-store index P + i 15 note oa activations: layers below LAA at l ASTR, later layers in two windows at AW + (l mod 2) ASTR 16 note flushed to bank B oab at (l - LAA) ASTR after their forward and staged back before their 17 note backward; HF and the backward temporaries follow. When END <= OAC, LAA = L (v2 layout). 18 note opa attention probabilities PA[l][n][t][j] of every layer 19 note og slots: 20 note 0 V 1 T 2 D 3 H 4 F 5 L 6 B 7 E=D/H 8 P 9 CT (positions in use, <= T) 21 note 10 GRD (base in the gradient pool of the gradient being accumulated: 0, or P for BLK) 11 MOM 22 note (0 in the Adam pool) 12 VEL (P) 13 ACT (0 in oa) 14 ASTR (per-layer activation stride) 15 HF 23 note 16 SF 17 Z 18 PR 19 DH 20 DA 21 DQ 22 DK 23 DU 24 DO 25 DZ 26 DF 27 TMP 28 END 24 note 29 PSTR (per-layer PA stride H T T in opa) 30 WPE 31 LAY0 32 LSTR (per-layer parameter 25 note stride) 33 GF 34 SB (first virtual index of bank B) 35 LA (first layer in bank B) 36 window base 26 note in ow 37 gf in ow 38 LAA (first layer in the activation windows) 39 AW (activation window 27 note base) 48 the layer staged in the parameter window (L: none) 28 note 40 seed S 41 step 42 TOTAL 43 lr bits 44 G (gradient blocks) 45 K (workers) 29 note 46 checkpoint magic of the run (1 OLLMCK01, 2 OLLMCK02) 30 note Per layer activations (T rows of width D unless noted): HIN, A, Q, K, U, O, HMID, BN, S1[T], 31 note S2[T], FP[T][F] (pre-relu), RL[T][F]. Every sum runs over its index ascending. 32 note Sizes at the CONTRACT-V2 maxima (V 4096, T 256, D 384, H 16, F 1536, L 8), Wolfram: 33 note P = V D + T D + L (4 D^2 + 2 D F + 2 D) + D -> `15833472` 34 note ASTR = 8 T D + 2 T + 2 T F -> `1573376`; END = L ASTR + 10 T D + T + T V + T F + 4096 -> `15016192` 35 note and at the CONTRACT-V3 maxima (L 16): P `29995392`, PA = L H T T -> `16777216`; the bank-B sizes 36 note are in lpool.om. ogeom refuses any layout past a pool. 37 note Bank A capacities: OWC parameters, OGC BLK values, OAC activations (the bank-crossing test 38 note build of tests/omegallm/build.sh shrinks all three). 39 const OWC ulong `15833472` 40 global ow f64 `15833472` 41 const OGC ulong `15833472` 42 global ogr f64 `15833472` 43 const OAC ulong `15016192` 44 global oa f64 `15016192` 45 const OPAN ulong `16777216` 46 global opa f64 `16777216` 47 48 note gradient pool element i (i < 2 P) 49 fn ogrg i ulong to f64 50 p = get og 8 51 if lt i p 52 ret ocsg i 53 end 54 j = sub i p 55 if lt j OGC 56 ret get ogr j 57 end 58 ret get ogb sub j OGC 59 end 60 61 fn ogrp i ulong v f64 to void 62 p = get og 8 63 if lt i p 64 ocsp i v 65 ret 66 end 67 j = sub i p 68 if lt j OGC 69 put ogr j v 70 else 71 put ogb sub j OGC v 72 end 73 end 74 75 note gradient pool element i += a 76 fn ogra i ulong a f64 to void 77 p = get og 8 78 if lt i p 79 ocsp i add ocsg i a 80 ret 81 end 82 j = sub i p 83 if lt j OGC 84 put ogr j add get ogr j a 85 else 86 k = sub j OGC 87 put ogb k add get ogb k a 88 end 89 end 90 91 note Adam pool element i (i < 2 P), cold-store index P + i 92 fn omvg i ulong to f64 93 ret ocsg add get og 8 i 94 end 95 96 fn omvp i ulong v f64 to void 97 ocsp add get og 8 i v 98 end 99 100 note parameter at virtual index i (bank A, bank B or gf), and its store 101 fn owg i ulong to f64 102 sb = get og 34 103 if lt i sb 104 ret get ow i 105 end 106 g = get og 33 107 if ge i g 108 ret get ow add get og 37 sub i g 109 end 110 ret get oww sub i sb 111 end 112 113 fn owp i ulong v f64 to void 114 sb = get og 34 115 if lt i sb 116 put ow i v 117 ret 118 end 119 g = get og 33 120 if ge i g 121 put ow add get og 37 sub i g v 122 ret 123 end 124 put oww sub i sb v 125 end 126 127 note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only 128 note when it is the staged layer og[48] (else exit 16, a missing ostw) 129 fn opx i ulong to ulong 130 if lt i get og 34 131 ret i 132 end 133 g = get og 33 134 if ge i g 135 ret add get og 37 sub i g 136 end 137 l = div sub i get og 31 get og 32 138 if ne l get og 48 139 exit 16 140 end 141 ret add get og 36 sub i opg1 l 142 end 143 144 note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product, 145 note 0 in the mutant. 146 const OMUTY ulong 1 147 148 const OCH ulong 999999 149 150 note stage bank-B layer l into the parameter window (layers below LA live in bank A) 151 fn ostw l ulong to void 152 la = get og 35 153 if lt l la 154 ret 155 end 156 n = get og 32 157 s = mul sub l la n 158 if eq OMUTY 0 159 s = 0 160 end 161 ostwc get og 36 s n 162 put og 48 l 163 end 164 165 note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes) 166 fn ostwc wb ulong s ulong n ulong to void 167 for q ulong ocn n 168 i = mul q OCH 169 ie = oce q n 170 while lt i ie bound 999999 171 put ow add wb i get oww add s i 172 i = add i 1 173 end 174 end 175 end 176 177 note copy layer l's activation block between its window and bank B oab (to oab when fo) 178 fn oaflip l ulong fo bool to void 179 la = get og 38 180 if lt l la 181 ret 182 end 183 n = get og 14 184 s = mul sub l la n 185 oaflc oahin l s n fo 186 end 187 188 note oab[s + i] = oa[w + i] for i < n when fo, else the reverse 189 fn oaflc w ulong s ulong n ulong fo bool to void 190 if fo 191 oafo w s n 192 else 193 oafi w s n 194 end 195 end 196 197 fn oafo w ulong s ulong n ulong to void 198 for q ulong ocn n 199 i = mul q OCH 200 ie = oce q n 201 while lt i ie bound 999999 202 put oab add s i get oa add w i 203 i = add i 1 204 end 205 end 206 end 207 208 fn oafi w ulong s ulong n ulong to void 209 for q ulong ocn n 210 i = mul q OCH 211 ie = oce q n 212 while lt i ie bound 999999 213 put oa add w i get oab add s i 214 i = add i 1 215 end 216 end 217 end 218 219 note TMP holds V logits (sample) or T attention dots 220 const OTMP ulong 4096 221 222 note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant. 223 const OMUTA ulong 1 224 225 note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or 226 note when the layout does not fit the pools. 227 fn ogeom to bool 228 if not ogok 229 ret false 230 end 231 t = get og 1 232 d = get og 2 233 put og 7 div d get og 3 234 u = mul 4 mul d d 235 u = add u mul 2 mul d get og 4 236 u = add u mul 2 d 237 put og 32 u 238 put og 30 mul get og 0 d 239 u = mul t d 240 put og 31 add get og 30 u 241 u = mul get og 5 get og 32 242 put og 33 add get og 31 u 243 u = add get og 33 d 244 put og 8 u 245 put og 9 t 246 put og 10 0 247 put og 11 0 248 put og 12 u 249 put og 13 0 250 put og 29 mul mul get og 3 t t 251 u = mul t d 252 put og 14 oastr u 253 if not ogwb 254 ret false 255 end 256 if not ogab u 257 ret false 258 end 259 p = get og 8 260 if gt p add OGC OGBN 261 ret false 262 end 263 if gt mul 3 p ocsinit 264 ret false 265 end 266 ret le mul get og 5 get og 29 OPAN 267 end 268 269 note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after 270 note them, layers LA .. L-1 in oww 271 fn ogwb to bool 272 ll = get og 5 273 g = get og 33 274 put og 34 g 275 put og 35 ll 276 put og 36 g 277 put og 37 g 278 put og 48 ll 279 if le get og 8 OWC 280 ret true 281 end 282 ls = get og 32 283 nd = add add get og 31 ls get og 2 284 if lt OWC nd 285 ret false 286 end 287 la = div sub OWC nd ls 288 if gt mul sub ll la ls OWWN 289 ret false 290 end 291 sb = add get og 31 mul la ls 292 put og 34 sb 293 put og 35 la 294 put og 36 sb 295 put og 37 add sb ls 296 ret true 297 end 298 299 note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the 300 note backward temporaries 301 fn ogab td ulong to bool 302 ll = get og 5 303 st = get og 14 304 put og 38 ll 305 put og 39 mul ll st 306 ogeom3 td mul ll st 307 ogeom2 td 308 if le get og 28 OAC 309 ret true 310 end 311 w2 = add sub get og 28 get og 15 mul 2 st 312 if lt OAC w2 313 ret false 314 end 315 la = div sub OAC w2 st 316 if gt mul sub ll la st OABN 317 ret false 318 end 319 aw = mul la st 320 put og 38 la 321 put og 39 aw 322 ogeom3 td add aw mul 2 st 323 ogeom2 td 324 ret le get og 28 OAC 325 end 326 327 fn ogeom3 td ulong hf ulong to void 328 put og 15 hf 329 put og 16 add get og 15 td 330 put og 17 add get og 16 get og 1 331 put og 18 add get og 17 td 332 end 333 334 note per-layer activation stride: 8 T D + 2 T + 2 T F 335 fn oastr td ulong to ulong 336 t = get og 1 337 a = add mul 8 td mul 2 t 338 ret add a mul mul 2 t get og 4 339 end 340 341 note shape limits (CONTRACT-V3 section 1): every shape >= 1, 256 <= V <= 4096, T <= 256, 342 note D <= 384, H <= 16, F <= 1536, L <= 16, B <= 64, D mod H = 0 343 fn ogok to bool 344 for i ulong 7 345 if eq get og i 0 346 ret false 347 end 348 end 349 if or lt get og 0 256 gt get og 0 4096 350 ret false 351 end 352 if or gt get og 1 256 gt get og 2 384 353 ret false 354 end 355 if or gt get og 3 16 gt get og 4 1536 356 ret false 357 end 358 if or gt get og 5 16 gt get og 6 64 359 ret false 360 end 361 ret eq rem get og 2 get og 3 0 362 end 363 364 fn ogeom2 td ulong to void 365 t = get og 1 366 put og 19 add get og 18 mul t get og 0 367 put og 20 add get og 19 td 368 put og 21 add get og 20 td 369 put og 22 add get og 21 td 370 put og 23 add get og 22 td 371 put og 24 add get og 23 td 372 put og 25 add get og 24 td 373 put og 26 add get og 25 td 374 put og 27 add get og 26 mul t get og 4 375 put og 28 add get og 27 OTMP 376 end 377 378 note parameter offsets of layer l 379 fn opg1 l ulong to ulong 380 ret add get og 31 mul l get og 32 381 end 382 fn opwq l ulong to ulong 383 ret add opg1 l get og 2 384 end 385 fn opwk l ulong to ulong 386 d = get og 2 387 ret add opwq l mul d d 388 end 389 fn opwv l ulong to ulong 390 d = get og 2 391 ret add opwk l mul d d 392 end 393 fn opwo l ulong to ulong 394 d = get og 2 395 ret add opwv l mul d d 396 end 397 fn opg2 l ulong to ulong 398 d = get og 2 399 ret add opwo l mul d d 400 end 401 fn opw1 l ulong to ulong 402 ret add opg2 l get og 2 403 end 404 fn opw2 l ulong to ulong 405 ret add opw1 l mul get og 2 get og 4 406 end 407 408 note activation offsets of layer l (layers from LAA in the two windows at AW) 409 fn oahin l ulong to ulong 410 if lt l get og 38 411 ret add get og 13 mul l get og 14 412 end 413 ret add get og 39 mul rem l 2 get og 14 414 end 415 fn oatd n ulong to ulong 416 ret mul n mul get og 1 get og 2 417 end 418 fn oaa l ulong to ulong 419 ret add oahin l oatd 1 420 end 421 fn oaq l ulong to ulong 422 ret add oahin l oatd 2 423 end 424 fn oak l ulong to ulong 425 ret add oahin l oatd 3 426 end 427 fn oau l ulong to ulong 428 ret add oahin l oatd 4 429 end 430 fn oao l ulong to ulong 431 ret add oahin l oatd 5 432 end 433 fn oahm l ulong to ulong 434 ret add oahin l oatd 6 435 end 436 fn oabn l ulong to ulong 437 ret add oahin l oatd 7 438 end 439 fn oas1 l ulong to ulong 440 ret add oahin l oatd 8 441 end 442 fn oas2 l ulong to ulong 443 ret add oas1 l get og 1 444 end 445 fn oafp l ulong to ulong 446 ret add oas2 l get og 1 447 end 448 fn oarl l ulong to ulong 449 ret add oafp l mul get og 1 get og 4 450 end 451 note output rows of layer l: the next layer's HIN, or HF after the last layer 452 fn oaout l ulong to ulong 453 if lt add l 1 get og 5 454 ret oahin add l 1 455 end 456 ret get og 15 457 end 458 459 note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at 460 note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four 461 note outputs run side by side in separate accumulators, so every result has v0's bits. Results of 462 note the four-wide blocks pass through oq. 463 global oq f64 4 464 465 note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv 466 fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void 467 wo = opx wv 468 xe = add xo r 469 j = ulong 0 470 while le add j 4 c bound 999999 471 kvmb xo xe add wo j c 472 kput4 add yo j 473 j = add j 4 474 end 475 while lt j c bound 4 476 put oa add yo j kvm1 xo xe add wo j c 477 j = add j 1 478 end 479 end 480 481 note oa[yo..yo+4) = oq[0..4) 482 fn kput4 yo ulong to void 483 put oa yo get oq 0 484 put oa add yo 1 get oq 1 485 put oa add yo 2 get oq 2 486 put oa add yo 3 get oq 3 487 end 488 489 note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4 490 fn kvmb xo ulong xe ulong wi ulong c ulong to void 491 s0 = 0.0f64 492 s1 = 0.0f64 493 s2 = 0.0f64 494 s3 = 0.0f64 495 while lt xo xe bound 999999 496 a = get oa xo 497 s0 = add s0 mul a get ow wi 498 s1 = add s1 mul a get ow add wi 1 499 s2 = add s2 mul a get ow add wi 2 500 s3 = add s3 mul a get ow add wi 3 501 wi = add wi c 502 xo = add xo 1 503 end 504 put oq 0 s0 505 put oq 1 s1 506 put oq 2 s2 507 put oq 3 s3 508 end 509 510 note sum over xo <= i < xe of x[i] W[wi + (i - xo) c] 511 fn kvm1 xo ulong xe ulong wi ulong c ulong to f64 512 s = 0.0f64 513 while lt xo xe bound 999999 514 a = get oa xo 515 s = add s mul a get ow wi 516 wi = add wi c 517 xo = add xo 1 518 end 519 ret s 520 end 521 522 note y[k] += sum_j dy[j] W[k][j] for k < r, j < c (y += dy . W^T); dy, y in oa, W in ow at virtual 523 note index wv 524 fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void 525 wo = opx wv 526 de = add dyo c 527 k = ulong 0 528 while le add k 4 r bound 999999 529 kdot4 dyo de add wo mul k c c 530 kacc4 add yo k 531 k = add k 4 532 end 533 while lt k r bound 4 534 a = add get oa add yo k kdot1 dyo de add wo mul k c 535 put oa add yo k a 536 k = add k 1 537 end 538 end 539 540 note oa[yo + m] = oa[yo + m] + oq[m], m < 4 541 fn kacc4 yo ulong to void 542 put oa yo add get oa yo get oq 0 543 q = add yo 1 544 put oa q add get oa q get oq 1 545 q = add yo 2 546 put oa q add get oa q get oq 2 547 q = add yo 3 548 put oa q add get oa q get oq 3 549 end 550 551 note oq[m] = sum over xo <= i < xe of x[i] W[wi + m c + (i - xo)], m < 4; x in oa, W in ow 552 fn kdot4 xo ulong xe ulong wi ulong c ulong to void 553 s0 = 0.0f64 554 s1 = 0.0f64 555 s2 = 0.0f64 556 s3 = 0.0f64 557 w2 = add wi add c c 558 while lt xo xe bound 999999 559 a = get oa xo 560 s0 = add s0 mul a get ow wi 561 s1 = add s1 mul a get ow add wi c 562 s2 = add s2 mul a get ow w2 563 s3 = add s3 mul a get ow add w2 c 564 wi = add wi 1 565 w2 = add w2 1 566 xo = add xo 1 567 end 568 put oq 0 s0 569 put oq 1 s1 570 put oq 2 s2 571 put oq 3 s3 572 end 573 574 note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow 575 fn kdot1 xo ulong xe ulong wi ulong to f64 576 s = 0.0f64 577 while lt xo xe bound 999999 578 a = get oa xo 579 s = add s mul a get ow wi 580 wi = add wi 1 581 xo = add xo 1 582 end 583 ret s 584 end 585 586 note kdot4 with both operands in oa 587 fn kdot4a xo ulong xe ulong wi ulong c ulong to void 588 s0 = 0.0f64 589 s1 = 0.0f64 590 s2 = 0.0f64 591 s3 = 0.0f64 592 w2 = add wi add c c 593 while lt xo xe bound 999999 594 a = get oa xo 595 s0 = add s0 mul a get oa wi 596 s1 = add s1 mul a get oa add wi c 597 s2 = add s2 mul a get oa w2 598 s3 = add s3 mul a get oa add w2 c 599 wi = add wi 1 600 w2 = add w2 1 601 xo = add xo 1 602 end 603 put oq 0 s0 604 put oq 1 s1 605 put oq 2 s2 606 put oq 3 s3 607 end 608 609 note kdot1 with both operands in oa 610 fn kdot1a xo ulong xe ulong wi ulong to f64 611 s = 0.0f64 612 while lt xo xe bound 999999 613 a = get oa xo 614 s = add s mul a get oa wi 615 wi = add wi 1 616 xo = add xo 1 617 end 618 ret s 619 end 620 621 note G[k][j] += x[k] dy[j] for k < r, j < c (outer product into a gradient); x, dy in oa, G in ogr 622 fn kger xo ulong dyo ulong go ulong r ulong c ulong to void 623 for k ulong r 624 kger1 get oa add xo k add go mul k c dyo c 625 end 626 end 627 628 note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass) 629 fn kger1 xv f64 gi ulong yi ulong c ulong to void 630 ye = add yi c 631 while le add yi 4 ye bound 999999 632 ogra gi mul xv get oa yi 633 ogra add gi 1 mul xv get oa add yi 1 634 ogra add gi 2 mul xv get oa add yi 2 635 ogra add gi 3 mul xv get oa add yi 3 636 gi = add gi 4 637 yi = add yi 4 638 end 639 while lt yi ye bound 4 640 ogra gi mul xv get oa yi 641 gi = add gi 1 642 yi = add yi 1 643 end 644 end 645 646 note y[i] += a x[i]; x, y in oa 647 fn kaxpy yo ulong xo ulong a f64 n ulong to void 648 for i ulong n 649 q = add yo i 650 v = mul a get oa add xo i 651 put oa q add get oa q v 652 end 653 end 654 655 note y[i] += a x[i]; x in oa, y in the gradient pool 656 fn kaxpg yo ulong xo ulong a f64 n ulong to void 657 for i ulong n 658 ogra add yo i mul a get oa add xo i 659 end 660 end 661 662 fn ozero o ulong n ulong to void 663 for i ulong n 664 put oa add o i 0.0f64 665 end 666 end 667 668 fn ozerog o ulong n ulong to void 669 for q ulong ocn n 670 i = mul q OCH 671 ie = oce q n 672 while lt i ie bound 999999 673 ogrp add o i 0.0f64 674 i = add i 1 675 end 676 end 677 end 678 679 note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so 680 note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n). 681 fn ocn n ulong to ulong 682 ret div add n 999998 OCH 683 end 684 fn oce q ulong n ulong to ulong 685 e = mul add q 1 OCH 686 if gt e n 687 ret n 688 end 689 ret e 690 end 691 692 note y = rms(v, g): s = sqrt((sum v^2)/D + 1e-5) stored at so; y[i] = (v[i]/s) g[i]; g at virtual 693 note index gv 694 fn frms vo ulong gv ulong yo ulong so ulong to void 695 go = opx gv 696 d = get og 2 697 ss = 0.0f64 698 for i ulong d 699 v = get oa add vo i 700 ss = add ss mul v v 701 end 702 s = fsqrt add div ss f64 d 0.00001 703 put oa so s 704 for j ulong d 705 v = div get oa add vo j s 706 v = mul v get ow add go j 707 put oa add yo j v 708 end 709 end 710 711 note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh; 712 note dg += da n added to the gain's gradient. go is the gain's virtual index. 713 global ofs f64 8 714 fn brms vo ulong go ulong so ulong dao ulong dho ulong to void 715 d = get og 2 716 put ofs 0 get oa so 717 gg = add get og 10 go 718 go = opx go 719 put ofs 1 div brmsc vo go dao get ofs 0 f64 d 720 for j ulong d 721 brms1 add vo j add go j add dao j add dho j add gg j 722 end 723 end 724 725 note sum over i < D of (da[i] g[i]) (v[i]/s), ascending 726 fn brmsc vo ulong go ulong dao ulong s f64 to f64 727 c = 0.0f64 728 for i ulong get og 2 729 dn = get oa add dao i 730 dn = mul dn get ow add go i 731 v = div get oa add vo i s 732 c = add c mul dn v 733 end 734 ret c 735 end 736 737 note one element of the rms backward; s = ofs[0], c = ofs[1] 738 fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void 739 s = get ofs 0 740 n = div get oa vp s 741 da = get oa dap 742 ogra ggp mul da n 743 dn = mul da get ow gp 744 dn = div sub dn mul n get ofs 1 s 745 put oa dhp add get oa dhp dn 746 end 747 748 note z[i] = a[i] + b[i] for i < n (all in oa) 749 fn kadd3 zo ulong ao ulong bo ulong n ulong to void 750 for i ulong n 751 v = get oa add ao i 752 v = add v get oa add bo i 753 put oa add zo i v 754 end 755 end 756 757 note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa) 758 fn kadd3w zo ulong ao ulong bo ulong n ulong to void 759 for i ulong n 760 v = get ow add ao i 761 v = add v get ow add bo i 762 put oa add zo i v 763 end 764 end 765 766 note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch 767 fn oemb b ulong to void 768 d = get og 2 769 tt = get og 1 770 for t ulong get og 9 771 wr = get obx add mul b tt t 772 wr = mul wr d 773 pr = add get og 30 mul t d 774 kadd3w add oahin 0 mul t d wr pr d 775 end 776 end 777 778 note offset in opa of PA[n][t][0] of layer l 779 fn opar l ulong n ulong t ulong to ulong 780 tt = get og 1 781 p = add mul n tt t 782 ret add mul l get og 29 mul p tt 783 end 784 785 note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t 786 fn fatts l ulong n ulong t ulong to void 787 e = get og 7 788 qo = add add oaq l mul t get og 2 mul n e 789 ko = add oak l mul n e 790 pa = opar l n t 791 m = fattsk qo ko pa add t 1 792 sm = osofp pa add t 1 m 793 end 794 795 note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score 796 fn fattsk qo ulong ko ulong pa ulong nj ulong to f64 797 d = get og 2 798 e = get og 7 799 isq = fsqrt f64 e 800 m = bitcast f64 ulong `18442240474082181120` 801 qe = add qo e 802 j = ulong 0 803 while le add j 4 nj bound 999999 804 kdot4a qo qe ko d 805 m = fattp4 add pa j isq m 806 ko = add ko mul 4 d 807 j = add j 4 808 end 809 while lt j nj bound 4 810 s = div kdot1a qo qe ko isq 811 put opa add pa j s 812 m = fmax m s 813 ko = add ko d 814 j = add j 1 815 end 816 ret m 817 end 818 819 note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max 820 fn fattp4 p ulong isq f64 m f64 to f64 821 for i ulong 4 822 s = div get oq i isq 823 put opa add p i s 824 m = fmax m s 825 end 826 ret m 827 end 828 829 note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum 830 fn osoft o ulong n ulong m f64 to f64 831 sm = 0.0f64 832 for j ulong n 833 ev = fexp sub get oa add o j m 834 put oa add o j ev 835 sm = add sm ev 836 end 837 for k ulong n 838 v = div get oa add o k sm 839 put oa add o k v 840 end 841 ret sm 842 end 843 844 note osoft on opa 845 fn osofp o ulong n ulong m f64 to f64 846 sm = 0.0f64 847 for j ulong n 848 ev = fexp sub get opa add o j m 849 put opa add o j ev 850 sm = add sm ev 851 end 852 for k ulong n 853 v = div get opa add o k sm 854 put opa add o k v 855 end 856 ret sm 857 end 858 859 note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i] 860 fn fatto l ulong n ulong t ulong to void 861 e = get og 7 862 oo = add add oao l mul t get og 2 mul n e 863 uo = add oau l mul n e 864 pa = opar l n t 865 ozero oo e 866 fattok oo uo pa add t 1 867 end 868 869 note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D 870 fn fattok oo ulong uo ulong pa ulong nj ulong to void 871 d = get og 2 872 e = get og 7 873 for j ulong nj 874 kaxpy oo uo get opa add pa j e 875 uo = add uo d 876 end 877 end 878 879 fn olayf l ulong to void 880 ostw l 881 for t ulong get og 9 882 olayf1 l t 883 end 884 for n ulong get og 3 885 for t2 ulong get og 9 886 fatts l n t2 887 fatto l n t2 888 end 889 end 890 for t3 ulong get og 9 891 olayf2 l t3 892 end 893 oaflip l true 894 end 895 896 note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv 897 fn olayf1 l ulong t ulong to void 898 d = get og 2 899 r = mul t d 900 a = add oaa l r 901 frms add oahin l r opg1 l a add oas1 l t 902 kvm a opwq l add oaq l r d d 903 kvm a opwk l add oak l r d d 904 kvm a opwv l add oau l r d d 905 end 906 907 note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2 908 fn olayf2 l ulong t ulong to void 909 d = get og 2 910 f = get og 4 911 r = mul t d 912 hm = add oahm l r 913 kvm add oao l r opwo l hm d d 914 kadd3 hm add oahin l r hm d 915 frms hm opg2 l add oabn l r add oas2 l t 916 fp = add oafp l mul t f 917 kvm add oabn l r opw1 l fp d f 918 rl = add oarl l mul t f 919 krelu rl fp f 920 ou = add oaout l r 921 kvm rl opw2 l ou f d 922 kadd3 ou hm ou d 923 end 924 925 note y[j] = x[j] when x[j] > 0, else 0, for j < n 926 fn krelu yo ulong xo ulong n ulong to void 927 for j ulong n 928 v = get oa add xo j 929 if le v 0.0 930 v = 0.0f64 931 end 932 put oa add yo j v 933 end 934 end 935 936 note loss of row t: the logits at o (n entries) become probabilities; returns 937 note log(sum exp(l - max)) - (l_y - max) 938 fn olossrow o ulong n ulong y ulong to f64 939 m = bitcast f64 ulong `18442240474082181120` 940 for j ulong n 941 m = fmax m get oa add o j 942 end 943 ly = get oa add o y 944 sm = osoft o n m 945 ret sub flog sm sub ly m 946 end 947 948 note final rms, tied head and loss of sequence b; returns the mean loss over CT positions 949 fn ohead b ulong to f64 950 ls = 0.0f64 951 for t ulong get og 9 952 ls = add ls oheadr b t 953 end 954 ret div ls f64 get og 9 955 end 956 957 note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss 958 fn oheadr b ulong t ulong to f64 959 d = get og 2 960 r = mul t d 961 z = add get og 17 r 962 hf = add get og 15 r 963 frms hf get og 33 z add get og 16 t 964 pr = add get og 18 mul t get og 0 965 klogit z pr get og 0 d 966 y = get oby add mul b get og 1 t 967 ret olossrow pr get og 0 y 968 end 969 970 note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa) 971 fn klogit zo ulong po ulong n ulong d ulong to void 972 ze = add zo d 973 c = ulong 0 974 while le add c 4 n bound 999999 975 kdot4 zo ze mul c d d 976 kput4 add po c 977 c = add c 4 978 end 979 while lt c n bound 4 980 put oa add po c kdot1 zo ze mul c d 981 c = add c 1 982 end 983 end 984 985 note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but 986 note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and 987 note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight. 988 global ogkm ulong 2 989 note ofzlay: when > 0, obwd stops after layer ofzlay[0] and skips obwde (frozen base layers). 990 global ofzlay ulong 1 991 992 note okv (ham/ollm-speed): the CPU decode's row reuse. okv[0] = the rows of obx row 0 whose layer 993 note activations the last ofwdi left valid, okvt[0..okv[0]) their tokens. ofwd clears okv[0]: it 994 note rewrites the same activation rows for any sequence b. 995 global okv ulong 1 996 global okvt ulong 16384 997 note OMUTI: 1 in the product; 0 in mutant i (okvlcp trusts okv[0] without comparing the tokens), which 998 note kvcmp must catch. 999 const OMUTI ulong 1 1000 1001 note forward pass of sequence b (x in obx, y in oby); returns its mean loss 1002 fn ofwd b ulong to f64 1003 put okv 0 0 1004 put ogkm 1 b 1005 oemb b 1006 for l ulong get og 5 1007 olayf l 1008 end 1009 ret ohead b 1010 end 1011 1012 note the first row ofwdi computes: the common prefix of obx row 0 and okvt, at most okv[0] and 1013 note CT - 1 (the last row is always recomputed). Bank-B layers (l >= LAA) restore/save only their 1014 note K/V slices (oak/oau) in oab without touching oahin, so KV cache works identically for all L. 1015 fn okvlcp to ulong 1016 ct = get og 9 1017 n = get okv 0 1018 if ge n ct 1019 n = sub ct 1 1020 end 1021 if eq OMUTI 0 1022 ret n 1023 end 1024 for i ulong n 1025 if ne get obx i get okvt i 1026 ret i 1027 end 1028 end 1029 ret n 1030 end 1031 1032 note ofwdi: ofwd 0 for the decode (olastf on the CPU): rows t0..CT-1 of every layer, t0 = okvlcp, 1033 note then the final RMS of row CT - 1 only (olast's z; no head for the other rows, no loss). A causal 1034 note row reads only rows at or before it, rows below t0 hold what the same model.om row functions 1035 note computed for the same tokens at the same positions, so olast's logits are bit-identical to 1036 note ofwd 0 then olast (kvcmp checks every step). Returns t0. 1037 fn ofwdi to ulong 1038 t0 = okvlcp 1039 put okv 0 0 1040 put ogkm 1 0 1041 ct = get og 9 1042 nr = sub ct t0 1043 okvemb t0 nr 1044 for l ulong get og 5 1045 okvlay l t0 nr 1046 end 1047 okvend ct 1048 ret t0 1049 end 1050 1051 note oemb for rows t0 .. t0 + nr - 1 of sequence 0 1052 fn okvemb t0 ulong nr ulong to void 1053 d = get og 2 1054 for i ulong nr 1055 t = add t0 i 1056 wr = mul get obx t d 1057 pr = add get og 30 mul t d 1058 kadd3w add oahin 0 mul t d wr pr d 1059 end 1060 end 1061 1062 note Bank-B KV slice transfer for layer l >= LAA: copy rows [r0 .. r0 + cnt) of oak and oau 1063 note between oa and oab (to oab when fo, else from oab). Never touches oahin. 1064 fn okvkv l ulong r0 ulong cnt ulong fo bool to void 1065 la = get og 38 1066 if or lt l la eq cnt 0 1067 ret 1068 end 1069 d = get og 2 1070 off = mul r0 d 1071 n = mul cnt d 1072 s = mul sub l la get og 14 1073 oaflc add oak l off add add s oatd 3 off n fo 1074 oaflc add oau l off add add s oatd 4 off n fo 1075 end 1076 1077 note olayf l for rows t0 .. t0 + nr - 1 1078 fn okvlay l ulong t0 ulong nr ulong to void 1079 ostw l 1080 okvkv l 0 t0 false 1081 for i ulong nr 1082 olayf1 l add t0 i 1083 end 1084 for n ulong get og 3 1085 for i2 ulong nr 1086 t = add t0 i2 1087 fatts l n t 1088 fatto l n t 1089 end 1090 end 1091 for i3 ulong nr 1092 olayf2 l add t0 i3 1093 end 1094 okvkv l t0 nr true 1095 end 1096 1097 note the final RMS of row CT - 1 into z (olast reads it), then okv = the CT context tokens 1098 fn okvend ct ulong to void 1099 d = get og 2 1100 t = sub ct 1 1101 r = mul t d 1102 hf = add get og 15 r 1103 z = add get og 17 r 1104 frms hf get og 33 z add get og 16 t 1105 for i ulong ct 1106 put okvt i get obx i 1107 end 1108 put okv 0 ct 1109 end 1110 1111 note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte; 1112 note DH[t] = rms backward of HF[t]. 1113 fn ohbwd b ulong t ulong sc f64 to void 1114 v = get og 0 1115 pr = add get og 18 mul t v 1116 y = get oby add mul b get og 1 t 1117 kdlog pr v y sc 1118 ohbwd2 pr t 1119 end 1120 1121 note p[c] = (p[c] - [c = y]) sc for c < n 1122 fn kdlog po ulong n ulong y ulong sc f64 to void 1123 for c ulong n 1124 g = get oa add po c 1125 if eq c y 1126 g = sub g 1.0 1127 end 1128 put oa add po c mul g sc 1129 end 1130 end 1131 1132 fn ohbwd2 pr ulong t ulong to void 1133 d = get og 2 1134 v = get og 0 1135 r = mul t d 1136 z = add get og 17 r 1137 kger pr z get og 10 v d 1138 dz = add get og 25 r 1139 kvm pr ulong 0 dz v d 1140 hf = add get og 15 r 1141 dh = add get og 19 r 1142 brms hf get og 33 add get og 16 t dz dh 1143 end 1144 1145 note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path 1146 fn offb l ulong t ulong to void 1147 f = get og 4 1148 dh = add get og 19 mul t get og 2 1149 df = add get og 26 mul t f 1150 ozero df f 1151 kvta dh opw2 l df f get og 2 1152 if eq get ogkm 0 0 1153 kdrelu df add oafp l mul t f f 1154 else 1155 ogmask df l t 1156 end 1157 g2 = add get og 10 opw2 l 1158 kger add oarl l mul t f dh g2 f get og 2 1159 offb2 l t df 1160 end 1161 1162 note y[j] = 0 where x[j] <= 0, for j < n 1163 fn kdrelu yo ulong xo ulong n ulong to void 1164 for j ulong n 1165 if le get oa add xo j 0.0 1166 put oa add yo j 0.0f64 1167 end 1168 end 1169 end 1170 1171 fn offb2 l ulong t ulong df ulong to void 1172 d = get og 2 1173 r = mul t d 1174 g1 = add get og 10 opw1 l 1175 kger add oabn l r df g1 d get og 4 1176 da = add get og 20 r 1177 ozero da d 1178 kvta df opw1 l da d get og 4 1179 hm = add oahm l r 1180 dh = add get og 19 r 1181 brms hm opg2 l add oas2 l t da dh 1182 end 1183 1184 note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j] 1185 fn oattd l ulong n ulong t ulong to f64 1186 e = get og 7 1187 dor = add add get og 24 mul t get og 2 mul n e 1188 uo = add oau l mul n e 1189 pa = opar l n t 1190 ret kattd dor uo pa add t 1 1191 end 1192 1193 fn kattd dor ulong uo ulong pa ulong nj ulong to f64 1194 d = get og 2 1195 de = add dor get og 7 1196 dt = 0.0f64 1197 j = ulong 0 1198 while le add j 4 nj bound 999999 1199 kdot4a dor de uo d 1200 dt = kattd4 add pa j add get og 27 j dt 1201 uo = add uo mul 4 d 1202 j = add j 4 1203 end 1204 while lt j nj bound 4 1205 dp = kdot1a dor de uo 1206 put oa add get og 27 j dp 1207 a = get opa add pa j 1208 dt = add dt mul a dp 1209 uo = add uo d 1210 j = add j 1 1211 end 1212 ret dt 1213 end 1214 1215 note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order 1216 fn kattd4 p ulong tm ulong dt f64 to f64 1217 for i ulong 4 1218 dp = get oq i 1219 put oa add tm i dp 1220 a = get opa add p i 1221 dt = add dt mul a dp 1222 end 1223 ret dt 1224 end 1225 1226 note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied) 1227 global oz ulong 8 1228 1229 note ds[j] = p[j](dp[j] - dot)/sqrt(E); dq[t] += ds k[j]; dk[j] += ds q[t]; du[j] += p[j] dO[t] 1230 fn oatta l ulong n ulong t ulong dt f64 to void 1231 e = get og 7 1232 hn = mul n e 1233 r = mul t get og 2 1234 put ofs 2 dt 1235 put oz 0 add add oaq l r hn 1236 put oz 1 add add get og 21 r hn 1237 put oz 2 add add get og 24 r hn 1238 put oz 3 add oak l hn 1239 put oz 4 add get og 22 hn 1240 put oz 5 add get og 23 hn 1241 pa = opar l n t 1242 oatta2 pa add t 1 1243 end 1244 1245 fn oatta2 pa ulong nj ulong to void 1246 d = get og 2 1247 e = get og 7 1248 for j ulong nj 1249 jr = mul j d 1250 ds = oads pa j 1251 kaxpy get oz 1 add get oz 3 jr ds e 1252 if eq OMUTA 1 1253 kaxpy add get oz 4 jr get oz 0 ds e 1254 else 1255 kaxpy add get oz 4 jr get oz 0 neg ds e 1256 end 1257 pj = get opa add pa j 1258 kaxpy add get oz 5 jr get oz 2 pj e 1259 end 1260 end 1261 1262 note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2 1263 fn oads pa ulong j ulong to f64 1264 pj = get opa add pa j 1265 u = sub get oa add get og 27 j get ofs 2 1266 ret div mul pj u fsqrt f64 get og 7 1267 end 1268 1269 fn olayb l ulong to void 1270 ostw l 1271 oaflip l false 1272 ct = get og 9 1273 for t ulong ct 1274 offb l t 1275 end 1276 for t2 ulong ct 1277 olaybo l t2 1278 end 1279 d = get og 2 1280 ozero get og 21 mul ct d 1281 ozero get og 22 mul ct d 1282 ozero get og 23 mul ct d 1283 for n ulong get og 3 1284 for t3 ulong ct 1285 dt = oattd l n t3 1286 oatta l n t3 dt 1287 end 1288 end 1289 for t4 ulong ct 1290 olayb2 l t4 1291 end 1292 end 1293 1294 note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t] 1295 fn olaybo l ulong t ulong to void 1296 d = get og 2 1297 r = mul t d 1298 dor = add get og 24 r 1299 dh = add get og 19 r 1300 ozero dor d 1301 kvta dh opwo l dor d d 1302 kger add oao l r dh add get og 10 opwo l d d 1303 end 1304 1305 note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path 1306 fn olayb2 l ulong t ulong to void 1307 d = get og 2 1308 r = mul t d 1309 da = add get og 20 r 1310 ozero da d 1311 kvta add get og 21 r opwq l da d d 1312 kvta add get og 22 r opwk l da d d 1313 kvta add get og 23 r opwv l da d d 1314 olayb3 l t 1315 end 1316 1317 fn olayb3 l ulong t ulong to void 1318 d = get og 2 1319 r = mul t d 1320 a = add oaa l r 1321 g0 = get og 10 1322 kger a add get og 21 r add g0 opwq l d d 1323 kger a add get og 22 r add g0 opwk l d d 1324 kger a add get og 23 r add g0 opwv l d d 1325 hi = add oahin l r 1326 dh = add get og 19 r 1327 brms hi opg1 l add oas1 l t add get og 20 r dh 1328 end 1329 1330 note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of 1331 note the step loss. Gradients accumulate into the gradient at og[10] in ogr. 1332 fn obwd b ulong sc f64 to void 1333 ct = get og 9 1334 ozero get og 19 mul ct get og 2 1335 for t ulong ct 1336 ohbwd b t sc 1337 end 1338 ll = get og 5 1339 fl = get ofzlay 0 1340 for i ulong sub ll fl 1341 olayb sub sub ll 1 i 1342 end 1343 if eq fl 0 1344 for t2 ulong ct 1345 obwde b t2 1346 end 1347 end 1348 end 1349 1350 note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t] 1351 fn obwde b ulong t ulong to void 1352 d = get og 2 1353 c = get obx add mul b get og 1 t 1354 dh = add get og 19 mul t d 1355 g0 = get og 10 1356 kaxpg add g0 mul c d dh 1.0 d 1357 kaxpg add add g0 get og 30 mul t d dh 1.0 d 1358 end