|
| 1 | +#include "common.cuh" |
| 2 | + |
| 3 | +struct mma_int_A_I16K8 { |
| 4 | + static constexpr int I = 16; |
| 5 | + static constexpr int K = 8; |
| 6 | + static constexpr int ne = 4; |
| 7 | + |
| 8 | + int x[ne] = {0}; |
| 9 | + |
| 10 | + static __device__ __forceinline__ int get_i(const int l) { |
| 11 | + const int ret = (l%2) * (I/2) + threadIdx.x / (K/2); |
| 12 | + GGML_CUDA_ASSUME(ret >= 0); |
| 13 | + GGML_CUDA_ASSUME(ret < I); |
| 14 | + return ret; |
| 15 | + } |
| 16 | + |
| 17 | + static __device__ __forceinline__ int get_k(const int l) { |
| 18 | + const int ret = (l/2) * (K/2) + threadIdx.x % (K/2); |
| 19 | + GGML_CUDA_ASSUME(ret >= 0); |
| 20 | + GGML_CUDA_ASSUME(ret < K); |
| 21 | + return ret; |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +struct mma_int_B_J8K8 { |
| 26 | + static constexpr int J = 8; |
| 27 | + static constexpr int K = 8; |
| 28 | + static constexpr int ne = 2; |
| 29 | + |
| 30 | + int x[ne] = {0}; |
| 31 | + |
| 32 | + static __device__ __forceinline__ int get_j(const int /* l */) { |
| 33 | + const int ret = threadIdx.x / (K/2); |
| 34 | + GGML_CUDA_ASSUME(ret >= 0); |
| 35 | + GGML_CUDA_ASSUME(ret < J); |
| 36 | + return ret; |
| 37 | + } |
| 38 | + |
| 39 | + static __device__ __forceinline__ int get_k(const int l) { |
| 40 | + const int ret = l * (K/2) + threadIdx.x % (K/2); |
| 41 | + GGML_CUDA_ASSUME(ret >= 0); |
| 42 | + GGML_CUDA_ASSUME(ret < K); |
| 43 | + return ret; |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +struct mma_int_C_I16J8 { |
| 48 | + static constexpr int I = 16; |
| 49 | + static constexpr int J = 8; |
| 50 | + static constexpr int ne = 4; |
| 51 | + |
| 52 | + int x[ne] = {0}; |
| 53 | + |
| 54 | + static __device__ __forceinline__ int get_i(const int l) { |
| 55 | + const int ret = (l/2) * (I/2) + threadIdx.x / (J/2); |
| 56 | + GGML_CUDA_ASSUME(ret >= 0); |
| 57 | + GGML_CUDA_ASSUME(ret < I); |
| 58 | + return ret; |
| 59 | + } |
| 60 | + |
| 61 | + static __device__ __forceinline__ int get_j(const int l) { |
| 62 | + const int ret = 2 * (threadIdx.x % (J/2)) + l%2; |
| 63 | + GGML_CUDA_ASSUME(ret >= 0); |
| 64 | + GGML_CUDA_ASSUME(ret < J); |
| 65 | + return ret; |
| 66 | + } |
| 67 | + |
| 68 | + __device__ __forceinline__ void mma_K8(const mma_int_A_I16K8 & mma_A, const mma_int_B_J8K8 & mma_B) { |
| 69 | +#ifdef INT8_MMA_AVAILABLE |
| 70 | +#if __CUDA_ARCH__ >= CC_AMPERE |
| 71 | + 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};" |
| 72 | + : "+r"(x[0]), "+r"(x[1]), "+r"(x[2]), "+r"(x[3]) |
| 73 | + : "r"(mma_A.x[0]), "r"(mma_A.x[1]), "r"(mma_A.x[2]), "r"(mma_A.x[3]), "r"(mma_B.x[0]), "r"(mma_B.x[1])); |
| 74 | +#else |
| 75 | + // On Turing m16n8k32 mma is not available, use 4x m8n8k16 mma instead: |
| 76 | + asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" |
| 77 | + : "+r"(x[0]), "+r"(x[1]) |
| 78 | + : "r"(mma_A.x[0]), "r"(mma_B.x[0])); |
| 79 | + asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" |
| 80 | + : "+r"(x[2]), "+r"(x[3]) |
| 81 | + : "r"(mma_A.x[1]), "r"(mma_B.x[0])); |
| 82 | + asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" |
| 83 | + : "+r"(x[0]), "+r"(x[1]) |
| 84 | + : "r"(mma_A.x[2]), "r"(mma_B.x[1])); |
| 85 | + asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0, %1}, {%2}, {%3}, {%0, %1};" |
| 86 | + : "+r"(x[2]), "+r"(x[3]) |
| 87 | + : "r"(mma_A.x[3]), "r"(mma_B.x[1])); |
| 88 | +#endif // __CUDA_ARCH__ >= CC_AMPERE |
| 89 | +#else |
| 90 | + GGML_UNUSED(mma_A); |
| 91 | + GGML_UNUSED(mma_B); |
| 92 | + NO_DEVICE_CODE; |
| 93 | +#endif // INT8_MMA_AVAILABLE |
| 94 | + } |
| 95 | +}; |
0 commit comments