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