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/gk.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 / gk.om` 1754 lines ยท 45753 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note gk.om: general sm_121 launcher and f32 kernel emitters for the Omega LLM. 3 note Needs context.om (CUDApp native/nvrm), vector.om and launch.om (CUDApp native/blackwell), vendored 4 note unmodified from aien-dev/CUDApp `dcba8bb8` (last change `2e242169`) into native/omegallm/gpu/. 5 note QMD words follow omega_blackwell_qmd.c; control words follow omega_blackwell_codegen_gemm.c. 6 note Each kernel id owns a 16 KiB slot of gkcode (64 slots); gkthr/gkgpr/gksm describe its launch shape. 7 global gkcode uchar `1048576` 8 global gkat ulong 1 9 global gkpend uint 1 10 global gkthr uint 64 11 global gkgpr uint 64 12 global gksm uint 64 13 global gkarg uint 32 14 global gkna uint 1 15 global gkmd uint 2 16 fn gkw v uint to void 17 ap = get gkat 0 18 put gkcode ap uchar and v 255 19 put gkcode add ap 1 uchar and shr v 8 255 20 put gkcode add ap 2 uchar and shr v 16 255 21 put gkcode add ap 3 uchar shr v 24 22 put gkat 0 add ap 4 23 end 24 fn gkq a uint b uint c uint d uint to void 25 gkw a 26 gkw b 27 gkw c 28 gkw d 29 end 30 fn gkcw st uint wb uint rb uint wt uint to uint 31 ret or or or shl st 9 shl wb 14 shl rb 17 shl wt 20 32 end 33 note Scheduling kinds: fixed, predicate, variable load, more loads, uniform load, store, more stores. 34 fn gkfx a uint b uint c uint to void 35 gkq a b c gkcw 6 7 7 get gkpend 0 36 put gkpend 0 0 37 end 38 fn gkpr a uint b uint c uint to void 39 gkq a b c gkcw 14 7 7 get gkpend 0 40 put gkpend 0 0 41 end 42 fn gkvr a uint b uint c uint to void 43 gkq a b c or gkcw 4 0 1 get gkpend 0 8192 44 put gkpend 0 3 45 end 46 fn gkvm a uint b uint c uint to void 47 gkq a b c or gkcw 4 0 1 and get gkpend 0 2 8192 48 put gkpend 0 or get gkpend 0 3 49 end 50 fn gkvar a uint b uint c uint more bool to void 51 if more 52 gkvm a b c 53 else 54 gkvr a b c 55 end 56 end 57 fn gkst a uint b uint c uint more bool to void 58 w = get gkpend 0 59 if more 60 w = and w `4294967293` 61 end 62 gkq a b c or gkcw 4 7 1 w 8192 63 put gkpend 0 or w 2 64 end 65 fn gksync to void 66 gkq 31517 0 65536 gkcw 6 7 7 get gkpend 0 67 put gkpend 0 0 68 end 69 fn gkexit to void 70 gkq 31053 0 `58720256` or gkcw 5 7 7 get gkpend 0 8192 71 put gkpend 0 0 72 gkq `16546119` `4294967292` `58982399` `1032192` 73 end 74 note Instruction words, verified against nvdisasm -b SM121. 75 fn gkldc d uint off uint more bool to void 76 gkvar or `4278221698` shl d 16 shl div off 4 8 2048 more 77 end 78 fn gkldc64 d uint off uint to void 79 gkvr or `4278221698` shl d 16 shl div off 4 8 2560 80 end 81 fn gks2r d uint sr uint more bool to void 82 gkvar or 31001 shl d 16 0 shl sr 8 more 83 end 84 fn gkimadi d uint a uint imm uint c uint to void 85 gkfx or or 30756 shl d 16 shl a 24 imm or `126747136` c 86 end 87 fn gkimad d uint a uint b uint c uint to void 88 gkfx or or 29220 shl d 16 shl a 24 b or `126747136` c 89 end 90 fn gkimadw d uint a uint imm uint c uint to void 91 gkfx or or 30757 shl d 16 shl a 24 imm or `126746624` c 92 end 93 fn gkimadh d uint a uint b uint c uint to void 94 gkfx or or 29223 shl d 16 shl a 24 b or `126746624` c 95 end 96 fn gkshr d uint a uint n uint to void 97 gkfx or `4278220825` shl d 16 n or 71168 a 98 end 99 fn gkandi d uint a uint imm uint to void 100 gkfx or or 30738 shl d 16 shl a 24 imm `126796031` 101 end 102 fn gkmovi d uint imm uint to void 103 gkfx or 30722 shl d 16 imm 3840 104 end 105 fn gkmovz d uint to void 106 gkfx or 29186 shl d 16 255 3840 107 end 108 fn gkiadd d uint a uint b uint to void 109 gkfx or or 29200 shl d 16 shl a 24 b `134209791` 110 end 111 note ISETP.GE P p = R a >= imm (signed); the destination predicate is word 2 bit 17, as in gkisetp 112 fn gkisge p uint a uint imm uint to void 113 gkpr or 30732 shl a 24 imm or `66085488` shl p 17 114 end 115 note ISETP P p = R a cc R b (signed; cc LT 1, EQ 2, LE 3, GT 4, NE 5, GE 6) 116 fn gkisetp p uint a uint b uint cc uint to void 117 gkpr or 29196 shl a 24 b or or `66060400` shl cc 12 shl p 17 118 end 119 note IMAD.WIDE.U32 R d:d+1 = R a * R b + R c:c+1 120 fn gkimadwr d uint a uint b uint c uint to void 121 gkfx or or 29221 shl d 16 shl a 24 b or `126746624` c 122 end 123 note @P p EXIT (no trailing self-branch: the block continues when P p is false) 124 fn gkexitp p uint to void 125 gkq or 2381 shl p 12 0 `58720256` or gkcw 5 7 7 get gkpend 0 8192 126 put gkpend 0 0 127 end 128 note @P p LDG/STG: the guard predicate is bits 12-15 of word 0 (p 7 = PT, p + 8 negates), so the 129 note unguarded forms below are p 7 and keep their words 130 fn gkldgp d uint a uint off uint more bool p uint to void 131 gkvar or or or 2433 shl p 12 shl d 16 shl a 24 or 4 shl off 8 `203299072` more 132 end 133 fn gkstgp a uint b uint off uint more bool p uint to void 134 gkst or or 2438 shl p 12 shl a 24 or b shl off 8 `202381572` more 135 end 136 fn gkldg d uint a uint off uint more bool to void 137 gkldgp d a off more 7 138 end 139 fn gkstg a uint b uint off uint more bool to void 140 gkstgp a b off more 7 141 end 142 fn gksts a uint off uint b uint more bool to void 143 gkst or 31112 shl a 24 or b shl off 8 `134220031` more 144 end 145 fn gklds d uint a uint off uint sb uint wt uint to void 146 gkq or or 31108 shl d 16 shl a 24 shl off 8 3072 or gkcw 2 sb 7 wt 8192 147 end 148 note FFMA d = a b + c; ru is the operand reuse mask (bit 0 Ra, bit 1 Rb, bit 2 Rc; control bits 122-124). 149 note A nonzero mask also sets control bit 109 (word-3 bit 13): nvdisasm decodes .reuse only with it set, 150 note and ptxas (CUDA 13 sm_121) emits every .reuse instruction with it set. gkcw never sets it. 151 fn gkffma d uint a uint b uint c uint wt uint ru uint to void 152 y = uint 0 153 if ne ru 0 154 y = or shl ru 26 8192 155 end 156 gkq or or 29219 shl d 16 shl a 24 b c or gkcw 1 7 7 wt y 157 end 158 fn gkfmulf d uint a uint b uint to void 159 gkfx or or 29216 shl d 16 shl a 24 b `4194304` 160 end 161 fn gkffmaf d uint a uint b uint c uint to void 162 gkfx or or 29219 shl d 16 shl a 24 b c 163 end 164 fn gkbra p uint tgt ulong to void 165 here = div get gkat 0 16 166 d = uint and sub sub tgt here 1 `4294967295` 167 w1 = shl shr d 6 2 168 c = uint `58720256` 169 if le tgt here 170 w1 = or w1 `4026531840` 171 c = `58982399` 172 end 173 gkq or or 2375 shl p 12 shl and d 63 18 w1 c gkcw 5 7 7 get gkpend 0 174 put gkpend 0 0 175 end 176 note GEMM operand setup. Mode 0 (O): thread loads outer tid&31, depth tid>>5, step depth 2. 177 note Mode 1 (K): outer tid>>4, depth tid&15, step outer 4. Args of operand ab start at word 8*ab: 178 note ptr lo/hi, outer stride, depth stride, batch stride 1, batch stride 2, per-load delta, tile step. 179 fn gkopnd mode uint blk uint ab uint to void 180 o = add uint 896 mul ab 32 181 if eq mode 0 182 gkandi 11 0 31 183 gkshr 12 0 5 184 else 185 gkshr 11 0 4 186 gkandi 12 0 15 187 end 188 gkimadi 13 11 4 255 189 gkimadi add 92 ab 12 144 13 190 gkimadi 13 blk 32 11 191 gkldc 14 add o 8 false 192 gkldc 15 add o 12 true 193 gkimad 13 13 14 255 194 gkimad 13 12 15 13 195 gkldc 14 add o 16 false 196 gkldc 15 add o 20 true 197 gkimad 13 6 14 13 198 gkimad 13 7 15 13 199 gkldc64 10 o 200 gkimadw add 88 mul ab 2 13 1 10 201 gkldc add 97 ab add o 24 false 202 gkldc add 99 ab add o 28 true 203 end 204 note One k step of the tile: prefetch k+1 fragments into the other set, then 16 FFMA on set kk&1. 205 note Set 0 is a R32-35 b R36-39 on SB3; set 1 is a R40-43 b R44-47 on SB4. Element k accumulates in 206 note R16+k; the FFMAs of one row share a and carry gkru. 207 fn gkfrag k uint a uint b uint w uint ru uint to void 208 c = add 16 k 209 gkffma c a b c w ru 210 end 211 fn gkstep kk uint to void 212 s = and kk 1 213 if lt kk 15 214 o = sub 1 s 215 gklds add 32 mul o 8 94 mul add kk 1 144 add 3 o 0 216 gklds add 36 mul o 8 95 add 2304 mul add kk 1 144 add 3 o 0 217 end 218 w = shl uint 1 add 3 s 219 ra = add 32 mul s 8 220 k = uint 0 221 for i uint 4 222 for j uint 4 223 gkfrag k add ra i add add ra 4 j w gkru j 3 224 w = 0 225 k = add k 1 226 end 227 end 228 end 229 note Causal attention (gkmm cz). gkczx: the block exits when its column tile R2 exceeds R3 + word 28 230 note (word 28 is 0 when only C on and below the diagonal is needed, 2^30 otherwise). gkczk: K tile 231 note count R kn = R3 word 26 + word 22, and both operand pointers (pa, pa + 2) start R3 word 27 tiles 232 note in (tile steps st, st + 1). Dense: word 26 = word 27 = 0. Uses R12, R13 before the main loop. 233 fn gkczx to void 234 gkldc 12 1008 false 235 gkiadd 12 12 3 236 gkisetp 0 2 12 4 237 gkexitp 0 238 end 239 fn gkczk kn uint st uint pa uint to void 240 gkldc kn 984 false 241 gkldc 12 1000 true 242 gkldc 13 1004 true 243 gkimad kn 3 12 kn 244 gkimad 13 3 13 255 245 gkimadwr pa 13 st pa 246 gkimadwr add pa 2 13 add st 1 add pa 2 247 end 248 note One 32x32 tile of C per 64-thread block, 4x4 per thread, K tiles of 16 through shared memory. 249 note C args: ptr words 16/17, row stride 18, col stride 19, batch strides 20/21; 22 ktiles (base), 250 note 23 batch magic, 24 minus level-2 count, 25 alpha bits, 26-28 causal (gkczx, gkczk), 30/31 the 251 note fused epilogue's second pointer (gkope). beta true adds the old C; ep is the fused epilogue. 252 fn gkgemm ma uint mb uint beta bool ep uint to void 253 put gkpend 0 0 254 gkldc 1 892 false 255 gks2r 0 33 true 256 gks2r 2 37 true 257 gks2r 3 38 true 258 gks2r 5 39 true 259 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 260 put gkpend 0 4 261 gkldc 8 988 false 262 gkldc 9 992 true 263 gkimadh 6 5 8 255 264 gkimad 7 6 9 5 265 gkczx 266 gkshr 94 0 3 267 gkimadi 94 94 16 255 268 gkandi 95 0 7 269 gkimadi 95 95 16 255 270 gkopnd ma 3 0 271 gkopnd mb 2 1 272 gkczk 96 99 88 273 gkmovi 101 `4294967295` 274 for i uint 16 275 gkmovz add 16 i 276 end 277 da = uint 288 278 if eq ma 1 279 da = 16 280 end 281 db = uint 288 282 if eq mb 1 283 db = 16 284 end 285 top = div get gkat 0 16 286 put gkpend 0 63 287 for r uint 8 288 gkimadw add 56 mul r 2 97 r 88 289 end 290 for r uint 8 291 gkimadw add 72 mul r 2 98 r 90 292 end 293 gkimadw 88 99 1 88 294 gkimadw 90 100 1 90 295 for r uint 8 296 gkldg add 40 r add 56 mul r 2 0 ne r 0 297 end 298 for r uint 8 299 gkldg add 48 r add 72 mul r 2 0 true 300 end 301 gksync 302 for r uint 8 303 gksts 92 mul r da add 40 r ne r 0 304 end 305 for r uint 8 306 gksts 93 add 2304 mul r db add 48 r true 307 end 308 gksync 309 gklds 32 94 0 3 0 310 gklds 36 95 2304 3 0 311 for kk uint 16 312 gkstep kk 313 end 314 gkiadd 96 96 101 315 gkisge 0 96 1 316 gkbra 0 ulong top 317 gkepi beta ep 318 end 319 note GEMM epilogue: C address of the thread's 4x4 block, alpha scale, optional old-C add, store. 320 note Fused epilogue ep (gkope, beta false): 1 also stores Y = C > 0 ? C : 0 at the second pointer 321 note (words 30/31, base R90), the FFN ReLU forward; 2 loads M at the second pointer's same offsets 322 note into R40-55 and stores M > 0 ? C : 0, the FFN ReLU backward mask (NaN M gives 0). 323 fn gkepi beta bool ep uint to void 324 gkshr 12 0 3 325 gkimadi 12 12 4 255 326 gkimadi 12 3 32 12 327 gkandi 13 0 7 328 gkimadi 13 13 4 255 329 gkimadi 13 2 32 13 330 gkldc 14 968 false 331 gkldc 15 972 true 332 gkimad 12 12 14 255 333 gkimad 12 13 15 12 334 gkldc 10 976 false 335 gkldc 11 980 true 336 gkimad 12 6 10 12 337 gkimad 12 7 11 12 338 gkldc64 10 960 339 gkimadw 88 12 1 10 340 if ne ep 0 341 gkldc64 10 1016 342 gkimadw 90 12 1 10 343 end 344 gkldc 12 996 false 345 if eq ep 2 346 gkadr32 90 347 for k uint 16 348 gkldg add 40 k add 56 mul k 2 0 ne k 0 349 end 350 end 351 gkadr32 88 352 if beta 353 for k uint 16 354 gkldg add 40 k add 56 mul k 2 0 ne k 0 355 end 356 for k uint 16 357 gkffmaf add 16 k add 16 k 12 add 40 k 358 end 359 else 360 for k uint 16 361 gkfmulf add 16 k add 16 k 12 362 end 363 end 364 if eq ep 2 365 t = gkthm 92 366 for k uint 16 367 gkepm add 16 k add 40 k t 368 end 369 end 370 for k uint 16 371 gkstg add 56 mul k 2 add 16 k 0 ne k 0 372 end 373 if eq ep 1 374 for k uint 16 375 gkepm add 16 k add 16 k 255 376 end 377 gkadr32 90 378 for k uint 16 379 gkstg add 56 mul k 2 add 16 k 0 ne k 0 380 end 381 end 382 gkexit 383 end 384 note Address pairs R56-87 of the thread's 4x4 block (row stride R14, column stride R15) from base b. 385 fn gkadr32 b uint to void 386 for i uint 4 387 r0 = add 56 mul i 8 388 gkimadw r0 14 i b 389 for j uint 3 390 gkimadw add r0 mul add j 1 2 15 add j 1 r0 391 end 392 end 393 end 394 note Fused epilogue select: P2 = R m > R t, R e = P2 ? R e : 0 (FSETP.GT, FSEL; gr.om's encoders, 395 note verified against nvdisasm there). t is RZ (255) except under mutant m. 396 fn gkepm e uint m uint t uint to void 397 grfsetp 2 m t 4 398 grfsel e e 255 2 399 end 400 note Mutant m: 1 in the product, 0 in the mutant, whose fused backward mask (ep 2) is M > 2^-10 (the 401 note forward and its stored FP unchanged). gkthm loads that threshold into R r and returns the 402 note mask's compare register: RZ (255) in the product, r in the mutant. 403 const OMUTM ulong 1 404 fn gkthm r uint to uint 405 if eq OMUTM 0 406 gkmovi r grf 0.`0009765625f64` 407 ret r 408 end 409 ret 255 410 end 411 note 64x64 GEMM: one 64x64 tile of C per 64-thread block, 8x8 per thread at rows ty*4+i and 32+ty*4+i, 412 note columns tx*4+j and 32+tx*4+j (ty = tid>>3, tx = tid&7), K tiles of 8 through shared memory 413 note (272-byte k rows, B at 2176). Each C element is still one FFMA chain over k in order, so results 414 note equal the 32x32 kernel bit for bit. The K loop is software pipelined (k8): two 4352-byte shared 415 note buffers; each tile first issues the global loads of the next tile (guarded by P1 = ktiles left 416 note >= 2) into staging registers, runs its 64 FFMA steps from the current buffer, then stores the 417 note staging into the other buffer behind one barrier. Registers: R16-79 accumulators; R80-111 418 note fragments (set 0 a R80-87 b R88-95 on SB3, set 1 a R96-103 b R104-111 on SB4), later epilogue 419 note addresses; R112-127 load addresses, later old C; R128/R130 operand pointers, R132/133 STS bases, 420 note R134/135 LDS bases, R136 ktiles, R137/138 per-load delta, R139/140 tile step, R141 minus one, 421 note later alpha; R142-149 A staging, R150-157 B staging; R158 buffer step (+4352 or -4352). 422 note OMUTT: 1 in the product; 0 in mutant t, whose 64x64 kernels drop the k = 7 product of the thread's 423 note C element (row 0, column 0). 424 const OMUTT ulong 1 425 note OKREU: 1 sets the Ra reuse bit (control bit 122, with bit 109) on each GEMM FFMA whose successor 426 note reads the same a fragment; 0 (variant r0) leaves both clear as k6 did. It does not change the FFMA 427 note chain of any C element, so both builds give the same bits. k7's window also measured a remap of C 428 note column j to accumulator column j xor 1, which moves every k-loop b fragment and accumulator to 429 note different low two index bits; it made no measured difference on sm_121 and was removed 430 note (evidence/OMEGALLM/gpu-k7/). 431 const OKREU ulong 1 432 note gkru j last: the reuse operand mask of the FFMA at column j of a run that ends at column last. 433 fn gkru j uint last uint to uint 434 if and eq OKREU 1 lt j last 435 ret 1 436 end 437 ret 0 438 end 439 note Mode 0 (O): thread loads outer tid, depths 0..7. Mode 1 (K): outer tid>>3 step 8, depth tid&7. 440 fn gkopnd64 mode uint blk uint ab uint to void 441 o = add uint 896 mul ab 32 442 if eq mode 0 443 gkandi 11 0 63 444 gkmovz 12 445 else 446 gkshr 11 0 3 447 gkandi 12 0 7 448 end 449 gkimadi 13 11 4 255 450 gkimadi add 132 ab 12 272 13 451 gkimadi 13 blk 64 11 452 gkldc 14 add o 8 false 453 gkldc 15 add o 12 true 454 gkimad 13 13 14 255 455 gkimad 13 12 15 13 456 gkldc 14 add o 16 false 457 gkldc 15 add o 20 true 458 gkimad 13 6 14 13 459 gkimad 13 7 15 13 460 gkldc64 10 o 461 gkimadw add 128 mul ab 2 13 1 10 462 gkldc add 137 ab add o 24 false 463 gkldc add 139 ab add o 28 true 464 end 465 fn gkstep64 kk uint to void 466 s = and kk 1 467 if lt kk 7 468 o = sub 1 s 469 d = add 80 mul o 16 470 ko = mul add kk 1 272 471 gklds d 134 ko add 3 o 0 472 gklds add d 4 134 add ko 128 add 3 o 0 473 gklds add d 8 135 add 2176 ko add 3 o 0 474 gklds add d 12 135 add 2304 ko add 3 o 0 475 end 476 w = shl uint 1 add 3 s 477 ra = add 80 mul s 16 478 cut = and eq OMUTT 0 eq kk 7 479 for i uint 8 480 for j uint 8 481 k = add mul i 8 j 482 if or not cut ne k 0 483 c = add 16 k 484 u = gkru j 7 485 gkffma c add ra i add add ra 8 j c w u 486 w = 0 487 end 488 end 489 end 490 end 491 note Load round q of a K tile into staging under predicate p (7 = PT): four A and four B loads (tile 492 note loads 4q..4q+3); round 1 advances the operand pointers after its addresses are formed. Round 1 493 note waits only for round 0's address reads. 494 fn gkld64 q uint p uint to void 495 if gt q 0 496 put gkpend 0 2 497 end 498 l = mul q 4 499 for r uint 4 500 gkimadw add 112 mul r 2 137 add l r 128 501 end 502 for r uint 4 503 gkimadw add 120 mul r 2 138 add l r 130 504 end 505 if gt q 0 506 gkimadw 128 139 1 128 507 gkimadw 130 140 1 130 508 end 509 for r uint 4 510 gkldgp add 142 add l r add 112 mul r 2 0 ne r 0 p 511 end 512 for r uint 4 513 gkldgp add 150 add l r add 120 mul r 2 0 true p 514 end 515 end 516 fn gksts64 ma uint mb uint to void 517 da = uint 272 518 if eq ma 1 519 da = 32 520 end 521 db = uint 272 522 if eq mb 1 523 db = 32 524 end 525 for r uint 8 526 gksts 132 mul r da add 142 r ne r 0 527 end 528 for r uint 8 529 gksts 133 add 2176 mul r db add 150 r true 530 end 531 end 532 fn gkgemm64 ma uint mb uint beta bool ep uint to void 533 put gkpend 0 0 534 gkldc 1 892 false 535 gks2r 0 33 true 536 gks2r 2 37 true 537 gks2r 3 38 true 538 gks2r 5 39 true 539 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 540 put gkpend 0 4 541 gkldc 8 988 false 542 gkldc 9 992 true 543 gkimadh 6 5 8 255 544 gkimad 7 6 9 5 545 gkczx 546 gkshr 134 0 3 547 gkimadi 134 134 16 255 548 gkandi 135 0 7 549 gkimadi 135 135 16 255 550 gkopnd64 ma 3 0 551 gkopnd64 mb 2 1 552 gkczk 136 139 128 553 gkmovi 141 `4294967295` 554 for i uint 64 555 gkmovz add 16 i 556 end 557 put gkpend 0 63 558 gkld64 0 7 559 gkld64 1 7 560 gksts64 ma mb 561 gksync 562 gkmovi 158 4352 563 gkiadd 132 132 158 564 gkiadd 133 133 158 565 top = div get gkat 0 16 566 gkisge 1 136 2 567 gklds 80 134 0 3 0 568 gklds 84 134 128 3 0 569 gklds 88 135 2176 3 0 570 gklds 92 135 2304 3 0 571 gkld64 0 1 572 gkld64 1 1 573 for kk uint 8 574 gkstep64 kk 575 end 576 gksts64 ma mb 577 gksync 578 gkiadd 134 134 158 579 gkiadd 135 135 158 580 gkimad 158 158 141 255 581 gkiadd 132 132 158 582 gkiadd 133 133 158 583 gkiadd 136 136 141 584 gkisge 0 136 1 585 gkbra 0 ulong top 586 gkepi64 beta ep 587 end 588 note Accumulator register of element k (row k>>2, column k&3) of quadrant (rh, ch). 589 fn gkacc64 rh uint ch uint k uint to uint 590 ret add add 16 mul add mul rh 4 shr k 2 8 add mul ch 4 and k 3 591 end 592 note One 4x4 quadrant of the 64x64 epilogue: address pairs R80-111 from base R128 (+32 rows R10, 593 note +32 columns R11), alpha R141, old C in R112-127 when beta, store. Fused epilogue ep as in gkepi, 594 note second base R142 (quadrant base R144); ep 2 loads M into R112-127 (beta is false there). 595 fn gkquad64 rh uint ch uint beta bool ep uint to void 596 if eq ep 2 597 gkqadr rh ch 142 144 598 for k uint 16 599 gkldg add 112 k add 80 mul k 2 0 ne k 0 600 end 601 end 602 gkqadr rh ch 128 130 603 if beta 604 for k uint 16 605 gkldg add 112 k add 80 mul k 2 0 ne k 0 606 end 607 end 608 for k uint 16 609 e = gkacc64 rh ch k 610 if beta 611 gkffmaf e e 141 add 112 k 612 else 613 gkfmulf e e 141 614 end 615 end 616 if eq ep 2 617 t = gkthm 140 618 for k uint 16 619 gkepm gkacc64 rh ch k add 112 k t 620 end 621 end 622 for k uint 16 623 e = gkacc64 rh ch k 624 gkstg add 80 mul k 2 e 0 ne k 0 625 end 626 if eq ep 1 627 for k uint 16 628 e = gkacc64 rh ch k 629 gkepm e e 255 630 end 631 gkqadr rh ch 142 144 632 for k uint 16 633 e = gkacc64 rh ch k 634 gkstg add 80 mul k 2 e 0 ne k 0 635 end 636 end 637 end 638 note Address pairs R80-111 of quadrant (rh, ch) from thread base b (quadrant base in R t when moved). 639 fn gkqadr rh uint ch uint b uint t uint to void 640 qb = b 641 if eq rh 1 642 gkimadw t 10 1 b 643 qb = t 644 end 645 if eq ch 1 646 gkimadw t 11 1 qb 647 qb = t 648 end 649 for i uint 4 650 r0 = add 80 mul i 8 651 gkimadw r0 14 i qb 652 for j uint 3 653 gkimadw add r0 mul add j 1 2 15 add j 1 r0 654 end 655 end 656 end 657 note 64x64 epilogue: C base of the thread (R128), second base R142 when fused, quadrant offsets 658 note R10/R11, then four quadrants. 659 fn gkepi64 beta bool ep uint to void 660 gkshr 12 0 3 661 gkimadi 12 12 4 255 662 gkimadi 12 3 64 12 663 gkandi 13 0 7 664 gkimadi 13 13 4 255 665 gkimadi 13 2 64 13 666 gkldc 14 968 false 667 gkldc 15 972 true 668 gkimad 12 12 14 255 669 gkimad 12 13 15 12 670 gkldc 10 976 false 671 gkldc 11 980 true 672 gkimad 12 6 10 12 673 gkimad 12 7 11 12 674 gkldc64 10 960 675 gkimadw 128 12 1 10 676 if ne ep 0 677 gkldc64 10 1016 678 gkimadw 142 12 1 10 679 end 680 gkimadi 10 14 32 255 681 gkimadi 11 15 32 255 682 gkldc 141 996 false 683 for h uint 4 684 gkquad64 shr h 1 and h 1 beta ep 685 end 686 gkexit 687 end 688 note BF16 tensor-core GEMM (ids 27..36, docs/omegallm/CONTRACT-BF16.md): one 64x64 tile of C per 689 note 128-thread block, K tiles of 32, f32 operands rounded to bf16 (round to nearest even) by F2FP while 690 note staging into shared memory, HMMA.16816.F32.BF16 accumulating in f32, the f32 epilogue of gkepi64. 691 note Words verified with nvdisasm -b SM121 against ptxas 13.0 sm_121 output for mma.sync.aligned 692 note .m16n8k16.row.col.f32.bf16.bf16.f32, cvt.rn.bf16x2.f32 and ld.global.v4.f32. 693 note HMMA d a b c: d, c and a 4-aligned, b 2-aligned. ptxas keeps 16 cycles between the HMMAs of a warp 694 note (stall 15, then gknop of stall 1); a dependent HMMA comes 128 cycles later here. 695 fn gkhmma d uint a uint b uint c uint wt uint to void 696 gkq or or 29244 shl d 16 shl a 24 b or c 268288 gkcw 15 7 7 wt 697 gknop 698 end 699 fn gknop to void 700 gkq 31000 0 0 gkcw 1 7 7 0 701 end 702 note F2FP.BF16.F32.PACK_AB d a b: d = bf16(a) << 16 | bf16(b), round to nearest even; rz rounds toward 703 note zero (mutant v only). Stall 2 as ptxas. 704 const OMUTV ulong 1 705 fn gkf2fp d uint a uint b uint wt uint to void 706 c = uint 4351 707 if eq OMUTV 0 708 c = 102655 709 end 710 gkq or or 29246 shl d 16 shl a 24 b c or gkcw 2 7 7 wt 8192 711 end 712 note LDG.E.128 R d..d+3 = [R a:a+1] under predicate p (7 = PT) 713 fn gkldgq d uint a uint more bool p uint to void 714 gkvar or or or 2433 shl p 12 shl d 16 shl a 24 4 `203300096` more 715 end 716 note R d = ((tid >> s) & m) mult (+ R d unless first); R8 is the scratch. 717 fn gkbt d uint s uint m uint mult uint first bool to void 718 if eq s 0 719 gkandi 8 0 m 720 else 721 gkshr 8 0 s 722 gkandi 8 8 m 723 end 724 c = d 725 if first 726 c = 255 727 end 728 gkimadi d 8 mult c 729 end 730 note Operand ab of the BF16 tile. Mode 0 (O, outer stride 4): thread loads outers 4 (tid & 15) + 0..3, 731 note depths 4 (tid >> 4) + j, j < 4 (one LDG.128 per depth, delta = depth stride). Mode 1 (K, depth 732 note stride 4): outers (tid >> 3) + 16 i, i < 4 (delta 16 outer strides), depths 4 (tid & 7) + 0..3. 733 note Pointer R112 + 2 ab, delta R137 + ab, tile step (32 depths) R139 + ab. 734 fn gkopndb mode uint blk uint ab uint to void 735 o = add uint 896 mul ab 32 736 if eq mode 0 737 gkandi 11 0 15 738 gkimadi 11 11 4 255 739 gkshr 12 0 4 740 gkimadi 12 12 4 255 741 else 742 gkshr 11 0 3 743 gkandi 12 0 7 744 gkimadi 12 12 4 255 745 end 746 gkimadi 13 blk 64 11 747 gkldc 14 add o 8 false 748 gkldc 15 add o 12 true 749 gkimad 13 13 14 255 750 gkimad 13 12 15 13 751 gkldc 14 add o 16 false 752 gkldc 15 add o 20 true 753 gkimad 13 6 14 13 754 gkimad 13 7 15 13 755 gkldc64 10 o 756 gkimadw add 112 mul ab 2 13 1 10 757 gkldc add 137 ab add o 24 false 758 gkldc add 139 ab add o 28 true 759 end 760 note Shared memory, per 8192-byte buffer, k pair p = k >> 1 (0..15), outer r (0..63): A at 761 note 2048 (p >> 3) + 512 (r >> 4) + 64 (r & 7) + 16 (p & 3) + 4 ((r >> 3) & 1) + 8 ((p >> 2) & 1), 762 note B at 4096 + the same with the weights 4 and 8 exchanged, so that one LDS.128 at 512 j + 16 lane 763 note (+ 2048 per k16 step) gives a lane its four A registers (rows g, g + 8; k 2t, 2t + 8) of m16 764 note block j, or the two B registers of n8 blocks 2j and 2j + 1 (g = lane >> 2, t = lane & 3). 765 note gkstsb: the thread's STS base R132 + ab for operand mode (o = 4 for A, 8 for B is the r-bit-3 weight). 766 fn gkstsb mode uint ab uint to void 767 d = add 132 ab 768 ro = add 4 mul ab 4 769 ko = sub 12 ro 770 if eq mode 1 771 gkbt d 2 1 2048 true 772 gkbt d 3 7 64 false 773 gkbt d 0 1 32 false 774 gkbt d 6 1 ro false 775 gkbt d 1 1 ko false 776 else 777 gkbt d 6 1 2048 true 778 gkbt d 2 3 512 false 779 gkbt d 0 1 256 false 780 gkbt d 4 1 32 false 781 gkbt d 1 1 ro false 782 gkbt d 5 1 ko false 783 end 784 end 785 note LDS bases: R134 = 1024 wm + 16 lane (A, warp row wm = tid >> 6), R135 = 1024 wn + 16 lane (B, warp 786 note column wn = (tid >> 5) & 1). Mutant l (OMUTL 0) swaps the A rows of lanes g and g ^ 1. 787 const OMUTL ulong 1 788 fn gkldsb to void 789 gkbt 134 6 1 1024 true 790 gkbt 134 0 31 16 false 791 if eq OMUTL 0 792 gkbt 134 2 1 `4294967168` false 793 gkmovi 8 64 794 gkiadd 134 134 8 795 end 796 gkbt 135 5 1 1024 true 797 gkbt 135 0 31 16 false 798 end 799 note Load a K tile into staging under predicate p: A in R80-95, B in R96-111 (addresses R116-131), 800 note then advance both operand pointers by their tile steps. 801 fn gkldb p uint to void 802 for i uint 4 803 gkimadw add 116 mul i 2 137 i 112 804 end 805 for i uint 4 806 gkimadw add 124 mul i 2 138 i 114 807 end 808 gkimadw 112 139 1 112 809 gkimadw 114 140 1 114 810 for i uint 4 811 gkldgq add 80 mul i 4 add 116 mul i 2 ne i 0 p 812 end 813 for i uint 4 814 gkldgq add 96 mul i 4 add 124 mul i 2 true p 815 end 816 end 817 note Packed word x (0..7) of a staged operand at s: mode 1 is (i, e) = (x >> 1, x & 1), the k pair e of 818 note outer load i; mode 0 is (jp, c) = (x >> 2, x & 3), depth pair jp of outer c. gkcvd is the packed 819 note register (in place), gkcvl the lower-k f32 register (the hi one follows it, + 1 or + 4), gkcvo 820 note the STS offset. 821 fn gkcvl md uint x uint s uint to uint 822 if eq md 1 823 ret add add s mul shr x 1 4 mul and x 1 2 824 end 825 ret add add s mul shr x 2 8 and x 3 826 end 827 fn gkcvd md uint x uint s uint to uint 828 if eq md 1 829 ret add add s mul shr x 1 4 and x 1 830 end 831 ret gkcvl md x s 832 end 833 fn gkcvo md uint x uint to uint 834 if eq md 1 835 ret add mul shr x 1 512 mul and x 1 16 836 end 837 ret add mul and x 3 64 mul shr x 2 16 838 end 839 note Round the staged tile to bf16 pairs (the first F2FP waits for the loads), then store it. 840 fn gkcvsb ma uint mb uint to void 841 w = get gkpend 0 842 put gkpend 0 0 843 for ab uint 2 844 md = ma 845 if eq ab 1 846 md = mb 847 end 848 s = add 80 mul ab 16 849 hd = uint 1 850 if eq md 0 851 hd = 4 852 end 853 for x uint 8 854 l = gkcvl md x s 855 gkf2fp gkcvd md x s add l hd l w 856 w = 0 857 end 858 end 859 for ab uint 2 860 md = ma 861 if eq ab 1 862 md = mb 863 end 864 gkcvst md ab add 80 mul ab 16 865 end 866 end 867 note gkcvsb's stores of operand ab (mode md, staged at register s) 868 fn gkcvst md uint ab uint s uint to void 869 for x uint 8 870 gksts add 132 ab add gkcvo md x mul ab 4096 gkcvd md x s or ne x 0 ne ab 0 871 end 872 end 873 note Mutant o (OMUTO 0) runs one K tile fewer than the tile count. 874 const OMUTO ulong 1 875 fn gkgemmb ma uint mb uint beta bool ep uint to void 876 put gkpend 0 0 877 gkldc 1 892 false 878 gks2r 0 33 true 879 gks2r 2 37 true 880 gks2r 3 38 true 881 gks2r 5 39 true 882 gkq `4278482860` 27392 `134220288` or gkcw 4 2 7 get gkpend 0 8192 883 put gkpend 0 4 884 gkldc 8 988 false 885 gkldc 9 992 true 886 gkimadh 6 5 8 255 887 gkimad 7 6 9 5 888 gkczx 889 gkldsb 890 gkstsb ma 0 891 gkstsb mb 1 892 gkopndb ma 3 0 893 gkopndb mb 2 1 894 gkczk 136 139 112 895 gkmovi 141 `4294967295` 896 if eq OMUTO 0 897 gkiadd 136 136 141 898 end 899 for i uint 32 900 gkmovz add 16 i 901 end 902 put gkpend 0 63 903 gkldb 7 904 gkcvsb ma mb 905 gksync 906 gkmovi 142 8192 907 gkiadd 132 132 142 908 gkiadd 133 133 142 909 top = div get gkat 0 16 910 gkisge 1 136 2 911 for kk uint 2 912 s = add 3 kk 913 f = add 48 mul kk 16 914 ko = mul kk 2048 915 gklds f 134 ko s 0 916 gklds add f 4 134 add ko 512 s 0 917 gklds add f 8 135 add ko 4096 s 0 918 gklds add f 12 135 add ko 4608 s 0 919 end 920 gkldb 1 921 for kk uint 2 922 gkmmak kk 923 end 924 gkcvsb ma mb 925 gksync 926 gkiadd 134 134 142 927 gkiadd 135 135 142 928 gkimad 142 142 141 255 929 gkiadd 132 132 142 930 gkiadd 133 133 142 931 gkiadd 136 136 141 932 gkisge 0 136 1 933 gkbra 0 ulong top 934 gkepib beta ep 935 end 936 note gkgemmb's eight HMMAs of k16 step kk (the first waits on barrier 3 + kk of the LDS loads) 937 fn gkmmak kk uint to void 938 w = shl uint 1 add 3 kk 939 f = add 48 mul kk 16 940 for mi uint 2 941 for ni uint 4 942 d = add add 16 mul mi 16 mul ni 4 943 b = add add add f 8 mul shr ni 1 4 mul and ni 1 2 944 gkhmma d add f mul mi 4 b d w 945 w = 0 946 end 947 end 948 end 949 note Address pairs R48 + 2 e of the 16 elements e = 4 ni + c of one m16 half (row g + 8 (c >> 1), 950 note column 8 ni + 2t + (c & 1)) from half base hb; row stride R14, column stride R15. 951 fn gkhadrb hb uint to void 952 gkimadw 52 14 8 hb 953 gkimadw 48 15 0 hb 954 for e uint 16 955 if and ne e 0 ne e 2 956 src = hb 957 if ge and e 3 2 958 src = 52 959 end 960 gkimadw add 48 mul e 2 15 add mul shr e 2 8 and e 1 src 961 end 962 end 963 end 964 note One m16 half mi of the thread's accumulators (R16 + 16 mi + e): ep 2 loads M into R80-95, beta 965 note the old C; alpha R141; fused epilogue as gkquad64. Half bases: C R112 + 4 mi, second R114 + 4 mi. 966 fn gkhalfb mi uint beta bool ep uint to void 967 hc = add 112 mul mi 4 968 hy = add 114 mul mi 4 969 a0 = add 16 mul mi 16 970 if eq ep 2 971 gkhadrb hy 972 for e uint 16 973 gkldg add 80 e add 48 mul e 2 0 ne e 0 974 end 975 end 976 gkhadrb hc 977 if beta 978 for e uint 16 979 gkldg add 80 e add 48 mul e 2 0 ne e 0 980 end 981 end 982 for e uint 16 983 if beta 984 gkffmaf add a0 e add a0 e 141 add 80 e 985 else 986 gkfmulf add a0 e add a0 e 141 987 end 988 end 989 if eq ep 2 990 t = gkthm 140 991 for e uint 16 992 gkepm add a0 e add 80 e t 993 end 994 end 995 for e uint 16 996 gkstg add 48 mul e 2 add a0 e 0 ne e 0 997 end 998 if eq ep 1 999 for e uint 16 1000 gkepm add a0 e add a0 e 255 1001 end 1002 gkhadrb hy 1003 for e uint 16 1004 gkstg add 48 mul e 2 add a0 e 0 ne e 0 1005 end 1006 end 1007 end 1008 note Thread C base R112 (and second base R114 when fused): row 64 R3 + 32 wm + g, column 1009 note 64 R2 + 32 wn + 2t; half 1 bases R116 / R118 are 16 rows further. 1010 fn gkepib beta bool ep uint to void 1011 gkbt 12 6 1 32 true 1012 gkbt 12 2 7 1 false 1013 gkimadi 12 3 64 12 1014 gkbt 13 5 1 32 true 1015 gkbt 13 0 3 2 false 1016 gkimadi 13 2 64 13 1017 gkldc 14 968 false 1018 gkldc 15 972 true 1019 gkimad 12 12 14 255 1020 gkimad 12 13 15 12 1021 gkldc 10 976 false 1022 gkldc 11 980 true 1023 gkimad 12 6 10 12 1024 gkimad 12 7 11 12 1025 gkldc64 10 960 1026 gkimadw 112 12 1 10 1027 if ne ep 0 1028 gkldc64 10 1016 1029 gkimadw 114 12 1 10 1030 end 1031 gkimadw 116 14 16 112 1032 if ne ep 0 1033 gkimadw 118 14 16 114 1034 end 1035 gkldc 141 996 false 1036 for mi uint 2 1037 gkhalfb mi beta ep 1038 end 1039 gkexit 1040 end 1041 note Kernel table: ids 0..7 = 32x32 gemm (one 16 KiB slot each), ids 40, 42, ..., 54 = 64x64 gemm 1042 note (40 + 2 (ma + 2 mb + 4 beta), two slots each: the pipelined K loop does not fit in one). Fused 1043 note epilogue kernels (gkope, beta false): 25 (32x32) and 56 (64x64) ep 1 with ma 1 mb 0 (the FFN W1 1044 note forward, ggxw), 26 and 58 ep 2 with ma 1 mb 1 (the FFN dF backward, ggxwt). 1045 note gkgpr must be at least the highest GPR the kernel touches + 3: the hardware burns 2 GPRs of the 1046 note QMD count for the program counter (Mesa NAK sm70.rs hw_reserved_gprs), so R(n-2) and R(n-1) are 1047 note outside the allocation and a kernel using them hangs (launch wait -110). The 64x64 kernels use 1048 note R158 (buffer step), so 160 hung (evidence/OMEGALLM/gpu-k8 attempt 1); 168 is the next multiple of 8. 1049 note tests/omegallm/gpu-disasm.sh checks this rule per slot. 1050 fn gkslot id uint to void 1051 put gkat 0 mul ulong id 16384 1052 end 1053 fn gkall to bool 1054 for g uint 20 1055 id = g 1056 ma = and g 1 1057 mb = and shr g 1 1 1058 bt = ne and g 4 0 1059 ep = uint 0 1060 if ge g 16 1061 ep = add and g 1 1 1062 ma = 1 1063 mb = sub ep 1 1064 bt = false 1065 end 1066 wide = and ge g 8 and ne g 16 ne g 17 1067 if ge g 8 1068 id = add 40 mul sub g 8 2 1069 end 1070 if ge g 16 1071 id = add 24 ep 1072 end 1073 if ge g 18 1074 id = add 54 mul ep 2 1075 end 1076 if not gkemit id ma mb bt ep wide 1077 ret false 1078 end 1079 end 1080 for g uint 10 1081 ma = and g 1 1082 mb = and shr g 1 1 1083 bt = ne and g 4 0 1084 ep = uint 0 1085 if ge g 8 1086 ep = sub g 7 1087 ma = 1 1088 mb = sub ep 1 1089 bt = false 1090 end 1091 if not gkemitb add 27 g ma mb bt ep 1092 ret false 1093 end 1094 end 1095 ret true 1096 end 1097 note Emit the BF16 kernel (ma, mb, beta, ep) into slot id: ids 27 + ma + 2 mb + 4 beta, 35 ep 1 (ma 1 1098 note mb 0), 36 ep 2 (ma 1 mb 1); highest GPR R142, so 152 (multiple of 8 at least 145). 1099 fn gkemitb id uint ma uint mb uint bt bool ep uint to bool 1100 gkslot id 1101 gkgemmb ma mb bt ep 1102 put gkgpr id 152 1103 put gksm id 16384 1104 if gt get gkat 0 mul ulong add id 1 16384 1105 ret false 1106 end 1107 put gkthr id 128 1108 ret true 1109 end 1110 note Emit one GEMM kernel into slot id (two slots when wide, the 64x64 kernel); false on overflow. 1111 fn gkemit id uint ma uint mb uint bt bool ep uint wide bool to bool 1112 gkslot id 1113 ns = uint 1 1114 if wide 1115 gkgemm64 ma mb bt ep 1116 put gkgpr id 168 1117 put gksm id 8704 1118 ns = 2 1119 else 1120 gkgemm ma mb bt ep 1121 put gkgpr id 104 1122 put gksm id 4608 1123 end 1124 if gt get gkat 0 mul ulong add id ns 16384 1125 ret false 1126 end 1127 put gkthr id 64 1128 ret true 1129 end 1130 note Launch: QMD for kernel id with grid gx gy gz; args gkarg[0..gkna). 1131 fn gkqq to void 1132 q = mapaddr get gbmem 2 1133 for i ulong 96 1134 put gbq0 i 0 1135 put gbq1 i 0 1136 end 1137 put gbq0 4 `8388610` 1138 put gbq0 10 `2293760` 1139 put gbq0 12 gblo shr add q 4096 8 1140 put gbq0 14 `5243392` 1141 put gbq0 34 65537 1142 put gbq0 35 2049 1143 put gbq1 4 `20905984` 1144 put gbq1 9 3 1145 put gbq1 10 `1638400` 1146 put gbq1 12 gblo shr q 8 1147 put gbq1 14 `793772964` 1148 put gbq1 15 gblo add q 8192 1149 put gbq1 16 gbhi add q 8192 1150 put gbq1 17 6 1151 put gbq1 19 `2172715008` 1152 put gbq1 20 56 1153 put gbq1 22 `67108864` 1154 put gbq1 58 `271581201` 1155 end 1156 fn gkqcb to void 1157 cb = mapaddr get gbmem 1 1158 scr = add mapaddr get gbmem 2 16384 1159 put gbq1 42 gblo shr cb 6 1160 put gbq1 43 or uint and shr cb 38 524287 `33554432` 1161 put gbq1 44 gblo shr scr 6 1162 put gbq1 45 or uint and shr scr 38 524287 `75497472` 1163 put gbq1 52 gblo shr add cb 768 6 1164 put gbq1 53 or uint and shr add cb 768 38 524287 `8388608` 1165 put gbq1 56 gblo shr scr 6 1166 put gbq1 57 or uint and shr scr 38 524287 `2147483648` 1167 gbdriver cb 1168 e = add cb add 896 mul ulong get gkna 0 4 1169 put gbcb 212 gbhi e 1170 put gbcb 213 gblo e 1171 end 1172 note QMD word 36 (QMDV05_00 bits 1152-1180): SHARED_MEMORY_SIZE_SHIFTED7, then the MIN (32 KiB, hw 9), 1173 note MAX (100 KiB, hw 26) and TARGET SM shared-memory configs, hw value = KiB / 4 + 1 from the GB10 1174 note carveouts 0 8 16 32 64 100. TARGET is the smallest carveout that holds the kernel's shared memory 1175 note times its register-limited blocks per SM, 65536 / (gkgpr * gkthr). Mesa NAK qmd.rs 1176 note gv100_get_hw_smem_sizes sizes TARGET the same way from warp-limited workgroups; the register limit is 1177 note the tighter bound here. A fixed 32 KiB TARGET under-sized the 32x32 GEMM (9 blocks * 4608 B) and the 1178 note pipelined 64x64 GEMM (6 * 8704 B): under it the pipelined kernel was 1.12x slower than the k7b one, 1179 note under this rule 0.91x (evidence/OMEGALLM/gpu-k8). Only ids with gkthr set may call this. 1180 fn gksmw id uint to uint 1181 sm = div add get gksm id 127 128 1182 if lt sm 8 1183 sm = 8 1184 end 1185 need = mul mul div 65536 mul get gkgpr id get gkthr id sm 128 1186 tg = uint 9 1187 if gt need 32768 1188 tg = 17 1189 end 1190 if gt need 65536 1191 tg = 26 1192 end 1193 ret or or `3426304` shl tg 23 sm 1194 end 1195 fn gkqmd id uint gx uint gy uint gz uint to void 1196 gkqq 1197 code = add mapaddr get gbmem 4 mul ulong id 16384 1198 put gbq1 32 gblo shr code 4 1199 put gbq1 33 or uint and shr code 36 `2097151` `20971520` 1200 put gbq1 59 gblo shr code 8 1201 put gbq1 34 or get gkthr id 65536 1202 put gbq1 35 or or 1 shl get gkgpr id 8 131072 1203 put gbq1 36 gksmw id 1204 put gbq1 39 gx 1205 put gbq1 40 gy 1206 put gbq1 41 gz 1207 gkqcb 1208 put gbcb 216 get gkthr id 1209 end 1210 note set after host writes to the arena; the next launch invalidates GPU L2 sysmem lines first 1211 global gkinv uint 1 1212 fn gkbuild id uint gx uint gy uint gz uint to void 1213 gkbody id gx gy gz 1214 gbtail 1215 end 1216 note gkbody: one launch without the completion tail (ham/ollm-speed): QMD, setup, the optional L2 1217 note invalidate, constant bank and argument DMA, QMD semaphore, QMD launch. batch.om (CUDApp 1218 note ham/launch-batch `3b4f5807`, vendored) stacks bodies under one gbtail. 1219 fn gkbody id uint gx uint gy uint gz uint to void 1220 gkqmd id gx gy gz 1221 put gbused 0 0 1222 gbsetup 1223 if ne get gkinv 0 0 1224 note L2_SYSMEM_NCOH_INVALIDATE (clc96f.h:73, MEM_OP_D operation 0x11 in bits 31:27): drop L2 lines 1225 note the host rewrote in the arena; the previous launch ended with L2_FLUSH_DIRTY, so none is dirty 1226 gbmth 0 40 4 1227 gbemit 0 1228 gbemit 0 1229 gbemit 0 1230 gbemit `2281701376` 1231 put gkinv 0 0 1232 end 1233 cb = mapaddr get gbmem 1 1234 q = mapaddr get gbmem 2 1235 gbdma cb 224 1236 for i ulong 224 1237 gbemit get gbcb i 1238 end 1239 na = get gkna 0 1240 gbdma add cb 896 na 1241 for i uint 32 1242 if lt i na 1243 gbemit get gkarg i 1244 end 1245 end 1246 gbinline q false 1247 gbdma add q 8192 1 1248 gbemit 1 1249 gbinline add q 4096 true 1250 end 1251 note gbtail: the completion tail, byte for byte the old end of gkbuild (CUDApp launch.om's gbtail at 1252 note ham/launch-batch has the same words): mark 1, L2_FLUSH_DIRTY, mark 2 1253 fn gbtail to void 1254 mark = mapaddr get gbmem 3 1255 gbrel mark `1145324612` 1256 gbmth 0 40 4 1257 gbemit 0 1258 gbemit 0 1259 gbemit 0 1260 gbemit `2147483648` 1261 gbrel add mark 16 `1179010630` 1262 end 1263 note Launch profile (gprof): while gkpf[0] is 1, each launch appends id gx gy gz m n k wait_ns to gkpl 1264 note (8 words, at most 1024 launches); gkmm sets gkps to its m n k first, other launches log zeros. 1265 global gkpf uint 1 1266 global gkpc ulong 1 1267 global gkpl ulong 8192 1268 global gkps uint 3 1269 fn gkplog id uint gx uint gy uint gz uint w long to void 1270 c = get gkpc 0 1271 if and eq get gkpf 0 1 lt c 1024 1272 o = mul c 8 1273 put gkpl o ulong id 1274 put gkpl add o 1 ulong gx 1275 put gkpl add o 2 ulong gy 1276 put gkpl add o 3 ulong gz 1277 put gkpl add o 4 ulong get gkps 0 1278 put gkpl add o 5 ulong get gkps 1 1279 put gkpl add o 6 ulong get gkps 2 1280 put gkpl add o 7 ulong w 1281 put gkpc 0 add c 1 1282 end 1283 put gkps 0 0 1284 put gkps 1 0 1285 put gkps 2 0 1286 end 1287 note launch accounting: 0 launches, 1 build ns, 2 wait ns (send to the QMD release semaphore), 1288 note 3 max wait ns 1289 global gkt long 4 1290 note wait for the release: spin up to 2 ms on the mark (vendored context.om nvwait sleeps 100 us 1291 note between polls, which cost each launch about 110-150 us in the rung-6 gprof ZERO round trips), 1292 note then hand the rest of the budget to nvwait, which keeps its checks, sleep and timeout 1293 fn gkwait m long off ulong value uint budget ulong to bool 1294 if eq get nvfault 0 0 1295 start = monons 1296 while lt sub monons start `2000000` 1297 if eq mapget m off 4 ulong value 1298 if ne get nvput 0 get nvret 0 1299 put nvret 0 get nvput 0 1300 ret true 1301 end 1302 end 1303 end 1304 end 1305 ret nvwait m off value budget 1306 end 1307 note wait for a word nvwait cannot (it refuses once nvret has caught up with nvput): spin 2 ms, 1308 note then poll every 100 us up to budget ns; a timeout sets nverr -110 and nvfault as nvwait does 1309 fn gkpoll m long off ulong value uint budget ulong to bool 1310 if ne get nvfault 0 0 1311 put nverr 0 neg long 22 1312 ret false 1313 end 1314 start = monons 1315 while true 1316 if eq mapget m off 4 ulong value 1317 ret true 1318 end 1319 el = sub monons start 1320 if or lt el 0 ge el long budget 1321 put nverr 0 neg long 110 1322 put nvfault 0 1 1323 ret false 1324 end 1325 if ge el `2000000` 1326 r = sleepns long 100000 1327 if ne r 0 1328 put nverr 0 r 1329 put nvfault 0 1 1330 ret false 1331 end 1332 end 1333 end 1334 ret false 1335 end 1336 note submit the built pushbuffer and wait for the launch to retire; returns the wait in ns, or -1 1337 fn gksend to long 1338 r = mapset get gbmem 2 8192 4 0 1339 r = mapset get gbmem 3 0 4 0 1340 r = mapset get gbmem 3 16 4 0 1341 n = get gbused 0 1342 r = mapwrite get gbmem 0 0 gbcmd 0 n 1343 put nvstage 0 41 1344 if ne r long n 1345 put nverr 0 neg long 5 1346 ret neg long 1 1347 end 1348 t1 = monons 1349 if not nvsend get gbmem 0 0 uint div n 4 1350 ret neg long 1 1351 end 1352 put nvstage 0 42 1353 if not gkwait get gbmem 3 16 `1179010630` `5000000000` 1354 ret neg long 1 1355 end 1356 put nvstage 0 43 1357 if ne mapget get gbmem 3 0 4 `1145324612` 1358 put nverr 0 neg long 5 1359 put nvfault 0 1 1360 ret neg long 1 1361 end 1362 note the QMD's own release semaphore is written after the grid retires and may land after the 1363 note second mark (omega_blackwell_engine.c be_wait_release_semaphore waits for it): wait, not peek 1364 put nvstage 0 44 1365 if not gkpoll get gbmem 2 8192 6 `1000000000` 1366 ret neg long 1 1367 end 1368 w = sub monons t1 1369 put gkt 2 add get gkt 2 w 1370 if gt w get gkt 3 1371 put gkt 3 w 1372 end 1373 ret w 1374 end 1375 global gkbat uint 1 1376 fn gklaunch id uint gx uint gy uint gz uint to bool 1377 if or or or eq gx 0 eq gy 0 eq gz 0 ne get nvfault 0 0 1378 put nverr 0 neg long 22 1379 ret false 1380 end 1381 if or eq get gkna 0 0 gt get gkna 0 32 1382 put nverr 0 neg long 22 1383 ret false 1384 end 1385 put nvstage 0 40 1386 t0 = monons 1387 if eq get gkbat 0 1 1388 ret gkrec id gx gy gz t0 1389 end 1390 gkbuild id gx gy gz 1391 put gkt 0 add get gkt 0 1 1392 put gkt 1 add get gkt 1 sub monons t0 1393 w = gksend 1394 if lt w 0 1395 ret false 1396 end 1397 gkplog id gx gy gz w 1398 ret true 1399 end 1400 note gkbat[0] = 1 (ham/ollm-speed, gg.om ggdbat only): gklaunch records each launch into the open 1401 note batch.om batch (gkbody, then gbappend with the previous ordinal as its dependency: a serial chain) 1402 note instead of sending it; gkbsub sends the batch once and waits once. Profile lines log wait 0. 1403 fn gkrec id uint gx uint gy uint gz uint t0 long to bool 1404 i = get gbst 1 1405 gkbody id gx gy gz 1406 put gkt 0 add get gkt 0 1 1407 put gkt 1 add get gkt 1 sub monons t0 1408 if not gbappend ulong id i 1409 ret false 1410 end 1411 gkplog id gx gy gz 0 1412 ret true 1413 end 1414 note submit the recorded batch: one nvsend, one nvwait (batch.om gbbsubmit); the wait goes to gkt 2 1415 fn gkbsub to bool 1416 t1 = monons 1417 ok = gbbsubmit `5000000000` 1418 w = sub monons t1 1419 put gkt 2 add get gkt 2 w 1420 ret ok 1421 end 1422 note Open the chip: slots 0 cmd, 1 cbank, 2 qmd, 3 marks, 4 code, 5 arena of asz bytes. 1423 fn gkopen asz ulong to bool 1424 if not nvfull 1425 ret false 1426 end 1427 if not nvchan 1428 ret false 1429 end 1430 for i ulong 6 1431 size = ulong 4096 1432 if or eq i 0 eq i 2 1433 size = 65536 1434 end 1435 if eq i 4 1436 size = `1048576` 1437 end 1438 if eq i 5 1439 size = asz 1440 end 1441 note slots 0-4 (pushbuffer, cbank, QMD, marks, code) are CPU-written or host-polled: GPU-uncached 1442 note as in omega_blackwell_engine.c alloc_checked; cached, a release sat in L2 about 1.06 s (cortex 1443 note `d03a999a`). The arena (slot 5) stays cached: gkinv invalidates it after host writes and every 1444 note launch ends with L2_FLUSH_DIRTY before its second mark. 1445 m = nvalloc size eq i 5 1446 if le m 0 1447 ret false 1448 end 1449 put gbmem i m 1450 end 1451 if not gkall 1452 put nverr 0 neg long 7 1453 ret false 1454 end 1455 r = mapwrite get gbmem 4 0 gkcode 0 `1048576` 1456 ret eq r long `1048576` 1457 end 1458 fn gkbase to ulong 1459 ret mapaddr get gbmem 5 1460 end 1461 note GEMM host side. Operand ab (0 = A, 1 = B): device address, outer/depth/batch strides in bytes. 1462 note Mode 1 (K) when the depth stride is 4; gkmm derives per-load delta and tile step for the tile. 1463 fn gkopa ab uint ptr ulong so uint sk uint s1 uint s2 uint to void 1464 o = mul ab 8 1465 put gkarg o gblo ptr 1466 put gkarg add o 1 gbhi ptr 1467 put gkarg add o 2 so 1468 put gkarg add o 3 sk 1469 put gkarg add o 4 s1 1470 put gkarg add o 5 s2 1471 md = uint 0 1472 if eq sk 4 1473 md = 1 1474 end 1475 put gkmd ab md 1476 end 1477 fn gkopc ptr ulong scm uint scn uint s1 uint s2 uint al f32 to void 1478 put gkarg 16 gblo ptr 1479 put gkarg 17 gbhi ptr 1480 put gkarg 18 scm 1481 put gkarg 19 scn 1482 put gkarg 20 s1 1483 put gkarg 21 s2 1484 put gkarg 25 bitcast uint al 1485 end 1486 note C[z] = alpha A[z] B[z] (+ C[z] if beta), z = z1 h2 + z2, z < nz; m n multiples of 32, k of 16. 1487 note m and n both multiples of 64 take the 64x64 kernels (ids 40..54 even), else the 32x32 ones (ids 0..7). 1488 fn gkshape m uint n uint k uint nz uint h2 uint to bool 1489 if ne rem m 32 0 1490 ret false 1491 end 1492 if ne rem n 32 0 1493 ret false 1494 end 1495 if or ne rem k 16 0 eq k 0 1496 ret false 1497 end 1498 if or eq nz 0 gt nz 65535 1499 ret false 1500 end 1501 ret ne h2 0 1502 end 1503 note Batch split magic: z1 = umulhi(z, mg) must equal z / h2 for every z < nz; else nverr -33. 1504 fn gkmagic nz uint h2 uint to uint 1505 mg = uint 0 1506 if gt h2 1 1507 mg = uint div add `4294967296` ulong sub h2 1 ulong h2 1508 end 1509 ok = true 1510 for z uint nz 1511 if ne uint shr mul ulong z ulong mg 32 div z h2 1512 ok = false 1513 end 1514 end 1515 if not ok 1516 put nverr 0 neg long 33 1517 end 1518 ret mg 1519 end 1520 note Per-load delta (word 6) and tile step (word 7) of operand ab for tile width t (32 or 64). 1521 fn gkopt ab uint t uint to void 1522 o = mul ab 8 1523 so = get gkarg add o 2 1524 sk = get gkarg add o 3 1525 gd = mul sk 2 1526 st = mul sk 16 1527 if eq t 64 1528 gd = sk 1529 st = mul sk 8 1530 end 1531 if eq get gkmd ab 1 1532 gd = mul so 4 1533 if eq t 64 1534 gd = mul so 8 1535 end 1536 end 1537 put gkarg add o 6 gd 1538 put gkarg add o 7 st 1539 end 1540 note Tile width: 64 when m and n are both multiples of 64, else 32. 1541 fn gktile m uint n uint to uint 1542 if and eq rem m 64 0 eq rem n 64 0 1543 ret 64 1544 end 1545 ret 32 1546 end 1547 note Causal mode cz, set by gkopz before gkmm (attention over a T x T score matrix, zero above its 1548 note diagonal); gkmm consumes it, so a GEMM without gkopz is dense. 0 dense; 1 A is 1549 note zero above its diagonal (C = P U, DQ = DS K), so row tile r stops after K column (r + 1) t; 1550 note 2 A is zero below its diagonal (C = P^T DO, DK = DS^T Q), so row tile r starts at K row r t; 1551 note 3 only C on and below the diagonal is needed (S = Q K^T, DP = DO U^T), so blocks above it exit. 1552 note Modes 1 and 2 need k = m, mode 3 needs m = n. Mutant q (OMUTQ 0) stops mode 1 one K tile short. 1553 const OMUTQ ulong 1 1554 note BF16 path (docs/omegallm/CONTRACT-BF16.md): callers select gkbf 1 to run every 1555 note 64x64 gkmm on the tensor-core kernels 27..36; 32x32 GEMMs (n = E) stay f32. gkbck 1 checks each 1556 note BF16 launch against gb.om's sampled reference (gbpre before, gbpost after), with gkbp holding 1557 note m n k nz h2 beta ep cz. No fallback: a GEMM the kernels cannot take fails with nverr -22. 1558 global gkbf uint 1 1559 global gkbck uint 1 1560 global gkbp uint 8 1561 global gkcz uint 1 1562 fn gkopz cz uint to void 1563 put gkcz 0 cz 1564 end 1565 fn gkczok m uint n uint k uint cz uint to bool 1566 if eq cz 0 1567 ret true 1568 end 1569 if le cz 2 1570 ret eq k m 1571 end 1572 if eq cz 3 1573 ret eq m n 1574 end 1575 ret false 1576 end 1577 note GEMM K-loop words: 22 K tiles (kd deep), 26 ka, 27 kc, 28 X for tile t and the gkopz mode, 1578 note which it consumes 1579 fn gkczarg t uint k uint to void 1580 cz = get gkcz 0 1581 put gkcz 0 0 1582 kd = uint 16 1583 if eq t 64 1584 kd = 8 1585 if eq get gkbf 0 1 1586 kd = 32 1587 end 1588 end 1589 pt = div t kd 1590 put gkarg 22 div k kd 1591 put gkarg 26 0 1592 put gkarg 27 0 1593 put gkarg 28 `1073741824` 1594 if eq cz 1 1595 put gkarg 22 pt 1596 put gkarg 26 pt 1597 if eq OMUTQ 0 1598 put gkarg 22 sub pt 1 1599 end 1600 end 1601 if eq cz 2 1602 put gkarg 26 neg pt 1603 put gkarg 27 pt 1604 end 1605 if eq cz 3 1606 put gkarg 28 0 1607 end 1608 end 1609 note Fused epilogue ep for the next gkmm, which consumes it (see gkepi): 1 also stores 1610 note Y = C > 0 ? C : 0 at y, 2 stores M > 0 ? C : 0 with M read at y; y has C's strides. ep 1 needs 1611 note operand modes ma 1 mb 0 and ep 2 ma 1 mb 1 (the only fused kernels, gkall), both beta false. 1612 note Measured (k9, evidence/OMEGALLM/gpu-k9): replaces gr.om RELUF/RELUB, 16 launches per max-shape step 1613 note fewer; summed GPU wait 144.56 -> 133.54 ms/step (-7.62%, three rounds each, non-quiet); t200 checkpoint 1614 note bit-identical to the unfused k8 product. 1615 global gkep uint 1 1616 fn gkope ep uint y ulong to void 1617 put gkep 0 ep 1618 put gkarg 30 gblo y 1619 put gkarg 31 gbhi y 1620 end 1621 note true when fused epilogue ep may run with the pending operand modes and beta 1622 fn gkepok ep uint beta bool to bool 1623 if eq ep 0 1624 ret true 1625 end 1626 if or gt ep 2 beta 1627 ret false 1628 end 1629 if ne get gkmd 0 1 1630 ret false 1631 end 1632 ret eq get gkmd 1 sub ep 1 1633 end 1634 fn gkmm m uint n uint k uint nz uint h2 uint beta bool to bool 1635 ep = get gkep 0 1636 put gkep 0 0 1637 if not gkepok ep beta 1638 put gkcz 0 0 1639 put nverr 0 neg long 22 1640 ret false 1641 end 1642 if or not gkshape m n k nz h2 not gkczok m n k get gkcz 0 1643 put gkcz 0 0 1644 put nverr 0 neg long 22 1645 ret false 1646 end 1647 put nverr 0 0 1648 put gkarg 23 gkmagic nz h2 1649 if ne get nverr 0 0 1650 put gkcz 0 0 1651 ret false 1652 end 1653 put gkarg 24 neg h2 1654 t = gktile m n 1655 bf = and eq get gkbf 0 1 eq t 64 1656 if bf 1657 if not gkbfok k 1658 put gkcz 0 0 1659 put nverr 0 neg long 22 1660 ret false 1661 end 1662 else 1663 gkopt 0 t 1664 gkopt 1 t 1665 end 1666 put gkbp 7 get gkcz 0 1667 gkczarg t k 1668 put gkps 0 m 1669 put gkps 1 n 1670 put gkps 2 k 1671 id = gkmmid t beta ep 1672 if bf 1673 id = gkmmb beta ep 1674 end 1675 if not and bf eq get gkbck 0 1 1676 ret gklaunch id div n t div m t nz 1677 end 1678 gkbpset m n k nz h2 beta 1679 put gkbp 6 ep 1680 gbpre 1681 if not gklaunch id div n t div m t nz 1682 ret false 1683 end 1684 ret gbpost 1685 end 1686 note BF16 operands both take the 16-byte tile (gkbfop) and k is a multiple of the K tile 32 1687 fn gkbfok k uint to bool 1688 ba = gkbfop 0 1689 bb = gkbfop 1 1690 ret and and ba bb eq rem k 32 0 1691 end 1692 note gkbp words 0..5 for gb.om's check: m n k nz h2 beta 1693 fn gkbpset m uint n uint k uint nz uint h2 uint beta bool to void 1694 put gkbp 0 m 1695 put gkbp 1 n 1696 put gkbp 2 k 1697 put gkbp 3 nz 1698 put gkbp 4 h2 1699 put gkbp 5 0 1700 if beta 1701 put gkbp 5 1 1702 end 1703 end 1704 note Operand ab for the BF16 tile: 16-byte aligned pointer and batch strides, and 16-byte loads 1705 note (mode 1: so a multiple of 16; mode 0: so = 4, sk a multiple of 16); sets delta and tile step. 1706 fn gkbfop ab uint to bool 1707 o = mul ab 8 1708 so = get gkarg add o 2 1709 sk = get gkarg add o 3 1710 ok = eq rem get gkarg o 16 0 1711 ok = and ok eq rem get gkarg add o 4 16 0 1712 ok = and ok eq rem get gkarg add o 5 16 0 1713 if eq get gkmd ab 1 1714 ok = and ok eq rem so 16 0 1715 put gkarg add o 6 mul so 16 1716 else 1717 ok = and ok and eq so 4 eq rem sk 16 0 1718 put gkarg add o 6 sk 1719 end 1720 put gkarg add o 7 mul sk 32 1721 ret ok 1722 end 1723 note BF16 kernel id for beta and fused epilogue ep; sets the arg count 1724 fn gkmmb beta bool ep uint to uint 1725 if ne ep 0 1726 put gkna 0 32 1727 ret add 34 ep 1728 end 1729 put gkna 0 29 1730 id = add add 27 get gkmd 0 mul 2 get gkmd 1 1731 if beta 1732 id = add id 4 1733 end 1734 ret id 1735 end 1736 note kernel id of a gkmm (gkall's table) for tile t, beta and fused epilogue ep; sets the arg count 1737 fn gkmmid t uint beta bool ep uint to uint 1738 if ne ep 0 1739 put gkna 0 32 1740 if eq t 64 1741 ret add 54 mul ep 2 1742 end 1743 ret add 24 ep 1744 end 1745 put gkna 0 29 1746 id = add get gkmd 0 mul 2 get gkmd 1 1747 if beta 1748 id = add id 4 1749 end 1750 if eq t 64 1751 id = add 40 mul id 2 1752 end 1753 ret id 1754 end