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