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
omega-mv / gpu / nvidia / om / gemmex / plan.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note The plan of the general GEMM (CUDApp#144): validation, route, tiling, k parts and the per-batch alpha, over two |
| 3 | note word windows. x holds the call (written by the C shell, src/omega_gpu_gemm_ex.c), p the plan (read back by it). |
| 4 | note Codes: 0 OK, 1 BAD_ARGS, 2 TOO_LARGE, 3 UNSUPPORTED (the shell maps them to OMEGA_GPU_GEMM_*). |
| 5 | note x: 0 op_a, 1 op_b, 2 m, 3 n, 4 k, 5 batch, 6 a_type, 7 b_type, 8 c_type, 9 lda, 10 stride_a, 11 ldb, 12 stride_b, |
| 6 | note 13 ldc, 14 stride_c, 15 alpha f32 bits, 16 beta f32 bits, 17 compute, 18 scale, 19 scale_a ptr, 20 scale_b ptr, |
| 7 | note 21 scale_stride_a, 22 scale_stride_b, 23 block_m, 24 block_n, 25 block_k, 26 a ptr, 27 b ptr, 28 c ptr, 29 mutant; |
| 8 | note op 1: 30 k0, 31 mc, 32 nc, 33 any_bad, 34 m0, 35 n0; op 2: 30 sa[0] bits, 31 sb[0] bits, 32 any_bad; |
| 9 | note op 3 (fp8 rows): 30 w, 31 scale, 32 y, 33 x32, 34 x16 (ptrs), 35 n, 36 k, 37 m, 38 block, 39 ldy. |
| 10 | note p (op 0): 0 code, 1 batch, 2 lda, 3 ldb, 4 ldc, 5 op_b in effect, 6 route, 7 core type, 8 kmax, 9 one part, |
| 11 | note 10 compute (alpha != 0), 11 beta f64 bits, 12 sa_r, 13 sa_c, 14 sb_c, 15 16 scale counts a b, 17 18 scale batches |
| 12 | note a b, 19 direct shape, 20 dm, 21 tm, 22 tn, 23 kcore, 24 layout, 25 core esize, 26 27 28 esize a b c, 30 31 row op |
| 13 | note of A' and B', 32 B' row-major (FFMA), 33 core layout, 34 na, 35 nb, 44 45 46 extents a b c; |
| 14 | note op 1: 36 kc (advance), 37 ku (k the core runs), 38 kb, 39 zero copy, 40 41 byte offsets of A and B, 47 core K; |
| 15 | note op 2: 42 alpha f64 bits, 43 direct now; op 3: 0 code, 1 ldy, 2 kb, 3 slab rows, 4 a_type. |
| 16 | note Constants (Wolfram): 2^64-1 18446744073709551615, 2^56 72057594037927936, 2^58 288230376151711744, |
| 17 | note 2^31-1 2147483647, 2^32-1 4294967295, 32 MiB 33554432, f64 1.0 bits 4607182418800017408. |
| 18 | |
| 19 | note stored rows x cols with leading dimension ld: the span, 0 when ld is short or the span passes 2^56 |
| 20 | fn gx_span rows ulong cols ulong ld ulong to ulong |
| 21 | if lt ld cols |
| 22 | ret 0 |
| 23 | end |
| 24 | if gt sub rows 1 div 72057594037927936 ld |
| 25 | ret 0 |
| 26 | end |
| 27 | ret add mul sub rows 1 ld cols |
| 28 | end |
| 29 | |
| 30 | note the elements batch copies of a span reach: 0 when the product or sum overflows or passes 2^58 |
| 31 | fn gx_ext span ulong stride ulong batch ulong to ulong |
| 32 | b1 = sub batch 1 |
| 33 | if ne b1 0 |
| 34 | if gt stride div 18446744073709551615 b1 |
| 35 | ret 0 |
| 36 | end |
| 37 | end |
| 38 | t = mul b1 stride |
| 39 | e = add t span |
| 40 | if lt e t |
| 41 | ret 0 |
| 42 | end |
| 43 | if gt e 288230376151711744 |
| 44 | ret 0 |
| 45 | end |
| 46 | ret e |
| 47 | end |
| 48 | |
| 49 | note 1 when byte ranges [a, a + pa) and [b, b + qb) overlap (address arithmetic modulo 2^64) |
| 50 | fn gx_ovl a ulong pa ulong b ulong qb ulong to ulong |
| 51 | if and lt a add b qb lt b add a pa |
| 52 | ret 1 |
| 53 | end |
| 54 | ret 0 |
| 55 | end |
| 56 | |
| 57 | note the C3 leading-dimension check of one operand (omega_gpu_gemm.c ld_span): 1 when the span fits 32-bit indexing |
| 58 | fn gx_lds col ulong r ulong kk ulong ld ulong to ulong |
| 59 | ou = r |
| 60 | iu = kk |
| 61 | if eq col 1 |
| 62 | ou = kk |
| 63 | iu = r |
| 64 | end |
| 65 | l = ld |
| 66 | if eq l 0 |
| 67 | l = iu |
| 68 | end |
| 69 | if or lt l iu gt l 4294967295 |
| 70 | ret 0 |
| 71 | end |
| 72 | if gt add mul sub ou 1 l iu 4294967295 |
| 73 | ret 0 |
| 74 | end |
| 75 | ret 1 |
| 76 | end |
| 77 | |
| 78 | note omega_gpu_gemm_span: 1 when C3 can address the operands in place |
| 79 | fn gx_gspan lay ulong mm ulong nn ulong kk ulong lda ulong ldb ulong to ulong |
| 80 | if or or eq mm 0 eq nn 0 eq kk 0 |
| 81 | ret 0 |
| 82 | end |
| 83 | if eq gx_lds and lay 1 mm kk lda 0 |
| 84 | ret 0 |
| 85 | end |
| 86 | ret gx_lds xor shr and lay 2 1 1 nn kk ldb |
| 87 | end |
| 88 | |
| 89 | note the route (low byte) and the core type (next byte): C l.228-239 |
| 90 | fn gx_route comp ulong ta ulong tb ulong to ulong |
| 91 | if eq comp 2 |
| 92 | ret 773 |
| 93 | end |
| 94 | if and eq ta tb eq ta 1 |
| 95 | ret 1 |
| 96 | end |
| 97 | if and eq ta tb eq ta 2 |
| 98 | ret 513 |
| 99 | end |
| 100 | if and eq ta tb eq ta 3 |
| 101 | ret 257 |
| 102 | end |
| 103 | if and and ne ta 0 ne ta 2 and ne tb 0 ne tb 2 |
| 104 | ret 2 |
| 105 | end |
| 106 | if and and ne ta 0 ne ta 1 and ne tb 0 ne tb 1 |
| 107 | ret 514 |
| 108 | end |
| 109 | if eq comp 1 |
| 110 | ret 4 |
| 111 | end |
| 112 | ret 3 |
| 113 | end |
| 114 | |
| 115 | note op 0, part 1b (C l.202-211): default leading dimensions, spans, the C batch stride, extents |
| 116 | fn gx_valb x view ulong 48 p edit ulong 64 to ulong |
| 117 | r = get x 2 |
| 118 | c = get x 4 |
| 119 | if eq get x 0 1 |
| 120 | r = get x 4 |
| 121 | c = get x 2 |
| 122 | end |
| 123 | put p 2 get x 9 |
| 124 | if eq get x 9 0 |
| 125 | put p 2 c |
| 126 | end |
| 127 | put p 48 gx_span r c get p 2 |
| 128 | r = get x 4 |
| 129 | c = get x 3 |
| 130 | if eq get x 1 1 |
| 131 | r = get x 3 |
| 132 | c = get x 4 |
| 133 | end |
| 134 | put p 3 get x 11 |
| 135 | if eq get x 11 0 |
| 136 | put p 3 c |
| 137 | end |
| 138 | put p 49 gx_span r c get p 3 |
| 139 | put p 4 get x 13 |
| 140 | if eq get x 13 0 |
| 141 | put p 4 get x 3 |
| 142 | end |
| 143 | put p 50 gx_span get x 2 get x 3 get p 4 |
| 144 | if or eq get p 48 0 eq get p 49 0 |
| 145 | ret 1 |
| 146 | end |
| 147 | if eq get p 50 0 |
| 148 | ret 1 |
| 149 | end |
| 150 | if gt get p 1 1 |
| 151 | if lt get x 14 get p 50 |
| 152 | ret 1 |
| 153 | end |
| 154 | end |
| 155 | put p 44 gx_ext get p 48 get x 10 get p 1 |
| 156 | put p 45 gx_ext get p 49 get x 12 get p 1 |
| 157 | put p 46 gx_ext get p 50 get x 14 get p 1 |
| 158 | if or eq get p 44 0 eq get p 45 0 |
| 159 | ret 1 |
| 160 | end |
| 161 | if eq get p 46 0 |
| 162 | ret 1 |
| 163 | end |
| 164 | ret 0 |
| 165 | end |
| 166 | |
| 167 | note op 0, part 1 (C l.194-211): defaults, ranges, sizes, spans and extents |
| 168 | fn gx_val x view ulong 48 p edit ulong 64 to ulong |
| 169 | b = get x 5 |
| 170 | if eq b 0 |
| 171 | b = 1 |
| 172 | end |
| 173 | put p 1 b |
| 174 | if or gt get x 0 1 gt get x 1 1 |
| 175 | ret 1 |
| 176 | end |
| 177 | if or gt get x 6 4 gt get x 7 4 |
| 178 | ret 1 |
| 179 | end |
| 180 | if or gt get x 8 4 gt get x 17 2 |
| 181 | ret 1 |
| 182 | end |
| 183 | if or gt get x 18 2 eq get x 28 0 |
| 184 | ret 1 |
| 185 | end |
| 186 | m = get x 2 |
| 187 | n = get x 3 |
| 188 | k = get x 4 |
| 189 | if or or eq m 0 eq n 0 eq k 0 |
| 190 | ret 1 |
| 191 | end |
| 192 | if or gt m 2147483647 gt n 2147483647 |
| 193 | ret 2 |
| 194 | end |
| 195 | if or gt k 2147483647 gt b 2147483647 |
| 196 | ret 2 |
| 197 | end |
| 198 | if gt get x 8 2 |
| 199 | ret 3 |
| 200 | end |
| 201 | ret gx_valb view x edit p |
| 202 | end |
| 203 | |
| 204 | note op 0, part 2 (C l.212-226): operand pointers, overlap with C, the scale grid |
| 205 | fn gx_val2 x view ulong 48 p edit ulong 64 to ulong |
| 206 | put p 26 gx_es get x 6 |
| 207 | put p 27 gx_es get x 7 |
| 208 | put p 28 gx_es get x 8 |
| 209 | cb = mul get p 46 get p 28 |
| 210 | put p 10 0 |
| 211 | if ne bitcast f32 uint get x 15 bitcast f32 uint 0 |
| 212 | put p 10 1 |
| 213 | if or eq get x 26 0 eq get x 27 0 |
| 214 | ret 1 |
| 215 | end |
| 216 | if eq gx_ovl get x 28 cb get x 26 mul get p 44 get p 26 1 |
| 217 | ret 1 |
| 218 | end |
| 219 | if eq gx_ovl get x 28 cb get x 27 mul get p 45 get p 27 1 |
| 220 | ret 1 |
| 221 | end |
| 222 | end |
| 223 | sc = get x 18 |
| 224 | put p 12 0 |
| 225 | put p 13 0 |
| 226 | put p 14 0 |
| 227 | put p 15 0 |
| 228 | put p 16 0 |
| 229 | if ne sc 0 |
| 230 | if or eq get x 19 0 eq get x 20 0 |
| 231 | ret 1 |
| 232 | end |
| 233 | put p 15 1 |
| 234 | put p 16 1 |
| 235 | end |
| 236 | if eq sc 2 |
| 237 | if or or eq get x 23 0 eq get x 24 0 eq get x 25 0 |
| 238 | ret 1 |
| 239 | end |
| 240 | put p 12 div sub add get x 2 get x 23 1 get x 23 |
| 241 | put p 13 div sub add get x 4 get x 25 1 get x 25 |
| 242 | put p 14 div sub add get x 3 get x 24 1 get x 24 |
| 243 | put p 15 mul get p 12 get p 13 |
| 244 | put p 16 mul get p 13 get p 14 |
| 245 | end |
| 246 | put p 17 1 |
| 247 | if ne get x 21 0 |
| 248 | put p 17 get p 1 |
| 249 | end |
| 250 | put p 18 1 |
| 251 | if ne get x 22 0 |
| 252 | put p 18 get p 1 |
| 253 | end |
| 254 | ret 0 |
| 255 | end |
| 256 | |
| 257 | note op 0, part 4: the core k of one part, buffer sizes, the core esize and the row ops of A' and B' (row.om ops) |
| 258 | fn gx_val4 x view ulong 48 p edit ulong 64 to ulong |
| 259 | r = get p 6 |
| 260 | kc = get x 4 |
| 261 | if gt kc get p 8 |
| 262 | kc = get p 8 |
| 263 | end |
| 264 | kcore = kc |
| 265 | if eq r 3 |
| 266 | kcore = mul 9 mul shr add kc 15 4 16 |
| 267 | end |
| 268 | put p 23 kcore |
| 269 | put p 34 add mul get p 21 kcore 16 |
| 270 | put p 35 add mul get p 22 kcore 16 |
| 271 | ct = get p 7 |
| 272 | put p 25 2 |
| 273 | if eq ct 1 |
| 274 | put p 25 1 |
| 275 | end |
| 276 | if eq ct 3 |
| 277 | put p 25 4 |
| 278 | end |
| 279 | put p 32 0 |
| 280 | put p 33 2 |
| 281 | if eq r 5 |
| 282 | put p 30 0 |
| 283 | put p 31 0 |
| 284 | put p 32 1 |
| 285 | put p 33 0 |
| 286 | ret 0 |
| 287 | end |
| 288 | if eq r 3 |
| 289 | put p 30 5 |
| 290 | put p 31 6 |
| 291 | ret 0 |
| 292 | end |
| 293 | if eq r 4 |
| 294 | put p 30 4 |
| 295 | put p 31 4 |
| 296 | ret 0 |
| 297 | end |
| 298 | put p 30 2 |
| 299 | if eq ct 1 |
| 300 | put p 30 3 |
| 301 | end |
| 302 | if eq ct 2 |
| 303 | put p 30 1 |
| 304 | end |
| 305 | put p 31 get p 30 |
| 306 | ret 0 |
| 307 | end |
| 308 | |
| 309 | note op 0, part 3 (C l.227-240 and the plan of omega_gpu_gemm_ex l.351-370): route, k part size, tiles, row ops |
| 310 | fn gx_val3 x view ulong 48 p edit ulong 64 to ulong |
| 311 | r = gx_route get x 17 get x 6 get x 7 |
| 312 | put p 7 shr r 8 |
| 313 | r = and r 255 |
| 314 | put p 6 r |
| 315 | put p 8 16384 |
| 316 | if eq r 3 |
| 317 | put p 8 1808 |
| 318 | end |
| 319 | put p 5 get x 1 |
| 320 | if eq get x 29 2 |
| 321 | put p 5 0 |
| 322 | end |
| 323 | put p 9 0 |
| 324 | if and le get x 4 get p 8 ne get x 18 2 |
| 325 | put p 9 1 |
| 326 | end |
| 327 | bt = f64 bitcast f32 uint get x 16 |
| 328 | if eq get x 29 4 |
| 329 | bt = bitcast f64 ulong 0 |
| 330 | end |
| 331 | put p 11 bitcast ulong bt |
| 332 | put p 24 or get x 0 shl get p 5 1 |
| 333 | n = get x 2 |
| 334 | put p 20 n |
| 335 | if gt n 16384 |
| 336 | put p 20 16384 |
| 337 | end |
| 338 | put p 21 n |
| 339 | if gt n 4096 |
| 340 | put p 21 4096 |
| 341 | end |
| 342 | n = get x 3 |
| 343 | put p 22 n |
| 344 | if gt n 4096 |
| 345 | put p 22 4096 |
| 346 | end |
| 347 | put p 19 0 |
| 348 | if and eq get p 10 1 eq get p 9 1 |
| 349 | if and eq r 1 eq bt bitcast f64 ulong 0 |
| 350 | if and eq get x 8 0 eq get p 4 n |
| 351 | if le n 16384 |
| 352 | put p 19 gx_gspan get p 24 get p 20 n get x 4 get p 2 get p 3 |
| 353 | end |
| 354 | end |
| 355 | end |
| 356 | end |
| 357 | ret gx_val4 view x edit p |
| 358 | end |
| 359 | |
| 360 | note op 1 (C l.388, l.411-420, run_part l.264-270): one k part from k0 of the tile at (m0, n0), mc x nc |
| 361 | fn gx_kpart x view ulong 48 p edit ulong 64 to ulong |
| 362 | k0 = get x 30 |
| 363 | kc = sub get x 4 k0 |
| 364 | if gt kc get p 8 |
| 365 | kc = get p 8 |
| 366 | end |
| 367 | put p 38 0 |
| 368 | if eq get x 18 2 |
| 369 | bk = get x 25 |
| 370 | if gt kc sub bk rem k0 bk |
| 371 | kc = sub bk rem k0 bk |
| 372 | end |
| 373 | put p 38 div k0 bk |
| 374 | if eq get x 29 3 |
| 375 | put p 38 rem add get p 38 1 get p 13 |
| 376 | end |
| 377 | end |
| 378 | ku = kc |
| 379 | if and eq get x 29 1 eq add k0 kc get x 4 |
| 380 | ku = sub kc 1 |
| 381 | end |
| 382 | put p 36 kc |
| 383 | put p 37 ku |
| 384 | put p 47 ku |
| 385 | if eq get p 6 3 |
| 386 | put p 47 mul 9 mul shr add ku 15 4 16 |
| 387 | end |
| 388 | put p 39 0 |
| 389 | if and eq get p 6 1 eq get x 33 0 |
| 390 | put p 39 gx_gspan get p 24 get x 31 get x 32 ku get p 2 get p 3 |
| 391 | end |
| 392 | put p 40 add mul get x 34 get p 2 k0 |
| 393 | if eq get x 0 1 |
| 394 | put p 40 add mul k0 get p 2 get x 34 |
| 395 | end |
| 396 | put p 41 add mul k0 get p 3 get x 35 |
| 397 | if eq get p 5 1 |
| 398 | put p 41 add mul get x 35 get p 3 k0 |
| 399 | end |
| 400 | put p 40 mul get p 40 get p 26 |
| 401 | put p 41 mul get p 41 get p 26 |
| 402 | ret 0 |
| 403 | end |
| 404 | |
| 405 | note op 2 (C l.377-389): this batch's alpha (times the tensor scales) and whether C3 writes C directly |
| 406 | fn gx_batch x view ulong 48 p edit ulong 64 to ulong |
| 407 | al = f64 bitcast f32 uint get x 15 |
| 408 | if eq get x 18 1 |
| 409 | al = mul al mul f64 bitcast f32 uint get x 30 f64 bitcast f32 uint get x 31 |
| 410 | end |
| 411 | put p 42 bitcast ulong al |
| 412 | put p 43 0 |
| 413 | if and and eq get p 19 1 eq get x 32 0 eq al bitcast f64 ulong 4607182418800017408 |
| 414 | put p 43 1 |
| 415 | end |
| 416 | ret 0 |
| 417 | end |
| 418 | |
| 419 | note op 3 (C l.455-461): the fp8 block-row fallback's checks, ldy, k blocks and dequantized rows per slab |
| 420 | fn gx_fp8v x view ulong 48 p edit ulong 64 to ulong |
| 421 | n = get x 35 |
| 422 | k = get x 36 |
| 423 | bl = get x 38 |
| 424 | ly = get x 39 |
| 425 | put p 0 1 |
| 426 | if or eq get x 30 0 eq get x 31 0 |
| 427 | ret 1 |
| 428 | end |
| 429 | if eq get x 32 0 |
| 430 | ret 1 |
| 431 | end |
| 432 | if eq eq get x 33 0 eq get x 34 0 |
| 433 | ret 1 |
| 434 | end |
| 435 | if or or eq n 0 eq k 0 or eq get x 37 0 eq bl 0 |
| 436 | ret 1 |
| 437 | end |
| 438 | if and ne ly 0 lt ly n |
| 439 | ret 1 |
| 440 | end |
| 441 | if eq ly 0 |
| 442 | ly = n |
| 443 | end |
| 444 | put p 0 0 |
| 445 | put p 1 ly |
| 446 | put p 2 div sub add k bl 1 bl |
| 447 | rows = div 33554432 mul k 4 |
| 448 | if eq rows 0 |
| 449 | rows = 1 |
| 450 | end |
| 451 | if gt rows n |
| 452 | rows = n |
| 453 | end |
| 454 | put p 3 rows |
| 455 | put p 4 1 |
| 456 | if ne get x 33 0 |
| 457 | put p 4 0 |
| 458 | end |
| 459 | ret 0 |
| 460 | end |
| 461 | |
| 462 | note the entry: op 0 plans a call (validation through the row ops), 1 one k part, 2 one batch, 3 the fp8 rows checks |
| 463 | fn gemm_ex_plan op ulong x view ulong 48 p edit ulong 64 to ulong |
| 464 | if eq op 0 |
| 465 | r = gx_val view x edit p |
| 466 | if ne r 0 |
| 467 | ret r |
| 468 | end |
| 469 | r = gx_val2 view x edit p |
| 470 | if ne r 0 |
| 471 | ret r |
| 472 | end |
| 473 | ret gx_val3 view x edit p |
| 474 | end |
| 475 | if eq op 1 |
| 476 | ret gx_kpart view x edit p |
| 477 | end |
| 478 | if eq op 2 |
| 479 | ret gx_batch view x edit p |
| 480 | end |
| 481 | if eq op 3 |
| 482 | ret gx_fp8v view x edit p |
| 483 | end |
| 484 | ret 1 |
| 485 | end |