Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-tool-model/blob/main/deployment/speed/timed.om # omega-tool-model First-party Omega tool-use model source, serving harnesses, and verification records Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-tool-model / deployment / speed / timed.om` 11505 lines ยท 244102 bytes Raw Blame History 1 global tmarks ulong 180 2 global tbytes uchar 1440 3 note SPDX-License-Identifier: AGPL-3.0-only 4 note omegallm math.om (aien-train#52): first-party f64 exp and log, splitmix64, FNV-1a 64, 5 note little-endian byte packing and the stdout writer. First unit; see docs/omegallm/README.md. 6 note Constants (Wolfram, 2026-11-01): 7 note splitmix64: IntegerString[`11400714819323198485`,16] -> `9e3779b97f4a7c15`, 8 note IntegerString[`13787848793156543929`,16] -> `bf58476d1ce4e5b9`, 9 note IntegerString[`10723151780598845931`,16] -> `94d049bb133111eb`. 10 note FNV-1a 64: IntegerString[`14695981039346656037`,16] -> `cbf29ce484222325`, prime `100000001b3`. 11 note 1/ln2: N[1/Log[2]] bits `4609176140021203710` -> 1.`4426950408889634`. 12 note ln2 hi = 0x3FE62E42FEE00000 (low 21 bits zero, so k*hi is exact for |k| < 2^20) 13 note = 0.`6931471803691238`; lo = N[Log[2]-hi] -> 1.`9082149292705877`*^-10. 14 note overflow: N[Log[$MaxMachineNumber]] -> 709.`782712893384`; underflow to 0 below 15 note N[Log[2^-1075]] -> -745.`1332191019412`. 16 note 1/k! for k=2..13 and 1/(2k+1) for k=1..11: Table[ToString[N[1/k!],InputForm],{k,2,13}], 17 note Table[ToString[N[1/(2k+1)],InputForm],{k,1,11}] (values below). 18 note Taylor remainder of exp at |r| <= ln2/2: N[0.3466^14/14!] -> 4.14*^-18 (degree 13). 19 note atanh series remainder at s <= 3-2Sqrt[2]: N[(3-2Sqrt[2])^2]^11/23 -> 6.25*^-19 (to s^23). 20 note bit patterns: 2^52 = `4503599627370496`, +inf `9218868437227405312`, -inf `18442240474082181120`, 21 note NaN `9221120237041090560`, exponent 1023 field `4607182418800017408`, 2^54 = `18014398509481984`. 22 23 const OMOUT ulong 4096 24 global oout uchar 4096 25 global oon ulong 1 26 note og: run geometry shared by all units (slots listed in model.om). 27 global og ulong 64 28 29 fn smx x ulong to ulong 30 z = add x `11400714819323198485` 31 z = mul xor z shr z 30 `13787848793156543929` 32 z = mul xor z shr z 27 `10723151780598845931` 33 ret xor z shr z 31 34 end 35 36 note e^x for every f64 x. Cody-Waite reduction x = k ln2 + r with |r| <= ln2/2, then the Taylor 37 note polynomial of degree 13 in Horner form, then scaling by 2^k1 * 2^k2 (k1 = k/2) so every 38 note intermediate is normal and a subnormal result is rounded once. 39 fn fexp x f64 to f64 40 if ne x x 41 ret x 42 end 43 if gt x 709.`782712893384` 44 ret bitcast f64 ulong `9218868437227405312` 45 end 46 if lt x -745.`1332191019412` 47 ret 0.0f64 48 end 49 kf = mul x 1.`4426950408889634` 50 if ge kf 0.0 51 kf = add kf 0.5 52 else 53 kf = sub kf 0.5 54 end 55 k = long kf 56 fk = f64 k 57 r = sub sub x mul fk 0.`6931471803691238` mul fk 1.`9082149292705877e`-10 58 p = add mul r 1.`6059043836821613e`-10 2.`08767569878681e`-9 59 p = add mul p r 2.`505210838544172e`-8 60 p = add mul p r 2.`755731922398589e`-7 61 p = add mul p r 2.`7557319223985893e`-6 62 p = add mul p r 0.`0000248015873015873` 63 p = add mul p r 0.`0001984126984126984` 64 p = add mul p r 0.`001388888888888889` 65 p = add mul p r 0.`008333333333333333` 66 p = add mul p r 0.`041666666666666664` 67 p = add mul p r 0.`16666666666666666` 68 p = add mul p r 0.5 69 p = add mul p r 1.0 70 p = add mul p r 1.0 71 k1 = div k 2 72 k2 = sub k k1 73 s1 = bitcast f64 shl ulong add k1 1023 52 74 s2 = bitcast f64 shl ulong add k2 1023 52 75 ret mul mul p s1 s2 76 end 77 78 note natural log for every f64 x: NaN for x < 0 or NaN, -inf for 0, +inf for +inf. x = m 2^e with 79 note m in [sqrt(1/2), sqrt 2]; log m = 2 atanh(s), s = (m-1)/(m+1), series to s^23. 80 fn flog x f64 to f64 81 if ne x x 82 ret x 83 end 84 if lt x 0.0 85 ret bitcast f64 ulong `9221120237041090560` 86 end 87 if eq x 0.0 88 ret bitcast f64 ulong `18442240474082181120` 89 end 90 b = bitcast ulong x 91 if eq b `9218868437227405312` 92 ret x 93 end 94 e = long 0 95 if lt b `4503599627370496` 96 b = bitcast ulong mul x `18014398509481984`.0 97 e = long -54 98 end 99 e = add e sub long shr b 52 1023 100 m = bitcast f64 or and b `4503599627370495` `4607182418800017408` 101 if gt m 1.`4142135623730951` 102 m = mul m 0.5 103 e = add e 1 104 end 105 f = sub m 1.0 106 s = div f add f 2.0 107 z = mul s s 108 q = add mul z 0.`043478260869565216` 0.`047619047619047616` 109 q = add mul q z 0.`05263157894736842` 110 q = add mul q z 0.`058823529411764705` 111 q = add mul q z 0.`06666666666666667` 112 q = add mul q z 0.`07692307692307693` 113 q = add mul q z 0.`09090909090909091` 114 q = add mul q z 0.`1111111111111111` 115 q = add mul q z 0.`14285714285714285` 116 q = add mul q z 0.2 117 q = add mul q z 0.`3333333333333333` 118 q = add mul q z 1.0 119 lm = mul add s s q 120 fe = f64 e 121 ret add mul fe 0.`6931471803691238` add lm mul fe 1.`9082149292705877e`-10 122 end 123 124 note round(v * 10^6) half away from zero (CONTRACT section 7). 125 fn rmic v f64 to long 126 x = mul v `1000000`.0 127 q = long x 128 fr = sub x f64 q 129 if ge fr 0.5 130 q = add q 1 131 end 132 if le fr -0.5 133 q = sub q 1 134 end 135 ret q 136 end 137 138 fn oputc c ulong to void 139 n = get oon 0 140 if ge n OMOUT 141 oflush 142 n = ulong 0 143 end 144 put oout n uchar and c 255 145 put oon 0 add n 1 146 end 147 148 fn oputu v ulong to void 149 d = new uchar 24 0 150 n = ulong 0 151 x = v 152 while true bound 21 153 put d n uchar add rem x 10 48 154 n = add n 1 155 x = div x 10 156 if eq x 0 157 break 158 end 159 end 160 for i ulong n 161 oputc ulong get d sub sub n 1 i 162 end 163 end 164 165 fn oputl v long to void 166 if lt v 0 167 oputc 45 168 if eq v -`9223372036854775808` 169 oputu `9223372036854775808` 170 else 171 oputu ulong neg v 172 end 173 else 174 oputu ulong v 175 end 176 end 177 178 note 16 lowercase hex digits. 179 fn oputx v ulong to void 180 for i ulong 16 181 h = and shr v mul sub 15 i 4 15 182 if lt h 10 183 oputc add h 48 184 else 185 oputc add h 87 186 end 187 end 188 end 189 note SPDX-License-Identifier: AGPL-3.0-only 190 note omegallm data.om (aien-train#52): argument paths, whole-file data reads, prep (CONTRACT 191 note section 4), the splitmix64 batch rule and the add-one byte baselines. Needs math.om before it. 192 note A data file lives in two byte pools (od0 then od1), 2^25 bytes in all; a byte is read by odb. 193 const ODHALF ulong `16777216` 194 const ODCAP ulong `33554432` 195 global od0 uchar `16777216` 196 global od1 uchar `16777216` 197 global odn ulong 1 198 global oarg uchar 4100 199 global ochk uchar 65536 200 global obx ulong 16384 201 global oby ulong 16384 202 global owb0 uchar 65536 203 global owb1 uchar 65536 204 global owbn ulong 2 205 global ocnt ulong 65536 206 global ocnu ulong 256 207 global ocnr ulong 256 208 global oseps uchar 16 209 note v1 (CONTRACT-V1 section 3): the window that the platform's positioned read oprd fills; prep, 210 note the bpe commands and the windowed data reads all use it. 211 const OPWCAP ulong `2097152` 212 global opw uchar `2097152` 213 214 note exit codes of this unit: 2 usage, 20 data file empty or too short for T, 21 data file over 215 note 2^25 bytes, 70 I/O. 216 217 note argument i into oarg; returns its length (1 .. 4096) or exits 2. 218 fn oargp i ulong to ulong 219 r = oargr i 220 if or le r 0 gt r 4096 221 exit 2 222 end 223 ret ulong r 224 end 225 226 note argv[i] as an unsigned decimal of 1 .. 19 digits. 227 fn oargu i ulong to ulong 228 n = oargp i 229 if gt n 19 230 exit 2 231 end 232 v = ulong 0 233 for k ulong n 234 c = ulong get oarg k 235 if or lt c 48 gt c 57 236 exit 2 237 end 238 v = add mul v 10 sub c 48 239 end 240 ret v 241 end 242 243 fn odb i ulong to ulong 244 if lt i ODHALF 245 ret ulong get od0 i 246 end 247 ret ulong get od1 sub i ODHALF 248 end 249 250 fn odset i ulong c ulong to void 251 if lt i ODHALF 252 put od0 i uchar c 253 else 254 put od1 sub i ODHALF uchar c 255 end 256 end 257 258 note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD); owopen, owflush and owclose are 259 note the platform unit's (lio.om, eio.om). 260 fn owbyte k ulong c ulong to void 261 n = get owbn k 262 if ge n 65536 263 owflush k 264 n = ulong 0 265 end 266 if eq k 0 267 put owb0 n uchar c 268 else 269 put owb1 n uchar c 270 end 271 put owbn k add n 1 272 end 273 274 note 1 when the separator <|endoftext|> starts at opw[p] (p + 13 <= n). 275 fn oissep p ulong n ulong to bool 276 if gt add p 13 n 277 ret false 278 end 279 for k ulong 13 280 if ne get opw add p k get oseps k 281 ret false 282 end 283 end 284 ret true 285 end 286 287 fn owsp c ulong to bool 288 ret or or eq c 9 eq c 10 or eq c 13 eq c 32 289 end 290 291 note Emit story opw[a, e) after trimming whitespace; an empty story is dropped. st[0..4] count 292 note stories, train stories, held stories, train bytes, held bytes. A story over 999999 bytes is 293 note refused with exit 24 (cmdprep refuses it already while scanning; every loop here is bounded). 294 fn ostory a ulong e ulong st edit ulong 8 to void 295 if gt sub e a 999999 296 exit 24 297 end 298 lo = a 299 hi = e 300 while lt lo hi bound 999999 301 if not owsp ulong get opw lo 302 break 303 end 304 lo = add lo 1 305 end 306 while gt hi lo bound 999999 307 if not owsp ulong get opw sub hi 1 308 break 309 end 310 hi = sub hi 1 311 end 312 if lt lo hi 313 k = ulong 0 314 if eq rem get st 0 10 9 315 k = ulong 1 316 end 317 put st 0 add get st 0 1 318 put st add k 1 add get st add k 1 1 319 for i ulong sub hi lo 320 owbyte k ulong get opw add lo i 321 end 322 owbyte k 0 323 put st add k 3 add add get st add k 3 sub hi lo 1 324 end 325 end 326 327 note `<|endoftext|>` = 60 124 101 110 100 111 102 116 101 120 116 124 62. 328 fn oseptab to void 329 put oseps 0 60 330 put oseps 1 124 331 put oseps 2 101 332 put oseps 3 110 333 put oseps 4 100 334 put oseps 5 111 335 put oseps 6 102 336 put oseps 7 116 337 put oseps 8 101 338 put oseps 9 120 339 put oseps 10 116 340 put oseps 11 124 341 put oseps 12 62 342 end 343 344 note Stream window over argv[ai] (prep and the bpe commands): opw holds the file's bytes 345 note [wb, wb + wn) with opst = wb, wn, eof (1 once a refill came back short). opstart fills it from 346 note byte 0; opshift a moves opw[a, wn) to the front and refills the rest of the window. 347 global opst ulong 4 348 fn opstart ai ulong to void 349 wn = oprd ai 0 0 OPWCAP 350 put opst 0 0 351 put opst 1 wn 352 put opst 2 0 353 if lt wn OPWCAP 354 put opst 2 1 355 end 356 end 357 358 fn opshift ai ulong a ulong to void 359 wn = get opst 1 360 cnt = sub wn a 361 i = ulong 0 362 for o ulong 3 363 lim = mul add o 1 999999 364 if gt lim cnt 365 lim = cnt 366 end 367 while lt i lim bound 999999 368 put opw i get opw add a i 369 i = add i 1 370 end 371 end 372 wb = add get opst 0 a 373 wn = sub wn a 374 r = oprd ai add wb wn wn sub OPWCAP wn 375 put opst 0 wb 376 put opst 1 add wn r 377 put opst 2 0 378 if lt r sub OPWCAP wn 379 put opst 2 1 380 end 381 end 382 383 note prep IN TRAIN HELD (CONTRACT-V1 section 3): IN is read as a stream through the window opw. 384 note The current story starts at opw[a]; p scans. When the separator test at p needs bytes past 385 note the window, opshift moves [a, wn) to the front and refills. A story whose raw length passes 386 note 999999 is exit 24 as soon as the scan shows it (v0 refused the same story, after the same 387 note earlier stories, in ostory); so a moved story keeps at most `1000012` bytes and every refill 388 note reads at least `1097140`. Each inner pass advances p by 1 or 13 or ends the scan: 999999 x 999999 389 note passes cover any IN below 2^39 bytes. 390 fn cmdprep to void 391 if ne oargc 5 392 exit 2 393 end 394 oseptab 395 opstart 2 396 wn = get opst 1 397 eof = ne get opst 2 0 398 owopen 0 3 399 owopen 1 4 400 st = new ulong 8 0 401 a = ulong 0 402 p = ulong 0 403 dn = false 404 while not dn bound 999999 405 q = ulong 0 406 while and not dn lt q 999999 bound 999999 407 q = add q 1 408 if and gt add p 13 wn not eof 409 opshift 2 a 410 p = sub p a 411 a = ulong 0 412 wn = get opst 1 413 eof = ne get opst 2 0 414 end 415 if ge p wn 416 dn = true 417 else 418 if eq get opw p 60 419 if oissep p wn 420 ostory a p st 421 p = add p 13 422 a = p 423 continue 424 end 425 end 426 p = add p 1 427 if gt sub p a 999999 428 exit 24 429 end 430 end 431 end 432 end 433 ostory a wn st 434 owclose 0 435 owclose 1 436 oputc 80 437 for i ulong 5 438 oputc 32 439 oputu get st i 440 end 441 oputc 10 442 oflush 443 end 444 445 note Batch for step s (CONTRACT section 4): x and y for b = 0..B-1 into obx/oby (stride T); N, the 446 note data length in tokens, is odn[0] (odopen). 447 note OMUTB is the target offset: 1 in the product, 2 in mutant b (targets shifted by one). 448 const OMUTB ulong 1 449 fn obatch s ulong to void 450 bb = get og 6 451 tt = get og 1 452 n = get odn 0 453 sd = get og 40 454 for b ulong bb 455 o = rem smx xor sd mul add mul s bb b `11400714819323198485` sub n tt 456 obrow b o 457 end 458 end 459 460 note v1 data file (CONTRACT-V1 section 3): argument ai holds bytes when V = 256 and u16 461 note little-endian tokens when V > 256 (og 49 = bytes per token, og 47 = the argument). Sets 462 note odn[0] = N tokens; exit 21 on an odd byte length at V > 256, 20 when N < T + 1. Rows are read 463 note through oprd when used, so a token >= V is refused (25) only in a row actually read. 464 fn odopen ai ulong to void 465 w = ulong 1 466 if gt get og 0 256 467 w = 2 468 end 469 nb = ofsz ai 470 if ne rem nb w 0 471 exit 21 472 end 473 put og 47 ai 474 put og 49 w 475 put odn 0 div nb w 476 if le get odn 0 get og 1 477 exit 20 478 end 479 end 480 481 note token k of the window last read by odrow. OMUTE: 1 in the product (u16 little-endian); 0 in 482 note mutant e (big-endian), which the referee comparison must catch. 483 const OMUTE ulong 1 484 fn odtk k ulong to ulong 485 if eq get og 49 1 486 ret ulong get opw k 487 end 488 lo = ulong get opw mul k 2 489 hi = ulong get opw add mul k 2 1 490 if eq OMUTE 0 491 ret or shl lo 8 hi 492 end 493 ret or shl hi 8 lo 494 end 495 496 note read tokens o .. o + n - 1 (fewer at the end of the file; the rest read as 0) into opw; 497 note exit 25 when a token read is >= V 498 fn odrow o ulong n ulong to void 499 w = get og 49 500 nb = mul n w 501 for i ulong nb 502 put opw i 0 503 end 504 r = oprd get og 47 mul o w 0 nb 505 m = div r w 506 v = get og 0 507 for k ulong m 508 if ge odtk k v 509 exit 25 510 end 511 end 512 end 513 514 fn obrow b ulong o ulong to void 515 tt = get og 1 516 odrow o add tt OMUTB 517 for t ulong tt 518 put obx add mul b tt t odtk t 519 put oby add mul b tt t odtk add t OMUTB 520 end 521 end 522 523 note base TRAIN HELD (CONTRACT section 7 and agreed reading A5): unigram and bigram add-one 524 note counts over all TRAIN bytes and all N-1 adjacent TRAIN pairs; scored on HELD targets 1..M-1. 525 fn cmdbase to void 526 if ne oargc 4 527 exit 2 528 end 529 n = odread 2 530 if lt n 2 531 exit 20 532 end 533 for o ulong 64 534 for i ulong 524288 535 j = add mul o 524288 i 536 if ge j n 537 break 538 end 539 obfit j 540 end 541 end 542 m = odread 3 543 if lt m 2 544 exit 20 545 end 546 su = 0.0f64 547 sb = 0.0f64 548 dn = f64 add n 256 549 for o2 ulong 64 550 for i2 ulong 524288 551 j2 = add mul o2 524288 i2 552 if ge j2 m 553 break 554 end 555 if gt j2 0 556 su = add su obsu j2 dn 557 sb = add sb obsb j2 558 end 559 end 560 end 561 fm = f64 sub m 1 562 oputc 66 563 oputc 32 564 oputl rmic div div neg su fm 0.`6931471805599453` 565 oputc 32 566 oputl rmic div div neg sb fm 0.`6931471805599453` 567 oputc 10 568 oflush 569 end 570 571 note count TRAIN byte j and the pair (j-1, j) 572 fn obfit j ulong to void 573 c = odb j 574 put ocnu c add get ocnu c 1 575 if gt j 0 576 pv = odb sub j 1 577 q = add mul pv 256 c 578 put ocnt q add get ocnt q 1 579 put ocnr pv add get ocnr pv 1 580 end 581 end 582 583 note log of the unigram probability (c+1)/(N+256) of HELD byte j 584 fn obsu j ulong dn f64 to f64 585 ret flog div f64 add get ocnu odb j 1 dn 586 end 587 588 note log of the bigram probability (c+1)/(row+256) of HELD pair (j-1, j) 589 fn obsb j ulong to f64 590 pv = odb sub j 1 591 q = add mul pv 256 odb j 592 ret flog div f64 add get ocnt q 1 f64 add get ocnr pv 256 593 end 594 note SPDX-License-Identifier: AGPL-3.0-only 595 note omegallm lpool.om (aien-train#52, CONTRACT-V3 section 2): the bank-B pools of the Linux targets 596 note (aarch64, gb10, x86linux), concatenated after data.om and before model.om; eio targets use 597 note epool.om. Sizes at the CONTRACT-V3 maxima (V 4096, T 256, D 384, H 16, F 1536, L 16), Wolfram: 598 note oww parameters of layers LA .. L-1: (16 - 7) LSTR = 9 `1770240` -> `15932160` (LA = 7) 599 note ogb BLK values past OGC: P - OGC = `29995392` - `15833472` -> `14161920` 600 note oab activations of layers LAA .. L-1: (16 - 6) ASTR = 10 `1573376` -> `15733760` (LAA = 6) 601 const OWWN ulong `15932160` 602 global oww f64 `15932160` 603 const OGBN ulong `14161920` 604 global ogb f64 `14161920` 605 const OABN ulong `15733760` 606 global oab f64 `15733760` 607 note SPDX-License-Identifier: AGPL-3.0-only 608 note omegallm model.om (aien-train#52): geometry, forward pass and hand-derived backward pass 609 note (CONTRACT sections 1-3, CONTRACT-V1 section 1). Needs math.om and data.om before it. 610 note 611 note f64 values live in banked pools (one omc global holds at most `16777216` elements, and all 612 note globals together at most 2^30 bytes, omc error 23; CONTRACT-V3 section 2): 613 note ow parameter bank A: [0, SB) of the CONTRACT section 2 order, then a one-layer window of 614 note LSTR values at og[36] and the final gain gf at og[37]; oww (lpool.om/epool.om) bank B holds 615 note layers LA .. L-1 at (l - LA) LSTR. When P <= OWC, SB = GF, LA = L and ow is the v2 layout. 616 note Every index outside the kernels is virtual (the section 2 order); owg/owp map it, and opx 617 note maps a hot index of bank A or the staged layer (og[48]). 618 note the gradient pool [0, 2P): GRD [0, P) in the platform cold store (ocsg/ocsp), BLK [P, 2P) in 619 note ogr[j] below OGC and ogb[j - OGC] from OGC (j = i - P), reached through ogrg/ogrp/ogra 620 note the Adam pool [0, 2P) (omvg/omvp): m at 0, v at P, at cold-store index P + i 621 note oa activations: layers below LAA at l ASTR, later layers in two windows at AW + (l mod 2) ASTR 622 note flushed to bank B oab at (l - LAA) ASTR after their forward and staged back before their 623 note backward; HF and the backward temporaries follow. When END <= OAC, LAA = L (v2 layout). 624 note opa attention probabilities PA[l][n][t][j] of every layer 625 note og slots: 626 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) 627 note 10 GRD (base in the gradient pool of the gradient being accumulated: 0, or P for BLK) 11 MOM 628 note (0 in the Adam pool) 12 VEL (P) 13 ACT (0 in oa) 14 ASTR (per-layer activation stride) 15 HF 629 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 630 note 29 PSTR (per-layer PA stride H T T in opa) 30 WPE 31 LAY0 32 LSTR (per-layer parameter 631 note stride) 33 GF 34 SB (first virtual index of bank B) 35 LA (first layer in bank B) 36 window base 632 note in ow 37 gf in ow 38 LAA (first layer in the activation windows) 39 AW (activation window 633 note base) 48 the layer staged in the parameter window (L: none) 634 note 40 seed S 41 step 42 TOTAL 43 lr bits 44 G (gradient blocks) 45 K (workers) 635 note 46 checkpoint magic of the run (1 OLLMCK01, 2 OLLMCK02) 636 note Per layer activations (T rows of width D unless noted): HIN, A, Q, K, U, O, HMID, BN, S1[T], 637 note S2[T], FP[T][F] (pre-relu), RL[T][F]. Every sum runs over its index ascending. 638 note Sizes at the CONTRACT-V2 maxima (V 4096, T 256, D 384, H 16, F 1536, L 8), Wolfram: 639 note P = V D + T D + L (4 D^2 + 2 D F + 2 D) + D -> `15833472` 640 note ASTR = 8 T D + 2 T + 2 T F -> `1573376`; END = L ASTR + 10 T D + T + T V + T F + 4096 -> `15016192` 641 note and at the CONTRACT-V3 maxima (L 16): P `29995392`, PA = L H T T -> `16777216`; the bank-B sizes 642 note are in lpool.om. ogeom refuses any layout past a pool. 643 note Bank A capacities: OWC parameters, OGC BLK values, OAC activations (the bank-crossing test 644 note build of tests/omegallm/build.sh shrinks all three). 645 const OWC ulong `15833472` 646 global ow f64 `15833472` 647 const OGC ulong `15833472` 648 global ogr f64 `15833472` 649 const OAC ulong `15016192` 650 global oa f64 `15016192` 651 const OPAN ulong `16777216` 652 global opa f64 `16777216` 653 654 note gradient pool element i (i < 2 P) 655 fn ogrg i ulong to f64 656 p = get og 8 657 if lt i p 658 ret ocsg i 659 end 660 j = sub i p 661 if lt j OGC 662 ret get ogr j 663 end 664 ret get ogb sub j OGC 665 end 666 667 fn ogrp i ulong v f64 to void 668 p = get og 8 669 if lt i p 670 ocsp i v 671 ret 672 end 673 j = sub i p 674 if lt j OGC 675 put ogr j v 676 else 677 put ogb sub j OGC v 678 end 679 end 680 681 note gradient pool element i += a 682 fn ogra i ulong a f64 to void 683 p = get og 8 684 if lt i p 685 ocsp i add ocsg i a 686 ret 687 end 688 j = sub i p 689 if lt j OGC 690 put ogr j add get ogr j a 691 else 692 k = sub j OGC 693 put ogb k add get ogb k a 694 end 695 end 696 697 note Adam pool element i (i < 2 P), cold-store index P + i 698 fn omvg i ulong to f64 699 ret ocsg add get og 8 i 700 end 701 702 fn omvp i ulong v f64 to void 703 ocsp add get og 8 i v 704 end 705 706 note parameter at virtual index i (bank A, bank B or gf), and its store 707 fn owg i ulong to f64 708 sb = get og 34 709 if lt i sb 710 ret get ow i 711 end 712 g = get og 33 713 if ge i g 714 ret get ow add get og 37 sub i g 715 end 716 ret get oww sub i sb 717 end 718 719 fn owp i ulong v f64 to void 720 sb = get og 34 721 if lt i sb 722 put ow i v 723 ret 724 end 725 g = get og 33 726 if ge i g 727 put ow add get og 37 sub i g v 728 ret 729 end 730 put oww sub i sb v 731 end 732 733 note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only 734 note when it is the staged layer og[48] (else exit 16, a missing ostw) 735 fn opx i ulong to ulong 736 if lt i get og 34 737 ret i 738 end 739 g = get og 33 740 if ge i g 741 ret add get og 37 sub i g 742 end 743 l = div sub i get og 31 get og 32 744 if ne l get og 48 745 exit 16 746 end 747 ret add get og 36 sub i opg1 l 748 end 749 750 note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product, 751 note 0 in the mutant. 752 const OMUTY ulong 1 753 754 const OCH ulong 999999 755 756 note stage bank-B layer l into the parameter window (layers below LA live in bank A) 757 fn ostw l ulong to void 758 la = get og 35 759 if lt l la 760 ret 761 end 762 n = get og 32 763 s = mul sub l la n 764 if eq OMUTY 0 765 s = 0 766 end 767 ostwc get og 36 s n 768 put og 48 l 769 end 770 771 note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes) 772 fn ostwc wb ulong s ulong n ulong to void 773 for q ulong ocn n 774 i = mul q OCH 775 ie = oce q n 776 while lt i ie bound 999999 777 put ow add wb i get oww add s i 778 i = add i 1 779 end 780 end 781 end 782 783 note copy layer l's activation block between its window and bank B oab (to oab when fo) 784 fn oaflip l ulong fo bool to void 785 la = get og 38 786 if lt l la 787 ret 788 end 789 n = get og 14 790 s = mul sub l la n 791 oaflc oahin l s n fo 792 end 793 794 note oab[s + i] = oa[w + i] for i < n when fo, else the reverse 795 fn oaflc w ulong s ulong n ulong fo bool to void 796 if fo 797 oafo w s n 798 else 799 oafi w s n 800 end 801 end 802 803 fn oafo w ulong s ulong n ulong to void 804 for q ulong ocn n 805 i = mul q OCH 806 ie = oce q n 807 while lt i ie bound 999999 808 put oab add s i get oa add w i 809 i = add i 1 810 end 811 end 812 end 813 814 fn oafi w ulong s ulong n ulong to void 815 for q ulong ocn n 816 i = mul q OCH 817 ie = oce q n 818 while lt i ie bound 999999 819 put oa add w i get oab add s i 820 i = add i 1 821 end 822 end 823 end 824 825 note TMP holds V logits (sample) or T attention dots 826 const OTMP ulong 4096 827 828 note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant. 829 const OMUTA ulong 1 830 831 note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or 832 note when the layout does not fit the pools. 833 fn ogeom to bool 834 if not ogok 835 ret false 836 end 837 t = get og 1 838 d = get og 2 839 put og 7 div d get og 3 840 u = mul 4 mul d d 841 u = add u mul 2 mul d get og 4 842 u = add u mul 2 d 843 put og 32 u 844 put og 30 mul get og 0 d 845 u = mul t d 846 put og 31 add get og 30 u 847 u = mul get og 5 get og 32 848 put og 33 add get og 31 u 849 u = add get og 33 d 850 put og 8 u 851 put og 9 t 852 put og 10 0 853 put og 11 0 854 put og 12 u 855 put og 13 0 856 put og 29 mul mul get og 3 t t 857 u = mul t d 858 put og 14 oastr u 859 if not ogwb 860 ret false 861 end 862 if not ogab u 863 ret false 864 end 865 p = get og 8 866 if gt p add OGC OGBN 867 ret false 868 end 869 if gt mul 3 p ocsinit 870 ret false 871 end 872 ret le mul get og 5 get og 29 OPAN 873 end 874 875 note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after 876 note them, layers LA .. L-1 in oww 877 fn ogwb to bool 878 ll = get og 5 879 g = get og 33 880 put og 34 g 881 put og 35 ll 882 put og 36 g 883 put og 37 g 884 put og 48 ll 885 if le get og 8 OWC 886 ret true 887 end 888 ls = get og 32 889 nd = add add get og 31 ls get og 2 890 if lt OWC nd 891 ret false 892 end 893 la = div sub OWC nd ls 894 if gt mul sub ll la ls OWWN 895 ret false 896 end 897 sb = add get og 31 mul la ls 898 put og 34 sb 899 put og 35 la 900 put og 36 sb 901 put og 37 add sb ls 902 ret true 903 end 904 905 note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the 906 note backward temporaries 907 fn ogab td ulong to bool 908 ll = get og 5 909 st = get og 14 910 put og 38 ll 911 put og 39 mul ll st 912 ogeom3 td mul ll st 913 ogeom2 td 914 if le get og 28 OAC 915 ret true 916 end 917 w2 = add sub get og 28 get og 15 mul 2 st 918 if lt OAC w2 919 ret false 920 end 921 la = div sub OAC w2 st 922 if gt mul sub ll la st OABN 923 ret false 924 end 925 aw = mul la st 926 put og 38 la 927 put og 39 aw 928 ogeom3 td add aw mul 2 st 929 ogeom2 td 930 ret le get og 28 OAC 931 end 932 933 fn ogeom3 td ulong hf ulong to void 934 put og 15 hf 935 put og 16 add get og 15 td 936 put og 17 add get og 16 get og 1 937 put og 18 add get og 17 td 938 end 939 940 note per-layer activation stride: 8 T D + 2 T + 2 T F 941 fn oastr td ulong to ulong 942 t = get og 1 943 a = add mul 8 td mul 2 t 944 ret add a mul mul 2 t get og 4 945 end 946 947 note shape limits (CONTRACT-V3 section 1): every shape >= 1, 256 <= V <= 4096, T <= 256, 948 note D <= 384, H <= 16, F <= 1536, L <= 16, B <= 64, D mod H = 0 949 fn ogok to bool 950 for i ulong 7 951 if eq get og i 0 952 ret false 953 end 954 end 955 if or lt get og 0 256 gt get og 0 4096 956 ret false 957 end 958 if or gt get og 1 256 gt get og 2 384 959 ret false 960 end 961 if or gt get og 3 16 gt get og 4 1536 962 ret false 963 end 964 if or gt get og 5 16 gt get og 6 64 965 ret false 966 end 967 ret eq rem get og 2 get og 3 0 968 end 969 970 fn ogeom2 td ulong to void 971 t = get og 1 972 put og 19 add get og 18 mul t get og 0 973 put og 20 add get og 19 td 974 put og 21 add get og 20 td 975 put og 22 add get og 21 td 976 put og 23 add get og 22 td 977 put og 24 add get og 23 td 978 put og 25 add get og 24 td 979 put og 26 add get og 25 td 980 put og 27 add get og 26 mul t get og 4 981 put og 28 add get og 27 OTMP 982 end 983 984 note parameter offsets of layer l 985 fn opg1 l ulong to ulong 986 ret add get og 31 mul l get og 32 987 end 988 fn opwq l ulong to ulong 989 ret add opg1 l get og 2 990 end 991 fn opwk l ulong to ulong 992 d = get og 2 993 ret add opwq l mul d d 994 end 995 fn opwv l ulong to ulong 996 d = get og 2 997 ret add opwk l mul d d 998 end 999 fn opwo l ulong to ulong 1000 d = get og 2 1001 ret add opwv l mul d d 1002 end 1003 fn opg2 l ulong to ulong 1004 d = get og 2 1005 ret add opwo l mul d d 1006 end 1007 fn opw1 l ulong to ulong 1008 ret add opg2 l get og 2 1009 end 1010 fn opw2 l ulong to ulong 1011 ret add opw1 l mul get og 2 get og 4 1012 end 1013 1014 note activation offsets of layer l (layers from LAA in the two windows at AW) 1015 fn oahin l ulong to ulong 1016 if lt l get og 38 1017 ret add get og 13 mul l get og 14 1018 end 1019 ret add get og 39 mul rem l 2 get og 14 1020 end 1021 fn oatd n ulong to ulong 1022 ret mul n mul get og 1 get og 2 1023 end 1024 fn oaa l ulong to ulong 1025 ret add oahin l oatd 1 1026 end 1027 fn oaq l ulong to ulong 1028 ret add oahin l oatd 2 1029 end 1030 fn oak l ulong to ulong 1031 ret add oahin l oatd 3 1032 end 1033 fn oau l ulong to ulong 1034 ret add oahin l oatd 4 1035 end 1036 fn oao l ulong to ulong 1037 ret add oahin l oatd 5 1038 end 1039 fn oahm l ulong to ulong 1040 ret add oahin l oatd 6 1041 end 1042 fn oabn l ulong to ulong 1043 ret add oahin l oatd 7 1044 end 1045 fn oas1 l ulong to ulong 1046 ret add oahin l oatd 8 1047 end 1048 fn oas2 l ulong to ulong 1049 ret add oas1 l get og 1 1050 end 1051 fn oafp l ulong to ulong 1052 ret add oas2 l get og 1 1053 end 1054 fn oarl l ulong to ulong 1055 ret add oafp l mul get og 1 get og 4 1056 end 1057 note output rows of layer l: the next layer's HIN, or HF after the last layer 1058 fn oaout l ulong to ulong 1059 if lt add l 1 get og 5 1060 ret oahin add l 1 1061 end 1062 ret get og 15 1063 end 1064 1065 note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at 1066 note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four 1067 note outputs run side by side in separate accumulators, so every result has v0's bits. Results of 1068 note the four-wide blocks pass through oq. 1069 global oq f64 4 1070 1071 note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv 1072 fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void 1073 wo = opx wv 1074 xe = add xo r 1075 j = ulong 0 1076 while le add j 4 c bound 999999 1077 kvmb xo xe add wo j c 1078 kput4 add yo j 1079 j = add j 4 1080 end 1081 while lt j c bound 4 1082 put oa add yo j kvm1 xo xe add wo j c 1083 j = add j 1 1084 end 1085 end 1086 1087 note oa[yo..yo+4) = oq[0..4) 1088 fn kput4 yo ulong to void 1089 put oa yo get oq 0 1090 put oa add yo 1 get oq 1 1091 put oa add yo 2 get oq 2 1092 put oa add yo 3 get oq 3 1093 end 1094 1095 note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4 1096 fn kvmb xo ulong xe ulong wi ulong c ulong to void 1097 s0 = 0.0f64 1098 s1 = 0.0f64 1099 s2 = 0.0f64 1100 s3 = 0.0f64 1101 while lt xo xe bound 999999 1102 a = get oa xo 1103 s0 = add s0 mul a get ow wi 1104 s1 = add s1 mul a get ow add wi 1 1105 s2 = add s2 mul a get ow add wi 2 1106 s3 = add s3 mul a get ow add wi 3 1107 wi = add wi c 1108 xo = add xo 1 1109 end 1110 put oq 0 s0 1111 put oq 1 s1 1112 put oq 2 s2 1113 put oq 3 s3 1114 end 1115 1116 note sum over xo <= i < xe of x[i] W[wi + (i - xo) c] 1117 fn kvm1 xo ulong xe ulong wi ulong c ulong to f64 1118 s = 0.0f64 1119 while lt xo xe bound 999999 1120 a = get oa xo 1121 s = add s mul a get ow wi 1122 wi = add wi c 1123 xo = add xo 1 1124 end 1125 ret s 1126 end 1127 1128 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 1129 note index wv 1130 fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void 1131 wo = opx wv 1132 de = add dyo c 1133 k = ulong 0 1134 while le add k 4 r bound 999999 1135 kdot4 dyo de add wo mul k c c 1136 kacc4 add yo k 1137 k = add k 4 1138 end 1139 while lt k r bound 4 1140 a = add get oa add yo k kdot1 dyo de add wo mul k c 1141 put oa add yo k a 1142 k = add k 1 1143 end 1144 end 1145 1146 note oa[yo + m] = oa[yo + m] + oq[m], m < 4 1147 fn kacc4 yo ulong to void 1148 put oa yo add get oa yo get oq 0 1149 q = add yo 1 1150 put oa q add get oa q get oq 1 1151 q = add yo 2 1152 put oa q add get oa q get oq 2 1153 q = add yo 3 1154 put oa q add get oa q get oq 3 1155 end 1156 1157 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 1158 fn kdot4 xo ulong xe ulong wi ulong c ulong to void 1159 s0 = 0.0f64 1160 s1 = 0.0f64 1161 s2 = 0.0f64 1162 s3 = 0.0f64 1163 w2 = add wi add c c 1164 while lt xo xe bound 999999 1165 a = get oa xo 1166 s0 = add s0 mul a get ow wi 1167 s1 = add s1 mul a get ow add wi c 1168 s2 = add s2 mul a get ow w2 1169 s3 = add s3 mul a get ow add w2 c 1170 wi = add wi 1 1171 w2 = add w2 1 1172 xo = add xo 1 1173 end 1174 put oq 0 s0 1175 put oq 1 s1 1176 put oq 2 s2 1177 put oq 3 s3 1178 end 1179 1180 note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow 1181 fn kdot1 xo ulong xe ulong wi ulong to f64 1182 s = 0.0f64 1183 while lt xo xe bound 999999 1184 a = get oa xo 1185 s = add s mul a get ow wi 1186 wi = add wi 1 1187 xo = add xo 1 1188 end 1189 ret s 1190 end 1191 1192 note kdot4 with both operands in oa 1193 fn kdot4a xo ulong xe ulong wi ulong c ulong to void 1194 s0 = 0.0f64 1195 s1 = 0.0f64 1196 s2 = 0.0f64 1197 s3 = 0.0f64 1198 w2 = add wi add c c 1199 while lt xo xe bound 999999 1200 a = get oa xo 1201 s0 = add s0 mul a get oa wi 1202 s1 = add s1 mul a get oa add wi c 1203 s2 = add s2 mul a get oa w2 1204 s3 = add s3 mul a get oa add w2 c 1205 wi = add wi 1 1206 w2 = add w2 1 1207 xo = add xo 1 1208 end 1209 put oq 0 s0 1210 put oq 1 s1 1211 put oq 2 s2 1212 put oq 3 s3 1213 end 1214 1215 note kdot1 with both operands in oa 1216 fn kdot1a xo ulong xe ulong wi ulong to f64 1217 s = 0.0f64 1218 while lt xo xe bound 999999 1219 a = get oa xo 1220 s = add s mul a get oa wi 1221 wi = add wi 1 1222 xo = add xo 1 1223 end 1224 ret s 1225 end 1226 1227 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 1228 fn kger xo ulong dyo ulong go ulong r ulong c ulong to void 1229 for k ulong r 1230 kger1 get oa add xo k add go mul k c dyo c 1231 end 1232 end 1233 1234 note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass) 1235 fn kger1 xv f64 gi ulong yi ulong c ulong to void 1236 ye = add yi c 1237 while le add yi 4 ye bound 999999 1238 ogra gi mul xv get oa yi 1239 ogra add gi 1 mul xv get oa add yi 1 1240 ogra add gi 2 mul xv get oa add yi 2 1241 ogra add gi 3 mul xv get oa add yi 3 1242 gi = add gi 4 1243 yi = add yi 4 1244 end 1245 while lt yi ye bound 4 1246 ogra gi mul xv get oa yi 1247 gi = add gi 1 1248 yi = add yi 1 1249 end 1250 end 1251 1252 note y[i] += a x[i]; x, y in oa 1253 fn kaxpy yo ulong xo ulong a f64 n ulong to void 1254 for i ulong n 1255 q = add yo i 1256 v = mul a get oa add xo i 1257 put oa q add get oa q v 1258 end 1259 end 1260 1261 note y[i] += a x[i]; x in oa, y in the gradient pool 1262 fn kaxpg yo ulong xo ulong a f64 n ulong to void 1263 for i ulong n 1264 ogra add yo i mul a get oa add xo i 1265 end 1266 end 1267 1268 fn ozero o ulong n ulong to void 1269 for i ulong n 1270 put oa add o i 0.0f64 1271 end 1272 end 1273 1274 fn ozerog o ulong n ulong to void 1275 for q ulong ocn n 1276 i = mul q OCH 1277 ie = oce q n 1278 while lt i ie bound 999999 1279 ogrp add o i 0.0f64 1280 i = add i 1 1281 end 1282 end 1283 end 1284 1285 note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so 1286 note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n). 1287 fn ocn n ulong to ulong 1288 ret div add n 999998 OCH 1289 end 1290 fn oce q ulong n ulong to ulong 1291 e = mul add q 1 OCH 1292 if gt e n 1293 ret n 1294 end 1295 ret e 1296 end 1297 1298 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 1299 note index gv 1300 fn frms vo ulong gv ulong yo ulong so ulong to void 1301 go = opx gv 1302 d = get og 2 1303 ss = 0.0f64 1304 for i ulong d 1305 v = get oa add vo i 1306 ss = add ss mul v v 1307 end 1308 s = fsqrt add div ss f64 d 0.00001 1309 put oa so s 1310 for j ulong d 1311 v = div get oa add vo j s 1312 v = mul v get ow add go j 1313 put oa add yo j v 1314 end 1315 end 1316 1317 note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh; 1318 note dg += da n added to the gain's gradient. go is the gain's virtual index. 1319 global ofs f64 8 1320 fn brms vo ulong go ulong so ulong dao ulong dho ulong to void 1321 d = get og 2 1322 put ofs 0 get oa so 1323 gg = add get og 10 go 1324 go = opx go 1325 put ofs 1 div brmsc vo go dao get ofs 0 f64 d 1326 for j ulong d 1327 brms1 add vo j add go j add dao j add dho j add gg j 1328 end 1329 end 1330 1331 note sum over i < D of (da[i] g[i]) (v[i]/s), ascending 1332 fn brmsc vo ulong go ulong dao ulong s f64 to f64 1333 c = 0.0f64 1334 for i ulong get og 2 1335 dn = get oa add dao i 1336 dn = mul dn get ow add go i 1337 v = div get oa add vo i s 1338 c = add c mul dn v 1339 end 1340 ret c 1341 end 1342 1343 note one element of the rms backward; s = ofs[0], c = ofs[1] 1344 fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void 1345 s = get ofs 0 1346 n = div get oa vp s 1347 da = get oa dap 1348 ogra ggp mul da n 1349 dn = mul da get ow gp 1350 dn = div sub dn mul n get ofs 1 s 1351 put oa dhp add get oa dhp dn 1352 end 1353 1354 note z[i] = a[i] + b[i] for i < n (all in oa) 1355 fn kadd3 zo ulong ao ulong bo ulong n ulong to void 1356 for i ulong n 1357 v = get oa add ao i 1358 v = add v get oa add bo i 1359 put oa add zo i v 1360 end 1361 end 1362 1363 note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa) 1364 fn kadd3w zo ulong ao ulong bo ulong n ulong to void 1365 for i ulong n 1366 v = get ow add ao i 1367 v = add v get ow add bo i 1368 put oa add zo i v 1369 end 1370 end 1371 1372 note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch 1373 fn oemb b ulong to void 1374 d = get og 2 1375 tt = get og 1 1376 for t ulong get og 9 1377 wr = get obx add mul b tt t 1378 wr = mul wr d 1379 pr = add get og 30 mul t d 1380 kadd3w add oahin 0 mul t d wr pr d 1381 end 1382 end 1383 1384 note offset in opa of PA[n][t][0] of layer l 1385 fn opar l ulong n ulong t ulong to ulong 1386 tt = get og 1 1387 p = add mul n tt t 1388 ret add mul l get og 29 mul p tt 1389 end 1390 1391 note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t 1392 fn fatts l ulong n ulong t ulong to void 1393 e = get og 7 1394 qo = add add oaq l mul t get og 2 mul n e 1395 ko = add oak l mul n e 1396 pa = opar l n t 1397 m = fattsk qo ko pa add t 1 1398 sm = osofp pa add t 1 m 1399 end 1400 1401 note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score 1402 fn fattsk qo ulong ko ulong pa ulong nj ulong to f64 1403 d = get og 2 1404 e = get og 7 1405 isq = fsqrt f64 e 1406 m = bitcast f64 ulong `18442240474082181120` 1407 qe = add qo e 1408 j = ulong 0 1409 while le add j 4 nj bound 999999 1410 kdot4a qo qe ko d 1411 m = fattp4 add pa j isq m 1412 ko = add ko mul 4 d 1413 j = add j 4 1414 end 1415 while lt j nj bound 4 1416 s = div kdot1a qo qe ko isq 1417 put opa add pa j s 1418 m = fmax m s 1419 ko = add ko d 1420 j = add j 1 1421 end 1422 ret m 1423 end 1424 1425 note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max 1426 fn fattp4 p ulong isq f64 m f64 to f64 1427 for i ulong 4 1428 s = div get oq i isq 1429 put opa add p i s 1430 m = fmax m s 1431 end 1432 ret m 1433 end 1434 1435 note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum 1436 fn osoft o ulong n ulong m f64 to f64 1437 sm = 0.0f64 1438 for j ulong n 1439 ev = fexp sub get oa add o j m 1440 put oa add o j ev 1441 sm = add sm ev 1442 end 1443 for k ulong n 1444 v = div get oa add o k sm 1445 put oa add o k v 1446 end 1447 ret sm 1448 end 1449 1450 note osoft on opa 1451 fn osofp o ulong n ulong m f64 to f64 1452 sm = 0.0f64 1453 for j ulong n 1454 ev = fexp sub get opa add o j m 1455 put opa add o j ev 1456 sm = add sm ev 1457 end 1458 for k ulong n 1459 v = div get opa add o k sm 1460 put opa add o k v 1461 end 1462 ret sm 1463 end 1464 1465 note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i] 1466 fn fatto l ulong n ulong t ulong to void 1467 e = get og 7 1468 oo = add add oao l mul t get og 2 mul n e 1469 uo = add oau l mul n e 1470 pa = opar l n t 1471 ozero oo e 1472 fattok oo uo pa add t 1 1473 end 1474 1475 note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D 1476 fn fattok oo ulong uo ulong pa ulong nj ulong to void 1477 d = get og 2 1478 e = get og 7 1479 for j ulong nj 1480 kaxpy oo uo get opa add pa j e 1481 uo = add uo d 1482 end 1483 end 1484 1485 fn olayf l ulong to void 1486 ostw l 1487 for t ulong get og 9 1488 olayf1 l t 1489 end 1490 for n ulong get og 3 1491 for t2 ulong get og 9 1492 fatts l n t2 1493 fatto l n t2 1494 end 1495 end 1496 for t3 ulong get og 9 1497 olayf2 l t3 1498 end 1499 oaflip l true 1500 end 1501 1502 note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv 1503 fn olayf1 l ulong t ulong to void 1504 d = get og 2 1505 r = mul t d 1506 a = add oaa l r 1507 frms add oahin l r opg1 l a add oas1 l t 1508 kvm a opwq l add oaq l r d d 1509 kvm a opwk l add oak l r d d 1510 kvm a opwv l add oau l r d d 1511 end 1512 1513 note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2 1514 fn olayf2 l ulong t ulong to void 1515 d = get og 2 1516 f = get og 4 1517 r = mul t d 1518 hm = add oahm l r 1519 kvm add oao l r opwo l hm d d 1520 kadd3 hm add oahin l r hm d 1521 frms hm opg2 l add oabn l r add oas2 l t 1522 fp = add oafp l mul t f 1523 kvm add oabn l r opw1 l fp d f 1524 rl = add oarl l mul t f 1525 krelu rl fp f 1526 ou = add oaout l r 1527 kvm rl opw2 l ou f d 1528 kadd3 ou hm ou d 1529 end 1530 1531 note y[j] = x[j] when x[j] > 0, else 0, for j < n 1532 fn krelu yo ulong xo ulong n ulong to void 1533 for j ulong n 1534 v = get oa add xo j 1535 if le v 0.0 1536 v = 0.0f64 1537 end 1538 put oa add yo j v 1539 end 1540 end 1541 1542 note loss of row t: the logits at o (n entries) become probabilities; returns 1543 note log(sum exp(l - max)) - (l_y - max) 1544 fn olossrow o ulong n ulong y ulong to f64 1545 m = bitcast f64 ulong `18442240474082181120` 1546 for j ulong n 1547 m = fmax m get oa add o j 1548 end 1549 ly = get oa add o y 1550 sm = osoft o n m 1551 ret sub flog sm sub ly m 1552 end 1553 1554 note final rms, tied head and loss of sequence b; returns the mean loss over CT positions 1555 fn ohead b ulong to f64 1556 ls = 0.0f64 1557 for t ulong get og 9 1558 ls = add ls oheadr b t 1559 end 1560 ret div ls f64 get og 9 1561 end 1562 1563 note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss 1564 fn oheadr b ulong t ulong to f64 1565 d = get og 2 1566 r = mul t d 1567 z = add get og 17 r 1568 hf = add get og 15 r 1569 frms hf get og 33 z add get og 16 t 1570 pr = add get og 18 mul t get og 0 1571 klogit z pr get og 0 d 1572 y = get oby add mul b get og 1 t 1573 ret olossrow pr get og 0 y 1574 end 1575 1576 note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa) 1577 fn klogit zo ulong po ulong n ulong d ulong to void 1578 ze = add zo d 1579 c = ulong 0 1580 while le add c 4 n bound 999999 1581 kdot4 zo ze mul c d d 1582 kput4 add po c 1583 c = add c 4 1584 end 1585 while lt c n bound 4 1586 put oa add po c kdot1 zo ze mul c d 1587 c = add c 1 1588 end 1589 end 1590 1591 note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but 1592 note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and 1593 note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight. 1594 global ogkm ulong 2 1595 1596 note forward pass of sequence b (x in obx, y in oby); returns its mean loss 1597 fn ofwd b ulong to f64 1598 put ogkm 1 b 1599 oemb b 1600 for l ulong get og 5 1601 olayf l 1602 end 1603 ret ohead b 1604 end 1605 1606 note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte; 1607 note DH[t] = rms backward of HF[t]. 1608 fn ohbwd b ulong t ulong sc f64 to void 1609 v = get og 0 1610 pr = add get og 18 mul t v 1611 y = get oby add mul b get og 1 t 1612 kdlog pr v y sc 1613 ohbwd2 pr t 1614 end 1615 1616 note p[c] = (p[c] - [c = y]) sc for c < n 1617 fn kdlog po ulong n ulong y ulong sc f64 to void 1618 for c ulong n 1619 g = get oa add po c 1620 if eq c y 1621 g = sub g 1.0 1622 end 1623 put oa add po c mul g sc 1624 end 1625 end 1626 1627 fn ohbwd2 pr ulong t ulong to void 1628 d = get og 2 1629 v = get og 0 1630 r = mul t d 1631 z = add get og 17 r 1632 kger pr z get og 10 v d 1633 dz = add get og 25 r 1634 kvm pr ulong 0 dz v d 1635 hf = add get og 15 r 1636 dh = add get og 19 r 1637 brms hf get og 33 add get og 16 t dz dh 1638 end 1639 1640 note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path 1641 fn offb l ulong t ulong to void 1642 f = get og 4 1643 dh = add get og 19 mul t get og 2 1644 df = add get og 26 mul t f 1645 ozero df f 1646 kvta dh opw2 l df f get og 2 1647 if eq get ogkm 0 0 1648 kdrelu df add oafp l mul t f f 1649 else 1650 ogmask df l t 1651 end 1652 g2 = add get og 10 opw2 l 1653 kger add oarl l mul t f dh g2 f get og 2 1654 offb2 l t df 1655 end 1656 1657 note y[j] = 0 where x[j] <= 0, for j < n 1658 fn kdrelu yo ulong xo ulong n ulong to void 1659 for j ulong n 1660 if le get oa add xo j 0.0 1661 put oa add yo j 0.0f64 1662 end 1663 end 1664 end 1665 1666 fn offb2 l ulong t ulong df ulong to void 1667 d = get og 2 1668 r = mul t d 1669 g1 = add get og 10 opw1 l 1670 kger add oabn l r df g1 d get og 4 1671 da = add get og 20 r 1672 ozero da d 1673 kvta df opw1 l da d get og 4 1674 hm = add oahm l r 1675 dh = add get og 19 r 1676 brms hm opg2 l add oas2 l t da dh 1677 end 1678 1679 note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j] 1680 fn oattd l ulong n ulong t ulong to f64 1681 e = get og 7 1682 dor = add add get og 24 mul t get og 2 mul n e 1683 uo = add oau l mul n e 1684 pa = opar l n t 1685 ret kattd dor uo pa add t 1 1686 end 1687 1688 fn kattd dor ulong uo ulong pa ulong nj ulong to f64 1689 d = get og 2 1690 de = add dor get og 7 1691 dt = 0.0f64 1692 j = ulong 0 1693 while le add j 4 nj bound 999999 1694 kdot4a dor de uo d 1695 dt = kattd4 add pa j add get og 27 j dt 1696 uo = add uo mul 4 d 1697 j = add j 4 1698 end 1699 while lt j nj bound 4 1700 dp = kdot1a dor de uo 1701 put oa add get og 27 j dp 1702 a = get opa add pa j 1703 dt = add dt mul a dp 1704 uo = add uo d 1705 j = add j 1 1706 end 1707 ret dt 1708 end 1709 1710 note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order 1711 fn kattd4 p ulong tm ulong dt f64 to f64 1712 for i ulong 4 1713 dp = get oq i 1714 put oa add tm i dp 1715 a = get opa add p i 1716 dt = add dt mul a dp 1717 end 1718 ret dt 1719 end 1720 1721 note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied) 1722 global oz ulong 8 1723 1724 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] 1725 fn oatta l ulong n ulong t ulong dt f64 to void 1726 e = get og 7 1727 hn = mul n e 1728 r = mul t get og 2 1729 put ofs 2 dt 1730 put oz 0 add add oaq l r hn 1731 put oz 1 add add get og 21 r hn 1732 put oz 2 add add get og 24 r hn 1733 put oz 3 add oak l hn 1734 put oz 4 add get og 22 hn 1735 put oz 5 add get og 23 hn 1736 pa = opar l n t 1737 oatta2 pa add t 1 1738 end 1739 1740 fn oatta2 pa ulong nj ulong to void 1741 d = get og 2 1742 e = get og 7 1743 for j ulong nj 1744 jr = mul j d 1745 ds = oads pa j 1746 kaxpy get oz 1 add get oz 3 jr ds e 1747 if eq OMUTA 1 1748 kaxpy add get oz 4 jr get oz 0 ds e 1749 else 1750 kaxpy add get oz 4 jr get oz 0 neg ds e 1751 end 1752 pj = get opa add pa j 1753 kaxpy add get oz 5 jr get oz 2 pj e 1754 end 1755 end 1756 1757 note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2 1758 fn oads pa ulong j ulong to f64 1759 pj = get opa add pa j 1760 u = sub get oa add get og 27 j get ofs 2 1761 ret div mul pj u fsqrt f64 get og 7 1762 end 1763 1764 fn olayb l ulong to void 1765 ostw l 1766 oaflip l false 1767 ct = get og 9 1768 for t ulong ct 1769 offb l t 1770 end 1771 for t2 ulong ct 1772 olaybo l t2 1773 end 1774 d = get og 2 1775 ozero get og 21 mul ct d 1776 ozero get og 22 mul ct d 1777 ozero get og 23 mul ct d 1778 for n ulong get og 3 1779 for t3 ulong ct 1780 dt = oattd l n t3 1781 oatta l n t3 dt 1782 end 1783 end 1784 for t4 ulong ct 1785 olayb2 l t4 1786 end 1787 end 1788 1789 note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t] 1790 fn olaybo l ulong t ulong to void 1791 d = get og 2 1792 r = mul t d 1793 dor = add get og 24 r 1794 dh = add get og 19 r 1795 ozero dor d 1796 kvta dh opwo l dor d d 1797 kger add oao l r dh add get og 10 opwo l d d 1798 end 1799 1800 note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path 1801 fn olayb2 l ulong t ulong to void 1802 d = get og 2 1803 r = mul t d 1804 da = add get og 20 r 1805 ozero da d 1806 kvta add get og 21 r opwq l da d d 1807 kvta add get og 22 r opwk l da d d 1808 kvta add get og 23 r opwv l da d d 1809 olayb3 l t 1810 end 1811 1812 fn olayb3 l ulong t ulong to void 1813 d = get og 2 1814 r = mul t d 1815 a = add oaa l r 1816 g0 = get og 10 1817 kger a add get og 21 r add g0 opwq l d d 1818 kger a add get og 22 r add g0 opwk l d d 1819 kger a add get og 23 r add g0 opwv l d d 1820 hi = add oahin l r 1821 dh = add get og 19 r 1822 brms hi opg1 l add oas1 l t add get og 20 r dh 1823 end 1824 1825 note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of 1826 note the step loss. Gradients accumulate into the gradient at og[10] in ogr. 1827 fn obwd b ulong sc f64 to void 1828 ct = get og 9 1829 ozero get og 19 mul ct get og 2 1830 for t ulong ct 1831 ohbwd b t sc 1832 end 1833 ll = get og 5 1834 for i ulong ll 1835 olayb sub sub ll 1 i 1836 end 1837 for t2 ulong ct 1838 obwde b t2 1839 end 1840 end 1841 1842 note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t] 1843 fn obwde b ulong t ulong to void 1844 d = get og 2 1845 c = get obx add mul b get og 1 t 1846 dh = add get og 19 mul t d 1847 g0 = get og 10 1848 kaxpg add g0 mul c d dh 1.0 d 1849 kaxpg add add g0 get og 30 mul t d dh 1.0 d 1850 end 1851 note SPDX-License-Identifier: AGPL-3.0-only 1852 note omegallm adam.om (aien-train#52): init, global-norm clip, Adam with bias correction, the LR 1853 note schedule, gradient blocks (CONTRACT-V1 section 5) and one training step (CONTRACT section 5). 1854 note Needs math.om, data.om, model.om. 1855 note Exact operation order (agreed with the oracle lane, recorded in CONTRACT section 5): 1856 note n = sqrt(sum_i g_i^2) (i ascending); if n > 1: c = 1/n, g_i = g_i c. 1857 note b^(s+1) by s+1 repeated multiplications starting from 1.0; bc = 1 - b^(s+1). 1858 note m = 0.9 m + (1.0 - 0.9) g; v = 0.99 v + (1.0 - 0.99)(g g); 1859 note p = p - (lr_s (m/bc1)) / (sqrt(v/bc2) + 1e-8). 1860 note lr_s = (lr (s+1))/100 for s < 100, else lr (1 - (0.9 (s-100))/max(1, TOTAL-100)). 1861 1862 note Mutant c (bias correction removed): 1 in the product, 0 in the mutant. 1863 const OMUTC ulong 1 1864 1865 note Mutant d (gradient blocks combined in reverse order, G-1 first): 1 in the product, 0 in the 1866 note mutant. 1867 const OMUTD ulong 1 1868 1869 note per-sequence losses of the current batch 1870 global olos f64 64 1871 1872 note init (CONTRACT section 5): r_i = (splitmix64(S+i) >> 11) 2^-53 (N[2^-53] -> 1873 note 1.`1102230246251565`*^-16); w = (2 r - 1) 0.`034641016151377546` (N[0.02 Sqrt[3]]); wo and w2 1874 note times 1/sqrt(2L); gains 1; m = v = 0. 1875 fn oinit to void 1876 p = get og 8 1877 sd = get og 40 1878 for q ulong ocn p 1879 i = mul q OCH 1880 ie = oce q p 1881 while lt i ie bound 999999 1882 r = mul f64 shr smx add sd i 11 1.`1102230246251565e`-16 1883 owp i mul sub add r r 1.0 0.`034641016151377546` 1884 i = add i 1 1885 end 1886 end 1887 d = get og 2 1888 ri = div 1.0f64 fsqrt f64 mul 2 get og 5 1889 for l ulong get og 5 1890 oiset opg1 l d 1.0 1891 oiset opg2 l d 1.0 1892 oiscale opwo l mul d d ri 1893 oiscale opw2 l mul d get og 4 ri 1894 end 1895 oiset get og 33 d 1.0 1896 ozerom mul 2 p 1897 end 1898 1899 fn ozerom n ulong to void 1900 for q ulong ocn n 1901 i = mul q OCH 1902 ie = oce q n 1903 while lt i ie bound 999999 1904 omvp i 0.0f64 1905 i = add i 1 1906 end 1907 end 1908 end 1909 1910 fn oiset o ulong n ulong v f64 to void 1911 for i ulong n 1912 owp add o i v 1913 end 1914 end 1915 1916 fn oiscale o ulong n ulong v f64 to void 1917 for i ulong n 1918 owp add o i mul owg add o i v 1919 end 1920 end 1921 1922 fn olrs s ulong lr f64 to f64 1923 if lt s 100 1924 ret div mul lr f64 add s 1 100.0 1925 end 1926 den = ulong 1 1927 if gt get og 42 101 1928 den = sub get og 42 100 1929 end 1930 ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den 1931 end 1932 1933 fn opowb b f64 s ulong to f64 1934 pw = 1.0f64 1935 for i ulong add s 1 1936 pw = mul pw b 1937 end 1938 ret pw 1939 end 1940 1941 note clip the gradient to global norm 1 and apply one Adam update at step og[41] 1942 fn oadam to void 1943 p = get og 8 1944 ss = 0.0f64 1945 for q ulong ocn p 1946 i = mul q OCH 1947 ie = oce q p 1948 while lt i ie bound 999999 1949 g = ocsg i 1950 ss = add ss mul g g 1951 i = add i 1 1952 end 1953 end 1954 nr = fsqrt ss 1955 if gt nr 1.0 1956 oclip div 1.0 nr 1957 end 1958 s = get og 41 1959 lrs = olrs s bitcast f64 get og 43 1960 bc1 = sub 1.0 opowb 0.9 s 1961 bc2 = sub 1.0 opowb 0.99 s 1962 if eq OMUTC 0 1963 bc1 = 1.0f64 1964 bc2 = 1.0f64 1965 end 1966 put ofs 3 lrs 1967 put ofs 4 bc1 1968 put ofs 5 bc2 1969 for q2 ulong ocn p 1970 k = mul q2 OCH 1971 ke = oce q2 p 1972 while lt k ke bound 999999 1973 oadam1 k 1974 k = add k 1 1975 end 1976 end 1977 end 1978 1979 note g_i = g_i c for i < P 1980 fn oclip c f64 to void 1981 p = get og 8 1982 for q ulong ocn p 1983 j = mul q OCH 1984 je = oce q p 1985 while lt j je bound 999999 1986 ocsp j mul ocsg j c 1987 j = add j 1 1988 end 1989 end 1990 end 1991 1992 note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2 1993 fn oadam1 i ulong to void 1994 g = ogrg add get og 10 i 1995 mo = add get og 11 i 1996 m = omvg mo 1997 m = mul 0.9 m 1998 a = sub 1.0f64 0.9 1999 m = add m mul a g 2000 omvp mo m 2001 vo = add get og 12 i 2002 v = omvg vo 2003 v = mul 0.99 v 2004 a = sub 1.0f64 0.99 2005 v = add v mul a mul g g 2006 omvp vo v 2007 oadam2 i m v 2008 end 2009 2010 note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5 2011 fn oadam2 i ulong m f64 v f64 to void 2012 u = div m get ofs 4 2013 u = mul get ofs 3 u 2014 w = div v get ofs 5 2015 w = add fsqrt w 0.`00000001` 2016 u = div u w 2017 w = owg i 2018 owp i sub w u 2019 end 2020 2021 note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update 2022 note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers. 2023 fn ostep to f64 2024 s = get og 41 2025 obatch s 2026 ls = ogradw 2027 oadam 2028 put og 41 add s 1 2029 ret ls 2030 end 2031 2032 note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss. 2033 note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from 2034 note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending. 2035 note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2) 2036 note and block 0 copied into GRD, the same bits as accumulating it in GRD. 2037 fn ograd to f64 2038 gg = get og 44 2039 for i ulong gg 2040 j = i 2041 if eq OMUTD 0 2042 j = sub sub gg 1 i 2043 end 2044 oblkc j 2045 ogfold eq i 0 2046 end 2047 ret olsum 2048 end 2049 2050 note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos 2051 fn oblkc j ulong to void 2052 p = get og 8 2053 put og 10 p 2054 ozerog p p 2055 bb = get og 6 2056 sc = div 1.0f64 f64 mul get og 1 bb 2057 r = div bb get og 44 2058 b = mul j r 2059 for i ulong r 2060 put olos b ofwd b 2061 obwd b sc 2062 b = add b 1 2063 end 2064 put og 10 0 2065 end 2066 2067 note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd) 2068 fn ogfold first bool to void 2069 if not first 2070 ogadd 2071 ret 2072 end 2073 p = get og 8 2074 for q ulong ocn p 2075 i = mul q OCH 2076 ie = oce q p 2077 while lt i ie bound 999999 2078 ocsp i ogrg add p i 2079 i = add i 1 2080 end 2081 end 2082 end 2083 2084 note GRD[i] = GRD[i] + BLK[i] for i ascending 2085 fn ogadd to void 2086 p = get og 8 2087 for q ulong ocn p 2088 i = mul q OCH 2089 ie = oce q p 2090 while lt i ie bound 999999 2091 ogadd1 i p 2092 i = add i 1 2093 end 2094 end 2095 end 2096 2097 note GRD[i] += BLK[i] (BLK at P) 2098 fn ogadd1 i ulong p ulong to void 2099 x = ogrg add p i 2100 ocsp i add ocsg i x 2101 end 2102 2103 note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B 2104 fn olsum to f64 2105 ls = 0.0f64 2106 bb = get og 6 2107 for b ulong bb 2108 ls = add ls get olos b 2109 end 2110 ret div ls f64 bb 2111 end 2112 2113 note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments, 2114 note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps 2115 note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when 2116 note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2 2117 note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing". 2118 fn cmdadamck to void 2119 put og 0 256 2120 put og 1 8 2121 put og 2 16 2122 put og 3 2 2123 put og 4 32 2124 put og 5 2 2125 put og 6 2 2126 if not ogeom 2127 exit 12 2128 end 2129 put og 42 200 2130 put og 43 bitcast ulong 0.001f64 2131 put og 41 0 2132 p = get og 8 2133 for i ulong p 2134 x = sub f64 rem i 7 3.0 2135 x = mul x 0.0001 2136 ogrp add get og 10 i x 2137 end 2138 for k ulong 5 2139 oadam 2140 put og 41 add get og 41 1 2141 end 2142 mx = 0.0f64 2143 for j ulong p 2144 mx = fmax mx oadamex j 2145 end 2146 oputc 65 2147 oputc 32 2148 oputu 5 2149 oputc 32 2150 oputx bitcast ulong mx 2151 oputc 32 2152 oputl long mul mx `1000000000000000`.0 2153 oputc 10 2154 oflush 2155 if gt mx 0.`000000000001` 2156 exit 41 2157 end 2158 end 2159 2160 fn oadamex j ulong to f64 2161 g = ogrg add get og 10 j 2162 w = 0.0f64 2163 for s ulong 5 2164 w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.`00000001` 2165 end 2166 d = fabs sub owg j w 2167 if eq w 0.0 2168 ret d 2169 end 2170 ret div d fabs w 2171 end 2172 note SPDX-License-Identifier: AGPL-3.0-only 2173 note omegallm ckpt.om (aien-train#52): checkpoint OLLMCK01 / OLLMCK02 and gradient OLLMGR01 files 2174 note (CONTRACT section 6, CONTRACT-V1 section 6). Needs math.om, data.om, model.om, adam.om before it. 2175 note Files are streamed: a save encodes the f64 payload little-endian in chunks of OCKC values into 2176 note ock, hashes each chunk (FNV-1a 64) and writes it at its file position through the platform 2177 note unit (ocwopen, ocwput, ocwend; lio.om writes PATH.tmp, fsync, rename; eio.om the file in 2178 note place), then writes the header at position 0. A load reads the header and then the payload in 2179 note chunks through oprd into opw, hashing and decoding into the pools in one pass. 2180 note CK01 header 112 bytes (FNV at 104); CK02 120 bytes (G at 104, FNV at 112); GR01 112 bytes, step 2181 note field = B, one payload. Largest file at the v3 maxima: 120 + 24 P with P = `29995392` (Wolfram: 2182 note 120+24*`29995392` -> `719889528`); a file over OCKCAP = `719889529` bytes is refused with 15 before its 2183 note header is read, as v0 refused a file over its image (exactly OCKCAP bytes goes to the header 2184 note checks, then 15). 2185 note Magic as a little-endian u64 (Wolfram FromDigits[Reverse[ToCharacterCode["OLLMCK01"]],256] 2186 note -> `3544415659172318287`; "OLLMGR01" -> `3544423372933581903`; "OLLMCK02" -> `3616473253210246223`). 2187 note Load refusals (exit codes): 10 file shorter than its header or than header + 24 P, 11 bad magic, 2188 note 12 a shape zero or over the maximum, V < 256, D mod H != 0, TOTAL > 999999, or (CK02) G = 0, 2189 note G > B or B mod G != 0, 13 P disagrees with the shapes, 14 FNV-1a mismatch, 15 file longer than 2190 note header + 24 P; 70 I/O. 2191 const OCKCAP ulong `719889529` 2192 const OCKC ulong 65536 2193 global ock uchar 524288 2194 2195 fn ocput8 o ulong v ulong to void 2196 for k ulong 8 2197 put ock add o k uchar and shr v mul k 8 255 2198 end 2199 end 2200 2201 2202 note little-endian u64 at ock[o .. o + 8) 2203 fn ocget8 o ulong to ulong 2204 v = ulong 0 2205 for k ulong 8 2206 v = or v shl ulong get ock add o k mul k 8 2207 end 2208 ret v 2209 end 2210 2211 note decode m values from ock into gradient pool [o .. o + m) (a worker's gradient block, lio.om) 2212 fn ocdecb o ulong m ulong to void 2213 for j ulong m 2214 ogrp add o j bitcast f64 ocget8 mul j 8 2215 end 2216 end 2217 note little-endian u64 at opw[o .. o + 8) 2218 fn opw8 o ulong to ulong 2219 v = ulong 0 2220 for k ulong 8 2221 v = or v shl ulong get opw add o k mul k 8 2222 end 2223 ret v 2224 end 2225 2226 note FNV-1a 64 step over ock[0 .. n) from state h (basis `cbf29ce484222325`, prime `100000001b3`) 2227 fn ocfnvk h ulong n ulong to ulong 2228 x = h 2229 for i ulong n 2230 x = mul xor x ulong get ock i `1099511628211` 2231 end 2232 ret x 2233 end 2234 2235 fn ocfnvw h ulong n ulong to ulong 2236 x = h 2237 for i ulong n 2238 x = mul xor x ulong get opw i `1099511628211` 2239 end 2240 ret x 2241 end 2242 2243 note header fields from og into ock[0 .. 104); magic mg, step field st 2244 fn ochead mg ulong st ulong to void 2245 ocput8 0 mg 2246 for i ulong 7 2247 ocput8 add 8 mul i 8 get og i 2248 end 2249 ocput8 64 get og 40 2250 ocput8 72 st 2251 ocput8 80 get og 42 2252 ocput8 88 get og 43 2253 ocput8 96 get og 8 2254 end 2255 2256 note encode m values of pool k (0 params, 1 m, 2 v, 3 GRD, 4 BLK) from value index i into ock 2257 fn ocenc k ulong i ulong m ulong to void 2258 if eq k 0 2259 for j ulong m 2260 ocput8 mul j 8 bitcast ulong owg add i j 2261 end 2262 ret 2263 end 2264 if ge k 3 2265 o = add get og 10 i 2266 if eq k 4 2267 o = add get og 8 i 2268 end 2269 for j ulong m 2270 ocput8 mul j 8 bitcast ulong ogrg add o j 2271 end 2272 ret 2273 end 2274 o = add get og add 10 k i 2275 for j ulong m 2276 ocput8 mul j 8 bitcast ulong omvg add o j 2277 end 2278 end 2279 2280 note decode m values from opw into pool k (0 params, 1 m, 2 v) from value index i 2281 fn ocdec k ulong i ulong m ulong to void 2282 if eq k 0 2283 for j ulong m 2284 owp add i j bitcast f64 opw8 mul j 8 2285 end 2286 ret 2287 end 2288 o = add get og add 10 k i 2289 for j ulong m 2290 omvp add o j bitcast f64 opw8 mul j 8 2291 end 2292 end 2293 2294 note stream pools ks .. ke-1 (ocenc numbering) after a header of hl bytes; returns the FNV-1a 2295 fn ocbody ks ulong ke ulong hl ulong to ulong 2296 p = get og 8 2297 h = ulong `14695981039346656037` 2298 pos = hl 2299 nc = div add p sub OCKC 1 OCKC 2300 for k ulong sub ke ks 2301 for c ulong nc 2302 i = mul c OCKC 2303 m = sub p i 2304 if gt m OCKC 2305 m = OCKC 2306 end 2307 ocenc add ks k i m 2308 nb = mul m 8 2309 h = ocfnvk h nb 2310 ocwput pos nb 2311 pos = add pos nb 2312 end 2313 end 2314 ret h 2315 end 2316 2317 note write checkpoint (params, m, v) to argv[ai] with the loaded or initialised magic (og 46) 2318 fn ocksave ai ulong to void 2319 ocwopen ai 2320 hl = ulong 112 2321 if eq get og 46 2 2322 hl = 120 2323 end 2324 h = ocbody 0 3 hl 2325 if eq hl 120 2326 ochead `3616473253210246223` get og 41 2327 ocput8 104 get og 44 2328 ocput8 112 h 2329 else 2330 ochead `3544415659172318287` get og 41 2331 ocput8 104 h 2332 end 2333 ocwput 0 hl 2334 ocwend ai 2335 end 2336 2337 note write gradient file GR01: header with step = B, payload GRD (for either checkpoint magic) 2338 fn ogrsave ai ulong to void 2339 ocwopen ai 2340 h = ocbody 3 4 112 2341 ochead `3544423372933581903` get og 6 2342 ocput8 104 h 2343 ocwput 0 112 2344 ocwend ai 2345 end 2346 2347 note load the checkpoint named by argv[ai] into og, ow (params) and omv (m, v) 2348 fn ockload ai ulong to void 2349 n = ofsz ai 2350 if gt n OCKCAP 2351 exit 15 2352 end 2353 hn = oprd ai 0 0 120 2354 if lt n 112 2355 exit 10 2356 end 2357 mg = opw8 0 2358 hl = ulong 112 2359 if eq mg `3616473253210246223` 2360 hl = 120 2361 put og 46 2 2362 else 2363 if ne mg `3544415659172318287` 2364 exit 11 2365 end 2366 put og 46 1 2367 end 2368 if lt n hl 2369 exit 10 2370 end 2371 for i ulong 7 2372 put og i opw8 add 8 mul i 8 2373 end 2374 if not ogeom 2375 exit 12 2376 end 2377 put og 40 opw8 64 2378 put og 41 opw8 72 2379 put og 42 opw8 80 2380 put og 43 opw8 88 2381 if gt get og 42 999999 2382 exit 12 2383 end 2384 g = ulong 1 2385 if eq hl 120 2386 g = opw8 104 2387 end 2388 if not ogok2 g 2389 exit 12 2390 end 2391 put og 44 g 2392 put og 45 1 2393 p = get og 8 2394 if ne opw8 96 p 2395 exit 13 2396 end 2397 hs = opw8 sub hl 8 2398 pb = mul 24 p 2399 if lt n add hl pb 2400 exit 10 2401 end 2402 if gt n add hl pb 2403 exit 15 2404 end 2405 if ne ocklp ai hl p hs 1 2406 exit 14 2407 end 2408 end 2409 2410 note one pass over the payload at hl: read, hash and decode each chunk; exit 70 on a short read. 2411 note Returns 1 when the payload FNV equals hs, else 0. 2412 fn ocklp ai ulong hl ulong p ulong hs ulong to ulong 2413 h = ulong `14695981039346656037` 2414 nc = div add p sub OCKC 1 OCKC 2415 pos = hl 2416 for k ulong 3 2417 for c ulong nc 2418 i = mul c OCKC 2419 m = sub p i 2420 if gt m OCKC 2421 m = OCKC 2422 end 2423 nb = mul m 8 2424 if ne oprd ai pos 0 nb nb 2425 exit 70 2426 end 2427 h = ocfnvw h nb 2428 ocdec k i m 2429 pos = add pos nb 2430 end 2431 end 2432 if eq h hs 2433 ret 1 2434 end 2435 ret 0 2436 end 2437 2438 note G against the loaded shapes: 1 <= G <= B and B mod G = 0 2439 fn ogok2 g ulong to bool 2440 b = get og 6 2441 if or eq g 0 gt g b 2442 ret false 2443 end 2444 ret eq rem b g 0 2445 end 2446 note SPDX-License-Identifier: AGPL-3.0-only 2447 note omegallm bpe.om (aien-train#52, CONTRACT-V1 sections 2, 7 and 8): the byte-pair tokenizer. 2448 note Text is read as a stream through the opw window of data.om (opstart, opshift) and cut into 2449 note chunks (2.1). bpefit counts chunk types, then merges with a hashed pair table, per-pair lists 2450 note of the types that hold the pair and a lazy max-heap keyed count << 24 | (4095 - a) << 12 | 2451 note (4095 - b): the largest key is the largest count, then the smallest a, then the smallest b. 2452 note A pair's count only falls once its newest token exists, so a popped key whose count is still 2453 note current is the true maximum; a stale one is pushed again with its current count (if any). 2454 note Encode applies, per chunk, the present merge of lowest rank until none is left. That equals 2455 note merges 0, 1, ... in order (2.4): merge k's pair never reappears after merge k, since a later 2456 note merge only makes pairs that hold its own new token. Chunk types cache their tokens. 2457 note Exits: 2 usage (bpefit V outside 257..4096); 21 a capacity below, an odd token file, or 2458 note MAXBYTES with no byte 0 inside; 25 a token >= V; 26 a bad merges file; 27 V above the file's 2459 note V, or V <= 256; 28 no pair left to merge; 70 I/O. 2460 note Interface for the model units (section 8): obload, obtlen, obtput, obprompt and obtk. 2461 note Capacities: 524288 chunk types, `4194304` pool symbols (2 per byte of type), 900000 pairs, 2462 note `4194304` type-in-pair list nodes. 2463 const BQTCAP ulong 524288 2464 const BQHMSK ulong `1048575` 2465 const BQSCAP ulong `4194304` 2466 const BQPCAP ulong 900000 2467 const BQPMSK ulong `1048575` 2468 const BQNCAP ulong `4194304` 2469 const BQLCAP ulong `1099511627776` 2470 global obtk ulong 4096 2471 global obm uchar 15384 2472 global bqma ushort 4096 2473 global bqmb ushort 4096 2474 global bqln ulong 4096 2475 global bqrk uint 8192 2476 global bqrt ushort 8192 2477 global bqv ulong 2 2478 global bqcl uchar 256 2479 global bqtm ushort 128 2480 global bqsk ushort 4100 2481 global bqst ulong 16 2482 global bqhs uint `1048576` 2483 global bqto uint 524288 2484 global bqtl uchar 524288 2485 global bqtn uchar 524288 2486 global bqtc ulong 524288 2487 global bqts uint 524288 2488 global bqsy ushort `4194304` 2489 global bqpk uint `1048576` 2490 global bqpc ulong `1048576` 2491 global bqph uint `1048576` 2492 global bqpl uint `1048576` 2493 global bqnl uint `1048576` 2494 global bqnt uint `4194304` 2495 global bqnx uint `4194304` 2496 global bqhp ulong `1048576` 2497 2498 note bqst: 0 types, 1 pool symbols used, 2 chunks, 3 bytes read, 4 tokens, 5 pairs, 6 new pairs 2499 note of this merge (bqnl), 7 list nodes, 8 heap entries. 2500 2501 note class table: bqcl[c] = 1 when A(c) (section 2) 2502 fn bpcls to void 2503 for c ulong 256 2504 v = uchar 0 2505 if or or and ge c 48 le c 57 and ge c 65 le c 90 or and ge c 97 le c 122 ge c 128 2506 v = uchar 1 2507 end 2508 put bqcl c v 2509 end 2510 end 2511 2512 note length of the chunk at opw[i] (section 2.1, pieces of at most 64) in a text ending at n > i 2513 fn bpclen i ulong n ulong to ulong 2514 c = ulong get opw i 2515 j = add i 1 2516 if eq get bqcl c 0 2517 if ne c 32 2518 ret 1 2519 end 2520 if ge j n 2521 ret 1 2522 end 2523 if eq get bqcl get opw j 0 2524 ret 1 2525 end 2526 j = add j 1 2527 end 2528 e = add i 64 2529 if lt n e 2530 e = n 2531 end 2532 while lt j e bound 64 2533 if eq get bqcl get opw j 0 2534 break 2535 end 2536 j = add j 1 2537 end 2538 ret sub j i 2539 end 2540 2541 note as bpclen, over the argument bytes oarg (obprompt) 2542 fn bpclena i ulong n ulong to ulong 2543 c = ulong get oarg i 2544 j = add i 1 2545 if eq get bqcl c 0 2546 if ne c 32 2547 ret 1 2548 end 2549 if ge j n 2550 ret 1 2551 end 2552 if eq get bqcl get oarg j 0 2553 ret 1 2554 end 2555 j = add j 1 2556 end 2557 e = add i 64 2558 if lt n e 2559 e = n 2560 end 2561 while lt j e bound 64 2562 if eq get bqcl get oarg j 0 2563 break 2564 end 2565 j = add j 1 2566 end 2567 ret sub j i 2568 end 2569 2570 note FNV-1a 64 of opw[i .. i + l) 2571 fn bphash i ulong l ulong to ulong 2572 h = ulong `14695981039346656037` 2573 for k ulong l 2574 h = mul xor h ulong get opw add i k `1099511628211` 2575 end 2576 ret h 2577 end 2578 2579 note does type y hold the bytes opw[i .. i + l)? 2580 fn bpsame y ulong i ulong l ulong to bool 2581 if ne ulong get bqtl y l 2582 ret false 2583 end 2584 o = ulong get bqto y 2585 for k ulong l 2586 if ne ulong get bqsy add o k ulong get opw add i k 2587 ret false 2588 end 2589 end 2590 ret true 2591 end 2592 2593 note new type in empty slot s for opw[i .. i + l): its bytes become symbols at bqsy[o], 2 l pool 2594 note symbols are reserved (encode stores the tokens after the bytes); BQTCAP when full 2595 fn bpins s ulong i ulong l ulong to ulong 2596 nt = get bqst 0 2597 o = get bqst 1 2598 if ge nt BQTCAP 2599 ret BQTCAP 2600 end 2601 if gt add o mul 2 l BQSCAP 2602 ret BQTCAP 2603 end 2604 put bqhs s uint add nt 1 2605 put bqto nt uint o 2606 put bqtl nt uchar l 2607 put bqtn nt uchar l 2608 put bqtc nt 0 2609 put bqts nt 0 2610 for k ulong l 2611 put bqsy add o k ushort get opw add i k 2612 end 2613 put bqst 0 add nt 1 2614 put bqst 1 add o l 2615 ret nt 2616 end 2617 2618 note type id of the chunk opw[i .. i + l), added when absent; BQTCAP when absent and full 2619 fn bpfind i ulong l ulong to ulong 2620 s = and bphash i l BQHMSK 2621 for q ulong 524289 2622 y = ulong get bqhs s 2623 if eq y 0 2624 ret bpins s i l 2625 end 2626 y = sub y 1 2627 if bpsame y i l 2628 ret y 2629 end 2630 s = and add s 1 BQHMSK 2631 end 2632 ret BQTCAP 2633 end 2634 2635 note fit: count one chunk 2636 fn bpcount i ulong l ulong to void 2637 y = bpfind i l 2638 if eq y BQTCAP 2639 exit 21 2640 end 2641 put bqtc y add get bqtc y 1 2642 put bqst 2 add get bqst 2 1 2643 end 2644 2645 note append token t to output 0 as u16 LE 2646 fn bpwtok t ulong to void 2647 owbyte 0 and t 255 2648 owbyte 0 shr t 8 2649 end 2650 2651 note rank of pair (a, b): its merge token, or 65535 when it is not a merge below V 2652 fn bprank a ulong b ulong to ulong 2653 key = add mul a 4096 b 2654 s = shr mul add key 1 ulong `11400714819323198485` 51 2655 for q ulong 8192 2656 k = ulong get bqrk s 2657 if eq k 0 2658 ret 65535 2659 end 2660 if eq k add key 1 2661 ret ulong get bqrt s 2662 end 2663 s = and add s 1 8191 2664 end 2665 ret 65535 2666 end 2667 2668 note replace in bqtm[0 .. m) every (a, b) of merge token t, left to right without overlap 2669 fn bprepl m ulong t ulong to ulong 2670 a = get bqma t 2671 b = get bqmb t 2672 w = ulong 0 2673 j = ulong 0 2674 while lt j m bound 128 2675 x = get bqtm j 2676 z = ushort 65535 2677 if lt add j 1 m 2678 z = get bqtm add j 1 2679 end 2680 if and eq x a eq z b 2681 put bqtm w ushort t 2682 j = add j 2 2683 else 2684 put bqtm w x 2685 j = add j 1 2686 end 2687 w = add w 1 2688 end 2689 ret w 2690 end 2691 2692 note encode bqtm[0 .. n) (bytes) in place (section 2.4); returns the token count 2693 fn bpenct n ulong to ulong 2694 m = n 2695 dn = false 2696 while and not dn gt m 1 bound 64 2697 best = ulong 65535 2698 for j ulong sub m 1 2699 r = bprank ulong get bqtm j ulong get bqtm add j 1 2700 if lt r best 2701 best = r 2702 end 2703 end 2704 if eq best 65535 2705 dn = true 2706 else 2707 m = bprepl m best 2708 end 2709 end 2710 ret m 2711 end 2712 2713 note encode: one chunk opw[i .. i + l) to output 0, through the type cache when it has room 2714 fn bpenc1 i ulong l ulong to void 2715 put bqst 2 add get bqst 2 1 2716 y = bpfind i l 2717 if eq y BQTCAP 2718 for k ulong l 2719 put bqtm k ushort get opw add i k 2720 end 2721 m = bpenct l 2722 for k ulong m 2723 bpwtok ulong get bqtm k 2724 end 2725 put bqst 4 add get bqst 4 m 2726 else 2727 o = add ulong get bqto y l 2728 if eq get bqtc y 0 2729 for k ulong l 2730 put bqtm k ushort get opw add i k 2731 end 2732 mt = bpenct l 2733 for k ulong mt 2734 put bqsy add o k get bqtm k 2735 end 2736 put bqtn y uchar mt 2737 put bqtc y 1 2738 put bqst 1 add get bqst 1 mt 2739 end 2740 m = ulong get bqtn y 2741 for k ulong m 2742 bpwtok ulong get bqsy add o k 2743 end 2744 put bqst 4 add get bqst 4 m 2745 end 2746 end 2747 2748 note the chunks of the first lim bytes of argument ai (all of it when shorter) through bpcount 2749 note (mode 0) or bpenc1 (mode 1, output 0 opened on argument bo once the text is open); bqst 3 = 2750 note bytes read. A chunk needs at most 65 bytes past its start, so the window moves to the 2751 note current chunk when fewer are left and the file goes on. 2752 note window position of i after a refill: when fewer than need bytes are left from opw[i] and the 2753 note file goes on, opw[i ..] moves to the front (0); else i 2754 fn bprefil ai ulong i ulong need ulong to ulong 2755 if gt add i need get opst 1 2756 if eq get opst 2 0 2757 opshift ai i 2758 ret 0 2759 end 2760 end 2761 ret i 2762 end 2763 2764 fn bpscan ai ulong lim ulong mode ulong bo ulong to void 2765 opstart ai 2766 if eq mode 1 2767 owopen 0 bo 2768 end 2769 i = ulong 0 2770 dn = false 2771 while not dn bound 999999 2772 q = ulong 0 2773 while and not dn lt q 999999 bound 999999 2774 q = add q 1 2775 i = bprefil ai i 65 2776 wn = get opst 1 2777 wb = get opst 0 2778 n = wn 2779 if lt sub lim wb n 2780 n = sub lim wb 2781 end 2782 if ge i n 2783 dn = true 2784 put bqst 3 add wb i 2785 else 2786 l = bpclen i n 2787 if eq mode 0 2788 bpcount i l 2789 else 2790 bpenc1 i l 2791 end 2792 i = add i l 2793 end 2794 end 2795 end 2796 end 2797 2798 note fitbytes for MAXBYTES m: just after the last byte 0 among the first m bytes of argument ai; 2799 note exit 21 when there is none 2800 fn bpcut ai ulong m ulong to ulong 2801 opstart ai 2802 last = ulong 0 2803 dn = false 2804 while not dn bound 999999 2805 wb = get opst 0 2806 wn = get opst 1 2807 e = wn 2808 if lt sub m wb e 2809 e = sub m wb 2810 end 2811 j = ulong 0 2812 for o ulong 3 2813 lj = mul add o 1 999999 2814 if gt lj e 2815 lj = e 2816 end 2817 while lt j lj bound 999999 2818 if eq get opw j 0 2819 last = add add wb j 1 2820 end 2821 j = add j 1 2822 end 2823 end 2824 if or lt e wn ne get opst 2 0 2825 dn = true 2826 else 2827 opshift ai wn 2828 end 2829 end 2830 if eq last 0 2831 exit 21 2832 end 2833 ret last 2834 end 2835 2836 note pair table slot of key a 4096 + b, made (count 0, no list) when absent; new slots are listed 2837 note in bqnl; exit 21 past BQPCAP pairs 2838 fn bppslot key ulong to ulong 2839 s = shr mul add key 1 ulong `11400714819323198485` 44 2840 for q ulong `1048577` 2841 k = ulong get bqpk s 2842 if eq k 0 2843 nf = get bqst 5 2844 if ge nf BQPCAP 2845 exit 21 2846 end 2847 put bqpk s uint add key 1 2848 put bqpc s 0 2849 put bqph s 0 2850 put bqpl s 0 2851 put bqst 5 add nf 1 2852 nn = get bqst 6 2853 put bqnl nn uint s 2854 put bqst 6 add nn 1 2855 ret s 2856 end 2857 if eq k add key 1 2858 ret s 2859 end 2860 s = and add s 1 BQPMSK 2861 end 2862 ret s 2863 end 2864 2865 note list type y under pair slot s (once per type in a row); exit 21 past BQNCAP nodes 2866 fn bpnode s ulong y ulong to void 2867 if ne ulong get bqpl s add y 1 2868 put bqpl s uint add y 1 2869 nn = get bqst 7 2870 if ge nn BQNCAP 2871 exit 21 2872 end 2873 put bqnt nn uint y 2874 put bqnx nn get bqph s 2875 put bqph s uint add nn 1 2876 put bqst 7 add nn 1 2877 end 2878 end 2879 2880 fn bpkey c ulong a ulong b ulong to ulong 2881 ret or or shl c 24 shl sub 4095 a 12 sub 4095 b 2882 end 2883 2884 fn bphpush x ulong to void 2885 i = get bqst 8 2886 put bqst 8 add i 1 2887 dn = false 2888 while and not dn gt i 0 bound 64 2889 p = div sub i 1 2 2890 v = get bqhp p 2891 if ge v x 2892 dn = true 2893 else 2894 put bqhp i v 2895 i = p 2896 end 2897 end 2898 put bqhp i x 2899 end 2900 2901 note pop the heap's largest key (the heap is not empty) 2902 fn bphpop to ulong 2903 top = get bqhp 0 2904 n = sub get bqst 8 1 2905 put bqst 8 n 2906 x = get bqhp n 2907 i = ulong 0 2908 dn = false 2909 while not dn bound 64 2910 l = add mul i 2 1 2911 if ge l n 2912 dn = true 2913 else 2914 c = l 2915 r = add l 1 2916 if lt r n 2917 if gt get bqhp r get bqhp l 2918 c = r 2919 end 2920 end 2921 v = get bqhp c 2922 if ge x v 2923 dn = true 2924 else 2925 put bqhp i v 2926 i = c 2927 end 2928 end 2929 end 2930 if gt n 0 2931 put bqhp i x 2932 end 2933 ret top 2934 end 2935 2936 note bqpc[s] += c and -= c, one big array per function (error 12 at this pin otherwise) 2937 fn bppcadd s ulong c ulong to void 2938 put bqpc s add get bqpc s c 2939 end 2940 fn bppcsub s ulong c ulong to void 2941 put bqpc s sub get bqpc s c 2942 end 2943 2944 note count the pairs of type y and list y under them 2945 fn bpinty y ulong to void 2946 o = ulong get bqto y 2947 c = get bqtc y 2948 for j ulong sub ulong get bqtn y 1 2949 x = ulong get bqsy add o j 2950 z = ulong get bqsy add add o j 1 2951 s = bppslot add mul x 4096 z 2952 bppcadd s c 2953 bpnode s y 2954 end 2955 end 2956 2957 note count the pairs of every type, list the types under them, heap every pair 2958 fn bpinit to void 2959 nt = get bqst 0 2960 for y ulong nt 2961 bpinty y 2962 end 2963 for s ulong `1048576` 2964 k = ulong get bqpk s 2965 if ne k 0 2966 c = get bqpc s 2967 if gt c 0 2968 k = sub k 1 2969 bphpush bpkey c div k 4096 and k 4095 2970 end 2971 end 2972 end 2973 end 2974 2975 note the pair to merge: the slot of the heap's largest current key; exit 28 when none is left. 2976 note Each pair has at most one heap entry, and one pop sequence meets a pair at most twice. 2977 fn bpsel to ulong 2978 for o ulong 3 2979 while gt get bqst 8 0 bound 999999 2980 x = bphpop 2981 c = shr x 24 2982 a = sub 4095 and shr x 12 4095 2983 b = sub 4095 and x 4095 2984 s = bppslot add mul a 4096 b 2985 cc = get bqpc s 2986 if eq cc c 2987 ret s 2988 end 2989 if gt cc 0 2990 bphpush bpkey cc a b 2991 end 2992 end 2993 end 2994 exit 28 2995 end 2996 2997 fn bphas o ulong n ulong a ulong b ulong to bool 2998 for j ulong sub n 1 2999 x = ulong get bqsy add o j 3000 z = ulong get bqsy add add o j 1 3001 if and eq x a eq z b 3002 ret true 3003 end 3004 end 3005 ret false 3006 end 3007 3008 note take count c of every pair in bqsy[o .. o + n) 3009 fn bpsub o ulong n ulong c ulong to void 3010 for j ulong sub n 1 3011 x = ulong get bqsy add o j 3012 z = ulong get bqsy add add o j 1 3013 s = bppslot add mul x 4096 z 3014 bppcsub s c 3015 end 3016 end 3017 3018 note replace (a, b) by t in bqsy[o .. o + n), left to right without overlap; returns the length 3019 fn bprpool o ulong n ulong a ulong b ulong t ulong to ulong 3020 w = ulong 0 3021 j = ulong 0 3022 while lt j n bound 128 3023 x = ulong get bqsy add o j 3024 z = ulong 65535 3025 if lt add j 1 n 3026 z = ulong get bqsy add add o j 1 3027 end 3028 if and eq x a eq z b 3029 put bqsy add o w ushort t 3030 j = add j 2 3031 else 3032 put bqsy add o w ushort x 3033 j = add j 1 3034 end 3035 w = add w 1 3036 end 3037 ret w 3038 end 3039 3040 note add count c of every pair in bqsy[o .. o + m) of type y; list y under the pairs holding t 3041 fn bpadd o ulong m ulong c ulong y ulong t ulong to void 3042 for j ulong sub m 1 3043 x = ulong get bqsy add o j 3044 z = ulong get bqsy add add o j 1 3045 s = bppslot add mul x 4096 z 3046 bppcadd s c 3047 if or eq x t eq z t 3048 bpnode s y 3049 end 3050 end 3051 end 3052 3053 note apply merge (a, b) -> t to type y, moving its pair counts 3054 fn bpmtype y ulong a ulong b ulong t ulong to void 3055 o = ulong get bqto y 3056 n = ulong get bqtn y 3057 if bphas o n a b 3058 c = get bqtc y 3059 bpsub o n c 3060 m = bprpool o n a b t 3061 put bqtn y uchar m 3062 bpadd o m c y t 3063 end 3064 end 3065 3066 note merge t over every type listed under pair slot s (each type once: stamp t - 255) 3067 fn bpwalk s ulong a ulong b ulong t ulong to void 3068 nd = ulong get bqph s 3069 st = uint sub t 255 3070 for o ulong 5 3071 while gt nd 0 bound 999999 3072 y = ulong get bqnt sub nd 1 3073 nd = ulong get bqnx sub nd 1 3074 if ne get bqts y st 3075 put bqts y st 3076 bpmtype y a b t 3077 end 3078 end 3079 end 3080 end 3081 3082 note merges 0 .. v - 257 into bqma/bqmb and obm[24 ..) 3083 fn bpmerge v ulong to void 3084 for k ulong sub v 256 3085 s = bpsel 3086 key = sub ulong get bqpk s 1 3087 a = div key 4096 3088 b = and key 4095 3089 t = add 256 k 3090 put bqma t ushort a 3091 put bqmb t ushort b 3092 p = add 24 mul 4 k 3093 put obm p uchar and a 255 3094 put obm add p 1 uchar shr a 8 3095 put obm add p 2 uchar and b 255 3096 put obm add p 3 uchar shr b 8 3097 put bqst 6 0 3098 bpwalk s a b t 3099 bpnewp 3100 end 3101 end 3102 3103 note heap every pair slot made by this merge (bqnl[0 .. bqst 6)) that has a count 3104 fn bpnewp to void 3105 nw = get bqst 6 3106 for i ulong nw 3107 q = ulong get bqnl i 3108 c = get bqpc q 3109 if gt c 0 3110 kq = sub ulong get bqpk q 1 3111 bphpush bpkey c div kq 4096 and kq 4095 3112 end 3113 end 3114 end 3115 3116 note obm[p .. p + 8) = v as u64 LE 3117 fn bpu64 p ulong v ulong to void 3118 for k ulong 8 3119 put obm add p k uchar and shr v mul k 8 255 3120 end 3121 end 3122 3123 note FNV-1a 64 of obm[24 .. e) 3124 fn bpfnvm e ulong to ulong 3125 h = ulong `14695981039346656037` 3126 for k ulong sub e 24 3127 h = mul xor h ulong get obm add 24 k `1099511628211` 3128 end 3129 ret h 3130 end 3131 3132 note u16 LE at opw[p] 3133 fn bprd16 p ulong to ulong 3134 lo = ulong get opw p 3135 hi = ulong get opw add p 1 3136 ret or lo shl hi 8 3137 end 3138 3139 note u64 LE at opw[p] 3140 fn bprd64 p ulong to ulong 3141 v = ulong 0 3142 for k ulong 8 3143 v = or v shl ulong get opw add p k mul k 8 3144 end 3145 ret v 3146 end 3147 3148 note read and check the merges file of argument ai (section 2.3; refusals 26) into bqma/bqmb; 3149 note returns its V. Uses opw. 3150 fn bpload ai ulong to ulong 3151 n = oprd ai 0 0 15385 3152 if or lt n 24 gt n 15384 3153 exit 26 3154 end 3155 if ne bprd64 0 `3550332126946413647` 3156 exit 26 3157 end 3158 vm = bprd64 8 3159 if or lt vm 257 gt vm 4096 3160 exit 26 3161 end 3162 if ne n add 24 mul 4 sub vm 256 3163 exit 26 3164 end 3165 h = ulong `14695981039346656037` 3166 for k ulong sub n 24 3167 h = mul xor h ulong get opw add 24 k `1099511628211` 3168 end 3169 if ne h bprd64 16 3170 exit 26 3171 end 3172 for k ulong sub vm 256 3173 p = add 24 mul 4 k 3174 a = bprd16 p 3175 b = bprd16 add p 2 3176 lk = add 256 k 3177 if or ge a lk ge b lk 3178 exit 26 3179 end 3180 t = add 256 k 3181 put bqma t ushort a 3182 put bqmb t ushort b 3183 end 3184 put bqv 1 vm 3185 ret vm 3186 end 3187 3188 note use the first v - 256 merges: decoded lengths (held at 2^40) and the rank table 3189 fn bpsetv v ulong to void 3190 for t ulong 256 3191 put bqln t 1 3192 end 3193 for k ulong sub v 256 3194 t = add 256 k 3195 ma = ulong get bqma t 3196 mb = ulong get bqmb t 3197 la = get bqln ma 3198 s = add la get bqln mb 3199 if gt s BQLCAP 3200 s = BQLCAP 3201 end 3202 put bqln t s 3203 end 3204 for s ulong 8192 3205 put bqrk s 0 3206 end 3207 for k ulong sub v 256 3208 t = add 256 k 3209 ma = ulong get bqma t 3210 mb = ulong get bqmb t 3211 key = add mul ma 4096 mb 3212 s = shr mul add key 1 ulong `11400714819323198485` 51 3213 dn = false 3214 while not dn bound 8192 3215 if eq get bqrk s 0 3216 put bqrk s uint add key 1 3217 put bqrt s ushort t 3218 dn = true 3219 else 3220 s = and add s 1 8191 3221 end 3222 end 3223 end 3224 put bqv 0 v 3225 end 3226 3227 note write the decoded bytes of token t to stdout (mode 0) or to output 0 (mode 1) 3228 fn bpexp t ulong mode ulong to void 3229 put bqsk 0 ushort t 3230 sp = ulong 1 3231 while gt sp 0 bound 999999 3232 q = ulong 0 3233 while and gt sp 0 lt q 999999 bound 999999 3234 q = add q 1 3235 sp = sub sp 1 3236 x = ulong get bqsk sp 3237 if lt x 256 3238 if eq mode 0 3239 oputc x 3240 else 3241 owbyte 0 x 3242 end 3243 else 3244 put bqsk sp get bqmb x 3245 put bqsk add sp 1 get bqma x 3246 sp = add sp 2 3247 end 3248 end 3249 end 3250 end 3251 3252 note decode token t to output 0 and add its length to bqst 4 3253 fn bpdec1 t ulong to void 3254 bpexp t 1 3255 put bqst 4 add get bqst 4 get bqln t 3256 end 3257 3258 note token file argument ai: mode 0 checks it (exit 21 odd length, then 25 a token >= v) and 3259 note returns its token count; mode 1 decodes it to output 0 (bqst 4 = bytes) 3260 fn bpdscan ai ulong v ulong mode ulong to ulong 3261 opstart ai 3262 i = ulong 0 3263 nt = ulong 0 3264 bad = false 3265 odd = false 3266 dn = false 3267 while not dn bound 999999 3268 q = ulong 0 3269 while and not dn lt q 999999 bound 999999 3270 q = add q 1 3271 i = bprefil ai i 2 3272 wn = get opst 1 3273 if gt add i 2 wn 3274 dn = true 3275 if lt i wn 3276 odd = true 3277 end 3278 else 3279 t = bprd16 i 3280 if ge t v 3281 bad = true 3282 end 3283 if eq mode 1 3284 bpdec1 t 3285 end 3286 nt = add nt 1 3287 i = add i 2 3288 end 3289 end 3290 end 3291 if odd 3292 exit 21 3293 end 3294 if bad 3295 exit 25 3296 end 3297 ret nt 3298 end 3299 3300 note print C a b c (C = 77 M, 75 K, 68 D) 3301 fn bpline c ulong a ulong b ulong d ulong to void 3302 oputc c 3303 oputc 32 3304 oputu a 3305 oputc 32 3306 oputu b 3307 if ne c 75 3308 if ne c 68 3309 oputc 32 3310 oputu d 3311 end 3312 end 3313 oputc 10 3314 oflush 3315 end 3316 3317 note section 8: load merges argument ai for vocabulary v (26, 27) 3318 fn obload ai ulong v ulong to void 3319 vm = bpload ai 3320 if or le v 256 gt v vm 3321 exit 27 3322 end 3323 bpsetv v 3324 bpcls 3325 end 3326 3327 fn obtlen t ulong to ulong 3328 ret get bqln t 3329 end 3330 3331 fn obtput t ulong to void 3332 bpexp t 0 3333 end 3334 3335 note section 8: encode argument ai (at most 4000 bytes, CONTRACT 7.2; longer exit 2) into obtk; 3336 note returns the token count 3337 fn obprompt ai ulong to ulong 3338 n = oargp ai 3339 if gt n 4000 3340 exit 2 3341 end 3342 i = ulong 0 3343 m = ulong 0 3344 while lt i n bound 4097 3345 l = bpclena i n 3346 for k ulong l 3347 put bqtm k ushort get oarg add i k 3348 end 3349 c = bpenct l 3350 for k ulong c 3351 put obtk add m k ulong get bqtm k 3352 end 3353 m = add m c 3354 i = add i l 3355 end 3356 ret m 3357 end 3358 3359 note bpefit TEXT MERGES V [MAXBYTES] 3360 fn cmdbpfit to void 3361 na = oargc 3362 if or lt na 5 gt na 6 3363 exit 2 3364 end 3365 v = oargu 4 3366 if or lt v 257 gt v 4096 3367 exit 2 3368 end 3369 lim = ulong `18446744073709551615` 3370 if eq na 6 3371 lim = bpcut 2 oargu 5 3372 end 3373 bpcls 3374 bpscan 2 lim 0 0 3375 bpinit 3376 bpmerge v 3377 nb = add 24 mul 4 sub v 256 3378 bpu64 0 `3550332126946413647` 3379 bpu64 8 v 3380 bpu64 16 bpfnvm nb 3381 obmsave 3 nb 3382 oputc 77 3383 oputc 32 3384 oputu v 3385 oputc 32 3386 oputu get bqst 0 3387 oputc 32 3388 oputu get bqst 2 3389 oputc 32 3390 oputu get bqst 3 3391 oputc 10 3392 oflush 3393 end 3394 3395 note bpeenc MERGES V TEXT TOKS 3396 fn cmdbpenc to void 3397 if ne oargc 6 3398 exit 2 3399 end 3400 v = oargu 3 3401 obload 2 v 3402 bpscan 4 ulong `18446744073709551615` 1 5 3403 owclose 0 3404 bpline 75 get bqst 3 get bqst 4 0 3405 end 3406 3407 note bpedec MERGES TOKS OUT (V = the file's V) 3408 fn cmdbpdec to void 3409 if ne oargc 5 3410 exit 2 3411 end 3412 vm = bpload 2 3413 bpsetv vm 3414 nt = bpdscan 3 vm 0 3415 owopen 0 4 3416 put bqst 4 0 3417 nd = bpdscan 3 vm 1 3418 owclose 0 3419 bpline 68 nt get bqst 4 0 3420 end 3421 note SPDX-License-Identifier: AGPL-3.0-only 3422 note omegallm cli.om (aien-train#52): the ollm commands (CONTRACT section 7). The platform unit 3423 note (lio.om or eio.om) follows it and holds main. 3424 note Command words are matched as little-endian u64 packings of their bytes (Wolfram 3425 note FromDigits[Reverse[ToCharacterCode[w]],256]): prep `1885696624`, init `1953066601`, 3426 note grad `1684107879`, train `474214396532`, eval `1818326629`, base `1702060386`, sample `111516417089907`, 3427 note gcheck `118074647274343`, mathck `118074698391917`, adamck `118074781033569`; v1: bpefit 3428 note `127996038312034`, bpeenc `109325798699106`, bpedec `109287127216226` (bpe.om). 3429 note Exit codes: 0 ok; 2 usage; 10-15 checkpoint refusals (ckpt.om); 20 data file shorter than 3430 note T + 1 bytes; 21 data over 2^25 bytes or OFFS not exactly 8 B bytes; 22 an OFFS offset with 3431 note o + T >= N; 23 eval W = 0 or W T + 1 > M; 24 prep story over 999999 bytes; 40 gcheck over 3432 note tolerance; 41 adamck over tolerance; 42 mathck over tolerance; 70 I/O. 3433 3434 fn ocmdw to ulong 3435 r = oargr 1 3436 if or le r 0 gt r 8 3437 ret 0 3438 end 3439 v = ulong 0 3440 for k ulong ulong r 3441 v = or v shl ulong get oarg k mul k 8 3442 end 3443 ret v 3444 end 3445 3446 note run the command named by argument 1; the platform main checks that it exists. 3447 fn ocmd to int 3448 w = ocmdw 3449 if eq w `1885696624` 3450 cmdprep 3451 ret 0 3452 end 3453 if eq w `127996038312034` 3454 cmdbpfit 3455 ret 0 3456 end 3457 if eq w `109325798699106` 3458 cmdbpenc 3459 ret 0 3460 end 3461 if eq w `109287127216226` 3462 cmdbpdec 3463 ret 0 3464 end 3465 if eq w `1953066601` 3466 cmdinit 3467 ret 0 3468 end 3469 if eq w `1684107879` 3470 cmdgrad 3471 ret 0 3472 end 3473 if eq w `474214396532` 3474 cmdtrain 3475 ret 0 3476 end 3477 if eq w `1818326629` 3478 cmdeval 3479 ret 0 3480 end 3481 if eq w `1702060386` 3482 cmdbase 3483 ret 0 3484 end 3485 if eq w `111516417089907` 3486 cmdsamp 3487 ret 0 3488 end 3489 if eq w `118074647274343` 3490 cmdgchk 3491 ret 0 3492 end 3493 if eq w `118074698391917` 3494 cmdmath 3495 ret 0 3496 end 3497 if eq w `118074781033569` 3498 cmdadamck 3499 ret 0 3500 end 3501 exit 2 3502 end 3503 3504 note init CK V T D H F L B SEED TOTAL LRMICRO [G]; lr = f64(LRMICRO)/`1000000`.0. Without G the file 3505 note is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6). 3506 fn cmdinit to void 3507 na = oargc 3508 if and ne na 13 ne na 14 3509 exit 2 3510 end 3511 for i ulong 7 3512 put og i oargu add 3 i 3513 end 3514 if not ogeom 3515 exit 12 3516 end 3517 put og 40 oargu 10 3518 tt = oargu 11 3519 if gt tt 999999 3520 exit 12 3521 end 3522 g = ulong 1 3523 put og 46 1 3524 if eq na 14 3525 g = oargu 13 3526 put og 46 2 3527 end 3528 if not ogok2 g 3529 exit 12 3530 end 3531 put og 44 g 3532 put og 42 tt 3533 put og 43 bitcast ulong div f64 oargu 12 `1000000`.0 3534 put og 41 0 3535 oinit 3536 ocksave 2 3537 oputc 73 3538 oputc 32 3539 oputu get og 8 3540 oputc 10 3541 oflush 3542 end 3543 3544 note little-endian u64 at ochk[o..o+8) 3545 fn ochk64 o ulong to ulong 3546 v = ulong 0 3547 for k ulong 8 3548 x = ulong get ochk add o k 3549 v = or v shl x mul k 8 3550 end 3551 ret v 3552 end 3553 3554 note row b from offset ochk[b] (checked by ooffok) 3555 fn ooffrow b ulong to void 3556 obrow b ochk64 mul b 8 3557 end 3558 3559 note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap) 3560 fn ooffok to void 3561 n = get odn 0 3562 tt = get og 1 3563 for b ulong get og 6 3564 if ge ochk64 mul b 8 sub n tt 3565 exit 22 3566 end 3567 end 3568 end 3569 3570 note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1) 3571 fn cmdgrad to void 3572 if ne oargc 6 3573 exit 2 3574 end 3575 ockload 2 3576 odopen 3 3577 ooffrd 4 3578 ooffok 3579 for b ulong get og 6 3580 ooffrow b 3581 end 3582 ls = ograd 3583 ogrsave 5 3584 oputc 71 3585 oputc 32 3586 oputx bitcast ulong ls 3587 oputc 32 3588 oputl rmic ls 3589 oputc 10 3590 oflush 3591 end 3592 3593 note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when 3594 note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`. 3595 note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29. 3596 fn cmdtrain to void 3597 na = oargc 3598 if and ne na 6 ne na 7 3599 exit 2 3600 end 3601 ockload 2 3602 odopen 3 3603 un = oargu 4 3604 ev = oargu 5 3605 if eq ev 0 3606 exit 2 3607 end 3608 k = ulong 1 3609 if eq na 7 3610 k = oargu 6 3611 end 3612 gg = get og 44 3613 if or eq k 0 gt k gg 3614 exit 29 3615 end 3616 if ne rem gg k 0 3617 exit 29 3618 end 3619 put og 45 k 3620 en = get og 42 3621 if lt un en 3622 en = un 3623 end 3624 while lt get og 41 en bound 999999 3625 ls = ostep 3626 s1 = get og 41 3627 if or eq rem s1 ev 0 eq s1 en 3628 ocksave 2 3629 oputc 84 3630 oputc 32 3631 oputu s1 3632 oputc 32 3633 oputl rmic ls 3634 oputc 10 3635 oflush 3636 end 3637 end 3638 end 3639 3640 note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`. 3641 note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the 3642 note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2. 3643 fn cmdeval to void 3644 na = oargc 3645 if and ne na 5 ne na 6 3646 exit 2 3647 end 3648 ockload 2 3649 ww = oargu 4 3650 v = get og 0 3651 if gt v 256 3652 if eq na 5 3653 exit 2 3654 end 3655 obload 5 v 3656 end 3657 odopen 3 3658 tt = get og 1 3659 m = get odn 0 3660 note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T 3661 if eq ww 0 3662 exit 23 3663 end 3664 if gt ww div sub m 1 tt 3665 exit 23 3666 end 3667 ls = 0.0f64 3668 nb = ulong 0 3669 for w ulong ww 3670 obrow 0 mul w tt 3671 ls = add ls ofwd 0 3672 if gt v 256 3673 nb = add nb oevnb tt 3674 end 3675 end 3676 lo = div ls f64 ww 3677 bp = div lo 0.`6931471805599453` 3678 if gt v 256 3679 bp = div mul bp f64 mul ww tt f64 nb 3680 end 3681 oputc 69 3682 oputc 32 3683 oputu get og 41 3684 oputc 32 3685 oputu ww 3686 oputc 32 3687 oputl rmic lo 3688 oputc 32 3689 oputl rmic bp 3690 oputc 10 3691 oflush 3692 end 3693 3694 note decoded bytes of the targets oby[0 .. n) 3695 fn oevnb n ulong to ulong 3696 s = ulong 0 3697 for t ulong n 3698 s = add s obtlen get oby t 3699 end 3700 ret s 3701 end 3702 3703 note append token c to the context window obx[0 .. CT), keeping the last T tokens 3704 fn ocpush c ulong to void 3705 cn = get og 9 3706 tt = get og 1 3707 if lt cn tt 3708 put obx cn c 3709 put og 9 add cn 1 3710 else 3711 for i ulong sub tt 1 3712 put obx i get obx add i 1 3713 end 3714 put obx sub tt 1 c 3715 end 3716 end 3717 3718 note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256, 3719 note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens 3720 note at positions 0..; the next token comes from the logits at the last position. 3721 note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000)) 3722 note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD); 3723 note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0. 3724 note A generated token 0 ends the output and is not written; another writes its decoded bytes. 3725 fn cmdsamp to void 3726 na = oargc 3727 if and ne na 7 ne na 8 3728 exit 2 3729 end 3730 ockload 2 3731 st = oargu 3 3732 nn = oargu 4 3733 if gt nn 999999 3734 exit 2 3735 end 3736 tm = oargu 5 3737 v = get og 0 3738 put og 9 0 3739 ocpush 0 3740 if gt v 256 3741 if eq na 7 3742 exit 2 3743 end 3744 r = oargr 6 3745 if or lt r 0 gt r 4000 3746 exit 2 3747 end 3748 obload 7 v 3749 np = obprompt 6 3750 for i ulong np 3751 ocpush get obtk i 3752 end 3753 else 3754 r = oargr 6 3755 if or lt r 0 gt r 4096 3756 exit 2 3757 end 3758 for i ulong ulong r 3759 ocpush ulong get oarg i 3760 end 3761 end 3762 for g ulong nn 3763 lf = ofwd 0 3764 olast 3765 c = ulong 0 3766 if eq tm 0 3767 c = oargmax 3768 else 3769 c = odraw st div f64 tm 1000.0 3770 st = add st `11400714819323198485` 3771 end 3772 if eq c 0 3773 break 3774 end 3775 if gt v 256 3776 obtput c 3777 else 3778 oputc c 3779 end 3780 ocpush c 3781 end 3782 oflush 3783 end 3784 3785 note logits of the last context position into TMP[0..V) (oa) 3786 fn olast to void 3787 d = get og 2 3788 z = add get og 17 mul sub get og 9 1 d 3789 klogit z get og 27 get og 0 d 3790 end 3791 3792 fn oargmax to ulong 3793 tm = get og 27 3794 b = ulong 0 3795 bv = get oa tm 3796 for c ulong get og 0 3797 if gt get oa add tm c bv 3798 bv = get oa add tm c 3799 b = c 3800 end 3801 end 3802 ret b 3803 end 3804 3805 fn odraw st ulong tau f64 to ulong 3806 tm = get og 27 3807 v = get og 0 3808 m = bitcast f64 ulong `18442240474082181120` 3809 for c ulong v 3810 x = div get oa add tm c tau 3811 put oa add tm c x 3812 m = fmax m x 3813 end 3814 sm = osoft tm v m 3815 u = mul f64 shr smx st 11 1.`1102230246251565e`-16 3816 acc = 0.0f64 3817 la = ulong 0 3818 for k ulong v 3819 pk = get oa add tm k 3820 acc = add acc pk 3821 if lt u acc 3822 ret k 3823 end 3824 if gt pk 0.0 3825 la = k 3826 end 3827 end 3828 ret la 3829 end 3830 3831 note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is 3832 note the retry step for an index whose difference at GCH crossed a ReLU kink. 3833 const GCH f64 0.000001 3834 const GCH2 f64 0.`00000025` 3835 const GCFLOOR f64 0.000001 3836 const GCTOL f64 0.001 3837 note mathck: max relative error allowed against the correctly rounded reference 3838 const MCTOL f64 0.`000000000000001` 3839 3840 note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is 3841 note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every 3842 note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64. 3843 note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured 3844 note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints 3845 note one `c index g_e12 num_e12` line per index (num of the kept step), then 3846 note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL. 3847 fn cmdgchk to void 3848 if ne oargc 4 3849 exit 2 3850 end 3851 ockload 2 3852 odopen 3 3853 obatch get og 41 3854 ls = ograd 3855 d = get og 2 3856 cnt = ulong 0 3857 put og 50 0 3858 put og 51 0 3859 put og 52 0 3860 put og 53 0 3861 ogcten 0 mul get og 0 d 0 3862 ogcten get og 30 mul get og 1 d 1 3863 for l ulong get og 5 3864 ogcten opg1 l d 2 3865 ogcten opwq l mul d d 3 3866 ogcten opwk l mul d d 4 3867 ogcten opwv l mul d d 5 3868 ogcten opwo l mul d d 6 3869 ogcten opg2 l d 7 3870 ogcten opw1 l mul d get og 4 8 3871 ogcten opw2 l mul d get og 4 9 3872 end 3873 ogcten get og 33 d 10 3874 mx = bitcast f64 get og 51 3875 oputc 67 3876 oputc 32 3877 oputu get og 50 3878 oputc 32 3879 oputx get og 51 3880 oputc 32 3881 oputl long mul mx `1000000000`.0 3882 oputc 32 3883 oputu get og 52 3884 oputc 32 3885 oputu get og 53 3886 oputc 10 3887 oflush 3888 if gt mx GCTOL 3889 exit 40 3890 end 3891 end 3892 3893 note step-loss for the batch in obx/oby without touching the gradient 3894 fn oloss to f64 3895 ls = 0.0f64 3896 for b ulong get og 6 3897 ls = add ls ofwd b 3898 end 3899 ret div ls f64 get og 6 3900 end 3901 3902 fn ogcten o ulong n ulong tag ulong to void 3903 for j ulong 4 3904 i = add o rem smx xor get og 40 add mul get og 50 4 j n 3905 ogc1 i 3906 end 3907 end 3908 3909 note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p 3910 fn ogcnum i ulong h f64 to f64 3911 p0 = owg i 3912 pp = add p0 h 3913 pm = sub p0 h 3914 owp i pp 3915 lp = oloss 3916 owp i pm 3917 lm = oloss 3918 owp i p0 3919 ret div sub lp lm sub pp pm 3920 end 3921 3922 fn ogcrel num f64 g f64 to f64 3923 den = fmax fmax fabs num fabs g GCFLOOR 3924 ret div fabs sub num g den 3925 end 3926 3927 note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst 3928 note index, og[53] retries at GCH2 3929 fn ogc1 i ulong to void 3930 g = ocsg i 3931 num = ogcnum i GCH 3932 rel = ogcrel num g 3933 if gt rel GCTOL 3934 put og 53 add get og 53 1 3935 n2 = ogcnum i GCH2 3936 r2 = ogcrel n2 g 3937 if lt r2 rel 3938 num = n2 3939 rel = r2 3940 end 3941 end 3942 oputc 99 3943 oputc 32 3944 oputu i 3945 oputc 32 3946 oputl long mul g `1000000000000`.0 3947 oputc 32 3948 oputl long mul num `1000000000000`.0 3949 oputc 10 3950 put og 50 add get og 50 1 3951 if gt rel bitcast f64 get og 51 3952 put og 51 bitcast ulong rel 3953 put og 52 i 3954 end 3955 end 3956 3957 note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each, 3958 note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact 3959 note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l; 3960 note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has 3961 note no e line or no l line (a check that checked nothing fails). 3962 fn cmdmath to void 3963 if ne oargc 3 3964 exit 2 3965 end 3966 n = odread 2 3967 if ne rem n 36 0 3968 exit 21 3969 end 3970 for k ulong 6 3971 put og add 50 k 0 3972 end 3973 for i ulong div n 36 3974 omline mul i 36 3975 end 3976 omprint 101 0 3977 omprint 108 3 3978 if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL 3979 exit 42 3980 end 3981 if or eq get og 50 0 eq get og 53 0 3982 exit 42 3983 end 3984 end 3985 3986 fn ohex o ulong to ulong 3987 v = ulong 0 3988 for k ulong 16 3989 c = odb add o k 3990 if lt c 58 3991 v = or shl v 4 sub c 48 3992 else 3993 v = or shl v 4 sub c 87 3994 end 3995 end 3996 ret v 3997 end 3998 3999 note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log) 4000 fn omline o ulong to void 4001 kd = odb o 4002 x = bitcast f64 ohex add o 2 4003 rb = ohex add o 19 4004 b = ulong 0 4005 y = 0.0f64 4006 if eq kd 101 4007 y = fexp x 4008 else 4009 b = ulong 3 4010 y = flog x 4011 end 4012 yb = bitcast ulong y 4013 put og add 50 b add get og add 50 b 1 4014 r = bitcast f64 rb 4015 if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong `9218868437227405312` 4016 if ne yb rb 4017 exit 42 4018 end 4019 ret 4020 end 4021 ul = ulong 0 4022 if gt yb rb 4023 ul = sub yb rb 4024 else 4025 ul = sub rb yb 4026 end 4027 if gt ul get og add 51 b 4028 put og add 51 b ul 4029 end 4030 rel = div fabs sub y r fabs r 4031 if gt rel bitcast f64 get og add 52 b 4032 put og add 52 b bitcast ulong rel 4033 end 4034 end 4035 4036 fn omprint c ulong b ulong to void 4037 oputc 77 4038 oputc 32 4039 oputc c 4040 oputc 32 4041 oputu get og add 50 b 4042 oputc 32 4043 oputu get og add 51 b 4044 oputc 32 4045 oputx get og add 52 b 4046 oputc 32 4047 oputl long mul bitcast f64 get og add 52 b 1e18 4048 oputc 10 4049 oflush 4050 end 4051 note SPDX-License-Identifier: AGPL-3.0-only 4052 note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words. 4053 note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both 4054 note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit, 4055 note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is 4056 note free of host words. 4057 global owfd int 2 4058 global opth uchar 2100 4059 global opk uchar 4200 4060 global ocfd int 1 4061 global opp uchar 8 4062 global ost uchar 8 4063 global owrf int 64 4064 global owpd int 64 4065 4066 note the argument count (argv[0] included) 4067 fn oargc to ulong 4068 ret ulong argcnt 4069 end 4070 4071 note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument) 4072 fn oargr i ulong to long 4073 ret installedargs i 4074 end 4075 4076 fn ocpumain to int 4077 if lt oargc 2 4078 exit 2 4079 end 4080 ret ocmd 4081 end 4082 4083 fn oflush to void 4084 n = get oon 0 4085 p = ulong 0 4086 while lt p n bound 4097 4087 w = write 1 oout p sub n p 4088 if le w 0 4089 exit 70 4090 end 4091 p = add p ulong w 4092 end 4093 put oon 0 0 4094 end 4095 4096 note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied) 4097 fn ocwopen ai ulong to void 4098 pl = oargp ai 4099 if gt pl 2000 4100 exit 2 4101 end 4102 for i ulong pl 4103 put opth i get oarg i 4104 end 4105 put opth pl 46 4106 put opth add pl 1 116 4107 put opth add pl 2 109 4108 put opth add pl 3 112 4109 fd = int fopen opth 0 add pl 4 577 420 4110 if lt fd 0 4111 exit 70 4112 end 4113 put ocfd 0 fd 4114 end 4115 4116 note write ock[0 .. n) at byte position pos of the open PATH.tmp 4117 fn ocwput pos ulong n ulong to void 4118 fd = get ocfd 0 4119 p = ulong 0 4120 while lt p n bound 999999 4121 w = pwrite fd ock p sub n p long add pos p 4122 if le w 0 4123 exit 70 4124 end 4125 p = add p ulong w 4126 end 4127 end 4128 4129 note fsync and close PATH.tmp, then rename it onto the path in argv[ai] 4130 fn ocwend ai ulong to void 4131 fd = get ocfd 0 4132 if ne fsync fd 0 4133 exit 70 4134 end 4135 if ne fclose fd 0 4136 exit 70 4137 end 4138 ocrename oargp ai 4139 end 4140 4141 note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list 4142 fn ocrename pl ulong to void 4143 put opk 0 2 4144 put opk 1 0 4145 put opk 2 uchar and add pl 4 255 4146 put opk 3 uchar shr add pl 4 8 4147 for i ulong add pl 4 4148 put opk add 4 i get opth i 4149 end 4150 q = add 8 pl 4151 put opk q uchar and pl 255 4152 put opk add q 1 uchar shr pl 8 4153 for j ulong pl 4154 put opk add add q 2 j get opth j 4155 end 4156 r = renames opk 0 add add q 2 pl 0 4157 if ne r 0 4158 exit 70 4159 end 4160 end 4161 4162 note size in bytes of the file named by argv[ai] (seek to its end) 4163 fn ofsz ai ulong to ulong 4164 n = oargp ai 4165 fd = int fopen oarg 0 n 0 0 4166 if lt fd 0 4167 exit 70 4168 end 4169 sz = fseek fd 0 2 4170 if lt sz 0 4171 exit 70 4172 end 4173 if ne fclose fd 0 4174 exit 70 4175 end 4176 ret ulong sz 4177 end 4178 4179 note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3 4180 note maxima (3 `29995392` -> `89986176` values, `719889408` bytes), in one private anonymous mapping made 4181 note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited 4182 note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70). 4183 const OCSN ulong `89986176` 4184 global ocsh long 1 4185 global ocsf ulong 1 4186 4187 note capacity of the cold store in f64 values (maps it on the first call) 4188 fn ocsinit to ulong 4189 if eq get ocsf 0 0 4190 h = mapnew int -1 0 mul OCSN 8 3 34 4191 if lt h 0 4192 exit 70 4193 end 4194 put ocsh 0 h 4195 put ocsf 0 1 4196 end 4197 ret OCSN 4198 end 4199 4200 fn ocsg i ulong to f64 4201 ret bitcast f64 mapget get ocsh 0 mul i 8 8 4202 end 4203 4204 fn ocsp i ulong v f64 to void 4205 x = mapset get ocsh 0 mul i 8 8 bitcast ulong v 4206 end 4207 4208 note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is 4209 note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w 4210 note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each 4211 note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes 4212 note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD 4213 note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the 4214 note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that 4215 note does not exit 0 is an I/O refusal (exit 70). 4216 fn ogradw to f64 4217 k = get og 45 4218 if le k 1 4219 ret ograd 4220 end 4221 per = div get og 44 k 4222 oflush 4223 for w ulong sub k 1 4224 ofork1 w per 4225 end 4226 for j ulong per 4227 oblkc j 4228 ogfold eq j 0 4229 end 4230 for w ulong sub k 1 4231 ojoin1 w per 4232 end 4233 ret olsum 4234 end 4235 4236 note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid 4237 fn ofork1 w ulong per ulong to void 4238 r = pipefds opp 0 8 0 4239 if ne r 0 4240 exit 70 4241 end 4242 rf = int or ulong get opp 0 shl ulong get opp 1 8 4243 wf = int or ulong get opp 4 shl ulong get opp 5 8 4244 pid = fork 4245 if lt pid 0 4246 exit 70 4247 end 4248 if eq pid 0 4249 c = fclose rf 4250 owork wf mul add w 1 per per 4251 exit 0 4252 end 4253 c2 = fclose wf 4254 put owrf w rf 4255 put owpd w int pid 4256 end 4257 4258 note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it 4259 fn ojoin1 w ulong per ulong to void 4260 rf = get owrf w 4261 for j ulong per 4262 orecvb rf 4263 ogadd 4264 end 4265 r = div get og 6 get og 44 4266 m = mul per r 4267 orecvl rf mul add w 1 m m 4268 c = fclose rf 4269 pd = get owpd w 4270 x = waitpid pd ost 0 4 0 4271 if ne x long pd 4272 exit 70 4273 end 4274 s0 = get ost 0 4275 s1 = get ost 1 4276 if ne or s0 s1 0 4277 exit 70 4278 end 4279 end 4280 4281 note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then 4282 note the losses of its sequences 4283 fn owork fd int j0 ulong n ulong to void 4284 for t ulong n 4285 oblkc add j0 t 4286 osendb fd 4287 end 4288 r = div get og 6 get og 44 4289 b0 = mul j0 r 4290 m = mul n r 4291 for i ulong m 4292 ocput8 mul i 8 bitcast ulong get olos add b0 i 4293 end 4294 owall fd mul m 8 4295 end 4296 4297 fn owall fd int n ulong to void 4298 p = ulong 0 4299 while lt p n bound 999999 4300 w = write fd ock p sub n p 4301 if le w 0 4302 exit 70 4303 end 4304 p = add p ulong w 4305 end 4306 end 4307 4308 fn ordall fd int n ulong to void 4309 p = ulong 0 4310 while lt p n bound 999999 4311 r = read fd ock p sub n p 4312 if le r 0 4313 exit 70 4314 end 4315 p = add p ulong r 4316 end 4317 end 4318 4319 note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values 4320 fn osendb fd int to void 4321 p = get og 8 4322 nc = div add p sub OCKC 1 OCKC 4323 for c ulong nc 4324 i = mul c OCKC 4325 m = sub p i 4326 if gt m OCKC 4327 m = OCKC 4328 end 4329 ocenc 4 i m 4330 owall fd mul m 8 4331 end 4332 end 4333 4334 note receive one block into BLK (gradient pool [P .. 2P)) 4335 fn orecvb fd int to void 4336 p = get og 8 4337 nc = div add p sub OCKC 1 OCKC 4338 for c ulong nc 4339 i = mul c OCKC 4340 m = sub p i 4341 if gt m OCKC 4342 m = OCKC 4343 end 4344 ordall fd mul m 8 4345 ocdecb add p i m 4346 end 4347 end 4348 4349 note receive n losses into olos[b0 ..) 4350 fn orecvl fd int b0 ulong n ulong to void 4351 ordall fd mul n 8 4352 for i ulong n 4353 put olos add b0 i bitcast f64 ocget8 mul i 8 4354 end 4355 end 4356 4357 note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21). 4358 note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes 4359 note of at most 999999 inner passes each cover 2^25 + 2. 4360 fn odread i ulong to ulong 4361 n = oargp i 4362 fd = int fopen oarg 0 n 0 0 4363 if lt fd 0 4364 exit 70 4365 end 4366 p = ulong 0 4367 dn = false 4368 for o ulong 64 4369 q = ulong 0 4370 while and not dn lt q 999999 bound 999999 4371 q = add q 1 4372 r = long 0 4373 if lt p ODHALF 4374 r = read fd od0 p sub ODHALF p 4375 else 4376 if lt p ODCAP 4377 r = read fd od1 sub p ODHALF sub ODCAP p 4378 else 4379 r = read fd ochk 0 1 4380 end 4381 end 4382 if lt r 0 4383 exit 70 4384 end 4385 if eq r 0 4386 dn = true 4387 else 4388 p = add p ulong r 4389 if gt p ODCAP 4390 exit 21 4391 end 4392 end 4393 end 4394 if dn 4395 break 4396 end 4397 end 4398 if not dn 4399 exit 70 4400 end 4401 c = fclose fd 4402 put odn 0 p 4403 ret p 4404 end 4405 4406 note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD). 4407 fn owflush k ulong to void 4408 n = get owbn k 4409 fd = get owfd k 4410 p = ulong 0 4411 while lt p n bound 65537 4412 w = long 0 4413 if eq k 0 4414 w = write fd owb0 p sub n p 4415 else 4416 w = write fd owb1 p sub n p 4417 end 4418 if le w 0 4419 exit 70 4420 end 4421 p = add p ulong w 4422 end 4423 put owbn k 0 4424 end 4425 4426 fn owopen k ulong i ulong to void 4427 n = oargp i 4428 fd = int fopen oarg 0 n 577 420 4429 if lt fd 0 4430 exit 70 4431 end 4432 put owfd k fd 4433 put owbn k 0 4434 end 4435 4436 fn owclose k ulong to void 4437 owflush k 4438 fd = get owfd k 4439 if ne fsync fd 0 4440 exit 70 4441 end 4442 if ne fclose fd 0 4443 exit 70 4444 end 4445 end 4446 4447 note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes 4448 fn ooffrd ai ulong to void 4449 n = oargp ai 4450 fd = int fopen oarg 0 n 0 0 4451 if lt fd 0 4452 exit 70 4453 end 4454 p = ulong 0 4455 dn = false 4456 while not dn bound 70000 4457 r = read fd ochk p sub 65536 p 4458 if lt r 0 4459 exit 70 4460 end 4461 if eq r 0 4462 dn = true 4463 else 4464 p = add p ulong r 4465 if ge p 65536 4466 exit 21 4467 end 4468 end 4469 end 4470 c = fclose fd 4471 if ne p mul 8 get og 6 4472 exit 21 4473 end 4474 end 4475 4476 4477 note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai], 4478 note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns 4479 note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an 4480 note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one 4481 note byte or ends, so n + 1 <= `2097153` passes suffice: 3 outer passes of at most 999999 inner ones. 4482 fn oprd ai ulong pos ulong off ulong n ulong to ulong 4483 pl = oargp ai 4484 fd = int fopen oarg 0 pl 0 0 4485 if lt fd 0 4486 exit 70 4487 end 4488 p = ulong 0 4489 dn = false 4490 for o ulong 3 4491 while and not dn lt p n bound 999999 4492 r = pread fd opw add off p sub n p long add pos p 4493 if lt r 0 4494 exit 70 4495 end 4496 if eq r 0 4497 dn = true 4498 else 4499 p = add p ulong r 4500 end 4501 end 4502 end 4503 if ne fclose fd 0 4504 exit 70 4505 end 4506 ret p 4507 end 4508 4509 note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does: 4510 note PATH.tmp, fsync, rename 4511 fn obmsave ai ulong n ulong to void 4512 pl = oargp ai 4513 if gt pl 2000 4514 exit 2 4515 end 4516 for i ulong pl 4517 put opth i get oarg i 4518 end 4519 put opth pl 46 4520 put opth add pl 1 116 4521 put opth add pl 2 109 4522 put opth add pl 3 112 4523 fd = int fopen opth 0 add pl 4 577 420 4524 if lt fd 0 4525 exit 70 4526 end 4527 p = ulong 0 4528 while lt p n bound 999999 4529 w = write fd obm p sub n p 4530 if le w 0 4531 exit 70 4532 end 4533 p = add p ulong w 4534 end 4535 if ne fsync fd 0 4536 exit 70 4537 end 4538 if ne fclose fd 0 4539 exit 70 4540 end 4541 ocrename pl 4542 end 4543 note SPDX-License-Identifier: AGPL-3.0-only 4544 note Native continuation of canonical nvrm/nvrm.c, source349913f1, NVIDIA ABI580.173.02. 4545 note One explicitly owned context, bounded buffers, channel, submission and completion. 4546 text nvctl "/dev/nvidiactl" 4547 text nvdev "/dev/nvidia0" 4548 text nvuvm "/dev/nvidia-uvm" 4549 global nvfd int 4 4550 global nvobj uint 16 4551 global nvhdr uchar 32 4552 global nvpar uchar 16384 4553 global nvcard uchar 4608 4554 global nvseq uint 1 4555 global nverr long 1 4556 global nvstage uint 1 4557 global nvid uint 1 4558 global nvinst uint 1 4559 global nvsm uint 1 4560 global nvcls uint 3 4561 global nvuuid uchar 16 4562 global nvreg uchar 2 4563 note Context phases:0 closed,1 driver resources live,2 descriptors closed with mapping cleanup pending. 4564 global nvstate uchar 1 4565 global nvmm uint 1 4566 global nvfault uchar 1 4567 global nvnext ulong 1 4568 global nvbar long 1 4569 global nvfifo long 1 4570 global nvnotify long 1 4571 global nvchva long 1 4572 note A mapping not yet published to its caller remains owned after failed cleanup. 4573 global nvunpub long 1 4574 global nvtoken uint 1 4575 global nvchreg uchar 1 4576 global nvsched uchar 1 4577 global nvput ulong 1 4578 global nvret ulong 1 4579 global nvretry uint 1 4580 global nvmobj uint 96 4581 global nvmmap long 96 4582 global nvmaddr ulong 96 4583 global nvmlen ulong 96 4584 global nvmflag uchar 96 4585 fn nhput off ulong v uint to void 4586 for i ulong 4 4587 put nvhdr add off i uchar and shr v mul i 8 255 4588 end 4589 end 4590 fn nhget off ulong to uint 4591 v = uint 0 4592 for i ulong 4 4593 v = or v shl uint get nvhdr add off i mul i 8 4594 end 4595 ret v 4596 end 4597 fn npput off ulong v uint to void 4598 for i ulong 4 4599 put nvpar add off i uchar and shr v mul i 8 255 4600 end 4601 end 4602 fn npget off ulong to uint 4603 v = uint 0 4604 for i ulong 4 4605 v = or v shl uint get nvpar add off i mul i 8 4606 end 4607 ret v 4608 end 4609 fn npp64 off ulong v ulong to void 4610 for i ulong 8 4611 put nvpar add off i uchar and shr v mul i 8 255 4612 end 4613 end 4614 fn npg64 off ulong to ulong 4615 v = ulong 0 4616 for i ulong 8 4617 v = or v shl ulong get nvpar add off i mul i 8 4618 end 4619 ret v 4620 end 4621 fn npuuid to void 4622 npzero 4623 for i ulong 16 4624 put nvpar i get nvuuid i 4625 end 4626 end 4627 fn ncget off ulong to uint 4628 v = uint 0 4629 for i ulong 4 4630 v = or v shl uint get nvcard add off i mul i 8 4631 end 4632 ret v 4633 end 4634 fn nhzero to void 4635 for i ulong len nvhdr 4636 put nvhdr i 0 4637 end 4638 end 4639 fn npzero to void 4640 for i ulong len nvpar 4641 put nvpar i 0 4642 end 4643 end 4644 fn nvos r long status uint to bool 4645 if ne r 0 4646 put nverr 0 r 4647 ret false 4648 end 4649 if ne status 0 4650 put nverr 0 long status 4651 ret false 4652 end 4653 ret true 4654 end 4655 fn nvalc parent uint cls uint n ulong to uint 4656 nhzero 4657 nhput 0 get nvobj 0 4658 nhput 4 parent 4659 if ne cls 65 4660 if eq get nvseq 0 `4294967295` 4661 put nverr 0 neg long 75 4662 ret 0 4663 end 4664 nhput 8 get nvseq 0 4665 put nvseq 0 add get nvseq 0 1 4666 end 4667 nhput 12 cls 4668 r = devcall get nvfd 0 uint `3223340587` nvhdr 0 32 nvpar 0 n 4669 if not nvos r nhget 28 4670 ret 0 4671 end 4672 ret nhget 8 4673 end 4674 fn nvctrl obj uint cmd uint n ulong to bool 4675 nhzero 4676 nhput 0 get nvobj 0 4677 nhput 4 obj 4678 nhput 8 cmd 4679 r = devcall get nvfd 0 uint `3223340586` nvhdr 0 32 nvpar 0 n 4680 ret nvos r nhget 28 4681 end 4682 fn nvfree parent uint obj uint to bool 4683 if eq obj 0 4684 ret true 4685 end 4686 nhzero 4687 nhput 0 get nvobj 0 4688 nhput 4 parent 4689 nhput 8 obj 4690 r = devctl get nvfd 0 uint `3222292009` nvhdr 0 16 4691 ret nvos r nhget 12 4692 end 4693 fn nvstop to bool 4694 if eq get nvstate 0 0 4695 ret true 4696 end 4697 if eq get nvstate 0 2 4698 ret nvmapfinish 4699 end 4700 ok = true 4701 if nvchstop 4702 if not nvmclose 4703 ok = false 4704 end 4705 else 4706 ok = false 4707 end 4708 if gt get nvbar 0 0 4709 r = mapdrop get nvbar 0 4710 if ne r 0 4711 put nverr 0 r 4712 ok = false 4713 else 4714 put nvbar 0 0 4715 end 4716 end 4717 if ne get nvobj 5 0 4718 if not nvfree get nvobj 2 get nvobj 5 4719 ok = false 4720 end 4721 put nvobj 5 0 4722 end 4723 if ne get nvreg 1 0 4724 npuuid 4725 r = uvmctl get nvfd 2 uint 26 nvpar 0 20 4726 if not nvos r npget 16 4727 ok = false 4728 end 4729 put nvreg 1 0 4730 end 4731 if ne get nvreg 0 0 4732 npuuid 4733 r = uvmctl get nvfd 2 uint 38 nvpar 0 20 4734 if not nvos r npget 16 4735 ok = false 4736 end 4737 put nvreg 0 0 4738 end 4739 if ne get nvobj 4 0 4740 if not nvfree get nvobj 1 get nvobj 4 4741 ok = false 4742 end 4743 put nvobj 4 0 4744 end 4745 if ne get nvobj 3 0 4746 if not nvfree get nvobj 1 get nvobj 3 4747 ok = false 4748 end 4749 put nvobj 3 0 4750 end 4751 if ne get nvobj 2 0 4752 if not nvfree get nvobj 1 get nvobj 2 4753 ok = false 4754 end 4755 put nvobj 2 0 4756 end 4757 if ne get nvobj 1 0 4758 if not nvfree get nvobj 0 get nvobj 1 4759 ok = false 4760 end 4761 put nvobj 1 0 4762 end 4763 if ne get nvobj 0 0 4764 if not nvfree get nvobj 0 get nvobj 0 4765 ok = false 4766 end 4767 put nvobj 0 0 4768 end 4769 for j ulong len nvfd 4770 i = sub 3 j 4771 if ge get nvfd i 0 4772 if ne fclose get nvfd i 0 4773 ok = false 4774 end 4775 put nvfd i neg int 1 4776 end 4777 end 4778 note Close is attempted once per descriptor. Never repeat driver calls after this phase. 4779 put nvstate 0 2 4780 put nvsched 0 0 4781 if not nvmapfinish 4782 ok = false 4783 end 4784 if not ok 4785 put nvfault 0 1 4786 end 4787 ret ok 4788 end 4789 note The driver descriptors are closed; only owned CPU mappings may be retried here. 4790 fn nvmapfinish to bool 4791 if ne get nvstate 0 2 4792 put nverr 0 neg long 16 4793 ret false 4794 end 4795 ok = true 4796 if gt get nvunpub 0 0 4797 r = mapdrop get nvunpub 0 4798 if ne r 0 4799 put nverr 0 r 4800 ok = false 4801 else 4802 put nvunpub 0 0 4803 end 4804 end 4805 if gt get nvbar 0 0 4806 r = mapdrop get nvbar 0 4807 if ne r 0 4808 put nverr 0 r 4809 ok = false 4810 else 4811 put nvbar 0 0 4812 end 4813 end 4814 if gt get nvchva 0 0 4815 r = mapdrop get nvchva 0 4816 if ne r 0 4817 put nverr 0 r 4818 ok = false 4819 else 4820 put nvchva 0 0 4821 end 4822 end 4823 if not nvmforce 4824 ok = false 4825 end 4826 if not ok 4827 put nvfault 0 1 4828 ret false 4829 end 4830 note Descriptor closure retired every driver object, including a refused channel teardown. 4831 for i ulong len nvobj 4832 put nvobj i 0 4833 end 4834 for i ulong len nvreg 4835 put nvreg i 0 4836 end 4837 put nvchreg 0 0 4838 put nvput 0 0 4839 put nvret 0 0 4840 put nvfifo 0 0 4841 put nvnotify 0 0 4842 put nvstate 0 0 4843 ret true 4844 end 4845 fn nvstart to bool 4846 if ne get nvfault 0 0 4847 put nverr 0 neg long 5 4848 ret false 4849 end 4850 if ne get nvstate 0 0 4851 put nverr 0 neg long 16 4852 ret false 4853 end 4854 put nvstate 0 1 4855 put nvnext 0 `68719476736` 4856 for i ulong len nvfd 4857 put nvfd i neg int 1 4858 end 4859 for i ulong len nvobj 4860 put nvobj i 0 4861 end 4862 put nvseq 0 `3472883713` 4863 put nverr 0 0 4864 put nvstage 0 1 4865 r = fopen nvctl 0 len nvctl 524290 0 4866 if lt r 0 4867 put nverr 0 r 4868 ret false 4869 end 4870 put nvfd 0 int r 4871 put nvstage 0 2 4872 npzero 4873 root = nvalc 0 65 0 4874 if eq root 0 4875 ret false 4876 end 4877 put nvobj 0 root 4878 put nvstage 0 3 4879 for i ulong len nvcard 4880 put nvcard i 0 4881 end 4882 r = devctl get nvfd 0 uint `3523233480` nvcard 0 4608 4883 if not nvos r 0 4884 ret false 4885 end 4886 found = uint 0 4887 for i ulong 64 4888 off = mul i 72 4889 if ne get nvcard off 0 4890 found = add found 1 4891 if ne ncget add off 56 0 4892 put nverr 0 neg long 19 4893 ret false 4894 end 4895 put nvid 0 ncget add off 16 4896 end 4897 end 4898 if ne found 1 4899 put nverr 0 neg long 19 4900 ret false 4901 end 4902 put nvstage 0 4 4903 r = fopen nvdev 0 len nvdev 524290 0 4904 if lt r 0 4905 put nverr 0 r 4906 ret false 4907 end 4908 put nvfd 1 int r 4909 npzero 4910 npput 0 uint get nvfd 0 4911 r = devctl get nvfd 1 uint `3221505737` nvpar 0 4 4912 if not nvos r 0 4913 ret false 4914 end 4915 put nvstage 0 5 4916 npzero 4917 npput 0 get nvid 0 4918 if not nvctrl root 517 32 4919 ret false 4920 end 4921 put nvinst 0 npget 8 4922 put nvstage 0 6 4923 npzero 4924 npput 0 get nvinst 0 4925 npput 4 root 4926 dev = nvalc root 128 56 4927 if eq dev 0 4928 ret false 4929 end 4930 put nvobj 1 dev 4931 put nvstage 0 7 4932 npzero 4933 subd = nvalc dev 8320 4 4934 if eq subd 0 4935 ret false 4936 end 4937 put nvobj 2 subd 4938 put nvstage 0 8 4939 npzero 4940 if not nvctrl dev `8389266` 804 4941 ret false 4942 end 4943 n = npget 0 4944 if gt n 200 4945 put nverr 0 neg long 75 4946 ret false 4947 end 4948 for i ulong 3 4949 put nvcls i 0 4950 end 4951 for i ulong ulong n 4952 c = npget add 4 mul i 4 4953 if eq c 52928 4954 put nvcls 0 c 4955 end 4956 if eq c 51567 4957 put nvcls 1 c 4958 end 4959 if eq c 50017 4960 put nvcls 2 c 4961 end 4962 end 4963 for i ulong 3 4964 if eq get nvcls i 0 4965 put nverr 0 neg long 19 4966 ret false 4967 end 4968 end 4969 put nvstage 0 9 4970 npzero 4971 npput 0 1 4972 npput 4 12 4973 if not nvctrl subd `545264168` 488 4974 ret false 4975 end 4976 put nvsm 0 npget 8 4977 note RM reports SM10.04 on GB10; CUDA target sm121 uses a different numbering contract. 4978 note Matches canonical evidence/m16-blackwell-native-path-requalification-receipt.json. 4979 if ne get nvsm 0 2564 4980 put nverr 0 neg long 19 4981 ret false 4982 end 4983 put nvstage 0 10 4984 ret true 4985 end 4986 4987 note Low GPU command addresses are reserved without replacement before an owned bind. 4988 note BAR mappings may use Linux-selected addresses; GPU buffers stay below 40 bits. 4989 fn nvcpumap obj uint size ulong system bool flags uint to long 4990 if or ne get nvfault 0 0 ne get nvstate 0 1 4991 put nverr 0 neg long 5 4992 ret neg long 1 4993 end 4994 fd = neg long 1 4995 if system 4996 fd = fopen nvctl 0 len nvctl 524290 0 4997 else 4998 fd = fopen nvdev 0 len nvdev 524290 0 4999 end 5000 if lt fd 0 5001 put nverr 0 fd 5002 ret fd 5003 end 5004 if not system 5005 npzero 5006 npput 0 uint get nvfd 0 5007 r = devctl int fd uint `3221505737` nvpar 0 4 5008 if not nvos r 0 5009 r = fclose int fd 5010 ret neg long 1 5011 end 5012 end 5013 npzero 5014 npput 0 get nvobj 0 5015 npput 4 get nvobj 1 5016 npput 8 obj 5017 npp64 24 size 5018 npput 44 flags 5019 npput 48 uint fd 5020 r = devctl get nvfd 0 uint `3224913486` nvpar 0 56 5021 if not nvos r npget 40 5022 r = fclose int fd 5023 ret neg long 1 5024 end 5025 m = neg long 1 5026 if system 5027 if gt get nvnext 0 sub `1099511627776` size 5028 put nverr 0 neg long 75 5029 r = fclose int fd 5030 ret neg long 1 5031 end 5032 m = maphold get nvnext 0 size 5033 if gt m 0 5034 put nvnext 0 add get nvnext 0 size 5035 r = mapbind m int fd 0 3 5036 if ne r 0 5037 m = r 5038 end 5039 end 5040 else 5041 m = mapnew int fd 0 size 3 1 5042 end 5043 r = fclose int fd 5044 if ne r 0 5045 put nverr 0 r 5046 put nvfault 0 1 5047 if gt m 0 5048 r = mapdrop m 5049 if ne r 0 5050 put nvunpub 0 m 5051 end 5052 end 5053 ret neg long 1 5054 end 5055 if lt m 0 5056 put nverr 0 m 5057 end 5058 ret m 5059 end 5060 5061 note Release flags change only after confirmed acceptance. Failed entries stay quarantined. 5062 fn nvmrel slot ulong to bool 5063 f = get nvmflag slot 5064 if ne and f 4 0 5065 npzero 5066 npp64 0 get nvmaddr slot 5067 npp64 8 get nvmlen slot 5068 r = uvmctl get nvfd 2 uint 34 nvpar 0 24 5069 if not nvos r npget 16 5070 put nvfault 0 1 5071 ret false 5072 end 5073 f = and f 251 5074 put nvmflag slot f 5075 end 5076 if ne and f 8 0 5077 npzero 5078 npput 0 get nvobj 0 5079 npput 4 get nvobj 1 5080 npput 8 get nvobj 3 5081 npput 12 get nvmobj slot 5082 npp64 24 get nvmaddr slot 5083 npp64 32 get nvmlen slot 5084 r = devctl get nvfd 0 uint `3224389208` nvpar 0 48 5085 if not nvos r npget 40 5086 put nvfault 0 1 5087 ret false 5088 end 5089 f = and f 247 5090 put nvmflag slot f 5091 end 5092 if ne and f 1 0 5093 if not nvfree get nvobj 1 get nvmobj slot 5094 put nvfault 0 1 5095 ret false 5096 end 5097 f = and f 254 5098 put nvmflag slot f 5099 end 5100 if ne and f 2 0 5101 r = mapdrop get nvmmap slot 5102 if not nvos r 0 5103 put nvfault 0 1 5104 ret false 5105 end 5106 end 5107 put nvmflag slot 0 5108 put nvmobj slot 0 5109 put nvmmap slot 0 5110 put nvmaddr slot 0 5111 put nvmlen slot 0 5112 ret true 5113 end 5114 fn nvmclose to bool 5115 ok = true 5116 for i ulong len nvmflag 5117 if ne get nvmflag i 0 5118 if not nvmrel i 5119 ok = false 5120 end 5121 end 5122 end 5123 ret ok 5124 end 5125 note Called only after the owning driver context is closed, including error teardown. 5126 fn nvmforce to bool 5127 if ne get nvstate 0 2 5128 put nverr 0 neg long 16 5129 ret false 5130 end 5131 ok = true 5132 for i ulong len nvmflag 5133 note Driver-owned flags and handles retired when the descriptors closed. 5134 put nvmflag i and get nvmflag i 2 5135 put nvmobj i 0 5136 if ne and get nvmflag i 2 0 5137 r = mapdrop get nvmmap i 5138 if ne r 0 5139 put nverr 0 r 5140 ok = false 5141 else 5142 put nvmflag i 0 5143 put nvmmap i 0 5144 put nvmaddr i 0 5145 put nvmlen i 0 5146 end 5147 else 5148 put nvmmap i 0 5149 put nvmaddr i 0 5150 put nvmlen i 0 5151 end 5152 end 5153 ret ok 5154 end 5155 fn nvdrop m long to bool 5156 if ne get nvput 0 get nvret 0 5157 put nverr 0 neg long 16 5158 ret false 5159 end 5160 if and ne get nvobj 8 0 or eq m get nvfifo 0 eq m get nvnotify 0 5161 put nverr 0 neg long 16 5162 ret false 5163 end 5164 for i ulong len nvmmap 5165 if and gt m 0 eq get nvmmap i m 5166 ret nvmrel i 5167 end 5168 end 5169 put nverr 0 neg long 22 5170 ret false 5171 end 5172 fn nvalloc want ulong cached bool to long 5173 if or eq get nvreg 1 0 ne get nvfault 0 0 5174 put nverr 0 neg long 5 5175 ret neg long 1 5176 end 5177 if or eq want 0 gt want `68719476736` 5178 put nverr 0 neg long 22 5179 ret neg long 1 5180 end 5181 size = mul div add want 4095 4096 4096 5182 slot = ulong 96 5183 for i ulong 96 5184 if and eq slot 96 eq get nvmflag i 0 5185 slot = i 5186 end 5187 end 5188 if eq slot 96 5189 put nverr 0 neg long 24 5190 ret neg long 1 5191 end 5192 put nvstage 0 21 5193 npzero 5194 npput 0 get nvobj 0 5195 npput 8 49409 5196 npput 24 `436207616` 5197 attr = uint 9 5198 if cached 5199 attr = 5 5200 end 5201 npput 28 attr 5202 npput 32 6 5203 npp64 64 size 5204 npp64 72 4096 5205 npp64 88 sub size 1 5206 h = nvalc get nvobj 1 62 128 5207 if eq h 0 5208 put nvfault 0 1 5209 ret neg long 1 5210 end 5211 put nvmobj slot h 5212 put nvmlen slot size 5213 put nvmflag slot 1 5214 put nvstage 0 22 5215 m = nvcpumap h size true 0 5216 if le m 0 5217 put nvfault 0 1 5218 ret neg long 1 5219 end 5220 put nvmmap slot m 5221 put nvmaddr slot mapaddr m 5222 put nvmflag slot 3 5223 va = get nvmaddr slot 5224 if or ne and va 4095 0 gt va sub `562949869535232` size 5225 put nverr 0 neg long 75 5226 put nvfault 0 1 5227 ret neg long 1 5228 end 5229 put nvstage 0 23 5230 npzero 5231 npp64 0 va 5232 npp64 8 size 5233 r = uvmctl get nvfd 2 uint 73 nvpar 0 24 5234 if not nvos r npget 16 5235 put nvfault 0 1 5236 ret neg long 1 5237 end 5238 put nvmflag slot 7 5239 put nvstage 0 24 5240 npzero 5241 npput 0 get nvobj 0 5242 npput 4 get nvobj 1 5243 npput 8 get nvobj 3 5244 npput 12 h 5245 npp64 24 size 5246 npput 32 33040 5247 npp64 48 va 5248 r = devctl get nvfd 0 uint `3225437783` nvpar 0 64 5249 if not nvos r npget 56 5250 put nvfault 0 1 5251 ret neg long 1 5252 end 5253 put nvmflag slot 15 5254 if ne npg64 48 va 5255 put nverr 0 neg long 22 5256 put nvfault 0 1 5257 ret neg long 1 5258 end 5259 put nvstage 0 25 5260 npzero 5261 npp64 0 va 5262 npp64 8 size 5263 for i ulong 16 5264 put nvpar add i 24 get nvuuid i 5265 end 5266 npput 40 1 5267 npp64 9240 1 5268 npput 9248 uint get nvfd 0 5269 npput 9252 get nvobj 0 5270 npput 9256 h 5271 r = uvmctl get nvfd 2 uint 33 nvpar 0 9264 5272 if not nvos r npget 9260 5273 put nvfault 0 1 5274 ret neg long 1 5275 end 5276 npzero 5277 off = ulong 0 5278 while lt off size 5279 n = sub size off 5280 if gt n 16384 5281 n = ulong 16384 5282 end 5283 r = mapwrite m off nvpar 0 n 5284 off = add off n 5285 end 5286 put nvstage 0 26 5287 ret m 5288 end 5289 5290 note Register this private GPU virtual address space with UVM, no buffers or kernels. 5291 fn nvfull to bool 5292 if not nvstart 5293 ret false 5294 end 5295 put nvstage 0 11 5296 for i ulong 2 5297 r = fopen nvuvm 0 len nvuvm 524290 0 5298 if lt r 0 5299 put nverr 0 r 5300 ret false 5301 end 5302 put nvfd add i 2 int r 5303 end 5304 npzero 5305 r = uvmctl get nvfd 2 uint `805306369` nvpar 0 16 5306 if not nvos r npget 8 5307 ret false 5308 end 5309 put nvstage 0 12 5310 npzero 5311 npput 0 uint get nvfd 2 5312 r = uvmctl get nvfd 3 uint 75 nvpar 0 8 5313 if ne r 0 5314 put nverr 0 r 5315 ret false 5316 end 5317 note Driver platforms may return NV_WARN_NOTHING_TO_DO. Retain the returned status. 5318 put nvmm 0 npget 4 5319 if and ne get nvmm 0 0 ne get nvmm 0 65542 5320 put nverr 0 long get nvmm 0 5321 ret false 5322 end 5323 put nvstage 0 13 5324 npzero 5325 npp64 8 `562949953421311` 5326 v = nvalc get nvobj 1 112 24 5327 if eq v 0 5328 ret false 5329 end 5330 put nvobj 3 v 5331 put nvstage 0 14 5332 npzero 5333 npput 4 72 5334 npp64 8 `562949869535232` 5335 npp64 40 4096 5336 v = nvalc get nvobj 1 37105 56 5337 if eq v 0 5338 ret false 5339 end 5340 put nvobj 4 v 5341 put nvstage 0 15 5342 npzero 5343 npput 4 2 5344 npput 8 16 5345 if not nvctrl get nvobj 2 `545259850` 268 5346 ret false 5347 end 5348 if ne npget 8 16 5349 put nverr 0 neg long 22 5350 ret false 5351 end 5352 for i ulong 16 5353 put nvuuid i get nvpar add i 12 5354 end 5355 npuuid 5356 npput 24 `4294967295` 5357 r = uvmctl get nvfd 2 uint 37 nvpar 0 40 5358 if not nvos r npget 36 5359 ret false 5360 end 5361 put nvreg 0 1 5362 put nvstage 0 16 5363 npuuid 5364 npput 16 uint get nvfd 0 5365 npput 20 get nvobj 0 5366 npput 24 get nvobj 4 5367 r = uvmctl get nvfd 2 uint 25 nvpar 0 32 5368 if not nvos r npget 28 5369 ret false 5370 end 5371 put nvreg 1 1 5372 put nvstage 0 17 5373 ret true 5374 end 5375 5376 fn nvfind m long to ulong 5377 for i ulong len nvmmap 5378 if and gt m 0 eq get nvmmap i m 5379 ret i 5380 end 5381 end 5382 ret 96 5383 end 5384 note Reverse partial channel construction before releasing its buffers or address space. 5385 fn nvchstop to bool 5386 if ne get nvchreg 0 0 5387 npuuid 5388 npput 16 get nvobj 0 5389 npput 20 get nvobj 8 5390 r = uvmctl get nvfd 2 uint 28 nvpar 0 28 5391 if not nvos r npget 24 5392 put nvfault 0 1 5393 ret false 5394 end 5395 put nvchreg 0 0 5396 end 5397 for i ulong 4 5398 obj = sub 9 i 5399 parent = ulong 6 5400 if eq obj 9 5401 parent = ulong 8 5402 end 5403 if eq obj 6 5404 parent = ulong 1 5405 end 5406 if ne get nvobj obj 0 5407 if not nvfree get nvobj parent get nvobj obj 5408 put nvfault 0 1 5409 ret false 5410 end 5411 put nvobj obj 0 5412 end 5413 end 5414 if gt get nvchva 0 0 5415 r = mapdrop get nvchva 0 5416 if not nvos r 0 5417 put nvfault 0 1 5418 ret false 5419 end 5420 put nvchva 0 0 5421 end 5422 put nvsched 0 0 5423 put nvput 0 0 5424 put nvret 0 0 5425 put nvfifo 0 0 5426 put nvnotify 0 0 5427 ret true 5428 end 5429 5430 note Empty queue creation and scheduling; this routine submits no command packets. 5431 fn nvchan to bool 5432 if or eq get nvreg 1 0 ne get nvfault 0 0 5433 put nverr 0 neg long 16 5434 ret false 5435 end 5436 if or ne get nvobj 6 0 ne get nvobj 5 0 5437 put nverr 0 neg long 16 5438 ret false 5439 end 5440 put nvstage 0 31 5441 npzero 5442 u = nvalc get nvobj 2 50017 0 5443 if eq u 0 5444 ret false 5445 end 5446 put nvobj 5 u 5447 m = nvcpumap u 65536 false 0 5448 if le m 0 5449 ret false 5450 end 5451 put nvbar 0 m 5452 put nvstage 0 32 5453 npzero 5454 npput 12 1 5455 g = nvalc get nvobj 1 41068 20 5456 if eq g 0 5457 ret false 5458 end 5459 put nvobj 6 g 5460 put nvstage 0 33 5461 npzero 5462 npput 0 get nvobj 4 5463 npput 4 1 5464 c = nvalc g 36967 12 5465 if eq c 0 5466 ret false 5467 end 5468 put nvobj 7 c 5469 m = nvalloc 65536 false 5470 if le m 0 5471 ret false 5472 end 5473 put nvfifo 0 m 5474 m = nvalloc `1048576` true 5475 if le m 0 5476 ret false 5477 end 5478 put nvnotify 0 m 5479 put nvstage 0 34 5480 npzero 5481 npput 0 get nvmobj nvfind get nvnotify 0 5482 npput 4 get nvmobj nvfind get nvfifo 0 5483 npp64 8 mapaddr get nvfifo 0 5484 npput 16 1024 5485 npput 24 c 5486 npput 32 get nvmobj nvfind get nvfifo 0 5487 npp64 64 8192 5488 f = nvalc g 51567 368 5489 if eq f 0 5490 ret false 5491 end 5492 put nvobj 8 f 5493 put nvstage 0 35 5494 npzero 5495 put nvretry 0 0 5496 k = nvalc f 52928 0 5497 while eq k 0 5498 if or ne get nverr 0 81 ge get nvretry 0 50 5499 ret false 5500 end 5501 put nvretry 0 add get nvretry 0 1 5502 r = sleepns long `100000000` 5503 if ne r 0 5504 put nverr 0 r 5505 ret false 5506 end 5507 k = nvalc f 52928 0 5508 end 5509 put nvobj 9 k 5510 put nverr 0 0 5511 put nvstage 0 36 5512 npzero 5513 npput 0 `4294967295` 5514 if not nvctrl f `3278831880` 4 5515 ret false 5516 end 5517 put nvtoken 0 npget 0 5518 put nvstage 0 37 5519 if gt get nvnext 0 `1099444518912` 5520 put nverr 0 neg long 75 5521 ret false 5522 end 5523 v = maphold get nvnext 0 `67108864` 5524 if le v 0 5525 put nverr 0 v 5526 ret false 5527 end 5528 put nvchva 0 v 5529 put nvnext 0 add get nvnext 0 `67108864` 5530 npuuid 5531 npput 16 uint get nvfd 0 5532 npput 20 get nvobj 0 5533 npput 24 f 5534 npp64 32 mapaddr v 5535 npp64 40 `67108864` 5536 r = uvmctl get nvfd 2 uint 27 nvpar 0 56 5537 if not nvos r npget 48 5538 ret false 5539 end 5540 put nvchreg 0 1 5541 put nvstage 0 38 5542 npzero 5543 put nvpar 0 1 5544 if not nvctrl g `2691432705` 3 5545 ret false 5546 end 5547 put nvsched 0 1 5548 put nvstage 0 39 5549 ret true 5550 end 5551 5552 note One outstanding packet. Completion must be observed before reuse or any buffer release. 5553 fn nvsend m long off ulong words uint to bool 5554 if or ne get nvfault 0 0 eq get nvsched 0 0 5555 put nverr 0 neg long 5 5556 ret false 5557 end 5558 if ne get nvput 0 get nvret 0 5559 put nverr 0 neg long 16 5560 ret false 5561 end 5562 s = nvfind m 5563 if eq s 96 5564 put nverr 0 neg long 22 5565 ret false 5566 end 5567 if or or eq words 0 gt words `2097151` ne and off 3 0 5568 put nverr 0 neg long 22 5569 ret false 5570 end 5571 size = get nvmlen s 5572 if gt off size 5573 put nverr 0 neg long 22 5574 ret false 5575 end 5576 if gt mul ulong words 4 sub size off 5577 put nverr 0 neg long 22 5578 ret false 5579 end 5580 va = add get nvmaddr s off 5581 if ge va `1099511627776` 5582 put nverr 0 neg long 75 5583 ret false 5584 end 5585 p = get nvput 0 5586 if eq p `18446744073709551615` 5587 put nverr 0 neg long 75 5588 ret false 5589 end 5590 entry = or or va shl ulong words 42 `2199023255552` 5591 ret nvpush p entry 5592 end 5593 fn nvpush p ulong entry ulong to bool 5594 r = mapset get nvfifo 0 mul rem p 1024 8 8 entry 5595 p = add p 1 5596 put nvput 0 p 5597 r = mapset get nvfifo 0 8332 4 rem p 1024 5598 r = mapset get nvbar 0 144 4 ulong get nvtoken 0 5599 ret true 5600 end 5601 5602 fn nvwait m long off ulong value uint budget ulong to bool 5603 if or ne get nvfault 0 0 eq get nvsched 0 0 5604 put nverr 0 neg long 22 5605 ret false 5606 end 5607 if eq get nvput 0 get nvret 0 5608 put nverr 0 neg long 22 5609 ret false 5610 end 5611 s = nvfind m 5612 if or eq s 96 or eq budget 0 gt budget `30000000000` 5613 put nverr 0 neg long 22 5614 ret false 5615 end 5616 size = get nvmlen s 5617 if or gt off sub size 4 ne and off 3 0 5618 put nverr 0 neg long 22 5619 ret false 5620 end 5621 start = monons 5622 if lt start 0 5623 put nverr 0 start 5624 put nvfault 0 1 5625 ret false 5626 end 5627 while true 5628 if eq mapget m off 4 ulong value 5629 put nvret 0 get nvput 0 5630 ret true 5631 end 5632 now = monons 5633 if or lt now start ge sub now start long budget 5634 put nverr 0 neg long 110 5635 put nvfault 0 1 5636 ret false 5637 end 5638 r = sleepns long 100000 5639 if ne r 0 5640 put nverr 0 r 5641 put nvfault 0 1 5642 ret false 5643 end 5644 end 5645 ret false 5646 end 5647 note SPDX-License-Identifier: AGPL-3.0-only 5648 note Seed vecadd.s2 instruction forms, handwritten Omega emission of the same512bytes. 5649 note Each quad is one128-bit instruction. References: Omega57f5f425 seed/sm121. 5650 global gbcode uchar 4096 5651 global gblen ulong 1 5652 fn gbword v uint to void 5653 for j ulong 4 5654 put gbcode get gblen 0 uchar and shr v mul j 8 255 5655 put gblen 0 add get gblen 0 1 5656 end 5657 end 5658 fn gbquad a uint b uint c uint d uint to void 5659 gbword a 5660 gbword b 5661 gbword c 5662 gbword d 5663 end 5664 fn gbvec to void 5665 put gblen 0 0 5666 note Stack constant, block ID, block size, thread ID and element count. 5667 gbquad `4278287234` 57088 2048 `1040896` 5668 gbquad 620825 0 9472 926208 5669 gbquad `4278482860` 27648 `134219776` 926208 5670 gbquad 31001 0 8448 926208 5671 gbquad `4278548396` 29440 `134219776` 942592 5672 note Linear index and bounds predicate; inactive threads exit before any access. 5673 gbquad `151616548` 4 `260964864` `2083328` 5674 gbquad `151026700` 5 `200302704` `3136000` 5675 gbquad 2381 0 `58720256` `1042944` 5676 note Input/output addresses and memory descriptor from the constant bank. 5677 gbquad `4278352770` 57344 2560 926208 5678 gbquad `4278482860` 27392 `134220288` 945664 5679 gbquad `4278483842` 57856 2560 962560 5680 gbquad `4278614914` 58368 2560 975360 5681 note Address formation and independent input loads, retaining scheduling waits. 5682 gbquad `151156773` 4 `126746626` `2083840` 5683 gbquad `33716609` 4 `203299072` `3088896` 5684 gbquad `151287845` 4 `126746628` `5229568` 5685 gbquad `67467649` 4 `203299072` 991744 5686 gbquad `151418917` 4 `126746630` `9429504` 5687 note Unsigned32-bit addition, output store, exit and unreachable self-branch guard. 5688 gbquad `34173456` 5 `134209791` `17811968` 5689 gbquad `100694406` 9 `202381572` `1040896` 5690 gbquad 31053 0 `58720256` `1042944` 5691 gbquad `16546119` `4294967292` `58982399` `1032192` 5692 for i ulong 11 5693 gbquad 31000 0 0 `1032192` 5694 end 5695 end 5696 note SPDX-License-Identifier: AGPL-3.0-only 5697 note Native VECTOR_1D launch ABI continued from canonical CUDApp349913f1. 5698 note QMD, driver constant template and packet sequence: omega_blackwell_qmd.c/engine.c. 5699 global gbmem long 8 5700 global gbmax uint 1 5701 global gbq0 uint 96 5702 global gbq1 uint 96 5703 global gbcb uint 224 5704 global gbargs uint 7 5705 global gbcmd uchar 65536 5706 global gbused ulong 1 5707 fn gblo v ulong to uint 5708 ret uint and v `4294967295` 5709 end 5710 fn gbhi v ulong to uint 5711 ret uint shr v 32 5712 end 5713 fn gbemit w uint to void 5714 for i ulong 4 5715 put gbcmd get gbused 0 uchar and shr w mul i 8 255 5716 put gbused 0 add get gbused 0 1 5717 end 5718 end 5719 fn gbmth subc uint method uint count uint to void 5720 gbemit or or or uint `536870912` shl count 16 shl subc 13 shr method 2 5721 end 5722 fn gbptr va ulong to void 5723 gbemit gbhi va 5724 gbemit gblo va 5725 end 5726 fn gbdriver cb ulong to void 5727 for i ulong 224 5728 put gbcb i 0 5729 end 5730 note Nonzero words of the existing observed driver template, not a new inferred ABI. 5731 put gbcb 0 `641859584` 5732 put gbcb 1 3 5733 put gbcb 48 `2499697920` 5734 put gbcb 49 `3893478299` 5735 put gbcb 62 `1048575` 5736 put gbcb 64 `536870912` 5737 put gbcb 65 3 5738 put gbcb 66 `570425344` 5739 put gbcb 67 3 5740 put gbcb 68 gbhi cb 5741 put gbcb 69 gblo cb 5742 put gbcb 70 `604045312` 5743 put gbcb 71 3 5744 put gbcb 80 1 5745 put gbcb 82 `67158016` 5746 put gbcb 83 2 5747 put gbcb 91 1024 5748 put gbcb 108 `79311560` 5749 put gbcb 114 1 5750 put gbcb 118 288 5751 put gbcb 126 `632711424` 5752 put gbcb 127 3 5753 for i ulong 3 5754 put gbcb add 128 i 1 5755 put gbcb add 132 i `1065353216` 5756 end 5757 put gbcb 135 1024 5758 for i ulong 4 5759 put gbcb add 136 i 1 5760 end 5761 put gbcb 148 `79311560` 5762 put gbcb 154 1 5763 put gbcb 158 288 5764 put gbcb 166 `632711424` 5765 put gbcb 167 3 5766 for i ulong 3 5767 put gbcb add 168 i 1 5768 put gbcb add 172 i `1065353216` 5769 end 5770 put gbcb 175 1024 5771 for i ulong 4 5772 put gbcb add 176 i 1 5773 end 5774 put gbcb 180 48 5775 put gbcb 183 `16777216` 5776 put gbcb 185 59840 5777 put gbcb 189 59840 5778 put gbcb 190 `16777216` 5779 put gbcb 191 59839 5780 put gbcb 210 gbhi add cb 896 5781 put gbcb 211 gblo add cb 896 5782 put gbcb 212 gbhi add cb 924 5783 put gbcb 213 gblo add cb 924 5784 put gbcb 216 64 5785 put gbcb 217 1 5786 put gbcb 218 1 5787 put gbcb 220 1 5788 put gbcb 221 1 5789 put gbcb 222 1 5790 put gbcb 223 `16776640` 5791 end 5792 fn gbqmd n uint to void 5793 q = mapaddr get gbmem 2 5794 cb = mapaddr get gbmem 1 5795 code = mapaddr get gbmem 4 5796 scratch = add q 16384 5797 for i ulong 96 5798 put gbq0 i 0 5799 put gbq1 i 0 5800 end 5801 put gbq0 4 `8388610` 5802 put gbq0 10 `2293760` 5803 put gbq0 12 gblo shr add q 4096 8 5804 put gbq0 14 `5243392` 5805 put gbq0 34 65537 5806 put gbq0 35 2049 5807 put gbq1 4 `20905984` 5808 put gbq1 9 3 5809 put gbq1 10 `1638400` 5810 put gbq1 12 gblo shr q 8 5811 put gbq1 14 `793772964` 5812 put gbq1 15 gblo add q 8192 5813 put gbq1 16 gbhi add q 8192 5814 put gbq1 17 6 5815 put gbq1 19 `2172715008` 5816 put gbq1 20 56 5817 put gbq1 22 `67108864` 5818 put gbq1 32 gblo shr code 4 5819 put gbq1 33 or uint and shr code 36 `2097151` `20971520` 5820 put gbq1 34 65600 5821 put gbq1 35 135169 5822 put gbq1 36 `78923784` 5823 put gbq1 39 div add n 63 64 5824 put gbq1 40 1 5825 put gbq1 41 1 5826 put gbq1 42 gblo shr cb 6 5827 put gbq1 43 or uint and shr cb 38 524287 `33554432` 5828 put gbq1 44 gblo shr scratch 6 5829 put gbq1 45 or uint and shr scratch 38 524287 `75497472` 5830 put gbq1 52 gblo shr add cb 768 6 5831 put gbq1 53 or uint and shr add cb 768 38 524287 `8388608` 5832 put gbq1 56 gblo shr scratch 6 5833 put gbq1 57 or uint and shr scratch 38 524287 `2147483648` 5834 put gbq1 58 `271581201` 5835 put gbq1 59 gblo shr code 8 5836 gbdriver cb 5837 for i ulong 3 5838 va = mapaddr get gbmem add i 5 5839 put gbargs mul i 2 gblo va 5840 put gbargs add mul i 2 1 gbhi va 5841 end 5842 put gbargs 6 n 5843 end 5844 fn gbsetup to void 5845 gbemit `536944737` 5846 gbemit 52928 5847 gbemit `536944786` 5848 gbemit 1 5849 gbemit `536944808` 5850 gbemit 15 5851 gbemit `536946013` 5852 gbemit 3 5853 gbemit `536946014` 5854 gbemit `536870912` 5855 gbemit `536946015` 5856 gbemit `1048575` 5857 gbemit `536946007` 5858 gbemit 3 5859 gbemit `536946008` 5860 gbemit `570425344` 5861 gbemit `536946009` 5862 gbemit 0 5863 end 5864 fn gbdma va ulong count uint to void 5865 gbmth 1 392 2 5866 gbptr va 5867 gbmth 1 384 2 5868 gbemit mul count 4 5869 gbemit 1 5870 gbmth 1 432 1 5871 gbemit 65 5872 gbemit or or uint `1610621037` shl count 16 0 5873 end 5874 fn gbinline va ulong which bool to void 5875 gbmth 1 792 98 5876 gbemit or uint `1073741824` uint and shr va 40 511 5877 gbemit gblo shr va 8 5878 for i ulong 96 5879 if which 5880 gbemit get gbq1 i 5881 else 5882 gbemit get gbq0 i 5883 end 5884 end 5885 end 5886 fn gbrel va ulong value uint to void 5887 gbmth 0 92 5 5888 gbemit gblo va 5889 gbemit gbhi va 5890 gbemit value 5891 gbemit 0 5892 gbemit `1048577` 5893 end 5894 fn gbbuild n uint to void 5895 gbqmd n 5896 put gbused 0 0 5897 gbsetup 5898 cb = mapaddr get gbmem 1 5899 q = mapaddr get gbmem 2 5900 mark = mapaddr get gbmem 3 5901 gbdma cb 224 5902 for i ulong 224 5903 gbemit get gbcb i 5904 end 5905 gbdma add cb 896 7 5906 for i ulong 7 5907 gbemit get gbargs i 5908 end 5909 gbinline q false 5910 gbdma add q 8192 1 5911 gbemit 1 5912 gbinline add q 4096 true 5913 gbrel mark `1145324612` 5914 note C3: flush GPU L2 dirty lines, then a second WFI release before CPU readback. 5915 gbmth 0 40 4 5916 gbemit 0 5917 gbemit 0 5918 gbemit 0 5919 gbemit `2147483648` 5920 gbrel add mark 16 `1179010630` 5921 end 5922 fn gbopen n uint to bool 5923 if or eq n 0 gt n `1048576` 5924 put nverr 0 neg long 22 5925 ret false 5926 end 5927 if not nvfull 5928 ret false 5929 end 5930 if not nvchan 5931 ret false 5932 end 5933 put gbmax 0 n 5934 for i ulong 8 5935 size = ulong 4096 5936 if or eq i 0 eq i 2 5937 size = ulong 65536 5938 end 5939 if ge i 5 5940 size = mul ulong n 4 5941 end 5942 m = nvalloc size true 5943 if le m 0 5944 ret false 5945 end 5946 put gbmem i m 5947 end 5948 gbvec 5949 r = mapwrite get gbmem 4 0 gbcode 0 get gblen 0 5950 ret eq r long get gblen 0 5951 end 5952 fn gblaunch n uint to bool 5953 if or or eq n 0 gt n get gbmax 0 ne get nvfault 0 0 5954 put nverr 0 neg long 22 5955 ret false 5956 end 5957 put nvstage 0 40 5958 gbbuild n 5959 r = mapset get gbmem 2 8192 4 0 5960 r = mapset get gbmem 3 0 4 0 5961 r = mapset get gbmem 3 16 4 0 5962 r = mapwrite get gbmem 0 0 gbcmd 0 get gbused 0 5963 put nvstage 0 41 5964 if not nvsend get gbmem 0 0 uint div get gbused 0 4 5965 ret false 5966 end 5967 put nvstage 0 42 5968 if not nvwait get gbmem 3 16 `1179010630` `5000000000` 5969 ret false 5970 end 5971 put nvstage 0 43 5972 if ne mapget get gbmem 3 0 4 `1145324612` 5973 put nverr 0 neg long 5 5974 put nvfault 0 1 5975 ret false 5976 end 5977 if ne mapget get gbmem 2 8192 4 6 5978 put nverr 0 neg long 5 5979 put nvfault 0 1 5980 ret false 5981 end 5982 ret true 5983 end 5984 note SPDX-License-Identifier: AGPL-3.0-only 5985 note gk.om: general sm_121 launcher and f32 kernel emitters for the Omega LLM. 5986 note Needs context.om (CUDApp native/nvrm), vector.om and launch.om (CUDApp native/blackwell), vendored 5987 note unmodified from aien-dev/CUDApp `dcba8bb8` (last change `2e242169`) into native/omegallm/gpu/. 5988 note QMD words follow omega_blackwell_qmd.c; control words follow omega_blackwell_codegen_gemm.c. 5989 note Each kernel id owns a 16 KiB slot of gkcode (64 slots); gkthr/gkgpr/gksm describe its launch shape. 5990 global gkcode uchar `1048576` 5991 global gkat ulong 1 5992 global gkpend uint 1 5993 global gkthr uint 64 5994 global gkgpr uint 64 5995 global gksm uint 64 5996 global gkarg uint 32 5997 global gkna uint 1 5998 global gkmd uint 2 5999 fn gkw v uint to void 6000 at = get gkat 0 6001 put gkcode at uchar and v 255 6002 put gkcode add at 1 uchar and shr v 8 255 6003 put gkcode add at 2 uchar and shr v 16 255 6004 put gkcode add at 3 uchar shr v 24 6005 put gkat 0 add at 4 6006 end 6007 fn gkq a uint b uint c uint d uint to void 6008 gkw a 6009 gkw b 6010 gkw c 6011 gkw d 6012 end 6013 fn gkcw st uint wb uint rb uint wt uint to uint 6014 ret or or or shl st 9 shl wb 14 shl rb 17 shl wt 20 6015 end 6016 note Scheduling kinds: fixed, predicate, variable load, more loads, uniform load, store, more stores. 6017 fn gkfx a uint b uint c uint to void 6018 gkq a b c gkcw 6 7 7 get gkpend 0 6019 put gkpend 0 0 6020 end 6021 fn gkpr a uint b uint c uint to void 6022 gkq a b c gkcw 14 7 7 get gkpend 0 6023 put gkpend 0 0 6024 end 6025 fn gkvr a uint b uint c uint to void 6026 gkq a b c or gkcw 4 0 1 get gkpend 0 8192 6027 put gkpend 0 3 6028 end 6029 fn gkvm a uint b uint c uint to void 6030 gkq a b c or gkcw 4 0 1 and get gkpend 0 2 8192 6031 put gkpend 0 or get gkpend 0 3 6032 end 6033 fn gkvar a uint b uint c uint more bool to void 6034 if more 6035 gkvm a b c 6036 else 6037 gkvr a b c 6038 end 6039 end 6040 fn gkst a uint b uint c uint more bool to void 6041 w = get gkpend 0 6042 if more 6043 w = and w `4294967293` 6044 end 6045 gkq a b c or gkcw 4 7 1 w 8192 6046 put gkpend 0 or w 2 6047 end 6048 fn gksync to void 6049 gkq 31517 0 65536 gkcw 6 7 7 get gkpend 0 6050 put gkpend 0 0 6051 end 6052 fn gkexit to void 6053 gkq 31053 0 `58720256` or gkcw 5 7 7 get gkpend 0 8192 6054 put gkpend 0 0 6055 gkq `16546119` `4294967292` `58982399` `1032192` 6056 end 6057 note Instruction words, verified against nvdisasm -b SM121. 6058 fn gkldc d uint off uint more bool to void 6059 gkvar or `4278221698` shl d 16 shl div off 4 8 2048 more 6060 end 6061 fn gkldc64 d uint off uint to void 6062 gkvr or `4278221698` shl d 16 shl div off 4 8 2560 6063 end 6064 fn gks2r d uint sr uint more bool to void 6065 gkvar or 31001 shl d 16 0 shl sr 8 more 6066 end 6067 fn gkimadi d uint a uint imm uint c uint to void 6068 gkfx or or 30756 shl d 16 shl a 24 imm or `126747136` c 6069 end 6070 fn gkimad d uint a uint b uint c uint to void 6071 gkfx or or 29220 shl d 16 shl a 24 b or `126747136` c 6072 end 6073 fn gkimadw d uint a uint imm uint c uint to void 6074 gkfx or or 30757 shl d 16 shl a 24 imm or `126746624` c 6075 end 6076 fn gkimadh d uint a uint b uint c uint to void 6077 gkfx or or 29223 shl d 16 shl a 24 b or `126746624` c 6078 end 6079 fn gkshr d uint a uint n uint to void 6080 gkfx or `4278220825` shl d 16 n or 71168 a 6081 end 6082 fn gkandi d uint a uint imm uint to void 6083 gkfx or or 30738 shl d 16 shl a 24 imm `126796031` 6084 end 6085 fn gkmovi d uint imm uint to void 6086 gkfx or 30722 shl d 16 imm 3840 6087 end 6088 fn gkmovz d uint to void 6089 gkfx or 29186 shl d 16 255 3840 6090 end 6091 fn gkiadd d uint a uint b uint to void 6092 gkfx or or 29200 shl d 16 shl a 24 b `134209791` 6093 end 6094 note ISETP.GE P p = R a >= imm (signed); the destination predicate is word 2 bit 17, as in gkisetp 6095 fn gkisge p uint a uint imm uint to void 6096 gkpr or 30732 shl a 24 imm or `66085488` shl p 17 6097 end 6098 note ISETP P p = R a cc R b (signed; cc LT 1, EQ 2, LE 3, GT 4, NE 5, GE 6) 6099 fn gkisetp p uint a uint b uint cc uint to void 6100 gkpr or 29196 shl a 24 b or or `66060400` shl cc 12 shl p 17 6101 end 6102 note IMAD.WIDE.U32 R d:d+1 = R a * R b + R c:c+1 6103 fn gkimadwr d uint a uint b uint c uint to void 6104 gkfx or or 29221 shl d 16 shl a 24 b or `126746624` c 6105 end 6106 note @P p EXIT (no trailing self-branch: the block continues when P p is false) 6107 fn gkexitp p uint to void 6108 gkq or 2381 shl p 12 0 `58720256` or gkcw 5 7 7 get gkpend 0 8192 6109 put gkpend 0 0 6110 end 6111 note @P p LDG/STG: the guard predicate is bits 12-15 of word 0 (p 7 = PT, p + 8 negates), so the 6112 note unguarded forms below are p 7 and keep their words 6113 fn gkldgp d uint a uint off uint more bool p uint to void 6114 gkvar or or or 2433 shl p 12 shl d 16 shl a 24 or 4 shl off 8 `203299072` more 6115 end 6116 fn gkstgp a uint b uint off uint more bool p uint to void 6117 gkst or or 2438 shl p 12 shl a 24 or b shl off 8 `202381572` more 6118 end 6119 fn gkldg d uint a uint off uint more bool to void 6120 gkldgp d a off more 7 6121 end 6122 fn gkstg a uint b uint off uint more bool to void 6123 gkstgp a b off more 7 6124 end 6125 fn gksts a uint off uint b uint more bool to void 6126 gkst or 31112 shl a 24 or b shl off 8 `134220031` more 6127 end 6128 fn gklds d uint a uint off uint sb uint wt uint to void 6129 gkq or or 31108 shl d 16 shl a 24 shl off 8 3072 or gkcw 2 sb 7 wt 8192 6130 end 6131 note FFMA d = a b + c; ru is the operand reuse mask (bit 0 Ra, bit 1 Rb, bit 2 Rc; control bits 122-124). 6132 note A nonzero mask also sets control bit 109 (word-3 bit 13): nvdisasm decodes .reuse only with it set, 6133 note and ptxas (CUDA 13 sm_121) emits every .reuse instruction with it set. gkcw never sets it. 6134 fn gkffma d uint a uint b uint c uint wt uint ru uint to void 6135 y = uint 0 6136 if ne ru 0 6137 y = or shl ru 26 8192 6138 end 6139 gkq or or 29219 shl d 16 shl a 24 b c or gkcw 1 7 7 wt y 6140 end 6141 fn gkfmulf d uint a uint b uint to void 6142 gkfx or or 29216 shl d 16 shl a 24 b `4194304` 6143 end 6144 fn gkffmaf d uint a uint b uint c uint to void 6145 gkfx or or 29219 shl d 16 shl a 24 b c 6146 end 6147 fn gkbra p uint tgt ulong to void 6148 here = div get gkat 0 16 6149 d = uint and sub sub tgt here 1 `4294967295` 6150 w1 = shl shr d 6 2 6151 c = uint `58720256` 6152 if le tgt here 6153 w1 = or w1 `4026531840` 6154 c = `58982399` 6155 end 6156 gkq or or 2375 shl p 12 shl and d 63 18 w1 c gkcw 5 7 7 get gkpend 0 6157 put gkpend 0 0 6158 end 6159 note GEMM operand setup. Mode 0 (O): thread loads outer tid&31, depth tid>>5, step depth 2. 6160 note Mode 1 (K): outer tid>>4, depth tid&15, step outer 4. Args of operand ab start at word 8*ab: 6161 note ptr lo/hi, outer stride, depth stride, batch stride 1, batch stride 2, per-load delta, tile step. 6162 fn gkopnd mode uint blk uint ab uint to void 6163 o = add uint 896 mul ab 32 6164 if eq mode 0 6165 gkandi 11 0 31 6166 gkshr 12 0 5 6167 else 6168 gkshr 11 0 4 6169 gkandi 12 0 15 6170 end 6171 gkimadi 13 11 4 255 6172 gkimadi add 92 ab 12 144 13 6173 gkimadi 13 blk 32 11 6174 gkldc 14 add o 8 false 6175 gkldc 15 add o 12 true 6176 gkimad 13 13 14 255 6177 gkimad 13 12 15 13 6178 gkldc 14 add o 16 false 6179 gkldc 15 add o 20 true 6180 gkimad 13 6 14 13 6181 gkimad 13 7 15 13 6182 gkldc64 10 o 6183 gkimadw add 88 mul ab 2 13 1 10 6184 gkldc add 97 ab add o 24 false 6185 gkldc add 99 ab add o 28 true 6186 end 6187 note One k step of the tile: prefetch k+1 fragments into the other set, then 16 FFMA on set kk&1. 6188 note Set 0 is a R32-35 b R36-39 on SB3; set 1 is a R40-43 b R44-47 on SB4. Element k accumulates in 6189 note R16+k; the FFMAs of one row share a and carry gkru. 6190 fn gkfrag k uint a uint b uint w uint ru uint to void 6191 c = add 16 k 6192 gkffma c a b c w ru 6193 end 6194 fn gkstep kk uint to void 6195 s = and kk 1 6196 if lt kk 15 6197 o = sub 1 s 6198 gklds add 32 mul o 8 94 mul add kk 1 144 add 3 o 0 6199 gklds add 36 mul o 8 95 add 2304 mul add kk 1 144 add 3 o 0 6200 end 6201 w = shl uint 1 add 3 s 6202 ra = add 32 mul s 8 6203 k = uint 0 6204 for i uint 4 6205 for j uint 4 6206 gkfrag k add ra i add add ra 4 j w gkru j 3 6207 w = 0 6208 k = add k 1 6209 end 6210 end 6211 end 6212 note Causal attention (gkmm cz). gkczx: the block exits when its column tile R2 exceeds R3 + word 28 6213 note (word 28 is 0 when only C on and below the diagonal is needed, 2^30 otherwise). gkczk: K tile 6214 note count R kn = R3 word 26 + word 22, and both operand pointers (pa, pa + 2) start R3 word 27 tiles 6215 note in (tile steps st, st + 1). Dense: word 26 = word 27 = 0. Uses R12, R13 before the main loop. 6216 fn gkczx to void 6217 gkldc 12 1008 false 6218 gkiadd 12 12 3 6219 gkisetp 0 2 12 4 6220 gkexitp 0 6221 end 6222 fn gkczk kn uint st uint pa uint to void 6223 gkldc kn 984 false 6224 gkldc 12 1000 true 6225 gkldc 13 1004 true 6226 gkimad kn 3 12 kn 6227 gkimad 13 3 13 255 6228 gkimadwr pa 13 st pa 6229 gkimadwr add pa 2 13 add st 1 add pa 2 6230 end 6231 note One 32x32 tile of C per 64-thread block, 4x4 per thread, K tiles of 16 through shared memory. 6232 note C args: ptr words 16/17, row stride 18, col stride 19, batch strides 20/21; 22 ktiles (base), 6233 note 23 batch magic, 24 minus level-2 count, 25 alpha bits, 26-28 causal (gkczx, gkczk), 30/31 the 6234 note fused epilogue's second pointer (gkope). beta true adds the old C; ep is the fused epilogue. 6235 fn gkgemm ma uint mb uint beta bool ep uint to void 6236 put gkpend 0 0 6237 gkldc 1 892 false 6238 gks2r 0 33 true 6239 gks2r 2 37 true 6240 gks2r 3 38 true 6241 gks2r 5 39 true 6242 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 6243 put gkpend 0 4 6244 gkldc 8 988 false 6245 gkldc 9 992 true 6246 gkimadh 6 5 8 255 6247 gkimad 7 6 9 5 6248 gkczx 6249 gkshr 94 0 3 6250 gkimadi 94 94 16 255 6251 gkandi 95 0 7 6252 gkimadi 95 95 16 255 6253 gkopnd ma 3 0 6254 gkopnd mb 2 1 6255 gkczk 96 99 88 6256 gkmovi 101 `4294967295` 6257 for i uint 16 6258 gkmovz add 16 i 6259 end 6260 da = uint 288 6261 if eq ma 1 6262 da = 16 6263 end 6264 db = uint 288 6265 if eq mb 1 6266 db = 16 6267 end 6268 top = div get gkat 0 16 6269 put gkpend 0 63 6270 for r uint 8 6271 gkimadw add 56 mul r 2 97 r 88 6272 end 6273 for r uint 8 6274 gkimadw add 72 mul r 2 98 r 90 6275 end 6276 gkimadw 88 99 1 88 6277 gkimadw 90 100 1 90 6278 for r uint 8 6279 gkldg add 40 r add 56 mul r 2 0 ne r 0 6280 end 6281 for r uint 8 6282 gkldg add 48 r add 72 mul r 2 0 true 6283 end 6284 gksync 6285 for r uint 8 6286 gksts 92 mul r da add 40 r ne r 0 6287 end 6288 for r uint 8 6289 gksts 93 add 2304 mul r db add 48 r true 6290 end 6291 gksync 6292 gklds 32 94 0 3 0 6293 gklds 36 95 2304 3 0 6294 for kk uint 16 6295 gkstep kk 6296 end 6297 gkiadd 96 96 101 6298 gkisge 0 96 1 6299 gkbra 0 ulong top 6300 gkepi beta ep 6301 end 6302 note GEMM epilogue: C address of the thread's 4x4 block, alpha scale, optional old-C add, store. 6303 note Fused epilogue ep (gkope, beta false): 1 also stores Y = C > 0 ? C : 0 at the second pointer 6304 note (words 30/31, base R90), the FFN ReLU forward; 2 loads M at the second pointer's same offsets 6305 note into R40-55 and stores M > 0 ? C : 0, the FFN ReLU backward mask (NaN M gives 0). 6306 fn gkepi beta bool ep uint to void 6307 gkshr 12 0 3 6308 gkimadi 12 12 4 255 6309 gkimadi 12 3 32 12 6310 gkandi 13 0 7 6311 gkimadi 13 13 4 255 6312 gkimadi 13 2 32 13 6313 gkldc 14 968 false 6314 gkldc 15 972 true 6315 gkimad 12 12 14 255 6316 gkimad 12 13 15 12 6317 gkldc 10 976 false 6318 gkldc 11 980 true 6319 gkimad 12 6 10 12 6320 gkimad 12 7 11 12 6321 gkldc64 10 960 6322 gkimadw 88 12 1 10 6323 if ne ep 0 6324 gkldc64 10 1016 6325 gkimadw 90 12 1 10 6326 end 6327 gkldc 12 996 false 6328 if eq ep 2 6329 gkadr32 90 6330 for k uint 16 6331 gkldg add 40 k add 56 mul k 2 0 ne k 0 6332 end 6333 end 6334 gkadr32 88 6335 if beta 6336 for k uint 16 6337 gkldg add 40 k add 56 mul k 2 0 ne k 0 6338 end 6339 for k uint 16 6340 gkffmaf add 16 k add 16 k 12 add 40 k 6341 end 6342 else 6343 for k uint 16 6344 gkfmulf add 16 k add 16 k 12 6345 end 6346 end 6347 if eq ep 2 6348 t = gkthm 92 6349 for k uint 16 6350 gkepm add 16 k add 40 k t 6351 end 6352 end 6353 for k uint 16 6354 gkstg add 56 mul k 2 add 16 k 0 ne k 0 6355 end 6356 if eq ep 1 6357 for k uint 16 6358 gkepm add 16 k add 16 k 255 6359 end 6360 gkadr32 90 6361 for k uint 16 6362 gkstg add 56 mul k 2 add 16 k 0 ne k 0 6363 end 6364 end 6365 gkexit 6366 end 6367 note Address pairs R56-87 of the thread's 4x4 block (row stride R14, column stride R15) from base b. 6368 fn gkadr32 b uint to void 6369 for i uint 4 6370 r0 = add 56 mul i 8 6371 gkimadw r0 14 i b 6372 for j uint 3 6373 gkimadw add r0 mul add j 1 2 15 add j 1 r0 6374 end 6375 end 6376 end 6377 note Fused epilogue select: P2 = R m > R t, R e = P2 ? R e : 0 (FSETP.GT, FSEL; gr.om's encoders, 6378 note verified against nvdisasm there). t is RZ (255) except under mutant m. 6379 fn gkepm e uint m uint t uint to void 6380 grfsetp 2 m t 4 6381 grfsel e e 255 2 6382 end 6383 note Mutant m: 1 in the product, 0 in the mutant, whose fused backward mask (ep 2) is M > 2^-10 (the 6384 note forward and its stored FP unchanged). gkthm loads that threshold into R r and returns the 6385 note mask's compare register: RZ (255) in the product, r in the mutant. 6386 const OMUTM ulong 1 6387 fn gkthm r uint to uint 6388 if eq OMUTM 0 6389 gkmovi r grf 0.`0009765625f64` 6390 ret r 6391 end 6392 ret 255 6393 end 6394 note 64x64 GEMM: one 64x64 tile of C per 64-thread block, 8x8 per thread at rows ty*4+i and 32+ty*4+i, 6395 note columns tx*4+j and 32+tx*4+j (ty = tid>>3, tx = tid&7), K tiles of 8 through shared memory 6396 note (272-byte k rows, B at 2176). Each C element is still one FFMA chain over k in order, so results 6397 note equal the 32x32 kernel bit for bit. The K loop is software pipelined (k8): two 4352-byte shared 6398 note buffers; each tile first issues the global loads of the next tile (guarded by P1 = ktiles left 6399 note >= 2) into staging registers, runs its 64 FFMA steps from the current buffer, then stores the 6400 note staging into the other buffer behind one barrier. Registers: R16-79 accumulators; R80-111 6401 note fragments (set 0 a R80-87 b R88-95 on SB3, set 1 a R96-103 b R104-111 on SB4), later epilogue 6402 note addresses; R112-127 load addresses, later old C; R128/R130 operand pointers, R132/133 STS bases, 6403 note R134/135 LDS bases, R136 ktiles, R137/138 per-load delta, R139/140 tile step, R141 minus one, 6404 note later alpha; R142-149 A staging, R150-157 B staging; R158 buffer step (+4352 or -4352). 6405 note OMUTT: 1 in the product; 0 in mutant t, whose 64x64 kernels drop the k = 7 product of the thread's 6406 note C element (row 0, column 0). 6407 const OMUTT ulong 1 6408 note OKREU: 1 sets the Ra reuse bit (control bit 122, with bit 109) on each GEMM FFMA whose successor 6409 note reads the same a fragment; 0 (variant r0) leaves both clear as k6 did. It does not change the FFMA 6410 note chain of any C element, so both builds give the same bits. k7's window also measured a remap of C 6411 note column j to accumulator column j xor 1, which moves every k-loop b fragment and accumulator to 6412 note different low two index bits; it made no measured difference on sm_121 and was removed 6413 note (evidence/OMEGALLM/gpu-k7/). 6414 const OKREU ulong 1 6415 note gkru j last: the reuse operand mask of the FFMA at column j of a run that ends at column last. 6416 fn gkru j uint last uint to uint 6417 if and eq OKREU 1 lt j last 6418 ret 1 6419 end 6420 ret 0 6421 end 6422 note Mode 0 (O): thread loads outer tid, depths 0..7. Mode 1 (K): outer tid>>3 step 8, depth tid&7. 6423 fn gkopnd64 mode uint blk uint ab uint to void 6424 o = add uint 896 mul ab 32 6425 if eq mode 0 6426 gkandi 11 0 63 6427 gkmovz 12 6428 else 6429 gkshr 11 0 3 6430 gkandi 12 0 7 6431 end 6432 gkimadi 13 11 4 255 6433 gkimadi add 132 ab 12 272 13 6434 gkimadi 13 blk 64 11 6435 gkldc 14 add o 8 false 6436 gkldc 15 add o 12 true 6437 gkimad 13 13 14 255 6438 gkimad 13 12 15 13 6439 gkldc 14 add o 16 false 6440 gkldc 15 add o 20 true 6441 gkimad 13 6 14 13 6442 gkimad 13 7 15 13 6443 gkldc64 10 o 6444 gkimadw add 128 mul ab 2 13 1 10 6445 gkldc add 137 ab add o 24 false 6446 gkldc add 139 ab add o 28 true 6447 end 6448 fn gkstep64 kk uint to void 6449 s = and kk 1 6450 if lt kk 7 6451 o = sub 1 s 6452 d = add 80 mul o 16 6453 ko = mul add kk 1 272 6454 gklds d 134 ko add 3 o 0 6455 gklds add d 4 134 add ko 128 add 3 o 0 6456 gklds add d 8 135 add 2176 ko add 3 o 0 6457 gklds add d 12 135 add 2304 ko add 3 o 0 6458 end 6459 w = shl uint 1 add 3 s 6460 ra = add 80 mul s 16 6461 cut = and eq OMUTT 0 eq kk 7 6462 for i uint 8 6463 for j uint 8 6464 k = add mul i 8 j 6465 if or not cut ne k 0 6466 c = add 16 k 6467 u = gkru j 7 6468 gkffma c add ra i add add ra 8 j c w u 6469 w = 0 6470 end 6471 end 6472 end 6473 end 6474 note Load round q of a K tile into staging under predicate p (7 = PT): four A and four B loads (tile 6475 note loads 4q..4q+3); round 1 advances the operand pointers after its addresses are formed. Round 1 6476 note waits only for round 0's address reads. 6477 fn gkld64 q uint p uint to void 6478 if gt q 0 6479 put gkpend 0 2 6480 end 6481 l = mul q 4 6482 for r uint 4 6483 gkimadw add 112 mul r 2 137 add l r 128 6484 end 6485 for r uint 4 6486 gkimadw add 120 mul r 2 138 add l r 130 6487 end 6488 if gt q 0 6489 gkimadw 128 139 1 128 6490 gkimadw 130 140 1 130 6491 end 6492 for r uint 4 6493 gkldgp add 142 add l r add 112 mul r 2 0 ne r 0 p 6494 end 6495 for r uint 4 6496 gkldgp add 150 add l r add 120 mul r 2 0 true p 6497 end 6498 end 6499 fn gksts64 ma uint mb uint to void 6500 da = uint 272 6501 if eq ma 1 6502 da = 32 6503 end 6504 db = uint 272 6505 if eq mb 1 6506 db = 32 6507 end 6508 for r uint 8 6509 gksts 132 mul r da add 142 r ne r 0 6510 end 6511 for r uint 8 6512 gksts 133 add 2176 mul r db add 150 r true 6513 end 6514 end 6515 fn gkgemm64 ma uint mb uint beta bool ep uint to void 6516 put gkpend 0 0 6517 gkldc 1 892 false 6518 gks2r 0 33 true 6519 gks2r 2 37 true 6520 gks2r 3 38 true 6521 gks2r 5 39 true 6522 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 6523 put gkpend 0 4 6524 gkldc 8 988 false 6525 gkldc 9 992 true 6526 gkimadh 6 5 8 255 6527 gkimad 7 6 9 5 6528 gkczx 6529 gkshr 134 0 3 6530 gkimadi 134 134 16 255 6531 gkandi 135 0 7 6532 gkimadi 135 135 16 255 6533 gkopnd64 ma 3 0 6534 gkopnd64 mb 2 1 6535 gkczk 136 139 128 6536 gkmovi 141 `4294967295` 6537 for i uint 64 6538 gkmovz add 16 i 6539 end 6540 put gkpend 0 63 6541 gkld64 0 7 6542 gkld64 1 7 6543 gksts64 ma mb 6544 gksync 6545 gkmovi 158 4352 6546 gkiadd 132 132 158 6547 gkiadd 133 133 158 6548 top = div get gkat 0 16 6549 gkisge 1 136 2 6550 gklds 80 134 0 3 0 6551 gklds 84 134 128 3 0 6552 gklds 88 135 2176 3 0 6553 gklds 92 135 2304 3 0 6554 gkld64 0 1 6555 gkld64 1 1 6556 for kk uint 8 6557 gkstep64 kk 6558 end 6559 gksts64 ma mb 6560 gksync 6561 gkiadd 134 134 158 6562 gkiadd 135 135 158 6563 gkimad 158 158 141 255 6564 gkiadd 132 132 158 6565 gkiadd 133 133 158 6566 gkiadd 136 136 141 6567 gkisge 0 136 1 6568 gkbra 0 ulong top 6569 gkepi64 beta ep 6570 end 6571 note Accumulator register of element k (row k>>2, column k&3) of quadrant (rh, ch). 6572 fn gkacc64 rh uint ch uint k uint to uint 6573 ret add add 16 mul add mul rh 4 shr k 2 8 add mul ch 4 and k 3 6574 end 6575 note One 4x4 quadrant of the 64x64 epilogue: address pairs R80-111 from base R128 (+32 rows R10, 6576 note +32 columns R11), alpha R141, old C in R112-127 when beta, store. Fused epilogue ep as in gkepi, 6577 note second base R142 (quadrant base R144); ep 2 loads M into R112-127 (beta is false there). 6578 fn gkquad64 rh uint ch uint beta bool ep uint to void 6579 if eq ep 2 6580 gkqadr rh ch 142 144 6581 for k uint 16 6582 gkldg add 112 k add 80 mul k 2 0 ne k 0 6583 end 6584 end 6585 gkqadr rh ch 128 130 6586 if beta 6587 for k uint 16 6588 gkldg add 112 k add 80 mul k 2 0 ne k 0 6589 end 6590 end 6591 for k uint 16 6592 e = gkacc64 rh ch k 6593 if beta 6594 gkffmaf e e 141 add 112 k 6595 else 6596 gkfmulf e e 141 6597 end 6598 end 6599 if eq ep 2 6600 t = gkthm 140 6601 for k uint 16 6602 gkepm gkacc64 rh ch k add 112 k t 6603 end 6604 end 6605 for k uint 16 6606 e = gkacc64 rh ch k 6607 gkstg add 80 mul k 2 e 0 ne k 0 6608 end 6609 if eq ep 1 6610 for k uint 16 6611 e = gkacc64 rh ch k 6612 gkepm e e 255 6613 end 6614 gkqadr rh ch 142 144 6615 for k uint 16 6616 e = gkacc64 rh ch k 6617 gkstg add 80 mul k 2 e 0 ne k 0 6618 end 6619 end 6620 end 6621 note Address pairs R80-111 of quadrant (rh, ch) from thread base b (quadrant base in R t when moved). 6622 fn gkqadr rh uint ch uint b uint t uint to void 6623 qb = b 6624 if eq rh 1 6625 gkimadw t 10 1 b 6626 qb = t 6627 end 6628 if eq ch 1 6629 gkimadw t 11 1 qb 6630 qb = t 6631 end 6632 for i uint 4 6633 r0 = add 80 mul i 8 6634 gkimadw r0 14 i qb 6635 for j uint 3 6636 gkimadw add r0 mul add j 1 2 15 add j 1 r0 6637 end 6638 end 6639 end 6640 note 64x64 epilogue: C base of the thread (R128), second base R142 when fused, quadrant offsets 6641 note R10/R11, then four quadrants. 6642 fn gkepi64 beta bool ep uint to void 6643 gkshr 12 0 3 6644 gkimadi 12 12 4 255 6645 gkimadi 12 3 64 12 6646 gkandi 13 0 7 6647 gkimadi 13 13 4 255 6648 gkimadi 13 2 64 13 6649 gkldc 14 968 false 6650 gkldc 15 972 true 6651 gkimad 12 12 14 255 6652 gkimad 12 13 15 12 6653 gkldc 10 976 false 6654 gkldc 11 980 true 6655 gkimad 12 6 10 12 6656 gkimad 12 7 11 12 6657 gkldc64 10 960 6658 gkimadw 128 12 1 10 6659 if ne ep 0 6660 gkldc64 10 1016 6661 gkimadw 142 12 1 10 6662 end 6663 gkimadi 10 14 32 255 6664 gkimadi 11 15 32 255 6665 gkldc 141 996 false 6666 for h uint 4 6667 gkquad64 shr h 1 and h 1 beta ep 6668 end 6669 gkexit 6670 end 6671 note BF16 tensor-core GEMM (ids 27..36, docs/omegallm/CONTRACT-BF16.md): one 64x64 tile of C per 6672 note 128-thread block, K tiles of 32, f32 operands rounded to bf16 (round to nearest even) by F2FP while 6673 note staging into shared memory, HMMA.16816.F32.BF16 accumulating in f32, the f32 epilogue of gkepi64. 6674 note Words verified with nvdisasm -b SM121 against ptxas 13.0 sm_121 output for mma.sync.aligned 6675 note .m16n8k16.row.col.f32.bf16.bf16.f32, cvt.rn.bf16x2.f32 and ld.global.v4.f32. 6676 note HMMA d a b c: d, c and a 4-aligned, b 2-aligned. ptxas keeps 16 cycles between the HMMAs of a warp 6677 note (stall 15, then gknop of stall 1); a dependent HMMA comes 128 cycles later here. 6678 fn gkhmma d uint a uint b uint c uint wt uint to void 6679 gkq or or 29244 shl d 16 shl a 24 b or c 268288 gkcw 15 7 7 wt 6680 gknop 6681 end 6682 fn gknop to void 6683 gkq 31000 0 0 gkcw 1 7 7 0 6684 end 6685 note F2FP.BF16.F32.PACK_AB d a b: d = bf16(a) << 16 | bf16(b), round to nearest even; rz rounds toward 6686 note zero (mutant v only). Stall 2 as ptxas. 6687 const OMUTV ulong 1 6688 fn gkf2fp d uint a uint b uint wt uint to void 6689 c = uint 4351 6690 if eq OMUTV 0 6691 c = 102655 6692 end 6693 gkq or or 29246 shl d 16 shl a 24 b c or gkcw 2 7 7 wt 8192 6694 end 6695 note LDG.E.128 R d..d+3 = [R a:a+1] under predicate p (7 = PT) 6696 fn gkldgq d uint a uint more bool p uint to void 6697 gkvar or or or 2433 shl p 12 shl d 16 shl a 24 4 `203300096` more 6698 end 6699 note R d = ((tid >> s) & m) mult (+ R d unless first); R8 is the scratch. 6700 fn gkbt d uint s uint m uint mult uint first bool to void 6701 if eq s 0 6702 gkandi 8 0 m 6703 else 6704 gkshr 8 0 s 6705 gkandi 8 8 m 6706 end 6707 c = d 6708 if first 6709 c = 255 6710 end 6711 gkimadi d 8 mult c 6712 end 6713 note Operand ab of the BF16 tile. Mode 0 (O, outer stride 4): thread loads outers 4 (tid & 15) + 0..3, 6714 note depths 4 (tid >> 4) + j, j < 4 (one LDG.128 per depth, delta = depth stride). Mode 1 (K, depth 6715 note stride 4): outers (tid >> 3) + 16 i, i < 4 (delta 16 outer strides), depths 4 (tid & 7) + 0..3. 6716 note Pointer R112 + 2 ab, delta R137 + ab, tile step (32 depths) R139 + ab. 6717 fn gkopndb mode uint blk uint ab uint to void 6718 o = add uint 896 mul ab 32 6719 if eq mode 0 6720 gkandi 11 0 15 6721 gkimadi 11 11 4 255 6722 gkshr 12 0 4 6723 gkimadi 12 12 4 255 6724 else 6725 gkshr 11 0 3 6726 gkandi 12 0 7 6727 gkimadi 12 12 4 255 6728 end 6729 gkimadi 13 blk 64 11 6730 gkldc 14 add o 8 false 6731 gkldc 15 add o 12 true 6732 gkimad 13 13 14 255 6733 gkimad 13 12 15 13 6734 gkldc 14 add o 16 false 6735 gkldc 15 add o 20 true 6736 gkimad 13 6 14 13 6737 gkimad 13 7 15 13 6738 gkldc64 10 o 6739 gkimadw add 112 mul ab 2 13 1 10 6740 gkldc add 137 ab add o 24 false 6741 gkldc add 139 ab add o 28 true 6742 end 6743 note Shared memory, per 8192-byte buffer, k pair p = k >> 1 (0..15), outer r (0..63): A at 6744 note 2048 (p >> 3) + 512 (r >> 4) + 64 (r & 7) + 16 (p & 3) + 4 ((r >> 3) & 1) + 8 ((p >> 2) & 1), 6745 note B at 4096 + the same with the weights 4 and 8 exchanged, so that one LDS.128 at 512 j + 16 lane 6746 note (+ 2048 per k16 step) gives a lane its four A registers (rows g, g + 8; k 2t, 2t + 8) of m16 6747 note block j, or the two B registers of n8 blocks 2j and 2j + 1 (g = lane >> 2, t = lane & 3). 6748 note gkstsb: the thread's STS base R132 + ab for operand mode (o = 4 for A, 8 for B is the r-bit-3 weight). 6749 fn gkstsb mode uint ab uint to void 6750 d = add 132 ab 6751 ro = add 4 mul ab 4 6752 ko = sub 12 ro 6753 if eq mode 1 6754 gkbt d 2 1 2048 true 6755 gkbt d 3 7 64 false 6756 gkbt d 0 1 32 false 6757 gkbt d 6 1 ro false 6758 gkbt d 1 1 ko false 6759 else 6760 gkbt d 6 1 2048 true 6761 gkbt d 2 3 512 false 6762 gkbt d 0 1 256 false 6763 gkbt d 4 1 32 false 6764 gkbt d 1 1 ro false 6765 gkbt d 5 1 ko false 6766 end 6767 end 6768 note LDS bases: R134 = 1024 wm + 16 lane (A, warp row wm = tid >> 6), R135 = 1024 wn + 16 lane (B, warp 6769 note column wn = (tid >> 5) & 1). Mutant l (OMUTL 0) swaps the A rows of lanes g and g ^ 1. 6770 const OMUTL ulong 1 6771 fn gkldsb to void 6772 gkbt 134 6 1 1024 true 6773 gkbt 134 0 31 16 false 6774 if eq OMUTL 0 6775 gkbt 134 2 1 `4294967168` false 6776 gkmovi 8 64 6777 gkiadd 134 134 8 6778 end 6779 gkbt 135 5 1 1024 true 6780 gkbt 135 0 31 16 false 6781 end 6782 note Load a K tile into staging under predicate p: A in R80-95, B in R96-111 (addresses R116-131), 6783 note then advance both operand pointers by their tile steps. 6784 fn gkldb p uint to void 6785 for i uint 4 6786 gkimadw add 116 mul i 2 137 i 112 6787 end 6788 for i uint 4 6789 gkimadw add 124 mul i 2 138 i 114 6790 end 6791 gkimadw 112 139 1 112 6792 gkimadw 114 140 1 114 6793 for i uint 4 6794 gkldgq add 80 mul i 4 add 116 mul i 2 ne i 0 p 6795 end 6796 for i uint 4 6797 gkldgq add 96 mul i 4 add 124 mul i 2 true p 6798 end 6799 end 6800 note Packed word x (0..7) of a staged operand at s: mode 1 is (i, e) = (x >> 1, x & 1), the k pair e of 6801 note outer load i; mode 0 is (jp, c) = (x >> 2, x & 3), depth pair jp of outer c. gkcvd is the packed 6802 note register (in place), gkcvl the lower-k f32 register (the hi one follows it, + 1 or + 4), gkcvo 6803 note the STS offset. 6804 fn gkcvl md uint x uint s uint to uint 6805 if eq md 1 6806 ret add add s mul shr x 1 4 mul and x 1 2 6807 end 6808 ret add add s mul shr x 2 8 and x 3 6809 end 6810 fn gkcvd md uint x uint s uint to uint 6811 if eq md 1 6812 ret add add s mul shr x 1 4 and x 1 6813 end 6814 ret gkcvl md x s 6815 end 6816 fn gkcvo md uint x uint to uint 6817 if eq md 1 6818 ret add mul shr x 1 512 mul and x 1 16 6819 end 6820 ret add mul and x 3 64 mul shr x 2 16 6821 end 6822 note Round the staged tile to bf16 pairs (the first F2FP waits for the loads), then store it. 6823 fn gkcvsb ma uint mb uint to void 6824 w = get gkpend 0 6825 put gkpend 0 0 6826 for ab uint 2 6827 md = ma 6828 if eq ab 1 6829 md = mb 6830 end 6831 s = add 80 mul ab 16 6832 hd = uint 1 6833 if eq md 0 6834 hd = 4 6835 end 6836 for x uint 8 6837 l = gkcvl md x s 6838 gkf2fp gkcvd md x s add l hd l w 6839 w = 0 6840 end 6841 end 6842 for ab uint 2 6843 md = ma 6844 if eq ab 1 6845 md = mb 6846 end 6847 gkcvst md ab add 80 mul ab 16 6848 end 6849 end 6850 note gkcvsb's stores of operand ab (mode md, staged at register s) 6851 fn gkcvst md uint ab uint s uint to void 6852 for x uint 8 6853 gksts add 132 ab add gkcvo md x mul ab 4096 gkcvd md x s or ne x 0 ne ab 0 6854 end 6855 end 6856 note Mutant o (OMUTO 0) runs one K tile fewer than the tile count. 6857 const OMUTO ulong 1 6858 fn gkgemmb ma uint mb uint beta bool ep uint to void 6859 put gkpend 0 0 6860 gkldc 1 892 false 6861 gks2r 0 33 true 6862 gks2r 2 37 true 6863 gks2r 3 38 true 6864 gks2r 5 39 true 6865 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 6866 put gkpend 0 4 6867 gkldc 8 988 false 6868 gkldc 9 992 true 6869 gkimadh 6 5 8 255 6870 gkimad 7 6 9 5 6871 gkczx 6872 gkldsb 6873 gkstsb ma 0 6874 gkstsb mb 1 6875 gkopndb ma 3 0 6876 gkopndb mb 2 1 6877 gkczk 136 139 112 6878 gkmovi 141 `4294967295` 6879 if eq OMUTO 0 6880 gkiadd 136 136 141 6881 end 6882 for i uint 32 6883 gkmovz add 16 i 6884 end 6885 put gkpend 0 63 6886 gkldb 7 6887 gkcvsb ma mb 6888 gksync 6889 gkmovi 142 8192 6890 gkiadd 132 132 142 6891 gkiadd 133 133 142 6892 top = div get gkat 0 16 6893 gkisge 1 136 2 6894 for kk uint 2 6895 s = add 3 kk 6896 f = add 48 mul kk 16 6897 ko = mul kk 2048 6898 gklds f 134 ko s 0 6899 gklds add f 4 134 add ko 512 s 0 6900 gklds add f 8 135 add ko 4096 s 0 6901 gklds add f 12 135 add ko 4608 s 0 6902 end 6903 gkldb 1 6904 for kk uint 2 6905 gkmmak kk 6906 end 6907 gkcvsb ma mb 6908 gksync 6909 gkiadd 134 134 142 6910 gkiadd 135 135 142 6911 gkimad 142 142 141 255 6912 gkiadd 132 132 142 6913 gkiadd 133 133 142 6914 gkiadd 136 136 141 6915 gkisge 0 136 1 6916 gkbra 0 ulong top 6917 gkepib beta ep 6918 end 6919 note gkgemmb's eight HMMAs of k16 step kk (the first waits on barrier 3 + kk of the LDS loads) 6920 fn gkmmak kk uint to void 6921 w = shl uint 1 add 3 kk 6922 f = add 48 mul kk 16 6923 for mi uint 2 6924 for ni uint 4 6925 d = add add 16 mul mi 16 mul ni 4 6926 b = add add add f 8 mul shr ni 1 4 mul and ni 1 2 6927 gkhmma d add f mul mi 4 b d w 6928 w = 0 6929 end 6930 end 6931 end 6932 note Address pairs R48 + 2 e of the 16 elements e = 4 ni + c of one m16 half (row g + 8 (c >> 1), 6933 note column 8 ni + 2t + (c & 1)) from half base hb; row stride R14, column stride R15. 6934 fn gkhadrb hb uint to void 6935 gkimadw 52 14 8 hb 6936 gkimadw 48 15 0 hb 6937 for e uint 16 6938 if and ne e 0 ne e 2 6939 src = hb 6940 if ge and e 3 2 6941 src = 52 6942 end 6943 gkimadw add 48 mul e 2 15 add mul shr e 2 8 and e 1 src 6944 end 6945 end 6946 end 6947 note One m16 half mi of the thread's accumulators (R16 + 16 mi + e): ep 2 loads M into R80-95, beta 6948 note the old C; alpha R141; fused epilogue as gkquad64. Half bases: C R112 + 4 mi, second R114 + 4 mi. 6949 fn gkhalfb mi uint beta bool ep uint to void 6950 hc = add 112 mul mi 4 6951 hy = add 114 mul mi 4 6952 a0 = add 16 mul mi 16 6953 if eq ep 2 6954 gkhadrb hy 6955 for e uint 16 6956 gkldg add 80 e add 48 mul e 2 0 ne e 0 6957 end 6958 end 6959 gkhadrb hc 6960 if beta 6961 for e uint 16 6962 gkldg add 80 e add 48 mul e 2 0 ne e 0 6963 end 6964 end 6965 for e uint 16 6966 if beta 6967 gkffmaf add a0 e add a0 e 141 add 80 e 6968 else 6969 gkfmulf add a0 e add a0 e 141 6970 end 6971 end 6972 if eq ep 2 6973 t = gkthm 140 6974 for e uint 16 6975 gkepm add a0 e add 80 e t 6976 end 6977 end 6978 for e uint 16 6979 gkstg add 48 mul e 2 add a0 e 0 ne e 0 6980 end 6981 if eq ep 1 6982 for e uint 16 6983 gkepm add a0 e add a0 e 255 6984 end 6985 gkhadrb hy 6986 for e uint 16 6987 gkstg add 48 mul e 2 add a0 e 0 ne e 0 6988 end 6989 end 6990 end 6991 note Thread C base R112 (and second base R114 when fused): row 64 R3 + 32 wm + g, column 6992 note 64 R2 + 32 wn + 2t; half 1 bases R116 / R118 are 16 rows further. 6993 fn gkepib beta bool ep uint to void 6994 gkbt 12 6 1 32 true 6995 gkbt 12 2 7 1 false 6996 gkimadi 12 3 64 12 6997 gkbt 13 5 1 32 true 6998 gkbt 13 0 3 2 false 6999 gkimadi 13 2 64 13 7000 gkldc 14 968 false 7001 gkldc 15 972 true 7002 gkimad 12 12 14 255 7003 gkimad 12 13 15 12 7004 gkldc 10 976 false 7005 gkldc 11 980 true 7006 gkimad 12 6 10 12 7007 gkimad 12 7 11 12 7008 gkldc64 10 960 7009 gkimadw 112 12 1 10 7010 if ne ep 0 7011 gkldc64 10 1016 7012 gkimadw 114 12 1 10 7013 end 7014 gkimadw 116 14 16 112 7015 if ne ep 0 7016 gkimadw 118 14 16 114 7017 end 7018 gkldc 141 996 false 7019 for mi uint 2 7020 gkhalfb mi beta ep 7021 end 7022 gkexit 7023 end 7024 note Kernel table: ids 0..7 = 32x32 gemm (one 16 KiB slot each), ids 40, 42, ..., 54 = 64x64 gemm 7025 note (40 + 2 (ma + 2 mb + 4 beta), two slots each: the pipelined K loop does not fit in one). Fused 7026 note epilogue kernels (gkope, beta false): 25 (32x32) and 56 (64x64) ep 1 with ma 1 mb 0 (the FFN W1 7027 note forward, ggxw), 26 and 58 ep 2 with ma 1 mb 1 (the FFN dF backward, ggxwt). 7028 note gkgpr must be at least the highest GPR the kernel touches + 3: the hardware burns 2 GPRs of the 7029 note QMD count for the program counter (Mesa NAK sm70.rs hw_reserved_gprs), so R(n-2) and R(n-1) are 7030 note outside the allocation and a kernel using them hangs (launch wait -110). The 64x64 kernels use 7031 note R158 (buffer step), so 160 hung (evidence/OMEGALLM/gpu-k8 attempt 1); 168 is the next multiple of 8. 7032 note tests/omegallm/gpu-disasm.sh checks this rule per slot. 7033 fn gkslot id uint to void 7034 put gkat 0 mul ulong id 16384 7035 end 7036 fn gkall to bool 7037 for g uint 20 7038 id = g 7039 ma = and g 1 7040 mb = and shr g 1 1 7041 bt = ne and g 4 0 7042 ep = uint 0 7043 if ge g 16 7044 ep = add and g 1 1 7045 ma = 1 7046 mb = sub ep 1 7047 bt = false 7048 end 7049 big = and ge g 8 and ne g 16 ne g 17 7050 if ge g 8 7051 id = add 40 mul sub g 8 2 7052 end 7053 if ge g 16 7054 id = add 24 ep 7055 end 7056 if ge g 18 7057 id = add 54 mul ep 2 7058 end 7059 if not gkemit id ma mb bt ep big 7060 ret false 7061 end 7062 end 7063 for g uint 10 7064 ma = and g 1 7065 mb = and shr g 1 1 7066 bt = ne and g 4 0 7067 ep = uint 0 7068 if ge g 8 7069 ep = sub g 7 7070 ma = 1 7071 mb = sub ep 1 7072 bt = false 7073 end 7074 if not gkemitb add 27 g ma mb bt ep 7075 ret false 7076 end 7077 end 7078 ret true 7079 end 7080 note Emit the BF16 kernel (ma, mb, beta, ep) into slot id: ids 27 + ma + 2 mb + 4 beta, 35 ep 1 (ma 1 7081 note mb 0), 36 ep 2 (ma 1 mb 1); highest GPR R142, so 152 (multiple of 8 at least 145). 7082 fn gkemitb id uint ma uint mb uint bt bool ep uint to bool 7083 gkslot id 7084 gkgemmb ma mb bt ep 7085 put gkgpr id 152 7086 put gksm id 16384 7087 if gt get gkat 0 mul ulong add id 1 16384 7088 ret false 7089 end 7090 put gkthr id 128 7091 ret true 7092 end 7093 note Emit one GEMM kernel into slot id (two slots when big, the 64x64 kernel); false on overflow. 7094 fn gkemit id uint ma uint mb uint bt bool ep uint big bool to bool 7095 gkslot id 7096 ns = uint 1 7097 if big 7098 gkgemm64 ma mb bt ep 7099 put gkgpr id 168 7100 put gksm id 8704 7101 ns = 2 7102 else 7103 gkgemm ma mb bt ep 7104 put gkgpr id 104 7105 put gksm id 4608 7106 end 7107 if gt get gkat 0 mul ulong add id ns 16384 7108 ret false 7109 end 7110 put gkthr id 64 7111 ret true 7112 end 7113 note Launch: QMD for kernel id with grid gx gy gz; args gkarg[0..gkna). 7114 fn gkqq to void 7115 q = mapaddr get gbmem 2 7116 for i ulong 96 7117 put gbq0 i 0 7118 put gbq1 i 0 7119 end 7120 put gbq0 4 `8388610` 7121 put gbq0 10 `2293760` 7122 put gbq0 12 gblo shr add q 4096 8 7123 put gbq0 14 `5243392` 7124 put gbq0 34 65537 7125 put gbq0 35 2049 7126 put gbq1 4 `20905984` 7127 put gbq1 9 3 7128 put gbq1 10 `1638400` 7129 put gbq1 12 gblo shr q 8 7130 put gbq1 14 `793772964` 7131 put gbq1 15 gblo add q 8192 7132 put gbq1 16 gbhi add q 8192 7133 put gbq1 17 6 7134 put gbq1 19 `2172715008` 7135 put gbq1 20 56 7136 put gbq1 22 `67108864` 7137 put gbq1 58 `271581201` 7138 end 7139 fn gkqcb to void 7140 cb = mapaddr get gbmem 1 7141 scr = add mapaddr get gbmem 2 16384 7142 put gbq1 42 gblo shr cb 6 7143 put gbq1 43 or uint and shr cb 38 524287 `33554432` 7144 put gbq1 44 gblo shr scr 6 7145 put gbq1 45 or uint and shr scr 38 524287 `75497472` 7146 put gbq1 52 gblo shr add cb 768 6 7147 put gbq1 53 or uint and shr add cb 768 38 524287 `8388608` 7148 put gbq1 56 gblo shr scr 6 7149 put gbq1 57 or uint and shr scr 38 524287 `2147483648` 7150 gbdriver cb 7151 e = add cb add 896 mul ulong get gkna 0 4 7152 put gbcb 212 gbhi e 7153 put gbcb 213 gblo e 7154 end 7155 note QMD word 36 (QMDV05_00 bits 1152-1180): SHARED_MEMORY_SIZE_SHIFTED7, then the MIN (32 KiB, hw 9), 7156 note MAX (100 KiB, hw 26) and TARGET SM shared-memory configs, hw value = KiB / 4 + 1 from the GB10 7157 note carveouts 0 8 16 32 64 100. TARGET is the smallest carveout that holds the kernel's shared memory 7158 note times its register-limited blocks per SM, 65536 / (gkgpr * gkthr). Mesa NAK qmd.rs 7159 note gv100_get_hw_smem_sizes sizes TARGET the same way from warp-limited workgroups; the register limit is 7160 note the tighter bound here. A fixed 32 KiB TARGET under-sized the 32x32 GEMM (9 blocks * 4608 B) and the 7161 note pipelined 64x64 GEMM (6 * 8704 B): under it the pipelined kernel was 1.12x slower than the k7b one, 7162 note under this rule 0.91x (evidence/OMEGALLM/gpu-k8). Only ids with gkthr set may call this. 7163 fn gksmw id uint to uint 7164 sm = div add get gksm id 127 128 7165 if lt sm 8 7166 sm = 8 7167 end 7168 need = mul mul div 65536 mul get gkgpr id get gkthr id sm 128 7169 tg = uint 9 7170 if gt need 32768 7171 tg = 17 7172 end 7173 if gt need 65536 7174 tg = 26 7175 end 7176 ret or or `3426304` shl tg 23 sm 7177 end 7178 fn gkqmd id uint gx uint gy uint gz uint to void 7179 gkqq 7180 code = add mapaddr get gbmem 4 mul ulong id 16384 7181 put gbq1 32 gblo shr code 4 7182 put gbq1 33 or uint and shr code 36 `2097151` `20971520` 7183 put gbq1 59 gblo shr code 8 7184 put gbq1 34 or get gkthr id 65536 7185 put gbq1 35 or or 1 shl get gkgpr id 8 131072 7186 put gbq1 36 gksmw id 7187 put gbq1 39 gx 7188 put gbq1 40 gy 7189 put gbq1 41 gz 7190 gkqcb 7191 put gbcb 216 get gkthr id 7192 end 7193 note set after host writes to the arena; the next launch invalidates GPU L2 sysmem lines first 7194 global gkinv uint 1 7195 fn gkbuild id uint gx uint gy uint gz uint to void 7196 gkqmd id gx gy gz 7197 put gbused 0 0 7198 gbsetup 7199 if ne get gkinv 0 0 7200 note L2_SYSMEM_NCOH_INVALIDATE (clc96f.h:73, MEM_OP_D operation 0x11 in bits 31:27): drop L2 lines 7201 note the host rewrote in the arena; the previous launch ended with L2_FLUSH_DIRTY, so none is dirty 7202 gbmth 0 40 4 7203 gbemit 0 7204 gbemit 0 7205 gbemit 0 7206 gbemit `2281701376` 7207 put gkinv 0 0 7208 end 7209 cb = mapaddr get gbmem 1 7210 q = mapaddr get gbmem 2 7211 mark = mapaddr get gbmem 3 7212 gbdma cb 224 7213 for i ulong 224 7214 gbemit get gbcb i 7215 end 7216 na = get gkna 0 7217 gbdma add cb 896 na 7218 for i uint 32 7219 if lt i na 7220 gbemit get gkarg i 7221 end 7222 end 7223 gbinline q false 7224 gbdma add q 8192 1 7225 gbemit 1 7226 gbinline add q 4096 true 7227 gbrel mark `1145324612` 7228 gbmth 0 40 4 7229 gbemit 0 7230 gbemit 0 7231 gbemit 0 7232 gbemit `2147483648` 7233 gbrel add mark 16 `1179010630` 7234 end 7235 note Launch profile (gprof): while gkpf[0] is 1, each launch appends id gx gy gz m n k wait_ns to gkpl 7236 note (8 words, at most 1024 launches); gkmm sets gkps to its m n k first, other launches log zeros. 7237 global gkpf uint 1 7238 global gkpc ulong 1 7239 global gkpl ulong 8192 7240 global gkps uint 3 7241 fn gkplog id uint gx uint gy uint gz uint w long to void 7242 c = get gkpc 0 7243 if and eq get gkpf 0 1 lt c 1024 7244 o = mul c 8 7245 put gkpl o ulong id 7246 put gkpl add o 1 ulong gx 7247 put gkpl add o 2 ulong gy 7248 put gkpl add o 3 ulong gz 7249 put gkpl add o 4 ulong get gkps 0 7250 put gkpl add o 5 ulong get gkps 1 7251 put gkpl add o 6 ulong get gkps 2 7252 put gkpl add o 7 ulong w 7253 put gkpc 0 add c 1 7254 end 7255 put gkps 0 0 7256 put gkps 1 0 7257 put gkps 2 0 7258 end 7259 note launch accounting: 0 launches, 1 build ns, 2 wait ns (send to the QMD release semaphore), 7260 note 3 max wait ns 7261 global gkt long 4 7262 note wait for the release: spin up to 2 ms on the mark (vendored context.om nvwait sleeps 100 us 7263 note between polls, which cost each launch about 110-150 us in the rung-6 gprof ZERO round trips), 7264 note then hand the rest of the budget to nvwait, which keeps its checks, sleep and timeout 7265 fn gkwait m long off ulong value uint budget ulong to bool 7266 if eq get nvfault 0 0 7267 start = monons 7268 while lt sub monons start `2000000` 7269 if eq mapget m off 4 ulong value 7270 if ne get nvput 0 get nvret 0 7271 put nvret 0 get nvput 0 7272 ret true 7273 end 7274 end 7275 end 7276 end 7277 ret nvwait m off value budget 7278 end 7279 note wait for a word nvwait cannot (it refuses once nvret has caught up with nvput): spin 2 ms, 7280 note then poll every 100 us up to budget ns; a timeout sets nverr -110 and nvfault as nvwait does 7281 fn gkpoll m long off ulong value uint budget ulong to bool 7282 if ne get nvfault 0 0 7283 put nverr 0 neg long 22 7284 ret false 7285 end 7286 start = monons 7287 while true 7288 if eq mapget m off 4 ulong value 7289 ret true 7290 end 7291 el = sub monons start 7292 if or lt el 0 ge el long budget 7293 put nverr 0 neg long 110 7294 put nvfault 0 1 7295 ret false 7296 end 7297 if ge el `2000000` 7298 r = sleepns long 100000 7299 if ne r 0 7300 put nverr 0 r 7301 put nvfault 0 1 7302 ret false 7303 end 7304 end 7305 end 7306 ret false 7307 end 7308 note submit the built pushbuffer and wait for the launch to retire; returns the wait in ns, or -1 7309 fn gksend to long 7310 r = mapset get gbmem 2 8192 4 0 7311 r = mapset get gbmem 3 0 4 0 7312 r = mapset get gbmem 3 16 4 0 7313 n = get gbused 0 7314 r = mapwrite get gbmem 0 0 gbcmd 0 n 7315 put nvstage 0 41 7316 if ne r long n 7317 put nverr 0 neg long 5 7318 ret neg long 1 7319 end 7320 t1 = monons 7321 if not nvsend get gbmem 0 0 uint div n 4 7322 ret neg long 1 7323 end 7324 put nvstage 0 42 7325 if not gkwait get gbmem 3 16 `1179010630` `5000000000` 7326 ret neg long 1 7327 end 7328 put nvstage 0 43 7329 if ne mapget get gbmem 3 0 4 `1145324612` 7330 put nverr 0 neg long 5 7331 put nvfault 0 1 7332 ret neg long 1 7333 end 7334 note the QMD's own release semaphore is written after the grid retires and may land after the 7335 note second mark (omega_blackwell_engine.c be_wait_release_semaphore waits for it): wait, not peek 7336 put nvstage 0 44 7337 if not gkpoll get gbmem 2 8192 6 `1000000000` 7338 ret neg long 1 7339 end 7340 w = sub monons t1 7341 put gkt 2 add get gkt 2 w 7342 if gt w get gkt 3 7343 put gkt 3 w 7344 end 7345 ret w 7346 end 7347 fn gklaunch id uint gx uint gy uint gz uint to bool 7348 if or or or eq gx 0 eq gy 0 eq gz 0 ne get nvfault 0 0 7349 put nverr 0 neg long 22 7350 ret false 7351 end 7352 if or eq get gkna 0 0 gt get gkna 0 32 7353 put nverr 0 neg long 22 7354 ret false 7355 end 7356 put nvstage 0 40 7357 t0 = monons 7358 gkbuild id gx gy gz 7359 put gkt 0 add get gkt 0 1 7360 put gkt 1 add get gkt 1 sub monons t0 7361 w = gksend 7362 if lt w 0 7363 ret false 7364 end 7365 gkplog id gx gy gz w 7366 ret true 7367 end 7368 note Open the chip: slots 0 cmd, 1 cbank, 2 qmd, 3 marks, 4 code, 5 arena of asz bytes. 7369 fn gkopen asz ulong to bool 7370 if not nvfull 7371 ret false 7372 end 7373 if not nvchan 7374 ret false 7375 end 7376 for i ulong 6 7377 size = ulong 4096 7378 if or eq i 0 eq i 2 7379 size = 65536 7380 end 7381 if eq i 4 7382 size = `1048576` 7383 end 7384 if eq i 5 7385 size = asz 7386 end 7387 note slots 0-4 (pushbuffer, cbank, QMD, marks, code) are CPU-written or host-polled: GPU-uncached 7388 note as in omega_blackwell_engine.c alloc_checked; cached, a release sat in L2 about 1.06 s (cortex 7389 note `d03a999a`). The arena (slot 5) stays cached: gkinv invalidates it after host writes and every 7390 note launch ends with L2_FLUSH_DIRTY before its second mark. 7391 m = nvalloc size eq i 5 7392 if le m 0 7393 ret false 7394 end 7395 put gbmem i m 7396 end 7397 if not gkall 7398 put nverr 0 neg long 7 7399 ret false 7400 end 7401 r = mapwrite get gbmem 4 0 gkcode 0 `1048576` 7402 ret eq r long `1048576` 7403 end 7404 fn gkbase to ulong 7405 ret mapaddr get gbmem 5 7406 end 7407 note GEMM host side. Operand ab (0 = A, 1 = B): device address, outer/depth/batch strides in bytes. 7408 note Mode 1 (K) when the depth stride is 4; gkmm derives per-load delta and tile step for the tile. 7409 fn gkopa ab uint ptr ulong so uint sk uint s1 uint s2 uint to void 7410 o = mul ab 8 7411 put gkarg o gblo ptr 7412 put gkarg add o 1 gbhi ptr 7413 put gkarg add o 2 so 7414 put gkarg add o 3 sk 7415 put gkarg add o 4 s1 7416 put gkarg add o 5 s2 7417 md = uint 0 7418 if eq sk 4 7419 md = 1 7420 end 7421 put gkmd ab md 7422 end 7423 fn gkopc ptr ulong scm uint scn uint s1 uint s2 uint al f32 to void 7424 put gkarg 16 gblo ptr 7425 put gkarg 17 gbhi ptr 7426 put gkarg 18 scm 7427 put gkarg 19 scn 7428 put gkarg 20 s1 7429 put gkarg 21 s2 7430 put gkarg 25 bitcast uint al 7431 end 7432 note C[z] = alpha A[z] B[z] (+ C[z] if beta), z = z1 h2 + z2, z < nz; m n multiples of 32, k of 16. 7433 note m and n both multiples of 64 take the 64x64 kernels (ids 40..54 even), else the 32x32 ones (ids 0..7). 7434 fn gkshape m uint n uint k uint nz uint h2 uint to bool 7435 if ne rem m 32 0 7436 ret false 7437 end 7438 if ne rem n 32 0 7439 ret false 7440 end 7441 if or ne rem k 16 0 eq k 0 7442 ret false 7443 end 7444 if or eq nz 0 gt nz 65535 7445 ret false 7446 end 7447 ret ne h2 0 7448 end 7449 note Batch split magic: z1 = umulhi(z, mg) must equal z / h2 for every z < nz; else nverr -33. 7450 fn gkmagic nz uint h2 uint to uint 7451 mg = uint 0 7452 if gt h2 1 7453 mg = uint div add `4294967296` ulong sub h2 1 ulong h2 7454 end 7455 ok = true 7456 for z uint nz 7457 if ne uint shr mul ulong z ulong mg 32 div z h2 7458 ok = false 7459 end 7460 end 7461 if not ok 7462 put nverr 0 neg long 33 7463 end 7464 ret mg 7465 end 7466 note Per-load delta (word 6) and tile step (word 7) of operand ab for tile width t (32 or 64). 7467 fn gkopt ab uint t uint to void 7468 o = mul ab 8 7469 so = get gkarg add o 2 7470 sk = get gkarg add o 3 7471 gd = mul sk 2 7472 st = mul sk 16 7473 if eq t 64 7474 gd = sk 7475 st = mul sk 8 7476 end 7477 if eq get gkmd ab 1 7478 gd = mul so 4 7479 if eq t 64 7480 gd = mul so 8 7481 end 7482 end 7483 put gkarg add o 6 gd 7484 put gkarg add o 7 st 7485 end 7486 note Tile width: 64 when m and n are both multiples of 64, else 32. 7487 fn gktile m uint n uint to uint 7488 if and eq rem m 64 0 eq rem n 64 0 7489 ret 64 7490 end 7491 ret 32 7492 end 7493 note Causal mode cz, set by gkopz before gkmm (attention over a T x T score matrix, zero above its 7494 note diagonal); gkmm consumes it, so a GEMM without gkopz is dense. 0 dense; 1 A is 7495 note zero above its diagonal (C = P U, DQ = DS K), so row tile r stops after K column (r + 1) t; 7496 note 2 A is zero below its diagonal (C = P^T DO, DK = DS^T Q), so row tile r starts at K row r t; 7497 note 3 only C on and below the diagonal is needed (S = Q K^T, DP = DO U^T), so blocks above it exit. 7498 note Modes 1 and 2 need k = m, mode 3 needs m = n. Mutant q (OMUTQ 0) stops mode 1 one K tile short. 7499 const OMUTQ ulong 1 7500 note BF16 path (docs/omegallm/CONTRACT-BF16.md): gkbf 1 (gtrainb, gprofb and the gb.om gates) runs every 7501 note 64x64 gkmm on the tensor-core kernels 27..36; 32x32 GEMMs (n = E) stay f32. gkbck 1 checks each 7502 note BF16 launch against gb.om's sampled reference (gbpre before, gbpost after), with gkbp holding 7503 note m n k nz h2 beta ep cz. No fallback: a GEMM the kernels cannot take fails with nverr -22. 7504 global gkbf uint 1 7505 global gkbck uint 1 7506 global gkbp uint 8 7507 global gkcz uint 1 7508 fn gkopz cz uint to void 7509 put gkcz 0 cz 7510 end 7511 fn gkczok m uint n uint k uint cz uint to bool 7512 if eq cz 0 7513 ret true 7514 end 7515 if le cz 2 7516 ret eq k m 7517 end 7518 if eq cz 3 7519 ret eq m n 7520 end 7521 ret false 7522 end 7523 note GEMM K-loop words: 22 K tiles (kd deep), 26 ka, 27 kc, 28 X for tile t and the gkopz mode, 7524 note which it consumes 7525 fn gkczarg t uint k uint to void 7526 cz = get gkcz 0 7527 put gkcz 0 0 7528 kd = uint 16 7529 if eq t 64 7530 kd = 8 7531 if eq get gkbf 0 1 7532 kd = 32 7533 end 7534 end 7535 pt = div t kd 7536 put gkarg 22 div k kd 7537 put gkarg 26 0 7538 put gkarg 27 0 7539 put gkarg 28 `1073741824` 7540 if eq cz 1 7541 put gkarg 22 pt 7542 put gkarg 26 pt 7543 if eq OMUTQ 0 7544 put gkarg 22 sub pt 1 7545 end 7546 end 7547 if eq cz 2 7548 put gkarg 26 neg pt 7549 put gkarg 27 pt 7550 end 7551 if eq cz 3 7552 put gkarg 28 0 7553 end 7554 end 7555 note Fused epilogue ep for the next gkmm, which consumes it (see gkepi): 1 also stores 7556 note Y = C > 0 ? C : 0 at y, 2 stores M > 0 ? C : 0 with M read at y; y has C's strides. ep 1 needs 7557 note operand modes ma 1 mb 0 and ep 2 ma 1 mb 1 (the only fused kernels, gkall), both beta false. 7558 note Measured (k9, evidence/OMEGALLM/gpu-k9): replaces gr.om RELUF/RELUB, 16 launches per max-shape step 7559 note fewer; summed GPU wait 144.56 -> 133.54 ms/step (-7.62%, three rounds each, non-quiet); t200 checkpoint 7560 note bit-identical to the unfused k8 product. 7561 global gkep uint 1 7562 fn gkope ep uint y ulong to void 7563 put gkep 0 ep 7564 put gkarg 30 gblo y 7565 put gkarg 31 gbhi y 7566 end 7567 note true when fused epilogue ep may run with the pending operand modes and beta 7568 fn gkepok ep uint beta bool to bool 7569 if eq ep 0 7570 ret true 7571 end 7572 if or gt ep 2 beta 7573 ret false 7574 end 7575 if ne get gkmd 0 1 7576 ret false 7577 end 7578 ret eq get gkmd 1 sub ep 1 7579 end 7580 fn gkmm m uint n uint k uint nz uint h2 uint beta bool to bool 7581 ep = get gkep 0 7582 put gkep 0 0 7583 if not gkepok ep beta 7584 put gkcz 0 0 7585 put nverr 0 neg long 22 7586 ret false 7587 end 7588 if or not gkshape m n k nz h2 not gkczok m n k get gkcz 0 7589 put gkcz 0 0 7590 put nverr 0 neg long 22 7591 ret false 7592 end 7593 put nverr 0 0 7594 put gkarg 23 gkmagic nz h2 7595 if ne get nverr 0 0 7596 put gkcz 0 0 7597 ret false 7598 end 7599 put gkarg 24 neg h2 7600 t = gktile m n 7601 bf = and eq get gkbf 0 1 eq t 64 7602 if bf 7603 if not gkbfok k 7604 put gkcz 0 0 7605 put nverr 0 neg long 22 7606 ret false 7607 end 7608 else 7609 gkopt 0 t 7610 gkopt 1 t 7611 end 7612 put gkbp 7 get gkcz 0 7613 gkczarg t k 7614 put gkps 0 m 7615 put gkps 1 n 7616 put gkps 2 k 7617 id = gkmmid t beta ep 7618 if bf 7619 id = gkmmb beta ep 7620 end 7621 if not and bf eq get gkbck 0 1 7622 ret gklaunch id div n t div m t nz 7623 end 7624 gkbpset m n k nz h2 beta 7625 put gkbp 6 ep 7626 gbpre 7627 if not gklaunch id div n t div m t nz 7628 ret false 7629 end 7630 ret gbpost 7631 end 7632 note BF16 operands both take the 16-byte tile (gkbfop) and k is a multiple of the K tile 32 7633 fn gkbfok k uint to bool 7634 ba = gkbfop 0 7635 bb = gkbfop 1 7636 ret and and ba bb eq rem k 32 0 7637 end 7638 note gkbp words 0..5 for gb.om's check: m n k nz h2 beta 7639 fn gkbpset m uint n uint k uint nz uint h2 uint beta bool to void 7640 put gkbp 0 m 7641 put gkbp 1 n 7642 put gkbp 2 k 7643 put gkbp 3 nz 7644 put gkbp 4 h2 7645 put gkbp 5 0 7646 if beta 7647 put gkbp 5 1 7648 end 7649 end 7650 note Operand ab for the BF16 tile: 16-byte aligned pointer and batch strides, and 16-byte loads 7651 note (mode 1: so a multiple of 16; mode 0: so = 4, sk a multiple of 16); sets delta and tile step. 7652 fn gkbfop ab uint to bool 7653 o = mul ab 8 7654 so = get gkarg add o 2 7655 sk = get gkarg add o 3 7656 ok = eq rem get gkarg o 16 0 7657 ok = and ok eq rem get gkarg add o 4 16 0 7658 ok = and ok eq rem get gkarg add o 5 16 0 7659 if eq get gkmd ab 1 7660 ok = and ok eq rem so 16 0 7661 put gkarg add o 6 mul so 16 7662 else 7663 ok = and ok and eq so 4 eq rem sk 16 0 7664 put gkarg add o 6 sk 7665 end 7666 put gkarg add o 7 mul sk 32 7667 ret ok 7668 end 7669 note BF16 kernel id for beta and fused epilogue ep; sets the arg count 7670 fn gkmmb beta bool ep uint to uint 7671 if ne ep 0 7672 put gkna 0 32 7673 ret add 34 ep 7674 end 7675 put gkna 0 29 7676 id = add add 27 get gkmd 0 mul 2 get gkmd 1 7677 if beta 7678 id = add id 4 7679 end 7680 ret id 7681 end 7682 note kernel id of a gkmm (gkall's table) for tile t, beta and fused epilogue ep; sets the arg count 7683 fn gkmmid t uint beta bool ep uint to uint 7684 if ne ep 0 7685 put gkna 0 32 7686 if eq t 64 7687 ret add 54 mul ep 2 7688 end 7689 ret add 24 ep 7690 end 7691 put gkna 0 29 7692 id = add get gkmd 0 mul 2 get gkmd 1 7693 if beta 7694 id = add id 4 7695 end 7696 if eq t 64 7697 id = add 40 mul id 2 7698 end 7699 ret id 7700 end 7701 note SPDX-License-Identifier: AGPL-3.0-only 7702 note gr.om: row and elementwise SM121 kernels for the ollm GPU step (slots 9..24), emitted after 7703 note gkall with the model dimensions baked in as immediates. Words verified with nvdisasm -b SM121. 7704 note Launch shape: 64 threads per block, R4 = ctaid.x 64 + tid.x, R3 = ctaid.y, R7 = ctaid.z; 7705 note arg 0 is the R4 bound (threads at or past it exit). Registers: R8-R23 pointers and ints, 7706 note R24-R29 constants, R26 = 1, R30 loop counter, R31 = -1, R32-R63 data, R64-R69 scalars. 7707 global grd ulong 8 7708 note grd: 0 V 1 T 2 D 3 H 4 F 5 B 7709 fn grf v f64 to uint 7710 ret bitcast uint f32 v 7711 end 7712 fn grarg i uint to uint 7713 ret add 896 mul i 4 7714 end 7715 fn grfsetp p uint a uint b uint cc uint to void 7716 gkpr or 29195 shl a 24 b or or `66060288` shl cc 12 shl p 17 7717 end 7718 fn grfsel d uint a uint b uint p uint to void 7719 gkfx or or 29192 shl d 16 shl a 24 b shl p 23 7720 end 7721 fn grfmax d uint a uint b uint to void 7722 gkfx or or 29193 shl d 16 shl a 24 b `125829120` 7723 end 7724 fn grfadd d uint a uint b uint to void 7725 gkfx or or 29217 shl d 16 shl a 24 b 0 7726 end 7727 fn grfsub d uint a uint b uint to void 7728 gkfx or or 29217 shl d 16 shl a 24 or b `2147483648` 0 7729 end 7730 fn grmufu d uint a uint op uint to void 7731 gkvr or 29448 shl d 16 a op 7732 end 7733 note pointer number i (odd) sits in arg slots i+1, i+2 so its LDC.64 is 8-byte aligned 7734 fn grptr d uint i uint idx uint scale uint to void 7735 gkldc64 d grarg add i 1 7736 gkimadw d idx scale d 7737 end 7738 fn grtop c uint n uint to ulong 7739 gkmovi c n 7740 ret div get gkat 0 16 7741 end 7742 fn grbot c uint top ulong to void 7743 gkiadd c c 31 7744 gkisge 0 c 1 7745 gkbra 0 top 7746 end 7747 fn gradv p uint n uint to void 7748 gkimadw p 26 n p 7749 end 7750 fn grld8 d uint p uint first bool to void 7751 for u uint 8 7752 gkldg add d u p mul u 4 or ne u 0 not first 7753 end 7754 end 7755 fn grst8 p uint s uint to void 7756 for u uint 8 7757 gkstg p add s u mul u 4 ne u 0 7758 end 7759 end 7760 fn grpro to void 7761 gkldc 1 892 false 7762 gks2r 0 33 true 7763 gks2r 2 37 true 7764 gks2r 3 38 true 7765 gks2r 7 39 true 7766 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 7767 put gkpend 0 4 7768 gkimadi 4 2 64 0 7769 gkldc 5 896 false 7770 gkisetp 0 4 5 6 7771 gkexitp 0 7772 gkmovi 31 `4294967295` 7773 gkmovi 26 1 7774 end 7775 note 9 EMB: out[b T + t][i] = wte[x[b T + t]][i] + wpe[t][i]; grid (D/64, T, B); args D out wte wpe x 7776 fn gremb to void 7777 d = uint get grd 2 7778 t = uint get grd 1 7779 grpro 7780 gkimadi 8 7 t 3 7781 grptr 12 7 8 4 7782 gkldg 9 12 0 false 7783 gkimadi 10 9 d 4 7784 grptr 14 3 10 4 7785 gkimadi 11 3 d 4 7786 grptr 16 5 11 4 7787 gkimadi 20 8 d 4 7788 grptr 18 1 20 4 7789 gkldg 32 14 0 false 7790 gkldg 33 16 0 true 7791 grfadd 34 32 33 7792 gkstg 18 34 0 false 7793 gkexit 7794 end 7795 note RMS kernels: one warp per row (threads R*32, arg 0); lane l holds columns l + 32 u, u < D/32, 7796 note so each warp load and store is one coalesced 128-byte line and every row is read once. 7797 note R8 = row, R9 = lane; ld/st w: n words at p + 128 u (gkldg/gkstg chain flag: first false). 7798 fn grrrow to void 7799 grpro 7800 gkshr 8 4 5 7801 gkandi 9 4 31 7802 end 7803 fn grrptr d uint i uint d4 uint to void 7804 grptr d i 8 d4 7805 gkimadw d 9 4 d 7806 end 7807 fn grldw d uint p uint n uint first bool to void 7808 for u uint n 7809 gkldg add d u p mul u 128 or ne u 0 not first 7810 end 7811 end 7812 fn grstw p uint s uint n uint to void 7813 for u uint n 7814 gkstg p add s u mul u 128 ne u 0 7815 end 7816 end 7817 note RMS register bank k (k < 4) of n = D/32 words: R32 + 8 k while n <= 8 (the v1 layout, scalars at 7818 note R64..R67 above it); from n = 9 the banks sit above the scalars at R72 + n k, so D <= 384 (n <= 12) 7819 note ends at R119 and grall raises the slot's GPR count to 128 (gk.om: highest GPR + 3, multiple of 8). 7820 fn grrb k uint n uint to uint 7821 if le n 8 7822 ret add 32 mul 8 k 7823 end 7824 ret add 72 mul n k 7825 end 7826 note 10 RMSF: per row r < R: s = 1/sqrt(sum v^2 / D + 1e-5); y = v s g; args R*32 v g y s 7827 fn grrmsf to void 7828 d = uint get grd 2 7829 d4 = mul d 4 7830 n = div d 32 7831 grrrow 7832 gkmovi 24 grf div 1.0f64 f64 d 7833 gkmovi 25 grf 0.`00001f64` 7834 grrptr 10 1 d4 7835 gkldc64 14 grarg 4 7836 gkimadw 14 9 4 14 7837 b0 = grrb 0 n 7838 b1 = grrb 1 n 7839 grldw b0 10 n true 7840 grldw b1 14 n false 7841 gkmovz 64 7842 for u uint n 7843 gkffmaf 64 add b0 u add b0 u 64 7844 end 7845 grwred 0 0 7846 gkffmaf 65 64 24 25 7847 grmufu 66 65 5120 7848 grptr 12 7 8 4 7849 gkstg 12 66 0 false 7850 grrptr 16 5 d4 7851 for u uint n 7852 gkfmulf add b0 u add b0 u 66 7853 gkfmulf add b0 u add b0 u add b1 u 7854 end 7855 grstw 16 b0 n 7856 gkexit 7857 end 7858 note Mutant r: 1 in the product, 0 in the mutant, whose RMSB row sum sum(da g v) skips the lane-1 7859 note butterfly step (k1 from half the row), which gcmp2 must catch. 7860 const OMUTR ulong 1 7861 note 11 RMSB: per row: k1 = s^2 sum(da g v)/D; dh += (da g - v k1) s; w = da v s; args R*32 v g s da dh w 7862 fn grrmsb to void 7863 d = uint get grd 2 7864 d4 = mul d 4 7865 n = div d 32 7866 grrrow 7867 gkmovi 24 grf div 1.0f64 f64 d 7868 grrptr 10 1 d4 7869 gkldc64 12 grarg 4 7870 gkimadw 12 9 4 12 7871 grrptr 14 7 d4 7872 grrptr 16 9 d4 7873 grptr 20 5 8 4 7874 b0 = grrb 0 n 7875 b1 = grrb 1 n 7876 b2 = grrb 2 n 7877 b3 = grrb 3 n 7878 grldw b0 14 n true 7879 grldw b1 12 n false 7880 grldw b2 10 n false 7881 grldw b3 16 n false 7882 gkldg 67 20 0 true 7883 gkmovz 64 7884 for u uint n 7885 gkfmulf add b1 u add b0 u add b1 u 7886 gkffmaf 64 add b1 u add b2 u 64 7887 end 7888 if eq OMUTR 0 7889 grwred 0 1 7890 else 7891 grwred 0 0 7892 end 7893 gkfmulf 66 67 67 7894 gkfmulf 66 66 64 7895 gkfmulf 66 66 24 7896 grrptr 18 11 d4 7897 for u uint n 7898 gkfmulf add b0 u add b0 u add b2 u 7899 gkfmulf add b0 u add b0 u 67 7900 gkfmulf add b2 u add b2 u 66 7901 grfsub add b1 u add b1 u add b2 u 7902 gkfmulf add b1 u add b1 u 67 7903 grfadd add b3 u add b3 u add b1 u 7904 end 7905 grstw 16 b3 n 7906 grstw 18 b0 n 7907 gkexit 7908 end 7909 note 13/14 COLSUM: out[c ostr + i] (=, +=) sum_k in[c cstr + k kstr + i]; grid (n/64, chunks) 7910 note arg slots: 0 n, 1 kcnt (>= 1), 2-3 in, 4 kstr, 5 cstr, 6-7 out, 8 ostr (strides in bytes) 7911 fn grcol acc bool to void 7912 grpro 7913 grptr 10 1 4 4 7914 gkldc 20 grarg 5 false 7915 gkimadwr 10 3 20 10 7916 gkldc 21 grarg 4 false 7917 gkldc 30 grarg 1 false 7918 gkmovz 64 7919 top = div get gkat 0 16 7920 gkldg 32 10 0 false 7921 grfadd 64 64 32 7922 gkimadwr 10 26 21 10 7923 grbot 30 top 7924 grptr 12 5 4 4 7925 gkldc 22 grarg 8 false 7926 gkimadwr 12 3 22 12 7927 if acc 7928 gkldg 33 12 0 false 7929 grfadd 64 64 33 7930 end 7931 gkstg 12 64 0 false 7932 gkexit 7933 end 7934 note SHFL.BFLY PT, Rd, Ra, lane, 0x1f (codegen.c:671 SHFL.DOWN with the BFLY mode bits, sm121.tbl:183) 7935 fn grshfl d uint a uint lane uint to void 7936 gkvr or or 32649 shl d 16 shl a 24 or `201334528` shl lane 21 917504 7937 end 7938 note Softmax kernels: one warp per row; lane l holds columns l + 32 u, u < T/32, in R32.. (p) and 7939 note R48.. (dp), so every load and store of a warp is one coalesced 128-byte line. Threads: T 32 7940 note per score block (arg 0), grid (T/2, Z); R21 = row t, R22 = lane, R10/R12 = row base + 4 lane. 7941 fn grsrow tt uint dp bool to void 7942 grpro 7943 gkshr 21 4 5 7944 gkandi 22 4 31 7945 gkimadi 8 3 tt 21 7946 grptr 10 1 8 mul tt 4 7947 gkimadw 10 22 4 10 7948 if dp 7949 grptr 12 3 8 mul tt 4 7950 gkimadw 12 22 4 12 7951 end 7952 end 7953 note sum (op 0) or max (op 1) of R64 across the warp by butterfly, R65 scratch 7954 fn grwred op uint skip uint to void 7955 for i uint 5 7956 o = uint shr 16 i 7957 if ne o skip 7958 grshfl 65 64 o 7959 if eq op 0 7960 grfadd 64 64 65 7961 else 7962 grfmax 64 64 65 7963 end 7964 end 7965 end 7966 end 7967 note Mutant s: 1 in the product, 0 in the mutant, whose SOFTF row sum skips the lane-1 butterfly step 7968 note (each lane then normalizes by half the row), which gcmp and gcmp2 must catch. 7969 const OMUTS ulong 1 7970 note Causal chunks (k6): row t (R21) loads 32-column chunk u only if t >= 32 u (P u-1 for u 1..7; 7971 note chunk 0 and chunks from 8 on stay PT, loaded and masked), and stores chunk u only if 7972 note t >= 64 floor(u/2). The causal GEMMs read a P or DS row only up to the end of its diagonal tile 7973 note (32 or 64 columns), so later chunks are neither read nor written and keep stale arena data. 7974 note Predicates are set before the loads: an ISETP between them would wait on their scoreboard. 7975 note Mutant w: 1 in the product, 0 in the mutant, whose loads also skip the diagonal chunk (t >= 32 u + 32). 7976 const OMUTW ulong 1 7977 fn grcpred n uint off uint to void 7978 for u uint n 7979 if and ge u 1 le u 7 7980 gkisge sub u 1 21 add mul u 32 off 7981 end 7982 end 7983 end 7984 fn grcp u uint to uint 7985 if or eq u 0 ge u 8 7986 ret 7 7987 end 7988 ret sub u 1 7989 end 7990 fn grcwo to uint 7991 if eq OMUTW 0 7992 ret 32 7993 end 7994 ret 0 7995 end 7996 note 15 SOFTF: causal softmax of row t of score block z in place, zero above t; args T*32 P 7997 fn grsoftf to void 7998 tt = uint get grd 1 7999 n = div tt 32 8000 grsrow tt false 8001 gkmovi 27 `4286578688` 8002 gkmovi 28 grf 1.`4426950408889634f64` 8003 gkmovi 29 grf neg 1.`4426950408889634f64` 8004 gkmovi 23 32 8005 grcpred n grcwo 8006 for u uint n 8007 gkldgp add 32 u 10 mul u 128 ne u 0 grcp u 8008 end 8009 gkimadi 20 22 1 255 8010 gkmovi 64 `4286578688` 8011 for u uint n 8012 gkisetp 1 20 21 4 8013 grfsel add 32 u 27 add 32 u 1 8014 gkiadd 20 20 23 8015 grfmax 64 64 add 32 u 8016 end 8017 grwred 1 0 8018 gkfmulf 66 64 29 8019 gkmovz 64 8020 for u uint n 8021 gkffmaf add 32 u add 32 u 28 66 8022 grmufu add 32 u add 32 u 2048 8023 grfadd 64 64 add 32 u 8024 end 8025 if eq OMUTS 0 8026 grwred 0 1 8027 else 8028 grwred 0 0 8029 end 8030 grmufu 67 64 4096 8031 grcpred n 0 8032 for u uint n 8033 gkfmulf add 32 u add 32 u 67 8034 gkstgp 10 add 32 u mul u 128 ne u 0 grcp and u `4294967294` 8035 end 8036 gkexit 8037 end 8038 note 16 SOFTB: ds = p (dp - sum p dp) / sqrt(E) in place over dp; args T*32 P DP. p and dp above the 8039 note diagonal (column > t) read as 0: their chunks may be unloaded (k6) or stale (the causal DP GEMM, 8040 note gkmm cz 3, skips those tiles), and 0 times a stale Inf or NaN is NaN. 8041 fn grsoftb to void 8042 tt = uint get grd 1 8043 n = div tt 32 8044 e = div get grd 2 get grd 3 8045 grsrow tt true 8046 gkmovi 29 grf div 1.0f64 fsqrt f64 e 8047 grcpred n grcwo 8048 for u uint n 8049 gkldgp add 32 u 10 mul u 128 ne u 0 grcp u 8050 end 8051 for u uint n 8052 gkldgp add 48 u 12 mul u 128 true grcp u 8053 end 8054 gkmovi 23 32 8055 gkimadi 20 22 1 255 8056 for u uint n 8057 gkisetp 1 20 21 4 8058 grfsel add 48 u 255 add 48 u 1 8059 grfsel add 32 u 255 add 32 u 1 8060 gkiadd 20 20 23 8061 end 8062 gkmovz 64 8063 for u uint n 8064 gkffmaf 64 add 32 u add 48 u 64 8065 end 8066 grwred 0 0 8067 grcpred n 0 8068 for u uint n 8069 grfsub add 48 u add 48 u 64 8070 gkfmulf add 48 u add 48 u add 32 u 8071 gkfmulf add 48 u add 48 u 29 8072 gkstgp 12 add 48 u mul u 128 ne u 0 grcp and u `4294967294` 8073 end 8074 gkexit 8075 end 8076 note 19 COPY y = x; 20 ZERO; args n then pointers. The FFN ReLU forward and backward mask are fused 8077 note into the GEMM epilogue (gk.om gkepi, kernels 25/26/56/58), so slots 17/18 stay empty. 8078 fn grew kind uint to void 8079 grpro 8080 grptr 10 1 4 4 8081 if eq kind 20 8082 gkstg 10 255 0 false 8083 else 8084 grptr 12 3 4 4 8085 gkldg 32 10 0 false 8086 grfadd 34 32 255 8087 gkstg 12 34 0 false 8088 end 8089 gkexit 8090 end 8091 note 21 CE: per row r of logits L (R x V): m, e = exp(l - m), sum; L <- (e/sum - [c = y]) sc; 8092 note stats[r] = (m, sum, l_y); sc = 1/(T B); args R*32 L y st. One warp per row (grrrow): lane l 8093 note holds columns l + 32 u, read as 8 coalesced 128-byte lines per step of 256 columns. Pass 1 takes 8094 note the lane max, then the warp max. Pass 2 sums e per lane without storing it, then the warp sum 8095 note of the lane totals. Pass 3 recomputes e with the same FFMA and MUFU (bit-identical to pass 2), 8096 note scales it and stores it. The lane that owns column y (lane y & 31, the thread that stored it) 8097 note subtracts sc there and writes stats, so no lane reads another lane's store. l_y is loaded 8098 note before any store, and the next instruction waits for that load. 8099 note Each lane's sum is compensated (Kahan: R40 carries the lost low part, R41 y, R42 t) and the 8100 note lane total is s - c. A plain sequential f32 sum over V 4096 in one thread biased log sum low by 8101 note about 1.04e-5 relative on trained checkpoints (gdiag receipt). Mutant k (OMUTK 0) keeps the 8102 note plain per-lane sum. Mutant x (OMUTX 0) skips the lane-1 butterfly step of the warp sum, which 8103 note gcmp2 and geval must catch. Kernels launch with 72 GPRs (gkgpr in grall); Kahan state in 8104 note R69-R71 hung this kernel (launch wait -110, evidence/OMEGALLM/geval/a1), so it uses R40-R42. 8105 note The warp sum overwrites R64 and its scratch R65 (grwred), so R43 keeps m and pass 3 recomputes 8106 note R65 = -m log2e from it (the same FMUL as pass 2, so the same value). 8107 const OMUTK ulong 1 8108 const OMUTX ulong 1 8109 fn grce to void 8110 v = uint get grd 0 8111 v4 = mul v 4 8112 it = div v 256 8113 sc = div 1.0f64 f64 mul get grd 1 get grd 5 8114 grrrow 8115 gkmovi 28 grf 1.`4426950408889634f64` 8116 gkmovi 29 grf neg 1.`4426950408889634f64` 8117 gkmovi 25 grf sc 8118 grptr 12 3 8 4 8119 gkldg 20 12 0 false 8120 grptr 14 1 8 v4 8121 gkimadw 14 20 4 14 8122 gkldg 68 14 0 false 8123 grrptr 10 1 v4 8124 gkmovi 64 `4286578688` 8125 top = grtop 30 it 8126 grldw 32 10 8 true 8127 for u uint 8 8128 grfmax 64 64 add 32 u 8129 end 8130 gradv 10 1024 8131 grbot 30 top 8132 grwred 1 0 8133 grfadd 43 64 255 8134 gkfmulf 65 64 29 8135 grrptr 10 1 v4 8136 gkmovz 66 8137 gkmovz 40 8138 top2 = grtop 30 it 8139 grldw 32 10 8 true 8140 for u uint 8 8141 gkffmaf add 32 u add 32 u 28 65 8142 grmufu add 32 u add 32 u 2048 8143 if eq OMUTK 0 8144 grfadd 66 66 add 32 u 8145 end 8146 if ne OMUTK 0 8147 grfsub 41 add 32 u 40 8148 grfadd 42 66 41 8149 grfsub 66 42 66 8150 grfsub 40 66 41 8151 grfadd 66 42 255 8152 end 8153 end 8154 gradv 10 1024 8155 grbot 30 top2 8156 grfsub 64 66 40 8157 if eq OMUTX 0 8158 grwred 0 1 8159 else 8160 grwred 0 0 8161 end 8162 grmufu 67 64 4096 8163 gkfmulf 67 67 25 8164 gkfmulf 65 43 29 8165 grrptr 10 1 v4 8166 top3 = grtop 30 it 8167 grldw 32 10 8 true 8168 for u uint 8 8169 gkffmaf add 32 u add 32 u 28 65 8170 grmufu add 32 u add 32 u 2048 8171 gkfmulf add 32 u add 32 u 67 8172 end 8173 grstw 10 32 8 8174 gradv 10 1024 8175 grbot 30 top3 8176 gkandi 21 20 31 8177 gkisetp 1 9 21 5 8178 gkexitp 1 8179 gkldg 33 14 0 false 8180 grfsub 33 33 25 8181 gkstg 14 33 0 false 8182 grptr 16 5 8 12 8183 gkstg 16 43 0 false 8184 gkstg 16 64 4 true 8185 gkstg 16 68 8 true 8186 gkexit 8187 end 8188 note 22 CSRG: gw[c][i] += sum over k in [start[c], start[c+1]) of dh[ord[k]][i]; grid (D/64, V) 8189 note args D dh start ord gw (start: V + 1 uints, ord: rows sorted by token, ascending within a token) 8190 fn grcsr to void 8191 d = uint get grd 2 8192 grpro 8193 grptr 10 3 3 4 8194 gkldg 20 10 0 false 8195 gkldg 21 10 4 true 8196 gkisetp 1 20 21 2 8197 gkexitp 1 8198 gkimad 30 20 31 21 8199 grptr 12 5 20 4 8200 grptr 16 1 4 4 8201 gkmovz 64 8202 top = div get gkat 0 16 8203 gkldg 22 12 0 false 8204 gkimadw 14 22 mul d 4 16 8205 gkldg 32 14 0 false 8206 grfadd 64 64 32 8207 gradv 12 4 8208 grbot 30 top 8209 gkimadi 23 3 d 4 8210 grptr 18 7 23 4 8211 gkldg 33 18 0 false 8212 grfadd 64 64 33 8213 gkstg 18 64 0 false 8214 gkexit 8215 end 8216 note 23 SQN: out[c] = sum over i in [512 c, 512 c + 512) of g[i]^2 (f32); args nchunk g out. The 8217 note gradient buffer is padded with zeros to a multiple of 512 (ggplan, ggbwd), so no tail guard. 8218 fn grsqn to void 8219 grpro 8220 grptr 10 1 4 2048 8221 gkmovz 64 8222 top = grtop 30 64 8223 grld8 32 10 true 8224 for u uint 8 8225 gkffmaf 64 add 32 u add 32 u 64 8226 end 8227 gradv 10 32 8228 grbot 30 top 8229 grptr 12 3 4 4 8230 gkstg 12 64 0 false 8231 gkexit 8232 end 8233 note OMUTH: 1 in the product; 0 in mutant h (beta1 0.8 on chip), which gacmp must catch. 8234 const OMUTH ulong 1 8235 note 24 ADAM (f32, on chip): g = g c; m = b1 m + (1 - b1) g; v = b2 v + (1 - b2) g^2; 8236 note w -= lrb m / (sqrt(v ibc2) + 1e-8), the f32 image of og.om oadam1/oadam2 with lrb = lr/bc1 and 8237 note ibc2 = 1/bc2 from the host. Args P w m v g c lrb ibc2 (slots 0, 2-9, 10, 11, 12). 8238 fn gradam to void 8239 grpro 8240 b1 = 0.9f64 8241 if eq OMUTH 0 8242 b1 = 0.8f64 8243 end 8244 gkmovi 24 grf b1 8245 gkmovi 25 grf sub 1.0f64 0.9 8246 gkmovi 27 grf 0.99f64 8247 gkmovi 28 grf sub 1.0f64 0.99 8248 gkmovi 29 grf 0.`00000001f64` 8249 grptr 10 1 4 4 8250 grptr 12 3 4 4 8251 grptr 14 5 4 4 8252 grptr 16 7 4 4 8253 gkldc 64 grarg 10 false 8254 gkldc 65 grarg 11 true 8255 gkldc 66 grarg 12 true 8256 gkldg 32 16 0 true 8257 gkldg 33 12 0 true 8258 gkldg 34 14 0 true 8259 gkldg 35 10 0 true 8260 gkfmulf 32 32 64 8261 gkfmulf 33 33 24 8262 gkffmaf 33 32 25 33 8263 gkfmulf 36 32 32 8264 gkfmulf 34 34 27 8265 gkffmaf 34 36 28 34 8266 gkfmulf 37 34 66 8267 grmufu 38 37 8192 8268 grfadd 38 38 29 8269 grmufu 39 38 4096 8270 gkfmulf 40 33 65 8271 gkfmulf 40 40 39 8272 grfsub 35 35 40 8273 gkstg 10 35 0 false 8274 gkstg 12 33 0 true 8275 gkstg 14 34 0 true 8276 gkexit 8277 end 8278 note emit slots 9..24 (12, 17 and 18 empty) for dims V T D H F B; false when a kernel overflows its 16 KiB slot 8279 fn grset v ulong t ulong d ulong h ulong f ulong b ulong to void 8280 put grd 0 v 8281 put grd 1 t 8282 put grd 2 d 8283 put grd 3 h 8284 put grd 4 f 8285 put grd 5 b 8286 end 8287 fn grall to bool 8288 note the warp-per-row RMS kernels hold D/32 columns per lane (D <= 384: four banks end at R119, grrb), 8289 note softmax T/32, and CE steps 256 columns 8290 if or ne rem get grd 2 32 0 ne rem get grd 1 32 0 8291 ret false 8292 end 8293 if gt get grd 2 384 8294 ret false 8295 end 8296 if ne rem get grd 0 256 0 8297 ret false 8298 end 8299 for k uint 16 8300 id = add k 9 8301 gkslot id 8302 if eq id 9 8303 gremb 8304 end 8305 if eq id 10 8306 grrmsf 8307 end 8308 if eq id 11 8309 grrmsb 8310 end 8311 if eq id 13 8312 grcol false 8313 end 8314 if eq id 14 8315 grcol true 8316 end 8317 if eq id 15 8318 grsoftf 8319 end 8320 if eq id 16 8321 grsoftb 8322 end 8323 if and ge id 19 le id 20 8324 grew id 8325 end 8326 if eq id 21 8327 grce 8328 end 8329 if eq id 22 8330 grcsr 8331 end 8332 if eq id 23 8333 grsqn 8334 end 8335 if eq id 24 8336 gradam 8337 end 8338 if gt get gkat 0 mul ulong add id 1 16384 8339 ret false 8340 end 8341 put gkthr id 64 8342 put gkgpr id 72 8343 if and gt get grd 2 256 or eq id 10 eq id 11 8344 put gkgpr id 128 8345 end 8346 put gksm id 0 8347 end 8348 ret true 8349 end 8350 note open the chip with the row slots 9..24 for the given dims, then gkopen's GEMM slots, arena asz bytes 8351 fn gropen asz ulong to bool 8352 if not grall 8353 put nverr 0 neg long 7 8354 ret false 8355 end 8356 ret gkopen asz 8357 end 8358 note SPDX-License-Identifier: AGPL-3.0-only 8359 note gb.om: ollm BF16 tensor-core path, commands and gates (docs/omegallm/CONTRACT-BF16.md). With gkbf 1, 8360 note gk.om runs every 64x64 gkmm on kernels 27..36 (HMMA.16816.F32.BF16; the kernel rounds both operands 8361 note to bf16 RN with F2FP and accumulates in f32). Commands: gtrainb and gprofb (gtrainf and gprof with 8362 note gkbf 1), gbcmp (sampled check of every BF16 GEMM of one step, exit 48), gbcmp2 (gcmp2's step parity 8363 note with the BF16 bound, exit 49) and gbfrag (exact check of all ten kernels on fixed data, exit 50). 8364 note Concatenated before gg.om: it calls gg.om functions but uses none of gg.om's globals. 8365 8366 note OMUTU: 1 in the product; 0 in mutant u, whose reference skips the bf16 rounding (gbfrag must then 8367 note fail; gbcmp only reports it, CONTRACT-BF16 section 5). 8368 const OMUTU ulong 1 8369 8370 note OMUTN: 1 in the product; 0 in mutant n, the negative control of gbfrag's padding check: after each 8371 note short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, column n), so 8372 note only that check can fail (CONTRACT-BF16 section 4.1). 8373 const OMUTN ulong 1 8374 8375 note bf16 round to nearest even of the f32 bits u, as f32 bits (NaN unchanged) 8376 fn gbbf u uint to uint 8377 if eq OMUTU 0 8378 ret u 8379 end 8380 if and eq and shr u 23 255 255 ne and u `8388607` 0 8381 ret u 8382 end 8383 v = add add u 32767 and shr u 16 1 8384 ret and v `4294901760` 8385 end 8386 8387 note f32 bits x rounded to bf16, as f64 8388 fn gbrv x uint to f64 8389 ret f64 bitcast f32 gbbf x 8390 end 8391 8392 fn gbabs x f64 to f64 8393 if lt x 0.0f64 8394 ret neg x 8395 end 8396 ret x 8397 end 8398 8399 note arena word at host byte offset o (offset from gkbase) 8400 fn gbw o ulong to uint 8401 ret uint mapget get gbmem 5 o 4 8402 end 8403 8404 fn gbput o ulong x uint to void 8405 m = mapset get gbmem 5 o 4 ulong x 8406 end 8407 8408 note host byte offset of the device address held in arg words w, w + 1 8409 fn gbhp w uint to ulong 8410 lo = ulong get gkarg w 8411 hi = ulong get gkarg add w 1 8412 ret sub or lo shl hi 32 gkbase 8413 end 8414 8415 note batch offset z1 s1 + z2 s2 (z = z1 h2 + z2) with s1, s2 in arg words w, w + 1 8416 fn gbzo z uint h2 uint w uint to ulong 8417 a = mul ulong div z h2 ulong get gkarg w 8418 ret add a mul ulong rem z h2 ulong get gkarg add w 1 8419 end 8420 8421 note verdict line GBCMP (c 48), GBCMP2 (49) or GBFRAG (50): PASS, or FAIL and exit c 8422 fn gbvd c ulong ok bool to void 8423 oputc 71 8424 oputc 66 8425 if eq c 50 8426 oputc 70 8427 oputc 82 8428 oputc 65 8429 oputc 71 8430 else 8431 oputc 67 8432 oputc 77 8433 oputc 80 8434 if eq c 49 8435 oputc 50 8436 end 8437 end 8438 oputc 32 8439 if ok 8440 oputc 80 8441 oputc 65 8442 oputc 83 8443 oputc 83 8444 oputc 10 8445 oflush 8446 ret 8447 end 8448 oputc 70 8449 oputc 65 8450 oputc 73 8451 oputc 76 8452 oputc 10 8453 oflush 8454 if eq c 48 8455 exit 48 8456 end 8457 if eq c 49 8458 exit 49 8459 end 8460 exit 50 8461 end 8462 8463 note gbcmp state. gbst: 0 BF16 GEMMs checked, 1 samples past their bound, 2 kernel id mask, 3 cz mask, 8464 note 4 ep mask, 5 beta mask, 6 the sample generator. gbmx[0]: the worst |chip - ref| / bound. gbsp: the 8465 note 66 sampled (z, r, c) of the launch in flight, gbold the old C there (beta), gbacc the reference sums. 8466 global gbst ulong 8 8467 global gbmx f64 1 8468 global gbsp uint 198 8469 global gbold f64 66 8470 global gbacc f64 3 8471 8472 note next value in [0, n) of the sample generator (31-bit LCG) 8473 fn gbrnd n uint to uint 8474 s = rem add mul get gbst 6 `1103515245` 12345 `2147483648` 8475 put gbst 6 s 8476 ret uint rem shr s 8 ulong n 8477 end 8478 8479 note host byte offset at sample i in C (w 16) or in the epilogue's Y / M (w 30, C's strides) 8480 fn gbco i ulong w uint to ulong 8481 o = mul i 3 8482 z = get gbsp o 8483 b = add gbhp w gbzo z get gkbp 4 20 8484 b = add b mul ulong get gbsp add o 1 ulong get gkarg 18 8485 ret add b mul ulong get gbsp add o 2 ulong get gkarg 19 8486 end 8487 8488 note gkmm's hook before a checked BF16 launch (gkbp = m n k nz h2 beta ep cz): 64 random (z, r, c) 8489 note and the corners (0, 0, 0), (nz - 1, m - 1, n - 1); with cz 3 a position in a block above the 8490 note diagonal (not computed) is mirrored below it. Keeps the old C when beta. 8491 fn gbpre to void 8492 for i ulong 66 8493 gbpre1 i 8494 end 8495 end 8496 8497 note one sample i of gbpre 8498 fn gbpre1 i ulong to void 8499 z = gbrnd get gkbp 3 8500 r = gbrnd get gkbp 0 8501 c = gbrnd get gkbp 1 8502 if eq i 64 8503 z = 0 8504 r = 0 8505 c = 0 8506 end 8507 if eq i 65 8508 z = sub get gkbp 3 1 8509 r = sub get gkbp 0 1 8510 c = sub get gkbp 1 1 8511 end 8512 if eq get gkbp 7 3 8513 if gt div c 64 div r 64 8514 t = r 8515 r = c 8516 c = t 8517 end 8518 end 8519 o = mul i 3 8520 put gbsp o z 8521 put gbsp add o 1 r 8522 put gbsp add o 2 c 8523 put gbold i 0.0f64 8524 if eq get gkbp 5 1 8525 put gbold i f64 bitcast f32 gbw gbco i 16 8526 end 8527 end 8528 8529 note reference sums of sample i over the K range the kernel reads (cz 1: k < (r / 64 + 1) 64, 8530 note cz 2: k >= (r / 64) 64): gbacc 0 sum a b, 1 sum |a b|, 2 the term count; a, b rounded to bf16 8531 fn gbref i ulong to void 8532 r = ulong get gbsp add mul i 3 1 8533 k = ulong get gkbp 2 8534 lo = ulong 0 8535 hi = mul add div r 64 1 64 8536 cz = get gkbp 7 8537 if eq cz 1 8538 if lt hi k 8539 k = hi 8540 end 8541 end 8542 if eq cz 2 8543 lo = mul div r 64 64 8544 end 8545 gbdot gbsa i 0 gbsa i 1 lo sub k lo 8546 end 8547 8548 note host address of sample i's A row (w 0) or B column (w 1) at k = 0 8549 fn gbsa i ulong w uint to ulong 8550 o = mul i 3 8551 z = get gbsp o 8552 h2 = get gkbp 4 8553 g = mul w 8 8554 x = ulong get gbsp add o ulong add w 1 8555 p = add gbhp g gbzo z h2 add g 4 8556 ret add p mul x ulong get gkarg add g 2 8557 end 8558 8559 fn gbdot pa ulong pb ulong lo ulong kn ulong to void 8560 ka = ulong get gkarg 3 8561 kb = ulong get gkarg 11 8562 s = 0.0f64 8563 sa = 0.0f64 8564 for j ulong kn 8565 kk = add lo j 8566 p = mul gbrv gbw add pa mul kk ka gbrv gbw add pb mul kk kb 8567 s = add s p 8568 sa = add sa gbabs p 8569 end 8570 put gbacc 0 s 8571 put gbacc 1 sa 8572 put gbacc 2 f64 kn 8573 end 8574 8575 note d / b, 0 when both are 0; 1e9 when d > b with b = 0 or the ratio non-finite or past 1e9 8576 fn gbrat d f64 b f64 to f64 8577 if le d b 8578 if gt b 0.0f64 8579 ret div d b 8580 end 8581 ret 0.0f64 8582 end 8583 if gt b 0.0f64 8584 q = div d b 8585 if lt q `1000000000`.0f64 8586 ret q 8587 end 8588 end 8589 ret `1000000000`.0f64 8590 end 8591 8592 note |chip - ref| / bound at sample i, bound (kn 2^-23 + 2^-22)(|alpha| sum |a b| + |old C|); 1e9 8593 note for a wrong epilogue (ep 2: C must be +0 where M is not > 0; ep 1: Y = C when C > 0, else +0) 8594 fn gbchk i ulong to f64 8595 co = gbco i 16 8596 cb = gbw co 8597 ep = get gkbp 6 8598 if eq ep 2 8599 if not gt f64 bitcast f32 gbw gbco i 30 0.0f64 8600 if eq cb 0 8601 ret 0.0f64 8602 end 8603 ret `1000000000`.0f64 8604 end 8605 end 8606 if eq ep 1 8607 w = uint 0 8608 if gt f64 bitcast f32 cb 0.0f64 8609 w = cb 8610 end 8611 if ne gbw gbco i 30 w 8612 ret `1000000000`.0f64 8613 end 8614 end 8615 gbref i 8616 al = f64 bitcast f32 get gkarg 25 8617 old = get gbold i 8618 d = gbabs sub f64 bitcast f32 cb add mul al get gbacc 0 old 8619 e = add div get gbacc 2 `8388608`.0f64 div 1.0f64 `4194304`.0f64 8620 ret gbrat d mul e add mul gbabs al get gbacc 1 gbabs old 8621 end 8622 8623 note kernel id of the launch in flight (gk.om gkmmb) 8624 fn gbid to ulong 8625 ep = ulong get gkbp 6 8626 if ne ep 0 8627 ret add 34 ep 8628 end 8629 id = add 27 ulong get gkmd 0 8630 id = add id mul 2 ulong get gkmd 1 8631 ret add id mul 4 ulong get gkbp 5 8632 end 8633 8634 note gkmm's hook after a checked BF16 launch: check the samples, print 8635 note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict) 8636 fn gbpost to bool 8637 bad = ulong 0 8638 mr = 0.0f64 8639 for i ulong 66 8640 q = gbchk i 8641 if gt q 1.0f64 8642 bad = add bad 1 8643 end 8644 if gt q mr 8645 mr = q 8646 end 8647 end 8648 gbnote bad mr 8649 ret true 8650 end 8651 8652 fn gbnote bad ulong mr f64 to void 8653 id = gbid 8654 put gbst 0 add get gbst 0 1 8655 put gbst 1 add get gbst 1 bad 8656 put gbst 2 or get gbst 2 shl ulong 1 id 8657 put gbst 3 or get gbst 3 shl ulong 1 ulong get gkbp 7 8658 put gbst 4 or get gbst 4 shl ulong 1 ulong get gkbp 6 8659 put gbst 5 or get gbst 5 shl ulong 1 ulong get gkbp 5 8660 if gt mr get gbmx 0 8661 put gbmx 0 mr 8662 end 8663 oputc 71 8664 oputc 66 8665 oputc 32 8666 oputu id 8667 for j ulong 8 8668 oputc 32 8669 oputu ulong get gkbp j 8670 end 8671 oputc 32 8672 oputl rmic mr 8673 oputc 32 8674 oputu bad 8675 oputc 10 8676 end 8677 8678 note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so 8679 note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6 8680 note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound, 8681 note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta 8682 note false and true). 8683 fn gbcmp to void 8684 if ne oargc 5 8685 exit 2 8686 end 8687 ockload 2 8688 odopen 3 8689 obatch oargu 4 8690 put og 10 0 8691 put gbst 6 12345 8692 put gkbf 0 1 8693 put gkbck 0 1 8694 ggopen 8695 l = gggrad 8696 put gkbck 0 0 8697 r = nvstop 8698 oputc 71 8699 oputc 66 8700 oputc 67 8701 for j ulong 2 8702 oputc 32 8703 oputu get gbst j 8704 end 8705 oputc 32 8706 oputl rmic get gbmx 0 8707 for j ulong 4 8708 oputc 32 8709 oputu get gbst add j 2 8710 end 8711 oputc 10 8712 ok = and eq get gbst 1 0 gt get gbst 0 0 8713 cv = eq and get gbst 4 6 6 8714 cv = and cv ne and get gbst 3 8 0 8715 cv = and cv eq get gbst 5 3 8716 gbvd 48 and ok cv 8717 end 8718 8719 note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest 8720 note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV; 8721 note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic 8722 fn gbtolb to f64 8723 nc = f64 add mul 10 get og 5 3 8724 ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64 8725 end 8726 8727 note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss 8728 fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong 8729 tb = gbtolb 8730 fl = ulong 0 8731 if ne nf 0 8732 fl = or fl 1 8733 end 8734 if not lt ggclp rho tb 8735 fl = or fl 2 8736 end 8737 if not lt gg2rl 2 tb 8738 fl = or fl 4 8739 end 8740 if not lt gbabs sub lg l0 mul tb gbabs l0 8741 fl = or fl 8 8742 end 8743 ret fl 8744 end 8745 8746 note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p)) 8747 global gbcq f64 3 8748 fn gbcos p ulong to f64 8749 for k ulong 3 8750 put gbcq k 0.0f64 8751 end 8752 for q ulong ocn p 8753 i0 = mul q OCH 8754 for j ulong sub oce q p i0 8755 gbcq1 add i0 j p 8756 end 8757 end 8758 d = fsqrt mul get gbcq 1 get gbcq 2 8759 if gt d 0.0f64 8760 ret ggclp div get gbcq 0 d 8761 end 8762 ret 0.0f64 8763 end 8764 8765 fn gbcq1 i ulong p ulong to void 8766 x = omvg i 8767 y = omvg add p i 8768 put gbcq 0 add get gbcq 0 mul x y 8769 put gbcq 1 add get gbcq 1 mul x x 8770 put gbcq 2 add get gbcq 2 mul y y 8771 end 8772 8773 note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49) 8774 fn gbc2vd lg f64 l0 f64 fl ulong to void 8775 cs = gbcos get og 8 8776 oputc 71 8777 oputc 66 8778 oputc 50 8779 oputc 32 8780 oputu get og 5 8781 oputc 32 8782 oputu add mul 10 get og 5 3 8783 oputc 32 8784 oputl rmic mul gbtolb 1000.0f64 8785 oputc 32 8786 oputl rmic mul cs 1000.0f64 8787 oputc 32 8788 oputl rmic mul gg2rl 0 1000.0f64 8789 oputc 32 8790 oputl rmic mul gg2rl 2 1000.0f64 8791 oputc 32 8792 oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64 8793 oputc 10 8794 gbvd 49 eq fl 0 8795 end 8796 8797 note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd) 8798 fn gbcmp2 to void 8799 put gkbf 0 1 8800 ggcmp2 8801 end 8802 8803 note gtrainb: gtrainf with gkbf 1 (geval stays f32) 8804 fn gbtrain to void 8805 put gkbf 0 1 8806 ggtrainf 8807 end 8808 8809 note gprofb: gprof with gkbf 1 8810 fn gbprof to void 8811 put gkbf 0 1 8812 ggprof 8813 end 8814 8815 note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by 8816 note element against a reference built from the bf16-rounded operands. One operand holds integers in 8817 note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9 8818 note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen 8819 note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles 8820 note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the 8821 note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN 8822 note afterwards. Data in the logits buffer (A, B, C, Y at 0, 1, 2, 3 MiB). Prints `GF pass case id k cz ep bad` per case, 8823 note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL 8824 note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi). 8825 note gbcs: 0 ma, 1 mb (1 = K-contiguous), 2 beta, 3 ep, 4 cz, 5 k, 6 m, 7 n, 8 pass, 9 alpha 1/2 8826 global gbcs uint 10 8827 global gbfb ulong 1 8828 global gbha f64 98304 8829 global gbhb f64 147456 8830 8831 fn gbfrag to void 8832 if ne oargc 3 8833 exit 2 8834 end 8835 ockload 2 8836 ggopen 8837 if lt mul ggr get og 0 `4194304` 8838 ggdie 3 8839 end 8840 put gkbf 0 1 8841 put gkbck 0 0 8842 put gbfb 0 sub gga1 11 gkbase 8843 bad = ulong 0 8844 for p ulong 2 8845 for i ulong 13 8846 bad = add bad gbf1 i p 8847 end 8848 end 8849 for p ulong 2 8850 for q uint 4 8851 bad = add bad gbfl p and q 1 shr q 1 8852 end 8853 end 8854 r = nvstop 8855 oputc 71 8856 oputc 70 8857 oputc 83 8858 oputc 32 8859 oputu bad 8860 oputc 10 8861 gbvd 50 eq bad 0 8862 end 8863 8864 note case i of pass p into gbcs 8865 fn gbcase i ulong p ulong to void 8866 for j ulong 10 8867 put gbcs j 0 8868 end 8869 put gbcs 8 uint p 8870 put gbcs 6 128 8871 put gbcs 7 192 8872 put gbcs 5 128 8873 if eq rem i 3 0 8874 put gbcs 5 32 8875 end 8876 if eq rem i 3 1 8877 put gbcs 5 96 8878 put gbcs 9 1 8879 end 8880 if lt i 8 8881 put gbcs 0 uint and i 1 8882 put gbcs 1 uint and shr i 1 1 8883 put gbcs 2 uint and shr i 2 1 8884 ret 8885 end 8886 put gbcs 9 0 8887 put gbcs 0 1 8888 if eq i 8 8889 put gbcs 3 1 8890 put gbcs 5 96 8891 end 8892 if eq i 9 8893 put gbcs 1 1 8894 put gbcs 3 2 8895 end 8896 note cz 1 needs k = m 8897 if eq i 10 8898 put gbcs 4 1 8899 put gbcs 5 128 8900 end 8901 if eq i 11 8902 put gbcs 0 0 8903 put gbcs 2 1 8904 put gbcs 4 2 8905 end 8906 if eq i 12 8907 put gbcs 1 1 8908 put gbcs 4 3 8909 put gbcs 5 32 8910 put gbcs 7 128 8911 put gbcs 9 1 8912 end 8913 end 8914 8915 note one case: write the data, launch, compare; the mismatch count 8916 fn gbf1 i ulong p ulong to ulong 8917 gbcase i p 8918 gbnan 8919 gbwop 0 8920 gbwop 1 8921 gbwc 8922 put gkinv 0 1 8923 if not gbflch 8924 ggdie 98 8925 end 8926 if eq OMUTN 0 8927 n = ulong get gbcs 7 8928 gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0 8929 end 8930 b = gbfcmp 8931 ep = ulong get gbcs 3 8932 id = add 27 ulong get gbcs 0 8933 id = add id mul 2 ulong get gbcs 1 8934 id = add id mul 4 ulong get gbcs 2 8935 if ne ep 0 8936 id = add 34 ep 8937 end 8938 oputc 71 8939 oputc 70 8940 oputc 32 8941 oputu p 8942 oputc 32 8943 oputu i 8944 oputc 32 8945 oputu id 8946 oputc 32 8947 oputu ulong get gbcs 5 8948 oputc 32 8949 oputu ulong get gbcs 4 8950 oputc 32 8951 oputu ep 8952 oputc 32 8953 oputu b 8954 oputc 10 8955 ret b 8956 end 8957 8958 note NaN in every A and B word (2 MiB) 8959 fn gbnan to void 8960 b = get gbfb 0 8961 for i ulong 524288 8962 gbput add b mul i 4 `2143289344` 8963 end 8964 end 8965 8966 note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B); 8967 note batch z at (z / 3)(3 zs + 64) + (z % 3) zs 8968 fn gbzs md uint w ulong kd ulong to ulong 8969 if eq md 1 8970 ret mul w add kd 8 8971 end 8972 ret mul kd add w 16 8973 end 8974 8975 fn gbzof z ulong zs ulong to ulong 8976 ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs 8977 end 8978 8979 fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong 8980 if eq md 1 8981 ret add mul x add kd 8 kk 8982 end 8983 ret add x mul kk add w 16 8984 end 8985 8986 note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z) 8987 fn gbfr x ulong kk ulong z ulong to f64 8988 s = add mul x 7 mul kk 3 8989 s = add s mul z 5 8990 a = sub f64 rem s 5 2.0f64 8991 s = add mul x 13 mul kk 11 8992 s = add s mul z 3 8993 b = f64 rem s 64 8994 ret add a div b 512.0f64 8995 end 8996 8997 fn gbint x ulong kk ulong z ulong to f64 8998 s = add mul x 5 mul kk 7 8999 s = add s mul z 3 9000 ret sub f64 rem s 11 5.0f64 9001 end 9002 9003 note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded 9004 note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped. 9005 fn gbwop ab uint to void 9006 md = get gbcs ab 9007 k = ulong get gbcs 5 9008 w = ulong get gbcs add 6 ab 9009 zs = gbzs md w k 9010 base = add get gbfb 0 mul ulong ab `1048576` 9011 fr = eq get gbcs 8 ab 9012 for z ulong 6 9013 for x ulong w 9014 gbwrow ab z x base zs fr 9015 end 9016 end 9017 end 9018 9019 fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void 9020 zb = add base mul gbzof z zs 4 9021 t = add 128 mul 64 ulong ab 9022 hi = mul add mul z t x 128 9023 k = ulong get gbcs 5 9024 for kk ulong k 9025 gbwel ab zb x kk gbwv x kk z fr hi 9026 end 9027 end 9028 9029 note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer 9030 fn gbwv x ulong kk ulong z ulong fr bool to uint 9031 v = gbint x kk z 9032 if fr 9033 v = gbfr x kk z 9034 end 9035 ret bitcast uint f32 v 9036 end 9037 9038 note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk 9039 fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void 9040 md = get gbcs ab 9041 w = ulong get gbcs add 6 ab 9042 o = gbopf md x kk w ulong get gbcs 5 9043 gbput add zb mul o 4 u 9044 if eq ab 0 9045 put gbha add hi kk gbrv u 9046 else 9047 put gbhb add hi kk gbrv u 9048 end 9049 end 9050 9051 note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0, 9052 note else ((r + 3 c + z) mod 3) - 1, as f32 bits 9053 fn gbold3 z ulong r ulong c ulong to f64 9054 s = add mul r 3 mul c 5 9055 s = add s z 9056 ret sub f64 rem s 17 8.0f64 9057 end 9058 9059 fn gbmv z ulong r ulong c ulong to uint 9060 if eq rem add mul r 5 c 13 0 9061 ret `2143289344` 9062 end 9063 ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64 9064 end 9065 9066 note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns 9067 note (and Y's / M's with a fused epilogue) are NaN 9068 fn gbwc to void 9069 m = ulong get gbcs 6 9070 zs = mul m add ulong get gbcs 7 8 9071 for z ulong 6 9072 for r ulong m 9073 gbwcr z r zs 9074 end 9075 end 9076 end 9077 9078 fn gbwcr z ulong r ulong zs ulong to void 9079 n = ulong get gbcs 7 9080 ep = get gbcs 3 9081 o = gbcro z r zs 9082 for c ulong n 9083 oc = add o mul c 4 9084 gbput oc bitcast uint f32 gbold3 z r c 9085 if eq ep 1 9086 gbput add oc `1048576` bitcast uint f32 77.0f64 9087 end 9088 if eq ep 2 9089 gbput add oc `1048576` gbmv z r c 9090 end 9091 end 9092 for c ulong 8 9093 op = add o mul add n c 4 9094 gbput op `2143289344` 9095 if ne ep 0 9096 gbput add op `1048576` `2143289344` 9097 end 9098 end 9099 end 9100 9101 note operand ab's gkopa for the case at device address p 9102 fn gbfopa ab uint p ulong to void 9103 md = get gbcs ab 9104 k = get gbcs 5 9105 w = get gbcs add 6 ab 9106 zs = uint gbzs md ulong w ulong k 9107 so = mul add k 8 4 9108 sk = uint 4 9109 if eq md 0 9110 so = 4 9111 sk = mul add w 16 4 9112 end 9113 gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4 9114 end 9115 9116 fn gbflch to bool 9117 d = add gkbase get gbfb 0 9118 gbfopa 0 d 9119 gbfopa 1 add d `1048576` 9120 n = get gbcs 7 9121 zc = mul get gbcs 6 add n 8 9122 al = 1.0f64 9123 if eq get gbcs 9 1 9124 al = 0.5f64 9125 end 9126 sm = mul add n 8 4 9127 s1 = mul add mul zc 3 64 4 9128 s2 = mul zc 4 9129 gkopc add d `2097152` sm 4 s1 s2 f32 al 9130 if ne get gbcs 3 0 9131 gkope get gbcs 3 add d `3145728` 9132 end 9133 if ne get gbcs 4 0 9134 gkopz get gbcs 4 9135 end 9136 m = get gbcs 6 9137 k = get gbcs 5 9138 bt = eq get gbcs 2 1 9139 ret gkmm m n k 6 3 bt 9140 end 9141 9142 note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN 9143 fn gbfcmp to ulong 9144 bad = ulong 0 9145 for z ulong 6 9146 for r ulong ulong get gbcs 6 9147 bad = add bad gbfrow z r 9148 end 9149 end 9150 ret bad 9151 end 9152 9153 note host byte offset of C row r of block z (row stride n + 8 f32) 9154 fn gbcro z ulong r ulong zs ulong to ulong 9155 n = ulong get gbcs 7 9156 b = add get gbfb 0 `2097152` 9157 b = add b mul gbzof z zs 4 9158 ret add b mul mul r add n 8 4 9159 end 9160 9161 fn gbfrow z ulong r ulong to ulong 9162 n = ulong get gbcs 7 9163 zs = mul ulong get gbcs 6 add n 8 9164 o = gbcro z r zs 9165 bad = ulong 0 9166 for c ulong n 9167 if not gbfel z r c add o mul c 4 9168 bad = add bad 1 9169 end 9170 end 9171 for c ulong 8 9172 op = add o mul add n c 4 9173 if ne gbw op `2143289344` 9174 bad = add bad 1 9175 end 9176 if ne get gbcs 3 0 9177 if ne gbw add op `1048576` `2143289344` 9178 bad = add bad 1 9179 end 9180 end 9181 end 9182 ret bad 9183 end 9184 9185 note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2 9186 note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0 9187 fn gbfel z ulong r ulong c ulong oc ulong to bool 9188 cb = gbw oc 9189 old = gbold3 z r c 9190 if and eq get gbcs 4 3 gt div c 64 div r 64 9191 ret eq cb bitcast uint f32 old 9192 end 9193 ep = get gbcs 3 9194 if eq ep 2 9195 if not gt f64 bitcast f32 gbmv z r c 0.0f64 9196 ret eq cb 0 9197 end 9198 end 9199 s = gbfref z r c 9200 if eq get gbcs 2 1 9201 s = add s old 9202 end 9203 if ne f64 bitcast f32 cb s 9204 ret false 9205 end 9206 if eq ep 1 9207 w = uint 0 9208 if gt s 0.0f64 9209 w = cb 9210 end 9211 ret eq gbw add oc `1048576` w 9212 end 9213 ret true 9214 end 9215 9216 note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands) 9217 fn gbfref z ulong r ulong c ulong to f64 9218 k = ulong get gbcs 5 9219 lo = ulong 0 9220 hi = mul add div r 64 1 64 9221 cz = get gbcs 4 9222 if and eq cz 1 lt hi k 9223 k = hi 9224 end 9225 if eq cz 2 9226 lo = mul div r 64 64 9227 end 9228 ia = mul add mul z 128 r 128 9229 s = gbfdot ia mul add mul z 192 c 128 lo sub k lo 9230 if eq get gbcs 9 1 9231 s = mul s 0.5f64 9232 end 9233 ret s 9234 end 9235 9236 fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64 9237 s = 0.0f64 9238 for j ulong kn 9239 kk = add lo j 9240 a = get gbha add ia kk 9241 s = add s mul a get gbhb add ib kk 9242 end 9243 ret s 9244 end 9245 9246 note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0, 9247 note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile), 9248 note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples 9249 note of 2^-9, so the f32 result is exact. Logits buffer: A at 0, B at 6 MiB, C (rows of 72) at 12 MiB, 9250 note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`. 9251 fn gbfl p ulong ma uint mb uint to ulong 9252 for j ulong 10 9253 put gbcs j 0 9254 end 9255 put gbcs 0 ma 9256 put gbcs 1 mb 9257 put gbcs 5 16384 9258 put gbcs 6 64 9259 put gbcs 7 64 9260 put gbcs 8 uint p 9261 b = get gbfb 0 9262 for i ulong `3162112` 9263 gbput add b mul i 4 `2143289344` 9264 end 9265 gblw 0 9266 gblw 1 9267 put gkinv 0 1 9268 if not gbllch 9269 ggdie 98 9270 end 9271 bad = ulong 0 9272 for r ulong 64 9273 bad = add bad gblrow r 9274 end 9275 oputc 71 9276 oputc 70 9277 oputc 76 9278 oputc 32 9279 oputu p 9280 oputc 32 9281 oputu ulong ma 9282 oputc 32 9283 oputu ulong mb 9284 oputc 32 9285 oputu bad 9286 oputc 10 9287 ret bad 9288 end 9289 9290 note operand ab of the long case: nonzero words only at kk = 0 mod 29 9291 fn gblw ab uint to void 9292 md = get gbcs ab 9293 base = add get gbfb 0 mul ulong ab `6291456` 9294 fr = eq get gbcs 8 ab 9295 for x ulong 64 9296 gblwr md base x fr 9297 end 9298 end 9299 9300 fn gblwr md uint base ulong x ulong fr bool to void 9301 w = ulong 64 9302 kd = ulong 16384 9303 for kk ulong kd 9304 u = uint 0 9305 if eq rem kk 29 0 9306 u = gbwv x kk 0 fr 9307 end 9308 o = gbopf md x kk w kd 9309 gbput add base mul o 4 u 9310 end 9311 end 9312 9313 note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats 9314 fn gbllch to bool 9315 d = add gkbase get gbfb 0 9316 gblopa 0 d 9317 gblopa 1 add d `6291456` 9318 gkopc add d `12582912` 288 4 0 0 f32 1.0f64 9319 gkopz 0 9320 ret gkmm 64 64 16384 1 1 false 9321 end 9322 9323 fn gblopa ab uint p ulong to void 9324 so = uint 65568 9325 sk = uint 4 9326 if eq get gbcs ab 0 9327 so = 4 9328 sk = 320 9329 end 9330 gkopa ab p so sk 0 0 9331 end 9332 9333 note row r of the long case: 64 exact values, then 8 padding words that must still be NaN 9334 fn gblrow r ulong to ulong 9335 o = add add get gbfb 0 `12582912` mul r 288 9336 bad = ulong 0 9337 for c ulong 64 9338 cb = gbw add o mul c 4 9339 if ne f64 bitcast f32 cb gblref r c 9340 bad = add bad 1 9341 end 9342 end 9343 for c ulong 8 9344 if ne gbw add o mul add 64 c 4 `2143289344` 9345 bad = add bad 1 9346 end 9347 end 9348 ret bad 9349 end 9350 9351 note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk) 9352 fn gblref r ulong c ulong to f64 9353 fa = eq get gbcs 8 0 9354 fb = eq get gbcs 8 1 9355 s = 0.0f64 9356 for j ulong 565 9357 kk = mul j 29 9358 a = gbrv gbwv r kk 0 fa 9359 s = add s mul a gbrv gbwv c kk 0 fb 9360 end 9361 ret s 9362 end 9363 note SPDX-License-Identifier: AGPL-3.0-only 9364 note gg.om: ollm GPU gradient (aien-train#52, rung 4). The step loss and its gradient for the batch in 9365 note obx/oby, computed on the GB10 by gk.om GEMMs and gr.om row kernels over all R = B T rows at 9366 note once in f32; the gradient lands in ogr[0..P) as f64 and the loss in olos, so oadam and olsum run 9367 note unchanged on the CPU. Arena (gbmem 5) byte offsets in gga: 0 W, 1 G, 2 X, 3 Y, 4 CSR start 9368 note (V + 1), 5 CSR order (R), 6 first layer, 7 layer stride, 8 HF, 9 SF, 10 Z, 11 logits (R V), 9369 note 12 CE stats (R 3), 13 DH, 14 DA, 15 DQ DK DU (contiguous), 16 DO, 17 DF, 18 DP, 19 WS, 20 CS; 9370 note layer fields 24 + f: 0 hin 1 a 2 q 3 k 4 u 5 o 6 hm 7 bn 8 s1 9 s2 10 fp 11 rl 12 P; 9371 note 40 the arena device address, 47 the plan cursor. s1, s2 and SF hold 1/s (the CPU keeps s). 9372 global gga ulong 48 9373 global ggcn ulong 65537 9374 note byte strides: 0 D, 1 T D, 2 E, 3 T, 4 T T, 5 H T T, 6 R D, 7 D D (each times 4) 9375 global ggs uint 8 9376 note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch. 9377 const OMUTG ulong 1 9378 9379 fn ggdie c ulong to void 9380 oputc 71 9381 oputc 88 9382 oputc 32 9383 oputu c 9384 oputc 32 9385 oputl get nverr 0 9386 oputc 32 9387 oputu ulong get nvstage 0 9388 note diagnostics: launches done, mark1, mark2, QMD semaphore 9389 oputc 32 9390 oputl get gkt 0 9391 oputc 32 9392 oputu mapget get gbmem 3 0 4 9393 oputc 32 9394 oputu mapget get gbmem 3 16 4 9395 oputc 32 9396 oputu mapget get gbmem 2 8192 4 9397 oputc 10 9398 oflush 9399 exit 84 9400 end 9401 9402 fn ggcd n ulong k ulong to ulong 9403 ret div add n sub k 1 k 9404 end 9405 9406 fn ggr to ulong 9407 ret mul get og 6 get og 1 9408 end 9409 9410 note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it 9411 fn ggp5 to ulong 9412 ret mul ggcd get og 8 512 512 9413 end 9414 9415 fn ggput i ulong n ulong to void 9416 c = get gga 47 9417 put gga i c 9418 b = mul div add mul n 4 255 256 256 9419 put gga 47 add c b 9420 end 9421 9422 note arena plan for the dims in og; returns its size in bytes 9423 fn ggplan to ulong 9424 tt = get og 1 9425 d = get og 2 9426 h = get og 3 9427 f = get og 4 9428 bb = get og 6 9429 r = ggr 9430 rd = mul r d 9431 pp = mul mul bb h mul tt tt 9432 put gga 47 0 9433 for i ulong 8 9434 ggput add 24 i rd 9435 end 9436 ggput 32 r 9437 ggput 33 r 9438 ggput 34 mul r f 9439 ggput 35 mul r f 9440 ggput 36 pp 9441 put gga 7 get gga 47 9442 put gga 47 0 9443 ggput 0 get og 8 9444 ggput 1 ggp5 9445 ggput 2 r 9446 ggput 3 r 9447 ggput 4 add get og 0 1 9448 ggput 5 r 9449 put gga 6 get gga 47 9450 ls = mul get gga 7 get og 5 9451 put gga 47 add get gga 47 ls 9452 ggput 8 rd 9453 ggput 9 r 9454 ggput 10 rd 9455 ggput 11 mul r get og 0 9456 ggput 12 mul r 3 9457 ggput 13 rd 9458 ggput 14 rd 9459 ggput 15 mul rd 3 9460 ggput 16 rd 9461 ggput 17 mul r f 9462 ggput 18 pp 9463 ggput 19 rd 9464 ggput 20 mul bb d 9465 ggput 21 get og 8 9466 ggput 22 get og 8 9467 ggput 23 div ggp5 512 9468 note 37: split-K partials for ggsk, at most 2^21 floats (ggks stops doubling once a grid has 1024 blocks) 9469 ggput 37 `2097152` 9470 ret get gga 47 9471 end 9472 9473 fn gga1 i ulong to ulong 9474 ret add get gga 40 get gga i 9475 end 9476 9477 fn ggl l ulong f ulong to ulong 9478 o = add get gga 6 mul l get gga 7 9479 ret add add get gga 40 o get gga add 24 f 9480 end 9481 9482 note address of the output of layer l (the next hin, or HF after the last layer) 9483 fn ggout l ulong to ulong 9484 if lt add l 1 get og 5 9485 ret ggl add l 1 0 9486 end 9487 ret gga1 8 9488 end 9489 9490 fn ggw o ulong to ulong 9491 ret add gga1 0 mul o 4 9492 end 9493 9494 fn ggg o ulong to ulong 9495 ret add gga1 1 mul o 4 9496 end 9497 9498 note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T; 9499 note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512 9500 fn ggok to bool 9501 for i ulong 5 9502 v = get og i 9503 if eq i 3 9504 v = get og 7 9505 end 9506 if eq i 1 9507 v = get og 1 9508 end 9509 if or eq v 0 ne rem v 32 0 9510 ret false 9511 end 9512 end 9513 if gt get og 0 65536 9514 ret false 9515 end 9516 if gt get og 1 512 9517 ret false 9518 end 9519 ret le mul get og 6 get og 3 65535 9520 end 9521 9522 note open the chip and emit the kernels for the dims in og 9523 fn ggopen to void 9524 if not ggok 9525 ggdie 1 9526 end 9527 asz = ggplan 9528 v = get og 0 9529 tt = get og 1 9530 d = get og 2 9531 h = get og 3 9532 f = get og 4 9533 bb = get og 6 9534 grset v tt d h f bb 9535 if not gropen asz 9536 ggdie 2 9537 end 9538 put gga 40 gkbase 9539 ggstr 9540 end 9541 9542 note pointer j (1-based) into arg slots 2j, 2j + 1 9543 fn ggpt j uint a ulong to void 9544 put gkarg mul j 2 gblo a 9545 put gkarg add mul j 2 1 gbhi a 9546 end 9547 9548 fn ggu s uint v ulong to void 9549 put gkarg s uint v 9550 end 9551 9552 fn ggrun id uint na uint gx ulong gy ulong gz ulong to void 9553 put gkna 0 na 9554 x = uint gx 9555 y = uint gy 9556 z = uint gz 9557 if not gklaunch id x y z 9558 ggdie add 100 ulong id 9559 end 9560 end 9561 9562 fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void 9563 a = uint m 9564 b = uint n 9565 c = uint k 9566 e = uint nz 9567 g = uint h2 9568 if not gkmm a b c e g bt 9569 ggdie 99 9570 end 9571 end 9572 9573 note C[R][n] (+)= X[R][k] W[k][n] 9574 fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void 9575 k4 = uint mul k 4 9576 n4 = uint mul n 4 9577 gkopa 0 x k4 4 0 0 9578 gkopa 1 w 4 n4 0 0 9579 gkopc c n4 4 0 0 f32 1.0f64 9580 ggmm ggr n k 1 1 bt 9581 end 9582 9583 note C[R][n] (+)= X[R][k] W[n][k]^T 9584 fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void 9585 k4 = uint mul k 4 9586 n4 = uint mul n 4 9587 gkopa 0 x k4 4 0 0 9588 gkopa 1 w k4 4 0 0 9589 gkopc c n4 4 0 0 f32 1.0f64 9590 ggmm ggr n k 1 1 bt 9591 end 9592 9593 note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks) 9594 fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void 9595 k4 = uint mul kk 4 9596 n4 = uint mul n 4 9597 s = ggks kk n 1 9598 if eq s 1 9599 gkopa 0 x 4 k4 0 0 9600 gkopa 1 dy 4 n4 0 0 9601 gkopc g n4 4 0 0 f32 1.0f64 9602 ggmm kk n ggr 1 1 true 9603 ret 9604 end 9605 sl = div ggr s 9606 gkopa 0 x 4 k4 0 uint mul mul sl kk 4 9607 gkopa 1 dy 4 n4 0 uint mul mul sl n 4 9608 ggsk kk n 1 s g 0 9609 end 9610 9611 note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks 9612 note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles 9613 note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then 9614 note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37. 9615 fn ggks m ulong n ulong nz ulong to ulong 9616 b = mul mul div m 32 div n 32 nz 9617 r = ggr 9618 s = ulong 1 9619 while lt mul b s 1024 bound 8 9620 s2 = mul s 2 9621 if or gt s2 16 ne rem r mul s2 16 0 9622 ret s 9623 end 9624 s = s2 9625 end 9626 ret s 9627 end 9628 9629 note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the 9630 note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37, 9631 note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order. 9632 note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch. 9633 const OMUTP ulong 1 9634 fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void 9635 mn = mul m n 9636 p = gga1 37 9637 n4 = uint mul n 4 9638 c1 = uint mul mul mn s 4 9639 c2 = uint mul mn 4 9640 gkopc p n4 4 c1 c2 f32 1.0f64 9641 k = div ggr s 9642 z = mul nz s 9643 ggmm m n k z s false 9644 ggskr mn nz s g gz 9645 end 9646 9647 note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], i < mn, c < nz 9648 fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void 9649 kc = s 9650 if eq OMUTP 0 9651 kc = sub s 1 9652 end 9653 ggu 0 mn 9654 ggu 1 kc 9655 ggpt 1 gga1 37 9656 ggu 4 mul mn 4 9657 ggu 5 mul mul mn s 4 9658 ggpt 3 g 9659 ggu 8 ulong gz 9660 ggrun 14 9 ggcd mn 64 nz 1 9661 end 9662 9663 note 19 COPY (x into y), 20 ZERO (x) over n elements (the FFN ReLU is fused into the GEMMs, gkope) 9664 fn ggew id uint n ulong x ulong y ulong to void 9665 ggu 0 n 9666 ggpt 1 x 9667 ggpt 2 y 9668 ggrun id 6 ggcd n 64 1 1 9669 end 9670 9671 note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32 9672 fn ggrmsf v ulong g ulong y ulong s ulong to void 9673 r = mul ggr 32 9674 ggu 0 r 9675 ggpt 1 v 9676 ggpt 2 g 9677 ggpt 3 y 9678 ggpt 4 s 9679 ggrun 10 10 ggcd r 64 1 1 9680 end 9681 9682 note dh += RMS backward of da; the gain terms go to WS 9683 fn ggrmsb v ulong g ulong s ulong to void 9684 r = mul ggr 32 9685 ggu 0 r 9686 ggpt 1 v 9687 ggpt 2 g 9688 ggpt 3 s 9689 ggpt 4 gga1 14 9690 ggpt 5 gga1 13 9691 ggpt 6 gga1 19 9692 ggrun 11 14 ggcd r 64 1 1 9693 end 9694 9695 note gain gradient at parameter offset go += column sums of WS: per sequence (slot 13), then over B 9696 fn ggain go ulong to void 9697 d = get og 2 9698 tt = get og 1 9699 bb = get og 6 9700 ggu 0 d 9701 ggu 1 tt 9702 ggpt 1 gga1 19 9703 ggu 4 mul d 4 9704 ggu 5 mul mul tt d 4 9705 ggpt 3 gga1 20 9706 ggu 8 mul d 4 9707 ggrun 13 9 ggcd d 64 bb 1 9708 ggu 0 d 9709 ggu 1 bb 9710 ggpt 1 gga1 20 9711 ggu 4 mul d 4 9712 ggu 5 0 9713 ggpt 3 ggg go 9714 ggu 8 0 9715 ggrun 14 9 ggcd d 64 1 1 9716 end 9717 9718 note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H) 9719 fn ggsoft id uint p ulong dp ulong to void 9720 tt = get og 1 9721 ggu 0 mul tt 32 9722 ggpt 1 p 9723 ggpt 2 dp 9724 ggrun id 6 div tt 2 mul get og 6 get og 3 1 9725 end 9726 9727 note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first; 9728 note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP) 9729 fn ggbh m ulong n ulong k ulong bt bool cz uint to void 9730 gkopz cz 9731 ggmm m n k mul get og 6 get og 3 get og 3 bt 9732 end 9733 9734 note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows 9735 fn gghd ab uint p ulong tr bool to void 9736 s0 = get ggs 0 9737 s1 = get ggs 1 9738 s2 = get ggs 2 9739 if tr 9740 gkopa ab p 4 s0 s1 s2 9741 else 9742 gkopa ab p s0 4 s1 s2 9743 end 9744 end 9745 9746 note T x T score operand (batch b then head); tr: transposed 9747 fn ggpl ab uint p ulong tr bool to void 9748 s3 = get ggs 3 9749 s4 = get ggs 4 9750 s5 = get ggs 5 9751 if tr 9752 gkopa ab p 4 s3 s5 s4 9753 else 9754 gkopa ab p s3 4 s5 s4 9755 end 9756 end 9757 9758 fn ggch c ulong al f64 to void 9759 s0 = get ggs 0 9760 s1 = get ggs 1 9761 s2 = get ggs 2 9762 gkopc c s0 4 s1 s2 f32 al 9763 end 9764 9765 fn ggcp c ulong al f64 to void 9766 s3 = get ggs 3 9767 s4 = get ggs 4 9768 s5 = get ggs 5 9769 gkopc c s3 4 s5 s4 f32 al 9770 end 9771 9772 note stride table for the dims in og (bytes) 9773 fn ggstr to void 9774 d = get og 2 9775 tt = get og 1 9776 put ggs 0 uint mul d 4 9777 put ggs 1 uint mul tt mul d 4 9778 put ggs 2 uint mul get og 7 4 9779 put ggs 3 uint mul tt 4 9780 put ggs 4 uint mul tt mul tt 4 9781 put ggs 5 uint mul get og 3 mul tt mul tt 4 9782 put ggs 6 uint mul ggr mul d 4 9783 put ggs 7 uint mul d mul d 4 9784 end 9785 9786 note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous) 9787 fn ggqkv l ulong to void 9788 d = get og 2 9789 a = ggl l 1 9790 w = ggw opwq l 9791 c = ggl l 2 9792 s0 = get ggs 0 9793 s6 = get ggs 6 9794 s7 = get ggs 7 9795 gkopa 0 a s0 4 0 0 9796 gkopa 1 w 4 s0 0 s7 9797 gkopc c s0 4 0 s6 f32 1.0f64 9798 ggmm ggr d d 3 3 false 9799 end 9800 9801 note P = softmax(q k^T / sqrt(E)), o = P u; causal: only S on and below the diagonal is formed 9802 note (gkmm cz 3) and o stops at the diagonal (cz 1), since P is exactly 0 above it 9803 fn ggatt l ulong to void 9804 tt = get og 1 9805 e = get og 7 9806 pp = ggl l 12 9807 q = ggl l 2 9808 k = ggl l 3 9809 gghd 0 q false 9810 gghd 1 k false 9811 isq = div 1.0f64 fsqrt f64 e 9812 ggcp pp isq 9813 ggbh tt tt e false 3 9814 ggsoft 15 pp pp 9815 u = ggl l 4 9816 o = ggl l 5 9817 ggpl 0 pp false 9818 gghd 1 u true 9819 ggch o 1.0f64 9820 ggbh tt e tt false 1 9821 end 9822 9823 note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2 9824 fn ggffn l ulong to void 9825 d = get og 2 9826 f = get og 4 9827 rd = mul ggr d 9828 hm = ggl l 6 9829 x = ggl l 0 9830 ggew 19 rd x hm 9831 x = ggl l 5 9832 w = ggw opwo l 9833 ggxw x w hm d d true 9834 x = ggl l 7 9835 w = ggw opg2 l 9836 s = ggl l 9 9837 ggrmsf hm w x s 9838 fp = ggl l 10 9839 w = ggw opw1 l 9840 y = ggl l 11 9841 gkope 1 y 9842 ggxw x w fp f d false 9843 x = y 9844 o = ggout l 9845 ggew 19 rd hm o 9846 w = ggw opw2 l 9847 ggxw x w o d f true 9848 end 9849 9850 fn ggfl l ulong to void 9851 x = ggl l 0 9852 w = ggw opg1 l 9853 a = ggl l 1 9854 s = ggl l 8 9855 ggrmsf x w a s 9856 ggqkv l 9857 ggatt l 9858 ggffn l 9859 end 9860 9861 fn ggfwd to void 9862 d = get og 2 9863 x = ggl 0 0 9864 ggu 0 d 9865 ggpt 1 x 9866 x = ggw 0 9867 ggpt 2 x 9868 x = ggw get og 30 9869 ggpt 3 x 9870 x = gga1 2 9871 ggpt 4 x 9872 tt = get og 1 9873 bb = get og 6 9874 ggrun 9 10 ggcd d 64 tt bb 9875 for l ulong get og 5 9876 ggfl l 9877 end 9878 x = gga1 8 9879 w = ggw get og 33 9880 z = gga1 10 9881 s = gga1 9 9882 ggrmsf x w z s 9883 x = gga1 11 9884 w = ggw 0 9885 v = get og 0 9886 ggxwt z w x v d false 9887 r = mul ggr 32 9888 ggu 0 r 9889 ggpt 1 x 9890 x = gga1 3 9891 ggpt 2 x 9892 x = gga1 12 9893 ggpt 3 x 9894 ggrun 21 8 ggcd r 64 1 1 9895 end 9896 9897 note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2) 9898 note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y. 9899 fn ggbffn l ulong to void 9900 d = get og 2 9901 f = get og 4 9902 dh = gga1 13 9903 df = gga1 17 9904 da = gga1 14 9905 w = ggw opw2 l 9906 x = ggl l 10 9907 gkope 2 x 9908 ggxwt dh w df f d false 9909 x = ggl l 11 9910 w = ggg opw2 l 9911 ggwg x dh w f d 9912 x = ggl l 7 9913 w = ggg opw1 l 9914 ggwg x df w d f 9915 w = ggw opw1 l 9916 ggxwt df w da d f false 9917 x = ggl l 6 9918 w = ggw opg2 l 9919 s = ggl l 9 9920 ggrmsb x w s 9921 ggain opg2 l 9922 end 9923 9924 note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP 9925 note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal 9926 note (cz 2), DQ stops there (cz 1). 9927 fn ggbatt l ulong to void 9928 d = get og 2 9929 tt = get og 1 9930 e = get og 7 9931 dh = gga1 13 9932 dn = gga1 16 9933 w = ggw opwo l 9934 ggxwt dh w dn d d false 9935 x = ggl l 5 9936 w = ggg opwo l 9937 ggwg x dh w d d 9938 dp = gga1 18 9939 x = ggl l 4 9940 gghd 0 dn false 9941 gghd 1 x false 9942 ggcp dp 1.0f64 9943 ggbh tt tt e false 3 9944 pp = ggl l 12 9945 dq = gga1 15 9946 dk = add dq ulong get ggs 6 9947 du = add dk ulong get ggs 6 9948 ggpl 0 pp true 9949 gghd 1 dn true 9950 ggch du 1.0f64 9951 ggbh tt e tt false 2 9952 ggsoft 16 pp dp 9953 x = ggl l 3 9954 ggpl 0 dp false 9955 gghd 1 x true 9956 ggch dq 1.0f64 9957 ggbh tt e tt false 1 9958 al = 1.0f64 9959 if eq OMUTG 0 9960 al = neg 1.0f64 9961 end 9962 x = ggl l 2 9963 ggpl 0 dp true 9964 gghd 1 x true 9965 ggch dk al 9966 ggbh tt e tt false 2 9967 end 9968 9969 note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward 9970 fn ggbqkv l ulong to void 9971 d = get og 2 9972 da = gga1 14 9973 dq = gga1 15 9974 dk = add dq ulong get ggs 6 9975 du = add dk ulong get ggs 6 9976 w = ggw opwq l 9977 ggxwt dq w da d d false 9978 w = ggw opwk l 9979 ggxwt dk w da d d true 9980 w = ggw opwv l 9981 ggxwt du w da d d true 9982 a = ggl l 1 9983 w = ggg opwq l 9984 s0 = get ggs 0 9985 s6 = get ggs 6 9986 s7 = get ggs 7 9987 k = ggks d d 3 9988 if eq k 1 9989 gkopa 0 a 4 s0 0 0 9990 gkopa 1 dq 4 s0 0 s6 9991 gkopc w s0 4 0 s7 f32 1.0f64 9992 ggmm d d ggr 3 3 true 9993 else 9994 sl = uint div ggr k 9995 gkopa 0 a 4 s0 0 mul sl s0 9996 gkopa 1 dq 4 s0 s6 mul sl s0 9997 ggsk d d 3 k w s7 9998 end 9999 x = ggl l 0 10000 w = ggw opg1 l 10001 s = ggl l 8 10002 ggrmsb x w s 10003 ggain opg1 l 10004 end 10005 10006 fn ggbl l ulong to void 10007 ggbffn l 10008 ggbatt l 10009 ggbqkv l 10010 end 10011 10012 fn ggbwd to void 10013 d = get og 2 10014 v = get og 0 10015 tt = get og 1 10016 r = ggr 10017 ggew 20 ggp5 gga1 1 gga1 1 10018 ggew 20 mul r d gga1 13 gga1 13 10019 ggxw gga1 11 ggw 0 gga1 14 d v false 10020 ggwg gga1 11 gga1 10 ggg 0 v d 10021 ggrmsb gga1 8 ggw get og 33 gga1 9 10022 ggain get og 33 10023 l = get og 5 10024 while gt l 0 bound 1000 10025 l = sub l 1 10026 ggbl l 10027 end 10028 td = mul tt d 10029 ggu 0 td 10030 ggu 1 get og 6 10031 ggpt 1 gga1 13 10032 ggu 4 mul td 4 10033 ggu 5 0 10034 ggpt 3 ggg get og 30 10035 ggu 8 0 10036 ggrun 14 9 ggcd td 64 1 1 10037 ggu 0 d 10038 ggpt 1 gga1 13 10039 ggpt 2 gga1 4 10040 ggpt 3 gga1 5 10041 ggpt 4 ggg 0 10042 ggrun 22 10 ggcd d 64 v 1 10043 end 10044 10045 note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a 10046 note forward error on the chip that gcmp2's FP check must name). 10047 const OMUTF ulong 1 10048 10049 note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes) 10050 fn ggupc q ulong p ulong to void 10051 h = get gbmem 5 10052 wo = get gga 0 10053 i0 = mul q OCH 10054 for j ulong sub oce q p i0 10055 i = add i0 j 10056 x = bitcast uint f32 ggupv i 10057 m = mapset h add wo mul i 4 4 ulong x 10058 end 10059 end 10060 10061 note the uploaded value of W[i]: the parameter at virtual index i in the product 10062 fn ggupv i ulong to f64 10063 w = owg i 10064 if eq OMUTF 0 10065 a = opw1 0 10066 if and ge i a lt i add a mul get og 2 get og 4 10067 w = mul w 1.`0009765625f64` 10068 end 10069 end 10070 ret w 10071 end 10072 10073 note upload the f32 parameters 10074 fn ggupw to void 10075 p = get og 8 10076 for q ulong ocn p 10077 ggupc q p 10078 end 10079 put gkinv 0 1 10080 end 10081 10082 note upload the batch tokens and the CSR of x (rows sorted by token) 10083 fn ggupb to void 10084 ggupt 10085 ggcsr 10086 put gkinv 0 1 10087 end 10088 10089 note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an 10090 note Adam moment) 10091 fn ggupmc s ulong o ulong n ulong to void 10092 h = get gbmem 5 10093 for j ulong n 10094 x = bitcast uint f32 omvg add s j 10095 m = mapset h add o mul j 4 4 ulong x 10096 end 10097 end 10098 10099 fn ggdnmc s ulong o ulong n ulong to void 10100 h = get gbmem 5 10101 for j ulong n 10102 x = uint mapget h add o mul j 4 4 10103 omvp add s j f64 bitcast f32 x 10104 end 10105 end 10106 10107 note chip parameters of chunk q back into the parameter banks as f64 10108 fn ggdnwc q ulong p ulong to void 10109 h = get gbmem 5 10110 o = get gga 0 10111 i0 = mul q OCH 10112 for j ulong sub oce q p i0 10113 i = add i0 j 10114 x = uint mapget h add o mul i 4 4 10115 owp i f64 bitcast f32 x 10116 end 10117 end 10118 10119 note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back 10120 fn ggupst to void 10121 ggupw 10122 p = get og 8 10123 for q ulong ocn p 10124 i0 = mul q OCH 10125 n = sub oce q p i0 10126 ggupmc add get og 11 i0 add get gga 21 mul i0 4 n 10127 ggupmc add get og 12 i0 add get gga 22 mul i0 4 n 10128 end 10129 put gkinv 0 1 10130 end 10131 10132 fn ggdnst to void 10133 p = get og 8 10134 for q ulong ocn p 10135 ggdnwc q p 10136 i0 = mul q OCH 10137 n = sub oce q p i0 10138 ggdnmc add get og 11 i0 add get gga 21 mul i0 4 n 10139 ggdnmc add get og 12 i0 add get gga 22 mul i0 4 n 10140 end 10141 end 10142 10143 fn ggupt to void 10144 h = get gbmem 5 10145 xo = get gga 2 10146 yo = get gga 3 10147 for i ulong ggr 10148 tk = get obx i 10149 m = mapset h add xo mul i 4 4 tk 10150 ty = get oby i 10151 m = mapset h add yo mul i 4 4 ty 10152 end 10153 end 10154 10155 note counting sort of the R rows by input token: start (V + 1) and order (R) arrays 10156 fn ggcsr to void 10157 h = get gbmem 5 10158 v = get og 0 10159 for c ulong add v 1 10160 put ggcn c 0 10161 end 10162 for i ulong ggr 10163 c1 = add get obx i 1 10164 n = add get ggcn c1 1 10165 put ggcn c1 n 10166 end 10167 for c ulong v 10168 c1 = add c 1 10169 n = add get ggcn c1 get ggcn c 10170 put ggcn c1 n 10171 end 10172 so = get gga 4 10173 for c ulong add v 1 10174 n = get ggcn c 10175 m = mapset h add so mul c 4 4 n 10176 end 10177 so = get gga 5 10178 for i ulong ggr 10179 c = get obx i 10180 p = get ggcn c 10181 m = mapset h add so mul p 4 4 i 10182 put ggcn c add p 1 10183 end 10184 end 10185 10186 note GRD[i] = G[i] for i in chunk q of P 10187 fn ggdnc q ulong p ulong to void 10188 h = get gbmem 5 10189 go = get gga 1 10190 i0 = mul q OCH 10191 for j ulong sub oce q p i0 10192 i = add i0 j 10193 x = uint mapget h add go mul i 4 4 10194 ocsp i f64 bitcast f32 x 10195 end 10196 end 10197 10198 fn ggdown to void 10199 p = get og 8 10200 for q ulong ocn p 10201 ggdnc q p 10202 end 10203 end 10204 10205 note one f32 at arena byte offset o, as f64 10206 fn ggf o ulong to f64 10207 x = uint mapget get gbmem 5 o 4 10208 ret f64 bitcast f32 x 10209 end 10210 10211 note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y 10212 fn ggrl r ulong to f64 10213 o = add get gga 12 mul r 12 10214 m = ggf o 10215 sm = ggf add o 4 10216 ly = ggf add o 8 10217 ret sub add flog sm m ly 10218 end 10219 10220 fn ggloss to f64 10221 tt = get og 1 10222 for b ulong get og 6 10223 s = 0.0f64 10224 for t ulong tt 10225 s = add s ggrl add mul b tt t 10226 end 10227 put olos b div s f64 tt 10228 end 10229 ret olsum 10230 end 10231 10232 note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32 10233 note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip. 10234 note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam 10235 global ggtm long 6 10236 fn ggpass full bool to f64 10237 t = monons 10238 if full 10239 ggupw 10240 end 10241 ggupb 10242 u = monons 10243 put ggtm 0 sub u t 10244 ggfwd 10245 t = monons 10246 put ggtm 1 sub t u 10247 ggbwd 10248 u = monons 10249 put ggtm 2 sub u t 10250 if full 10251 ggdown 10252 end 10253 t = monons 10254 put ggtm 3 sub t u 10255 l = ggloss 10256 u = monons 10257 put ggtm 4 sub u t 10258 ret l 10259 end 10260 10261 fn gggrad to f64 10262 ret ggpass true 10263 end 10264 10265 note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the 10266 note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P 10267 fn ggadam to void 10268 p = get og 8 10269 nc = div ggp5 512 10270 ggu 0 nc 10271 ggpt 1 gga1 1 10272 ggpt 2 gga1 23 10273 ggrun 23 6 ggcd nc 64 1 1 10274 h = get gbmem 5 10275 so = get gga 23 10276 ss = 0.0f64 10277 for c ulong nc 10278 x = uint mapget h add so mul c 4 4 10279 ss = add ss f64 bitcast f32 x 10280 end 10281 nr = fsqrt ss 10282 cl = 1.0f64 10283 if gt nr 1.0 10284 cl = div 1.0 nr 10285 end 10286 s = get og 41 10287 lrs = olrs s bitcast f64 get og 43 10288 bc1 = sub 1.0 opowb 0.9 s 10289 bc2 = sub 1.0 opowb 0.99 s 10290 if eq OMUTC 0 10291 bc1 = 1.0f64 10292 bc2 = 1.0f64 10293 end 10294 ggu 0 p 10295 ggpt 1 gga1 0 10296 ggpt 2 gga1 21 10297 ggpt 3 gga1 22 10298 ggpt 4 gga1 1 10299 ggu 10 ulong grf cl 10300 ggu 11 ulong grf div lrs bc1 10301 ggu 12 ulong grf div 1.0f64 bc2 10302 ggrun 24 13 ggcd p 64 1 1 10303 end 10304 10305 fn ggtms to void 10306 oputc 71 10307 oputc 84 10308 for i ulong 6 10309 oputc 32 10310 oputl get ggtm i 10311 end 10312 for i ulong 4 10313 oputc 32 10314 oputl get gkt i 10315 end 10316 oputc 10 10317 end 10318 10319 note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes) 10320 fn ggkeep p ulong to void 10321 for q ulong ocn p 10322 i0 = mul q OCH 10323 for j ulong sub oce q p i0 10324 i = add i0 j 10325 ogrp add p i ocsg i 10326 end 10327 end 10328 end 10329 10330 note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P 10331 global ggcm f64 2 10332 fn ggmaxd p ulong to void 10333 put ggcm 0 0.0f64 10334 put ggcm 1 0.0f64 10335 for q ulong ocn p 10336 i0 = mul q OCH 10337 for j ulong sub oce q p i0 10338 i = add i0 j 10339 rf = ogrg add p i 10340 dd = sub ocsg i rf 10341 if lt dd 0.0f64 10342 dd = neg dd 10343 end 10344 if lt rf 0.0f64 10345 rf = neg rf 10346 end 10347 if gt dd get ggcm 0 10348 put ggcm 0 dd 10349 end 10350 if gt rf get ggcm 1 10351 put ggcm 1 rf 10352 end 10353 end 10354 end 10355 end 10356 10357 note gcmp CK DATA S: CPU ograd and GPU gradient for batch S; prints 10358 note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS 10359 note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order 10360 note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight 10361 note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64 10362 note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt: 10363 note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients. 10364 fn ggcmp to void 10365 if ne oargc 5 10366 exit 2 10367 end 10368 ockload 2 10369 odopen 3 10370 obatch oargu 4 10371 put og 10 0 10372 lc = ograd 10373 p = get og 8 10374 ggkeep p 10375 ggopen 10376 t0 = monons 10377 lg = gggrad 10378 t1 = monons 10379 ggmaxd p 10380 mx = get ggcm 0 10381 mr = get ggcm 1 10382 rel = div mx mr 10383 dl = sub lg lc 10384 if lt dl 0.0f64 10385 dl = neg dl 10386 end 10387 oputc 71 10388 oputc 67 10389 oputc 32 10390 oputu p 10391 oputc 32 10392 oputl rmic mul mx `1000000`.0f64 10393 oputc 32 10394 oputl rmic mul mr `1000000`.0f64 10395 oputc 32 10396 oputl rmic mul rel 1000.0f64 10397 oputc 32 10398 oputl rmic lc 10399 oputc 32 10400 oputl rmic lg 10401 oputc 32 10402 oputl sub t1 t0 10403 oputc 32 10404 tol = div f64 ggr `16777216`.0f64 10405 oputl rmic mul tol 1000.0f64 10406 oputc 10 10407 ggtms 10408 ok = and lt rel tol lt dl 0.`00001f64` 10409 r = nvstop 10410 oputc 71 10411 oputc 67 10412 oputc 77 10413 oputc 80 10414 oputc 32 10415 if ok 10416 oputc 80 10417 oputc 65 10418 oputc 83 10419 oputc 83 10420 oputc 10 10421 oflush 10422 ret 10423 end 10424 oputc 70 10425 oputc 65 10426 oputc 73 10427 oputc 76 10428 oputc 10 10429 oflush 10430 exit 45 10431 end 10432 10433 note true when x is finite (every comparison with NaN is false) 10434 fn ggfin x f64 to bool 10435 a = x 10436 if lt a 0.0f64 10437 a = neg a 10438 end 10439 ret le a bitcast f64 ulong `9218868437227405311` 10440 end 10441 10442 note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range) 10443 fn ggclp x f64 to f64 10444 if ggfin x 10445 if lt x `1000000`.0f64 10446 ret x 10447 end 10448 end 10449 ret `1000000`.0f64 10450 end 10451 10452 note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 1536, CONTRACT-V2). ggk2: [0] ReLU 10453 note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference), 10454 note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1). 10455 note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|. 10456 global ggfs f64 1536 10457 global ggk2 ulong 4 10458 global ggk3 f64 3 10459 10460 note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of 10461 note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb) 10462 fn ggsj l ulong t ulong to void 10463 d = get og 2 10464 f = get og 4 10465 bn = add oabn l mul t d 10466 w = opx opw1 l 10467 for j ulong f 10468 put ggfs j 0.0f64 10469 end 10470 for k ulong d 10471 a = get oa add bn k 10472 if lt a 0.0f64 10473 a = neg a 10474 end 10475 ggsjk a add w mul k f f 10476 end 10477 end 10478 10479 fn ggsjk a f64 wo ulong f ulong to void 10480 for j ulong f 10481 x = get ow add wo j 10482 if lt x 0.0f64 10483 x = neg x 10484 end 10485 put ggfs j add get ggfs j mul a x 10486 end 10487 end 10488 10489 note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in 10490 note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad), 10491 note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included). 10492 fn ogmask yo ulong l ulong t ulong to void 10493 f = get og 4 10494 xo = add oafp l mul t f 10495 ggsj l t 10496 row = add mul get ogkm 1 get og 1 t 10497 c0 = add sub ggl l 10 get gga 40 mul mul row f 4 10498 for j ulong f 10499 c = ggf add c0 mul j 4 10500 ggk1 get oa add xo j c j l 10501 if not gt c 0.0f64 10502 put oa add yo j 0.0f64 10503 end 10504 end 10505 end 10506 10507 note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the 10508 note values differ), and the flip count 10509 fn ggk1 v f64 c f64 j ulong l ulong to void 10510 if not and ggfin v ggfin c 10511 put ggk2 1 add get ggk2 1 1 10512 ret 10513 end 10514 dd = sub c v 10515 if lt dd 0.0f64 10516 dd = neg dd 10517 end 10518 if gt dd get ggk3 2 10519 put ggk3 2 dd 10520 end 10521 ggkr dd get ggfs j l 10522 ggkfl v c 10523 end 10524 10525 fn ggkr dd f64 s f64 l ulong to void 10526 if not gt dd 0.0f64 10527 ret 10528 end 10529 rho = bitcast f64 ulong `9218868437227405312` 10530 if gt s 0.0f64 10531 rho = div dd s 10532 end 10533 if gt rho get ggk3 0 10534 put ggk3 0 rho 10535 put ggk2 2 l 10536 end 10537 end 10538 10539 fn ggkfl v f64 c f64 to void 10540 a = gt v 0.0f64 10541 b = gt c 0.0f64 10542 if eq a b 10543 ret 10544 end 10545 put ggk2 0 add get ggk2 0 1 10546 if lt v 0.0f64 10547 v = neg v 10548 end 10549 if gt v get ggk3 1 10550 put ggk3 1 v 10551 end 10552 end 10553 10554 note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no 10555 note Adam step) 10556 fn ggstash p ulong o ulong to void 10557 for q ulong ocn p 10558 ggstq q p o 10559 end 10560 end 10561 10562 fn ggstq q ulong p ulong o ulong to void 10563 i0 = mul q OCH 10564 for j ulong sub oce q p i0 10565 i = add i0 j 10566 omvp add o i ocsg i 10567 end 10568 end 10569 10570 note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip 10571 note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3] 10572 global ggc2 f64 3 10573 fn gg2cmp p ulong to void 10574 for k ulong 3 10575 put ggc2 k 0.0f64 10576 end 10577 for q ulong ocn p 10578 i0 = mul q OCH 10579 for j ulong sub oce q p i0 10580 gg2c1 add i0 j p 10581 end 10582 end 10583 end 10584 10585 fn gg2c1 i ulong p ulong to void 10586 c = omvg i 10587 r0 = omvg add p i 10588 r1 = ocsg i 10589 if not and and ggfin c ggfin r0 ggfin r1 10590 put ggk2 3 add get ggk2 3 1 10591 ret 10592 end 10593 gg2mx 0 sub c r0 10594 gg2mx 1 r0 10595 gg2mx 2 sub c r1 10596 end 10597 10598 fn gg2mx k ulong x f64 to void 10599 if lt x 0.0f64 10600 x = neg x 10601 end 10602 if gt x get ggc2 k 10603 put ggc2 k x 10604 end 10605 end 10606 10607 note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip 10608 note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's 10609 note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks 10610 note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips 10611 note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or 10612 note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| / 10613 note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient 10614 note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip 10615 note loss - K0 loss| >= 1e-5. 10616 fn ggcmp2 to void 10617 if ne oargc 5 10618 exit 2 10619 end 10620 ockload 2 10621 odopen 3 10622 obatch oargu 4 10623 p = get og 8 10624 ggopen 10625 t0 = monons 10626 lg = gggrad 10627 t1 = monons 10628 ggstash p 0 10629 put og 10 0 10630 put ogkm 0 0 10631 l0 = ograd 10632 ggstash p p 10633 for k ulong 4 10634 put ggk2 k 0 10635 end 10636 for k ulong 3 10637 put ggk3 k 0.0f64 10638 end 10639 put ogkm 0 1 10640 put og 10 0 10641 l1 = ograd 10642 put ogkm 0 0 10643 gg2cmp p 10644 ggc2pr lg l0 sub t1 t0 10645 end 10646 10647 note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2 10648 note (gkbf 1) takes gb.om's verdict 10649 fn ggc2pr lg f64 l0 f64 ns long to void 10650 fl = ggc2fl lg l0 10651 ggc2ln lg l0 ns fl 10652 ggtms 10653 r = nvstop 10654 if eq get gkbf 0 1 10655 gbc2vd lg l0 fl 10656 ret 10657 end 10658 ggc2vd eq fl 0 10659 end 10660 10661 note ggc2[k] / max |K0|, clamped for printing 10662 fn gg2rl k ulong to f64 10663 ret ggclp div get ggc2 k get ggc2 1 10664 end 10665 10666 note tolf = max(D, F, T) 2^-24 10667 fn gg2tf to f64 10668 n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1 10669 ret div n `16777216`.0f64 10670 end 10671 10672 note tolr = R 2^-24 (gcmp's gamma_R) 10673 fn gg2tr to f64 10674 ret div f64 ggr `16777216`.0f64 10675 end 10676 10677 note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1 10678 fn ggc2fl lg f64 l0 f64 to ulong 10679 if eq get gkbf 0 1 10680 ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3 10681 end 10682 dl = sub lg l0 10683 if lt dl 0.0f64 10684 dl = neg dl 10685 end 10686 fl = ulong 0 10687 if ne add get ggk2 1 get ggk2 3 0 10688 fl = or fl 1 10689 end 10690 if not lt ggclp get ggk3 0 gg2tf 10691 fl = or fl 2 10692 end 10693 if not lt gg2rl 2 gg2tr 10694 fl = or fl 4 10695 end 10696 if not lt dl 0.`00001f64` 10697 fl = or fl 8 10698 end 10699 ret fl 10700 end 10701 10702 fn ggc2ln lg f64 l0 f64 ns long fl ulong to void 10703 oputc 71 10704 oputc 67 10705 oputc 50 10706 oputc 32 10707 oputu get og 8 10708 gg2pe gg2rl 0 10709 gg2pe gg2rl 2 10710 gg2pe gg2tr 10711 gg2pe ggclp get ggk3 0 10712 gg2pe gg2tf 10713 oputc 32 10714 oputu get ggk2 2 10715 oputc 32 10716 oputu get ggk2 0 10717 oputc 32 10718 oputl rmic mul ggclp get ggk3 1 `1000000`.0f64 10719 oputc 32 10720 oputl rmic mul ggclp get ggk3 2 `1000000`.0f64 10721 oputc 32 10722 oputu add get ggk2 1 get ggk2 3 10723 oputc 32 10724 oputl rmic ggclp l0 10725 oputc 32 10726 oputl rmic ggclp lg 10727 oputc 32 10728 oputl ns 10729 oputc 32 10730 oputu fl 10731 oputc 10 10732 end 10733 10734 note " x_e9" 10735 fn gg2pe x f64 to void 10736 oputc 32 10737 oputl rmic mul x 1000.0f64 10738 end 10739 10740 fn ggc2vd ok bool to void 10741 oputc 71 10742 oputc 67 10743 oputc 77 10744 oputc 80 10745 oputc 50 10746 oputc 32 10747 if ok 10748 oputc 80 10749 oputc 65 10750 oputc 83 10751 oputc 83 10752 oputc 10 10753 oflush 10754 ret 10755 end 10756 oputc 70 10757 oputc 65 10758 oputc 73 10759 oputc 76 10760 oputc 10 10761 oflush 10762 exit 47 10763 end 10764 10765 note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints 10766 note `T s+1 loss_micro` at save points like train 10767 fn ggtrain to void 10768 if ne oargc 6 10769 exit 2 10770 end 10771 ockload 2 10772 odopen 3 10773 un = oargu 4 10774 ev = oargu 5 10775 if eq ev 0 10776 exit 2 10777 end 10778 en = get og 42 10779 if lt un en 10780 en = un 10781 end 10782 ggopen 10783 while lt get og 41 en bound 999999 10784 s = get og 41 10785 obatch s 10786 put og 10 0 10787 ls = gggrad 10788 oadam 10789 s1 = add s 1 10790 put og 41 s1 10791 if or eq rem s1 ev 0 eq s1 en 10792 ocksave 2 10793 oputc 84 10794 oputc 32 10795 oputu s1 10796 oputc 32 10797 oputl rmic ls 10798 oputc 10 10799 oflush 10800 end 10801 end 10802 r = nvstop 10803 end 10804 10805 note BLK[i] = the f64 parameter i before the step 10806 fn ggkw q ulong p ulong to void 10807 i0 = mul q OCH 10808 for j ulong sub oce q p i0 10809 i = add i0 j 10810 ogrp add p i owg i 10811 end 10812 end 10813 10814 note GRD[i] = chip parameter i as f64 10815 fn ggdw q ulong p ulong to void 10816 h = get gbmem 5 10817 wo = get gga 0 10818 i0 = mul q OCH 10819 for j ulong sub oce q p i0 10820 i = add i0 j 10821 x = uint mapget h add wo mul i 4 4 10822 ocsp i f64 bitcast f32 x 10823 end 10824 end 10825 10826 note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result): 10827 note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part 10828 note of d above the half-ulp rounding of the f32 master weight 10829 global ggam f64 3 10830 fn ggad1 i ulong p ulong to void 10831 w0 = ogrg add p i 10832 c = owg i 10833 u = sub c w0 10834 g = sub ocsg i f64 f32 w0 10835 d = fabs sub g u 10836 u = fabs u 10837 e = sub d mul fabs c 0.`000000059604644775390625f64` 10838 if gt d get ggam 0 10839 put ggam 0 d 10840 end 10841 if gt u get ggam 1 10842 put ggam 1 u 10843 end 10844 if gt e get ggam 2 10845 put ggam 2 e 10846 end 10847 end 10848 10849 fn ggadc q ulong p ulong to void 10850 i0 = mul q OCH 10851 for j ulong sub oce q p i0 10852 ggad1 add i0 j p 10853 end 10854 end 10855 10856 note gacmp CK DATA S: one step from CK on batch S with the chip gradient, Adam on the CPU (oadam, f64) 10857 note and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 tol_e9 adam_ns 10858 note loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) or GACMP FAIL 10859 note (exit 46) 10860 fn ggacmp to void 10861 if ne oargc 5 10862 exit 2 10863 end 10864 ockload 2 10865 odopen 3 10866 obatch oargu 4 10867 put og 10 0 10868 p = get og 8 10869 ggopen 10870 ggupst 10871 lg = ggpass true 10872 for q ulong ocn p 10873 ggkw q p 10874 end 10875 oadam 10876 t0 = monons 10877 ggadam 10878 t1 = monons 10879 for q2 ulong ocn p 10880 ggdw q2 p 10881 end 10882 for k ulong 3 10883 put ggam k 0.0f64 10884 end 10885 for q3 ulong ocn p 10886 ggadc q3 p 10887 end 10888 mu = get ggam 1 10889 rel = div get ggam 0 mu 10890 res = div get ggam 2 mu 10891 tol = div 1.0f64 65536.0f64 10892 oputc 71 10893 oputc 65 10894 oputc 32 10895 oputu p 10896 oputc 32 10897 oputl rmic mul get ggam 0 `1000000`.0f64 10898 oputc 32 10899 oputl rmic mul mu `1000000`.0f64 10900 oputc 32 10901 oputl rmic mul rel 1000.0f64 10902 oputc 32 10903 oputl rmic mul res 1000.0f64 10904 oputc 32 10905 oputl rmic mul tol 1000.0f64 10906 oputc 32 10907 oputl sub t1 t0 10908 oputc 32 10909 oputl rmic lg 10910 oputc 10 10911 r = nvstop 10912 oputc 71 10913 oputc 65 10914 oputc 67 10915 oputc 77 10916 oputc 80 10917 oputc 32 10918 if lt res tol 10919 oputc 80 10920 oputc 65 10921 oputc 83 10922 oputc 83 10923 oputc 10 10924 oflush 10925 ret 10926 end 10927 oputc 70 10928 oputc 65 10929 oputc 73 10930 oputc 76 10931 oputc 10 10932 oflush 10933 exit 46 10934 end 10935 10936 note gtrainf CK TRAIN UNTIL EVERY: gtrain with the parameters and Adam state resident on the chip in 10937 note f32 (ggadam); only the batch goes up each step. At save points the state comes back to f64 for 10938 note ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase times. 10939 fn ggtrainf to void 10940 if ne oargc 6 10941 exit 2 10942 end 10943 ockload 2 10944 odopen 3 10945 un = oargu 4 10946 ev = oargu 5 10947 if eq ev 0 10948 exit 2 10949 end 10950 en = get og 42 10951 if lt un en 10952 en = un 10953 end 10954 ggopen 10955 ggupst 10956 while lt get og 41 en bound 999999 10957 s = get og 41 10958 obatch s 10959 put og 10 0 10960 ls = ggpass false 10961 t = monons 10962 ggadam 10963 put ggtm 5 sub monons t 10964 s1 = add s 1 10965 put og 41 s1 10966 if or eq rem s1 ev 0 eq s1 en 10967 ggdnst 10968 ocksave 2 10969 oputc 84 10970 oputc 32 10971 oputu s1 10972 oputc 32 10973 oputl rmic ls 10974 oputc 10 10975 oflush 10976 end 10977 end 10978 ggtms 10979 oflush 10980 r = nvstop 10981 end 10982 10983 note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch 10984 note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip. 10985 note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT. 10986 fn ggprof to void 10987 if ne oargc 5 10988 exit 2 10989 end 10990 ockload 2 10991 odopen 3 10992 nn = oargu 4 10993 if eq nn 0 10994 exit 2 10995 end 10996 ggopen 10997 ggupst 10998 ls = 0.0f64 10999 for i ulong nn 11000 s = get og 41 11001 obatch s 11002 put og 10 0 11003 if eq add i 1 nn 11004 put gkpf 0 1 11005 end 11006 ls = ggpass false 11007 t = monons 11008 ggadam 11009 put ggtm 5 sub monons t 11010 put og 41 add s 1 11011 end 11012 for i ulong 64 11013 ggew 20 64 ggg 0 ggg 0 11014 end 11015 put gkpf 0 0 11016 oputc 84 11017 oputc 32 11018 oputu get og 41 11019 oputc 32 11020 oputl rmic ls 11021 oputc 10 11022 for q ulong get gkpc 0 11023 o = mul q 8 11024 oputc 71 11025 oputc 80 11026 for j ulong 8 11027 oputc 32 11028 oputu get gkpl add o j 11029 end 11030 oputc 10 11031 end 11032 ggtms 11033 oflush 11034 r = nvstop 11035 end 11036 11037 note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows 11038 note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and 11039 note does not count them. Each window adds its mean row loss in window order and, when V > 256, the 11040 note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like 11041 note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval. 11042 global ggev f64 1 11043 global ggen ulong 1 11044 fn ggeval to void 11045 ww = ggevw 11046 t = monons 11047 ggopen 11048 ggupw 11049 put ggev 0 0.0f64 11050 put ggen 0 0 11051 bb = get og 6 11052 nc = div add ww sub bb 1 bb 11053 for c ulong nc 11054 ggevc c ww 11055 end 11056 ns = sub monons t 11057 ggevpr ww 11058 oputc 71 11059 oputc 69 11060 oputc 32 11061 oputl ns 11062 oputc 10 11063 oflush 11064 r = nvstop 11065 end 11066 11067 note eval's argument checks; loads CK, MERGES and HELD and returns W 11068 fn ggevw to ulong 11069 na = oargc 11070 if and ne na 5 ne na 6 11071 exit 2 11072 end 11073 ockload 2 11074 ww = oargu 4 11075 v = get og 0 11076 if gt v 256 11077 if eq na 5 11078 exit 2 11079 end 11080 obload 5 v 11081 end 11082 odopen 3 11083 tt = get og 1 11084 m = get odn 0 11085 if eq ww 0 11086 exit 23 11087 end 11088 if gt ww div sub m 1 tt 11089 exit 23 11090 end 11091 ret ww 11092 end 11093 11094 note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums 11095 fn ggevc c ulong ww ulong to void 11096 bb = get og 6 11097 tt = get og 1 11098 for b ulong bb 11099 w = add mul c bb b 11100 if ge w ww 11101 w = 0 11102 end 11103 obrow b mul w tt 11104 end 11105 ggupb 11106 ggfwd 11107 for b ulong bb 11108 w = add mul c bb b 11109 if lt w ww 11110 ggevr b 11111 end 11112 end 11113 end 11114 11115 note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes 11116 fn ggevr b ulong to void 11117 tt = get og 1 11118 o = mul b tt 11119 s = 0.0f64 11120 for t ulong tt 11121 s = add s ggrl add o t 11122 end 11123 put ggev 0 add get ggev 0 div s f64 tt 11124 if gt get og 0 256 11125 n = get ggen 0 11126 for t ulong tt 11127 n = add n obtlen get oby add o t 11128 end 11129 put ggen 0 n 11130 end 11131 end 11132 11133 note the E line of cmdeval from the sums 11134 fn ggevpr ww ulong to void 11135 lo = div get ggev 0 f64 ww 11136 bp = div lo 0.`6931471805599453` 11137 if gt get og 0 256 11138 tt = get og 1 11139 bp = div mul bp f64 mul ww tt f64 get ggen 0 11140 end 11141 oputc 69 11142 oputc 32 11143 oputu get og 41 11144 oputc 32 11145 oputu ww 11146 oputc 32 11147 oputl rmic lo 11148 oputc 32 11149 oputl rmic bp 11150 oputc 10 11151 end 11152 11153 note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip 11154 note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean 11155 note over rows times 1e9 of: d m, d l_y, d log sum, d loss, (sum of the chip's p) - 1, d (l_y - m). 11156 note It located the CE sum bias that the Kahan sum in gr.om grce removes. 11157 global gdacc f64 6 11158 global gdcpu f64 3 11159 fn ggdiag to void 11160 if ne oargc 5 11161 exit 2 11162 end 11163 ockload 2 11164 odopen 3 11165 obatch oargu 4 11166 put og 10 0 11167 ggopen 11168 ggupw 11169 ggupb 11170 ggfwd 11171 for i ulong 6 11172 put gdacc i 0.0f64 11173 end 11174 for b ulong get og 6 11175 x = ofwd b 11176 for t ulong get og 1 11177 ggdrow b t 11178 end 11179 end 11180 n = f64 ggr 11181 oputu ggr 11182 for i ulong 6 11183 oputc 32 11184 oputl rmic mul div get gdacc i n 1000.0f64 11185 end 11186 oputc 10 11187 oflush 11188 r = nvstop 11189 end 11190 11191 fn ggdrow b ulong t ulong to void 11192 r = add mul b get og 1 t 11193 ggdcpu t get oby r 11194 ggdacc r 11195 put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64 11196 end 11197 11198 note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y 11199 fn ggdcpu t ulong y ulong to void 11200 d = get og 2 11201 v = get og 0 11202 pr = add get og 18 mul t v 11203 klogit add get og 17 mul t d pr v d 11204 m = bitcast f64 ulong `18442240474082181120` 11205 for j ulong v 11206 m = fmax m get oa add pr j 11207 end 11208 s = 0.0f64 11209 for k ulong v 11210 s = add s fexp sub get oa add pr k m 11211 end 11212 put gdcpu 0 m 11213 put gdcpu 1 flog s 11214 put gdcpu 2 get oa add pr y 11215 end 11216 11217 fn ggdacc r ulong to void 11218 o = add get gga 12 mul r 12 11219 mg = sub ggf o get gdcpu 0 11220 ls = sub flog ggf add o 4 get gdcpu 1 11221 lg = sub ggf add o 8 get gdcpu 2 11222 put gdacc 0 add get gdacc 0 mg 11223 put gdacc 1 add get gdacc 1 lg 11224 put gdacc 2 add get gdacc 2 ls 11225 put gdacc 3 add get gdacc 3 sub add ls mg lg 11226 put gdacc 5 add get gdacc 5 sub lg mg 11227 end 11228 11229 note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc 11230 fn ggdps r ulong to f64 11231 v = get og 0 11232 sc = div 1.0f64 f64 ggr 11233 o = add get gga 11 mul mul r v 4 11234 s = 0.0f64 11235 for j ulong v 11236 s = add s div ggf add o mul j 4 sc 11237 end 11238 ret add s 1.0f64 11239 end 11240 11241 note gb10 platform main: gcmp, gcmp2, gtrain, gtrainf, gacmp, geval, gdiag and gprof here, the BF16 11242 note commands gtrainb, gprofb, gbcmp, gbcmp2 and gbfrag in gb.om, every other command through the CPU 11243 note main (lio.om) 11244 fn trainingmain to int 11245 if lt oargc 2 11246 exit 2 11247 end 11248 w = ocmdw 11249 if eq w `1886217063` 11250 ggcmp 11251 ret 0 11252 end 11253 if eq w `216634581863` 11254 ggcmp2 11255 ret 0 11256 end 11257 if eq w `121398885512295` 11258 ggtrain 11259 ret 0 11260 end 11261 if eq w `28831846509999207` 11262 ggtrainf 11263 ret 0 11264 end 11265 if eq w `482871566695` 11266 ggacmp 11267 ret 0 11268 end 11269 if eq w `465491617127` 11270 ggeval 11271 ret 0 11272 end 11273 if eq w `444015928423` 11274 ggdiag 11275 ret 0 11276 end 11277 if eq w `439956435047` 11278 ggprof 11279 ret 0 11280 end 11281 if eq w `27705946603156583` 11282 gbtrain 11283 ret 0 11284 end 11285 if eq w `108192095957095` 11286 gbprof 11287 ret 0 11288 end 11289 if eq w `482871566951` 11290 gbcmp 11291 ret 0 11292 end 11293 if eq w `55458452955751` 11294 gbcmp2 11295 ret 0 11296 end 11297 if eq w `113668228801127` 11298 gbfrag 11299 ret 0 11300 end 11301 ret ocpumain 11302 end 11303 note Experimental greedy GPU sampler; existing f32 forward kernels, one sequence. 11304 note Args CK N PROMPT MERGES. No checkpoint writes and no tool execution. 11305 fn gsforward to void 11306 d = get og 2 11307 x = ggl 0 0 11308 ggu 0 d 11309 ggpt 1 x 11310 x = ggw 0 11311 ggpt 2 x 11312 x = ggw get og 30 11313 ggpt 3 x 11314 x = gga1 2 11315 ggpt 4 x 11316 ggrun 9 10 ggcd d 64 get og 1 get og 6 11317 for l ulong get og 5 11318 ggfl l 11319 end 11320 x = gga1 8 11321 w = ggw get og 33 11322 z = gga1 10 11323 s = gga1 9 11324 ggrmsf x w z s 11325 x = gga1 11 11326 w = ggw 0 11327 ggxwt z w x get og 0 d false 11328 end 11329 fn gsnext to ulong 11330 ggupb 11331 gsforward 11332 o = add get gga 11 mul mul sub get og 9 1 get og 0 4 11333 best = ulong 0 11334 bv = ggf o 11335 for c ulong get og 0 11336 v = ggf add o mul c 4 11337 if gt v bv 11338 bv = v 11339 best = c 11340 end 11341 end 11342 ret best 11343 end 11344 fn main to int 11345 put tmarks 0 ulong monons 11346 iread 11347 put tmarks 1 ulong monons 11348 nn = ulong 160 11349 ockload 1 11350 put tmarks 2 ulong monons 11351 put og 6 1 11352 put og 9 0 11353 ocpush 0 11354 obload 4 get og 0 11355 np = obprompt 3 11356 put tmarks 7 np 11357 put tmarks 3 ulong monons 11358 if ge np get og 1 11359 ret 3 11360 end 11361 for i ulong np 11362 ocpush get obtk i 11363 end 11364 ggopen 11365 ggupw 11366 put tmarks 4 ulong monons 11367 for i ulong nn 11368 c = gsnext 11369 if eq c 0 11370 put tmarks 9 1 11371 break 11372 end 11373 put tmarks add i 10 ulong monons 11374 put tmarks 8 add i 1 11375 obtput c 11376 ocpush c 11377 end 11378 put tmarks 5 ulong monons 11379 oflush 11380 r = nvstop 11381 put tmarks 6 ulong monons 11382 timereport 11383 ret 0 11384 end 11385 11386 text imodel "$HOME/.local/share/omega-tool-model/round40/model.ck" 11387 text imerges "$HOME/.local/share/omega-tool-model/round40/merges.bin" 11388 text example "Task: Fix the error in this file and confirm its goal.\nFile: jobs/item76666.om\nGoal: calc starts with x and adds 4 exactly 3 times in a for loop. main calls calc with 70.\nState: unread\nAnswer:\n" 11389 note Native installed sampler input. No generated text is executed. 11390 global iprom uchar 4001 11391 global icount ulong 1 11392 global iflag uchar 16 11393 text idemo "--demo" 11394 text ihelp "--help" 11395 text iusage "Omega tool model, round40.\nUse: omega-tool-model < prompt.txt\nDemo: omega-tool-model --demo\nReads one compact current-state prompt and prints model text.\nNo tools are executed. No chat service is started.\nInput: 1..4000 bytes, no zero bytes, fewer than 256 tokens.\n" 11396 11397 fn iusageout to void 11398 off = ulong 0 11399 while lt off len iusage bound 4096 11400 r = write 1 iusage off sub len iusage off 11401 if le r 0 11402 exit 70 11403 end 11404 off = add off ulong r 11405 end 11406 end 11407 11408 fn iread to void 11409 if eq argcnt 2 11410 r = argread 1 iflag 0 len iflag 11411 if ne r 6 11412 iusageout 11413 exit 2 11414 end 11415 d = true 11416 h = true 11417 for i ulong 6 11418 if ne get iflag i get idemo i 11419 d = false 11420 end 11421 if ne get iflag i get ihelp i 11422 h = false 11423 end 11424 end 11425 if h 11426 iusageout 11427 exit 0 11428 end 11429 if not d 11430 iusageout 11431 exit 2 11432 end 11433 for i ulong len example 11434 put iprom i get example i 11435 end 11436 put icount 0 len example 11437 ret 11438 end 11439 if ne argcnt 1 11440 iusageout 11441 exit 2 11442 end 11443 n = ulong 0 11444 while lt n len iprom bound 4001 11445 r = read 0 iprom n sub len iprom n 11446 if lt r 0 11447 exit 70 11448 end 11449 if eq r 0 11450 break 11451 end 11452 n = add n ulong r 11453 end 11454 if or eq n 0 gt n 4000 11455 iusageout 11456 exit 2 11457 end 11458 for i ulong n 11459 if eq get iprom i 0 11460 exit 2 11461 end 11462 end 11463 put icount 0 n 11464 end 11465 11466 note Preserve the sampler's internal argument-index interface, with installed paths. 11467 fn installedargs i ulong to long 11468 if eq i 1 11469 for k ulong len imodel 11470 put oarg k get imodel k 11471 end 11472 ret long len imodel 11473 end 11474 if eq i 4 11475 for k ulong len imerges 11476 put oarg k get imerges k 11477 end 11478 ret long len imerges 11479 end 11480 if eq i 3 11481 for k ulong get icount 0 11482 put oarg k get iprom k 11483 end 11484 ret long get icount 0 11485 end 11486 ret neg long 22 11487 end 11488 11489 note Emit timing only after model output and device cleanup; 180 little-endian u64 values. 11490 fn timereport to void 11491 for i ulong 180 11492 for j ulong 8 11493 v = and shr get tmarks i mul j 8 255 11494 put tbytes add mul i 8 j uchar v 11495 end 11496 end 11497 p = ulong 0 11498 while lt p len tbytes bound 1440 11499 r = write 2 tbytes p sub len tbytes p 11500 if le r 0 11501 exit 70 11502 end 11503 p = add p ulong r 11504 end 11505 end