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/ggml/blob/master/src/ggml-cuda/mma.cuh # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / mma.cuh` 1606 lines ยท 70205 bytes Raw Blame History 1 #pragma once 2 // This file contains primitives that expose the tensor core PTX instructions for CUDA code. 3 // The primitives can be used in a similar way as the nvcuda::wmma interface but with a well-defined memory layout. 4 // The documentation for the PTX instructions can be found under: 5 // https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#matrix-multiply-accumulate-operation-using-mma-instruction 6 // 7 // Like with nvcuda::wmma there are three types of matrix tiles: A, B, and C with A @ B = C. 8 // A is a row-major matrix with shape M x K. 9 // B is a column-major matrix with shape K x N. 10 // C is a column-major matrix with shape M x N. 11 // A, B, and C are represented using the same fundamental data type: a row-major matrix with I rows and J columns. 12 // Note that J is measured in physical 32 bit elements instead of logical elements. 13 // The methods get_i and get_j can be used to get the physical 32 bit index of the lth element of a thread within a tile. 14 // All matrix tiles have ne physical 32 bit elements per warp. 15 // 16 // As described in the PTX documentation, all pointers for load_ldmatrix must be to shared memory and aligned to 16 bytes. 17 // The API in this file also assumes that the pointers for load_generic are aligned to 16 bytes, unaligned pointers are considered undefined behavior. 18 19 #include "common.cuh" 20 21 // On Volta each warp is doing 4 8x8 mma operations in parallel. 22 // The basic memory layout for a 32x8 output tile is to stack 4 input tiles in I direction and to mirror the B tile. 23 // However, the i indices in this file are by default permuted to simplify the index calculations. 24 // #define GGML_CUDA_MMA_NO_VOLTA_PERM 25 26 #if CUDART_VERSION >= 11080 27 28 static __device__ __forceinline__ int ggml_cuda_movmatrix(const int x) { 29 int ret = 0; 30 31 #ifdef TURING_MMA_AVAILABLE 32 asm("movmatrix.sync.aligned.m8n8.trans.b16 %0, %1;" 33 : "=r"(ret) : "r"(x)); 34 #else 35 GGML_UNUSED(x); 36 NO_DEVICE_CODE; 37 #endif // defined(TURING_MMA_AVAILABLE) 38 return ret; 39 } 40 41 #else 42 43 static __device__ __forceinline__ int ggml_cuda_movmatrix(const int x) { 44 // Imagine transposing row-major matrix to column-major matrix. 45 const int src_i_low = 2 * (threadIdx.x % 4); 46 const int src_i_high = src_i_low + 1; 47 const int src_j = threadIdx.x / 4; 48 49 const int src_laneid_low = src_i_low * 4 + src_j / 2; 50 const int src_laneid_high = src_i_high * 4 + src_j / 2; 51 52 const int shift_low = ((src_j + 0) % 2) * 16; 53 const int shift_high = ((src_j + 1) % 2) * 16; 54 55 const int ret_low = (__shfl_sync(0xFFFFFFFF, x, src_laneid_low, WARP_SIZE) >> shift_low) & 0x0000FFFF; 56 const int ret_high = (__shfl_sync(0xFFFFFFFF, x, src_laneid_high, WARP_SIZE) << shift_high) & 0xFFFF0000; 57 58 return ret_low | ret_high; 59 } 60 61 #endif // CUDART_VERSION >= 11080 62 63 static __device__ __forceinline__ half2 ggml_cuda_movmatrix(const half2 x) { 64 half2 ret; 65 *((int *) &ret) = ggml_cuda_movmatrix(*((const int *) &x)); 66 return ret; 67 } 68 69 namespace ggml_cuda_mma { 70 71 // Some architectures like Volta or CDNA3 perform multiple matrix multiplications per warp in parallel, 72 // effectively the warp is being split into subgroups of threads that each perform a single mma instruction. 73 // In those cases the data can be split in different ways across the warp. 74 enum data_layout { 75 // By default the data uses the I direction as its major dimension and the J direction as its minor dimension. 76 // For the A/C matrices this means I major == row major, J major == column major. 77 // For the B matrix this means I major == column major, J major == row major. 78 // MIRRORED == Each data value is held exactly once per thread subgroup. 79 DATA_LAYOUT_I_MAJOR = 0, // Always used for Turing, Ampere, Ada Lovelace, consumer Blackwell, matrix A&B for WMMA128 and CDNA. 80 DATA_LAYOUT_J_MAJOR = 10, // Matrix C for CDNA and WMMA128, int and float matrix C for WMMA256. 81 DATA_LAYOUT_I_MAJOR_MIRRORED = 20, // Volta, matrix A&B for RDNA3. 82 DATA_LAYOUT_J_MAJOR_MIRRORED = 30, 83 DATA_LAYOUT_I_MAJOR_SCRAMBLED = 40, // Scrambled matrix C for faster transposition (WMMA128/CDNA), convert to float to unscramble. 84 }; 85 // Implemented mma combinations are: 86 // - (I_MAJOR, I_MAJOR) -> I_MAJOR 87 // - (I_MAJOR, I_MAJOR_MIRRORED) -> I_MAJOR 88 // - (I_MAJOR, J_MAJOR_MIRRORED) -> I_MAJOR 89 90 static constexpr __device__ data_layout get_input_data_layout() { 91 #if defined(AMD_WMMA_256B_AVAILABLE) || defined(VOLTA_MMA_AVAILABLE) 92 return DATA_LAYOUT_I_MAJOR_MIRRORED; 93 #else 94 return DATA_LAYOUT_I_MAJOR; 95 #endif // defined(AMD_WMMA_256B_AVAILABLE) || defined(VOLTA_MMA_AVAILABLE) 96 } 97 98 template 99 struct tile {}; 100 101 template 102 struct tile { 103 static constexpr int I = I_; 104 static constexpr int J = J_; 105 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR; 106 107 #if defined(AMD_MFMA_AVAILABLE) 108 static constexpr int ne = I * J / 64; 109 T x[ne] = {0}; 110 111 static constexpr __device__ bool supported() { 112 if (I == 16 && J == 8) return true; 113 if (I == 32 && J == 4) return true; 114 if (I == 16 && J == 16) return true; 115 if (I == 32 && J == 32) return true; 116 return false; 117 } 118 119 static __device__ __forceinline__ int get_i(const int l) { 120 if constexpr (I == 16 && J == 4) { 121 return threadIdx.x % 16; 122 } else if constexpr (I == 16 && J == 8) { 123 return threadIdx.x % 16; 124 } else if constexpr (I == 32 && J == 4) { 125 return threadIdx.x % 32; 126 } else if constexpr (I == 16 && J == 16) { 127 return threadIdx.x % 16; 128 } else if constexpr (I == 32 && J == 32) { 129 return threadIdx.x % 32; 130 } else { 131 NO_DEVICE_CODE; 132 return -1; 133 } 134 } 135 136 static __device__ __forceinline__ int get_j(const int l) { 137 if constexpr (I == 16 && J == 4) { 138 return threadIdx.x / 16; 139 } else if constexpr (I == 16 && J == 8) { 140 return 2 * (threadIdx.x / 16) + l; 141 } else if constexpr (I == 32 && J == 4) { 142 return 2 * (threadIdx.x / 32) + l; 143 } else if constexpr (I == 16 && J == 16) { 144 return 4 * (threadIdx.x / 16) + l; 145 } else if constexpr (I == 32 && J == 32) { 146 return 4 * (threadIdx.x / 32) + 8 * (l / 4) + (l % 4); 147 } else { 148 NO_DEVICE_CODE; 149 return -1; 150 } 151 } 152 #elif defined(VOLTA_MMA_AVAILABLE) 153 static constexpr int ne = I * J / 32; 154 T x[ne] = {0}; 155 156 static constexpr __device__ bool supported() { 157 if (I == 32 && J == 8) return true; 158 return false; 159 } 160 161 static __device__ __forceinline__ int get_i(const int l) { 162 if constexpr (I == 32 && J == 8) { 163 #ifdef GGML_CUDA_MMA_NO_VOLTA_PERM 164 return (((threadIdx.x % 16) / 4) * 8) + ((threadIdx.x / 16) * 4) + (l & 2) + (threadIdx.x % 2); 165 #else 166 return (l & 2) + (threadIdx.x & ~2); 167 #endif // GGML_CUDA_MMA_NO_VOLTA_PERM 168 } else { 169 NO_DEVICE_CODE; 170 return -1; 171 } 172 } 173 174 static __device__ __forceinline__ int get_j(const int l) { 175 if constexpr (I == 32 && J == 8) { 176 return (threadIdx.x & 2) + (l & (4 + 1)); 177 } else { 178 NO_DEVICE_CODE; 179 return -1; 180 } 181 } 182 #elif defined(AMD_WMMA_AVAILABLE) 183 static constexpr int ne = I * J / 32; 184 T x[ne] = {0}; 185 186 static constexpr __device__ bool supported() { 187 if (I == 16 && J == 16) return true; 188 if (I == 16 && J == 8) return true; 189 if (I == 16 && J == 4) return true; 190 return false; 191 } 192 193 static __device__ __forceinline__ int get_i(const int l) { 194 if constexpr (supported()) { 195 return threadIdx.x % 16; 196 } else { 197 NO_DEVICE_CODE; 198 return -1; 199 } 200 } 201 202 static __device__ __forceinline__ int get_j(const int l) { 203 if constexpr (I == 16 && J == 16) { 204 #if defined(AMD_WMMA_256B_AVAILABLE) 205 if constexpr (std::is_same_v || std::is_same_v) { 206 // matrix C 207 return 2 * l + (threadIdx.x / 16); 208 } else { 209 // matrix A&B 210 return l; 211 } 212 #else 213 // matrix C is the transposed matrix A&B on WMMA128 214 return ne * (threadIdx.x / 16) + l; 215 #endif // defined(AMD_WMMA_256B_AVAILABLE) 216 } else if constexpr (I == 16 && J == 8) { 217 // mmq input for WMMA128 218 return ne * (threadIdx.x / 16) + l; 219 } else if constexpr (I == 16 && J == 4) { 220 return ne * (threadIdx.x / 16) + l; 221 } else { 222 NO_DEVICE_CODE; 223 return -1; 224 } 225 } 226 #else 227 static constexpr int ne = I * J / 32; 228 T x[ne] = {0}; 229 230 static constexpr __device__ bool supported() { 231 if (I == 8 && J == 4) return true; 232 if (I == 8 && J == 8) return true; 233 if (I == 16 && J == 8) return true; 234 if (I == 16 && J == 16) return true; 235 if (I == 32 && J == 8) return true; 236 return false; 237 } 238 239 static __device__ __forceinline__ int get_i(const int l) { 240 if constexpr (I == 8 && J == 4) { 241 return threadIdx.x / 4; 242 } else if constexpr (I == 8 && J == 8) { 243 return threadIdx.x / 4; 244 } else if constexpr (I == 16 && J == 8) { 245 return ((l / 2) * 8) + (threadIdx.x / 4); 246 } else if constexpr (I == 16 && J == 16) { 247 return (((l / 2) % 2) * 8) + (threadIdx.x / 4); 248 } else if constexpr (I == 32 && J == 8) { 249 return tile<16, 8, T>::get_i(l); // Memory layout simply repeated with same pattern in i direction. 250 } else { 251 NO_DEVICE_CODE; 252 return -1; 253 } 254 } 255 256 static __device__ __forceinline__ int get_j(const int l) { 257 if constexpr (I == 8 && J == 4) { 258 return threadIdx.x % 4; 259 } else if constexpr (I == 8 && J == 8) { 260 return (l * 4) + (threadIdx.x % 4); 261 } else if constexpr (I == 16 && J == 8) { 262 return ((threadIdx.x % 4) * 2) + (l % 2); 263 } else if constexpr (I == 16 && J == 16) { 264 return ((l / 4) * 8) + ((threadIdx.x % 4) * 2) + (l % 2); 265 } else if constexpr (I == 32 && J == 8) { 266 return tile<16, 8, T>::get_j(l); // Memory layout simply repeated with same pattern in i direction. 267 } else { 268 NO_DEVICE_CODE; 269 return -1; 270 } 271 } 272 #endif // defined(GGML_USE_HIP) 273 }; 274 275 template 276 struct tile { 277 static constexpr int I = I_; 278 static constexpr int J = J_; 279 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR; 280 281 #if defined(VOLTA_MMA_AVAILABLE) 282 static constexpr int ne = I * J / WARP_SIZE; 283 half2 x[ne] = {{0.0f, 0.0f}}; 284 285 static constexpr __device__ bool supported() { 286 if (I == 32 && J == 4) return true; 287 return false; 288 } 289 290 static __device__ __forceinline__ int get_i(const int l) { 291 if constexpr (I == 32 && J == 4) { 292 #ifdef GGML_CUDA_MMA_NO_VOLTA_PERM 293 return (((threadIdx.x % 16) / 4) * 8) + ((threadIdx.x / 16) * 4) + (threadIdx.x % 4); 294 #else 295 return threadIdx.x; 296 #endif // GGML_CUDA_MMA_NO_VOLTA_PERM 297 } else { 298 NO_DEVICE_CODE; 299 return -1; 300 } 301 } 302 303 static __device__ __forceinline__ int get_j(const int l) { 304 if constexpr (I == 32 && J == 4) { 305 return l; 306 } else { 307 NO_DEVICE_CODE; 308 return -1; 309 } 310 } 311 #elif defined(AMD_WMMA_AVAILABLE) 312 static constexpr int ne = I * J / 32; 313 half2 x[ne] = {{0.0f, 0.0f}}; 314 315 static constexpr __device__ bool supported() { 316 if (I == 16 && J == 8) return true; 317 if (I == 16 && J == 16) return true; 318 if (I == 32 && J == 8) return true; 319 return false; 320 } 321 322 static __device__ __forceinline__ int get_i(const int l) { 323 if constexpr (I == 16 && J == 8) { 324 return threadIdx.x % 16; 325 } else if constexpr (I == 16 && J == 16) { 326 return threadIdx.x % 16; 327 } else if constexpr (I == 32 && J == 8) { 328 return (threadIdx.x % 16) * 2 + l / (ne/2); 329 } else { 330 NO_DEVICE_CODE; 331 return -1; 332 } 333 } 334 335 static __device__ __forceinline__ int get_j(const int l) { 336 if constexpr (I == 16 && J == 8) { 337 return (threadIdx.x / 16) * ne + l; 338 } else if constexpr (I == 16 && J == 16) { 339 #ifdef RDNA3 340 return l*2 + (threadIdx.x / 16); 341 #else 342 return (threadIdx.x / 16) * ne + l; 343 #endif // RDNA3 344 } else if constexpr (I == 32 && J == 8) { 345 return (threadIdx.x / 16) * (ne/2) + l % (ne/2); 346 } else { 347 NO_DEVICE_CODE; 348 return -1; 349 } 350 } 351 #elif defined(AMD_MFMA_AVAILABLE) 352 static constexpr int ne = I * J / 64; 353 half2 x[ne] = {{0.0f, 0.0f}}; 354 355 static constexpr __device__ bool supported() { 356 if (I == 16 && J == 8) return true; 357 if (I == 16 && J == 16) return true; 358 if (I == 32 && J == 8) return true; 359 return false; 360 } 361 362 static __device__ __forceinline__ int get_i(const int l) { 363 if constexpr (I == 16 && J == 8) { 364 return threadIdx.x % 16; 365 } else if constexpr (I == 16 && J == 16) { 366 return threadIdx.x % 16; 367 } else if constexpr (I == 32 && J == 8) { 368 return (threadIdx.x % 16) * 2 + l / (ne/2); 369 } else { 370 NO_DEVICE_CODE; 371 return -1; 372 } 373 } 374 375 static __device__ __forceinline__ int get_j(const int l) { 376 if constexpr (I == 16 && J == 8) { 377 return (threadIdx.x / 16) * ne + l; 378 } else if constexpr (I == 16 && J == 16) { 379 return (threadIdx.x / 16) * ne + l; 380 } else if constexpr (I == 32 && J == 8) { 381 return (threadIdx.x / 16) * (ne/2) + l % (ne/2); 382 } else { 383 NO_DEVICE_CODE; 384 return -1; 385 } 386 } 387 #else 388 static constexpr int ne = I * J / WARP_SIZE; 389 half2 x[ne] = {{0.0f, 0.0f}}; 390 391 static constexpr __device__ bool supported() { 392 if (I == 8 && J == 4) return true; 393 if (I == 8 && J == 8) return true; 394 if (I == 16 && J == 8) return true; 395 if (I == 16 && J == 16) return true; 396 if (I == 32 && J == 8) return true; 397 return false; 398 } 399 400 static __device__ __forceinline__ int get_i(const int l) { 401 if constexpr (I == 8 && J == 8) { 402 return threadIdx.x / 4; 403 } else if constexpr (I == 16 && J == 4) { 404 return (l * 8) + (threadIdx.x / 4); 405 } else if constexpr (I == 16 && J == 8) { 406 return ((l % 2) * 8) + (threadIdx.x / 4); 407 } else if constexpr (I == 32 && J == 8) { 408 return ((l / 4) * 16) + ((l % 2) * 8) + (threadIdx.x / 4); 409 } else { 410 NO_DEVICE_CODE; 411 return -1; 412 } 413 } 414 415 static __device__ __forceinline__ int get_j(const int l) { 416 if constexpr (I == 8 && J == 8) { 417 return (l * 4) + (threadIdx.x % 4); 418 } else if constexpr (I == 16 && J == 4) { 419 return threadIdx.x % 4; 420 } else if constexpr (I == 16 && J == 8) { 421 return ((l / 2) * 4) + (threadIdx.x % 4); 422 } else if constexpr (I == 32 && J == 8) { 423 return ((l & 2) * 2) + (threadIdx.x % 4); 424 } else { 425 NO_DEVICE_CODE; 426 return -1; 427 } 428 } 429 #endif // defined(VOLTA_MMA_AVAILABLE) 430 }; 431 432 template 433 struct tile { 434 static constexpr int I = I_; 435 static constexpr int J = J_; 436 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR; 437 438 #if defined(AMD_WMMA_AVAILABLE) 439 static constexpr int ne = tile::ne; 440 nv_bfloat162 x[ne] = {{0.0f, 0.0f}}; 441 442 static constexpr __device__ bool supported() { 443 return tile::supported(); 444 } 445 446 static __device__ __forceinline__ int get_i(const int l) { 447 return tile::get_i(l); 448 } 449 450 static __device__ __forceinline__ int get_j(const int l) { 451 return tile::get_j(l); 452 } 453 #elif defined(AMD_MFMA_AVAILABLE) 454 static constexpr int ne = tile::ne; 455 nv_bfloat162 x[ne] = {{0.0f, 0.0f}}; 456 457 static constexpr __device__ bool supported() { 458 return tile::supported(); 459 } 460 461 static __device__ __forceinline__ int get_i(const int l) { 462 return tile::get_i(l); 463 } 464 465 static __device__ __forceinline__ int get_j(const int l) { 466 return tile::get_j(l); 467 } 468 #else 469 static constexpr int ne = I * J / WARP_SIZE; 470 nv_bfloat162 x[ne] = {{0.0f, 0.0f}}; 471 472 static constexpr __device__ bool supported() { 473 if (I == 8 && J == 8) return true; 474 if (I == 16 && J == 4) return true; 475 if (I == 16 && J == 8) return true; 476 return false; 477 } 478 479 static __device__ __forceinline__ int get_i(const int l) { 480 if constexpr (I == 8 && J == 8) { 481 return threadIdx.x / 4; 482 } else if constexpr (I == 16 && J == 4) { 483 return (l * 8) + (threadIdx.x / 4); 484 } else if constexpr (I == 16 && J == 8) { 485 return ((l % 2) * 8) + (threadIdx.x / 4); 486 } else { 487 NO_DEVICE_CODE; 488 return -1; 489 } 490 } 491 492 static __device__ __forceinline__ int get_j(const int l) { 493 if constexpr (I == 8 && J == 8) { 494 return (l * 4) + (threadIdx.x % 4); 495 } else if constexpr (I == 16 && J == 4) { 496 return threadIdx.x % 4; 497 } else if constexpr (I == 16 && J == 8) { 498 return ((l / 2) * 4) + (threadIdx.x % 4); 499 } else { 500 NO_DEVICE_CODE; 501 return -1; 502 } 503 } 504 #endif // defined(AMD_WMMA_AVAILABLE) 505 }; 506 507 template 508 struct tile { 509 static constexpr int I = I_; 510 static constexpr int J = J_; 511 static constexpr data_layout dl = DATA_LAYOUT_J_MAJOR; 512 513 static constexpr int ne = tile::ne; 514 T x[ne] = {0}; 515 516 static constexpr __device__ bool supported() { 517 return tile::supported(); 518 } 519 520 static __device__ __forceinline__ int get_i(const int l) { 521 return tile::get_j(l); 522 } 523 524 static __device__ __forceinline__ int get_j(const int l) { 525 return tile::get_i(l); 526 } 527 }; 528 529 template 530 struct tile { 531 static constexpr int I = I_; 532 static constexpr int J = J_; 533 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED; 534 535 // RDNA3 536 static constexpr int ne = I * J / 32 * 2; 537 538 T x[ne] = {0}; 539 540 static constexpr __device__ bool supported() { 541 if (I == 16 && J == 16) return true; 542 if (I == 16 && J == 8) return true; 543 if (I == 16 && J == 4) return true; 544 if (I == 32 && J == 8) return true; 545 return false; 546 } 547 548 static __device__ __forceinline__ int get_i(const int l) { 549 if constexpr (I == 16) { 550 return threadIdx.x % 16; 551 } else if constexpr (I == 32) { 552 return (threadIdx.x % 16) * 2 + l / (ne/2); 553 } else { 554 NO_DEVICE_CODE; 555 return -1; 556 } 557 } 558 559 static __device__ __forceinline__ int get_j(const int l) { 560 if constexpr (I == 16) { 561 return l; 562 } else if constexpr (I == 32) { 563 return l % (ne/2); 564 } else { 565 NO_DEVICE_CODE; 566 return -1; 567 } 568 } 569 }; 570 571 template 572 struct tile { 573 static constexpr int I = I_; 574 static constexpr int J = J_; 575 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED; 576 #if defined(AMD_WMMA_256B_AVAILABLE) 577 static constexpr int ne = tile::ne; 578 579 half2 x[ne] = {{0.0f, 0.0f}}; 580 581 static constexpr __device__ bool supported() { 582 return tile::supported(); 583 } 584 585 static __device__ __forceinline__ int get_i(const int l) { 586 return tile::get_i(l); 587 } 588 589 static __device__ __forceinline__ int get_j(const int l) { 590 return tile::get_j(l); 591 } 592 #else // Volta 593 static constexpr int ne = I * J / (WARP_SIZE/4); 594 595 half2 x[ne] = {{0.0f, 0.0f}}; 596 597 static constexpr __device__ bool supported() { 598 if (I == 8 && J == 4) return true; 599 return false; 600 } 601 602 static __device__ __forceinline__ int get_i(const int /*l*/) { 603 if constexpr (I == 8 && J == 4) { 604 return ((threadIdx.x / 16) * 4) + (threadIdx.x % 4); 605 } else { 606 NO_DEVICE_CODE; 607 return -1; 608 } 609 } 610 611 static __device__ __forceinline__ int get_j(const int l) { 612 if constexpr (I == 8 && J == 4) { 613 return l; 614 } else { 615 NO_DEVICE_CODE; 616 return -1; 617 } 618 } 619 #endif // defined(AMD_WMMA_256B_AVAILABLE) 620 }; 621 622 template 623 struct tile { 624 static constexpr int I = I_; 625 static constexpr int J = J_; 626 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED; 627 static constexpr int ne = tile::ne; 628 629 nv_bfloat162 x[ne] = {{0.0f, 0.0f}}; 630 631 static constexpr __device__ bool supported() { 632 return tile::supported(); 633 } 634 635 static __device__ __forceinline__ int get_i(const int l) { 636 return tile::get_i(l); 637 } 638 639 static __device__ __forceinline__ int get_j(const int l) { 640 return tile::get_j(l); 641 } 642 }; 643 644 template 645 struct tile { 646 static constexpr int I = I_; 647 static constexpr int J = J_; 648 static constexpr data_layout dl = DATA_LAYOUT_J_MAJOR_MIRRORED; 649 static constexpr int ne = I * J / (WARP_SIZE/4); 650 651 half2 x[ne] = {{0.0f, 0.0f}}; 652 653 static constexpr __device__ bool supported() { 654 if (I == 8 && J == 4) return true; 655 return false; 656 } 657 658 static __device__ __forceinline__ int get_i(const int l) { 659 if constexpr (I == 8 && J == 4) { 660 return ((l / 2) * 4) + (threadIdx.x % 4); 661 } else { 662 NO_DEVICE_CODE; 663 return -1; 664 } 665 } 666 667 static __device__ __forceinline__ int get_j(const int l) { 668 if constexpr (I == 8 && J == 4) { 669 return ((threadIdx.x / 16) * 2) + (l % 2); 670 } else { 671 NO_DEVICE_CODE; 672 return -1; 673 } 674 } 675 }; 676 677 template 678 struct tile { 679 static constexpr int I = I_; 680 static constexpr int J = J_; 681 static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_SCRAMBLED; 682 683 static constexpr int ne = I * J / ggml_cuda_get_physical_warp_size(); 684 half2 x[ne] = {{0.0f, 0.0f}}; 685 686 static constexpr __device__ bool supported() { 687 if (I == 16 && J == 16) return true; 688 return false; 689 } 690 691 static __device__ __forceinline__ int get_i(const int l) { 692 return tile::get_i(l); 693 } 694 }; 695 696 static __device__ __forceinline__ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> unscramble(const tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & t) { 697 #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE) 698 tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> ret; 699 #pragma unroll 700 for (int l0 = 0; l0 < t.ne/2; ++l0) { 701 ret.x[2*l0 + 0] = __lows2half2(t.x[l0], t.x[l0 + t.ne/2]); 702 ret.x[2*l0 + 1] = __highs2half2(t.x[l0], t.x[l0 + t.ne/2]); 703 } 704 return ret; 705 #else 706 NO_DEVICE_CODE; 707 GGML_UNUSED(t); 708 #endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE) 709 } 710 711 #if defined(TURING_MMA_AVAILABLE) 712 template 713 static __device__ __forceinline__ tile get_half2(const tile & tile_float) { 714 tile ret; 715 #pragma unroll 716 for (int l0 = 0; l0 < tile_float.ne; l0 += 2) { 717 ret.x[l0/2] = make_half2(tile_float.x[l0 + 0], tile_float.x[l0 + 1]); 718 } 719 return ret; 720 } 721 722 static __device__ __forceinline__ tile<8, 8, half2> get_transposed(const tile<16, 4, half2> & t) { 723 tile<8, 8, half2> ret; 724 ret.x[0] = ggml_cuda_movmatrix(t.x[0]); 725 ret.x[1] = ggml_cuda_movmatrix(t.x[1]); 726 727 return ret; 728 } 729 #elif defined(AMD_WMMA_256B_AVAILABLE) 730 static __device__ __forceinline__ tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> get_half2( 731 const tile<16, 16, float, DATA_LAYOUT_I_MAJOR> & tile_float) { 732 tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> ret; 733 #pragma unroll 734 for (int l = 0; l < tile_float.ne; ++l) { 735 float tmp[2]; 736 int i = threadIdx.x / 16; 737 tmp[i] = tile_float.x[l]; 738 i ^= 1; 739 tmp[i] = __shfl_xor_sync(0xFFFFFFFF, tile_float.x[l], 16, WARP_SIZE); 740 ret.x[l] = make_half2(tmp[0], tmp[1]); 741 } 742 return ret; 743 } 744 #elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE) 745 template 746 static __device__ __forceinline__ tile get_half2(const tile & tile_float) { 747 tile ret; 748 #pragma unroll 749 for (int l0 = 0; l0 < tile_float.ne; l0 += 2) { 750 ret.x[l0/2] = make_half2(tile_float.x[l0 + 0], tile_float.x[l0 + 1]); 751 } 752 return ret; 753 } 754 755 static __device__ __forceinline__ tile<8, 8, half2> get_transposed(const tile<16, 4, half2> & t) { 756 NO_DEVICE_CODE; 757 return tile<8, 8, half2>{}; 758 } 759 #else // Volta 760 template 761 static __device__ __forceinline__ tile get_half2(const tile & tile_float) { 762 tile ret; 763 #pragma unroll 764 for (int l0 = 0; l0 < tile_float.ne; l0 += 4) { 765 ret.x[l0/2 + 0] = make_half2(tile_float.x[l0 + 0], tile_float.x[l0 + 1]); 766 ret.x[l0/2 + 1] = make_half2(tile_float.x[l0 + 2], tile_float.x[l0 + 3]); 767 768 // On Volta FP16 and FP32 tiles have a different memory layout, 769 // for the conversion threads with an offset of 2 need to exchange half their values: 770 ret.x[l0/2 + (((threadIdx.x % 4) / 2) ^ 1)] = __shfl_xor_sync( 771 0xFFFFFFFF, ret.x[l0/2 + (((threadIdx.x % 4) / 2) ^ 1)], 2, WARP_SIZE); 772 } 773 return ret; 774 } 775 #endif // defined(TURING_MMA_AVAILABLE) 776 777 template 778 static __device__ __forceinline__ void load_generic(tile & t, const T * __restrict__ xs0, const int stride) { 779 #pragma unroll 780 for (int l = 0; l < t.ne; ++l) { 781 t.x[l] = xs0[t.get_i(l)*stride + t.get_j(l)]; 782 } 783 } 784 785 template 786 static __device__ __forceinline__ uint32_t swizzle(const uint32_t offset, const uint32_t i) { 787 static_assert(sizeof(T) <= 4, "unsupported type size"); 788 constexpr int stride_bytes = stride*sizeof(T); 789 static_assert(stride_bytes % 16 == 0, "bad stride"); 790 constexpr uint32_t shift = sizeof(T) == 1 ? 4 : (sizeof(T) == 2 ? 3 : 2); 791 if (stride_bytes % 32 != 0) { 792 return offset; // Equivalent to padding with 16 bytes. 793 } 794 if (stride_bytes % 64 != 0) { 795 return offset ^ (((i / 4) % 2) << shift); 796 } 797 if (stride_bytes % 128 != 0) { 798 return offset ^ (((i / 2) % 4) << shift); 799 } 800 return offset ^ ((i % 8) << shift); 801 } 802 803 template 804 static __device__ __forceinline__ T * swizzle(T * ptr, const uint32_t offset, const uint32_t i) { 805 return ptr + swizzle(offset, i); 806 } 807 808 template 809 static __device__ __forceinline__ void load_ldmatrix( 810 tile<8, 8, T> & t, const T * __restrict__ xs0, const int stride) { 811 #ifdef TURING_MMA_AVAILABLE 812 int * xi = (int *) t.x; 813 const int * xs = (const int *) xs0 + (threadIdx.x % t.I) * stride + ((threadIdx.x / t.I) * (t.J / 2)) % t.J; 814 asm volatile("ldmatrix.sync.aligned.m8n8.x2.b16 {%0, %1}, [%2];" 815 : "=r"(xi[0]), "=r"(xi[1]) 816 : "l"(xs)); 817 #else 818 GGML_UNUSED_VARS(t, xs0, stride); 819 NO_DEVICE_CODE; 820 #endif // TURING_MMA_AVAILABLE 821 } 822 823 template 824 static __device__ __forceinline__ void load_ldmatrix( 825 tile<16, 4, T, dl> & t, const T * __restrict__ xs0, const int stride) { 826 #ifdef TURING_MMA_AVAILABLE 827 int * xi = (int *) t.x; 828 const int * xs = (const int *) xs0 + (threadIdx.x % t.I) * stride; 829 asm volatile("ldmatrix.sync.aligned.m8n8.x2.b16 {%0, %1}, [%2];" 830 : "=r"(xi[0]), "=r"(xi[1]) 831 : "l"(xs)); 832 #elif defined(AMD_WMMA_AVAILABLE) 833 #ifdef RDNA3 834 static_assert(dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout"); 835 static_assert(sizeof(t.x) == 16, "bad ne"); 836 ggml_cuda_memcpy_1<8>(t.x + 0, xs0 + t.get_i(0)*stride + 0); 837 ggml_cuda_memcpy_1<8>(t.x + 2, xs0 + t.get_i(0)*stride + 2); 838 #else 839 static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout"); 840 static_assert(sizeof(t.x) == 8, "bad ne"); 841 ggml_cuda_memcpy_1<8>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0)); 842 #endif // RDNA3 843 #elif defined(AMD_MFMA_AVAILABLE) 844 static_assert(sizeof(t.x) == 4, "bad ne"); 845 ggml_cuda_memcpy_1<4>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0)); 846 #else 847 GGML_UNUSED_VARS(t, xs0, stride); 848 NO_DEVICE_CODE; 849 #endif // TURING_MMA_AVAILABLE 850 } 851 852 template 853 static __device__ __forceinline__ void load_ldmatrix( 854 tile<16, 8, T, dl> & t, const T * __restrict__ xs0, const int stride) { 855 #if defined(TURING_MMA_AVAILABLE) 856 int * xi = (int * ) t.x; 857 const int * xs = (const int *) xs0 + (threadIdx.x % t.I) * stride + (threadIdx.x / t.I) * (t.J / 2); 858 asm volatile("ldmatrix.sync.aligned.m8n8.x4.b16 {%0, %1, %2, %3}, [%4];" 859 : "=r"(xi[0]), "=r"(xi[1]), "=r"(xi[2]), "=r"(xi[3]) 860 : "l"(xs)); 861 #elif defined(VOLTA_MMA_AVAILABLE) 862 ggml_cuda_memcpy_1<4*sizeof(T)>(t.x + 0, xs0 + t.get_i(0)*stride + 0); 863 ggml_cuda_memcpy_1<4*sizeof(T)>(t.x + 4, xs0 + t.get_i(4)*stride + 4); 864 #elif defined(AMD_WMMA_AVAILABLE) 865 #ifdef RDNA3 866 static_assert(dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout"); 867 static_assert(sizeof(t.x) == 32, "bad ne"); 868 ggml_cuda_memcpy_1<16>(t.x + 0, xs0 + t.get_i(0)*stride + 0); 869 ggml_cuda_memcpy_1<16>(t.x + 4, xs0 + t.get_i(0)*stride + 4); 870 #else 871 static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout"); 872 static_assert(sizeof(t.x) == 16, "bad ne"); 873 ggml_cuda_memcpy_1<16>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0)); 874 #endif // RDNA3 875 #elif defined(AMD_MFMA_AVAILABLE) 876 static_assert(sizeof(t.x) == 8, "bad ne"); 877 ggml_cuda_memcpy_1<8>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0)); 878 #else 879 GGML_UNUSED_VARS(t, xs0, stride); 880 NO_DEVICE_CODE; 881 #endif // TURING_MMA_AVAILABLE 882 } 883 884 static __device__ __forceinline__ void load_ldmatrix( 885 tile<8, 4, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & t, const half2 * __restrict__ xs0, const int stride) { 886 ggml_cuda_memcpy_1<4*sizeof(half2)>(t.x, xs0 + t.get_i(0)*stride); 887 } 888 889 static __device__ __forceinline__ void load_ldmatrix( 890 tile<8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> & t, const half2 * __restrict__ xs0, const int stride) { 891 #ifdef VOLTA_MMA_AVAILABLE 892 #pragma unroll 893 for (int l0 = 0; l0 < t.ne; l0 += 2) { 894 ggml_cuda_memcpy_1<2*sizeof(half2)>(t.x + l0, xs0 + t.get_i(l0)*stride + t.get_j(l0)); 895 } 896 #else 897 GGML_UNUSED_VARS(t, xs0, stride); 898 NO_DEVICE_CODE; 899 #endif // VOLTA_MMA_AVAILABLE 900 } 901 902 static __device__ __forceinline__ void load_ldmatrix( 903 tile<32, 4, half2> & t, const half2 * __restrict__ xs0, const int stride) { 904 #if defined(VOLTA_MMA_AVAILABLE) 905 ggml_cuda_memcpy_1<4*sizeof(half2)>(t.x, xs0 + t.get_i(0)*stride); 906 #else 907 GGML_UNUSED_VARS(t, xs0, stride); 908 NO_DEVICE_CODE; 909 #endif // defined(VOLTA_MMA_AVAILABLE) 910 } 911 912 template 913 static __device__ __forceinline__ void load_ldmatrix_trans( 914 tile & t, const T * __restrict__ xs0, const int stride) { 915 #ifdef TURING_MMA_AVAILABLE 916 static_assert(I == 16, "bad tile width"); 917 static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout"); 918 int * xi = (int *) t.x; 919 const int * xs = (const int *) xs0 + (threadIdx.x % t.I) * stride + (threadIdx.x / t.I) * (t.J / 2); 920 asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.b16 {%0, %1, %2, %3}, [%4];" 921 : "=r"(xi[0]), "=r"(xi[2]), "=r"(xi[1]), "=r"(xi[3]) 922 : "l"(xs)); 923 #elif defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 924 static_assert(dl == DATA_LAYOUT_I_MAJOR || dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout"); 925 if constexpr (I == 32) { 926 #pragma unroll 927 for (int l0 = 0; l0 < t.ne/2; ++l0) { 928 const half2 tmp0 = xs0[(2*t.get_j(l0) + 0)*stride + t.get_i(l0)/2]; 929 const half2 tmp1 = xs0[(2*t.get_j(l0) + 1)*stride + t.get_i(l0)/2]; 930 931 t.x[l0] = __lows2half2(tmp0, tmp1); 932 t.x[l0 + t.ne/2] = __highs2half2(tmp0, tmp1); 933 } 934 } else { 935 half * xh = (half *) t.x; 936 #pragma unroll 937 for (int l = 0; l < t.ne; ++l) { 938 xh[2*l + 0] = ((const half *) xs0)[(2*t.get_j(l) + 0)*(2*stride) + t.get_i(l)]; 939 xh[2*l + 1] = ((const half *) xs0)[(2*t.get_j(l) + 1)*(2*stride) + t.get_i(l)]; 940 } 941 } 942 #else 943 GGML_UNUSED_VARS(t, xs0, stride); 944 NO_DEVICE_CODE; 945 #endif // TURING_MMA_AVAILABLE 946 } 947 948 template 949 static __device__ __forceinline__ void load_ldmatrix_swizzled( 950 tile & t, const T * __restrict__ xs0, const int offset) { 951 #if defined(TURING_MMA_AVAILABLE) 952 static_assert(I == 8 || I == 16, "bad tile width"); 953 static_assert(J == 8, "bad tile height"); 954 const int i = threadIdx.x % t.I; 955 const int j = ((threadIdx.x / t.I) * (t.J / 2)) % t.J; 956 int offset_ij = offset + i * stride + j; 957 offset_ij = swizzle(offset_ij, i); 958 int * xi = (int *) t.x; 959 if constexpr (I == 16) { 960 asm volatile("ldmatrix.sync.aligned.m8n8.x4.b16 {%0, %1, %2, %3}, [%4];" 961 : "=r"(xi[0]), "=r"(xi[1]), "=r"(xi[2]), "=r"(xi[3]) 962 : "l"(xs0 + offset_ij)); 963 } else { 964 asm volatile("ldmatrix.sync.aligned.m8n8.x2.b16 {%0, %1}, [%2];" 965 : "=r"(xi[0]), "=r"(xi[1]) 966 : "l"(xs0 + offset_ij)); 967 } 968 #elif defined(VOLTA_MMA_AVAILABLE) 969 #pragma unroll 970 for (int o = 0; o < t.ne; o += 4) { 971 const int offset_ij = offset + t.get_i(o) * stride + o; 972 ggml_cuda_memcpy_1<4*sizeof(T)>(t.x + o, swizzle(xs0, offset_ij, t.get_i(o))); 973 } 974 #elif defined(AMD_WMMA_AVAILABLE) 975 #ifdef RDNA3 976 static_assert(dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout"); 977 static_assert(sizeof(t.x) == 32, "bad ne"); 978 static_assert(I == 16, "bad tile width"); 979 static_assert(J == 8, "bad tile height"); 980 #pragma unroll 981 for (int o = 0; o < 8; o += 4) { 982 const int offset_ij = offset + t.get_i(0) * stride + o; 983 ggml_cuda_memcpy_1<16>(t.x + o, swizzle(xs0, offset_ij, t.get_i(0))); 984 } 985 #else 986 static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout"); 987 static_assert(sizeof(t.x) == 16, "bad ne"); 988 const int offset_ij = offset + t.get_i(0)*stride + t.get_j(0); 989 ggml_cuda_memcpy_1<16>(t.x, swizzle(xs0, offset_ij, t.get_i(0))); 990 #endif // RDNA3 991 #elif defined(AMD_MFMA_AVAILABLE) 992 static_assert(sizeof(t.x) == 8, "bad ne"); 993 const int offset_ij = offset + t.get_i(0)*stride + t.get_j(0); 994 ggml_cuda_memcpy_1<8>(t.x, swizzle(xs0, offset_ij, t.get_i(0))); 995 #else 996 GGML_UNUSED_VARS(t, xs0, offset); 997 NO_DEVICE_CODE; 998 #endif // defined(TURING_MMA_AVAILABLE) 999 } 1000 1001 template 1002 static __device__ __forceinline__ void load_ldmatrix_swizzled( 1003 tile<8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> & t, const half2 * __restrict__ xs0, const int offset) { 1004 #ifdef VOLTA_MMA_AVAILABLE 1005 #pragma unroll 1006 for (int l0 = 0; l0 < t.ne; l0 += 2) { 1007 const int offset_ij = offset + t.get_i(l0)*stride + t.get_j(l0); 1008 ggml_cuda_memcpy_1<2*sizeof(half2)>(t.x + l0, swizzle(xs0, offset_ij, t.get_i(l0))); 1009 } 1010 #else 1011 GGML_UNUSED_VARS(t, xs0, offset); 1012 NO_DEVICE_CODE; 1013 #endif // VOLTA_MMA_AVAILABLE 1014 } 1015 1016 template 1017 static __device__ __forceinline__ void load_ldmatrix_trans_swizzled( 1018 tile & t, const T * __restrict__ xs0, const int offset) { 1019 #if defined(TURING_MMA_AVAILABLE) 1020 static_assert(I == 8 || I == 16, "bad tile width"); 1021 static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout"); 1022 const int i = threadIdx.x % 16; 1023 const int j = I == 16 ? (threadIdx.x / 16) * (t.J / 2) : 0; 1024 int offset_ij = offset + i * stride + j; 1025 offset_ij = swizzle(offset_ij, i); 1026 int * xi = (int *) t.x; 1027 if constexpr (I == 16) { 1028 asm volatile("ldmatrix.sync.aligned.m8n8.x4.trans.b16 {%0, %1, %2, %3}, [%4];" 1029 : "=r"(xi[0]), "=r"(xi[2]), "=r"(xi[1]), "=r"(xi[3]) 1030 : "l"(xs0 + offset_ij)); 1031 } else { 1032 asm volatile("ldmatrix.sync.aligned.m8n8.x2.trans.b16 {%0, %1}, [%2];" 1033 : "=r"(xi[0]), "=r"(xi[1]) 1034 : "l"(xs0 + offset_ij)); 1035 } 1036 #elif defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 1037 static_assert(dl == DATA_LAYOUT_I_MAJOR || dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout"); 1038 if constexpr (I == 32) { 1039 #pragma unroll 1040 for (int l0 = 0; l0 < t.ne/2; ++l0) { 1041 half2 tmp[2]; 1042 #pragma unroll 1043 for (int o = 0; o < 2; ++o) { 1044 const int j = 2*t.get_j(l0) + o; 1045 int offset_ij = offset + j*stride + t.get_i(l0)/2; 1046 offset_ij = swizzle(offset_ij, j); 1047 tmp[o] = xs0[offset_ij]; 1048 } 1049 1050 t.x[l0] = __lows2half2(tmp[0], tmp[1]); 1051 t.x[l0 + t.ne/2] = __highs2half2(tmp[0], tmp[1]); 1052 } 1053 } else { 1054 half * xh = (half *) t.x; 1055 #pragma unroll 1056 for (int l = 0; l < t.ne; ++l) { 1057 #pragma unroll 1058 for (int o = 0; o < 2; ++o) { 1059 const int j = 2*t.get_j(l) + o; 1060 xh[2*l + o] = ((const half *) xs0)[swizzle<2*stride, half>(2*offset + j*(2*stride) + t.get_i(l), j)]; 1061 } 1062 } 1063 } 1064 #else 1065 GGML_UNUSED_VARS(t, xs0, offset); 1066 NO_DEVICE_CODE; 1067 #endif // defined(TURING_MMA_AVAILABLE) 1068 } 1069 1070 static __device__ __forceinline__ void mma( 1071 tile<16, 8, int> & D, const tile<16, 4, int> & A, const tile<8, 4, int> & B) { 1072 #ifdef TURING_MMA_AVAILABLE 1073 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1074 asm("mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1075 : "+r"(D.x[0]), "+r"(D.x[1]), "+r"(D.x[2]), "+r"(D.x[3]) 1076 : "r"(A.x[0]), "r"(A.x[1]), "r"(B.x[0])); 1077 #else 1078 // On Turing m16n8k16 mma is not available, use 2x m8n8k16 mma instead: 1079 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1080 : "+r"(D.x[0]), "+r"(D.x[1]) 1081 : "r"(A.x[0]), "r"(B.x[0])); 1082 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1083 : "+r"(D.x[2]), "+r"(D.x[3]) 1084 : "r"(A.x[1]), "r"(B.x[0])); 1085 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1086 #else 1087 GGML_UNUSED_VARS(D, A, B); 1088 NO_DEVICE_CODE; 1089 #endif // TURING_MMA_AVAILABLE 1090 } 1091 1092 static __device__ __forceinline__ void mma( 1093 tile<16, 8, int> & D, const tile<16, 8, int> & A, const tile<8, 8, int> & B) { 1094 #ifdef TURING_MMA_AVAILABLE 1095 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1096 asm("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1097 : "+r"(D.x[0]), "+r"(D.x[1]), "+r"(D.x[2]), "+r"(D.x[3]) 1098 : "r"(A.x[0]), "r"(A.x[1]), "r"(A.x[2]), "r"(A.x[3]), "r"(B.x[0]), "r"(B.x[1])); 1099 #else 1100 // On Turing m16n8k32 mma is not available, use 4x m8n8k16 mma instead: 1101 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1102 : "+r"(D.x[0]), "+r"(D.x[1]) 1103 : "r"(A.x[0]), "r"(B.x[0])); 1104 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1105 : "+r"(D.x[2]), "+r"(D.x[3]) 1106 : "r"(A.x[1]), "r"(B.x[0])); 1107 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1108 : "+r"(D.x[0]), "+r"(D.x[1]) 1109 : "r"(A.x[2]), "r"(B.x[1])); 1110 asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" 1111 : "+r"(D.x[2]), "+r"(D.x[3]) 1112 : "r"(A.x[3]), "r"(B.x[1])); 1113 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1114 #else 1115 GGML_UNUSED_VARS(D, A, B); 1116 NO_DEVICE_CODE; 1117 #endif // TURING_MMA_AVAILABLE 1118 } 1119 1120 static __device__ __forceinline__ void mma( 1121 tile<16, 4, half2> & D, const tile<16, 8, half2> & A, const tile<8, 8, half2> & B) { 1122 #ifdef TURING_MMA_AVAILABLE 1123 const int * Axi = (const int *) A.x; 1124 const int * Bxi = (const int *) B.x; 1125 int * Dxi = (int *) D.x; 1126 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1127 asm("mma.sync.aligned.m16n8k16.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3, %4, %5}, {%6, %7}, {%0, %1};" 1128 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1129 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1])); 1130 #else 1131 // On Turing m16n8k16 mma is not available, use 2x m8n8k8 mma instead: 1132 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1133 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1134 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0])); 1135 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1136 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1137 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[1])); 1138 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1139 #else 1140 GGML_UNUSED_VARS(D, A, B); 1141 NO_DEVICE_CODE; 1142 #endif // TURING_MMA_AVAILABLE 1143 } 1144 1145 static __device__ __forceinline__ void mma( 1146 tile<16, 8, half2> & D, const tile<16, 8, half2> & A, const tile<16, 8, half2> & B) { 1147 #ifdef TURING_MMA_AVAILABLE 1148 const int * Axi = (const int *) A.x; 1149 const int * Bxi = (const int *) B.x; 1150 int * Dxi = (int *) D.x; 1151 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1152 asm("mma.sync.aligned.m16n8k16.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3, %4, %5}, {%6, %7}, {%0, %1};" 1153 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1154 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[2])); 1155 asm("mma.sync.aligned.m16n8k16.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3, %4, %5}, {%6, %7}, {%0, %1};" 1156 : "+r"(Dxi[2]), "+r"(Dxi[3]) 1157 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[1]), "r"(Bxi[3])); 1158 #else 1159 // On Turing m16n8k16 mma is not available, use 4x m8n8k8 mma instead: 1160 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1161 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1162 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0])); 1163 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1164 : "+r"(Dxi[0]), "+r"(Dxi[1]) 1165 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[2])); 1166 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1167 : "+r"(Dxi[2]), "+r"(Dxi[3]) 1168 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[1])); 1169 asm("mma.sync.aligned.m16n8k8.row.col.f16.f16.f16.f16 {%0, %1}, {%2, %3}, {%4}, {%0, %1};" 1170 : "+r"(Dxi[2]), "+r"(Dxi[3]) 1171 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[3])); 1172 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1173 #elif defined(AMD_WMMA_AVAILABLE) 1174 #if defined(AMD_WMMA_128B_AVAILABLE) 1175 using halfx8_t = __attribute__((ext_vector_type(8))) _Float16; 1176 halfx8_t& acc_frag = reinterpret_cast(D.x[0]); 1177 const halfx8_t& a_frag = reinterpret_cast(A.x[0]); 1178 const halfx8_t& b_frag = reinterpret_cast(B.x[0]); 1179 acc_frag = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32_gfx12(a_frag, b_frag, acc_frag); 1180 #else 1181 GGML_UNUSED_VARS(D, A, B); 1182 NO_DEVICE_CODE; 1183 #endif // defined(AMD_WMMA_128B_AVAILABLE) 1184 #elif defined(AMD_MFMA_AVAILABLE) 1185 // MFMA: FP16 input, FP32 accumulate, convert back to half2. 1186 using halfx4_t = __attribute__((ext_vector_type(4))) _Float16; 1187 using floatx4_t = __attribute__((ext_vector_type(4))) float; 1188 1189 // Convert existing half2 accumulator to float for MFMA: 1190 floatx4_t acc_f32; 1191 { 1192 const halfx4_t acc_h = reinterpret_cast(D.x[0]); 1193 #pragma unroll 1194 for (int i = 0; i < 4; ++i) { 1195 acc_f32[i] = (float)acc_h[i]; 1196 } 1197 } 1198 1199 const halfx4_t& a_frag = reinterpret_cast(A.x[0]); 1200 const halfx4_t& b_frag = reinterpret_cast(B.x[0]); 1201 acc_f32 = __builtin_amdgcn_mfma_f32_16x16x16f16(a_frag, b_frag, acc_f32, 0, 0, 0); 1202 1203 // Convert back to half2: 1204 { 1205 halfx4_t result_h; 1206 #pragma unroll 1207 for (int i = 0; i < 4; ++i) { 1208 result_h[i] = (_Float16)acc_f32[i]; 1209 } 1210 reinterpret_cast(D.x[0]) = result_h; 1211 } 1212 #else 1213 GGML_UNUSED_VARS(D, A, B); 1214 NO_DEVICE_CODE; 1215 #endif // TURING_MMA_AVAILABLE 1216 } 1217 1218 static __device__ __forceinline__ void mma( 1219 tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR> & A, 1220 const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR> & B) { 1221 #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE) 1222 tile<16, 8, half2> * D16 = (tile<16, 8, half2> *) &D; 1223 const tile<16, 8, half2> * A16 = (const tile<16, 8, half2> *) &A; 1224 mma(D16[0], A16[0], B); 1225 mma(D16[1], A16[1], B); 1226 #else 1227 GGML_UNUSED_VARS(D, A, B); 1228 NO_DEVICE_CODE; 1229 #endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_128B_AVAILABLE) 1230 } 1231 1232 template 1233 static __device__ __forceinline__ void mma( 1234 tile<16, 8, float, dl_d> & D, const tile<16, 8, float, dl_ab> & A, const tile<8, 8, float, dl_ab> & B) { 1235 #ifdef AMPERE_MMA_AVAILABLE 1236 const int * Axi = (const int *) A.x; 1237 const int * Bxi = (const int *) B.x; 1238 int * Dxi = (int *) D.x; 1239 asm("mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1240 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1241 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1])); 1242 #else 1243 GGML_UNUSED_VARS(D, A, B); 1244 NO_DEVICE_CODE; 1245 #endif // AMPERE_MMA_AVAILABLE 1246 } 1247 1248 template 1249 static __device__ __forceinline__ void mma( 1250 tile<16, 16, float, dl_d> & D, const tile<16, 8, float, dl_ab> & A, const tile<16, 8, float, dl_ab> & B) { 1251 #ifdef AMD_MFMA_AVAILABLE 1252 using floatx4_t = __attribute__((ext_vector_type(4))) float; 1253 floatx4_t& acc_frag = reinterpret_cast(D.x[0]); 1254 #if defined(CDNA3) 1255 using floatx2_t = __attribute__((ext_vector_type(2))) float; 1256 const floatx2_t& a_frag = reinterpret_cast(A.x[0]); 1257 const floatx2_t& b_frag = reinterpret_cast(B.x[0]); 1258 acc_frag = __builtin_amdgcn_mfma_f32_16x16x8_xf32(a_frag, b_frag, acc_frag, 0, 0, 0); 1259 #elif defined(CDNA4) || defined(CDNA2) || defined(CDNA1) 1260 // CDNA4 (gfx950) does not support xf32 MFMA, use f32 path like CDNA2/CDNA1 1261 #pragma unroll 1262 for (int i = 0; i < 2; ++i) { 1263 acc_frag = __builtin_amdgcn_mfma_f32_16x16x4f32(A.x[i], B.x[i], acc_frag, 0, 0, 0); 1264 } 1265 #else 1266 GGML_UNUSED_VARS(D, A, B); 1267 NO_DEVICE_CODE; 1268 #endif // defined(CDNA3) 1269 #else 1270 GGML_UNUSED_VARS(D, A, B); 1271 NO_DEVICE_CODE; 1272 #endif // AMD_MFMA_AVAILABLE 1273 } 1274 1275 template 1276 static __device__ __forceinline__ void mma_block_scaled_fp4(tile<16, 8, float> & D, 1277 const tile<16, 8, int> & A, 1278 const tile<8, 8, int> & B, 1279 uint32_t a_scale, 1280 uint32_t b_scale) { 1281 #ifdef BLACKWELL_MMA_AVAILABLE 1282 const int * Axi = (const int *) A.x; 1283 const int * Bxi = (const int *) B.x; 1284 float * Dxi = (float *) D.x; 1285 1286 if constexpr (type == GGML_TYPE_MXFP4) { 1287 asm volatile( 1288 "mma.sync.aligned.kind::mxf4.block_scale.scale_vec::2X.m16n8k64.row.col.f32.e2m1.e2m1.f32.ue8m0 " 1289 "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3}, " 1290 "%10, {0, 0}, %11, {0, 0};" 1291 : "+f"(Dxi[0]), "+f"(Dxi[1]), "+f"(Dxi[2]), "+f"(Dxi[3]) 1292 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1]), "r"(a_scale), "r"(b_scale)); 1293 } else { 1294 asm volatile( 1295 "mma.sync.aligned.kind::mxf4nvf4.block_scale.scale_vec::4X.m16n8k64.row.col.f32.e2m1.e2m1.f32.ue4m3 " 1296 "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3}, " 1297 "%10, {0, 0}, %11, {0, 0};" 1298 : "+f"(Dxi[0]), "+f"(Dxi[1]), "+f"(Dxi[2]), "+f"(Dxi[3]) 1299 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1]), "r"(a_scale), "r"(b_scale)); 1300 } 1301 #else 1302 GGML_UNUSED_VARS(D, A, B, a_scale, b_scale); 1303 #endif // BLACKWELL_MMA_AVAILABLE 1304 } 1305 1306 static __device__ __forceinline__ void mma( 1307 tile<16, 8, float> & D, const tile<16, 8, half2> & A, const tile<8, 8, half2> & B) { 1308 #ifdef TURING_MMA_AVAILABLE 1309 const int * Axi = (const int *) A.x; 1310 const int * Bxi = (const int *) B.x; 1311 int * Dxi = (int *) D.x; 1312 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1313 asm("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1314 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1315 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1])); 1316 #else 1317 // On Turing m16n8k16 mma is not available, use 2x m8n8k8 mma instead: 1318 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1319 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1320 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0])); 1321 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1322 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1323 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[1])); 1324 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1325 #else 1326 GGML_UNUSED_VARS(D, A, B); 1327 NO_DEVICE_CODE; 1328 #endif // TURING_MMA_AVAILABLE 1329 } 1330 1331 static __device__ __forceinline__ void mma( 1332 tile<16, 8, float> & D, const tile<16, 8, nv_bfloat162> & A, const tile<8, 8, nv_bfloat162> & B) { 1333 #ifdef AMPERE_MMA_AVAILABLE 1334 const int * Axi = (const int *) A.x; 1335 const int * Bxi = (const int *) B.x; 1336 int * Dxi = (int *) D.x; 1337 asm("mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1338 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1339 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[1])); 1340 #else 1341 GGML_UNUSED_VARS(D, A, B); 1342 NO_DEVICE_CODE; 1343 #endif // AMPERE_MMA_AVAILABLE 1344 } 1345 1346 template 1347 static __device__ __forceinline__ void mma( 1348 tile<16, 16, float, dl_d> & D, const tile<16, 8, half2, dl_ab> & A, const tile<16, 8, half2, dl_ab> & B) { 1349 #ifdef TURING_MMA_AVAILABLE 1350 const int * Axi = (const int *) A.x; 1351 const int * Bxi = (const int *) B.x; 1352 int * Dxi = (int *) D.x; 1353 #if __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1354 asm("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1355 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1356 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[0]), "r"(Bxi[2])); 1357 asm("mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%0, %1, %2, %3};" 1358 : "+r"(Dxi[4]), "+r"(Dxi[5]), "+r"(Dxi[6]), "+r"(Dxi[7]) 1359 : "r"(Axi[0]), "r"(Axi[1]), "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[1]), "r"(Bxi[3])); 1360 #else 1361 // On Turing m16n8k16 mma is not available, use 4x m8n8k8 mma instead: 1362 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1363 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1364 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0])); 1365 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1366 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1367 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[2])); 1368 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1369 : "+r"(Dxi[4]), "+r"(Dxi[5]), "+r"(Dxi[6]), "+r"(Dxi[7]) 1370 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[1])); 1371 asm("mma.sync.aligned.m16n8k8.row.col.f32.f16.f16.f32 {%0, %1, %2, %3}, {%4, %5}, {%6}, {%0, %1, %2, %3};" 1372 : "+r"(Dxi[4]), "+r"(Dxi[5]), "+r"(Dxi[6]), "+r"(Dxi[7]) 1373 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[3])); 1374 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE 1375 #elif defined(AMD_WMMA_AVAILABLE) 1376 #if defined(AMD_WMMA_128B_AVAILABLE) 1377 using halfx8_t = __attribute__((ext_vector_type(8))) _Float16; 1378 using floatx8_t = __attribute__((ext_vector_type(8))) float; 1379 floatx8_t& acc_frag = reinterpret_cast(D.x[0]); 1380 const halfx8_t& a_frag = reinterpret_cast(A.x[0]); 1381 const halfx8_t& b_frag = reinterpret_cast(B.x[0]); 1382 acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a_frag, b_frag, acc_frag); 1383 #elif defined(AMD_WMMA_256B_AVAILABLE) 1384 using halfx16_t = __attribute__((ext_vector_type(16))) _Float16; 1385 using floatx8_t = __attribute__((ext_vector_type(8))) float; 1386 floatx8_t& acc_frag = reinterpret_cast(D.x[0]); 1387 const halfx16_t& a_frag = reinterpret_cast(A.x[0]); 1388 const halfx16_t& b_frag = reinterpret_cast(B.x[0]); 1389 acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a_frag, b_frag, acc_frag); 1390 #else 1391 GGML_UNUSED_VARS(D, A, B); 1392 NO_DEVICE_CODE; 1393 #endif // AMD_WMMA_128B_AVAILABLE 1394 #elif defined(AMD_MFMA_AVAILABLE) 1395 using halfx4_t = __attribute__((ext_vector_type(4))) _Float16; 1396 using floatx4_t = __attribute__((ext_vector_type(4))) float; 1397 floatx4_t& acc_frag = reinterpret_cast(D.x[0]); 1398 const halfx4_t& a_frag = reinterpret_cast(A.x[0]); 1399 const halfx4_t& b_frag = reinterpret_cast(B.x[0]); 1400 acc_frag = __builtin_amdgcn_mfma_f32_16x16x16f16(a_frag, b_frag, acc_frag, 0, 0, 0); 1401 #else 1402 GGML_UNUSED_VARS(D, A, B); 1403 NO_DEVICE_CODE; 1404 #endif // TURING_MMA_AVAILABLE 1405 } 1406 1407 template 1408 static __device__ __forceinline__ void mma( 1409 tile<16, 16, float, dl_d> & D, const tile<16, 8, nv_bfloat162, dl_ab> & A, const tile<16, 8, nv_bfloat162, dl_ab> & B) { 1410 #if defined(AMD_WMMA_AVAILABLE) 1411 #if defined(AMD_WMMA_128B_AVAILABLE) 1412 using bf16x8_t = __attribute__((ext_vector_type(8))) short; 1413 using floatx8_t = __attribute__((ext_vector_type(8))) float; 1414 floatx8_t& acc_frag = reinterpret_cast(D.x[0]); 1415 const bf16x8_t& a_frag = reinterpret_cast(A.x[0]); 1416 const bf16x8_t& b_frag = reinterpret_cast(B.x[0]); 1417 acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a_frag, b_frag, acc_frag); 1418 #elif defined(AMD_WMMA_256B_AVAILABLE) 1419 using bf16x16_t = __attribute__((ext_vector_type(16))) short; 1420 using floatx8_t = __attribute__((ext_vector_type(8))) float; 1421 floatx8_t& acc_frag = reinterpret_cast(D.x[0]); 1422 const bf16x16_t& a_frag = reinterpret_cast(A.x[0]); 1423 const bf16x16_t& b_frag = reinterpret_cast(B.x[0]); 1424 acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a_frag, b_frag, acc_frag); 1425 #else 1426 GGML_UNUSED_VARS(D, A, B); 1427 NO_DEVICE_CODE; 1428 #endif // defined(AMD_WMMA_128B_AVAILABLE) 1429 #elif defined(AMD_MFMA_AVAILABLE) 1430 using floatx4_t = __attribute__((ext_vector_type(4))) float; 1431 floatx4_t& acc_frag = reinterpret_cast(D.x[0]); 1432 #if defined(CDNA4) || defined(CDNA3) || defined(CDNA2) 1433 using bf16x4_t = __attribute__((ext_vector_type(4))) __bf16; 1434 const bf16x4_t& a_frag = reinterpret_cast(A.x[0]); 1435 const bf16x4_t& b_frag = reinterpret_cast(B.x[0]); 1436 acc_frag = __builtin_amdgcn_mfma_f32_16x16x16bf16_1k(a_frag, b_frag, acc_frag, 0, 0, 0); 1437 #elif defined(CDNA1) 1438 #pragma unroll 1439 for (int i = 0; i < 2; ++i) { 1440 using bf16x2_t = __attribute__((ext_vector_type(2))) __bf16; 1441 const bf16x2_t& a_frag = reinterpret_cast(A.x[i]); 1442 const bf16x2_t& b_frag = reinterpret_cast(B.x[i]); 1443 acc_frag = __builtin_amdgcn_mfma_f32_16x16x8bf16(a_frag, b_frag, acc_frag, 0, 0, 0); 1444 } 1445 #else 1446 GGML_UNUSED_VARS(D, A, B); 1447 NO_DEVICE_CODE; 1448 #endif // defined(CDNA3) || defined(CDNA2) 1449 #else 1450 GGML_UNUSED_VARS(D, A, B); 1451 NO_DEVICE_CODE; 1452 #endif // defined(AMD_WMMA_AVAILABLE) 1453 } 1454 1455 template 1456 static __device__ __forceinline__ void mma( 1457 tile<16, 16, int, dl_d> & D, const tile<16, 8, int, dl_ab> & A, const tile<16, 8, int, dl_ab> & B) { 1458 #if defined(AMD_MFMA_AVAILABLE) 1459 using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int; 1460 int32x4_t * acc = (int32x4_t *) D.x; 1461 #if defined(CDNA4) || defined(CDNA3) 1462 acc[0] = __builtin_amdgcn_mfma_i32_16x16x32_i8(((int64_t *) A.x)[0], ((int64_t *) B.x)[0], acc[0], 0, 0, 0); 1463 #elif defined(CDNA2) || defined(CDNA1) 1464 acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[0], B.x[0], acc[0], 0, 0, 0); 1465 acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[1], B.x[1], acc[0], 0, 0, 0); 1466 #endif // defined(CDNA4) || defined(CDNA3) 1467 #elif defined(AMD_WMMA_AVAILABLE) 1468 using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int; 1469 int32x8_t * acc = (int32x8_t *) D.x; 1470 #if defined(AMD_WMMA_128B_AVAILABLE) 1471 using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int; 1472 int32x2_t * a_vec = (int32x2_t *) A.x; 1473 int32x2_t * b_vec = (int32x2_t *) B.x; 1474 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], true); 1475 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[1], true, b_vec[1], acc[0], true); 1476 #elif defined(AMD_WMMA_256B_AVAILABLE) 1477 using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int; 1478 int32x4_t * a_vec = (int32x4_t *) A.x; 1479 int32x4_t * b_vec = (int32x4_t *) B.x; 1480 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], true); 1481 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[1], true, b_vec[1], acc[0], true); 1482 #endif // AMD_WMMA_128B_AVAILABLE 1483 #else 1484 GGML_UNUSED_VARS(D, A, B); 1485 NO_DEVICE_CODE; 1486 #endif // AMD_MFMA_AVAILABLE 1487 } 1488 1489 static __device__ __forceinline__ void mma( 1490 tile<32, 32, int> & D, const tile<32, 4, int> & A, const tile<32, 4, int> & B) { 1491 #if defined(AMD_MFMA_AVAILABLE) 1492 using int32x16_t = __attribute__((__vector_size__(16 * sizeof(int)))) int; 1493 int32x16_t * acc = (int32x16_t *) D.x; 1494 #if defined(CDNA4) || defined(CDNA3) 1495 acc[0] = __builtin_amdgcn_mfma_i32_32x32x16_i8(((int64_t *) A.x)[0], ((int64_t *) B.x)[0], acc[0], 0, 0, 0); 1496 #elif defined(CDNA2) || defined(CDNA1) 1497 acc[0] = __builtin_amdgcn_mfma_i32_32x32x8i8(A.x[0], B.x[0], acc[0], 0, 0, 0); 1498 acc[0] = __builtin_amdgcn_mfma_i32_32x32x8i8(A.x[1], B.x[1], acc[0], 0, 0, 0); 1499 #endif // defined(CDNA4) || defined(CDNA3) 1500 1501 #else 1502 GGML_UNUSED_VARS(D, A, B); 1503 NO_DEVICE_CODE; 1504 #endif // AMD_MFMA_AVAILABLE 1505 } 1506 1507 template 1508 static __device__ __forceinline__ void mma( 1509 tile<32, J, T1> & D, const tile<32, K, T2> & A, const tile & B) { 1510 tile <16, J, T1> * D16 = reinterpret_cast< tile<16, J, T1> *>(&D); 1511 const tile<16, K, T2> * A16 = reinterpret_cast *>(&A); 1512 mma(D16[0], A16[0], B); 1513 mma(D16[1], A16[1], B); 1514 } 1515 1516 static __device__ __forceinline__ void mma( 1517 tile<32, 8, float> & D, const tile<32, 4, half2> & A, const tile<8, 4, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & B) { 1518 #if defined(VOLTA_MMA_AVAILABLE) 1519 const int * Axi = (const int *) A.x; 1520 const int * Bxi = (const int *) B.x; 1521 int * Dxi = (int *) D.x; 1522 asm("mma.sync.aligned.m8n8k4.row.col.f32.f16.f16.f32 " 1523 "{%0, %1, %2, %3, %4, %5, %6, %7}, {%8, %9}, {%10, %11}, {%0, %1, %2, %3, %4, %5, %6, %7};" 1524 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]), "+r"(Dxi[4]), "+r"(Dxi[5]), "+r"(Dxi[6]), "+r"(Dxi[7]) 1525 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0]), "r"(Bxi[1])); 1526 asm("mma.sync.aligned.m8n8k4.row.col.f32.f16.f16.f32 " 1527 "{%0, %1, %2, %3, %4, %5, %6, %7}, {%8, %9}, {%10, %11}, {%0, %1, %2, %3, %4, %5, %6, %7};" 1528 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]), "+r"(Dxi[4]), "+r"(Dxi[5]), "+r"(Dxi[6]), "+r"(Dxi[7]) 1529 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[2]), "r"(Bxi[3])); 1530 #else 1531 GGML_UNUSED_VARS(D, A, B); 1532 NO_DEVICE_CODE; 1533 #endif // defined(VOLTA_MMA_AVAILABLE) 1534 } 1535 1536 static __device__ __forceinline__ void mma( 1537 tile<32, 4, half2> & D, const tile<32, 4, half2> & A, const tile<8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> & B) { 1538 #if defined(VOLTA_MMA_AVAILABLE) 1539 const int * Axi = (const int *) A.x; 1540 const int * Bxi = (const int *) B.x; 1541 int * Dxi = (int *) D.x; 1542 asm("mma.sync.aligned.m8n8k4.row.row.f16.f16.f16.f16 " 1543 "{%0, %1, %2, %3}, {%4, %5}, {%6, %7}, {%0, %1, %2, %3};" 1544 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1545 : "r"(Axi[0]), "r"(Axi[1]), "r"(Bxi[0]), "r"(Bxi[1])); 1546 asm("mma.sync.aligned.m8n8k4.row.row.f16.f16.f16.f16 " 1547 "{%0, %1, %2, %3}, {%4, %5}, {%6, %7}, {%0, %1, %2, %3};" 1548 : "+r"(Dxi[0]), "+r"(Dxi[1]), "+r"(Dxi[2]), "+r"(Dxi[3]) 1549 : "r"(Axi[2]), "r"(Axi[3]), "r"(Bxi[2]), "r"(Bxi[3])); 1550 #else 1551 GGML_UNUSED_VARS(D, A, B); 1552 NO_DEVICE_CODE; 1553 #endif // defined(VOLTA_MMA_AVAILABLE) 1554 } 1555 1556 static __device__ __forceinline__ void mma( 1557 tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & A, 1558 const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & B) { 1559 #if defined(AMD_WMMA_256B_AVAILABLE) 1560 using halfx16_t = __attribute__((ext_vector_type(16))) _Float16; 1561 halfx16_t * xD = (halfx16_t *) D.x; 1562 const halfx16_t * xA = (const halfx16_t *) A.x; 1563 const halfx16_t * xB = (const halfx16_t *) B.x; 1564 xD[0] = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32(xA[0], xB[0], xD[0], /*opsel =*/ 0); 1565 xD[0] = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32(xA[1], xB[0], xD[0], /*opsel =*/ 1); 1566 #else 1567 GGML_UNUSED_VARS(D, A, B); 1568 NO_DEVICE_CODE; 1569 #endif // TURING_MMA_AVAILABLE 1570 } 1571 1572 template 1573 static __device__ __forceinline__ void mma( 1574 tile<16, 16, int, dl_d> & D, const tile<16, 4, int, dl_ab> & A, const tile<16, 4, int, dl_ab> & B) { 1575 #if defined(AMD_MFMA_AVAILABLE) 1576 using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int; 1577 int32x4_t * acc = (int32x4_t *) D.x; 1578 #if defined(CDNA4) || defined(CDNA3) 1579 const int64_t xA = uint32_t(A.x[0]); 1580 const int64_t xB = uint32_t(B.x[0]); 1581 acc[0] = __builtin_amdgcn_mfma_i32_16x16x32_i8(xA, xB, acc[0], 0, 0, 0); 1582 #elif defined(CDNA2) || defined(CDNA1) 1583 acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[0], B.x[0], acc[0], 0, 0, 0); 1584 #endif // defined(CDNA4) || defined(CDNA3) 1585 #elif defined(AMD_WMMA_AVAILABLE) 1586 using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int; 1587 int32x8_t * acc = (int32x8_t *) D.x; 1588 #if defined(AMD_WMMA_128B_AVAILABLE) 1589 using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int; 1590 int32x2_t * a_vec = (int32x2_t *) A.x; 1591 int32x2_t * b_vec = (int32x2_t *) B.x; 1592 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], false); 1593 #elif defined(AMD_WMMA_256B_AVAILABLE) 1594 using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int; 1595 int32x4_t * a_vec = (int32x4_t *) A.x; 1596 int32x4_t * b_vec = (int32x4_t *) B.x; 1597 acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], false); 1598 #endif // AMD_WMMA_128B_AVAILABLE 1599 #else 1600 GGML_UNUSED(D); 1601 GGML_UNUSED(A); 1602 GGML_UNUSED(B); 1603 NO_DEVICE_CODE; 1604 #endif // AMD_WMMA_AVAILABLE 1605 } 1606 }