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