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-mv/blob/main/train/omega-llm/native/gpu/gb.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / train / omega-llm / native / gpu / gb.om` 1006 lines ยท 21856 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note gb.om: ollm BF16 tensor-core path, commands and gates (docs/omegallm/CONTRACT-BF16.md). With gkbf 1, 3 note gk.om runs every 64x64 gkmm on kernels 27..36 (HMMA.16816.F32.BF16; the kernel rounds both operands 4 note to bf16 RN with F2FP and accumulates in f32). Commands: gtrainb and gprofb (gtrainf and gprof with 5 note gkbf 1), gbcmp (sampled check of every BF16 GEMM of one step, exit 48), gbcmp2 (gcmp2's step parity 6 note with the BF16 bound, exit 49) and gbfrag (exact check of all ten kernels on fixed data, exit 50). 7 note Concatenated before gg.om: it calls gg.om functions but uses none of gg.om's globals. 8 9 note OMUTU: 1 in the product; 0 in mutant u, whose reference skips the bf16 rounding (gbfrag must then 10 note fail; gbcmp only reports it, CONTRACT-BF16 section 5). 11 const OMUTU ulong 1 12 13 note OMUTN: 1 in the product; 0 in mutant n, the negative control of gbfrag's padding check: after each 14 note short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, column n), so 15 note only that check can fail (CONTRACT-BF16 section 4.1). 16 const OMUTN ulong 1 17 18 note bf16 round to nearest even of the f32 bits u, as f32 bits (NaN unchanged) 19 fn gbbf u uint to uint 20 if eq OMUTU 0 21 ret u 22 end 23 if and eq and shr u 23 255 255 ne and u `8388607` 0 24 ret u 25 end 26 v = add add u 32767 and shr u 16 1 27 ret and v `4294901760` 28 end 29 30 note f32 bits x rounded to bf16, as f64 31 fn gbrv x uint to f64 32 ret f64 bitcast f32 gbbf x 33 end 34 35 fn gbabs x f64 to f64 36 if lt x 0.0f64 37 ret neg x 38 end 39 ret x 40 end 41 42 note arena word at host byte offset o (offset from gkbase) 43 fn gbw o ulong to uint 44 ret uint mapget get gbmem 5 o 4 45 end 46 47 fn gbput o ulong x uint to void 48 m = mapset get gbmem 5 o 4 ulong x 49 end 50 51 note host byte offset of the device address held in arg words w, w + 1 52 fn gbhp w uint to ulong 53 lo = ulong get gkarg w 54 hi = ulong get gkarg add w 1 55 ret sub or lo shl hi 32 gkbase 56 end 57 58 note batch offset z1 s1 + z2 s2 (z = z1 h2 + z2) with s1, s2 in arg words w, w + 1 59 fn gbzo z uint h2 uint w uint to ulong 60 a = mul ulong div z h2 ulong get gkarg w 61 ret add a mul ulong rem z h2 ulong get gkarg add w 1 62 end 63 64 note verdict line GBCMP (c 48), GBCMP2 (49) or GBFRAG (50): PASS, or FAIL and exit c 65 fn gbvd c ulong ok bool to void 66 oputc 71 67 oputc 66 68 if eq c 50 69 oputc 70 70 oputc 82 71 oputc 65 72 oputc 71 73 else 74 oputc 67 75 oputc 77 76 oputc 80 77 if eq c 49 78 oputc 50 79 end 80 end 81 oputc 32 82 if ok 83 oputc 80 84 oputc 65 85 oputc 83 86 oputc 83 87 oputc 10 88 oflush 89 ret 90 end 91 oputc 70 92 oputc 65 93 oputc 73 94 oputc 76 95 oputc 10 96 oflush 97 if eq c 48 98 exit 48 99 end 100 if eq c 49 101 exit 49 102 end 103 exit 50 104 end 105 106 note gbcmp state. gbcst (gbst until ham/ollm-speed: batch.om owns that name): 0 BF16 GEMMs checked, 1 samples past their bound, 2 kernel id mask, 3 cz mask, 107 note 4 ep mask, 5 beta mask, 6 the sample generator. gbmx[0]: the worst |chip - ref| / bound. gbsp: the 108 note 66 sampled (z, r, c) of the launch in flight, gbold the old C there (beta), gbacc the reference sums. 109 global gbcst ulong 8 110 global gbmx f64 1 111 global gbsp uint 198 112 global gbold f64 66 113 global gbacc f64 3 114 115 note next value in [0, n) of the sample generator (31-bit LCG) 116 fn gbrnd n uint to uint 117 s = rem add mul get gbcst 6 `1103515245` 12345 `2147483648` 118 put gbcst 6 s 119 ret uint rem shr s 8 ulong n 120 end 121 122 note host byte offset at sample i in C (w 16) or in the epilogue's Y / M (w 30, C's strides) 123 fn gbco i ulong w uint to ulong 124 o = mul i 3 125 z = get gbsp o 126 b = add gbhp w gbzo z get gkbp 4 20 127 b = add b mul ulong get gbsp add o 1 ulong get gkarg 18 128 ret add b mul ulong get gbsp add o 2 ulong get gkarg 19 129 end 130 131 note gkmm's hook before a checked BF16 launch (gkbp = m n k nz h2 beta ep cz): 64 random (z, r, c) 132 note and the corners (0, 0, 0), (nz - 1, m - 1, n - 1); with cz 3 a position in a block above the 133 note diagonal (not computed) is mirrored below it. Keeps the old C when beta. 134 fn gbpre to void 135 for i ulong 66 136 gbpre1 i 137 end 138 end 139 140 note one sample i of gbpre 141 fn gbpre1 i ulong to void 142 z = gbrnd get gkbp 3 143 r = gbrnd get gkbp 0 144 c = gbrnd get gkbp 1 145 if eq i 64 146 z = 0 147 r = 0 148 c = 0 149 end 150 if eq i 65 151 z = sub get gkbp 3 1 152 r = sub get gkbp 0 1 153 c = sub get gkbp 1 1 154 end 155 if eq get gkbp 7 3 156 if gt div c 64 div r 64 157 t = r 158 r = c 159 c = t 160 end 161 end 162 o = mul i 3 163 put gbsp o z 164 put gbsp add o 1 r 165 put gbsp add o 2 c 166 put gbold i 0.0f64 167 if eq get gkbp 5 1 168 put gbold i f64 bitcast f32 gbw gbco i 16 169 end 170 end 171 172 note reference sums of sample i over the K range the kernel reads (cz 1: k < (r / 64 + 1) 64, 173 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 174 fn gbref i ulong to void 175 r = ulong get gbsp add mul i 3 1 176 k = ulong get gkbp 2 177 lo = ulong 0 178 hi = mul add div r 64 1 64 179 cz = get gkbp 7 180 if eq cz 1 181 if lt hi k 182 k = hi 183 end 184 end 185 if eq cz 2 186 lo = mul div r 64 64 187 end 188 gbdot gbsa i 0 gbsa i 1 lo sub k lo 189 end 190 191 note host address of sample i's A row (w 0) or B column (w 1) at k = 0 192 fn gbsa i ulong w uint to ulong 193 o = mul i 3 194 z = get gbsp o 195 h2 = get gkbp 4 196 g = mul w 8 197 x = ulong get gbsp add o ulong add w 1 198 p = add gbhp g gbzo z h2 add g 4 199 ret add p mul x ulong get gkarg add g 2 200 end 201 202 fn gbdot pa ulong pb ulong lo ulong kn ulong to void 203 ka = ulong get gkarg 3 204 kb = ulong get gkarg 11 205 s = 0.0f64 206 sa = 0.0f64 207 for j ulong kn 208 kk = add lo j 209 p = mul gbrv gbw add pa mul kk ka gbrv gbw add pb mul kk kb 210 s = add s p 211 sa = add sa gbabs p 212 end 213 put gbacc 0 s 214 put gbacc 1 sa 215 put gbacc 2 f64 kn 216 end 217 218 note d / b, 0 when both are 0; 1e9 when d > b with b = 0 or the ratio non-finite or past 1e9 219 fn gbrat d f64 b f64 to f64 220 if le d b 221 if gt b 0.0f64 222 ret div d b 223 end 224 ret 0.0f64 225 end 226 if gt b 0.0f64 227 q = div d b 228 if lt q `1000000000`.0f64 229 ret q 230 end 231 end 232 ret `1000000000`.0f64 233 end 234 235 note |chip - ref| / bound at sample i, bound (kn 2^-23 + 2^-22)(|alpha| sum |a b| + |old C|); 1e9 236 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) 237 fn gbchk i ulong to f64 238 co = gbco i 16 239 cb = gbw co 240 ep = get gkbp 6 241 if eq ep 2 242 if not gt f64 bitcast f32 gbw gbco i 30 0.0f64 243 if eq cb 0 244 ret 0.0f64 245 end 246 ret `1000000000`.0f64 247 end 248 end 249 if eq ep 1 250 w = uint 0 251 if gt f64 bitcast f32 cb 0.0f64 252 w = cb 253 end 254 if ne gbw gbco i 30 w 255 ret `1000000000`.0f64 256 end 257 end 258 gbref i 259 al = f64 bitcast f32 get gkarg 25 260 old = get gbold i 261 d = gbabs sub f64 bitcast f32 cb add mul al get gbacc 0 old 262 e = add div get gbacc 2 `8388608`.0f64 div 1.0f64 `4194304`.0f64 263 ret gbrat d mul e add mul gbabs al get gbacc 1 gbabs old 264 end 265 266 note kernel id of the launch in flight (gk.om gkmmb) 267 fn gbid to ulong 268 ep = ulong get gkbp 6 269 if ne ep 0 270 ret add 34 ep 271 end 272 id = add 27 ulong get gkmd 0 273 id = add id mul 2 ulong get gkmd 1 274 ret add id mul 4 ulong get gkbp 5 275 end 276 277 note gkmm's hook after a checked BF16 launch: check the samples, print 278 note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict) 279 fn gbpost to bool 280 bad = ulong 0 281 mr = 0.0f64 282 for i ulong 66 283 q = gbchk i 284 if gt q 1.0f64 285 bad = add bad 1 286 end 287 if gt q mr 288 mr = q 289 end 290 end 291 gbnote bad mr 292 ret true 293 end 294 295 fn gbnote bad ulong mr f64 to void 296 id = gbid 297 put gbcst 0 add get gbcst 0 1 298 put gbcst 1 add get gbcst 1 bad 299 put gbcst 2 or get gbcst 2 shl ulong 1 id 300 put gbcst 3 or get gbcst 3 shl ulong 1 ulong get gkbp 7 301 put gbcst 4 or get gbcst 4 shl ulong 1 ulong get gkbp 6 302 put gbcst 5 or get gbcst 5 shl ulong 1 ulong get gkbp 5 303 if gt mr get gbmx 0 304 put gbmx 0 mr 305 end 306 oputc 71 307 oputc 66 308 oputc 32 309 oputu id 310 for j ulong 8 311 oputc 32 312 oputu ulong get gkbp j 313 end 314 oputc 32 315 oputl rmic mr 316 oputc 32 317 oputu bad 318 oputc 10 319 end 320 321 note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so 322 note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6 323 note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound, 324 note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta 325 note false and true). 326 fn gbcmp to void 327 if ne oargc 5 328 exit 2 329 end 330 ockloop 2 331 odopen 3 332 obatch oargu 4 333 put og 10 0 334 put gbcst 6 12345 335 put gkbf 0 1 336 put gkbck 0 1 337 ggopen 338 l = gggrad 339 put gkbck 0 0 340 r = nvstop 341 oputc 71 342 oputc 66 343 oputc 67 344 for j ulong 2 345 oputc 32 346 oputu get gbcst j 347 end 348 oputc 32 349 oputl rmic get gbmx 0 350 for j ulong 4 351 oputc 32 352 oputu get gbcst add j 2 353 end 354 oputc 10 355 ok = and eq get gbcst 1 0 gt get gbcst 0 0 356 cv = eq and get gbcst 4 6 6 357 cv = and cv ne and get gbcst 3 8 0 358 cv = and cv eq get gbcst 5 3 359 gbvd 48 and ok cv 360 end 361 362 note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest 363 note (L: the layer applications og 56, which is the layer count when the model has no loop) 364 note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV; 365 note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic 366 fn gbtolb to f64 367 nc = f64 add mul 10 get og 56 3 368 ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64 369 end 370 371 note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss 372 fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong 373 tb = gbtolb 374 fl = ulong 0 375 if ne nf 0 376 fl = or fl 1 377 end 378 if not lt ggclp rho tb 379 fl = or fl 2 380 end 381 if not lt gg2rl 2 tb 382 fl = or fl 4 383 end 384 if not lt gbabs sub lg l0 mul tb gbabs l0 385 fl = or fl 8 386 end 387 ret fl 388 end 389 390 note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p)) 391 global gbcq f64 3 392 fn gbcos p ulong to f64 393 for k ulong 3 394 put gbcq k 0.0f64 395 end 396 for q ulong ocn p 397 i0 = mul q OCH 398 for j ulong sub oce q p i0 399 gbcq1 add i0 j p 400 end 401 end 402 d = fsqrt mul get gbcq 1 get gbcq 2 403 if gt d 0.0f64 404 ret ggclp div get gbcq 0 d 405 end 406 ret 0.0f64 407 end 408 409 fn gbcq1 i ulong p ulong to void 410 x = omvg i 411 y = omvg add p i 412 put gbcq 0 add get gbcq 0 mul x y 413 put gbcq 1 add get gbcq 1 mul x x 414 put gbcq 2 add get gbcq 2 mul y y 415 end 416 417 note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49) 418 fn gbc2vd lg f64 l0 f64 fl ulong to void 419 cs = gbcos get og 8 420 oputc 71 421 oputc 66 422 oputc 50 423 oputc 32 424 oputu get og 56 425 oputc 32 426 oputu add mul 10 get og 56 3 427 oputc 32 428 oputl rmic mul gbtolb 1000.0f64 429 oputc 32 430 oputl rmic mul cs 1000.0f64 431 oputc 32 432 oputl rmic mul gg2rl 0 1000.0f64 433 oputc 32 434 oputl rmic mul gg2rl 2 1000.0f64 435 oputc 32 436 oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64 437 oputc 10 438 gbvd 49 eq fl 0 439 end 440 441 note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd) 442 fn gbcmp2 to void 443 put gkbf 0 1 444 ggcmp2 445 end 446 447 note gtrainb: gtrainf with gkbf 1 (geval stays f32) 448 fn gbtrain to void 449 put gkbf 0 1 450 ggtrainf 451 end 452 453 note gprofb: gprof with gkbf 1 454 fn gbprof to void 455 put gkbf 0 1 456 ggprof 457 end 458 459 note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by 460 note element against a reference built from the bf16-rounded operands. One operand holds integers in 461 note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9 462 note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen 463 note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles 464 note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the 465 note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN 466 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, 467 note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL 468 note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi). 469 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 470 global gbcs uint 10 471 global gbfb ulong 1 472 global gbha f64 98304 473 global gbhb f64 147456 474 475 fn gbfrag to void 476 if ne oargc 3 477 exit 2 478 end 479 ockloop 2 480 ggopen 481 if lt mul ggr get og 0 `4194304` 482 ggdie 3 483 end 484 put gkbf 0 1 485 put gkbck 0 0 486 put gbfb 0 sub gga1 11 gkbase 487 bad = ulong 0 488 for p ulong 2 489 for i ulong 13 490 bad = add bad gbf1 i p 491 end 492 end 493 for p ulong 2 494 for q uint 4 495 bad = add bad gbfl p and q 1 shr q 1 496 end 497 end 498 r = nvstop 499 oputc 71 500 oputc 70 501 oputc 83 502 oputc 32 503 oputu bad 504 oputc 10 505 gbvd 50 eq bad 0 506 end 507 508 note case i of pass p into gbcs 509 fn gbcase i ulong p ulong to void 510 for j ulong 10 511 put gbcs j 0 512 end 513 put gbcs 8 uint p 514 put gbcs 6 128 515 put gbcs 7 192 516 put gbcs 5 128 517 if eq rem i 3 0 518 put gbcs 5 32 519 end 520 if eq rem i 3 1 521 put gbcs 5 96 522 put gbcs 9 1 523 end 524 if lt i 8 525 put gbcs 0 uint and i 1 526 put gbcs 1 uint and shr i 1 1 527 put gbcs 2 uint and shr i 2 1 528 ret 529 end 530 put gbcs 9 0 531 put gbcs 0 1 532 if eq i 8 533 put gbcs 3 1 534 put gbcs 5 96 535 end 536 if eq i 9 537 put gbcs 1 1 538 put gbcs 3 2 539 end 540 note cz 1 needs k = m 541 if eq i 10 542 put gbcs 4 1 543 put gbcs 5 128 544 end 545 if eq i 11 546 put gbcs 0 0 547 put gbcs 2 1 548 put gbcs 4 2 549 end 550 if eq i 12 551 put gbcs 1 1 552 put gbcs 4 3 553 put gbcs 5 32 554 put gbcs 7 128 555 put gbcs 9 1 556 end 557 end 558 559 note one case: write the data, launch, compare; the mismatch count 560 fn gbf1 i ulong p ulong to ulong 561 gbcase i p 562 gbnan 563 gbwop 0 564 gbwop 1 565 gbwc 566 put gkinv 0 1 567 if not gbflch 568 ggdie 98 569 end 570 if eq OMUTN 0 571 n = ulong get gbcs 7 572 gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0 573 end 574 b = gbfcmp 575 ep = ulong get gbcs 3 576 id = add 27 ulong get gbcs 0 577 id = add id mul 2 ulong get gbcs 1 578 id = add id mul 4 ulong get gbcs 2 579 if ne ep 0 580 id = add 34 ep 581 end 582 oputc 71 583 oputc 70 584 oputc 32 585 oputu p 586 oputc 32 587 oputu i 588 oputc 32 589 oputu id 590 oputc 32 591 oputu ulong get gbcs 5 592 oputc 32 593 oputu ulong get gbcs 4 594 oputc 32 595 oputu ep 596 oputc 32 597 oputu b 598 oputc 10 599 ret b 600 end 601 602 note NaN in every A and B word (2 MiB) 603 fn gbnan to void 604 b = get gbfb 0 605 for i ulong 524288 606 gbput add b mul i 4 `2143289344` 607 end 608 end 609 610 note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B); 611 note batch z at (z / 3)(3 zs + 64) + (z % 3) zs 612 fn gbzs md uint w ulong kd ulong to ulong 613 if eq md 1 614 ret mul w add kd 8 615 end 616 ret mul kd add w 16 617 end 618 619 fn gbzof z ulong zs ulong to ulong 620 ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs 621 end 622 623 fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong 624 if eq md 1 625 ret add mul x add kd 8 kk 626 end 627 ret add x mul kk add w 16 628 end 629 630 note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z) 631 fn gbfr x ulong kk ulong z ulong to f64 632 s = add mul x 7 mul kk 3 633 s = add s mul z 5 634 a = sub f64 rem s 5 2.0f64 635 s = add mul x 13 mul kk 11 636 s = add s mul z 3 637 b = f64 rem s 64 638 ret add a div b 512.0f64 639 end 640 641 fn gbint x ulong kk ulong z ulong to f64 642 s = add mul x 5 mul kk 7 643 s = add s mul z 3 644 ret sub f64 rem s 11 5.0f64 645 end 646 647 note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded 648 note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped. 649 fn gbwop ab uint to void 650 md = get gbcs ab 651 k = ulong get gbcs 5 652 w = ulong get gbcs add 6 ab 653 zs = gbzs md w k 654 base = add get gbfb 0 mul ulong ab `1048576` 655 fr = eq get gbcs 8 ab 656 for z ulong 6 657 for x ulong w 658 gbwrow ab z x base zs fr 659 end 660 end 661 end 662 663 fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void 664 zb = add base mul gbzof z zs 4 665 t = add 128 mul 64 ulong ab 666 hi = mul add mul z t x 128 667 k = ulong get gbcs 5 668 for kk ulong k 669 gbwel ab zb x kk gbwv x kk z fr hi 670 end 671 end 672 673 note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer 674 fn gbwv x ulong kk ulong z ulong fr bool to uint 675 v = gbint x kk z 676 if fr 677 v = gbfr x kk z 678 end 679 ret bitcast uint f32 v 680 end 681 682 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 683 fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void 684 md = get gbcs ab 685 w = ulong get gbcs add 6 ab 686 o = gbopf md x kk w ulong get gbcs 5 687 gbput add zb mul o 4 u 688 if eq ab 0 689 put gbha add hi kk gbrv u 690 else 691 put gbhb add hi kk gbrv u 692 end 693 end 694 695 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, 696 note else ((r + 3 c + z) mod 3) - 1, as f32 bits 697 fn gbold3 z ulong r ulong c ulong to f64 698 s = add mul r 3 mul c 5 699 s = add s z 700 ret sub f64 rem s 17 8.0f64 701 end 702 703 fn gbmv z ulong r ulong c ulong to uint 704 if eq rem add mul r 5 c 13 0 705 ret `2143289344` 706 end 707 ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64 708 end 709 710 note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns 711 note (and Y's / M's with a fused epilogue) are NaN 712 fn gbwc to void 713 m = ulong get gbcs 6 714 zs = mul m add ulong get gbcs 7 8 715 for z ulong 6 716 for r ulong m 717 gbwcr z r zs 718 end 719 end 720 end 721 722 fn gbwcr z ulong r ulong zs ulong to void 723 n = ulong get gbcs 7 724 ep = get gbcs 3 725 o = gbcro z r zs 726 for c ulong n 727 oc = add o mul c 4 728 gbput oc bitcast uint f32 gbold3 z r c 729 if eq ep 1 730 gbput add oc `1048576` bitcast uint f32 77.0f64 731 end 732 if eq ep 2 733 gbput add oc `1048576` gbmv z r c 734 end 735 end 736 for c ulong 8 737 op = add o mul add n c 4 738 gbput op `2143289344` 739 if ne ep 0 740 gbput add op `1048576` `2143289344` 741 end 742 end 743 end 744 745 note operand ab's gkopa for the case at device address p 746 fn gbfopa ab uint p ulong to void 747 md = get gbcs ab 748 k = get gbcs 5 749 w = get gbcs add 6 ab 750 zs = uint gbzs md ulong w ulong k 751 so = mul add k 8 4 752 sk = uint 4 753 if eq md 0 754 so = 4 755 sk = mul add w 16 4 756 end 757 gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4 758 end 759 760 fn gbflch to bool 761 d = add gkbase get gbfb 0 762 gbfopa 0 d 763 gbfopa 1 add d `1048576` 764 n = get gbcs 7 765 zc = mul get gbcs 6 add n 8 766 al = 1.0f64 767 if eq get gbcs 9 1 768 al = 0.5f64 769 end 770 sm = mul add n 8 4 771 s1 = mul add mul zc 3 64 4 772 s2 = mul zc 4 773 gkopc add d `2097152` sm 4 s1 s2 f32 al 774 if ne get gbcs 3 0 775 gkope get gbcs 3 add d `3145728` 776 end 777 if ne get gbcs 4 0 778 gkopz get gbcs 4 779 end 780 m = get gbcs 6 781 k = get gbcs 5 782 bt = eq get gbcs 2 1 783 ret gkmm m n k 6 3 bt 784 end 785 786 note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN 787 fn gbfcmp to ulong 788 bad = ulong 0 789 for z ulong 6 790 for r ulong ulong get gbcs 6 791 bad = add bad gbfrow z r 792 end 793 end 794 ret bad 795 end 796 797 note host byte offset of C row r of block z (row stride n + 8 f32) 798 fn gbcro z ulong r ulong zs ulong to ulong 799 n = ulong get gbcs 7 800 b = add get gbfb 0 `2097152` 801 b = add b mul gbzof z zs 4 802 ret add b mul mul r add n 8 4 803 end 804 805 fn gbfrow z ulong r ulong to ulong 806 n = ulong get gbcs 7 807 zs = mul ulong get gbcs 6 add n 8 808 o = gbcro z r zs 809 bad = ulong 0 810 for c ulong n 811 if not gbfel z r c add o mul c 4 812 bad = add bad 1 813 end 814 end 815 for c ulong 8 816 op = add o mul add n c 4 817 if ne gbw op `2143289344` 818 bad = add bad 1 819 end 820 if ne get gbcs 3 0 821 if ne gbw add op `1048576` `2143289344` 822 bad = add bad 1 823 end 824 end 825 end 826 ret bad 827 end 828 829 note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2 830 note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0 831 fn gbfel z ulong r ulong c ulong oc ulong to bool 832 cb = gbw oc 833 old = gbold3 z r c 834 if and eq get gbcs 4 3 gt div c 64 div r 64 835 ret eq cb bitcast uint f32 old 836 end 837 ep = get gbcs 3 838 if eq ep 2 839 if not gt f64 bitcast f32 gbmv z r c 0.0f64 840 ret eq cb 0 841 end 842 end 843 s = gbfref z r c 844 if eq get gbcs 2 1 845 s = add s old 846 end 847 if ne f64 bitcast f32 cb s 848 ret false 849 end 850 if eq ep 1 851 w = uint 0 852 if gt s 0.0f64 853 w = cb 854 end 855 ret eq gbw add oc `1048576` w 856 end 857 ret true 858 end 859 860 note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands) 861 fn gbfref z ulong r ulong c ulong to f64 862 k = ulong get gbcs 5 863 lo = ulong 0 864 hi = mul add div r 64 1 64 865 cz = get gbcs 4 866 if and eq cz 1 lt hi k 867 k = hi 868 end 869 if eq cz 2 870 lo = mul div r 64 64 871 end 872 ia = mul add mul z 128 r 128 873 s = gbfdot ia mul add mul z 192 c 128 lo sub k lo 874 if eq get gbcs 9 1 875 s = mul s 0.5f64 876 end 877 ret s 878 end 879 880 fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64 881 s = 0.0f64 882 for j ulong kn 883 kk = add lo j 884 a = get gbha add ia kk 885 s = add s mul a get gbhb add ib kk 886 end 887 ret s 888 end 889 890 note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0, 891 note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile), 892 note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples 893 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, 894 note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`. 895 fn gbfl p ulong ma uint mb uint to ulong 896 for j ulong 10 897 put gbcs j 0 898 end 899 put gbcs 0 ma 900 put gbcs 1 mb 901 put gbcs 5 16384 902 put gbcs 6 64 903 put gbcs 7 64 904 put gbcs 8 uint p 905 b = get gbfb 0 906 for i ulong `3162112` 907 gbput add b mul i 4 `2143289344` 908 end 909 gblw 0 910 gblw 1 911 put gkinv 0 1 912 if not gbllch 913 ggdie 98 914 end 915 bad = ulong 0 916 for r ulong 64 917 bad = add bad gblrow r 918 end 919 oputc 71 920 oputc 70 921 oputc 76 922 oputc 32 923 oputu p 924 oputc 32 925 oputu ulong ma 926 oputc 32 927 oputu ulong mb 928 oputc 32 929 oputu bad 930 oputc 10 931 ret bad 932 end 933 934 note operand ab of the long case: nonzero words only at kk = 0 mod 29 935 fn gblw ab uint to void 936 md = get gbcs ab 937 base = add get gbfb 0 mul ulong ab `6291456` 938 fr = eq get gbcs 8 ab 939 for x ulong 64 940 gblwr md base x fr 941 end 942 end 943 944 fn gblwr md uint base ulong x ulong fr bool to void 945 w = ulong 64 946 kd = ulong 16384 947 for kk ulong kd 948 u = uint 0 949 if eq rem kk 29 0 950 u = gbwv x kk 0 fr 951 end 952 o = gbopf md x kk w kd 953 gbput add base mul o 4 u 954 end 955 end 956 957 note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats 958 fn gbllch to bool 959 d = add gkbase get gbfb 0 960 gblopa 0 d 961 gblopa 1 add d `6291456` 962 gkopc add d `12582912` 288 4 0 0 f32 1.0f64 963 gkopz 0 964 ret gkmm 64 64 16384 1 1 false 965 end 966 967 fn gblopa ab uint p ulong to void 968 so = uint 65568 969 sk = uint 4 970 if eq get gbcs ab 0 971 so = 4 972 sk = 320 973 end 974 gkopa ab p so sk 0 0 975 end 976 977 note row r of the long case: 64 exact values, then 8 padding words that must still be NaN 978 fn gblrow r ulong to ulong 979 o = add add get gbfb 0 `12582912` mul r 288 980 bad = ulong 0 981 for c ulong 64 982 cb = gbw add o mul c 4 983 if ne f64 bitcast f32 cb gblref r c 984 bad = add bad 1 985 end 986 end 987 for c ulong 8 988 if ne gbw add o mul add 64 c 4 `2143289344` 989 bad = add bad 1 990 end 991 end 992 ret bad 993 end 994 995 note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk) 996 fn gblref r ulong c ulong to f64 997 fa = eq get gbcs 8 0 998 fb = eq get gbcs 8 1 999 s = 0.0f64 1000 for j ulong 565 1001 kk = mul j 29 1002 a = gbrv gbwv r kk 0 fa 1003 s = add s mul a gbrv gbwv c kk 0 fb 1004 end 1005 ret s 1006 end