omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / gpu / gt.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note gt.om: Native SM121 (GB10) 1.58-bit Ternary & 2/4-bit LUT-GEMM SASS emitters, |
| 3 | note raw SM121 instruction encoders (LOP3.LUT, PRMT, SHFL, VOTE, POPC, OMMA.SF.16864, |
| 4 | note F2FP.E4M3/E2M1, MEMBAR.SC.SYS), warp-compaction prefix sum (Pillar 5), |
| 5 | note +16 B bank-conflict-free SMEM LUT builders, and host reference/selftest oracles. |
| 6 | note Pure OM-2 module; compose after om2 (or target a64macos + om2). Zero external deps. |
| 7 | |
| 8 | const GT_RZ uint 255 |
| 9 | const GT_PT uint 7 |
| 10 | const GT_URZ uint 63 |
| 11 | |
| 12 | const GT_SHFL_IDX uint 0 |
| 13 | const GT_SHFL_UP uint 1 |
| 14 | const GT_SHFL_DOWN uint 2 |
| 15 | const GT_SHFL_BFLY uint 3 |
| 16 | |
| 17 | const GT_VOTE_ALL uint 0 |
| 18 | const GT_VOTE_ANY uint 1 |
| 19 | |
| 20 | const GT_SMEM_BLK uint 128 |
| 21 | const GT_SMEM_PAD uint 16 |
| 22 | const GT_SMEM_STRIDE uint 144 |
| 23 | |
| 24 | const GT_SLOT_BYTES ulong 16384 |
| 25 | const GT_MAX_SLOTS ulong 4 |
| 26 | |
| 27 | note 4 kernel slots x 16 KiB = 65536 bytes |
| 28 | global gtcode uchar 65536 |
| 29 | global gtat ulong 1 |
| 30 | global gtpend uint 1 |
| 31 | global gtthr uint 4 |
| 32 | global gtgpr uint 4 |
| 33 | global gtsm uint 4 |
| 34 | |
| 35 | note Control word packer: stall(9..12), yield(13), wbar(14..16), rbar(17..19), wait(20..25) |
| 36 | fn gtcw_y st uint wb uint rb uint wt uint yld uint to uint |
| 37 | a = or shl and st 15 9 shl and yld 1 13 |
| 38 | b = or shl and wb 7 14 shl and rb 7 17 |
| 39 | ret or or a b shl and wt 63 20 |
| 40 | end |
| 41 | |
| 42 | fn gtcw st uint wb uint rb uint wt uint to uint |
| 43 | ret gtcw_y st wb rb wt 0 |
| 44 | end |
| 45 | |
| 46 | note Emit 32-bit little-endian word into gtcode at gtat |
| 47 | fn gtw v uint to void |
| 48 | ap = get gtat 0 |
| 49 | if ge add ap 4 65536 |
| 50 | ret |
| 51 | end |
| 52 | put gtcode ap uchar and v 255 |
| 53 | put gtcode add ap 1 uchar and shr v 8 255 |
| 54 | put gtcode add ap 2 uchar and shr v 16 255 |
| 55 | put gtcode add ap 3 uchar and shr v 24 255 |
| 56 | put gtat 0 add ap 4 |
| 57 | end |
| 58 | |
| 59 | note Emit 128-bit SM121 SASS instruction quad (w0, w1, w2, w3) |
| 60 | fn gtq a uint b uint c uint d uint to void |
| 61 | gtw a |
| 62 | gtw b |
| 63 | gtw c |
| 64 | gtw d |
| 65 | end |
| 66 | |
| 67 | fn gt_slot_start slot ulong to void |
| 68 | if ge slot GT_MAX_SLOTS |
| 69 | ret |
| 70 | end |
| 71 | base = mul slot GT_SLOT_BYTES |
| 72 | for i ulong GT_SLOT_BYTES |
| 73 | put gtcode add base i uchar 0 |
| 74 | end |
| 75 | put gtat 0 base |
| 76 | put gtpend 0 0 |
| 77 | end |
| 78 | |
| 79 | fn gt_slot_pc slot ulong to ulong |
| 80 | base = mul slot GT_SLOT_BYTES |
| 81 | ap = get gtat 0 |
| 82 | if lt ap base |
| 83 | ret 0 |
| 84 | end |
| 85 | ret div sub ap base 16 |
| 86 | end |
| 87 | |
| 88 | fn gt_w0 op uint d uint a uint to uint |
| 89 | v = or op shl and d 255 16 |
| 90 | ret or v shl and a 255 24 |
| 91 | end |
| 92 | |
| 93 | fn gt_cw_take st uint to uint |
| 94 | wt = get gtpend 0 |
| 95 | put gtpend 0 0 |
| 96 | ret gtcw st 7 7 wt |
| 97 | end |
| 98 | |
| 99 | fn gt_cw_sb0 st uint to uint |
| 100 | wt = get gtpend 0 |
| 101 | put gtpend 0 1 |
| 102 | ret gtcw_y st 0 7 wt 1 |
| 103 | end |
| 104 | |
| 105 | note ------------------------------------------------------------------------- |
| 106 | note Deliverable 1: Verified SM121 Raw SASS Instruction Encoders |
| 107 | note Every encoding verified against CUDA 13.0 nvdisasm -b SM121 on Spark. |
| 108 | note ------------------------------------------------------------------------- |
| 109 | |
| 110 | note LOP3.LUT Rd, Ra, Rb, Rc, lut, !PT (register Rb, Rc; op 51 in bw_enc_sm121) |
| 111 | note w0: 0x7212 | Rd<<16 | Ra<<24; w1: Rb; w2: 0x078e0000 | lut<<8 | Rc |
| 112 | fn gtlop3 d uint a uint b uint c uint lut uint to void |
| 113 | gtw gt_w0 uint 0x7212 d a |
| 114 | gtw and b 255 |
| 115 | w2 = or uint 0x078e0000 shl and lut 255 8 |
| 116 | gtw or w2 and c 255 |
| 117 | gtw gt_cw_take 6 |
| 118 | end |
| 119 | |
| 120 | note LOP3.LUT Rd, Ra, imm32, Rc, lut, !PT (immediate imm32, Rc) |
| 121 | note w0: 0x7812 | Rd<<16 | Ra<<24; w1: imm32; w2: 0x078e0000 | lut<<8 | Rc |
| 122 | fn gtlop3i d uint a uint imm uint c uint lut uint to void |
| 123 | gtw gt_w0 uint 0x7812 d a |
| 124 | gtw imm |
| 125 | w2 = or uint 0x078e0000 shl and lut 255 8 |
| 126 | gtw or w2 and c 255 |
| 127 | gtw gt_cw_take 6 |
| 128 | end |
| 129 | |
| 130 | note PRMT Rd, Ra, sel_imm, Rc (byte permute across Ra:Rc, 4-nibble selector) |
| 131 | note w0: 0x7816 | Rd<<16 | Ra<<24; w1: sel_imm; w2: Rc |
| 132 | fn gtprmt d uint a uint sel uint c uint to void |
| 133 | gtw gt_w0 uint 0x7816 d a |
| 134 | gtw sel |
| 135 | gtw and c 255 |
| 136 | gtw gt_cw_take 6 |
| 137 | end |
| 138 | |
| 139 | note PRMT Rd, Ra, Rb, Rc (byte permute across Ra:Rc, register selector Rb) |
| 140 | note w0: 0x7216 | Rd<<16 | Ra<<24; w1: Rb; w2: Rc |
| 141 | fn gtprmtr d uint a uint b uint c uint to void |
| 142 | gtw gt_w0 uint 0x7216 d a |
| 143 | gtw and b 255 |
| 144 | gtw and c 255 |
| 145 | gtw gt_cw_take 6 |
| 146 | end |
| 147 | |
| 148 | note SHFL.{IDX=0, UP=1, DOWN=2, BFLY=3} PT, Rd, Ra, lane_imm, mask_imm |
| 149 | note w0: 0x7f89 | Rd<<16 | Ra<<24; w1: mode<<26 | lane<<21 | mask<<8; w2: 0x000e0000 |
| 150 | fn gtshfl mode uint d uint a uint lane uint mask uint to void |
| 151 | gtw gt_w0 uint 0x7f89 d a |
| 152 | w1 = shl and mode 3 26 |
| 153 | w1 = or w1 shl and lane 31 21 |
| 154 | gtw or w1 shl and mask 31 8 |
| 155 | gtw uint 0x000e0000 |
| 156 | gtw gt_cw_sb0 2 |
| 157 | end |
| 158 | |
| 159 | note SHFL.{IDX=0, UP=1, DOWN=2, BFLY=3} PT, Rd, Ra, Rb, mask_imm (register lane/delta) |
| 160 | note w0: 0x7589 | Rd<<16 | Ra<<24; w1: mode<<26 | mask<<8 | Rb; w2: 0x000e0000 |
| 161 | fn gtshflr mode uint d uint a uint breg uint mask uint to void |
| 162 | gtw gt_w0 uint 0x7589 d a |
| 163 | w1 = shl and mode 3 26 |
| 164 | w1 = or w1 shl and mask 31 8 |
| 165 | gtw or w1 and breg 255 |
| 166 | gtw uint 0x000e0000 |
| 167 | gtw gt_cw_sb0 2 |
| 168 | end |
| 169 | |
| 170 | note VOTE.{ALL=0, ANY=1} Rd, Pd, [!]Ps |
| 171 | note w0: 0x7806 | Rd<<16; w1: 0; w2: negp<<26 | Ps<<23 | Pd<<17 | mode<<8 |
| 172 | fn gtvote d uint pd uint ps uint mode uint negp uint to void |
| 173 | gtw or uint 0x7806 shl and d 255 16 |
| 174 | gtw 0 |
| 175 | w2 = or shl and negp 1 26 shl and ps 7 23 |
| 176 | w2 = or w2 shl and pd 7 17 |
| 177 | gtw or w2 shl and mode 3 8 |
| 178 | gtw gt_cw_take 6 |
| 179 | end |
| 180 | |
| 181 | note POPC Rd, Ra (population count; writes SB0 so consumers wait cleanly) |
| 182 | note w0: 0x7309 | Rd<<16; w1: Ra; w2: 0 |
| 183 | fn gtpopc d uint a uint to void |
| 184 | gtw or uint 0x7309 shl and d 255 16 |
| 185 | gtw and a 255 |
| 186 | gtw 0 |
| 187 | gtw gt_cw_sb0 2 |
| 188 | end |
| 189 | |
| 190 | note OMMA.SF.16864.F32.E2M1.E2M1.UE4M3.4X Rd, Ra, Rb, Rd, Rsa, Rsb, URZ |
| 191 | note Silicon-verified GB10 NVFP4 Tensor Core (29-cycle same-acc RAW latency) |
| 192 | fn gtommasf rd uint ra uint rb uint rsa uint rsb uint stall uint to void |
| 193 | gtw gt_w0 uint 0x747f rd ra |
| 194 | w1 = or uint 0x70000000 and rb 255 |
| 195 | w1 = or w1 shl and rsa 255 8 |
| 196 | gtw or w1 shl and rsb 255 20 |
| 197 | gtw or uint 0x00043e00 and rd 255 |
| 198 | gtw gt_cw_take stall |
| 199 | end |
| 200 | |
| 201 | note F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C Rd, Ra, Rb, Rc (use GT_RZ for Rc=RZ) |
| 202 | fn gtf2fp_e4m3 d uint a uint b uint c uint to void |
| 203 | gtw gt_w0 uint 0x723e d a |
| 204 | gtw and b 255 |
| 205 | gtw or uint 0x04807000 and c 255 |
| 206 | gtw gt_cw_take 6 |
| 207 | end |
| 208 | |
| 209 | note F2FP.SATFINITE.E2M1.F32.PACK_AB_MERGE_C Rd, Ra, Rb, Rc (use GT_RZ for Rc=RZ) |
| 210 | fn gtf2fp_e2m1 d uint a uint b uint c uint to void |
| 211 | gtw gt_w0 uint 0x723e d a |
| 212 | gtw and b 255 |
| 213 | gtw or uint 0x04207000 and c 255 |
| 214 | gtw gt_cw_take 6 |
| 215 | end |
| 216 | |
| 217 | note MEMBAR.SC.SYS (system-scope sequential consistency fence for coherent Grace+GB10 ring) |
| 218 | fn gtmembar to void |
| 219 | gtw uint 0x7992 |
| 220 | gtw 0 |
| 221 | gtw uint 0x00003000 |
| 222 | gtw gt_cw_take 6 |
| 223 | end |
| 224 | |
| 225 | note ------------------------------------------------------------------------- |
| 226 | note Supporting SM121 SASS Encoders (Scoreboards, Shifts, Memory, Math, Flow) |
| 227 | note ------------------------------------------------------------------------- |
| 228 | |
| 229 | fn gtnop to void |
| 230 | gtq uint 0x7918 0 0 uint 0x000fc000 |
| 231 | end |
| 232 | |
| 233 | fn gtw0 to void |
| 234 | cw = gtcw 6 7 7 or get gtpend 0 1 |
| 235 | put gtpend 0 and get gtpend 0 62 |
| 236 | gtq uint 0x7918 0 0 cw |
| 237 | end |
| 238 | |
| 239 | fn gtw1 to void |
| 240 | cw = gtcw 6 7 7 or get gtpend 0 2 |
| 241 | put gtpend 0 and get gtpend 0 61 |
| 242 | gtq uint 0x7918 0 0 cw |
| 243 | end |
| 244 | |
| 245 | fn gtwall to void |
| 246 | cw = gtcw 6 7 7 get gtpend 0 |
| 247 | put gtpend 0 0 |
| 248 | gtq uint 0x7918 0 0 cw |
| 249 | end |
| 250 | |
| 251 | fn gtbar to void |
| 252 | gtwall |
| 253 | gtq uint 0x7b1d 0 uint 0x00010000 uint 0x000fec00 |
| 254 | end |
| 255 | |
| 256 | fn gtexit to void |
| 257 | gtwall |
| 258 | gtq uint 0x794d 0 uint 0x03800000 uint 0x000fea00 |
| 259 | gtq uint 0x00fc7947 uint 0xfffffffc uint 0x0383ffff uint 0x000fc000 |
| 260 | end |
| 261 | |
| 262 | fn gtexitp p uint to void |
| 263 | gtq or uint 0x094d shl and p 15 12 0 uint 0x03800000 gtcw 5 7 7 get gtpend 0 |
| 264 | put gtpend 0 0 |
| 265 | end |
| 266 | |
| 267 | fn gtmovi d uint imm uint to void |
| 268 | cw = gtcw 6 7 7 get gtpend 0 |
| 269 | put gtpend 0 0 |
| 270 | gtq or uint 0x7802 shl and d 255 16 imm uint 0x0f00 cw |
| 271 | end |
| 272 | |
| 273 | fn gtmovrz d uint to void |
| 274 | cw = gtcw 6 7 7 get gtpend 0 |
| 275 | put gtpend 0 0 |
| 276 | gtq or uint 0x7202 shl and d 255 16 GT_RZ uint 0x0f00 cw |
| 277 | end |
| 278 | |
| 279 | fn gt_cw_ld more bool to uint |
| 280 | w = get gtpend 0 |
| 281 | if more |
| 282 | w = and w 2 |
| 283 | end |
| 284 | put gtpend 0 or get gtpend 0 1 |
| 285 | ret gtcw_y 2 0 1 w 1 |
| 286 | end |
| 287 | |
| 288 | fn gt_cw_st more bool to uint |
| 289 | w = get gtpend 0 |
| 290 | if more |
| 291 | w = and w 2 |
| 292 | end |
| 293 | put gtpend 0 or get gtpend 0 2 |
| 294 | ret gtcw_y 4 7 1 w 1 |
| 295 | end |
| 296 | |
| 297 | fn gts2r d uint sr uint more bool to void |
| 298 | gtw or uint 0x7919 shl and d 255 16 |
| 299 | gtw 0 |
| 300 | gtw shl and sr 255 8 |
| 301 | gtw gt_cw_ld more |
| 302 | end |
| 303 | |
| 304 | fn gtldc32 d uint off uint more bool to void |
| 305 | gtw or uint 0xff007b82 shl and d 255 16 |
| 306 | gtw shl div off 4 8 |
| 307 | gtw uint 0x0800 |
| 308 | gtw gt_cw_ld more |
| 309 | end |
| 310 | |
| 311 | fn gtldc64 d uint off uint to void |
| 312 | gtw or uint 0xff007b82 shl and d 255 16 |
| 313 | gtw shl div off 4 8 |
| 314 | gtw uint 0x0a00 |
| 315 | gtw gt_cw_ld false |
| 316 | end |
| 317 | |
| 318 | fn gtldcu64 ud uint off uint to void |
| 319 | gtw or uint 0xff0077ac shl and ud 63 16 |
| 320 | gtw shl div off 8 8 |
| 321 | gtw uint 0x08000a00 |
| 322 | gtw gt_cw_sb0 2 |
| 323 | end |
| 324 | |
| 325 | fn gtiadd3 d uint a uint b uint c uint to void |
| 326 | gtw gt_w0 uint 0x7210 d a |
| 327 | gtw and b 255 |
| 328 | gtw or uint 0x07ffe000 and c 255 |
| 329 | gtw gt_cw_take 6 |
| 330 | end |
| 331 | |
| 332 | fn gtiadd3i d uint a uint imm uint c uint to void |
| 333 | gtw gt_w0 uint 0x7810 d a |
| 334 | gtw imm |
| 335 | gtw or uint 0x07ffe000 and c 255 |
| 336 | gtw gt_cw_take 6 |
| 337 | end |
| 338 | |
| 339 | fn gtimadi d uint a uint imm uint c uint to void |
| 340 | gtw gt_w0 uint 0x7824 d a |
| 341 | gtw imm |
| 342 | gtw or uint 0x078e0200 and c 255 |
| 343 | gtw gt_cw_take 6 |
| 344 | end |
| 345 | |
| 346 | fn gtimadw d uint a uint imm uint c uint to void |
| 347 | gtw gt_w0 uint 0x7825 d a |
| 348 | gtw imm |
| 349 | gtw or uint 0x078e0000 and c 255 |
| 350 | gtw gt_cw_take 6 |
| 351 | end |
| 352 | |
| 353 | fn gtshr d uint a uint n uint to void |
| 354 | gtw or uint 0xff007819 shl and d 255 16 |
| 355 | gtw n |
| 356 | gtw or uint 0x00011600 and a 255 |
| 357 | gtw gt_cw_take 6 |
| 358 | end |
| 359 | |
| 360 | fn gtshfl_l d uint a uint b uint to void |
| 361 | gtw gt_w0 uint 0x7219 d a |
| 362 | gtw and b 255 |
| 363 | gtw uint 0x000006ff |
| 364 | gtw gt_cw_take 6 |
| 365 | end |
| 366 | |
| 367 | fn gtshfl_li d uint a uint imm uint to void |
| 368 | gtw gt_w0 uint 0x7819 d a |
| 369 | gtw imm |
| 370 | gtw uint 0x000006ff |
| 371 | gtw gt_cw_take 6 |
| 372 | end |
| 373 | |
| 374 | fn gtisetp p uint a uint b uint cc uint to void |
| 375 | gtw or uint 0x720c shl and a 255 24 |
| 376 | gtw and b 255 |
| 377 | w2 = or uint 0x03f00070 shl and cc 7 12 |
| 378 | gtw or w2 shl and p 7 17 |
| 379 | gtw gt_cw_take 14 |
| 380 | end |
| 381 | |
| 382 | fn gtldg32 d uint a uint off uint ur uint more bool to void |
| 383 | gtw gt_w0 uint 0x7981 d a |
| 384 | gtw or and ur 63 shl and off 0xffffff 8 |
| 385 | gtw uint 0x0c1e1900 |
| 386 | gtw gt_cw_ld more |
| 387 | end |
| 388 | |
| 389 | fn gtldg128 d uint a uint off uint ur uint more bool to void |
| 390 | gtw gt_w0 uint 0x7981 d a |
| 391 | gtw or and ur 63 shl and off 0xffffff 8 |
| 392 | gtw uint 0x0c1e1d00 |
| 393 | gtw gt_cw_ld more |
| 394 | end |
| 395 | |
| 396 | fn gtstg32p p uint a uint b uint off uint ur uint to void |
| 397 | w0 = or uint 0x0986 shl and p 15 12 |
| 398 | gtw or w0 shl and a 255 24 |
| 399 | gtw or and b 255 shl and off 0xffffff 8 |
| 400 | gtw or uint 0x0c101900 and ur 63 |
| 401 | gtw gt_cw_st false |
| 402 | end |
| 403 | |
| 404 | fn gtstg32 a uint b uint off uint ur uint to void |
| 405 | gtstg32p GT_PT a b off ur |
| 406 | end |
| 407 | |
| 408 | fn gtlds32 d uint a uint off uint more bool to void |
| 409 | gtw gt_w0 uint 0x7984 d a |
| 410 | gtw shl and off 0xffffff 8 |
| 411 | gtw uint 0x0800 |
| 412 | gtw gt_cw_ld more |
| 413 | end |
| 414 | |
| 415 | fn gtsts32 a uint off uint b uint more bool to void |
| 416 | gtw or uint 0x7388 shl and a 255 24 |
| 417 | gtw or and b 255 shl and off 0xffffff 8 |
| 418 | gtw uint 0x0800 |
| 419 | gtw gt_cw_st more |
| 420 | end |
| 421 | |
| 422 | fn gtfadd d uint a uint b uint to void |
| 423 | gtw gt_w0 uint 0x7221 d a |
| 424 | gtw and b 255 |
| 425 | gtw 0 |
| 426 | gtw gt_cw_take 6 |
| 427 | end |
| 428 | |
| 429 | fn gtfsub d uint a uint b uint to void |
| 430 | gtw gt_w0 uint 0x7221 d a |
| 431 | gtw or and b 255 uint 0x80000000 |
| 432 | gtw 0 |
| 433 | gtw gt_cw_take 6 |
| 434 | end |
| 435 | |
| 436 | fn gtfmul d uint a uint b uint to void |
| 437 | gtw gt_w0 uint 0x7220 d a |
| 438 | gtw and b 255 |
| 439 | gtw uint 0x00400000 |
| 440 | gtw gt_cw_take 6 |
| 441 | end |
| 442 | |
| 443 | fn gtbra slot ulong p uint tgt_pc ulong to void |
| 444 | here = gt_slot_pc slot |
| 445 | d = uint and sub sub tgt_pc here 1 0xffffffff |
| 446 | w1 = shl shr d 6 2 |
| 447 | w2 = uint 0x03800000 |
| 448 | if le tgt_pc here |
| 449 | w1 = or w1 uint 0xf0000000 |
| 450 | w2 = uint 0x0383ffff |
| 451 | end |
| 452 | w0 = or uint 0x0947 shl and p 15 12 |
| 453 | gtw or w0 shl and d 63 18 |
| 454 | gtw w1 |
| 455 | gtw w2 |
| 456 | gtw gt_cw_take 5 |
| 457 | end |
| 458 | |
| 459 | note Warp compaction SASS sequence for Pillar 5 (ballot + POPC exclusive prefix sum): |
| 460 | note Computes dense compacted lane index rdst in [0..rcnt-1] and total active count rcnt. |
| 461 | fn gtwcompact ps uint rlane uint rtmp uint rdst uint rcnt uint to void |
| 462 | gtvote rtmp GT_PT ps GT_VOTE_ANY 0 |
| 463 | gtpopc rcnt rtmp |
| 464 | gtmovi rdst 1 |
| 465 | gtshfl_l rdst rdst rlane |
| 466 | gtiadd3i rdst rdst uint 0xffffffff GT_RZ |
| 467 | gtlop3 rdst rtmp rdst GT_RZ uint 0xc0 |
| 468 | gtpopc rdst rdst |
| 469 | gtw0 |
| 470 | end |
| 471 | |
| 472 | note ------------------------------------------------------------------------- |
| 473 | note Deliverable 2a: 1.58-bit Ternary Weight Packer/Unpacker + UE4M3 Scales |
| 474 | note ------------------------------------------------------------------------- |
| 475 | |
| 476 | note Convert UE4M3FN byte (0..126, sign=0) to f32 |
| 477 | fn gt_ue4m3_to_f32 c uchar to f32 |
| 478 | v = and uint c 127 |
| 479 | e = and shr v 3 15 |
| 480 | m = and v 7 |
| 481 | if eq e 0 |
| 482 | ret mul f32 m 0.001953125f32 |
| 483 | end |
| 484 | if and eq e 15 eq m 7 |
| 485 | ret bitcast f32 uint 0x7fc00000 |
| 486 | end |
| 487 | raw = or shl add e 120 23 shl m 20 |
| 488 | ret bitcast f32 raw |
| 489 | end |
| 490 | |
| 491 | note Quantize non-negative f32 to nearest UE4M3FN byte (0..126, max 448.0) |
| 492 | fn gt_f32_to_ue4m3 x f32 to uchar |
| 493 | if le x 0.0f32 |
| 494 | ret uchar 0 |
| 495 | end |
| 496 | v = x |
| 497 | if ge v 448.0f32 |
| 498 | ret uchar 126 |
| 499 | end |
| 500 | best = uchar 0 |
| 501 | best_d = v |
| 502 | for i ulong 127 |
| 503 | cand = gt_ue4m3_to_f32 uchar i |
| 504 | d = sub v cand |
| 505 | if lt d 0.0f32 |
| 506 | d = neg d |
| 507 | end |
| 508 | if lt d best_d |
| 509 | best_d = d |
| 510 | best = uchar i |
| 511 | end |
| 512 | end |
| 513 | ret best |
| 514 | end |
| 515 | |
| 516 | note Encode trit in {-1, 0, +1} to 2-bit code {0: 0, 1: +1, 2: -1} |
| 517 | fn gt_trit_enc t int to uint |
| 518 | if gt t 0 |
| 519 | ret 1 |
| 520 | end |
| 521 | if lt t 0 |
| 522 | ret 2 |
| 523 | end |
| 524 | ret 0 |
| 525 | end |
| 526 | |
| 527 | note Decode 2-bit code {0, 1, 2, 3} to trit {0, +1, -1, 0} |
| 528 | fn gt_trit_dec c uint to int |
| 529 | k = and c 3 |
| 530 | if eq k 1 |
| 531 | ret 1 |
| 532 | end |
| 533 | if eq k 2 |
| 534 | ret -1 |
| 535 | end |
| 536 | ret 0 |
| 537 | end |
| 538 | |
| 539 | fn gt_pack4_byte trits view int * base ulong to uchar |
| 540 | n = len trits |
| 541 | b = uint 0 |
| 542 | for j ulong 4 |
| 543 | idx = add base j |
| 544 | if lt idx n |
| 545 | c = gt_trit_enc get trits idx |
| 546 | b = or b shl c uint mul j 2 |
| 547 | end |
| 548 | end |
| 549 | ret uchar and b 255 |
| 550 | end |
| 551 | |
| 552 | note Pack trits into 4 trits/byte (2 bits/trit); returns packed bytes written |
| 553 | fn gt_pack_tern trits view int * packed edit uchar * to ulong |
| 554 | n = len trits |
| 555 | nb = div add n 3 4 |
| 556 | if gt nb len packed |
| 557 | ret 0 |
| 558 | end |
| 559 | for i ulong nb bound 16777216 |
| 560 | put packed i gt_pack4_byte trits mul i 4 |
| 561 | end |
| 562 | ret nb |
| 563 | end |
| 564 | |
| 565 | note Unpack 4 trits/byte (2 bits/trit) into trits; returns unpacked count |
| 566 | fn gt_unpack_tern packed view uchar * trits edit int * to ulong |
| 567 | n = len trits |
| 568 | nb = div add n 3 4 |
| 569 | if gt nb len packed |
| 570 | ret 0 |
| 571 | end |
| 572 | for i ulong n bound 16777216 |
| 573 | b = uint get packed div i 4 |
| 574 | sh = uint mul rem i 4 2 |
| 575 | put trits i gt_trit_dec shr b sh |
| 576 | end |
| 577 | ret n |
| 578 | end |
| 579 | |
| 580 | fn gt_pack5_byte trits view int * base ulong to uchar |
| 581 | n = len trits |
| 582 | acc = uint 0 |
| 583 | pwr = uint 1 |
| 584 | for j ulong 5 |
| 585 | idx = add base j |
| 586 | d = uint 1 |
| 587 | if lt idx n |
| 588 | t = get trits idx |
| 589 | if gt t 0 |
| 590 | d = 2 |
| 591 | else |
| 592 | if lt t 0 |
| 593 | d = 0 |
| 594 | end |
| 595 | end |
| 596 | end |
| 597 | acc = add acc mul d pwr |
| 598 | pwr = mul pwr 3 |
| 599 | end |
| 600 | ret uchar and acc 255 |
| 601 | end |
| 602 | |
| 603 | note Pack 5 trits/byte in base-3 (3^5 = 243 <= 255, 1.6 bits/trit) |
| 604 | fn gt_pack_tern5 trits view int * packed edit uchar * to ulong |
| 605 | n = len trits |
| 606 | nb = div add n 4 5 |
| 607 | if gt nb len packed |
| 608 | ret 0 |
| 609 | end |
| 610 | for i ulong nb bound 16777216 |
| 611 | put packed i gt_pack5_byte trits mul i 5 |
| 612 | end |
| 613 | ret nb |
| 614 | end |
| 615 | |
| 616 | fn gt_unpack5_byte trits edit int * base ulong byte_v uint to void |
| 617 | n = len trits |
| 618 | acc = byte_v |
| 619 | for j ulong 5 |
| 620 | idx = add base j |
| 621 | d = rem acc 3 |
| 622 | acc = div acc 3 |
| 623 | if lt idx n |
| 624 | put trits idx sub int d 1 |
| 625 | end |
| 626 | end |
| 627 | end |
| 628 | |
| 629 | note Unpack 5 trits/byte base-3 into trits |
| 630 | fn gt_unpack_tern5 packed view uchar * trits edit int * to ulong |
| 631 | n = len trits |
| 632 | nb = div add n 4 5 |
| 633 | if gt nb len packed |
| 634 | ret 0 |
| 635 | end |
| 636 | for i ulong nb bound 16777216 |
| 637 | gt_unpack5_byte trits mul i 5 uint get packed i |
| 638 | end |
| 639 | ret n |
| 640 | end |
| 641 | |
| 642 | fn gt_blk_mean_abs w view f32 * start ulong cnt ulong to f32 |
| 643 | sum_a = 0.0f32 |
| 644 | for j ulong cnt bound 4096 |
| 645 | v = get w add start j |
| 646 | if lt v 0.0f32 |
| 647 | v = neg v |
| 648 | end |
| 649 | sum_a = add sum_a v |
| 650 | end |
| 651 | ret div sum_a f32 cnt |
| 652 | end |
| 653 | |
| 654 | note Compute per-block UE4M3 scale (mean(|w_blk|), clamped to UE4M3 range) |
| 655 | fn gt_quant_ue4m3_scales w view f32 * scales edit uchar * blk ulong to ulong |
| 656 | n = len w |
| 657 | if eq blk 0 |
| 658 | ret 0 |
| 659 | end |
| 660 | ns = div add n sub blk 1 blk |
| 661 | if gt ns len scales |
| 662 | ret 0 |
| 663 | end |
| 664 | for b ulong ns bound 16777216 |
| 665 | start = mul b blk |
| 666 | cnt = sub n start |
| 667 | if gt cnt blk |
| 668 | cnt = blk |
| 669 | end |
| 670 | put scales b gt_f32_to_ue4m3 gt_blk_mean_abs w start cnt |
| 671 | end |
| 672 | ret ns |
| 673 | end |
| 674 | |
| 675 | fn gt_quant_one v f32 thr f32 to int |
| 676 | if gt v thr |
| 677 | ret 1 |
| 678 | end |
| 679 | if lt v neg thr |
| 680 | ret -1 |
| 681 | end |
| 682 | ret 0 |
| 683 | end |
| 684 | |
| 685 | note Quantize float weights into {-1, 0, +1} using per-block UE4M3 scales |
| 686 | fn gt_quant_tern w view f32 * scales view uchar * trits edit int * to ulong |
| 687 | n = len w |
| 688 | ns = len scales |
| 689 | if or eq ns 0 ne len trits n |
| 690 | ret 0 |
| 691 | end |
| 692 | blk = div add n sub ns 1 ns |
| 693 | for i ulong n bound 16777216 |
| 694 | sc = gt_ue4m3_to_f32 get scales div i blk |
| 695 | put trits i gt_quant_one get w i mul sc 0.5f32 |
| 696 | end |
| 697 | ret n |
| 698 | end |
| 699 | |
| 700 | fn gt_dequant_idx packed view uchar * scales view uchar * i ulong blk ulong to f32 |
| 701 | b = get packed div i 4 |
| 702 | sh = uint mul rem i 4 2 |
| 703 | sc = get scales div i blk |
| 704 | t = gt_trit_dec shr uint b sh |
| 705 | ret mul f32 t gt_ue4m3_to_f32 sc |
| 706 | end |
| 707 | |
| 708 | note Dequantize 2-bit packed ternary weights + UE4M3 block scales into f32 |
| 709 | fn gt_dequant_tern packed view uchar * scales view uchar * dst edit f32 * to ulong |
| 710 | n = len dst |
| 711 | ns = len scales |
| 712 | if or eq ns 0 lt len packed div add n 3 4 |
| 713 | ret 0 |
| 714 | end |
| 715 | blk = div add n sub ns 1 ns |
| 716 | for i ulong n bound 16777216 |
| 717 | put dst i gt_dequant_idx packed scales i blk |
| 718 | end |
| 719 | ret n |
| 720 | end |
| 721 | |
| 722 | note ------------------------------------------------------------------------- |
| 723 | note Deliverable 2b: SM121 LUT-GEMM & Ternary-GEMM Kernel Slot Emitters |
| 724 | note Uses +16 B bank-conflict-free SMEM stride (144 B/blk = 2x64 B LUT + 16 B pad) |
| 725 | note and gathers partial sums via LDS + FADD + SHFL.BFLY with ZERO multiplies! |
| 726 | note ------------------------------------------------------------------------- |
| 727 | |
| 728 | note Register map for LUT/Ternary GEMM kernels: |
| 729 | note R0:1=W_ptr, R2:3=x_ptr, R4:5=S_ptr, R6:7=C_ptr |
| 730 | note R8=K, R9=N, R10=tid, R11=ctaid.x, R12=lane, R13=warp |
| 731 | note R14=row, R15=smem_base, R16..23=x0..x7 (4-aligned LDG.E.128!), |
| 732 | note R24..29=LUT build scratch, R30=k_idx, R31=w_addr_lo/hi (R30:31 / R32:33), |
| 733 | note R34=w_u32, R35=nibble/addr, R36=lds_val, R37=acc_f32, R38=shfl_tmp, R39=blk_scale |
| 734 | note UR4=desc_hi_zero |
| 735 | |
| 736 | fn gtandi d uint a uint imm uint to void |
| 737 | gtlop3i d a imm GT_RZ uint 0xc0 |
| 738 | end |
| 739 | |
| 740 | fn gt_emit_gemm_prologue to void |
| 741 | gtldcu64 4 uint 0x358 |
| 742 | gtldc64 0 uint 0x380 |
| 743 | gtldc64 2 uint 0x388 |
| 744 | gtldc64 4 uint 0x390 |
| 745 | gtldc64 6 uint 0x398 |
| 746 | gtldc32 8 uint 0x3a0 false |
| 747 | gtldc32 9 uint 0x3a4 true |
| 748 | gts2r 10 uint 0x21 true |
| 749 | gts2r 11 uint 0x25 false |
| 750 | gtw0 |
| 751 | gtandi 12 10 31 |
| 752 | gtshr 13 10 5 |
| 753 | gtimadi 14 11 256 10 |
| 754 | gtisetp 0 14 9 6 |
| 755 | gtexitp 0 |
| 756 | gtmovrz 37 |
| 757 | gtmovrz 15 |
| 758 | end |
| 759 | |
| 760 | note Emit SMEM 16-entry binary/4-bit partial-sum LUT for 4 floats x[r_x..r_x+3] at sm_base+off |
| 761 | note Zero multiplications: builds all 16 subsets via FADD only! |
| 762 | fn gt_emit_lut4_table r_x uint r_sm uint off uint to void |
| 763 | x0 = r_x |
| 764 | x1 = add r_x 1 |
| 765 | x2 = add r_x 2 |
| 766 | x3 = add r_x 3 |
| 767 | gtmovrz 24 |
| 768 | gtsts32 r_sm off 24 false |
| 769 | gtsts32 r_sm add off 4 x0 true |
| 770 | gtsts32 r_sm add off 8 x1 true |
| 771 | gtfadd 24 x0 x1 |
| 772 | gtsts32 r_sm add off 12 24 true |
| 773 | gtsts32 r_sm add off 16 x2 true |
| 774 | gtfadd 25 x2 x0 |
| 775 | gtsts32 r_sm add off 20 25 true |
| 776 | gtfadd 26 x2 x1 |
| 777 | gtsts32 r_sm add off 24 26 true |
| 778 | gtfadd 27 24 x2 |
| 779 | gtsts32 r_sm add off 28 27 true |
| 780 | gtsts32 r_sm add off 32 x3 true |
| 781 | gtfadd 24 x3 x0 |
| 782 | gtsts32 r_sm add off 36 24 true |
| 783 | gtfadd 25 x3 x1 |
| 784 | gtsts32 r_sm add off 40 25 true |
| 785 | gtfadd 26 x3 x0 |
| 786 | gtfadd 26 26 x1 |
| 787 | gtsts32 r_sm add off 44 26 true |
| 788 | gtfadd 27 x3 x2 |
| 789 | gtsts32 r_sm add off 48 27 true |
| 790 | gtfadd 24 27 x0 |
| 791 | gtsts32 r_sm add off 52 24 true |
| 792 | gtfadd 25 27 x1 |
| 793 | gtsts32 r_sm add off 56 25 true |
| 794 | gtfadd 26 27 x0 |
| 795 | gtfadd 26 26 x1 |
| 796 | gtsts32 r_sm add off 60 26 true |
| 797 | gtw1 |
| 798 | end |
| 799 | |
| 800 | note Emit 16-entry 2-trit partial-sum LUT at r_sm+off for (x0, x1): |
| 801 | note code = t0 | (t1<<2), trit {0->0, 1->+1, 2->-1, 3->0}; zero multiplications! |
| 802 | fn gt_emit_tern2_table r_x uint r_sm uint off uint to void |
| 803 | x0 = r_x |
| 804 | x1 = add r_x 1 |
| 805 | gtmovrz 24 |
| 806 | gtfsub 25 24 x0 |
| 807 | gtfsub 26 24 x1 |
| 808 | gtfadd 27 x0 x1 |
| 809 | gtfsub 28 x1 x0 |
| 810 | gtfsub 29 x0 x1 |
| 811 | gtsts32 r_sm off 24 false |
| 812 | gtsts32 r_sm add off 4 x0 true |
| 813 | gtsts32 r_sm add off 8 25 true |
| 814 | gtsts32 r_sm add off 12 24 true |
| 815 | gtsts32 r_sm add off 16 x1 true |
| 816 | gtsts32 r_sm add off 20 27 true |
| 817 | gtsts32 r_sm add off 24 28 true |
| 818 | gtsts32 r_sm add off 28 x1 true |
| 819 | gtsts32 r_sm add off 32 26 true |
| 820 | gtsts32 r_sm add off 36 29 true |
| 821 | gtfsub 27 25 x1 |
| 822 | gtsts32 r_sm add off 40 27 true |
| 823 | gtsts32 r_sm add off 44 26 true |
| 824 | gtsts32 r_sm add off 48 24 true |
| 825 | gtsts32 r_sm add off 52 x0 true |
| 826 | gtsts32 r_sm add off 56 25 true |
| 827 | gtsts32 r_sm add off 60 24 true |
| 828 | gtw1 |
| 829 | end |
| 830 | |
| 831 | fn gt_emit_warp_bfly_reduce acc uint tmp uint to void |
| 832 | d = uint 16 |
| 833 | while ge d 1 bound 5 |
| 834 | gtshfl GT_SHFL_BFLY tmp acc d 31 |
| 835 | gtw0 |
| 836 | gtfadd acc acc tmp |
| 837 | d = shr d 1 |
| 838 | end |
| 839 | end |
| 840 | |
| 841 | fn gt_emit_store_epilogue to void |
| 842 | gt_emit_warp_bfly_reduce 37 38 |
| 843 | gtisetp 0 12 255 2 |
| 844 | gtimadw 32 14 4 6 |
| 845 | gtmembar |
| 846 | gtstg32p 0 32 37 0 4 |
| 847 | gtw1 |
| 848 | gtexit |
| 849 | end |
| 850 | |
| 851 | fn gt_emit_slot_finish slot ulong thr uint gpr uint smem uint to ulong |
| 852 | while or lt gt_slot_pc slot 32 ne rem gt_slot_pc slot 8 0 bound 2048 |
| 853 | gtnop |
| 854 | end |
| 855 | put gtthr slot thr |
| 856 | put gtgpr slot gpr |
| 857 | put gtsm slot smem |
| 858 | ret mul gt_slot_pc slot 16 |
| 859 | end |
| 860 | |
| 861 | note Emit 4-bit LUT-GEMM SM121 kernel into slot: |
| 862 | note Precomputes 2x16-entry LUTs per 8-activation chunk in +16 B bank-conflict-free SMEM |
| 863 | note (144 B stride), extracts nibbles with LOP3.LUT+SHR, gathers via LDS+FADD (0 multiplies). |
| 864 | fn gt_emit_lut_gemm slot ulong k_dim uint n_dim uint to ulong |
| 865 | if or ge slot GT_MAX_SLOTS or eq k_dim 0 eq n_dim 0 |
| 866 | ret 0 |
| 867 | end |
| 868 | gt_slot_start slot |
| 869 | gt_emit_gemm_prologue |
| 870 | gtmovi 30 0 |
| 871 | gtmovi 29 144 |
| 872 | top = gt_slot_pc slot |
| 873 | gtimadw 32 30 4 2 |
| 874 | gtldg128 16 32 0 4 false |
| 875 | gtldg128 20 32 16 4 true |
| 876 | gtw0 |
| 877 | gtimadi 15 30 18 255 |
| 878 | gt_emit_lut4_table 16 15 0 |
| 879 | gt_emit_lut4_table 20 15 64 |
| 880 | gtbar |
| 881 | gtimadi 32 14 k_dim 255 |
| 882 | gtshr 33 30 1 |
| 883 | gtiadd3 32 32 33 GT_RZ |
| 884 | gtimadw 32 32 1 0 |
| 885 | gtldg32 34 32 0 4 false |
| 886 | gtw0 |
| 887 | for b ulong 4 |
| 888 | sh = uint mul b 8 |
| 889 | gtshr 35 34 sh |
| 890 | gtlop3i 35 35 uint 0x0f GT_RZ uint 0xc0 |
| 891 | gtshfl_li 35 35 2 |
| 892 | gtiadd3 35 15 35 GT_RZ |
| 893 | gtlds32 36 35 0 false |
| 894 | gtw0 |
| 895 | gtfadd 37 37 36 |
| 896 | gtshr 35 34 add sh 4 |
| 897 | gtlop3i 35 35 uint 0x0f GT_RZ uint 0xc0 |
| 898 | gtshfl_li 35 35 2 |
| 899 | gtiadd3 35 15 35 GT_RZ |
| 900 | gtlds32 36 35 64 false |
| 901 | gtw0 |
| 902 | gtfadd 37 37 36 |
| 903 | end |
| 904 | gtiadd3i 30 30 8 GT_RZ |
| 905 | gtisetp 0 30 8 1 |
| 906 | gtbra slot 0 top |
| 907 | gt_emit_store_epilogue |
| 908 | ret gt_emit_slot_finish slot 256 48 mul div add k_dim 7 8 GT_SMEM_STRIDE |
| 909 | end |
| 910 | |
| 911 | note Emit 1.58-bit Ternary LUT-GEMM SM121 kernel into slot: |
| 912 | note Precomputes 2-trit partial-sum LUTs in +16 B padded SMEM (144 B/4-trit-grp), |
| 913 | note extracts 4 trits/byte via LOP3.LUT+SHR, gathers via LDS+FADD, applies UE4M3 scale. |
| 914 | fn gt_emit_tern_gemm slot ulong k_dim uint n_dim uint to ulong |
| 915 | if or ge slot GT_MAX_SLOTS or eq k_dim 0 eq n_dim 0 |
| 916 | ret 0 |
| 917 | end |
| 918 | gt_slot_start slot |
| 919 | gt_emit_gemm_prologue |
| 920 | gtmovi 30 0 |
| 921 | top = gt_slot_pc slot |
| 922 | gtimadw 32 30 4 2 |
| 923 | gtldg128 16 32 0 4 false |
| 924 | gtw0 |
| 925 | gtimadi 15 30 36 255 |
| 926 | gt_emit_tern2_table 16 15 0 |
| 927 | gt_emit_tern2_table 18 15 64 |
| 928 | gtbar |
| 929 | gtimadi 32 14 div k_dim 4 255 |
| 930 | gtshr 33 30 2 |
| 931 | gtiadd3 32 32 33 GT_RZ |
| 932 | gtimadw 32 32 1 0 |
| 933 | gtldg32 34 32 0 4 false |
| 934 | gtw0 |
| 935 | gtlop3i 35 34 uint 0x0f GT_RZ uint 0xc0 |
| 936 | gtshfl_li 35 35 2 |
| 937 | gtiadd3 35 15 35 GT_RZ |
| 938 | gtlds32 36 35 0 false |
| 939 | gtw0 |
| 940 | gtfadd 37 37 36 |
| 941 | gtshr 35 34 4 |
| 942 | gtlop3i 35 35 uint 0x0f GT_RZ uint 0xc0 |
| 943 | gtshfl_li 35 35 2 |
| 944 | gtiadd3 35 15 35 GT_RZ |
| 945 | gtlds32 36 35 64 false |
| 946 | gtw0 |
| 947 | gtfadd 37 37 36 |
| 948 | gtiadd3i 30 30 4 GT_RZ |
| 949 | gtisetp 0 30 8 1 |
| 950 | gtbra slot 0 top |
| 951 | gt_emit_store_epilogue |
| 952 | ret gt_emit_slot_finish slot 256 48 mul div add k_dim 3 4 GT_SMEM_STRIDE |
| 953 | end |
| 954 | |
| 955 | note Emit SM121 Pillar 4/5 feature-showcase kernel slot exercising ALL encoders: |
| 956 | note LOP3.LUT, PRMT, SHFL.{BFLY,UP,IDX}, VOTE+POPC warp compaction, F2FP.{E4M3,E2M1}, |
| 957 | note OMMA.SF.16864.F32.E2M1.E2M1.UE4M3.4X, and MEMBAR.SC.SYS. |
| 958 | fn gt_emit_all_ops_slot slot ulong to ulong |
| 959 | if ge slot GT_MAX_SLOTS |
| 960 | ret 0 |
| 961 | end |
| 962 | gt_slot_start slot |
| 963 | gt_emit_gemm_prologue |
| 964 | gtmovi 16 0x3f800000 |
| 965 | gtmovi 17 0x40000000 |
| 966 | gtmovi 18 0xbf800000 |
| 967 | gtmovi 19 0x3fc00000 |
| 968 | gtlop3 20 16 17 18 uint 0xea |
| 969 | gtlop3i 21 16 uint 0x0f0f0f0f 18 uint 0xc0 |
| 970 | gtprmt 22 20 uint 0x3210 21 |
| 971 | gtprmtr 23 20 12 21 |
| 972 | gtshfl GT_SHFL_BFLY 24 22 1 31 |
| 973 | gtshfl GT_SHFL_UP 25 22 1 0 |
| 974 | gtshfl GT_SHFL_IDX 26 22 0 31 |
| 975 | gtw0 |
| 976 | gtisetp 1 12 16 1 |
| 977 | gtwcompact 1 12 27 28 29 |
| 978 | gtf2fp_e4m3 30 16 17 GT_RZ |
| 979 | gtf2fp_e2m1 31 18 19 GT_RZ |
| 980 | gtommasf 32 20 24 30 31 15 |
| 981 | gtmembar |
| 982 | gtfadd 37 32 28 |
| 983 | gtimadw 38 14 4 6 |
| 984 | gtstg32 38 37 0 4 |
| 985 | gtw1 |
| 986 | gtexit |
| 987 | ret gt_emit_slot_finish slot 256 48 144 |
| 988 | end |
| 989 | |
| 990 | note ------------------------------------------------------------------------- |
| 991 | note Deliverable 3: Host Reference Functions & gt_selftest |
| 992 | note ------------------------------------------------------------------------- |
| 993 | |
| 994 | fn gt_popc_below ballot uint lane ulong to ulong |
| 995 | idx = ulong 0 |
| 996 | for b ulong lane bound 32 |
| 997 | if ne and shr ballot uint b 1 0 |
| 998 | idx = add idx 1 |
| 999 | end |
| 1000 | end |
| 1001 | ret idx |
| 1002 | end |
| 1003 | |
| 1004 | note Host reference for warp ballot + POPC prefix-sum compaction (Pillar 5) |
| 1005 | fn gt_wcompact_ref ballot uint active edit int * to ulong |
| 1006 | cnt = ulong 0 |
| 1007 | lim = len active |
| 1008 | for lane ulong 32 |
| 1009 | if ne and shr ballot uint lane 1 0 |
| 1010 | idx = gt_popc_below ballot lane |
| 1011 | if lt idx lim |
| 1012 | put active idx int lane |
| 1013 | end |
| 1014 | cnt = add cnt 1 |
| 1015 | end |
| 1016 | end |
| 1017 | ret cnt |
| 1018 | end |
| 1019 | |
| 1020 | fn gt_ref_lut4_hi lut edit f32 * loff ulong x3 f32 to void |
| 1021 | for i ulong 8 |
| 1022 | v = get lut add loff i |
| 1023 | put lut add loff add 8 i add x3 v |
| 1024 | end |
| 1025 | end |
| 1026 | |
| 1027 | note Build 16-entry LUT in +16 B bank-conflict-free SMEM stride (zero multiplies) |
| 1028 | fn gt_ref_build_lut4 x view f32 * xoff ulong lut edit f32 * loff ulong to void |
| 1029 | x0 = get x xoff |
| 1030 | x1 = get x add xoff 1 |
| 1031 | x2 = get x add xoff 2 |
| 1032 | put lut loff 0.0f32 |
| 1033 | put lut add loff 1 x0 |
| 1034 | put lut add loff 2 x1 |
| 1035 | put lut add loff 3 add x0 x1 |
| 1036 | put lut add loff 4 x2 |
| 1037 | put lut add loff 5 add x2 x0 |
| 1038 | put lut add loff 6 add x2 x1 |
| 1039 | put lut add loff 7 add x2 add x0 x1 |
| 1040 | gt_ref_lut4_hi lut loff get x add xoff 3 |
| 1041 | end |
| 1042 | |
| 1043 | note Build 16-entry 2-trit LUT (x0, x1) with zero multiplications |
| 1044 | fn gt_ref_build_tern2 x view f32 * xoff ulong lut edit f32 * loff ulong to void |
| 1045 | x0 = get x xoff |
| 1046 | x1 = get x add xoff 1 |
| 1047 | nx0 = neg x0 |
| 1048 | nx1 = neg x1 |
| 1049 | put lut loff 0.0f32 |
| 1050 | put lut add loff 1 x0 |
| 1051 | put lut add loff 2 nx0 |
| 1052 | put lut add loff 3 0.0f32 |
| 1053 | put lut add loff 4 x1 |
| 1054 | put lut add loff 5 add x1 x0 |
| 1055 | put lut add loff 6 add x1 nx0 |
| 1056 | put lut add loff 7 x1 |
| 1057 | put lut add loff 8 nx1 |
| 1058 | put lut add loff 9 add nx1 x0 |
| 1059 | put lut add loff 10 add nx1 nx0 |
| 1060 | put lut add loff 11 nx1 |
| 1061 | put lut add loff 12 0.0f32 |
| 1062 | put lut add loff 13 x0 |
| 1063 | put lut add loff 14 nx0 |
| 1064 | put lut add loff 15 0.0f32 |
| 1065 | end |
| 1066 | |
| 1067 | note Shared 2-nibble (+16 B padded SMEM stride=36) partial-sum gather with zero multiplies |
| 1068 | fn gt_lut_pair_sum smem view f32 * sbase ulong bv uint to f32 |
| 1069 | v0 = get smem add sbase ulong and bv 15 |
| 1070 | v1 = get smem add add sbase 16 ulong and shr bv 4 15 |
| 1071 | ret add v0 v1 |
| 1072 | end |
| 1073 | |
| 1074 | note Helper for gt_lut_gemm_ref: one row m against precomputed +16 B padded SMEM LUT |
| 1075 | fn gt_lut_row w4 view uchar * r_off ulong kbytes ulong smem view f32 * to f32 |
| 1076 | acc = 0.0f32 |
| 1077 | for b ulong kbytes bound 4194304 |
| 1078 | bv = uint get w4 add r_off b |
| 1079 | sb = mul b 36 |
| 1080 | p = gt_lut_pair_sum smem sb bv |
| 1081 | acc = add acc p |
| 1082 | end |
| 1083 | ret acc |
| 1084 | end |
| 1085 | |
| 1086 | fn gt_ref_build_lut4_grp x view f32 * g ulong smem edit f32 * to void |
| 1087 | xo = mul g 8 |
| 1088 | lo = mul g 36 |
| 1089 | gt_ref_build_lut4 x xo smem lo |
| 1090 | gt_ref_build_lut4 x add xo 4 smem add lo 16 |
| 1091 | end |
| 1092 | |
| 1093 | fn gt_ref_build_tern2_grp x view f32 * g ulong smem edit f32 * to void |
| 1094 | xo = mul g 4 |
| 1095 | lo = mul g 36 |
| 1096 | gt_ref_build_tern2 x xo smem lo |
| 1097 | gt_ref_build_tern2 x add xo 2 smem add lo 16 |
| 1098 | end |
| 1099 | |
| 1100 | note Host 4-bit LUT-GEMM reference (zero multiplies in inner loop, +16 B SMEM stride=36 floats) |
| 1101 | note y[m] = sum_{k} w4_bit(m, k) * x[k], where w4 packs 2 nibbles/byte (4 bits/nibble = 8 weights/byte) |
| 1102 | fn gt_lut_gemm_ref x view f32 * w4 view uchar * y edit f32 * to void |
| 1103 | m = len y |
| 1104 | k = len x |
| 1105 | if or eq m 0 or eq k 0 ne rem k 8 0 |
| 1106 | ret |
| 1107 | end |
| 1108 | grps = div k 8 |
| 1109 | if gt mul grps 36 16384 |
| 1110 | ret |
| 1111 | end |
| 1112 | smem = new f32 16384 0.0f32 |
| 1113 | for g ulong grps bound 4096 |
| 1114 | gt_ref_build_lut4_grp x g edit smem |
| 1115 | end |
| 1116 | for r ulong m bound 16384 |
| 1117 | put y r gt_lut_row w4 mul r grps grps view smem |
| 1118 | end |
| 1119 | end |
| 1120 | |
| 1121 | note Helper for gt_tern_gemm_ref: one row against precomputed +16 B padded SMEM LUT + optional UE4M3 scales |
| 1122 | fn gt_tern_row w2 view uchar * row ulong kbytes ulong smem view f32 * to f32 |
| 1123 | acc = 0.0f32 |
| 1124 | r_off = mul row kbytes |
| 1125 | for b ulong kbytes bound 4194304 |
| 1126 | bv = uint get w2 add r_off b |
| 1127 | sb = mul b 36 |
| 1128 | p = gt_lut_pair_sum smem sb bv |
| 1129 | acc = add acc p |
| 1130 | end |
| 1131 | ret acc |
| 1132 | end |
| 1133 | |
| 1134 | fn gt_tern_blk_sum w2 view uchar * cfg view ulong 4 base_b ulong smem view f32 * to f32 |
| 1135 | r_off = get cfg 0 |
| 1136 | blk_b = get cfg 3 |
| 1137 | bacc = 0.0f32 |
| 1138 | for b ulong blk_b bound 4096 |
| 1139 | gb = add base_b b |
| 1140 | bv = uint get w2 add r_off gb |
| 1141 | sb = mul gb 36 |
| 1142 | p = gt_lut_pair_sum smem sb bv |
| 1143 | bacc = add bacc p |
| 1144 | end |
| 1145 | ret bacc |
| 1146 | end |
| 1147 | |
| 1148 | fn gt_tern_row_sc w2 view uchar * cfg view ulong 4 smem view f32 * to f32 |
| 1149 | acc = 0.0f32 |
| 1150 | nb = div get cfg 1 get cfg 3 |
| 1151 | for sb ulong nb bound 65536 |
| 1152 | bacc = gt_tern_blk_sum w2 cfg mul sb get cfg 3 smem |
| 1153 | sc = gt_ue4m3_to_f32 get w2 add get cfg 2 sb |
| 1154 | acc = add acc mul bacc sc |
| 1155 | end |
| 1156 | ret acc |
| 1157 | end |
| 1158 | |
| 1159 | fn gt_tern_init_cfg w2 view uchar * m ulong grps ulong cfg edit ulong 4 to bool |
| 1160 | wbytes = mul m grps |
| 1161 | rem_s = sub len w2 wbytes |
| 1162 | sc_per_row = div rem_s m |
| 1163 | if or eq sc_per_row 0 ne rem grps sc_per_row 0 |
| 1164 | ret false |
| 1165 | end |
| 1166 | put cfg 1 grps |
| 1167 | put cfg 2 wbytes |
| 1168 | put cfg 3 div grps sc_per_row |
| 1169 | ret true |
| 1170 | end |
| 1171 | |
| 1172 | note Host 1.58-bit Ternary LUT-GEMM reference (zero multiplies per trit; optional trailing UE4M3 scales) |
| 1173 | fn gt_tern_gemm_ref x view f32 * w2 view uchar * y edit f32 * to void |
| 1174 | smem = new f32 16384 0.0f32 |
| 1175 | cfg = new ulong 4 0 |
| 1176 | if or eq len y 0 or eq len x 0 ne rem len x 4 0 |
| 1177 | ret |
| 1178 | end |
| 1179 | grps = div len x 4 |
| 1180 | if or lt len w2 mul len y grps gt mul grps 36 16384 |
| 1181 | ret |
| 1182 | end |
| 1183 | for g ulong grps bound 4096 |
| 1184 | gt_ref_build_tern2_grp x g edit smem |
| 1185 | end |
| 1186 | if eq len w2 mul len y grps |
| 1187 | for r ulong len y bound 16384 |
| 1188 | put y r gt_tern_row w2 r grps view smem |
| 1189 | end |
| 1190 | ret |
| 1191 | end |
| 1192 | if not gt_tern_init_cfg w2 len y grps edit cfg |
| 1193 | ret |
| 1194 | end |
| 1195 | for r ulong len y bound 16384 |
| 1196 | put cfg 0 mul r grps |
| 1197 | put y r gt_tern_row_sc w2 view cfg view smem |
| 1198 | put cfg 2 add get cfg 2 div grps get cfg 3 |
| 1199 | end |
| 1200 | end |
| 1201 | |
| 1202 | fn gt_abs_f32 v f32 to f32 |
| 1203 | if lt v 0.0f32 |
| 1204 | ret neg v |
| 1205 | end |
| 1206 | ret v |
| 1207 | end |
| 1208 | |
| 1209 | fn gt_selftest_fill_trits trits edit int * to void |
| 1210 | put trits 0 1 |
| 1211 | put trits 1 -1 |
| 1212 | put trits 2 0 |
| 1213 | put trits 3 1 |
| 1214 | put trits 4 -1 |
| 1215 | put trits 5 -1 |
| 1216 | put trits 6 0 |
| 1217 | put trits 7 1 |
| 1218 | put trits 8 1 |
| 1219 | put trits 9 -1 |
| 1220 | end |
| 1221 | |
| 1222 | fn gt_selftest_pack to int |
| 1223 | trits = new int 10 0 |
| 1224 | p2 = new uchar 3 0 |
| 1225 | u2 = new int 10 0 |
| 1226 | p5 = new uchar 2 0 |
| 1227 | u5 = new int 10 0 |
| 1228 | gt_selftest_fill_trits edit trits |
| 1229 | if ne gt_pack_tern view trits edit p2 3 |
| 1230 | ret 1 |
| 1231 | end |
| 1232 | if ne gt_unpack_tern view p2 edit u2 10 |
| 1233 | ret 2 |
| 1234 | end |
| 1235 | for i ulong 10 |
| 1236 | if ne get u2 i get trits i |
| 1237 | ret 3 |
| 1238 | end |
| 1239 | end |
| 1240 | if ne gt_pack_tern5 view trits edit p5 2 |
| 1241 | ret 4 |
| 1242 | end |
| 1243 | if ne gt_unpack_tern5 view p5 edit u5 10 |
| 1244 | ret 5 |
| 1245 | end |
| 1246 | for i ulong 10 |
| 1247 | if ne get u5 i get trits i |
| 1248 | ret 6 |
| 1249 | end |
| 1250 | end |
| 1251 | ret 0 |
| 1252 | end |
| 1253 | |
| 1254 | fn gt_selftest_compact to int |
| 1255 | act = new int 32 0 |
| 1256 | cnt = gt_wcompact_ref uint 0xa55a000f edit act |
| 1257 | if ne cnt 12 |
| 1258 | ret 7 |
| 1259 | end |
| 1260 | if or ne get act 0 0 ne get act 3 3 |
| 1261 | ret 8 |
| 1262 | end |
| 1263 | if or ne get act 4 17 ne get act 11 31 |
| 1264 | ret 9 |
| 1265 | end |
| 1266 | ret 0 |
| 1267 | end |
| 1268 | |
| 1269 | fn gt_selftest_lut_gemm to int |
| 1270 | x = new f32 8 0.0f32 |
| 1271 | w4 = new uchar 2 0 |
| 1272 | y4 = new f32 2 0.0f32 |
| 1273 | for i ulong 8 |
| 1274 | put x i f32 add i 1 |
| 1275 | end |
| 1276 | put w4 0 uchar 0x35 |
| 1277 | put w4 1 uchar 0xa7 |
| 1278 | gt_lut_gemm_ref view x view w4 edit y4 |
| 1279 | if gt gt_abs_f32 sub get y4 0 15.0f32 0.0001f32 |
| 1280 | ret 10 |
| 1281 | end |
| 1282 | if gt gt_abs_f32 sub get y4 1 20.0f32 0.0001f32 |
| 1283 | ret 11 |
| 1284 | end |
| 1285 | ret 0 |
| 1286 | end |
| 1287 | |
| 1288 | fn gt_selftest_tern_gemm to int |
| 1289 | x = new f32 8 0.0f32 |
| 1290 | tr = new int 16 0 |
| 1291 | w2 = new uchar 4 0 |
| 1292 | yt = new f32 2 0.0f32 |
| 1293 | for i ulong 8 |
| 1294 | put x i f32 add i 1 |
| 1295 | end |
| 1296 | put tr 0 1 |
| 1297 | put tr 1 -1 |
| 1298 | put tr 2 0 |
| 1299 | put tr 3 1 |
| 1300 | put tr 4 1 |
| 1301 | put tr 5 1 |
| 1302 | put tr 6 -1 |
| 1303 | put tr 7 0 |
| 1304 | put tr 8 -1 |
| 1305 | put tr 9 0 |
| 1306 | put tr 10 1 |
| 1307 | put tr 11 1 |
| 1308 | put tr 12 0 |
| 1309 | put tr 13 -1 |
| 1310 | put tr 14 1 |
| 1311 | put tr 15 -1 |
| 1312 | if ne gt_pack_tern view tr edit w2 4 |
| 1313 | ret 12 |
| 1314 | end |
| 1315 | gt_tern_gemm_ref view x view w2 edit yt |
| 1316 | if gt gt_abs_f32 sub get yt 0 7.0f32 0.0001f32 |
| 1317 | ret 13 |
| 1318 | end |
| 1319 | if gt gt_abs_f32 sub get yt 1 -1.0f32 0.0001f32 |
| 1320 | ret 14 |
| 1321 | end |
| 1322 | ret 0 |
| 1323 | end |
| 1324 | |
| 1325 | fn gt_selftest to int |
| 1326 | r = gt_selftest_pack |
| 1327 | if ne r 0 |
| 1328 | ret r |
| 1329 | end |
| 1330 | r = gt_selftest_compact |
| 1331 | if ne r 0 |
| 1332 | ret r |
| 1333 | end |
| 1334 | r = gt_selftest_lut_gemm |
| 1335 | if ne r 0 |
| 1336 | ret r |
| 1337 | end |
| 1338 | r = gt_selftest_tern_gemm |
| 1339 | if ne r 0 |
| 1340 | ret r |
| 1341 | end |
| 1342 | b0 = gt_emit_lut_gemm 0 64 256 |
| 1343 | b1 = gt_emit_tern_gemm 1 64 256 |
| 1344 | b2 = gt_emit_all_ops_slot 2 |
| 1345 | if or eq b0 0 or eq b1 0 eq b2 0 |
| 1346 | ret 15 |
| 1347 | end |
| 1348 | if or lt get gtgpr 0 48 lt get gtgpr 1 48 |
| 1349 | ret 16 |
| 1350 | end |
| 1351 | ret 0 |
| 1352 | end |