aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / native / omegallm / gpu / gb.om
| 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 | ockload 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 dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV; |
| 364 | note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic |
| 365 | fn gbtolb to f64 |
| 366 | nc = f64 add mul 10 get og 5 3 |
| 367 | ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64 |
| 368 | end |
| 369 | |
| 370 | note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss |
| 371 | fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong |
| 372 | tb = gbtolb |
| 373 | fl = ulong 0 |
| 374 | if ne nf 0 |
| 375 | fl = or fl 1 |
| 376 | end |
| 377 | if not lt ggclp rho tb |
| 378 | fl = or fl 2 |
| 379 | end |
| 380 | if not lt gg2rl 2 tb |
| 381 | fl = or fl 4 |
| 382 | end |
| 383 | if not lt gbabs sub lg l0 mul tb gbabs l0 |
| 384 | fl = or fl 8 |
| 385 | end |
| 386 | ret fl |
| 387 | end |
| 388 | |
| 389 | note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p)) |
| 390 | global gbcq f64 3 |
| 391 | fn gbcos p ulong to f64 |
| 392 | for k ulong 3 |
| 393 | put gbcq k 0.0f64 |
| 394 | end |
| 395 | for q ulong ocn p |
| 396 | i0 = mul q OCH |
| 397 | for j ulong sub oce q p i0 |
| 398 | gbcq1 add i0 j p |
| 399 | end |
| 400 | end |
| 401 | d = fsqrt mul get gbcq 1 get gbcq 2 |
| 402 | if gt d 0.0f64 |
| 403 | ret ggclp div get gbcq 0 d |
| 404 | end |
| 405 | ret 0.0f64 |
| 406 | end |
| 407 | |
| 408 | fn gbcq1 i ulong p ulong to void |
| 409 | x = omvg i |
| 410 | y = omvg add p i |
| 411 | put gbcq 0 add get gbcq 0 mul x y |
| 412 | put gbcq 1 add get gbcq 1 mul x x |
| 413 | put gbcq 2 add get gbcq 2 mul y y |
| 414 | end |
| 415 | |
| 416 | note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49) |
| 417 | fn gbc2vd lg f64 l0 f64 fl ulong to void |
| 418 | cs = gbcos get og 8 |
| 419 | oputc 71 |
| 420 | oputc 66 |
| 421 | oputc 50 |
| 422 | oputc 32 |
| 423 | oputu get og 5 |
| 424 | oputc 32 |
| 425 | oputu add mul 10 get og 5 3 |
| 426 | oputc 32 |
| 427 | oputl rmic mul gbtolb 1000.0f64 |
| 428 | oputc 32 |
| 429 | oputl rmic mul cs 1000.0f64 |
| 430 | oputc 32 |
| 431 | oputl rmic mul gg2rl 0 1000.0f64 |
| 432 | oputc 32 |
| 433 | oputl rmic mul gg2rl 2 1000.0f64 |
| 434 | oputc 32 |
| 435 | oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64 |
| 436 | oputc 10 |
| 437 | gbvd 49 eq fl 0 |
| 438 | end |
| 439 | |
| 440 | note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd) |
| 441 | fn gbcmp2 to void |
| 442 | put gkbf 0 1 |
| 443 | ggcmp2 |
| 444 | end |
| 445 | |
| 446 | note gtrainb: gtrainf with gkbf 1 (geval stays f32) |
| 447 | fn gbtrain to void |
| 448 | put gkbf 0 1 |
| 449 | ggtrainf |
| 450 | end |
| 451 | |
| 452 | note gprofb: gprof with gkbf 1 |
| 453 | fn gbprof to void |
| 454 | put gkbf 0 1 |
| 455 | ggprof |
| 456 | end |
| 457 | |
| 458 | note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by |
| 459 | note element against a reference built from the bf16-rounded operands. One operand holds integers in |
| 460 | note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9 |
| 461 | note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen |
| 462 | note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles |
| 463 | note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the |
| 464 | note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN |
| 465 | 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, |
| 466 | note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL |
| 467 | note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi). |
| 468 | 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 |
| 469 | global gbcs uint 10 |
| 470 | global gbfb ulong 1 |
| 471 | global gbha f64 98304 |
| 472 | global gbhb f64 147456 |
| 473 | |
| 474 | fn gbfrag to void |
| 475 | if ne oargc 3 |
| 476 | exit 2 |
| 477 | end |
| 478 | ockload 2 |
| 479 | ggopen |
| 480 | if lt mul ggr get og 0 4194304 |
| 481 | ggdie 3 |
| 482 | end |
| 483 | put gkbf 0 1 |
| 484 | put gkbck 0 0 |
| 485 | put gbfb 0 sub gga1 11 gkbase |
| 486 | bad = ulong 0 |
| 487 | for p ulong 2 |
| 488 | for i ulong 13 |
| 489 | bad = add bad gbf1 i p |
| 490 | end |
| 491 | end |
| 492 | for p ulong 2 |
| 493 | for q uint 4 |
| 494 | bad = add bad gbfl p and q 1 shr q 1 |
| 495 | end |
| 496 | end |
| 497 | r = nvstop |
| 498 | oputc 71 |
| 499 | oputc 70 |
| 500 | oputc 83 |
| 501 | oputc 32 |
| 502 | oputu bad |
| 503 | oputc 10 |
| 504 | gbvd 50 eq bad 0 |
| 505 | end |
| 506 | |
| 507 | note case i of pass p into gbcs |
| 508 | fn gbcase i ulong p ulong to void |
| 509 | for j ulong 10 |
| 510 | put gbcs j 0 |
| 511 | end |
| 512 | put gbcs 8 uint p |
| 513 | put gbcs 6 128 |
| 514 | put gbcs 7 192 |
| 515 | put gbcs 5 128 |
| 516 | if eq rem i 3 0 |
| 517 | put gbcs 5 32 |
| 518 | end |
| 519 | if eq rem i 3 1 |
| 520 | put gbcs 5 96 |
| 521 | put gbcs 9 1 |
| 522 | end |
| 523 | if lt i 8 |
| 524 | put gbcs 0 uint and i 1 |
| 525 | put gbcs 1 uint and shr i 1 1 |
| 526 | put gbcs 2 uint and shr i 2 1 |
| 527 | ret |
| 528 | end |
| 529 | put gbcs 9 0 |
| 530 | put gbcs 0 1 |
| 531 | if eq i 8 |
| 532 | put gbcs 3 1 |
| 533 | put gbcs 5 96 |
| 534 | end |
| 535 | if eq i 9 |
| 536 | put gbcs 1 1 |
| 537 | put gbcs 3 2 |
| 538 | end |
| 539 | note cz 1 needs k = m |
| 540 | if eq i 10 |
| 541 | put gbcs 4 1 |
| 542 | put gbcs 5 128 |
| 543 | end |
| 544 | if eq i 11 |
| 545 | put gbcs 0 0 |
| 546 | put gbcs 2 1 |
| 547 | put gbcs 4 2 |
| 548 | end |
| 549 | if eq i 12 |
| 550 | put gbcs 1 1 |
| 551 | put gbcs 4 3 |
| 552 | put gbcs 5 32 |
| 553 | put gbcs 7 128 |
| 554 | put gbcs 9 1 |
| 555 | end |
| 556 | end |
| 557 | |
| 558 | note one case: write the data, launch, compare; the mismatch count |
| 559 | fn gbf1 i ulong p ulong to ulong |
| 560 | gbcase i p |
| 561 | gbnan |
| 562 | gbwop 0 |
| 563 | gbwop 1 |
| 564 | gbwc |
| 565 | put gkinv 0 1 |
| 566 | if not gbflch |
| 567 | ggdie 98 |
| 568 | end |
| 569 | if eq OMUTN 0 |
| 570 | n = ulong get gbcs 7 |
| 571 | gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0 |
| 572 | end |
| 573 | b = gbfcmp |
| 574 | ep = ulong get gbcs 3 |
| 575 | id = add 27 ulong get gbcs 0 |
| 576 | id = add id mul 2 ulong get gbcs 1 |
| 577 | id = add id mul 4 ulong get gbcs 2 |
| 578 | if ne ep 0 |
| 579 | id = add 34 ep |
| 580 | end |
| 581 | oputc 71 |
| 582 | oputc 70 |
| 583 | oputc 32 |
| 584 | oputu p |
| 585 | oputc 32 |
| 586 | oputu i |
| 587 | oputc 32 |
| 588 | oputu id |
| 589 | oputc 32 |
| 590 | oputu ulong get gbcs 5 |
| 591 | oputc 32 |
| 592 | oputu ulong get gbcs 4 |
| 593 | oputc 32 |
| 594 | oputu ep |
| 595 | oputc 32 |
| 596 | oputu b |
| 597 | oputc 10 |
| 598 | ret b |
| 599 | end |
| 600 | |
| 601 | note NaN in every A and B word (2 MiB) |
| 602 | fn gbnan to void |
| 603 | b = get gbfb 0 |
| 604 | for i ulong 524288 |
| 605 | gbput add b mul i 4 2143289344 |
| 606 | end |
| 607 | end |
| 608 | |
| 609 | note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B); |
| 610 | note batch z at (z / 3)(3 zs + 64) + (z % 3) zs |
| 611 | fn gbzs md uint w ulong kd ulong to ulong |
| 612 | if eq md 1 |
| 613 | ret mul w add kd 8 |
| 614 | end |
| 615 | ret mul kd add w 16 |
| 616 | end |
| 617 | |
| 618 | fn gbzof z ulong zs ulong to ulong |
| 619 | ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs |
| 620 | end |
| 621 | |
| 622 | fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong |
| 623 | if eq md 1 |
| 624 | ret add mul x add kd 8 kk |
| 625 | end |
| 626 | ret add x mul kk add w 16 |
| 627 | end |
| 628 | |
| 629 | note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z) |
| 630 | fn gbfr x ulong kk ulong z ulong to f64 |
| 631 | s = add mul x 7 mul kk 3 |
| 632 | s = add s mul z 5 |
| 633 | a = sub f64 rem s 5 2.0f64 |
| 634 | s = add mul x 13 mul kk 11 |
| 635 | s = add s mul z 3 |
| 636 | b = f64 rem s 64 |
| 637 | ret add a div b 512.0f64 |
| 638 | end |
| 639 | |
| 640 | fn gbint x ulong kk ulong z ulong to f64 |
| 641 | s = add mul x 5 mul kk 7 |
| 642 | s = add s mul z 3 |
| 643 | ret sub f64 rem s 11 5.0f64 |
| 644 | end |
| 645 | |
| 646 | note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded |
| 647 | note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped. |
| 648 | fn gbwop ab uint to void |
| 649 | md = get gbcs ab |
| 650 | k = ulong get gbcs 5 |
| 651 | w = ulong get gbcs add 6 ab |
| 652 | zs = gbzs md w k |
| 653 | base = add get gbfb 0 mul ulong ab 1048576 |
| 654 | fr = eq get gbcs 8 ab |
| 655 | for z ulong 6 |
| 656 | for x ulong w |
| 657 | gbwrow ab z x base zs fr |
| 658 | end |
| 659 | end |
| 660 | end |
| 661 | |
| 662 | fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void |
| 663 | zb = add base mul gbzof z zs 4 |
| 664 | t = add 128 mul 64 ulong ab |
| 665 | hi = mul add mul z t x 128 |
| 666 | k = ulong get gbcs 5 |
| 667 | for kk ulong k |
| 668 | gbwel ab zb x kk gbwv x kk z fr hi |
| 669 | end |
| 670 | end |
| 671 | |
| 672 | note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer |
| 673 | fn gbwv x ulong kk ulong z ulong fr bool to uint |
| 674 | v = gbint x kk z |
| 675 | if fr |
| 676 | v = gbfr x kk z |
| 677 | end |
| 678 | ret bitcast uint f32 v |
| 679 | end |
| 680 | |
| 681 | 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 |
| 682 | fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void |
| 683 | md = get gbcs ab |
| 684 | w = ulong get gbcs add 6 ab |
| 685 | o = gbopf md x kk w ulong get gbcs 5 |
| 686 | gbput add zb mul o 4 u |
| 687 | if eq ab 0 |
| 688 | put gbha add hi kk gbrv u |
| 689 | else |
| 690 | put gbhb add hi kk gbrv u |
| 691 | end |
| 692 | end |
| 693 | |
| 694 | 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, |
| 695 | note else ((r + 3 c + z) mod 3) - 1, as f32 bits |
| 696 | fn gbold3 z ulong r ulong c ulong to f64 |
| 697 | s = add mul r 3 mul c 5 |
| 698 | s = add s z |
| 699 | ret sub f64 rem s 17 8.0f64 |
| 700 | end |
| 701 | |
| 702 | fn gbmv z ulong r ulong c ulong to uint |
| 703 | if eq rem add mul r 5 c 13 0 |
| 704 | ret 2143289344 |
| 705 | end |
| 706 | ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64 |
| 707 | end |
| 708 | |
| 709 | note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns |
| 710 | note (and Y's / M's with a fused epilogue) are NaN |
| 711 | fn gbwc to void |
| 712 | m = ulong get gbcs 6 |
| 713 | zs = mul m add ulong get gbcs 7 8 |
| 714 | for z ulong 6 |
| 715 | for r ulong m |
| 716 | gbwcr z r zs |
| 717 | end |
| 718 | end |
| 719 | end |
| 720 | |
| 721 | fn gbwcr z ulong r ulong zs ulong to void |
| 722 | n = ulong get gbcs 7 |
| 723 | ep = get gbcs 3 |
| 724 | o = gbcro z r zs |
| 725 | for c ulong n |
| 726 | oc = add o mul c 4 |
| 727 | gbput oc bitcast uint f32 gbold3 z r c |
| 728 | if eq ep 1 |
| 729 | gbput add oc 1048576 bitcast uint f32 77.0f64 |
| 730 | end |
| 731 | if eq ep 2 |
| 732 | gbput add oc 1048576 gbmv z r c |
| 733 | end |
| 734 | end |
| 735 | for c ulong 8 |
| 736 | op = add o mul add n c 4 |
| 737 | gbput op 2143289344 |
| 738 | if ne ep 0 |
| 739 | gbput add op 1048576 2143289344 |
| 740 | end |
| 741 | end |
| 742 | end |
| 743 | |
| 744 | note operand ab's gkopa for the case at device address p |
| 745 | fn gbfopa ab uint p ulong to void |
| 746 | md = get gbcs ab |
| 747 | k = get gbcs 5 |
| 748 | w = get gbcs add 6 ab |
| 749 | zs = uint gbzs md ulong w ulong k |
| 750 | so = mul add k 8 4 |
| 751 | sk = uint 4 |
| 752 | if eq md 0 |
| 753 | so = 4 |
| 754 | sk = mul add w 16 4 |
| 755 | end |
| 756 | gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4 |
| 757 | end |
| 758 | |
| 759 | fn gbflch to bool |
| 760 | d = add gkbase get gbfb 0 |
| 761 | gbfopa 0 d |
| 762 | gbfopa 1 add d 1048576 |
| 763 | n = get gbcs 7 |
| 764 | zc = mul get gbcs 6 add n 8 |
| 765 | al = 1.0f64 |
| 766 | if eq get gbcs 9 1 |
| 767 | al = 0.5f64 |
| 768 | end |
| 769 | sm = mul add n 8 4 |
| 770 | s1 = mul add mul zc 3 64 4 |
| 771 | s2 = mul zc 4 |
| 772 | gkopc add d 2097152 sm 4 s1 s2 f32 al |
| 773 | if ne get gbcs 3 0 |
| 774 | gkope get gbcs 3 add d 3145728 |
| 775 | end |
| 776 | if ne get gbcs 4 0 |
| 777 | gkopz get gbcs 4 |
| 778 | end |
| 779 | m = get gbcs 6 |
| 780 | k = get gbcs 5 |
| 781 | bt = eq get gbcs 2 1 |
| 782 | ret gkmm m n k 6 3 bt |
| 783 | end |
| 784 | |
| 785 | note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN |
| 786 | fn gbfcmp to ulong |
| 787 | bad = ulong 0 |
| 788 | for z ulong 6 |
| 789 | for r ulong ulong get gbcs 6 |
| 790 | bad = add bad gbfrow z r |
| 791 | end |
| 792 | end |
| 793 | ret bad |
| 794 | end |
| 795 | |
| 796 | note host byte offset of C row r of block z (row stride n + 8 f32) |
| 797 | fn gbcro z ulong r ulong zs ulong to ulong |
| 798 | n = ulong get gbcs 7 |
| 799 | b = add get gbfb 0 2097152 |
| 800 | b = add b mul gbzof z zs 4 |
| 801 | ret add b mul mul r add n 8 4 |
| 802 | end |
| 803 | |
| 804 | fn gbfrow z ulong r ulong to ulong |
| 805 | n = ulong get gbcs 7 |
| 806 | zs = mul ulong get gbcs 6 add n 8 |
| 807 | o = gbcro z r zs |
| 808 | bad = ulong 0 |
| 809 | for c ulong n |
| 810 | if not gbfel z r c add o mul c 4 |
| 811 | bad = add bad 1 |
| 812 | end |
| 813 | end |
| 814 | for c ulong 8 |
| 815 | op = add o mul add n c 4 |
| 816 | if ne gbw op 2143289344 |
| 817 | bad = add bad 1 |
| 818 | end |
| 819 | if ne get gbcs 3 0 |
| 820 | if ne gbw add op 1048576 2143289344 |
| 821 | bad = add bad 1 |
| 822 | end |
| 823 | end |
| 824 | end |
| 825 | ret bad |
| 826 | end |
| 827 | |
| 828 | note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2 |
| 829 | note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0 |
| 830 | fn gbfel z ulong r ulong c ulong oc ulong to bool |
| 831 | cb = gbw oc |
| 832 | old = gbold3 z r c |
| 833 | if and eq get gbcs 4 3 gt div c 64 div r 64 |
| 834 | ret eq cb bitcast uint f32 old |
| 835 | end |
| 836 | ep = get gbcs 3 |
| 837 | if eq ep 2 |
| 838 | if not gt f64 bitcast f32 gbmv z r c 0.0f64 |
| 839 | ret eq cb 0 |
| 840 | end |
| 841 | end |
| 842 | s = gbfref z r c |
| 843 | if eq get gbcs 2 1 |
| 844 | s = add s old |
| 845 | end |
| 846 | if ne f64 bitcast f32 cb s |
| 847 | ret false |
| 848 | end |
| 849 | if eq ep 1 |
| 850 | w = uint 0 |
| 851 | if gt s 0.0f64 |
| 852 | w = cb |
| 853 | end |
| 854 | ret eq gbw add oc 1048576 w |
| 855 | end |
| 856 | ret true |
| 857 | end |
| 858 | |
| 859 | note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands) |
| 860 | fn gbfref z ulong r ulong c ulong to f64 |
| 861 | k = ulong get gbcs 5 |
| 862 | lo = ulong 0 |
| 863 | hi = mul add div r 64 1 64 |
| 864 | cz = get gbcs 4 |
| 865 | if and eq cz 1 lt hi k |
| 866 | k = hi |
| 867 | end |
| 868 | if eq cz 2 |
| 869 | lo = mul div r 64 64 |
| 870 | end |
| 871 | ia = mul add mul z 128 r 128 |
| 872 | s = gbfdot ia mul add mul z 192 c 128 lo sub k lo |
| 873 | if eq get gbcs 9 1 |
| 874 | s = mul s 0.5f64 |
| 875 | end |
| 876 | ret s |
| 877 | end |
| 878 | |
| 879 | fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64 |
| 880 | s = 0.0f64 |
| 881 | for j ulong kn |
| 882 | kk = add lo j |
| 883 | a = get gbha add ia kk |
| 884 | s = add s mul a get gbhb add ib kk |
| 885 | end |
| 886 | ret s |
| 887 | end |
| 888 | |
| 889 | note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0, |
| 890 | note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile), |
| 891 | note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples |
| 892 | 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, |
| 893 | note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`. |
| 894 | fn gbfl p ulong ma uint mb uint to ulong |
| 895 | for j ulong 10 |
| 896 | put gbcs j 0 |
| 897 | end |
| 898 | put gbcs 0 ma |
| 899 | put gbcs 1 mb |
| 900 | put gbcs 5 16384 |
| 901 | put gbcs 6 64 |
| 902 | put gbcs 7 64 |
| 903 | put gbcs 8 uint p |
| 904 | b = get gbfb 0 |
| 905 | for i ulong 3162112 |
| 906 | gbput add b mul i 4 2143289344 |
| 907 | end |
| 908 | gblw 0 |
| 909 | gblw 1 |
| 910 | put gkinv 0 1 |
| 911 | if not gbllch |
| 912 | ggdie 98 |
| 913 | end |
| 914 | bad = ulong 0 |
| 915 | for r ulong 64 |
| 916 | bad = add bad gblrow r |
| 917 | end |
| 918 | oputc 71 |
| 919 | oputc 70 |
| 920 | oputc 76 |
| 921 | oputc 32 |
| 922 | oputu p |
| 923 | oputc 32 |
| 924 | oputu ulong ma |
| 925 | oputc 32 |
| 926 | oputu ulong mb |
| 927 | oputc 32 |
| 928 | oputu bad |
| 929 | oputc 10 |
| 930 | ret bad |
| 931 | end |
| 932 | |
| 933 | note operand ab of the long case: nonzero words only at kk = 0 mod 29 |
| 934 | fn gblw ab uint to void |
| 935 | md = get gbcs ab |
| 936 | base = add get gbfb 0 mul ulong ab 6291456 |
| 937 | fr = eq get gbcs 8 ab |
| 938 | for x ulong 64 |
| 939 | gblwr md base x fr |
| 940 | end |
| 941 | end |
| 942 | |
| 943 | fn gblwr md uint base ulong x ulong fr bool to void |
| 944 | w = ulong 64 |
| 945 | kd = ulong 16384 |
| 946 | for kk ulong kd |
| 947 | u = uint 0 |
| 948 | if eq rem kk 29 0 |
| 949 | u = gbwv x kk 0 fr |
| 950 | end |
| 951 | o = gbopf md x kk w kd |
| 952 | gbput add base mul o 4 u |
| 953 | end |
| 954 | end |
| 955 | |
| 956 | note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats |
| 957 | fn gbllch to bool |
| 958 | d = add gkbase get gbfb 0 |
| 959 | gblopa 0 d |
| 960 | gblopa 1 add d 6291456 |
| 961 | gkopc add d 12582912 288 4 0 0 f32 1.0f64 |
| 962 | gkopz 0 |
| 963 | ret gkmm 64 64 16384 1 1 false |
| 964 | end |
| 965 | |
| 966 | fn gblopa ab uint p ulong to void |
| 967 | so = uint 65568 |
| 968 | sk = uint 4 |
| 969 | if eq get gbcs ab 0 |
| 970 | so = 4 |
| 971 | sk = 320 |
| 972 | end |
| 973 | gkopa ab p so sk 0 0 |
| 974 | end |
| 975 | |
| 976 | note row r of the long case: 64 exact values, then 8 padding words that must still be NaN |
| 977 | fn gblrow r ulong to ulong |
| 978 | o = add add get gbfb 0 12582912 mul r 288 |
| 979 | bad = ulong 0 |
| 980 | for c ulong 64 |
| 981 | cb = gbw add o mul c 4 |
| 982 | if ne f64 bitcast f32 cb gblref r c |
| 983 | bad = add bad 1 |
| 984 | end |
| 985 | end |
| 986 | for c ulong 8 |
| 987 | if ne gbw add o mul add 64 c 4 2143289344 |
| 988 | bad = add bad 1 |
| 989 | end |
| 990 | end |
| 991 | ret bad |
| 992 | end |
| 993 | |
| 994 | note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk) |
| 995 | fn gblref r ulong c ulong to f64 |
| 996 | fa = eq get gbcs 8 0 |
| 997 | fb = eq get gbcs 8 1 |
| 998 | s = 0.0f64 |
| 999 | for j ulong 565 |
| 1000 | kk = mul j 29 |
| 1001 | a = gbrv gbwv r kk 0 fa |
| 1002 | s = add s mul a gbrv gbwv c kk 0 fb |
| 1003 | end |
| 1004 | ret s |
| 1005 | end |