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