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-helix/blob/main/native/gpu/gb.om # omega-helix This project has not described itself yet. 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-helix / native / gpu / gb.om` 1894 lines ยท 38761 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 gkmm / gkmmsw sets gklt) 267 fn gbid to ulong 268 ret ulong get gklt 0 269 end 270 271 note gkmm's hook after a checked BF16 launch: check the samples, print 272 note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict) 273 fn gbpost to bool 274 bad = ulong 0 275 mr = 0.0f64 276 for i ulong 66 277 q = gbchk i 278 if gt q 1.0f64 279 bad = add bad 1 280 end 281 if gt q mr 282 mr = q 283 end 284 end 285 gbnote bad mr 286 ret true 287 end 288 289 fn gbnote bad ulong mr f64 to void 290 id = gbid 291 put gbcst 0 add get gbcst 0 1 292 put gbcst 1 add get gbcst 1 bad 293 put gbcst 2 or get gbcst 2 shl ulong 1 id 294 put gbcst 3 or get gbcst 3 shl ulong 1 ulong get gkbp 7 295 put gbcst 4 or get gbcst 4 shl ulong 1 ulong get gkbp 6 296 put gbcst 5 or get gbcst 5 shl ulong 1 ulong get gkbp 5 297 if gt mr get gbmx 0 298 put gbmx 0 mr 299 end 300 oputc 71 301 oputc 66 302 oputc 32 303 oputu id 304 for j ulong 8 305 oputc 32 306 oputu ulong get gkbp j 307 end 308 oputc 32 309 oputl rmic mr 310 oputc 32 311 oputu bad 312 oputc 10 313 end 314 315 note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so 316 note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6 317 note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound, 318 note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta 319 note false and true). 320 fn gbcmp to void 321 if ne oargc 5 322 exit 2 323 end 324 ockloop 2 325 odopen 3 326 obatch oargu 4 327 put og 10 0 328 put gbcst 6 12345 329 put gkbf 0 1 330 put gkbck 0 1 331 ggopen 332 l = gggrad 333 put gkbck 0 0 334 r = nvstop 335 oputc 71 336 oputc 66 337 oputc 67 338 for j ulong 2 339 oputc 32 340 oputu get gbcst j 341 end 342 oputc 32 343 oputl rmic get gbmx 0 344 for j ulong 4 345 oputc 32 346 oputu get gbcst add j 2 347 end 348 oputc 10 349 ok = and eq get gbcst 1 0 gt get gbcst 0 0 350 cv = eq and get gbcst 4 6 6 351 cv = and cv gbcz3 352 cv = and cv eq get gbcst 5 3 353 gbvd 48 and ok cv 354 end 355 356 note gbcmp's cz 3 coverage: a BF16 causal score GEMM (S = Q K^T or DP = DO U^T, m = n = T) runs only 357 note on the 64 x 64 tile, so it is required only when T is a multiple of 64; at T 32 (the small shape 358 note of docs/LOOP.md) both stay f32 and the requirement could never be met 359 fn gbcz3 to bool 360 if ne rem get og 1 64 0 361 ret true 362 end 363 ret ne and get gbcst 3 8 0 364 end 365 366 note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest 367 note (L: the layer applications og 56, which is the layer count when the model has no loop) 368 note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV; 369 note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic 370 fn gbtolb to f64 371 nc = f64 add mul 10 get og 56 3 372 ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64 373 end 374 375 note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss, 376 note 16 and 32 the per-tensor check (gbtens) 377 fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong 378 tb = gbtolb 379 fl = ulong 0 380 if ne nf 0 381 fl = or fl 1 382 end 383 if not lt ggclp rho tb 384 fl = or fl 2 385 end 386 if not lt gg2rl 2 tb 387 fl = or fl 4 388 end 389 if not lt gbabs sub lg l0 mul tb gbabs l0 390 fl = or fl 8 391 end 392 ret or fl gbtens get og 8 393 end 394 395 note gbcmp2's per-tensor check. gbtolb is a normwise bound over the whole gradient that grows with the 396 note applications (0.65 at 8, 1.28 at 16): it cannot catch a wrong gradient. For every parameter 397 note tensor k (0 wte, 1 wpe, 2 the final gain, then g1 wq wk wv wo g2 w1 w2 of each parameter layer) 398 note the chip gradient x is compared with K0 (CPU f64, its own ReLU mask, omv[P, 2P)) and K1 (CPU f64, 399 note the chip's ReLU mask, GRD): cos = x.y / (|x| |y|) and rel = |x - y| / |y|. Fail bit 16: a 400 note cosine below GBCOS 0.995; 32: a rel at or above GBREL 0.1 = sqrt(2 (1 - 0.995)), the normwise 401 note error at which an error perpendicular to y alone brings the cosine down to 0.995. rel catches 402 note what the cosine cannot: a gradient that keeps its direction and loses scale, as in mutant lg, 403 note where a core w2 keeps one of its NP passes. 404 const GBCOS f64 0.995 405 const GBREL f64 0.1 406 note gbta: sums over the tensor in flight, 0 x.y0 1 x.x 2 y0.y0 3 |x - y0|^2 4 x.y1 5 y1.y1 407 note 6 |x - y1|^2; gbtw: 0 least cos0, 1 largest rel0, 2 least cos1, 3 largest rel1, 4 tensor of 408 note rel0, 5 tensor of rel1; gbtf: the fail bits 409 global gbta f64 7 410 global gbtw f64 6 411 global gbtf ulong 1 412 fn gbtens p ulong to ulong 413 put gbtf 0 0 414 put gbtw 0 2.0f64 415 put gbtw 1 0.0f64 416 put gbtw 2 2.0f64 417 put gbtw 3 0.0f64 418 put gbtw 4 0.0f64 419 put gbtw 5 0.0f64 420 for k ulong gbtn 421 gbt1 k p 422 end 423 ret get gbtf 0 424 end 425 426 fn gbtn to ulong 427 ret add 3 mul 8 get og 5 428 end 429 430 note offset of tensor j inside a parameter layer (model.om opg1 .. opw2); j 8 is the layer's end 431 fn gbjo j ulong to ulong 432 d = get og 2 433 dd = mul d d 434 if eq j 0 435 ret 0 436 end 437 if le j 5 438 ret add d mul sub j 1 dd 439 end 440 if eq j 6 441 ret add mul 2 d mul 4 dd 442 end 443 if eq j 7 444 ret add add mul 2 d mul 4 dd mul d get og 4 445 end 446 ret get og 32 447 end 448 449 note first parameter of tensor k, and (w 1) the one after its last 450 fn gbtb k ulong w ulong to ulong 451 if eq k 0 452 ret mul w get og 30 453 end 454 if eq k 1 455 ret get og add 30 w 456 end 457 if eq k 2 458 ret add get og 33 mul w get og 2 459 end 460 q = sub k 3 461 ret add opg1 div q 8 gbjo add rem q 8 w 462 end 463 464 fn gbt1 k ulong p ulong to void 465 for j ulong 7 466 put gbta j 0.0f64 467 end 468 i = gbtb k 0 469 e = gbtb k 1 470 while lt i e bound `9223372036854775807` 471 gbta1 i p 472 i = add i 1 473 end 474 gbtacc k 475 end 476 477 fn gbta1 i ulong p ulong to void 478 x = omvg i 479 y = omvg add p i 480 z = ocsg i 481 put gbta 0 add get gbta 0 mul x y 482 put gbta 1 add get gbta 1 mul x x 483 put gbta 2 add get gbta 2 mul y y 484 y = sub x y 485 put gbta 3 add get gbta 3 mul y y 486 put gbta 4 add get gbta 4 mul x z 487 put gbta 5 add get gbta 5 mul z z 488 z = sub x z 489 put gbta 6 add get gbta 6 mul z z 490 end 491 492 note cosine x.y / sqrt(x.x y.y): 1 when both are zero, 0 when one is 493 fn gbcsn xy f64 xx f64 yy f64 to f64 494 if and eq xx 0.0f64 eq yy 0.0f64 495 ret 1.0f64 496 end 497 d = fsqrt mul xx yy 498 if gt d 0.0f64 499 ret div xy d 500 end 501 ret 0.0f64 502 end 503 504 fn gbtacc k ulong to void 505 c0 = gbcsn get gbta 0 get gbta 1 get gbta 2 506 c1 = gbcsn get gbta 4 get gbta 1 get gbta 5 507 r0 = gbrat fsqrt get gbta 3 fsqrt get gbta 2 508 r1 = gbrat fsqrt get gbta 6 fsqrt get gbta 5 509 if or not ge c0 GBCOS not ge c1 GBCOS 510 put gbtf 0 or get gbtf 0 16 511 end 512 if or not lt r0 GBREL not lt r1 GBREL 513 put gbtf 0 or get gbtf 0 32 514 end 515 gbtlo 0 c0 516 gbthi 1 r0 k 517 gbtlo 2 c1 518 gbthi 3 r1 k 519 end 520 521 note gbtw[j] = x when x is below it (j 0, 2: the least cosines) 522 fn gbtlo j ulong x f64 to void 523 if lt x get gbtw j 524 put gbtw j x 525 end 526 end 527 528 note gbtw[j] = x when x is above it (j 1, 3: the largest rels), and its tensor k in gbtw[4 + j / 2] 529 fn gbthi j ulong x f64 k ulong to void 530 if gt x get gbtw j 531 put gbtw j x 532 put gbtw add 4 div j 2 f64 k 533 end 534 end 535 536 note `GBT tensors cos0_e9 rel0_e9 k0 cos1_e9 rel1_e9 k1 bits` 537 fn gbtpr to void 538 oputc 71 539 oputc 66 540 oputc 84 541 oputc 32 542 oputu gbtn 543 for h ulong 2 544 gbtp1 h 545 end 546 oputc 32 547 oputu get gbtf 0 548 oputc 10 549 end 550 551 fn gbtp1 h ulong to void 552 o = mul h 2 553 gg2pe ggclp get gbtw o 554 gg2pe ggclp get gbtw add o 1 555 oputc 32 556 oputu ulong get gbtw add 4 h 557 end 558 559 note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p)) 560 global gbcq f64 3 561 fn gbcos p ulong to f64 562 for k ulong 3 563 put gbcq k 0.0f64 564 end 565 for q ulong ocn p 566 i0 = mul q OCH 567 for j ulong sub oce q p i0 568 gbcq1 add i0 j p 569 end 570 end 571 d = fsqrt mul get gbcq 1 get gbcq 2 572 if gt d 0.0f64 573 ret ggclp div get gbcq 0 d 574 end 575 ret 0.0f64 576 end 577 578 fn gbcq1 i ulong p ulong to void 579 x = omvg i 580 y = omvg add p i 581 put gbcq 0 add get gbcq 0 mul x y 582 put gbcq 1 add get gbcq 1 mul x x 583 put gbcq 2 add get gbcq 2 mul y y 584 end 585 586 note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9`, the per-tensor line `GBT` 587 note (gbtpr) and verdict (exit 49) 588 fn gbc2vd lg f64 l0 f64 fl ulong to void 589 cs = gbcos get og 8 590 oputc 71 591 oputc 66 592 oputc 50 593 oputc 32 594 oputu get og 56 595 oputc 32 596 oputu add mul 10 get og 56 3 597 oputc 32 598 oputl rmic mul gbtolb 1000.0f64 599 oputc 32 600 oputl rmic mul cs 1000.0f64 601 oputc 32 602 oputl rmic mul gg2rl 0 1000.0f64 603 oputc 32 604 oputl rmic mul gg2rl 2 1000.0f64 605 oputc 32 606 oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64 607 oputc 10 608 gbtpr 609 gbvd 49 eq fl 0 610 end 611 612 note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd) 613 fn gbcmp2 to void 614 put gkbf 0 1 615 ggcmp2 616 end 617 618 note gtrainb CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrainf with gkbf 1 (geval stays f32); the frozen 619 note core (gg.om ggfz) is the same on BF16: the frozen layers launch no BF16 GEMM at all. 620 note When OLLM_NVFP4=1 is set in the environment, routes eligible 128x128x64 GEMMs through NVFP4. 621 fn gbtrain to void 622 if ggnv4_env 623 ggsnv4 1 624 end 625 put gkbf 0 1 626 ggtrainf 627 ggsnv4 0 628 end 629 630 note gtrain4 CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrainb with NVFP4 (OMMA.SF.16864) enabled 631 fn gbtrain4 to void 632 ggsnv4 1 633 put gkbf 0 1 634 ggtrainf 635 ggsnv4 0 636 end 637 638 note gprofb: gprof with gkbf 1 639 fn gbprof to void 640 if ggnv4_env 641 ggsnv4 1 642 end 643 put gkbf 0 1 644 ggprof 645 ggsnv4 0 646 end 647 648 note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by 649 note element against a reference built from the bf16-rounded operands. One operand holds integers in 650 note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9 651 note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen 652 note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles 653 note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the 654 note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN 655 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, 656 note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL 657 note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi). 658 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 659 global gbcs uint 10 660 global gbfb ulong 1 661 global gbha f64 98304 662 global gbhb f64 147456 663 664 fn gbfrag to void 665 if ne oargc 3 666 exit 2 667 end 668 ockloop 2 669 ggopen 670 if lt mul ggr get og 0 `4194304` 671 ggdie 3 672 end 673 put gkbf 0 1 674 put gkbck 0 0 675 put gbfb 0 sub gga1 11 gkbase 676 bad = ulong 0 677 for p ulong 2 678 for i ulong 13 679 bad = add bad gbf1 i p 680 end 681 end 682 for i ulong 10 683 bad = add bad gbf128 i 684 end 685 for p ulong 2 686 for q uint 4 687 bad = add bad gbfl p and q 1 shr q 1 688 end 689 end 690 bad = add bad gbf85 691 r = nvstop 692 oputc 71 693 oputc 70 694 oputc 83 695 oputc 32 696 oputu bad 697 oputc 10 698 gbvd 50 eq bad 0 699 end 700 701 note case i of pass p into gbcs 702 fn gbcase i ulong p ulong to void 703 for j ulong 10 704 put gbcs j 0 705 end 706 put gbcs 8 uint p 707 put gbcs 6 128 708 put gbcs 7 192 709 put gbcs 5 128 710 if eq rem i 3 0 711 put gbcs 5 32 712 end 713 if eq rem i 3 1 714 put gbcs 5 96 715 put gbcs 9 1 716 end 717 if lt i 8 718 put gbcs 0 uint and i 1 719 put gbcs 1 uint and shr i 1 1 720 put gbcs 2 uint and shr i 2 1 721 ret 722 end 723 put gbcs 9 0 724 put gbcs 0 1 725 if eq i 8 726 put gbcs 3 1 727 put gbcs 5 96 728 end 729 if eq i 9 730 put gbcs 1 1 731 put gbcs 3 2 732 end 733 note cz 1 needs k = m 734 if eq i 10 735 put gbcs 4 1 736 put gbcs 5 128 737 end 738 if eq i 11 739 put gbcs 0 0 740 put gbcs 2 1 741 put gbcs 4 2 742 end 743 if eq i 12 744 put gbcs 1 1 745 put gbcs 4 3 746 put gbcs 5 32 747 put gbcs 7 128 748 put gbcs 9 1 749 end 750 end 751 752 note one case: write the data, launch, compare; the mismatch count 753 fn gbf1 i ulong p ulong to ulong 754 gbcase i p 755 gbnan 756 gbwop 0 757 gbwop 1 758 gbwc 759 put gkinv 0 1 760 if not gbflch 761 ggdie 98 762 end 763 if eq OMUTN 0 764 n = ulong get gbcs 7 765 gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0 766 end 767 b = gbfcmp 768 ep = ulong get gbcs 3 769 id = add 27 ulong get gbcs 0 770 id = add id mul 2 ulong get gbcs 1 771 id = add id mul 4 ulong get gbcs 2 772 if ne ep 0 773 id = add 34 ep 774 end 775 oputc 71 776 oputc 70 777 oputc 32 778 oputu p 779 oputc 32 780 oputu i 781 oputc 32 782 oputu id 783 oputc 32 784 oputu ulong get gbcs 5 785 oputc 32 786 oputu ulong get gbcs 4 787 oputc 32 788 oputu ep 789 oputc 32 790 oputu b 791 oputc 10 792 ret b 793 end 794 795 note Exact check of all 10 128x128 BF16 Tensor Core kernels (m=128, n=128, cz=0, nz=6 h2=3, NaN padding) 796 fn gbf128 i ulong to ulong 797 gbcase i 0 798 put gbcs 7 128 799 gbnan 800 gbwop 0 801 gbwop 1 802 gbwc 803 put gkinv 0 1 804 if not gbflch 805 ggdie 98 806 end 807 if eq OMUTN 0 808 n = ulong get gbcs 7 809 gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0 810 end 811 b = gbfcmp 812 oputc 71 813 oputc 70 814 oputc 32 815 oputu 2 816 oputc 32 817 oputu i 818 oputc 32 819 oputu ulong get gklt 0 820 oputc 32 821 oputu ulong get gbcs 5 822 oputc 32 823 oputu ulong get gbcs 4 824 oputc 32 825 oputu ulong get gbcs 3 826 oputc 32 827 oputu b 828 oputc 10 829 ret b 830 end 831 832 note NaN in every A and B word (2 MiB) 833 fn gbnan to void 834 b = get gbfb 0 835 for i ulong 524288 836 gbput add b mul i 4 `2143289344` 837 end 838 end 839 840 note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B); 841 note batch z at (z / 3)(3 zs + 64) + (z % 3) zs 842 fn gbzs md uint w ulong kd ulong to ulong 843 if eq md 1 844 ret mul w add kd 8 845 end 846 ret mul kd add w 16 847 end 848 849 fn gbzof z ulong zs ulong to ulong 850 ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs 851 end 852 853 fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong 854 if eq md 1 855 ret add mul x add kd 8 kk 856 end 857 ret add x mul kk add w 16 858 end 859 860 note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z) 861 fn gbfr x ulong kk ulong z ulong to f64 862 s = add mul x 7 mul kk 3 863 s = add s mul z 5 864 a = sub f64 rem s 5 2.0f64 865 s = add mul x 13 mul kk 11 866 s = add s mul z 3 867 b = f64 rem s 64 868 ret add a div b 512.0f64 869 end 870 871 fn gbint x ulong kk ulong z ulong to f64 872 s = add mul x 5 mul kk 7 873 s = add s mul z 3 874 ret sub f64 rem s 11 5.0f64 875 end 876 877 note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded 878 note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped. 879 fn gbwop ab uint to void 880 md = get gbcs ab 881 k = ulong get gbcs 5 882 w = ulong get gbcs add 6 ab 883 zs = gbzs md w k 884 base = add get gbfb 0 mul ulong ab `1048576` 885 fr = eq get gbcs 8 ab 886 for z ulong 6 887 for x ulong w 888 gbwrow ab z x base zs fr 889 end 890 end 891 end 892 893 fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void 894 zb = add base mul gbzof z zs 4 895 t = add 128 mul 64 ulong ab 896 hi = mul add mul z t x 128 897 k = ulong get gbcs 5 898 for kk ulong k 899 gbwel ab zb x kk gbwv x kk z fr hi 900 end 901 end 902 903 note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer 904 fn gbwv x ulong kk ulong z ulong fr bool to uint 905 v = gbint x kk z 906 if fr 907 v = gbfr x kk z 908 end 909 ret bitcast uint f32 v 910 end 911 912 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 913 fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void 914 md = get gbcs ab 915 w = ulong get gbcs add 6 ab 916 o = gbopf md x kk w ulong get gbcs 5 917 gbput add zb mul o 4 u 918 if eq ab 0 919 put gbha add hi kk gbrv u 920 else 921 put gbhb add hi kk gbrv u 922 end 923 end 924 925 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, 926 note else ((r + 3 c + z) mod 3) - 1, as f32 bits 927 fn gbold3 z ulong r ulong c ulong to f64 928 s = add mul r 3 mul c 5 929 s = add s z 930 ret sub f64 rem s 17 8.0f64 931 end 932 933 fn gbmv z ulong r ulong c ulong to uint 934 if eq rem add mul r 5 c 13 0 935 ret `2143289344` 936 end 937 ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64 938 end 939 940 note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns 941 note (and Y's / M's with a fused epilogue) are NaN 942 fn gbwc to void 943 m = ulong get gbcs 6 944 zs = mul m add ulong get gbcs 7 8 945 for z ulong 6 946 for r ulong m 947 gbwcr z r zs 948 end 949 end 950 end 951 952 fn gbwcr z ulong r ulong zs ulong to void 953 n = ulong get gbcs 7 954 ep = get gbcs 3 955 o = gbcro z r zs 956 for c ulong n 957 oc = add o mul c 4 958 gbput oc bitcast uint f32 gbold3 z r c 959 if eq ep 1 960 gbput add oc `1048576` bitcast uint f32 77.0f64 961 end 962 if eq ep 2 963 gbput add oc `1048576` gbmv z r c 964 end 965 end 966 for c ulong 8 967 op = add o mul add n c 4 968 gbput op `2143289344` 969 if ne ep 0 970 gbput add op `1048576` `2143289344` 971 end 972 end 973 end 974 975 note operand ab's gkopa for the case at device address p 976 fn gbfopa ab uint p ulong to void 977 md = get gbcs ab 978 k = get gbcs 5 979 w = get gbcs add 6 ab 980 zs = uint gbzs md ulong w ulong k 981 so = mul add k 8 4 982 sk = uint 4 983 if eq md 0 984 so = 4 985 sk = mul add w 16 4 986 end 987 gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4 988 end 989 990 fn gbflch to bool 991 d = add gkbase get gbfb 0 992 gbfopa 0 d 993 gbfopa 1 add d `1048576` 994 n = get gbcs 7 995 zc = mul get gbcs 6 add n 8 996 al = 1.0f64 997 if eq get gbcs 9 1 998 al = 0.5f64 999 end 1000 sm = mul add n 8 4 1001 s1 = mul add mul zc 3 64 4 1002 s2 = mul zc 4 1003 gkopc add d `2097152` sm 4 s1 s2 f32 al 1004 if ne get gbcs 3 0 1005 gkope get gbcs 3 add d `3145728` 1006 end 1007 if ne get gbcs 4 0 1008 gkopz get gbcs 4 1009 end 1010 m = get gbcs 6 1011 k = get gbcs 5 1012 bt = eq get gbcs 2 1 1013 ret gkmm m n k 6 3 bt 1014 end 1015 1016 note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN 1017 fn gbfcmp to ulong 1018 bad = ulong 0 1019 for z ulong 6 1020 for r ulong ulong get gbcs 6 1021 bad = add bad gbfrow z r 1022 end 1023 end 1024 ret bad 1025 end 1026 1027 note host byte offset of C row r of block z (row stride n + 8 f32) 1028 fn gbcro z ulong r ulong zs ulong to ulong 1029 n = ulong get gbcs 7 1030 b = add get gbfb 0 `2097152` 1031 b = add b mul gbzof z zs 4 1032 ret add b mul mul r add n 8 4 1033 end 1034 1035 fn gbfrow z ulong r ulong to ulong 1036 n = ulong get gbcs 7 1037 zs = mul ulong get gbcs 6 add n 8 1038 o = gbcro z r zs 1039 bad = ulong 0 1040 for c ulong n 1041 if not gbfel z r c add o mul c 4 1042 bad = add bad 1 1043 end 1044 end 1045 for c ulong 8 1046 op = add o mul add n c 4 1047 if ne gbw op `2143289344` 1048 bad = add bad 1 1049 end 1050 if ne get gbcs 3 0 1051 if ne gbw add op `1048576` `2143289344` 1052 bad = add bad 1 1053 end 1054 end 1055 end 1056 ret bad 1057 end 1058 1059 note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2 1060 note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0 1061 fn gbfel z ulong r ulong c ulong oc ulong to bool 1062 cb = gbw oc 1063 old = gbold3 z r c 1064 if and eq get gbcs 4 3 gt div c 64 div r 64 1065 ret eq cb bitcast uint f32 old 1066 end 1067 ep = get gbcs 3 1068 if eq ep 2 1069 if not gt f64 bitcast f32 gbmv z r c 0.0f64 1070 ret eq cb 0 1071 end 1072 end 1073 s = gbfref z r c 1074 if eq get gbcs 2 1 1075 s = add s old 1076 end 1077 if ne f64 bitcast f32 cb s 1078 ret false 1079 end 1080 if eq ep 1 1081 w = uint 0 1082 if gt s 0.0f64 1083 w = cb 1084 end 1085 ret eq gbw add oc `1048576` w 1086 end 1087 ret true 1088 end 1089 1090 note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands) 1091 fn gbfref z ulong r ulong c ulong to f64 1092 k = ulong get gbcs 5 1093 lo = ulong 0 1094 hi = mul add div r 64 1 64 1095 cz = get gbcs 4 1096 if and eq cz 1 lt hi k 1097 k = hi 1098 end 1099 if eq cz 2 1100 lo = mul div r 64 64 1101 end 1102 ia = mul add mul z 128 r 128 1103 s = gbfdot ia mul add mul z 192 c 128 lo sub k lo 1104 if eq get gbcs 9 1 1105 s = mul s 0.5f64 1106 end 1107 ret s 1108 end 1109 1110 fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64 1111 s = 0.0f64 1112 for j ulong kn 1113 kk = add lo j 1114 a = get gbha add ia kk 1115 s = add s mul a get gbhb add ib kk 1116 end 1117 ret s 1118 end 1119 1120 note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0, 1121 note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile), 1122 note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples 1123 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, 1124 note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`. 1125 fn gbfl p ulong ma uint mb uint to ulong 1126 for j ulong 10 1127 put gbcs j 0 1128 end 1129 put gbcs 0 ma 1130 put gbcs 1 mb 1131 put gbcs 5 16384 1132 put gbcs 6 64 1133 put gbcs 7 64 1134 put gbcs 8 uint p 1135 b = get gbfb 0 1136 for i ulong `3162112` 1137 gbput add b mul i 4 `2143289344` 1138 end 1139 gblw 0 1140 gblw 1 1141 put gkinv 0 1 1142 if not gbllch 1143 ggdie 98 1144 end 1145 bad = ulong 0 1146 for r ulong 64 1147 bad = add bad gblrow r 1148 end 1149 oputc 71 1150 oputc 70 1151 oputc 76 1152 oputc 32 1153 oputu p 1154 oputc 32 1155 oputu ulong ma 1156 oputc 32 1157 oputu ulong mb 1158 oputc 32 1159 oputu bad 1160 oputc 10 1161 ret bad 1162 end 1163 1164 note operand ab of the long case: nonzero words only at kk = 0 mod 29 1165 fn gblw ab uint to void 1166 md = get gbcs ab 1167 base = add get gbfb 0 mul ulong ab `6291456` 1168 fr = eq get gbcs 8 ab 1169 for x ulong 64 1170 gblwr md base x fr 1171 end 1172 end 1173 1174 fn gblwr md uint base ulong x ulong fr bool to void 1175 w = ulong 64 1176 kd = ulong 16384 1177 for kk ulong kd 1178 u = uint 0 1179 if eq rem kk 29 0 1180 u = gbwv x kk 0 fr 1181 end 1182 o = gbopf md x kk w kd 1183 gbput add base mul o 4 u 1184 end 1185 end 1186 1187 note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats 1188 fn gbllch to bool 1189 d = add gkbase get gbfb 0 1190 gblopa 0 d 1191 gblopa 1 add d `6291456` 1192 gkopc add d `12582912` 288 4 0 0 f32 1.0f64 1193 gkopz 0 1194 ret gkmm 64 64 16384 1 1 false 1195 end 1196 1197 fn gblopa ab uint p ulong to void 1198 so = uint 65568 1199 sk = uint 4 1200 if eq get gbcs ab 0 1201 so = 4 1202 sk = 320 1203 end 1204 gkopa ab p so sk 0 0 1205 end 1206 1207 note row r of the long case: 64 exact values, then 8 padding words that must still be NaN 1208 fn gblrow r ulong to ulong 1209 o = add add get gbfb 0 `12582912` mul r 288 1210 bad = ulong 0 1211 for c ulong 64 1212 cb = gbw add o mul c 4 1213 if ne f64 bitcast f32 cb gblref r c 1214 bad = add bad 1 1215 end 1216 end 1217 for c ulong 8 1218 if ne gbw add o mul add 64 c 4 `2143289344` 1219 bad = add bad 1 1220 end 1221 end 1222 ret bad 1223 end 1224 1225 note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk) 1226 fn gblref r ulong c ulong to f64 1227 fa = eq get gbcs 8 0 1228 fb = eq get gbcs 8 1 1229 s = 0.0f64 1230 for j ulong 565 1231 kk = mul j 29 1232 a = gbrv gbwv r kk 0 fa 1233 s = add s mul a gbrv gbwv c kk 0 fb 1234 end 1235 ret s 1236 end 1237 1238 note Slot 85 (gkgemmh128_3way) exact 0-mismatch verification: M=128 N=128 K=128 across 3 passes. 1239 note A0..A2 spaced by s6=65536 (32 KiB NaN gaps), B0..B2 contiguous dd2=32768, NaN padding outside. 1240 fn gbf85_a p uint r uint k uint to long 1241 v = rem add add mul ulong r 3 mul ulong k 5 mul ulong p 7 7 1242 ret sub long v 3 1243 end 1244 1245 fn gbf85_b p uint c uint k uint to long 1246 v = rem add add mul ulong c 5 mul ulong k 7 mul ulong p 11 7 1247 ret sub long v 3 1248 end 1249 1250 fn gbf85_u16 v long to uint 1251 u = bitcast uint f32 f64 v 1252 ret shr u 16 1253 end 1254 1255 fn gbf85_wr1 p uint r uint cc uint to void 1256 ba = add get gbfb 0 mul ulong p 65536 1257 bb = add add get gbfb 0 524288 mul ulong p 32768 1258 off = add mul ulong r 256 mul ulong cc 4 1259 k = mul cc 2 1260 a0 = gbf85_u16 gbf85_a p r k 1261 a1 = gbf85_u16 gbf85_a p r add k 1 1262 gbput add ba off or a0 shl a1 16 1263 a0 = gbf85_u16 gbf85_b p r k 1264 a1 = gbf85_u16 gbf85_b p r add k 1 1265 gbput add bb off or a0 shl a1 16 1266 end 1267 1268 fn gbf85_wrp p uint to void 1269 for r uint 128 1270 for cc uint 64 1271 gbf85_wr1 p r cc 1272 end 1273 end 1274 end 1275 1276 fn gbf85_chk1 r uint c uint to long 1277 s = long 0 1278 for p uint 3 1279 for k uint 128 1280 s = add s mul gbf85_a p r k gbf85_b p c k 1281 end 1282 end 1283 ret s 1284 end 1285 1286 fn gbf85_chkel r uint c uint g uint to ulong 1287 s = gbf85_chk1 r c 1288 u = bitcast uint f32 f64 s 1289 e = shr gbbf u 16 1290 if ne g e 1291 ret 1 1292 end 1293 ret 0 1294 end 1295 1296 fn gbf85_chkpair r uint cc uint to ulong 1297 c = mul cc 2 1298 o = add add get gbfb 0 `1048576` add mul ulong r 256 mul ulong cc 4 1299 w = gbw o 1300 bad = gbf85_chkel r c and w 65535 1301 ret add bad gbf85_chkel r add c 1 shr w 16 1302 end 1303 1304 fn gbf85 to ulong 1305 b = get gbfb 0 1306 for i ulong 524288 1307 gbput add b mul i 4 `2143322048` 1308 end 1309 for p uint 3 1310 gbf85_wrp p 1311 end 1312 put gkinv 0 1 1313 d = add gkbase b 1314 gkoph 0 d 256 2 65280 0 1315 gkoph 1 add d 524288 256 2 32512 0 1316 gkopc add d `1048576` 256 2 0 0 f32 1.0f64 1317 ggmmh 128 128 128 1 1 8 1318 bad = ulong 0 1319 for r uint 128 1320 for cc uint 64 1321 bad = add bad gbf85_chkpair r cc 1322 end 1323 end 1324 for i ulong 16 1325 if ne gbw add add b `1081344` mul i 4 `2143322048` 1326 bad = add bad 1 1327 end 1328 end 1329 oputc 71 1330 oputc 70 1331 oputc 56 1332 oputc 53 1333 oputc 32 1334 oputu 128 1335 oputc 32 1336 oputu 128 1337 oputc 32 1338 oputu 128 1339 oputc 32 1340 oputu bad 1341 oputc 10 1342 ret bad 1343 end 1344 1345 note gbcmp2h CK DATA S: full ggbf_ok == true end-to-end packed-BF16 step parity against CPU f64 K0 1346 note (exercises slots 67..74, 75, 76, 77..80, 82, 83, 85, 86, 87). Prints `GBH 1` + GBCMP2 verdict. 1347 fn gbcmp2h to void 1348 if ne oargc 5 1349 exit 2 1350 end 1351 ockloop 2 1352 odopen 3 1353 obatch oargu 4 1354 p = get og 8 1355 put gkbf 0 1 1356 put gkbck 0 0 1357 ggsnofp 1 1358 ggopen 1359 if not ggbf_ok 1360 ggsnofp 0 1361 r = nvstop 1362 gbvd 49 false 1363 end 1364 ggupw 1365 ggf2b ggp5 gga1 0 gga1 38 1366 ggquant_w 1367 t0 = monons 1368 lg = ggpass false 1369 t1 = monons 1370 ggdown 1371 ggstash p 0 1372 ggsnofp 0 1373 put og 10 0 1374 put ogkm 0 0 1375 l0 = ograd 1376 ggstash p p 1377 ggk2z 1378 gg2cmp p 1379 oputc 71 1380 oputc 66 1381 oputc 72 1382 oputc 32 1383 oputc 49 1384 oputc 10 1385 oflush 1386 ggc2pr lg l0 sub t1 t0 1387 end 1388 1389 note NVFP4 standalone K-looped 128x128 GEMM parity verifier (gnv4gemm M N K) and full-step verifier (gnv4cmp) 1390 global gn4o ulong 12 1391 global gn4m f64 10 1392 global gn4s f64 4 1393 1394 fn gn4align x ulong to ulong 1395 ret mul div add x 255 256 256 1396 end 1397 1398 fn gn4p idx ulong to ulong 1399 ret add gkbase get gn4o idx 1400 end 1401 1402 fn gn4plan m ulong n ulong k ulong to ulong 1403 c = ulong 0 1404 put gn4o 0 c 1405 c = add c gn4align mul mul m k 2 1406 put gn4o 1 c 1407 c = add c gn4align mul mul n k 2 1408 put gn4o 2 c 1409 c = add c gn4align div mul m k 2 1410 put gn4o 3 c 1411 c = add c gn4align div mul m k 16 1412 put gn4o 4 c 1413 c = add c gn4align div mul n k 2 1414 put gn4o 5 c 1415 c = add c gn4align div mul n k 16 1416 put gn4o 6 c 1417 c = add c gn4align mul mul m n 4 1418 put gn4o 7 c 1419 c = add c gn4align mul mul m n 2 1420 put gn4o 8 m 1421 put gn4o 9 n 1422 put gn4o 10 k 1423 ret c 1424 end 1425 1426 note Deterministic value in {-1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5} (exact in BF16 and E2M1 with scale 0.25; coprime strides so every row/col differs) 1427 fn gn4v i ulong j ulong salt ulong to f64 1428 s = rem add add mul i 5 mul j 13 salt 7 1429 ret mul sub f64 s 3.0f64 0.5f64 1430 end 1431 1432 fn gn4bf16 v f64 to uint 1433 u = bitcast uint f32 v 1434 ret shr u 16 1435 end 1436 1437 fn gn4wr_rm1 ro ulong r ulong c2 ulong salt ulong to void 1438 for cc ulong c2 1439 c = mul cc 2 1440 lo = gn4bf16 gn4v r c salt 1441 hi = gn4bf16 gn4v r add c 1 salt 1442 gbput add ro mul cc 4 or lo shl hi 16 1443 end 1444 end 1445 1446 fn gn4wr_rm base ulong rows ulong cols ulong salt ulong to void 1447 c2 = div cols 2 1448 for r ulong rows 1449 gn4wr_rm1 add base mul mul r cols 2 r c2 salt 1450 end 1451 end 1452 1453 fn gn4wr_tr1 ro ulong r ulong c2 ulong salt ulong to void 1454 for cc ulong c2 1455 c = mul cc 2 1456 lo = gn4bf16 gn4v c r salt 1457 hi = gn4bf16 gn4v add c 1 r salt 1458 gbput add ro mul cc 4 or lo shl hi 16 1459 end 1460 end 1461 1462 fn gn4wr_tr base ulong rows ulong cols ulong salt ulong to void 1463 c2 = div cols 2 1464 for r ulong rows 1465 gn4wr_tr1 add base mul mul r cols 2 r c2 salt 1466 end 1467 end 1468 1469 fn gn4ref r ulong c ulong k ulong sc f64 to f64 1470 s = 0.0f64 1471 for kk ulong k 1472 s = add s mul gn4v r kk 1 gn4v c kk 5 1473 end 1474 ret mul s sc 1475 end 1476 1477 fn gn4rdf32 base ulong r ulong c ulong n ulong to f64 1478 o = add base mul add mul r n c 4 1479 ret f64 bitcast f32 gbw o 1480 end 1481 1482 fn gn4rdbf base ulong r ulong c ulong n ulong to f64 1483 idx = add mul r n c 1484 w = gbw add base mul div idx 2 4 1485 h = and w 65535 1486 if eq rem idx 2 1 1487 h = shr w 16 1488 end 1489 ret f64 bitcast f32 shl h 16 1490 end 1491 1492 fn gn4nf_f32r ro ulong n ulong to ulong 1493 nf = ulong 0 1494 for c ulong n 1495 v = f64 bitcast f32 gbw add ro mul c 4 1496 if not ggfin v 1497 nf = add nf 1 1498 end 1499 end 1500 ret nf 1501 end 1502 1503 fn gn4chk_nf_f32 base ulong m ulong n ulong to ulong 1504 nf = ulong 0 1505 for r ulong m 1506 nf = add nf gn4nf_f32r add base mul mul r n 4 n 1507 end 1508 ret nf 1509 end 1510 1511 fn gn4nf_bfr ro ulong n2 ulong to ulong 1512 nf = ulong 0 1513 for cc ulong n2 1514 w = gbw add ro mul cc 4 1515 v0 = f64 bitcast f32 shl and w 65535 16 1516 v1 = f64 bitcast f32 shl shr w 16 16 1517 if not ggfin v0 1518 nf = add nf 1 1519 end 1520 if not ggfin v1 1521 nf = add nf 1 1522 end 1523 end 1524 ret nf 1525 end 1526 1527 fn gn4chk_nf_bf base ulong m ulong n ulong to ulong 1528 nf = ulong 0 1529 n2 = div n 2 1530 for r ulong m 1531 nf = add nf gn4nf_bfr add base mul mul r n 2 n2 1532 end 1533 ret nf 1534 end 1535 1536 fn gn4acc g f64 rf f64 to void 1537 put gn4s 0 add get gn4s 0 mul g rf 1538 put gn4s 1 add get gn4s 1 mul g g 1539 put gn4s 2 add get gn4s 2 mul rf rf 1540 df = sub g rf 1541 put gn4s 3 add get gn4s 3 mul df df 1542 end 1543 1544 fn gn4wr_rm1s ro ulong r ulong c2 ulong sc f64 to void 1545 for cc ulong c2 1546 c = mul cc 2 1547 lo = gn4bf16 mul gn4v r c 1 sc 1548 hi = gn4bf16 mul gn4v r add c 1 1 sc 1549 gbput add ro mul cc 4 or lo shl hi 16 1550 end 1551 end 1552 1553 fn gn4wr_rms base ulong rows ulong cols ulong sc f64 to void 1554 c2 = div cols 2 1555 for r ulong rows 1556 gn4wr_rm1s add base mul mul r cols 2 r c2 sc 1557 end 1558 end 1559 1560 note Non-power-of-2 arbitrary float in [-1.73, +1.73] (exercises non-zero UE4M3 mantissas + E2M1 rounding) 1561 fn gn4va i ulong j ulong salt ulong to f64 1562 s = rem add add mul i 11 mul j 17 salt 23 1563 ret mul sub f64 s 11.0f64 0.157f64 1564 end 1565 1566 fn gn4wr_rma1 ro ulong r ulong c2 ulong salt ulong sc f64 to void 1567 for cc ulong c2 1568 c = mul cc 2 1569 lo = gn4bf16 mul gn4va r c salt sc 1570 hi = gn4bf16 mul gn4va r add c 1 salt sc 1571 gbput add ro mul cc 4 or lo shl hi 16 1572 end 1573 end 1574 1575 fn gn4wr_rma base ulong rows ulong cols ulong salt ulong sc f64 to void 1576 c2 = div cols 2 1577 for r ulong rows 1578 gn4wr_rma1 add base mul mul r cols 2 r c2 salt sc 1579 end 1580 end 1581 1582 fn gn4refa r ulong c ulong k ulong sc f64 to f64 1583 s = 0.0f64 1584 for kk ulong k 1585 s = add s mul gn4va r kk 3 mul gn4va c kk 9 sc 1586 end 1587 ret s 1588 end 1589 1590 fn gn4stat1 s ulong idx ulong bf bool to void 1591 m = get gn4o 8 1592 n = get gn4o 9 1593 k = get gn4o 10 1594 r = rem add mul s 37 13 m 1595 c = rem add mul s 53 29 n 1596 if eq s 0 1597 r = 0 1598 c = 0 1599 end 1600 if eq s 63 1601 r = sub m 1 1602 c = sub n 1 1603 end 1604 sc = 1.0f64 1605 if eq idx 2 1606 sc = 2.0f64 1607 end 1608 if eq idx 3 1609 sc = 0.`000244140625f64` 1610 end 1611 g = 0.0f64 1612 if bf 1613 g = gn4rdbf get gn4o 7 r c n 1614 else 1615 g = gn4rdf32 get gn4o 6 r c n 1616 end 1617 rf = 0.0f64 1618 if eq idx 4 1619 rf = gn4refa r c k 0.05f64 1620 else 1621 rf = gn4ref r c k sc 1622 end 1623 gn4acc g rf 1624 end 1625 1626 fn gn4stat idx ulong bf bool to void 1627 for j ulong 4 1628 put gn4s j 0.0f64 1629 end 1630 for s ulong 64 1631 gn4stat1 s idx bf 1632 end 1633 cs = gbcsn get gn4s 0 get gn4s 1 get gn4s 2 1634 rl = gbrat fsqrt get gn4s 3 fsqrt get gn4s 2 1635 put gn4m mul idx 2 cs 1636 put gn4m add mul idx 2 1 rl 1637 end 1638 1639 fn gn4gemm_pr m ulong n ulong k ulong nf ulong tf long to void 1640 ok = eq nf 0 1641 ok = and ok ge get gn4m 0 0.98f64 1642 ok = and ok ge get gn4m 2 0.98f64 1643 ok = and ok ge get gn4m 4 0.98f64 1644 ok = and ok ge get gn4m 6 0.98f64 1645 ok = and ok ge get gn4m 8 0.95f64 1646 oputc 71 1647 oputc 78 1648 oputc 86 1649 oputc 52 1650 oputc 71 1651 oputc 69 1652 oputc 77 1653 oputc 77 1654 oputc 32 1655 oputu m 1656 oputc 32 1657 oputu n 1658 oputc 32 1659 oputu k 1660 for j ulong 10 1661 oputc 32 1662 oputl rmic ggclp get gn4m j 1663 end 1664 oputc 32 1665 oputu nf 1666 oputc 32 1667 oputl tf 1668 oputc 32 1669 if ok 1670 oputc 80 1671 oputc 65 1672 oputc 83 1673 oputc 83 1674 oputc 10 1675 oflush 1676 ret 1677 end 1678 oputc 70 1679 oputc 65 1680 oputc 73 1681 oputc 76 1682 oputc 10 1683 oflush 1684 exit 52 1685 end 1686 1687 fn gn4gemm_run m ulong n ulong k ulong fl uint al f64 to void 1688 ggmm4_ab gn4p 2 gn4p 3 gn4p 4 gn4p 5 m n 1689 if ne and fl 2 0 1690 gkopc gn4p 7 uint mul n 2 2 0 0 f32 al 1691 else 1692 gkopc gn4p 6 uint mul n 4 4 0 0 f32 al 1693 end 1694 ggmm4_lch m n k fl 1695 end 1696 1697 fn gn4gemm_bench m ulong n ulong k ulong nf ulong to void 1698 it = ulong 1 1699 if ge mul mul m n k `67108864` 1700 it = 5 1701 end 1702 t0 = monons 1703 for q ulong it 1704 gn4gemm_run m n k 2 1.0f64 1705 end 1706 ns = sub monons t0 1707 tf = long 0 1708 if gt ns 0 1709 flops = mul mul 2.0f64 f64 mul m n mul f64 k f64 it 1710 tf = long div flops mul f64 ns 10.0f64 1711 end 1712 r = nvstop 1713 gn4gemm_pr m n k nf tf 1714 end 1715 1716 fn gnv4gemm to void 1717 if ne oargc 5 1718 exit 2 1719 end 1720 m = oargu 2 1721 n = oargu 3 1722 k = oargu 4 1723 if or eq m 0 eq n 0 1724 exit 2 1725 end 1726 if eq k 0 1727 exit 2 1728 end 1729 if or gt m 8192 gt n 8192 1730 exit 2 1731 end 1732 if gt k 16384 1733 exit 2 1734 end 1735 if gt mul m n `67108864` 1736 exit 2 1737 end 1738 if or ne rem m 128 0 ne rem n 128 0 1739 exit 2 1740 end 1741 if ne rem k 64 0 1742 exit 2 1743 end 1744 asz = gn4plan m n k 1745 grset 256 32 64 2 128 2 1746 if not gropen asz 1747 ggsetupfail 1748 end 1749 gn4wr_rm get gn4o 0 m k 1 1750 gn4wr_tr get gn4o 1 k n 5 1751 put gkinv 0 1 1752 ggqnv4_p 1.0f64 1753 ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false 1754 ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k true 1755 gn4gemm_run m n k 0 1.0f64 1756 nf = gn4chk_nf_f32 get gn4o 6 m n 1757 gn4stat 0 false 1758 gn4wr_rm get gn4o 1 n k 5 1759 put gkinv 0 1 1760 ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k false 1761 gn4gemm_run m n k 2 1.0f64 1762 nf = add nf gn4chk_nf_bf get gn4o 7 m n 1763 gn4stat 1 true 1764 gn4gemm_run m n k 3 1.0f64 1765 nf = add nf gn4chk_nf_bf get gn4o 7 m n 1766 gn4stat 2 true 1767 gn4wr_rms get gn4o 0 m k 0.`000244140625f64` 1768 put gkinv 0 1 1769 ggqnv4_p 4096.0f64 1770 ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false 1771 ggqnv4_p 1.0f64 1772 gn4gemm_run m n k 0 0.`000244140625f64` 1773 nf = add nf gn4chk_nf_f32 get gn4o 6 m n 1774 gn4stat 3 false 1775 gn4wr_rma get gn4o 0 m k 3 1.0f64 1776 gn4wr_rma get gn4o 1 n k 9 0.05f64 1777 put gkinv 0 1 1778 ggqnv4_p 1.0f64 1779 ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false 1780 ggqnv4_p 16.0f64 1781 ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k false 1782 ggqnv4_p 1.0f64 1783 gn4gemm_run m n k 0 0.`0625f64` 1784 nf = add nf gn4chk_nf_f32 get gn4o 6 m n 1785 gn4stat 4 false 1786 gn4gemm_bench m n k nf 1787 end 1788 1789 fn gn4cmp_nf p ulong to ulong 1790 nf = ulong 0 1791 for q ulong ocn p 1792 i0 = mul q OCH 1793 for j ulong sub oce q p i0 1794 if not ggfin omvg add i0 j 1795 nf = add nf 1 1796 end 1797 end 1798 end 1799 ret nf 1800 end 1801 1802 fn gnv4cmp_pr p ulong nf ulong ns long to void 1803 cs = get gn4m 0 1804 dl = get gn4m 1 1805 l0 = get gn4m 2 1806 lg = get gn4m 3 1807 ok = and eq nf 0 and ggfin lg and ge cs 0.90f64 lt dl 0.15f64 1808 oputc 71 1809 oputc 78 1810 oputc 86 1811 oputc 52 1812 oputc 67 1813 oputc 77 1814 oputc 80 1815 oputc 32 1816 oputu p 1817 oputc 32 1818 oputl rmic ggclp cs 1819 oputc 32 1820 oputl rmic ggclp dl 1821 oputc 32 1822 oputu nf 1823 oputc 32 1824 oputl rmic ggclp l0 1825 oputc 32 1826 oputl rmic ggclp lg 1827 oputc 32 1828 oputl ns 1829 oputc 32 1830 if ok 1831 oputc 80 1832 oputc 65 1833 oputc 83 1834 oputc 83 1835 oputc 10 1836 oflush 1837 ret 1838 end 1839 oputc 70 1840 oputc 65 1841 oputc 73 1842 oputc 76 1843 oputc 10 1844 oflush 1845 exit 53 1846 end 1847 1848 note gnv4cmp CK DATA S: full-step forward+backward NVFP4 vs CPU f64 K0 parity verifier 1849 fn gnv4cmp to void 1850 if ne oargc 5 1851 exit 2 1852 end 1853 ockloop 2 1854 odopen 3 1855 obatch oargu 4 1856 p = get og 8 1857 put gkbf 0 1 1858 put gkbck 0 0 1859 ggsnv4 1 1860 ggsnofp 1 1861 ggopen 1862 if not ggnv4_ok 1863 ggsnv4 0 1864 ggsnofp 0 1865 r = nvstop 1866 put gn4m 0 0.0f64 1867 put gn4m 1 1.0f64 1868 put gn4m 2 0.0f64 1869 put gn4m 3 0.0f64 1870 gnv4cmp_pr p 1 0 1871 end 1872 ggupw 1873 ggf2b ggp5 gga1 0 gga1 38 1874 t0 = monons 1875 lg = ggpass false 1876 t1 = monons 1877 ggdown 1878 ggstash p 0 1879 ggsnv4 0 1880 ggsnofp 0 1881 put og 10 0 1882 put ogkm 0 0 1883 l0 = ograd 1884 ggstash p p 1885 nf = gn4cmp_nf p 1886 cs = gbcos p 1887 dl = gbrat gbabs sub lg l0 gbabs l0 1888 r = nvstop 1889 put gn4m 0 cs 1890 put gn4m 1 dl 1891 put gn4m 2 l0 1892 put gn4m 3 lg 1893 gnv4cmp_pr p nf sub t1 t0 1894 end