omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / 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 | fn opag i ulong to f64 |
| 60 | if lt i OPAN |
| 61 | ret get opa i |
| 62 | end |
| 63 | ret get opab sub i OPAN |
| 64 | end |
| 65 | |
| 66 | fn opap i ulong v f64 to void |
| 67 | if lt i OPAN |
| 68 | put opa i v |
| 69 | else |
| 70 | put opab sub i OPAN v |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | note Neuralese Phase 1: non-destructive steering, adaptive looped-core thought, LTP0 latent handoff & live telemetry. |
| 75 | note All disabled when onlc[0]=0, onlc[4]=0, onlc[6]=0, onlc[10]=0 -> bit-identical to stock ollm. |
| 76 | note onlc: [0]=steer_on, [1]=steer_layer (999=all), [2]=steer_kind (1=neuron scale, 2=resid bias, 3=head scale), |
| 77 | note [3]=steer_idx, [4]=adapt_extra (0..4), [5]=adapt_thresh_milli (nats*1000), |
| 78 | note [6]=ltp_inject_on, [7]=ltp_inject_layer, [8]=last_extra_loops, [9]=last_lv_used, [10]=trace_on |
| 79 | global onlc ulong 16 |
| 80 | note onlf: [0]=steer_val, [1]=ltp_alpha, [2]=last_entropy, [3]=last_surprisal, [4]=last_max_prob, [5]=last_hf_norm |
| 81 | global onlf f64 8 |
| 82 | global onlv f64 2048 |
| 83 | global onlh f64 2048 |
| 84 | global onln f64 32 |
| 85 | global onla f64 32 |
| 86 | global onltn_i ulong 16 |
| 87 | global onltn_v f64 16 |
| 88 | |
| 89 | note Adaptive loop (docs/LOOP.md, "Per-token exit and messages between passes"). Every control 0 -> |
| 90 | note the hooks never run and every result keeps its bits. onlx: [0] exit threshold (a row's |
| 91 | note normalized core delta |out - in| / |in| of a pass, times 1e6; 0 off) [1] inter-pass message gain |
| 92 | note times 1e6 (0 off) [2] 1 = the LTP0 vector onlv (scaled to rms onlf[1]) is the pass-0 message |
| 93 | note [6] 1 = run the hooks for telemetry only (no value changes). Hooks run only when NP > 1. |
| 94 | global onlx ulong 8 |
| 95 | note onlxp[t]: the core passes row t ran in the last forward (NP when it never exited early) |
| 96 | global onlxp ulong 512 |
| 97 | note onlxd[t]: row t's normalized delta of pass 0; onlmn[t]: the norm of its last message |
| 98 | global onlxd f64 512 |
| 99 | global onlmn f64 512 |
| 100 | note onlmsg[t D + i]: row t's inter-pass message (the core update of its last pass); |
| 101 | note onlpin[t D + i]: row t's input to the current pass (after the message is mixed in) |
| 102 | global onlmsg f64 98304 |
| 103 | global onlpin f64 98304 |
| 104 | global onlxs f64 4 |
| 105 | note Mutant x (the cached decode skips the end-of-pass hook: no exits, no messages): 1 in the |
| 106 | note product, 0 in the mutant, which adaptcmp must catch. |
| 107 | const OMUTAX ulong 1 |
| 108 | |
| 109 | note gradient pool element i (i < 2 P) |
| 110 | fn ogrg i ulong to f64 |
| 111 | p = get og 8 |
| 112 | if lt i p |
| 113 | ret ocsg i |
| 114 | end |
| 115 | j = sub i p |
| 116 | if lt j OGC |
| 117 | ret get ogr j |
| 118 | end |
| 119 | ret get ogb sub j OGC |
| 120 | end |
| 121 | |
| 122 | fn ogrp i ulong v f64 to void |
| 123 | p = get og 8 |
| 124 | if lt i p |
| 125 | ocsp i v |
| 126 | ret |
| 127 | end |
| 128 | j = sub i p |
| 129 | if lt j OGC |
| 130 | put ogr j v |
| 131 | else |
| 132 | put ogb sub j OGC v |
| 133 | end |
| 134 | end |
| 135 | |
| 136 | note gradient pool element i += a |
| 137 | fn ogra i ulong a f64 to void |
| 138 | p = get og 8 |
| 139 | if lt i p |
| 140 | ocsp i add ocsg i a |
| 141 | ret |
| 142 | end |
| 143 | j = sub i p |
| 144 | if lt j OGC |
| 145 | put ogr j add get ogr j a |
| 146 | else |
| 147 | k = sub j OGC |
| 148 | put ogb k add get ogb k a |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | note Adam pool element i (i < 2 P), cold-store index P + i |
| 153 | fn omvg i ulong to f64 |
| 154 | ret ocsg add get og 8 i |
| 155 | end |
| 156 | |
| 157 | fn omvp i ulong v f64 to void |
| 158 | ocsp add get og 8 i v |
| 159 | end |
| 160 | |
| 161 | note parameter at virtual index i (bank A, bank B or gf), and its store |
| 162 | fn owg i ulong to f64 |
| 163 | sb = get og 34 |
| 164 | if lt i sb |
| 165 | ret get ow i |
| 166 | end |
| 167 | g = get og 33 |
| 168 | if ge i g |
| 169 | ret get ow add get og 37 sub i g |
| 170 | end |
| 171 | ret get oww sub i sb |
| 172 | end |
| 173 | |
| 174 | fn owp i ulong v f64 to void |
| 175 | sb = get og 34 |
| 176 | if lt i sb |
| 177 | put ow i v |
| 178 | ret |
| 179 | end |
| 180 | g = get og 33 |
| 181 | if ge i g |
| 182 | put ow add get og 37 sub i g v |
| 183 | ret |
| 184 | end |
| 185 | put oww sub i sb v |
| 186 | end |
| 187 | |
| 188 | note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only |
| 189 | note when it is the staged layer og[48] (else exit 16, a missing ostw) |
| 190 | fn opx i ulong to ulong |
| 191 | if lt i get og 34 |
| 192 | ret i |
| 193 | end |
| 194 | g = get og 33 |
| 195 | if ge i g |
| 196 | ret add get og 37 sub i g |
| 197 | end |
| 198 | l = div sub i get og 31 get og 32 |
| 199 | if ne l get og 48 |
| 200 | exit 16 |
| 201 | end |
| 202 | ret add get og 36 sub i opg1 l |
| 203 | end |
| 204 | |
| 205 | note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product, |
| 206 | note 0 in the mutant. |
| 207 | const OMUTY ulong 1 |
| 208 | |
| 209 | const OCH ulong 999999 |
| 210 | |
| 211 | note stage bank-B layer l into the parameter window (layers below LA live in bank A; direct-oww mode skips staging) |
| 212 | fn ostw l ulong to void |
| 213 | if eq get og 36 OWC |
| 214 | put og 48 l |
| 215 | ret |
| 216 | end |
| 217 | la = get og 35 |
| 218 | if lt l la |
| 219 | ret |
| 220 | end |
| 221 | n = get og 32 |
| 222 | s = mul sub l la n |
| 223 | if eq OMUTY 0 |
| 224 | s = 0 |
| 225 | end |
| 226 | ostwc get og 36 s n |
| 227 | put og 48 l |
| 228 | end |
| 229 | |
| 230 | fn owinb wv ulong to bool |
| 231 | ret and eq get og 36 OWC and ge wv get og 34 lt wv get og 33 |
| 232 | end |
| 233 | |
| 234 | note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes) |
| 235 | fn ostwc wb ulong s ulong n ulong to void |
| 236 | for q ulong ocn n |
| 237 | i = mul q OCH |
| 238 | ie = oce q n |
| 239 | while lt i ie bound 999999 |
| 240 | put ow add wb i get oww add s i |
| 241 | i = add i 1 |
| 242 | end |
| 243 | end |
| 244 | end |
| 245 | |
| 246 | note copy layer l's activation block between its window and bank B oab (to oab when fo) |
| 247 | fn oaflip l ulong fo bool to void |
| 248 | la = get og 38 |
| 249 | if lt l la |
| 250 | ret |
| 251 | end |
| 252 | n = get og 14 |
| 253 | s = mul sub l la n |
| 254 | oaflc oahin l s n fo |
| 255 | end |
| 256 | |
| 257 | note oab[s + i] = oa[w + i] for i < n when fo, else the reverse |
| 258 | fn oaflc w ulong s ulong n ulong fo bool to void |
| 259 | if fo |
| 260 | oafo w s n |
| 261 | else |
| 262 | oafi w s n |
| 263 | end |
| 264 | end |
| 265 | |
| 266 | fn oafo w ulong s ulong n ulong to void |
| 267 | for q ulong ocn n |
| 268 | i = mul q OCH |
| 269 | ie = oce q n |
| 270 | while lt i ie bound 999999 |
| 271 | put oab add s i get oa add w i |
| 272 | i = add i 1 |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | |
| 277 | fn oafi w ulong s ulong n ulong to void |
| 278 | for q ulong ocn n |
| 279 | i = mul q OCH |
| 280 | ie = oce q n |
| 281 | while lt i ie bound 999999 |
| 282 | put oa add w i get oab add s i |
| 283 | i = add i 1 |
| 284 | end |
| 285 | end |
| 286 | end |
| 287 | |
| 288 | note TMP holds V logits (sample) or T attention dots |
| 289 | const OTMP ulong 4096 |
| 290 | |
| 291 | note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant. |
| 292 | const OMUTA ulong 1 |
| 293 | |
| 294 | note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or |
| 295 | note when the layout does not fit the pools. |
| 296 | fn ogeom to bool |
| 297 | if not ogok |
| 298 | ret false |
| 299 | end |
| 300 | t = get og 1 |
| 301 | d = get og 2 |
| 302 | put og 7 div d get og 3 |
| 303 | u = mul 4 mul d d |
| 304 | u = add u mul 2 mul d get og 4 |
| 305 | u = add u mul 2 d |
| 306 | put og 32 u |
| 307 | put og 30 mul get og 0 d |
| 308 | u = mul t d |
| 309 | put og 31 add get og 30 u |
| 310 | u = mul get og 5 get og 32 |
| 311 | put og 33 add get og 31 u |
| 312 | u = add get og 33 d |
| 313 | put og 8 u |
| 314 | put og 9 t |
| 315 | put og 10 0 |
| 316 | put og 11 0 |
| 317 | put og 12 u |
| 318 | put og 13 0 |
| 319 | put og 29 mul mul get og 3 t t |
| 320 | if not oglv |
| 321 | ret false |
| 322 | end |
| 323 | u = mul t d |
| 324 | put og 14 oastr u |
| 325 | if not ogwb |
| 326 | ret false |
| 327 | end |
| 328 | if not ogab u |
| 329 | ret false |
| 330 | end |
| 331 | p = get og 8 |
| 332 | if gt p add OGC OGBN |
| 333 | ret false |
| 334 | end |
| 335 | if gt p OGC |
| 336 | if not olp_grow 0 sub p OGC 0 |
| 337 | ret false |
| 338 | end |
| 339 | end |
| 340 | if gt mul 3 p ocsinit |
| 341 | ret false |
| 342 | end |
| 343 | pa = mul get og 56 get og 29 |
| 344 | if gt pa add OPAN OPABN |
| 345 | ret false |
| 346 | end |
| 347 | if gt pa OPAN |
| 348 | ret olp_grow_pa sub pa OPAN |
| 349 | end |
| 350 | ret true |
| 351 | end |
| 352 | |
| 353 | note the layer applications og[56] from L and the loop og[57 .. 59]; false when the core is empty, |
| 354 | note does not fit in the L layers, or runs more passes than the attention pool has rows for |
| 355 | fn oglv to bool |
| 356 | ll = get og 5 |
| 357 | np = get og 59 |
| 358 | if le np 1 |
| 359 | put og 56 ll |
| 360 | ret true |
| 361 | end |
| 362 | lc = get og 58 |
| 363 | if or eq lc 0 gt np OPAN |
| 364 | ret false |
| 365 | end |
| 366 | if gt add get og 57 lc ll |
| 367 | ret false |
| 368 | end |
| 369 | put og 56 add ll mul lc sub np 1 |
| 370 | ret true |
| 371 | end |
| 372 | |
| 373 | note the parameter layer that layer application l runs |
| 374 | fn olp l ulong to ulong |
| 375 | np = get og 59 |
| 376 | if le np 1 |
| 377 | ret l |
| 378 | end |
| 379 | lp = get og 57 |
| 380 | if lt l lp |
| 381 | ret l |
| 382 | end |
| 383 | lc = get og 58 |
| 384 | j = sub l lp |
| 385 | if lt j mul lc np |
| 386 | ret add lp rem j lc |
| 387 | end |
| 388 | ret sub l mul lc sub np 1 |
| 389 | end |
| 390 | |
| 391 | note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after |
| 392 | note them, layers LA .. L-1 in oww |
| 393 | fn ogwb to bool |
| 394 | ll = get og 5 |
| 395 | g = get og 33 |
| 396 | put og 34 g |
| 397 | put og 35 ll |
| 398 | put og 36 g |
| 399 | put og 37 g |
| 400 | put og 48 ll |
| 401 | if le get og 8 OWC |
| 402 | ret true |
| 403 | end |
| 404 | ls = get og 32 |
| 405 | nd = add add get og 31 ls get og 2 |
| 406 | if lt OWC nd |
| 407 | if lt OWC add get og 31 get og 2 |
| 408 | ret false |
| 409 | end |
| 410 | nbw = mul ll ls |
| 411 | if gt nbw OWWN |
| 412 | ret false |
| 413 | end |
| 414 | if not olp_grow nbw 0 0 |
| 415 | ret false |
| 416 | end |
| 417 | put og 34 get og 31 |
| 418 | put og 35 0 |
| 419 | put og 36 OWC |
| 420 | put og 37 get og 31 |
| 421 | ret true |
| 422 | end |
| 423 | la = div sub OWC nd ls |
| 424 | nbw = mul sub ll la ls |
| 425 | if gt nbw OWWN |
| 426 | ret false |
| 427 | end |
| 428 | if not olp_grow nbw 0 0 |
| 429 | ret false |
| 430 | end |
| 431 | sb = add get og 31 mul la ls |
| 432 | put og 34 sb |
| 433 | put og 35 la |
| 434 | put og 36 sb |
| 435 | put og 37 add sb ls |
| 436 | ret true |
| 437 | end |
| 438 | |
| 439 | note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the |
| 440 | note backward temporaries |
| 441 | fn ogab td ulong to bool |
| 442 | ll = get og 56 |
| 443 | st = get og 14 |
| 444 | put og 38 ll |
| 445 | put og 39 mul ll st |
| 446 | ogeom3 td mul ll st |
| 447 | ogeom2 td |
| 448 | if le get og 28 OAC |
| 449 | ret true |
| 450 | end |
| 451 | w2 = add sub get og 28 get og 15 mul 2 st |
| 452 | if lt OAC w2 |
| 453 | ret false |
| 454 | end |
| 455 | la = div sub OAC w2 st |
| 456 | nba = mul sub ll la st |
| 457 | if gt nba OABN |
| 458 | ret false |
| 459 | end |
| 460 | if not olp_grow 0 0 nba |
| 461 | ret false |
| 462 | end |
| 463 | aw = mul la st |
| 464 | put og 38 la |
| 465 | put og 39 aw |
| 466 | ogeom3 td add aw mul 2 st |
| 467 | ogeom2 td |
| 468 | ret le get og 28 OAC |
| 469 | end |
| 470 | |
| 471 | fn ogeom3 td ulong hf ulong to void |
| 472 | put og 15 hf |
| 473 | put og 16 add get og 15 td |
| 474 | put og 17 add get og 16 get og 1 |
| 475 | put og 18 add get og 17 td |
| 476 | end |
| 477 | |
| 478 | note per-layer activation stride: 8 T D + 2 T + 2 T F |
| 479 | fn oastr td ulong to ulong |
| 480 | t = get og 1 |
| 481 | a = add mul 8 td mul 2 t |
| 482 | ret add a mul mul 2 t get og 4 |
| 483 | end |
| 484 | |
| 485 | note shape limits: dynamic-pool targets (OWWN > 16M: gb10, aarch64, macos) support up to 500M+ |
| 486 | note (T <= 512, D <= 2048, H <= 32, F <= 8192, L <= 24, looped LV = L_phys x NP); static-pool targets |
| 487 | note (x86linux, UEFI) keep CONTRACT-V3 limits (T <= 256, D <= 384, H <= 16, F <= 1536, L <= 16). |
| 488 | fn ogok to bool |
| 489 | for i ulong 7 |
| 490 | if eq get og i 0 |
| 491 | ret false |
| 492 | end |
| 493 | end |
| 494 | if or lt get og 0 256 gt get og 0 4096 |
| 495 | ret false |
| 496 | end |
| 497 | maxt = ulong 256 |
| 498 | maxd = ulong 384 |
| 499 | maxh = ulong 16 |
| 500 | maxf = ulong 1536 |
| 501 | maxl = ulong 16 |
| 502 | if gt OWWN 16000000 |
| 503 | maxt = 512 |
| 504 | maxd = 2048 |
| 505 | maxh = 32 |
| 506 | maxf = 8192 |
| 507 | maxl = 24 |
| 508 | end |
| 509 | if or gt get og 1 maxt gt get og 2 maxd |
| 510 | ret false |
| 511 | end |
| 512 | if or gt get og 3 maxh gt get og 4 maxf |
| 513 | ret false |
| 514 | end |
| 515 | if or gt get og 5 maxl gt get og 6 64 |
| 516 | ret false |
| 517 | end |
| 518 | ret eq rem get og 2 get og 3 0 |
| 519 | end |
| 520 | |
| 521 | fn ogeom2 td ulong to void |
| 522 | t = get og 1 |
| 523 | put og 19 add get og 18 mul t get og 0 |
| 524 | put og 20 add get og 19 td |
| 525 | put og 21 add get og 20 td |
| 526 | put og 22 add get og 21 td |
| 527 | put og 23 add get og 22 td |
| 528 | put og 24 add get og 23 td |
| 529 | put og 25 add get og 24 td |
| 530 | put og 26 add get og 25 td |
| 531 | put og 27 add get og 26 mul t get og 4 |
| 532 | put og 28 add get og 27 OTMP |
| 533 | end |
| 534 | |
| 535 | note parameter offsets of layer l |
| 536 | fn opg1 l ulong to ulong |
| 537 | ret add get og 31 mul l get og 32 |
| 538 | end |
| 539 | fn opwq l ulong to ulong |
| 540 | ret add opg1 l get og 2 |
| 541 | end |
| 542 | fn opwk l ulong to ulong |
| 543 | d = get og 2 |
| 544 | ret add opwq l mul d d |
| 545 | end |
| 546 | fn opwv l ulong to ulong |
| 547 | d = get og 2 |
| 548 | ret add opwk l mul d d |
| 549 | end |
| 550 | fn opwo l ulong to ulong |
| 551 | d = get og 2 |
| 552 | ret add opwv l mul d d |
| 553 | end |
| 554 | fn opg2 l ulong to ulong |
| 555 | d = get og 2 |
| 556 | ret add opwo l mul d d |
| 557 | end |
| 558 | fn opw1 l ulong to ulong |
| 559 | ret add opg2 l get og 2 |
| 560 | end |
| 561 | fn opw2 l ulong to ulong |
| 562 | ret add opw1 l mul get og 2 get og 4 |
| 563 | end |
| 564 | |
| 565 | note parameter offsets of layer application l |
| 566 | fn ovg1 l ulong to ulong |
| 567 | ret opg1 olp l |
| 568 | end |
| 569 | fn ovwq l ulong to ulong |
| 570 | ret opwq olp l |
| 571 | end |
| 572 | fn ovwk l ulong to ulong |
| 573 | ret opwk olp l |
| 574 | end |
| 575 | fn ovwv l ulong to ulong |
| 576 | ret opwv olp l |
| 577 | end |
| 578 | fn ovwo l ulong to ulong |
| 579 | ret opwo olp l |
| 580 | end |
| 581 | fn ovg2 l ulong to ulong |
| 582 | ret opg2 olp l |
| 583 | end |
| 584 | fn ovw1 l ulong to ulong |
| 585 | ret opw1 olp l |
| 586 | end |
| 587 | fn ovw2 l ulong to ulong |
| 588 | ret opw2 olp l |
| 589 | end |
| 590 | |
| 591 | note activation offsets of layer l (layers from LAA in the two windows at AW) |
| 592 | fn oahin l ulong to ulong |
| 593 | if lt l get og 38 |
| 594 | ret add get og 13 mul l get og 14 |
| 595 | end |
| 596 | ret add get og 39 mul rem l 2 get og 14 |
| 597 | end |
| 598 | fn oatd n ulong to ulong |
| 599 | ret mul n mul get og 1 get og 2 |
| 600 | end |
| 601 | fn oaa l ulong to ulong |
| 602 | ret add oahin l oatd 1 |
| 603 | end |
| 604 | fn oaq l ulong to ulong |
| 605 | ret add oahin l oatd 2 |
| 606 | end |
| 607 | fn oak l ulong to ulong |
| 608 | ret add oahin l oatd 3 |
| 609 | end |
| 610 | fn oau l ulong to ulong |
| 611 | ret add oahin l oatd 4 |
| 612 | end |
| 613 | fn oao l ulong to ulong |
| 614 | ret add oahin l oatd 5 |
| 615 | end |
| 616 | fn oahm l ulong to ulong |
| 617 | ret add oahin l oatd 6 |
| 618 | end |
| 619 | fn oabn l ulong to ulong |
| 620 | ret add oahin l oatd 7 |
| 621 | end |
| 622 | fn oas1 l ulong to ulong |
| 623 | ret add oahin l oatd 8 |
| 624 | end |
| 625 | fn oas2 l ulong to ulong |
| 626 | ret add oas1 l get og 1 |
| 627 | end |
| 628 | fn oafp l ulong to ulong |
| 629 | ret add oas2 l get og 1 |
| 630 | end |
| 631 | fn oarl l ulong to ulong |
| 632 | ret add oafp l mul get og 1 get og 4 |
| 633 | end |
| 634 | note output rows of layer application l: the next one's HIN, or HF after the last |
| 635 | fn oaout l ulong to ulong |
| 636 | if lt add l 1 get og 56 |
| 637 | ret oahin add l 1 |
| 638 | end |
| 639 | ret get og 15 |
| 640 | end |
| 641 | |
| 642 | note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at |
| 643 | note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four |
| 644 | note outputs run side by side in separate accumulators, so every result has v0's bits. Results of |
| 645 | note the four-wide blocks pass through oq. |
| 646 | global oq f64 4 |
| 647 | |
| 648 | note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv (or direct oww) |
| 649 | fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void |
| 650 | if owinb wv |
| 651 | kvm_b xo sub wv get og 34 yo r c |
| 652 | ret |
| 653 | end |
| 654 | wo = opx wv |
| 655 | xe = add xo r |
| 656 | j = ulong 0 |
| 657 | while le add j 4 c bound 999999 |
| 658 | kvmb xo xe add wo j c |
| 659 | kput4 add yo j |
| 660 | j = add j 4 |
| 661 | end |
| 662 | while lt j c bound 4 |
| 663 | put oa add yo j kvm1 xo xe add wo j c |
| 664 | j = add j 1 |
| 665 | end |
| 666 | end |
| 667 | |
| 668 | fn kvm_b xo ulong wo ulong yo ulong r ulong c ulong to void |
| 669 | xe = add xo r |
| 670 | j = ulong 0 |
| 671 | while le add j 4 c bound 999999 |
| 672 | kvmb_b xo xe add wo j c |
| 673 | kput4 add yo j |
| 674 | j = add j 4 |
| 675 | end |
| 676 | while lt j c bound 4 |
| 677 | put oa add yo j kvm1_b xo xe add wo j c |
| 678 | j = add j 1 |
| 679 | end |
| 680 | end |
| 681 | |
| 682 | fn kvmb_b xo ulong xe ulong wi ulong c ulong to void |
| 683 | s0 = 0.0f64 |
| 684 | s1 = 0.0f64 |
| 685 | s2 = 0.0f64 |
| 686 | s3 = 0.0f64 |
| 687 | while lt xo xe bound 999999 |
| 688 | a = get oa xo |
| 689 | s0 = add s0 mul a get oww wi |
| 690 | s1 = add s1 mul a get oww add wi 1 |
| 691 | s2 = add s2 mul a get oww add wi 2 |
| 692 | s3 = add s3 mul a get oww add wi 3 |
| 693 | wi = add wi c |
| 694 | xo = add xo 1 |
| 695 | end |
| 696 | put oq 0 s0 |
| 697 | put oq 1 s1 |
| 698 | put oq 2 s2 |
| 699 | put oq 3 s3 |
| 700 | end |
| 701 | |
| 702 | fn kvm1_b xo ulong xe ulong wi ulong c ulong to f64 |
| 703 | s = 0.0f64 |
| 704 | while lt xo xe bound 999999 |
| 705 | a = get oa xo |
| 706 | s = add s mul a get oww wi |
| 707 | wi = add wi c |
| 708 | xo = add xo 1 |
| 709 | end |
| 710 | ret s |
| 711 | end |
| 712 | |
| 713 | note oa[yo..yo+4) = oq[0..4) |
| 714 | fn kput4 yo ulong to void |
| 715 | put oa yo get oq 0 |
| 716 | put oa add yo 1 get oq 1 |
| 717 | put oa add yo 2 get oq 2 |
| 718 | put oa add yo 3 get oq 3 |
| 719 | end |
| 720 | |
| 721 | note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4 |
| 722 | fn kvmb xo ulong xe ulong wi ulong c ulong to void |
| 723 | s0 = 0.0f64 |
| 724 | s1 = 0.0f64 |
| 725 | s2 = 0.0f64 |
| 726 | s3 = 0.0f64 |
| 727 | while lt xo xe bound 999999 |
| 728 | a = get oa xo |
| 729 | s0 = add s0 mul a get ow wi |
| 730 | s1 = add s1 mul a get ow add wi 1 |
| 731 | s2 = add s2 mul a get ow add wi 2 |
| 732 | s3 = add s3 mul a get ow add wi 3 |
| 733 | wi = add wi c |
| 734 | xo = add xo 1 |
| 735 | end |
| 736 | put oq 0 s0 |
| 737 | put oq 1 s1 |
| 738 | put oq 2 s2 |
| 739 | put oq 3 s3 |
| 740 | end |
| 741 | |
| 742 | note sum over xo <= i < xe of x[i] W[wi + (i - xo) c] |
| 743 | fn kvm1 xo ulong xe ulong wi ulong c ulong to f64 |
| 744 | s = 0.0f64 |
| 745 | while lt xo xe bound 999999 |
| 746 | a = get oa xo |
| 747 | s = add s mul a get ow wi |
| 748 | wi = add wi c |
| 749 | xo = add xo 1 |
| 750 | end |
| 751 | ret s |
| 752 | end |
| 753 | |
| 754 | 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 |
| 755 | note index wv (or direct oww) |
| 756 | fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void |
| 757 | if owinb wv |
| 758 | kvta_b dyo sub wv get og 34 yo r c |
| 759 | ret |
| 760 | end |
| 761 | wo = opx wv |
| 762 | de = add dyo c |
| 763 | k = ulong 0 |
| 764 | while le add k 4 r bound 999999 |
| 765 | kdot4 dyo de add wo mul k c c |
| 766 | kacc4 add yo k |
| 767 | k = add k 4 |
| 768 | end |
| 769 | while lt k r bound 4 |
| 770 | a = add get oa add yo k kdot1 dyo de add wo mul k c |
| 771 | put oa add yo k a |
| 772 | k = add k 1 |
| 773 | end |
| 774 | end |
| 775 | |
| 776 | fn kvta_b dyo ulong wo ulong yo ulong r ulong c ulong to void |
| 777 | de = add dyo c |
| 778 | k = ulong 0 |
| 779 | while le add k 4 r bound 999999 |
| 780 | kdot4_b dyo de add wo mul k c c |
| 781 | kacc4 add yo k |
| 782 | k = add k 4 |
| 783 | end |
| 784 | while lt k r bound 4 |
| 785 | a = add get oa add yo k kdot1_b dyo de add wo mul k c |
| 786 | put oa add yo k a |
| 787 | k = add k 1 |
| 788 | end |
| 789 | end |
| 790 | |
| 791 | fn kdot4_b xo ulong xe ulong wi ulong c ulong to void |
| 792 | s0 = 0.0f64 |
| 793 | s1 = 0.0f64 |
| 794 | s2 = 0.0f64 |
| 795 | s3 = 0.0f64 |
| 796 | w2 = add wi add c c |
| 797 | while lt xo xe bound 999999 |
| 798 | a = get oa xo |
| 799 | s0 = add s0 mul a get oww wi |
| 800 | s1 = add s1 mul a get oww add wi c |
| 801 | s2 = add s2 mul a get oww w2 |
| 802 | s3 = add s3 mul a get oww add w2 c |
| 803 | wi = add wi 1 |
| 804 | w2 = add w2 1 |
| 805 | xo = add xo 1 |
| 806 | end |
| 807 | put oq 0 s0 |
| 808 | put oq 1 s1 |
| 809 | put oq 2 s2 |
| 810 | put oq 3 s3 |
| 811 | end |
| 812 | |
| 813 | fn kdot1_b xo ulong xe ulong wi ulong to f64 |
| 814 | s = 0.0f64 |
| 815 | while lt xo xe bound 999999 |
| 816 | a = get oa xo |
| 817 | s = add s mul a get oww wi |
| 818 | wi = add wi 1 |
| 819 | xo = add xo 1 |
| 820 | end |
| 821 | ret s |
| 822 | end |
| 823 | |
| 824 | note oa[yo + m] = oa[yo + m] + oq[m], m < 4 |
| 825 | fn kacc4 yo ulong to void |
| 826 | put oa yo add get oa yo get oq 0 |
| 827 | q = add yo 1 |
| 828 | put oa q add get oa q get oq 1 |
| 829 | q = add yo 2 |
| 830 | put oa q add get oa q get oq 2 |
| 831 | q = add yo 3 |
| 832 | put oa q add get oa q get oq 3 |
| 833 | end |
| 834 | |
| 835 | 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 |
| 836 | fn kdot4 xo ulong xe ulong wi ulong c ulong to void |
| 837 | s0 = 0.0f64 |
| 838 | s1 = 0.0f64 |
| 839 | s2 = 0.0f64 |
| 840 | s3 = 0.0f64 |
| 841 | w2 = add wi add c c |
| 842 | while lt xo xe bound 999999 |
| 843 | a = get oa xo |
| 844 | s0 = add s0 mul a get ow wi |
| 845 | s1 = add s1 mul a get ow add wi c |
| 846 | s2 = add s2 mul a get ow w2 |
| 847 | s3 = add s3 mul a get ow add w2 c |
| 848 | wi = add wi 1 |
| 849 | w2 = add w2 1 |
| 850 | xo = add xo 1 |
| 851 | end |
| 852 | put oq 0 s0 |
| 853 | put oq 1 s1 |
| 854 | put oq 2 s2 |
| 855 | put oq 3 s3 |
| 856 | end |
| 857 | |
| 858 | note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow |
| 859 | fn kdot1 xo ulong xe ulong wi ulong to f64 |
| 860 | s = 0.0f64 |
| 861 | while lt xo xe bound 999999 |
| 862 | a = get oa xo |
| 863 | s = add s mul a get ow wi |
| 864 | wi = add wi 1 |
| 865 | xo = add xo 1 |
| 866 | end |
| 867 | ret s |
| 868 | end |
| 869 | |
| 870 | note kdot4 with both operands in oa |
| 871 | fn kdot4a xo ulong xe ulong wi ulong c ulong to void |
| 872 | s0 = 0.0f64 |
| 873 | s1 = 0.0f64 |
| 874 | s2 = 0.0f64 |
| 875 | s3 = 0.0f64 |
| 876 | w2 = add wi add c c |
| 877 | while lt xo xe bound 999999 |
| 878 | a = get oa xo |
| 879 | s0 = add s0 mul a get oa wi |
| 880 | s1 = add s1 mul a get oa add wi c |
| 881 | s2 = add s2 mul a get oa w2 |
| 882 | s3 = add s3 mul a get oa add w2 c |
| 883 | wi = add wi 1 |
| 884 | w2 = add w2 1 |
| 885 | xo = add xo 1 |
| 886 | end |
| 887 | put oq 0 s0 |
| 888 | put oq 1 s1 |
| 889 | put oq 2 s2 |
| 890 | put oq 3 s3 |
| 891 | end |
| 892 | |
| 893 | note kdot1 with both operands in oa |
| 894 | fn kdot1a xo ulong xe ulong wi ulong to f64 |
| 895 | s = 0.0f64 |
| 896 | while lt xo xe bound 999999 |
| 897 | a = get oa xo |
| 898 | s = add s mul a get oa wi |
| 899 | wi = add wi 1 |
| 900 | xo = add xo 1 |
| 901 | end |
| 902 | ret s |
| 903 | end |
| 904 | |
| 905 | 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 |
| 906 | fn kger xo ulong dyo ulong go ulong r ulong c ulong to void |
| 907 | for k ulong r |
| 908 | kger1 get oa add xo k add go mul k c dyo c |
| 909 | end |
| 910 | end |
| 911 | |
| 912 | note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass) |
| 913 | fn kger1 xv f64 gi ulong yi ulong c ulong to void |
| 914 | ye = add yi c |
| 915 | while le add yi 4 ye bound 999999 |
| 916 | ogra gi mul xv get oa yi |
| 917 | ogra add gi 1 mul xv get oa add yi 1 |
| 918 | ogra add gi 2 mul xv get oa add yi 2 |
| 919 | ogra add gi 3 mul xv get oa add yi 3 |
| 920 | gi = add gi 4 |
| 921 | yi = add yi 4 |
| 922 | end |
| 923 | while lt yi ye bound 4 |
| 924 | ogra gi mul xv get oa yi |
| 925 | gi = add gi 1 |
| 926 | yi = add yi 1 |
| 927 | end |
| 928 | end |
| 929 | |
| 930 | note y[i] += a x[i]; x, y in oa |
| 931 | fn kaxpy yo ulong xo ulong a f64 n ulong to void |
| 932 | for i ulong n |
| 933 | q = add yo i |
| 934 | v = mul a get oa add xo i |
| 935 | put oa q add get oa q v |
| 936 | end |
| 937 | end |
| 938 | |
| 939 | note y[i] += a x[i]; x in oa, y in the gradient pool |
| 940 | fn kaxpg yo ulong xo ulong a f64 n ulong to void |
| 941 | for i ulong n |
| 942 | ogra add yo i mul a get oa add xo i |
| 943 | end |
| 944 | end |
| 945 | |
| 946 | fn ozero o ulong n ulong to void |
| 947 | for i ulong n |
| 948 | put oa add o i 0.0f64 |
| 949 | end |
| 950 | end |
| 951 | |
| 952 | fn ozerog o ulong n ulong to void |
| 953 | for q ulong ocn n |
| 954 | i = mul q OCH |
| 955 | ie = oce q n |
| 956 | while lt i ie bound 999999 |
| 957 | ogrp add o i 0.0f64 |
| 958 | i = add i 1 |
| 959 | end |
| 960 | end |
| 961 | end |
| 962 | |
| 963 | note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so |
| 964 | note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n). |
| 965 | fn ocn n ulong to ulong |
| 966 | ret div add n 999998 OCH |
| 967 | end |
| 968 | fn oce q ulong n ulong to ulong |
| 969 | e = mul add q 1 OCH |
| 970 | if gt e n |
| 971 | ret n |
| 972 | end |
| 973 | ret e |
| 974 | end |
| 975 | |
| 976 | 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 |
| 977 | note index gv |
| 978 | fn frms vo ulong gv ulong yo ulong so ulong to void |
| 979 | inb = owinb gv |
| 980 | go = ulong 0 |
| 981 | if inb |
| 982 | go = sub gv get og 34 |
| 983 | else |
| 984 | go = opx gv |
| 985 | end |
| 986 | d = get og 2 |
| 987 | ss = 0.0f64 |
| 988 | for i ulong d |
| 989 | v = get oa add vo i |
| 990 | ss = add ss mul v v |
| 991 | end |
| 992 | s = fsqrt add div ss f64 d 0.00001 |
| 993 | put oa so s |
| 994 | if inb |
| 995 | for j2 ulong d |
| 996 | v = div get oa add vo j2 s |
| 997 | v = mul v get oww add go j2 |
| 998 | put oa add yo j2 v |
| 999 | end |
| 1000 | ret |
| 1001 | end |
| 1002 | for j ulong d |
| 1003 | v = div get oa add vo j s |
| 1004 | v = mul v get ow add go j |
| 1005 | put oa add yo j v |
| 1006 | end |
| 1007 | end |
| 1008 | |
| 1009 | note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh; |
| 1010 | note dg += da n added to the gain's gradient. go is the gain's virtual index. |
| 1011 | global ofs f64 8 |
| 1012 | fn brms vo ulong go ulong so ulong dao ulong dho ulong to void |
| 1013 | d = get og 2 |
| 1014 | put ofs 0 get oa so |
| 1015 | gg = add get og 10 go |
| 1016 | if owinb go |
| 1017 | bo = sub go get og 34 |
| 1018 | put ofs 1 div brmsc_b vo bo dao get ofs 0 f64 d |
| 1019 | for j2 ulong d |
| 1020 | brms1_b add vo j2 add bo j2 add dao j2 add dho j2 add gg j2 |
| 1021 | end |
| 1022 | ret |
| 1023 | end |
| 1024 | go = opx go |
| 1025 | put ofs 1 div brmsc vo go dao get ofs 0 f64 d |
| 1026 | for j ulong d |
| 1027 | brms1 add vo j add go j add dao j add dho j add gg j |
| 1028 | end |
| 1029 | end |
| 1030 | |
| 1031 | fn brmsc_b vo ulong go ulong dao ulong s f64 to f64 |
| 1032 | c = 0.0f64 |
| 1033 | for i ulong get og 2 |
| 1034 | dn = get oa add dao i |
| 1035 | dn = mul dn get oww add go i |
| 1036 | v = div get oa add vo i s |
| 1037 | c = add c mul dn v |
| 1038 | end |
| 1039 | ret c |
| 1040 | end |
| 1041 | |
| 1042 | fn brms1_b vp ulong gp ulong dap ulong dhp ulong ggp ulong to void |
| 1043 | s = get ofs 0 |
| 1044 | n = div get oa vp s |
| 1045 | da = get oa dap |
| 1046 | ogra ggp mul da n |
| 1047 | dn = mul da get oww gp |
| 1048 | dn = div sub dn mul n get ofs 1 s |
| 1049 | put oa dhp add get oa dhp dn |
| 1050 | end |
| 1051 | |
| 1052 | note sum over i < D of (da[i] g[i]) (v[i]/s), ascending |
| 1053 | fn brmsc vo ulong go ulong dao ulong s f64 to f64 |
| 1054 | c = 0.0f64 |
| 1055 | for i ulong get og 2 |
| 1056 | dn = get oa add dao i |
| 1057 | dn = mul dn get ow add go i |
| 1058 | v = div get oa add vo i s |
| 1059 | c = add c mul dn v |
| 1060 | end |
| 1061 | ret c |
| 1062 | end |
| 1063 | |
| 1064 | note one element of the rms backward; s = ofs[0], c = ofs[1] |
| 1065 | fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void |
| 1066 | s = get ofs 0 |
| 1067 | n = div get oa vp s |
| 1068 | da = get oa dap |
| 1069 | ogra ggp mul da n |
| 1070 | dn = mul da get ow gp |
| 1071 | dn = div sub dn mul n get ofs 1 s |
| 1072 | put oa dhp add get oa dhp dn |
| 1073 | end |
| 1074 | |
| 1075 | note z[i] = a[i] + b[i] for i < n (all in oa) |
| 1076 | fn kadd3 zo ulong ao ulong bo ulong n ulong to void |
| 1077 | for i ulong n |
| 1078 | v = get oa add ao i |
| 1079 | v = add v get oa add bo i |
| 1080 | put oa add zo i v |
| 1081 | end |
| 1082 | end |
| 1083 | |
| 1084 | note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa) |
| 1085 | fn kadd3w zo ulong ao ulong bo ulong n ulong to void |
| 1086 | for i ulong n |
| 1087 | v = get ow add ao i |
| 1088 | v = add v get ow add bo i |
| 1089 | put oa add zo i v |
| 1090 | end |
| 1091 | end |
| 1092 | |
| 1093 | note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch |
| 1094 | fn oemb b ulong to void |
| 1095 | d = get og 2 |
| 1096 | tt = get og 1 |
| 1097 | for t ulong get og 9 |
| 1098 | wr = get obx add mul b tt t |
| 1099 | wr = mul wr d |
| 1100 | pr = add get og 30 mul t d |
| 1101 | kadd3w add oahin 0 mul t d wr pr d |
| 1102 | end |
| 1103 | end |
| 1104 | |
| 1105 | note offset in opa of PA[n][t][0] of layer l |
| 1106 | fn opar l ulong n ulong t ulong to ulong |
| 1107 | tt = get og 1 |
| 1108 | p = add mul n tt t |
| 1109 | ret add mul l get og 29 mul p tt |
| 1110 | end |
| 1111 | |
| 1112 | note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t |
| 1113 | fn fatts l ulong n ulong t ulong to void |
| 1114 | e = get og 7 |
| 1115 | qo = add add oaq l mul t get og 2 mul n e |
| 1116 | ko = add oak l mul n e |
| 1117 | pa = opar l n t |
| 1118 | m = fattsk qo ko pa add t 1 |
| 1119 | sm = osofp pa add t 1 m |
| 1120 | end |
| 1121 | |
| 1122 | note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score |
| 1123 | fn fattsk qo ulong ko ulong pa ulong nj ulong to f64 |
| 1124 | d = get og 2 |
| 1125 | e = get og 7 |
| 1126 | isq = fsqrt f64 e |
| 1127 | m = bitcast f64 ulong 18442240474082181120 |
| 1128 | qe = add qo e |
| 1129 | j = ulong 0 |
| 1130 | while le add j 4 nj bound 999999 |
| 1131 | kdot4a qo qe ko d |
| 1132 | m = fattp4 add pa j isq m |
| 1133 | ko = add ko mul 4 d |
| 1134 | j = add j 4 |
| 1135 | end |
| 1136 | while lt j nj bound 4 |
| 1137 | s = div kdot1a qo qe ko isq |
| 1138 | opap add pa j s |
| 1139 | m = fmax m s |
| 1140 | ko = add ko d |
| 1141 | j = add j 1 |
| 1142 | end |
| 1143 | ret m |
| 1144 | end |
| 1145 | |
| 1146 | note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max |
| 1147 | fn fattp4 p ulong isq f64 m f64 to f64 |
| 1148 | for i ulong 4 |
| 1149 | s = div get oq i isq |
| 1150 | opap add p i s |
| 1151 | m = fmax m s |
| 1152 | end |
| 1153 | ret m |
| 1154 | end |
| 1155 | |
| 1156 | note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum |
| 1157 | fn osoft o ulong n ulong m f64 to f64 |
| 1158 | sm = 0.0f64 |
| 1159 | for j ulong n |
| 1160 | ev = fexp sub get oa add o j m |
| 1161 | put oa add o j ev |
| 1162 | sm = add sm ev |
| 1163 | end |
| 1164 | for k ulong n |
| 1165 | v = div get oa add o k sm |
| 1166 | put oa add o k v |
| 1167 | end |
| 1168 | ret sm |
| 1169 | end |
| 1170 | |
| 1171 | note osoft on opa |
| 1172 | fn osofp o ulong n ulong m f64 to f64 |
| 1173 | sm = 0.0f64 |
| 1174 | for j ulong n |
| 1175 | ev = fexp sub opag add o j m |
| 1176 | opap add o j ev |
| 1177 | sm = add sm ev |
| 1178 | end |
| 1179 | for k ulong n |
| 1180 | v = div opag add o k sm |
| 1181 | opap add o k v |
| 1182 | end |
| 1183 | ret sm |
| 1184 | end |
| 1185 | |
| 1186 | note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i] |
| 1187 | fn fatto l ulong n ulong t ulong to void |
| 1188 | e = get og 7 |
| 1189 | oo = add add oao l mul t get og 2 mul n e |
| 1190 | uo = add oau l mul n e |
| 1191 | pa = opar l n t |
| 1192 | ozero oo e |
| 1193 | fattok oo uo pa add t 1 |
| 1194 | end |
| 1195 | |
| 1196 | note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D |
| 1197 | fn fattok oo ulong uo ulong pa ulong nj ulong to void |
| 1198 | d = get og 2 |
| 1199 | e = get og 7 |
| 1200 | for j ulong nj |
| 1201 | kaxpy oo uo opag add pa j e |
| 1202 | uo = add uo d |
| 1203 | end |
| 1204 | end |
| 1205 | |
| 1206 | fn olayf l ulong to void |
| 1207 | ostw olp l |
| 1208 | for t ulong get og 9 |
| 1209 | olayf1 l t |
| 1210 | end |
| 1211 | for n ulong get og 3 |
| 1212 | for t2 ulong get og 9 |
| 1213 | fatts l n t2 |
| 1214 | fatto l n t2 |
| 1215 | end |
| 1216 | end |
| 1217 | for t3 ulong get og 9 |
| 1218 | olayf2x l t3 |
| 1219 | end |
| 1220 | oaflip l true |
| 1221 | end |
| 1222 | |
| 1223 | note olayf2 of row t, or, for a row that left the loop before this core pass, its input unchanged |
| 1224 | note (its K and V above still come from that frozen row, so later rows see the same keys) |
| 1225 | fn olayf2x l ulong t ulong to void |
| 1226 | if ne get onlx 0 0 |
| 1227 | if onlfz l t |
| 1228 | d = get og 2 |
| 1229 | r = mul t d |
| 1230 | onlcopy_oa add oaout l r add oahin l r d |
| 1231 | ret |
| 1232 | end |
| 1233 | end |
| 1234 | olayf2 l t |
| 1235 | end |
| 1236 | |
| 1237 | note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv |
| 1238 | fn olayf1 l ulong t ulong to void |
| 1239 | d = get og 2 |
| 1240 | r = mul t d |
| 1241 | a = add oaa l r |
| 1242 | frms add oahin l r ovg1 l a add oas1 l t |
| 1243 | kvm a ovwq l add oaq l r d d |
| 1244 | kvm a ovwk l add oak l r d d |
| 1245 | kvm a ovwv l add oau l r d d |
| 1246 | end |
| 1247 | |
| 1248 | note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2 |
| 1249 | fn olayf2 l ulong t ulong to void |
| 1250 | d = get og 2 |
| 1251 | f = get og 4 |
| 1252 | r = mul t d |
| 1253 | hm = add oahm l r |
| 1254 | kvm add oao l r ovwo l hm d d |
| 1255 | kadd3 hm add oahin l r hm d |
| 1256 | frms hm ovg2 l add oabn l r add oas2 l t |
| 1257 | fp = add oafp l mul t f |
| 1258 | kvm add oabn l r ovw1 l fp d f |
| 1259 | rl = add oarl l mul t f |
| 1260 | krelu rl fp f |
| 1261 | if ne get onlc 0 0 |
| 1262 | onlsteer_ffn l t |
| 1263 | end |
| 1264 | ou = add oaout l r |
| 1265 | kvm rl ovw2 l ou f d |
| 1266 | kadd3 ou hm ou d |
| 1267 | if ne get onlc 0 0 |
| 1268 | onlsteer_res l t |
| 1269 | end |
| 1270 | end |
| 1271 | |
| 1272 | note y[j] = x[j] when x[j] > 0, else 0, for j < n |
| 1273 | fn krelu yo ulong xo ulong n ulong to void |
| 1274 | for j ulong n |
| 1275 | v = get oa add xo j |
| 1276 | if le v 0.0 |
| 1277 | v = 0.0f64 |
| 1278 | end |
| 1279 | put oa add yo j v |
| 1280 | end |
| 1281 | end |
| 1282 | |
| 1283 | note loss of row t: the logits at o (n entries) become probabilities; returns |
| 1284 | note log(sum exp(l - max)) - (l_y - max) |
| 1285 | fn olossrow o ulong n ulong y ulong to f64 |
| 1286 | m = bitcast f64 ulong 18442240474082181120 |
| 1287 | for j ulong n |
| 1288 | m = fmax m get oa add o j |
| 1289 | end |
| 1290 | ly = get oa add o y |
| 1291 | sm = osoft o n m |
| 1292 | ret sub flog sm sub ly m |
| 1293 | end |
| 1294 | |
| 1295 | note final rms, tied head and loss of sequence b; returns the mean loss over CT positions |
| 1296 | fn ohead b ulong to f64 |
| 1297 | ls = 0.0f64 |
| 1298 | for t ulong get og 9 |
| 1299 | ls = add ls oheadr b t |
| 1300 | end |
| 1301 | ret div ls f64 get og 9 |
| 1302 | end |
| 1303 | |
| 1304 | note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss |
| 1305 | fn oheadr b ulong t ulong to f64 |
| 1306 | d = get og 2 |
| 1307 | r = mul t d |
| 1308 | z = add get og 17 r |
| 1309 | hf = add get og 15 r |
| 1310 | frms hf get og 33 z add get og 16 t |
| 1311 | pr = add get og 18 mul t get og 0 |
| 1312 | klogit z pr get og 0 d |
| 1313 | y = get oby add mul b get og 1 t |
| 1314 | ret olossrow pr get og 0 y |
| 1315 | end |
| 1316 | |
| 1317 | note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa) |
| 1318 | fn klogit zo ulong po ulong n ulong d ulong to void |
| 1319 | ze = add zo d |
| 1320 | c = ulong 0 |
| 1321 | while le add c 4 n bound 999999 |
| 1322 | kdot4 zo ze mul c d d |
| 1323 | kput4 add po c |
| 1324 | c = add c 4 |
| 1325 | end |
| 1326 | while lt c n bound 4 |
| 1327 | put oa add po c kdot1 zo ze mul c d |
| 1328 | c = add c 1 |
| 1329 | end |
| 1330 | end |
| 1331 | |
| 1332 | note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but |
| 1333 | note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and |
| 1334 | note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight. |
| 1335 | global ogkm ulong 2 |
| 1336 | note ofzlay: when > 0, obwd stops after layer ofzlay[0] and skips obwde (frozen base layers). |
| 1337 | global ofzlay ulong 1 |
| 1338 | |
| 1339 | note okv (ham/ollm-speed): the CPU decode's row reuse. okv[0] = the rows of obx row 0 whose layer |
| 1340 | note activations the last ofwdi left valid, okvt[0..okv[0]) their tokens. ofwd clears okv[0]: it |
| 1341 | note rewrites the same activation rows for any sequence b. |
| 1342 | global okv ulong 1 |
| 1343 | global okvt ulong 16384 |
| 1344 | note OMUTI: 1 in the product; 0 in mutant i (okvlcp trusts okv[0] without comparing the tokens), which |
| 1345 | note kvcmp must catch. |
| 1346 | const OMUTI ulong 1 |
| 1347 | |
| 1348 | note forward pass of sequence b (x in obx, y in oby); returns its mean loss |
| 1349 | fn ofwd b ulong to f64 |
| 1350 | put okv 0 0 |
| 1351 | put ogkm 1 b |
| 1352 | oemb b |
| 1353 | xo = onlxon |
| 1354 | for l ulong get og 56 |
| 1355 | if xo |
| 1356 | onlxpre l 0 get og 9 |
| 1357 | end |
| 1358 | olayf l |
| 1359 | if xo |
| 1360 | onlxpost l 0 get og 9 |
| 1361 | end |
| 1362 | end |
| 1363 | ret ohead b |
| 1364 | end |
| 1365 | |
| 1366 | note the first row ofwdi computes: the common prefix of obx row 0 and okvt, at most okv[0] and |
| 1367 | note CT - 1 (the last row is always recomputed). Bank-B layers (l >= LAA) restore/save only their |
| 1368 | note K/V slices (oak/oau) in oab without touching oahin, so KV cache works identically for all L. |
| 1369 | fn okvlcp to ulong |
| 1370 | ct = get og 9 |
| 1371 | n = get okv 0 |
| 1372 | if ge n ct |
| 1373 | n = sub ct 1 |
| 1374 | end |
| 1375 | if eq OMUTI 0 |
| 1376 | ret n |
| 1377 | end |
| 1378 | for i ulong n |
| 1379 | if ne get obx i get okvt i |
| 1380 | ret i |
| 1381 | end |
| 1382 | end |
| 1383 | ret n |
| 1384 | end |
| 1385 | |
| 1386 | note ofwdi: ofwd 0 for the decode (olastf on the CPU): rows t0..CT-1 of every layer, t0 = okvlcp, |
| 1387 | note then the final RMS of row CT - 1 only (olast's z; no head for the other rows, no loss). A causal |
| 1388 | note row reads only rows at or before it, rows below t0 hold what the same model.om row functions |
| 1389 | note computed for the same tokens at the same positions, so olast's logits are bit-identical to |
| 1390 | note ofwd 0 then olast (kvcmp checks every step). Returns t0. |
| 1391 | fn ofwdi to ulong |
| 1392 | t0 = okvlcp |
| 1393 | put okv 0 0 |
| 1394 | put ogkm 1 0 |
| 1395 | ct = get og 9 |
| 1396 | nr = sub ct t0 |
| 1397 | okvemb t0 nr |
| 1398 | xo = onlxon |
| 1399 | for l ulong get og 56 |
| 1400 | if xo |
| 1401 | onlxpre l t0 nr |
| 1402 | end |
| 1403 | okvlay l t0 nr |
| 1404 | if and xo eq OMUTAX 1 |
| 1405 | onlxpost l t0 nr |
| 1406 | end |
| 1407 | end |
| 1408 | okvend ct |
| 1409 | ret t0 |
| 1410 | end |
| 1411 | |
| 1412 | |
| 1413 | note oemb for rows t0 .. t0 + nr - 1 of sequence 0 |
| 1414 | fn okvemb t0 ulong nr ulong to void |
| 1415 | d = get og 2 |
| 1416 | for i ulong nr |
| 1417 | t = add t0 i |
| 1418 | wr = mul get obx t d |
| 1419 | pr = add get og 30 mul t d |
| 1420 | kadd3w add oahin 0 mul t d wr pr d |
| 1421 | end |
| 1422 | end |
| 1423 | |
| 1424 | note Bank-B KV slice transfer for layer l >= LAA: copy rows [r0 .. r0 + cnt) of oak and oau |
| 1425 | note between oa and oab (to oab when fo, else from oab). Never touches oahin. |
| 1426 | fn okvkv l ulong r0 ulong cnt ulong fo bool to void |
| 1427 | la = get og 38 |
| 1428 | if or lt l la eq cnt 0 |
| 1429 | ret |
| 1430 | end |
| 1431 | d = get og 2 |
| 1432 | off = mul r0 d |
| 1433 | n = mul cnt d |
| 1434 | s = mul sub l la get og 14 |
| 1435 | oaflc add oak l off add add s oatd 3 off n fo |
| 1436 | oaflc add oau l off add add s oatd 4 off n fo |
| 1437 | end |
| 1438 | |
| 1439 | note olayf l for rows t0 .. t0 + nr - 1 |
| 1440 | fn okvlay l ulong t0 ulong nr ulong to void |
| 1441 | ostw olp l |
| 1442 | okvkv l 0 t0 false |
| 1443 | if ne get onlc 6 0 |
| 1444 | for i0 ulong nr |
| 1445 | onlinject l add t0 i0 |
| 1446 | end |
| 1447 | end |
| 1448 | for i ulong nr |
| 1449 | olayf1 l add t0 i |
| 1450 | end |
| 1451 | for n ulong get og 3 |
| 1452 | for i2 ulong nr |
| 1453 | t = add t0 i2 |
| 1454 | fatts l n t |
| 1455 | fatto l n t |
| 1456 | end |
| 1457 | end |
| 1458 | if ne get onlc 0 0 |
| 1459 | for ih ulong nr |
| 1460 | onlsteer_head l add t0 ih |
| 1461 | end |
| 1462 | end |
| 1463 | for i3 ulong nr |
| 1464 | olayf2x l add t0 i3 |
| 1465 | end |
| 1466 | okvkv l t0 nr true |
| 1467 | if ne get onlc 10 0 |
| 1468 | onlrec_lay l sub add t0 nr 1 |
| 1469 | end |
| 1470 | end |
| 1471 | |
| 1472 | note the final RMS of row CT - 1 into z (olast reads it), then okv = the CT context tokens |
| 1473 | fn okvend ct ulong to void |
| 1474 | d = get og 2 |
| 1475 | t = sub ct 1 |
| 1476 | r = mul t d |
| 1477 | hf = add get og 15 r |
| 1478 | z = add get og 17 r |
| 1479 | frms hf get og 33 z add get og 16 t |
| 1480 | for i ulong ct |
| 1481 | put okvt i get obx i |
| 1482 | end |
| 1483 | put okv 0 ct |
| 1484 | if ne get onlc 10 0 |
| 1485 | onlrec_top t |
| 1486 | end |
| 1487 | end |
| 1488 | |
| 1489 | note ======== Neuralese Phase 1 helpers (non-destructive steering, LTP0 injection, telemetry, adaptive looping) |
| 1490 | |
| 1491 | fn onlreset to void |
| 1492 | for i ulong 16 |
| 1493 | put onlc i 0 |
| 1494 | end |
| 1495 | for j ulong 8 |
| 1496 | put onlf j 0.0f64 |
| 1497 | put onlx j 0 |
| 1498 | end |
| 1499 | end |
| 1500 | |
| 1501 | fn onlmatchl l ulong to bool |
| 1502 | tl = get onlc 1 |
| 1503 | ret or eq tl 999 eq tl l |
| 1504 | end |
| 1505 | |
| 1506 | fn onlsteer_head l ulong t ulong to void |
| 1507 | if or ne get onlc 2 3 not onlmatchl l |
| 1508 | ret |
| 1509 | end |
| 1510 | hi = get onlc 3 |
| 1511 | if ge hi get og 3 |
| 1512 | ret |
| 1513 | end |
| 1514 | e = get og 7 |
| 1515 | oo = add add oao l mul t get og 2 mul hi e |
| 1516 | sc = get onlf 0 |
| 1517 | for i ulong e |
| 1518 | q = add oo i |
| 1519 | put oa q mul get oa q sc |
| 1520 | end |
| 1521 | end |
| 1522 | |
| 1523 | fn onlsteer_ffn l ulong t ulong to void |
| 1524 | if or ne get onlc 2 1 not onlmatchl l |
| 1525 | ret |
| 1526 | end |
| 1527 | idx = get onlc 3 |
| 1528 | f = get og 4 |
| 1529 | if ge idx f |
| 1530 | ret |
| 1531 | end |
| 1532 | q = add add oarl l mul t f idx |
| 1533 | put oa q mul get oa q get onlf 0 |
| 1534 | end |
| 1535 | |
| 1536 | fn onlsteer_res l ulong t ulong to void |
| 1537 | if or ne get onlc 2 2 not onlmatchl l |
| 1538 | ret |
| 1539 | end |
| 1540 | idx = get onlc 3 |
| 1541 | d = get og 2 |
| 1542 | if ge idx d |
| 1543 | ret |
| 1544 | end |
| 1545 | q = add add oaout l mul t d idx |
| 1546 | put oa q add get oa q get onlf 0 |
| 1547 | end |
| 1548 | |
| 1549 | fn onlv_rms d ulong to f64 |
| 1550 | ss = 0.0f64 |
| 1551 | for i ulong d |
| 1552 | v = get onlv i |
| 1553 | ss = add ss mul v v |
| 1554 | end |
| 1555 | ret fsqrt add div ss f64 d 1.0e-12f64 |
| 1556 | end |
| 1557 | |
| 1558 | fn onlinject l ulong t ulong to void |
| 1559 | if ne l get onlc 7 |
| 1560 | ret |
| 1561 | end |
| 1562 | d = get og 2 |
| 1563 | ho = add oahin l mul t d |
| 1564 | sc = div get onlf 1 onlv_rms d |
| 1565 | for i ulong d |
| 1566 | q = add ho i |
| 1567 | dv = mul sc get onlv i |
| 1568 | put oa q add get oa q dv |
| 1569 | end |
| 1570 | end |
| 1571 | |
| 1572 | fn onlrec_lay l ulong t ulong to void |
| 1573 | if ge l 32 |
| 1574 | ret |
| 1575 | end |
| 1576 | d = get og 2 |
| 1577 | ou = add oaout l mul t d |
| 1578 | ss = 0.0f64 |
| 1579 | for i ulong d |
| 1580 | v = get oa add ou i |
| 1581 | ss = add ss mul v v |
| 1582 | end |
| 1583 | put onln l fsqrt ss |
| 1584 | end |
| 1585 | |
| 1586 | fn onlrec_hf t ulong to void |
| 1587 | d = get og 2 |
| 1588 | hf = add get og 15 mul t d |
| 1589 | ss = 0.0f64 |
| 1590 | for i ulong d |
| 1591 | v = get oa add hf i |
| 1592 | put onlh i v |
| 1593 | ss = add ss mul v v |
| 1594 | end |
| 1595 | put onlf 5 fsqrt ss |
| 1596 | end |
| 1597 | |
| 1598 | fn onlrec_att ll ulong t ulong to void |
| 1599 | h = get og 3 |
| 1600 | for n ulong h |
| 1601 | pa = opar ll n t |
| 1602 | mx = 0.0f64 |
| 1603 | for j ulong add t 1 |
| 1604 | pv = opag add pa j |
| 1605 | if gt pv mx |
| 1606 | mx = pv |
| 1607 | end |
| 1608 | end |
| 1609 | put onla n mx |
| 1610 | end |
| 1611 | end |
| 1612 | |
| 1613 | fn onlrec_top t ulong to void |
| 1614 | onlrec_hf t |
| 1615 | ll = sub get og 56 1 |
| 1616 | onlrec_att ll t |
| 1617 | onlrec_neur ll t |
| 1618 | end |
| 1619 | |
| 1620 | fn onlrec_neur ll ulong t ulong to void |
| 1621 | f = get og 4 |
| 1622 | rl = add oarl ll mul t f |
| 1623 | for k ulong 16 |
| 1624 | put onltn_i k 0 |
| 1625 | put onltn_v k -1.0f64 |
| 1626 | end |
| 1627 | for j ulong f |
| 1628 | v = get oa add rl j |
| 1629 | if gt v get onltn_v 15 |
| 1630 | onlins_neur j v |
| 1631 | end |
| 1632 | end |
| 1633 | end |
| 1634 | |
| 1635 | fn onlins_neur idx ulong val f64 to void |
| 1636 | pos = ulong 15 |
| 1637 | while gt pos 0 bound 16 |
| 1638 | pv = get onltn_v sub pos 1 |
| 1639 | if le val pv |
| 1640 | break |
| 1641 | end |
| 1642 | put onltn_i pos get onltn_i sub pos 1 |
| 1643 | put onltn_v pos pv |
| 1644 | pos = sub pos 1 |
| 1645 | end |
| 1646 | put onltn_i pos idx |
| 1647 | put onltn_v pos val |
| 1648 | end |
| 1649 | |
| 1650 | fn onlent_max o ulong v ulong to f64 |
| 1651 | m = bitcast f64 ulong 18442240474082181120 |
| 1652 | for c ulong v |
| 1653 | x = get oa add o c |
| 1654 | if gt x m |
| 1655 | m = x |
| 1656 | end |
| 1657 | end |
| 1658 | ret m |
| 1659 | end |
| 1660 | |
| 1661 | fn onlent_lsm o ulong v ulong m f64 to f64 |
| 1662 | sm = 0.0f64 |
| 1663 | for c ulong v |
| 1664 | sm = add sm fexp sub get oa add o c m |
| 1665 | end |
| 1666 | ret flog sm |
| 1667 | end |
| 1668 | |
| 1669 | note Non-destructive Shannon entropy (nats) and max-prob of current TMP[0..V) logits |
| 1670 | fn onlentropy to f64 |
| 1671 | o = get og 27 |
| 1672 | v = get og 0 |
| 1673 | m = onlent_max o v |
| 1674 | lsm = onlent_lsm o v m |
| 1675 | ent = 0.0f64 |
| 1676 | mxp = 0.0f64 |
| 1677 | for c ulong v |
| 1678 | lp = sub sub get oa add o c m lsm |
| 1679 | p = fexp lp |
| 1680 | if gt p mxp |
| 1681 | mxp = p |
| 1682 | end |
| 1683 | if gt p 1.0e-15f64 |
| 1684 | ent = sub ent mul p lp |
| 1685 | end |
| 1686 | end |
| 1687 | put onlf 2 ent |
| 1688 | put onlf 4 mxp |
| 1689 | ret ent |
| 1690 | end |
| 1691 | |
| 1692 | fn onlcopy_oa dst ulong src ulong d ulong to void |
| 1693 | for i ulong d |
| 1694 | put oa add dst i get oa add src i |
| 1695 | end |
| 1696 | end |
| 1697 | |
| 1698 | note Single extra continuous-thought pass through core layers [c0 .. c0+cn) from pre-coda core output, then coda on row t |
| 1699 | fn onlpass1 t ulong c0 ulong cn ulong to void |
| 1700 | d = get og 2 |
| 1701 | r = mul t d |
| 1702 | coda = sub get og 5 add c0 cn |
| 1703 | lv = get og 56 |
| 1704 | if gt coda 0 |
| 1705 | csrc = add oaout sub sub lv coda 1 r |
| 1706 | onlcopy_oa add oahin c0 r csrc d |
| 1707 | else |
| 1708 | onlcopy_oa add oahin c0 r add get og 15 r d |
| 1709 | end |
| 1710 | for k ulong cn |
| 1711 | okvlay add c0 k t 1 |
| 1712 | end |
| 1713 | src = add oaout sub add c0 cn 1 r |
| 1714 | if gt coda 0 |
| 1715 | cstart = sub lv coda |
| 1716 | onlcopy_oa add oahin cstart r src d |
| 1717 | for k2 ulong coda |
| 1718 | okvlay add cstart k2 t 1 |
| 1719 | end |
| 1720 | else |
| 1721 | onlcopy_oa add get og 15 r src d |
| 1722 | end |
| 1723 | end |
| 1724 | |
| 1725 | note Entropy-gated adaptive continuous-thought looping after olastf (when onlc[4] > 0) |
| 1726 | fn onladapt to void |
| 1727 | put onlc 8 0 |
| 1728 | put onlc 9 get og 56 |
| 1729 | ent = onlentropy |
| 1730 | ex = get onlc 4 |
| 1731 | if eq ex 0 |
| 1732 | ret |
| 1733 | end |
| 1734 | if gt ex 4 |
| 1735 | ex = 4 |
| 1736 | end |
| 1737 | th = div f64 get onlc 5 1000.0 |
| 1738 | if and gt get onlc 5 0 lt ent th |
| 1739 | ret |
| 1740 | end |
| 1741 | onladapt_run ex th |
| 1742 | end |
| 1743 | |
| 1744 | fn onladapt_run ex ulong th f64 to void |
| 1745 | ct = get og 9 |
| 1746 | t = sub ct 1 |
| 1747 | c0 = get og 57 |
| 1748 | cn = get og 58 |
| 1749 | if eq cn 0 |
| 1750 | c0 = sub get og 5 1 |
| 1751 | cn = 1 |
| 1752 | end |
| 1753 | for ps ulong ex |
| 1754 | onlpass1 t c0 cn |
| 1755 | okvend ct |
| 1756 | olast |
| 1757 | ent = onlentropy |
| 1758 | put onlc 8 add ps 1 |
| 1759 | put onlc 9 add get onlc 9 cn |
| 1760 | if and gt get onlc 5 0 lt ent th |
| 1761 | break |
| 1762 | end |
| 1763 | end |
| 1764 | end |
| 1765 | |
| 1766 | note ======== Adaptive loop: per-token exit and inter-pass messages (onlx, docs/LOOP.md) |
| 1767 | note A core pass p runs applications LP + p LC .. LP + (p + 1) LC - 1. Before pass 0 (onlxpre) every |
| 1768 | note row of the range is active and its input is kept in onlpin. After pass p (onlxpost) every row |
| 1769 | note still active gets its normalized delta rel = |out - in| / max(|in|, 1e-12); its message |
| 1770 | note becomes out - in; when rel is below the threshold and a pass remains, the row leaves the loop |
| 1771 | note (onlxp[t] = p + 1) and olayf2x copies it unchanged through the remaining core applications. |
| 1772 | note A row still active enters pass p + 1 with out + gain * message. Every quantity of row t reads |
| 1773 | note only row t, so the full forward (ofwd) and the cached decode (ofwdi) compute the same bits. |
| 1774 | |
| 1775 | note true when the hooks run: a loop, and an exit threshold, a message gain or telemetry |
| 1776 | fn onlxon to bool |
| 1777 | if le get og 59 1 |
| 1778 | ret false |
| 1779 | end |
| 1780 | ret or or ne get onlx 0 0 ne get onlx 1 0 ne get onlx 6 0 |
| 1781 | end |
| 1782 | |
| 1783 | note the core pass of application l, or NP when l is outside the core (or there is no loop) |
| 1784 | fn onlxpass l ulong to ulong |
| 1785 | np = get og 59 |
| 1786 | if le np 1 |
| 1787 | ret np |
| 1788 | end |
| 1789 | lp = get og 57 |
| 1790 | if lt l lp |
| 1791 | ret np |
| 1792 | end |
| 1793 | j = sub l lp |
| 1794 | lc = get og 58 |
| 1795 | if ge j mul lc np |
| 1796 | ret np |
| 1797 | end |
| 1798 | ret div j lc |
| 1799 | end |
| 1800 | |
| 1801 | note true when row t left the loop before the core pass of application l |
| 1802 | fn onlfz l ulong t ulong to bool |
| 1803 | p = onlxpass l |
| 1804 | if or eq p 0 ge p get og 59 |
| 1805 | ret false |
| 1806 | end |
| 1807 | ret le get onlxp t p |
| 1808 | end |
| 1809 | |
| 1810 | note before application l = LP (pass 0): rows t0 .. t0 + nr - 1 active, inputs kept, optional LTP0 message |
| 1811 | fn onlxpre l ulong t0 ulong nr ulong to void |
| 1812 | if ne l get og 57 |
| 1813 | ret |
| 1814 | end |
| 1815 | for i ulong nr |
| 1816 | onlxpre1 l add t0 i |
| 1817 | end |
| 1818 | end |
| 1819 | |
| 1820 | fn onlxpre1 l ulong t ulong to void |
| 1821 | put onlxp t get og 59 |
| 1822 | put onlmn t 0.0f64 |
| 1823 | put onlxd t 0.0f64 |
| 1824 | d = get og 2 |
| 1825 | r = mul t d |
| 1826 | ho = add oahin l r |
| 1827 | if and ne get onlx 2 0 ne get onlx 1 0 |
| 1828 | onlxseed r d |
| 1829 | onlxmix ho r d |
| 1830 | put onlmn t onlxnrm r d |
| 1831 | ret |
| 1832 | end |
| 1833 | for i ulong d |
| 1834 | put onlpin add r i get oa add ho i |
| 1835 | put onlmsg add r i 0.0f64 |
| 1836 | end |
| 1837 | end |
| 1838 | |
| 1839 | note row t's message = onlf[1] onlv / rms(onlv) (the LTP0 vector handed off as the pass-0 message) |
| 1840 | fn onlxseed r ulong d ulong to void |
| 1841 | sc = div get onlf 1 onlv_rms d |
| 1842 | for i ulong d |
| 1843 | put onlmsg add r i mul sc get onlv i |
| 1844 | end |
| 1845 | end |
| 1846 | |
| 1847 | note |onlmsg row at r| |
| 1848 | fn onlxnrm r ulong d ulong to f64 |
| 1849 | ss = 0.0f64 |
| 1850 | for i ulong d |
| 1851 | v = get onlmsg add r i |
| 1852 | ss = add ss mul v v |
| 1853 | end |
| 1854 | ret fsqrt ss |
| 1855 | end |
| 1856 | |
| 1857 | note after application l: when it ends core pass p, every row still active in it (onlxrow) |
| 1858 | fn onlxpost l ulong t0 ulong nr ulong to void |
| 1859 | p = onlxpass l |
| 1860 | if ge p get og 59 |
| 1861 | ret |
| 1862 | end |
| 1863 | lc = get og 58 |
| 1864 | if ne rem sub l get og 57 lc sub lc 1 |
| 1865 | ret |
| 1866 | end |
| 1867 | for i ulong nr |
| 1868 | t = add t0 i |
| 1869 | if gt get onlxp t p |
| 1870 | onlxrow l t p |
| 1871 | end |
| 1872 | end |
| 1873 | end |
| 1874 | |
| 1875 | note row t after core pass p (application l its last): delta, message, exit, the next pass's input |
| 1876 | fn onlxrow l ulong t ulong p ulong to void |
| 1877 | d = get og 2 |
| 1878 | r = mul t d |
| 1879 | ou = add oaout l r |
| 1880 | rel = onlxdel ou r d |
| 1881 | if eq p 0 |
| 1882 | put onlxd t rel |
| 1883 | end |
| 1884 | if ne get onlx 1 0 |
| 1885 | put onlmn t get onlxs 0 |
| 1886 | end |
| 1887 | q = add p 1 |
| 1888 | if ge q get og 59 |
| 1889 | ret |
| 1890 | end |
| 1891 | if ne get onlx 0 0 |
| 1892 | if lt rel div f64 get onlx 0 1000000.0 |
| 1893 | put onlxp t q |
| 1894 | ret |
| 1895 | end |
| 1896 | end |
| 1897 | onlxmix ou r d |
| 1898 | end |
| 1899 | |
| 1900 | note rel = |out - in| / max(|in|, 1e-12) of the row (out at ou in oa, in at onlpin r); |out - in| into |
| 1901 | note onlxs[0]; the message onlmsg row = out - in when the gain is on |
| 1902 | fn onlxdel ou ulong r ulong d ulong to f64 |
| 1903 | ss = 0.0f64 |
| 1904 | si = 0.0f64 |
| 1905 | wm = ne get onlx 1 0 |
| 1906 | for i ulong d |
| 1907 | pv = get onlpin add r i |
| 1908 | dv = sub get oa add ou i pv |
| 1909 | ss = add ss mul dv dv |
| 1910 | si = add si mul pv pv |
| 1911 | if wm |
| 1912 | put onlmsg add r i dv |
| 1913 | end |
| 1914 | end |
| 1915 | put onlxs 0 fsqrt ss |
| 1916 | ret div fsqrt ss fmax fsqrt si 1.0e-12f64 |
| 1917 | end |
| 1918 | |
| 1919 | note the row at ou becomes out + gain * message (gain on) and the next pass's input onlpin |
| 1920 | fn onlxmix ou ulong r ulong d ulong to void |
| 1921 | g = div f64 get onlx 1 1000000.0 |
| 1922 | wm = ne get onlx 1 0 |
| 1923 | for i ulong d |
| 1924 | q = add ou i |
| 1925 | v = get oa q |
| 1926 | if wm |
| 1927 | v = add v mul g get onlmsg add r i |
| 1928 | put oa q v |
| 1929 | end |
| 1930 | put onlpin add r i v |
| 1931 | end |
| 1932 | end |
| 1933 | |
| 1934 | note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte; |
| 1935 | note DH[t] = rms backward of HF[t]. |
| 1936 | fn ohbwd b ulong t ulong sc f64 to void |
| 1937 | v = get og 0 |
| 1938 | pr = add get og 18 mul t v |
| 1939 | y = get oby add mul b get og 1 t |
| 1940 | kdlog pr v y sc |
| 1941 | ohbwd2 pr t |
| 1942 | end |
| 1943 | |
| 1944 | note p[c] = (p[c] - [c = y]) sc for c < n |
| 1945 | fn kdlog po ulong n ulong y ulong sc f64 to void |
| 1946 | for c ulong n |
| 1947 | g = get oa add po c |
| 1948 | if eq c y |
| 1949 | g = sub g 1.0 |
| 1950 | end |
| 1951 | put oa add po c mul g sc |
| 1952 | end |
| 1953 | end |
| 1954 | |
| 1955 | fn ohbwd2 pr ulong t ulong to void |
| 1956 | d = get og 2 |
| 1957 | v = get og 0 |
| 1958 | r = mul t d |
| 1959 | z = add get og 17 r |
| 1960 | kger pr z get og 10 v d |
| 1961 | dz = add get og 25 r |
| 1962 | kvm pr ulong 0 dz v d |
| 1963 | hf = add get og 15 r |
| 1964 | dh = add get og 19 r |
| 1965 | brms hf get og 33 add get og 16 t dz dh |
| 1966 | end |
| 1967 | |
| 1968 | note Mutant lp (the w2 gradient of a layer application that repeats an earlier parameter layer is |
| 1969 | note dropped, so a core w2 gets only the gradient of its first pass): 1 in the product, 0 in the |
| 1970 | note mutant, which gcheck on a looped checkpoint must catch. Without a loop the two are the same. |
| 1971 | note (Named OMUTLP: OMUTL is gpu/gk.om's BF16 mutant l, and the gb10 unit holds both files.) |
| 1972 | const OMUTLP ulong 1 |
| 1973 | |
| 1974 | note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path |
| 1975 | fn offb l ulong t ulong to void |
| 1976 | f = get og 4 |
| 1977 | dh = add get og 19 mul t get og 2 |
| 1978 | df = add get og 26 mul t f |
| 1979 | ozero df f |
| 1980 | kvta dh ovw2 l df f get og 2 |
| 1981 | if eq get ogkm 0 0 |
| 1982 | kdrelu df add oafp l mul t f f |
| 1983 | else |
| 1984 | ogmask df l t |
| 1985 | end |
| 1986 | g2 = add get og 10 ovw2 l |
| 1987 | if or eq OMUTLP 1 eq olp l l |
| 1988 | kger add oarl l mul t f dh g2 f get og 2 |
| 1989 | end |
| 1990 | offb2 l t df |
| 1991 | end |
| 1992 | |
| 1993 | note y[j] = 0 where x[j] <= 0, for j < n |
| 1994 | fn kdrelu yo ulong xo ulong n ulong to void |
| 1995 | for j ulong n |
| 1996 | if le get oa add xo j 0.0 |
| 1997 | put oa add yo j 0.0f64 |
| 1998 | end |
| 1999 | end |
| 2000 | end |
| 2001 | |
| 2002 | fn offb2 l ulong t ulong df ulong to void |
| 2003 | d = get og 2 |
| 2004 | r = mul t d |
| 2005 | g1 = add get og 10 ovw1 l |
| 2006 | kger add oabn l r df g1 d get og 4 |
| 2007 | da = add get og 20 r |
| 2008 | ozero da d |
| 2009 | kvta df ovw1 l da d get og 4 |
| 2010 | hm = add oahm l r |
| 2011 | dh = add get og 19 r |
| 2012 | brms hm ovg2 l add oas2 l t da dh |
| 2013 | end |
| 2014 | |
| 2015 | note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j] |
| 2016 | fn oattd l ulong n ulong t ulong to f64 |
| 2017 | e = get og 7 |
| 2018 | dor = add add get og 24 mul t get og 2 mul n e |
| 2019 | uo = add oau l mul n e |
| 2020 | pa = opar l n t |
| 2021 | ret kattd dor uo pa add t 1 |
| 2022 | end |
| 2023 | |
| 2024 | fn kattd dor ulong uo ulong pa ulong nj ulong to f64 |
| 2025 | d = get og 2 |
| 2026 | de = add dor get og 7 |
| 2027 | dt = 0.0f64 |
| 2028 | j = ulong 0 |
| 2029 | while le add j 4 nj bound 999999 |
| 2030 | kdot4a dor de uo d |
| 2031 | dt = kattd4 add pa j add get og 27 j dt |
| 2032 | uo = add uo mul 4 d |
| 2033 | j = add j 4 |
| 2034 | end |
| 2035 | while lt j nj bound 4 |
| 2036 | dp = kdot1a dor de uo |
| 2037 | put oa add get og 27 j dp |
| 2038 | a = opag add pa j |
| 2039 | dt = add dt mul a dp |
| 2040 | uo = add uo d |
| 2041 | j = add j 1 |
| 2042 | end |
| 2043 | ret dt |
| 2044 | end |
| 2045 | |
| 2046 | note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order |
| 2047 | fn kattd4 p ulong tm ulong dt f64 to f64 |
| 2048 | for i ulong 4 |
| 2049 | dp = get oq i |
| 2050 | put oa add tm i dp |
| 2051 | a = opag add p i |
| 2052 | dt = add dt mul a dp |
| 2053 | end |
| 2054 | ret dt |
| 2055 | end |
| 2056 | |
| 2057 | note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied) |
| 2058 | global oz ulong 8 |
| 2059 | |
| 2060 | 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] |
| 2061 | fn oatta l ulong n ulong t ulong dt f64 to void |
| 2062 | e = get og 7 |
| 2063 | hn = mul n e |
| 2064 | r = mul t get og 2 |
| 2065 | put ofs 2 dt |
| 2066 | put oz 0 add add oaq l r hn |
| 2067 | put oz 1 add add get og 21 r hn |
| 2068 | put oz 2 add add get og 24 r hn |
| 2069 | put oz 3 add oak l hn |
| 2070 | put oz 4 add get og 22 hn |
| 2071 | put oz 5 add get og 23 hn |
| 2072 | pa = opar l n t |
| 2073 | oatta2 pa add t 1 |
| 2074 | end |
| 2075 | |
| 2076 | fn oatta2 pa ulong nj ulong to void |
| 2077 | d = get og 2 |
| 2078 | e = get og 7 |
| 2079 | for j ulong nj |
| 2080 | jr = mul j d |
| 2081 | ds = oads pa j |
| 2082 | kaxpy get oz 1 add get oz 3 jr ds e |
| 2083 | if eq OMUTA 1 |
| 2084 | kaxpy add get oz 4 jr get oz 0 ds e |
| 2085 | else |
| 2086 | kaxpy add get oz 4 jr get oz 0 neg ds e |
| 2087 | end |
| 2088 | pj = opag add pa j |
| 2089 | kaxpy add get oz 5 jr get oz 2 pj e |
| 2090 | end |
| 2091 | end |
| 2092 | |
| 2093 | note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2 |
| 2094 | fn oads pa ulong j ulong to f64 |
| 2095 | pj = opag add pa j |
| 2096 | u = sub get oa add get og 27 j get ofs 2 |
| 2097 | ret div mul pj u fsqrt f64 get og 7 |
| 2098 | end |
| 2099 | |
| 2100 | fn olayb l ulong to void |
| 2101 | ostw olp l |
| 2102 | oaflip l false |
| 2103 | ct = get og 9 |
| 2104 | for t ulong ct |
| 2105 | offb l t |
| 2106 | end |
| 2107 | for t2 ulong ct |
| 2108 | olaybo l t2 |
| 2109 | end |
| 2110 | d = get og 2 |
| 2111 | ozero get og 21 mul ct d |
| 2112 | ozero get og 22 mul ct d |
| 2113 | ozero get og 23 mul ct d |
| 2114 | for n ulong get og 3 |
| 2115 | for t3 ulong ct |
| 2116 | dt = oattd l n t3 |
| 2117 | oatta l n t3 dt |
| 2118 | end |
| 2119 | end |
| 2120 | for t4 ulong ct |
| 2121 | olayb2 l t4 |
| 2122 | end |
| 2123 | end |
| 2124 | |
| 2125 | note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t] |
| 2126 | fn olaybo l ulong t ulong to void |
| 2127 | d = get og 2 |
| 2128 | r = mul t d |
| 2129 | dor = add get og 24 r |
| 2130 | dh = add get og 19 r |
| 2131 | ozero dor d |
| 2132 | kvta dh ovwo l dor d d |
| 2133 | kger add oao l r dh add get og 10 ovwo l d d |
| 2134 | end |
| 2135 | |
| 2136 | note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path |
| 2137 | fn olayb2 l ulong t ulong to void |
| 2138 | d = get og 2 |
| 2139 | r = mul t d |
| 2140 | da = add get og 20 r |
| 2141 | ozero da d |
| 2142 | kvta add get og 21 r ovwq l da d d |
| 2143 | kvta add get og 22 r ovwk l da d d |
| 2144 | kvta add get og 23 r ovwv l da d d |
| 2145 | olayb3 l t |
| 2146 | end |
| 2147 | |
| 2148 | fn olayb3 l ulong t ulong to void |
| 2149 | d = get og 2 |
| 2150 | r = mul t d |
| 2151 | a = add oaa l r |
| 2152 | g0 = get og 10 |
| 2153 | kger a add get og 21 r add g0 ovwq l d d |
| 2154 | kger a add get og 22 r add g0 ovwk l d d |
| 2155 | kger a add get og 23 r add g0 ovwv l d d |
| 2156 | hi = add oahin l r |
| 2157 | dh = add get og 19 r |
| 2158 | brms hi ovg1 l add oas1 l t add get og 20 r dh |
| 2159 | end |
| 2160 | |
| 2161 | note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of |
| 2162 | note the step loss. Gradients accumulate into the gradient at og[10] in ogr. |
| 2163 | fn obwd b ulong sc f64 to void |
| 2164 | ct = get og 9 |
| 2165 | ozero get og 19 mul ct get og 2 |
| 2166 | for t ulong ct |
| 2167 | ohbwd b t sc |
| 2168 | end |
| 2169 | ll = get og 56 |
| 2170 | fl = get ofzlay 0 |
| 2171 | for i ulong sub ll fl |
| 2172 | olayb sub sub ll 1 i |
| 2173 | end |
| 2174 | if eq fl 0 |
| 2175 | for t2 ulong ct |
| 2176 | obwde b t2 |
| 2177 | end |
| 2178 | end |
| 2179 | end |
| 2180 | |
| 2181 | note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t] |
| 2182 | fn obwde b ulong t ulong to void |
| 2183 | d = get og 2 |
| 2184 | c = get obx add mul b get og 1 t |
| 2185 | dh = add get og 19 mul t d |
| 2186 | g0 = get og 10 |
| 2187 | kaxpg add g0 mul c d dh 1.0 d |
| 2188 | kaxpg add add g0 get og 30 mul t d dh 1.0 d |
| 2189 | end |