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/mmq.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 / mmq.cuh` 1664 lines ยท 79586 bytes Raw Blame History 1 #pragma once 2 3 #include "common.cuh" 4 #include "unary.cuh" 5 6 #include 7 #include 8 9 #define MMQ_DP4A_MAX_BATCH_SIZE 64 // Max. batch size to use for dp4a MMQ kernels when FP16 tensor cores are available. 10 #define MMQ_ITER_K 256 11 #define MMQ_ITER_K_FP4 512 12 #define MMQ_NWARPS 8 13 14 typedef void (*ggml_cuda_mmq_load_tiles_t)(const char * __restrict__ x, const char * __restrict__ x_gate, int * x_tile, 15 const int kbx0, const int i_max, const int stride); 16 typedef void (*ggml_cuda_mmq_vec_dot_t)(const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00); 17 typedef void (*ggml_cuda_mmq_write_back_t)(const float * __restrict__ sum, const int32_t * __restrict__ get_rows_to_sorted, 18 float * __restrict__ dst, const float * __restrict__ y_scale, const int stride, const int i_max, const int j_max, 19 const ggml_glu_op glu_op, const float glu_limit); 20 21 enum mmq_q8_1_ds_layout { 22 MMQ_Q8_1_DS_LAYOUT_D4, 23 MMQ_Q8_1_DS_LAYOUT_DS4, 24 MMQ_Q8_1_DS_LAYOUT_D2S6, 25 }; 26 27 static constexpr int QK8_1_MMQ = 4*QK8_1; 28 static constexpr int QK_FP4_MMQ = 2*QK8_1_MMQ; 29 30 struct block_q8_1_mmq { 31 // The y float data is converted to a data layout that can simply be copied to shared memory as a contiguous block. 32 // The y float data is first grouped as blocks of 128 values. 33 // These blocks are then treated as individual data values and transposed. 34 // 35 // To avoid shared memory bank conflicts each block is padded with 16 bytes. 36 // This padding is also used to store block scales/partial sums. 37 // The scales multiplied with the quantized data are equal to the unquantized values. 38 // The partial sums are obtained by summing up a subgroup of the contained values (prior to quantization) 39 // and are only needed for performance reasons. 40 // 41 // The exact data stored depends on the x data type. 42 union { 43 float d4[4]; // 1 32 bit scale per 32 values, stored as d0,d1,d2,d3 44 half2 ds4[4]; // 1 16 bit scale + 1 16 bit partial sum per 32 values, stored as d0,s0,d1,s1,d2,s2,d3,s3 45 half d2s6[8]; // 1 16 bit scale per 64 values + 1 16 bit partial sum per 16 values for the first 96 values, 46 // stored as d0,d1,s1,s2,s3,s4,s5 47 }; 48 int8_t qs[QK8_1_MMQ]; 49 }; 50 51 // this struct is used for fp4 data types (currently only used for Blackwell) 52 // mxfp4 has block size 32, each int32 of d4 contains 2 e8m0 scales in the lower 16 bits 53 // nvfp4 has block size 16, each int32 of d4 contains 4 ue4m3 scales 54 struct block_fp4_mmq { 55 uint32_t d4[4]; 56 int8_t qs[QK_FP4_MMQ / 2]; 57 }; 58 59 static_assert(sizeof(block_q8_1_mmq) == QK8_1_MMQ + 4*sizeof(half2), "Unexpected block_q8_1_mmq size"); 60 static_assert(sizeof(block_q8_1_mmq) == 4*sizeof(block_q8_1), "Unexpected block_q8_1_mmq size"); 61 static_assert(sizeof(block_fp4_mmq) == sizeof(block_q8_1_mmq), "Unexpected block_fp4_mmq size"); 62 63 static mmq_q8_1_ds_layout mmq_get_q8_1_ds_layout(const ggml_type type_x) { 64 switch (type_x) { 65 case GGML_TYPE_Q1_0: 66 case GGML_TYPE_Q2_0: 67 return MMQ_Q8_1_DS_LAYOUT_D4; 68 case GGML_TYPE_Q4_0: 69 case GGML_TYPE_Q4_1: 70 return MMQ_Q8_1_DS_LAYOUT_DS4; 71 case GGML_TYPE_Q5_0: 72 return MMQ_Q8_1_DS_LAYOUT_D4; 73 case GGML_TYPE_Q5_1: 74 return MMQ_Q8_1_DS_LAYOUT_DS4; 75 case GGML_TYPE_Q8_0: 76 return MMQ_Q8_1_DS_LAYOUT_D4; 77 case GGML_TYPE_MXFP4: 78 return MMQ_Q8_1_DS_LAYOUT_D4; 79 case GGML_TYPE_NVFP4: 80 return MMQ_Q8_1_DS_LAYOUT_D4; 81 case GGML_TYPE_Q2_K: 82 return MMQ_Q8_1_DS_LAYOUT_D2S6; 83 case GGML_TYPE_Q3_K: 84 return MMQ_Q8_1_DS_LAYOUT_D4; 85 case GGML_TYPE_Q4_K: 86 case GGML_TYPE_Q5_K: 87 return MMQ_Q8_1_DS_LAYOUT_DS4; 88 case GGML_TYPE_Q6_K: 89 case GGML_TYPE_IQ2_XXS: 90 case GGML_TYPE_IQ2_XS: 91 case GGML_TYPE_IQ2_S: 92 case GGML_TYPE_IQ3_XXS: 93 case GGML_TYPE_IQ3_S: 94 return MMQ_Q8_1_DS_LAYOUT_D4; 95 case GGML_TYPE_IQ1_S: 96 return MMQ_Q8_1_DS_LAYOUT_DS4; 97 case GGML_TYPE_IQ4_XS: 98 case GGML_TYPE_IQ4_NL: 99 return MMQ_Q8_1_DS_LAYOUT_D4; 100 default: 101 GGML_ABORT("fatal error"); 102 } 103 } 104 105 struct tile_x_sizes { 106 int qs; 107 int dm; 108 int sc; 109 }; 110 111 // Decouple shared memory tile sizes from WARP_SIZE to allow for different warp sizes. 112 // The K dimension of the tiles has either, 113 // 1*MMQ_TILE_NE_K==32 (always for TILE_Y_K) or 2*MMQ_TILE_NE_K==64 (typically for TILE_X_K), 114 // 32 bit elements for the quantized data (does not include scales). 115 // In other words, the size of the quantized data in the K dimension is a multiple of MMQ_TILE_NE_K. 116 // The final tile size in K direction is padded to avoid shared memory bank conflicts, 117 // in terms of 32 bit elements that means K % 2 == 1 for dp4a or K % 8 == 4 for mma. 118 #define MMQ_TILE_NE_K 32 119 120 // block_q8_1_mmq has (128 8-bit ints == 32 32-bit ints + 4 32-bit scales) 121 #define MMQ_TILE_Y_K (MMQ_TILE_NE_K + MMQ_TILE_NE_K / QI8_1) 122 #define MMQ_TILE_Y_FP4_K MMQ_TILE_Y_K 123 124 enum ggml_cuda_mmq_sram_layout { 125 GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_0, 126 GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_1, 127 GGML_CUDA_MMQ_SRAM_LAYOUT_Q2_K, 128 GGML_CUDA_MMQ_SRAM_LAYOUT_Q3_K, 129 GGML_CUDA_MMQ_SRAM_LAYOUT_Q6_K, 130 GGML_CUDA_MMQ_SRAM_LAYOUT_FP4, // MXFP4 and NVFP4 on Blackwell. 131 GGML_CUDA_MMQ_SRAM_LAYOUT_NVFP4, // Generic NVFP4 132 }; 133 134 static constexpr __host__ __device__ int ggml_cuda_mmq_get_sram_stride(ggml_cuda_mmq_sram_layout sram_layout) { 135 switch (sram_layout) { 136 case GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_0: 137 return 2*MMQ_TILE_NE_K + 2*MMQ_TILE_NE_K/QI8_0 + 4; 138 case GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_1: 139 return 2*MMQ_TILE_NE_K + 2*MMQ_TILE_NE_K/QI8_1 + 4; 140 case GGML_CUDA_MMQ_SRAM_LAYOUT_Q2_K: 141 return 2*MMQ_TILE_NE_K + MMQ_TILE_NE_K + 4; 142 case GGML_CUDA_MMQ_SRAM_LAYOUT_Q3_K: 143 return 2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/2 + 4; 144 case GGML_CUDA_MMQ_SRAM_LAYOUT_Q6_K: 145 return 2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/QI6_K + MMQ_TILE_NE_K/8 + 7; 146 case GGML_CUDA_MMQ_SRAM_LAYOUT_FP4: 147 return 2*MMQ_TILE_NE_K + 8 + 4; 148 case GGML_CUDA_MMQ_SRAM_LAYOUT_NVFP4: 149 return 2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/2 + 4; 150 default: 151 return -1; 152 } 153 } 154 155 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_0) % 8 == 4, "Wrong padding."); 156 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_1) % 8 == 4, "Wrong padding."); 157 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q2_K) % 8 == 4, "Wrong padding."); 158 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q3_K) % 8 == 4, "Wrong padding."); 159 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q6_K) % 8 == 4, "Wrong padding."); 160 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_FP4) % 8 == 4, "Wrong padding."); 161 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_NVFP4) % 8 == 4, "Wrong padding."); 162 163 static_assert(ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_FP4) == ggml_cuda_mmq_get_sram_stride(GGML_CUDA_MMQ_SRAM_LAYOUT_Q8_1), "Wrong tile size for MXFP4"); 164 165 // Config options for the MMQ kernel. 166 // Should not affect results, only speed/register pressure/shared memory use. 167 struct ggml_cuda_mmq_config { 168 ggml_type type; // src0->type 169 int nthreads; // Number of threads per CUDA block. 170 int occupancy; // Targeted occupancy for the MMA kernel. 171 int I; // SRAM tile width in src0->ne[1]/dst->ne[0] direction. 172 int J; // SRAM tile width in src1->ne[1]/dst->ne[1] direction. 173 ggml_cuda_mmq_sram_layout sram_layout; // SRAM tile length in src0->ne[0]/src1->ne[0] direction (physical 32 bit elements). 174 int K_vram; // VRAM tile length in src0->ne[0]/src1->ne[0] direction (logical elements). 175 bool stream_k; // Whether or not to use stream-k decomposition. 176 bool fallback; // Whether a fallback for out-of-bounds check in src0->ne[1] direction is needed. 177 178 constexpr __host__ __device__ ggml_cuda_mmq_config( 179 ggml_type type, int nthreads, int occupancy, int I, int J, ggml_cuda_mmq_sram_layout sram_layout, int K_vram, bool stream_k, bool fallback) : 180 type(type), nthreads(nthreads), occupancy(occupancy), I(I), J(J), sram_layout(sram_layout), K_vram(K_vram), stream_k(stream_k), fallback(fallback) {} 181 182 constexpr __device__ int rows_per_warp() const { 183 #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 184 return 16; 185 #else 186 return J >= 48 && J % 16 == 0 ? 32 : 16; 187 #endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 188 } 189 190 // TODO transition all combinations of GPUs and quantizations to the MMA data layout. 191 __host__ int use_mma_data_layout(const int cc) const { 192 if (amd_mfma_available(cc) || amd_wmma_available(cc) || turing_mma_available(cc)) { 193 return true; 194 } 195 return false; 196 } 197 198 constexpr __device__ bool use_mma_data_layout() const { 199 #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) 200 return true; 201 #else 202 return false; 203 #endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) 204 } 205 206 }; 207 208 #define CASE(type_, nthreads_, occupancy_, I_, J_, sram_layout_, K_vram_, stream_k_, fallback_) \ 209 if (type == (type_) && J == (J_) && fallback == (fallback_)) { \ 210 static_assert((nthreads_) % 32 == 0 && (nthreads_) <= 512, "bad nthreads"); \ 211 static_assert( (occupancy_) <= 8, "bad occupancy"); \ 212 static_assert((I_) % 32 == 0, "bad I"); \ 213 static_assert((J_) % 8 == 0 && (J_) <= 128, "bad J"); \ 214 static_assert((K_vram_) % 256 == 0, "bad K_vram"); \ 215 return ggml_cuda_mmq_config((type_), (nthreads_), (occupancy_), (I_), (J_), (sram_layout_), (K_vram_), (stream_k_), (fallback_)); \ 216 } \ 217 218 #include "mmq-config-pascal-older.cuh" 219 #include "mmq-config-pascal-dp4a.cuh" 220 #include "mmq-config-ampere.cuh" 221 #include "mmq-config-blackwell.cuh" 222 223 #include "mmq-config-gcn.cuh" 224 #include "mmq-config-cdna.cuh" 225 #include "mmq-config-rdna2.cuh" 226 #include "mmq-config-rdna3.cuh" 227 #include "mmq-config-rdna3-5.cuh" 228 #include "mmq-config-rdna4.cuh" 229 230 #undef CASE 231 232 static __host__ ggml_cuda_mmq_config ggml_cuda_mmq_get_config(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 233 if (GGML_CUDA_CC_IS_AMD(cc)) { 234 if (GGML_CUDA_CC_IS_GCN(cc)) { 235 return ggml_cuda_mmq_get_config_gcn(type, J, fallback); 236 } 237 if (GGML_CUDA_CC_IS_CDNA(cc)) { 238 return ggml_cuda_mmq_get_config_cdna(type, J, fallback); 239 } 240 if (GGML_CUDA_CC_IS_RDNA4(cc)) { 241 return ggml_cuda_mmq_get_config_rdna4(type, J, fallback); 242 } 243 if (GGML_CUDA_CC_IS_RDNA4_M(cc)) { 244 return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback); 245 } 246 if (GGML_CUDA_CC_IS_RDNA3_5(cc)) { 247 return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback); 248 } 249 if (GGML_CUDA_CC_IS_RDNA3(cc)) { // covers RDNA 3.0 250 return ggml_cuda_mmq_get_config_rdna3(type, J, fallback); 251 } 252 return ggml_cuda_mmq_get_config_rdna2(type, J, fallback); 253 } 254 if (blackwell_mma_available(cc)) { 255 // only src1 at Q4 uses the native FP4 config, higher precisions keep src1 at Q8_1 256 if (prec_src1 != GGML_PREC_Q4 && (type == GGML_TYPE_NVFP4 || type == GGML_TYPE_MXFP4)) { 257 return ggml_cuda_mmq_get_config_ampere(type, J, fallback); 258 } 259 return ggml_cuda_mmq_get_config_blackwell(type, J, fallback); 260 } 261 if (ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_VOLTA) { 262 return ggml_cuda_mmq_get_config_ampere(type, J, fallback); 263 } 264 if (ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_DP4A) { 265 return ggml_cuda_mmq_get_config_pascal_dp4a(type, J, fallback); 266 } 267 return ggml_cuda_mmq_get_config_pascal_older(type, J, fallback); 268 } 269 270 static constexpr __device__ ggml_cuda_mmq_config ggml_cuda_mmq_get_config(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 271 #ifdef GGML_USE_HIP 272 #ifdef GCN 273 return ggml_cuda_mmq_get_config_gcn(type, J, fallback); 274 #elif defined(CDNA) 275 return ggml_cuda_mmq_get_config_cdna(type, J, fallback); 276 #elif defined(RDNA4) 277 return ggml_cuda_mmq_get_config_rdna4(type, J, fallback); 278 #elif defined(RDNA4_M) 279 return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback); 280 #elif defined(RDNA3_5) 281 return ggml_cuda_mmq_get_config_rdna3_5(type, J, fallback); 282 #elif defined(RDNA3) 283 return ggml_cuda_mmq_get_config_rdna3(type, J, fallback); 284 #else 285 return ggml_cuda_mmq_get_config_rdna2(type, J, fallback); 286 #endif // CDNA 287 #else 288 #ifdef BLACKWELL_MMA_AVAILABLE 289 // only src1 at Q4 uses the native FP4 config, higher precisions keep src1 at Q8_1 290 if (prec_src1 != GGML_PREC_Q4 && (type == GGML_TYPE_NVFP4 || type == GGML_TYPE_MXFP4)) { 291 return ggml_cuda_mmq_get_config_ampere(type, J, fallback); 292 } 293 return ggml_cuda_mmq_get_config_blackwell(type, J, fallback); 294 #elif !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= GGML_CUDA_CC_VOLTA 295 return ggml_cuda_mmq_get_config_ampere(type, J, fallback); 296 #elif !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= GGML_CUDA_CC_DP4A 297 return ggml_cuda_mmq_get_config_pascal_dp4a(type, J, fallback); 298 #else 299 return ggml_cuda_mmq_get_config_pascal_older(type, J, fallback); 300 #endif // BLACKWELL_MMA_AVAILABLE 301 #endif // GGML_USE_HIP 302 GGML_UNUSED_VARS(type, J, fallback, prec_src1); 303 } 304 305 static __host__ int ggml_cuda_mmq_get_type(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 306 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).type; 307 } 308 309 static constexpr __device__ int ggml_cuda_mmq_get_type(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 310 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).type; 311 } 312 313 static constexpr __device__ int ggml_cuda_mmq_get_nthreads(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 314 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).nthreads; 315 } 316 317 static constexpr __device__ int ggml_cuda_mmq_get_occupancy(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 318 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).occupancy; 319 } 320 321 static __host__ int ggml_cuda_mmq_get_I(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 322 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).I; 323 } 324 325 static constexpr __device__ int ggml_cuda_mmq_get_I(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 326 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).I; 327 } 328 329 static __host__ int ggml_cuda_mmq_get_J(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 330 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).J; 331 } 332 333 static constexpr __device__ int ggml_cuda_mmq_get_J(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 334 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).J; 335 } 336 337 static __host__ ggml_cuda_mmq_sram_layout ggml_cuda_mmq_get_sram_layout(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 338 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).sram_layout; 339 } 340 341 static constexpr __device__ ggml_cuda_mmq_sram_layout ggml_cuda_mmq_get_sram_layout(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 342 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).sram_layout; 343 } 344 345 static __host__ int ggml_cuda_mmq_get_K_vram(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 346 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).K_vram; 347 } 348 349 static constexpr __device__ int ggml_cuda_mmq_get_K_vram(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 350 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).K_vram; 351 } 352 353 static __host__ bool ggml_cuda_mmq_get_stream_k(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 354 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).stream_k; 355 } 356 357 static constexpr __device__ bool ggml_cuda_mmq_get_stream_k(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 358 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).stream_k; 359 } 360 361 static __host__ int ggml_cuda_mmq_get_fallback(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 362 return ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1).fallback; 363 } 364 365 static constexpr __device__ int ggml_cuda_mmq_get_fallback(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 366 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).fallback; 367 } 368 369 // --------------------------------------------------------------------------------------------- 370 371 static __host__ int ggml_cuda_mmq_get_sram_stride(const ggml_type type, const int J, const bool fallback, const int cc, const ggml_prec prec_src1) { 372 return ggml_cuda_mmq_get_sram_stride(ggml_cuda_mmq_get_sram_layout(type, J, fallback, cc, prec_src1)); 373 } 374 375 static constexpr __device__ int ggml_cuda_mmq_get_sram_stride(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 376 return ggml_cuda_mmq_get_sram_stride(ggml_cuda_mmq_get_sram_layout(type, J, fallback, prec_src1)); 377 } 378 379 static __host__ bool ggml_cuda_mmq_needs_fallback(const int64_t nrows_x) { 380 return nrows_x % 128 != 0; 381 } 382 383 static constexpr __device__ int ggml_cuda_mmq_get_rows_per_warp(ggml_type type, int J, bool fallback, ggml_prec prec_src1) { 384 return ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).rows_per_warp(); 385 } 386 387 #define MMQ_DP4A_TXS_Q4_0 tile_x_sizes{I*MMQ_TILE_NE_K + I, I*MMQ_TILE_NE_K/QI4_0 + I/QI4_0, 0} 388 #define MMQ_DP4A_TXS_Q4_1 tile_x_sizes{I*MMQ_TILE_NE_K + I, I*MMQ_TILE_NE_K/QI4_1 + I/QI4_1, 0} 389 #define MMQ_DP4A_TXS_Q8_0 tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K*2/QI8_0 + I/(QI8_0/2), 0} 390 #define MMQ_DP4A_TXS_Q8_0_16 tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K*4/QI8_0 + I/(QI8_0/4), 0} 391 #define MMQ_DP4A_TXS_Q8_1 tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K*2/QI8_1 + I/(QI8_1/2), 0} 392 #define MMQ_DP4A_TXS_Q2_K tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K + I, 0} 393 #define MMQ_DP4A_TXS_Q3_K tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I, I*MMQ_TILE_NE_K/8 + I/8} 394 #define MMQ_DP4A_TXS_Q4_K tile_x_sizes{I*MMQ_TILE_NE_K + I, I*MMQ_TILE_NE_K/QI4_K, I*MMQ_TILE_NE_K/8 + I/8} 395 #define MMQ_DP4A_TXS_Q5_K tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K/QI5_K + I/QI5_K, I*MMQ_TILE_NE_K/8 + I/8} 396 #define MMQ_DP4A_TXS_Q6_K tile_x_sizes{I*MMQ_TILE_NE_K*2 + I, I*MMQ_TILE_NE_K/QI6_K + I/QI6_K, I*MMQ_TILE_NE_K/8 + I/8} 397 398 static constexpr __host__ __device__ tile_x_sizes mmq_get_dp4a_tile_x_sizes(ggml_type type, int I) { 399 switch (type) { 400 case GGML_TYPE_Q1_0: return MMQ_DP4A_TXS_Q8_0; 401 case GGML_TYPE_Q2_0: return MMQ_DP4A_TXS_Q8_0; 402 case GGML_TYPE_Q4_0: return MMQ_DP4A_TXS_Q4_0; 403 case GGML_TYPE_Q4_1: return MMQ_DP4A_TXS_Q4_1; 404 case GGML_TYPE_Q5_0: return MMQ_DP4A_TXS_Q8_0; 405 case GGML_TYPE_Q5_1: return MMQ_DP4A_TXS_Q8_1; 406 case GGML_TYPE_Q8_0: return MMQ_DP4A_TXS_Q8_0; 407 case GGML_TYPE_MXFP4: return MMQ_DP4A_TXS_Q8_1; 408 case GGML_TYPE_NVFP4: return MMQ_DP4A_TXS_Q8_0_16; 409 case GGML_TYPE_Q2_K: return MMQ_DP4A_TXS_Q2_K; 410 case GGML_TYPE_Q3_K: return MMQ_DP4A_TXS_Q3_K; 411 case GGML_TYPE_Q4_K: return MMQ_DP4A_TXS_Q4_K; 412 case GGML_TYPE_Q5_K: return MMQ_DP4A_TXS_Q5_K; 413 case GGML_TYPE_Q6_K: return MMQ_DP4A_TXS_Q6_K; 414 case GGML_TYPE_IQ2_XXS: return MMQ_DP4A_TXS_Q8_0; 415 case GGML_TYPE_IQ2_XS: return MMQ_DP4A_TXS_Q8_0_16; 416 case GGML_TYPE_IQ2_S: return MMQ_DP4A_TXS_Q8_0_16; 417 case GGML_TYPE_IQ3_XXS: return MMQ_DP4A_TXS_Q8_0; 418 case GGML_TYPE_IQ3_S: return MMQ_DP4A_TXS_Q8_0; 419 case GGML_TYPE_IQ1_S: return MMQ_DP4A_TXS_Q8_0; 420 case GGML_TYPE_IQ4_XS: return MMQ_DP4A_TXS_Q8_0; 421 case GGML_TYPE_IQ4_NL: return MMQ_DP4A_TXS_Q8_0; 422 default: return tile_x_sizes{0, 0, 0}; 423 } 424 } 425 426 // FIXME temporary until all combinations of data types and GPUs can use the MMA data layout 427 static __host__ int ggml_cuda_mmq_get_nbytes_shared_x(const ggml_cuda_mmq_config & config, const int cc) { 428 if (config.use_mma_data_layout(cc)) { 429 return config.I * ggml_cuda_mmq_get_sram_stride(config.sram_layout) * 4; 430 } 431 const tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(config.type, config.I); 432 return (txs.qs + txs.dm + txs.sc) * 4; 433 } 434 435 // ------------------------------------------------------------ 436 437 // Weight types with a fused gate/up/GLU path (see ggml_cuda_mmq_get_util_funcs). 438 static constexpr __host__ __device__ bool ggml_cuda_mmq_fusion_supported(const ggml_type type) { 439 return type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q6_K || type == GGML_TYPE_Q8_0 || type == GGML_TYPE_IQ4_XS; 440 } 441 442 // Fused gate/up/GLU: the weight tile alternates chunks of this many up and gate rows, so rows r and r + 8 of an MMA tile (same thread) pair up. 443 static constexpr int GGML_CUDA_MMQ_FUSION_ROWS = 8; 444 445 // Source of weight tile row i; with has_fusion the up or gate row that the interleaved tile holds there. 446 template 447 static __device__ __forceinline__ const block_t * ggml_cuda_mmq_x_row( 448 const char * __restrict__ x, const char * __restrict__ x_gate, const int kbx0, const int i, const int stride) { 449 if constexpr (has_fusion) { 450 constexpr int R = GGML_CUDA_MMQ_FUSION_ROWS; 451 const char * x_src = (i / R) % 2 == 0 ? x : x_gate; 452 return (const block_t *) x_src + kbx0 + ((i / (2*R))*R + i % R)*stride; 453 } else { 454 GGML_UNUSED(x_gate); 455 return (const block_t *) x + kbx0 + i*stride; 456 } 457 } 458 459 #include "mmq-load-tiles.cuh" 460 #include "mmq-vec-dot.cuh" 461 462 template static __device__ __forceinline__ void ggml_cuda_mmq_write_back_dp4a( 463 const float * __restrict__ sum, const int32_t * __restrict__ ids_dst, float * __restrict__ dst, 464 const float * __restrict__ y_scale, const int stride, const int i_max, const int j_max, 465 const ggml_glu_op glu_op, const float glu_limit) { 466 GGML_UNUSED_VARS(glu_op, glu_limit); 467 468 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 469 constexpr int nwarps = ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) / warp_size; 470 constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, prec_src1); 471 static_assert(I % warp_size == 0, "DP4A MMQ tile height must be a multiple of the physical warp size"); 472 473 const bool y_scale_used = y_scale != nullptr; 474 475 #pragma unroll 476 for (int j0 = 0; j0 < J; j0 += nwarps) { 477 const int j = j0 + threadIdx.y; 478 479 if (j > j_max) { 480 return; 481 } 482 483 #pragma unroll 484 for (int i0 = 0; i0 < I; i0 += warp_size) { 485 const int i = i0 + threadIdx.x; 486 487 if (fallback && i > i_max) { 488 continue; 489 } 490 491 if constexpr (type == GGML_TYPE_NVFP4) { 492 if (y_scale_used) { 493 dst[ids_dst[j]*stride + i] = y_scale[j] * sum[(j0/nwarps) * (I/warp_size) + i0/warp_size]; 494 } else { 495 dst[ids_dst[j]*stride + i] = sum[(j0/nwarps) * (I/warp_size) + i0/warp_size]; 496 } 497 } else { 498 dst[ids_dst[j]*stride + i] = sum[(j0/nwarps) * (I/warp_size) + i0/warp_size]; 499 GGML_UNUSED(y_scale_used); 500 } 501 } 502 } 503 } 504 505 // has_fusion: accumulators l and l + ne/2 hold the up and gate values of one output element, the GLU of the two is written. 506 template 507 static __device__ __forceinline__ void ggml_cuda_mmq_write_back_mma( 508 const float * __restrict__ sum, const int * __restrict__ ids_dst, float * __restrict__ dst, 509 const float * __restrict__ y_scale, const int stride, const int i_max, const int j_max, 510 const ggml_glu_op glu_op, const float glu_limit) { 511 512 #if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 513 typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C; 514 #else 515 typedef tile<16, 8, int> tile_C; 516 #endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) 517 static_assert(!has_fusion || tile_C::I == 2*GGML_CUDA_MMQ_FUSION_ROWS, "each C tile must hold one chunk of up rows and one of gate rows"); 518 519 constexpr int rows_per_warp = ggml_cuda_mmq_get_rows_per_warp(type, J, fallback, prec_src1); 520 constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp. 521 522 const int i0 = (threadIdx.y / ntx) * (ntx*tile_C::I); 523 524 const bool y_scale_used = y_scale != nullptr; 525 526 #pragma unroll 527 for (int j0 = 0; j0 < J; j0 += ntx*tile_C::J) { 528 #pragma unroll 529 for (int n = 0; n < ntx; ++n) { 530 #pragma unroll 531 for (int l = 0; l < (has_fusion ? tile_C::ne/2 : tile_C::ne); ++l) { 532 const int j = j0 + (threadIdx.y % ntx) * tile_C::J + tile_C::get_j(l); 533 534 if (j > j_max) { 535 continue; 536 } 537 538 const int i = (i0 + n*tile_C::I)/(has_fusion ? 2 : 1) + tile_C::get_i(l); 539 540 if (fallback && i > i_max) { 541 continue; 542 } 543 544 if constexpr (has_fusion) { 545 const int sum_index = (j0/tile_C::J + n)*tile_C::ne + l; 546 dst[ids_dst[j]*stride + i] = ggml_cuda_op_glu_single(glu_op, sum[sum_index + tile_C::ne/2], sum[sum_index], glu_limit); 547 } else if constexpr (type == GGML_TYPE_NVFP4) { 548 if (y_scale_used) { 549 dst[ids_dst[j]*stride + i] = y_scale[j] * sum[(j0/tile_C::J + n)*tile_C::ne + l]; 550 } else { 551 dst[ids_dst[j]*stride + i] = sum[(j0/tile_C::J + n)*tile_C::ne + l]; 552 } 553 } else { 554 dst[ids_dst[j]*stride + i] = sum[(j0/tile_C::J + n)*tile_C::ne + l]; 555 GGML_UNUSED(y_scale_used); 556 } 557 } 558 } 559 } 560 561 GGML_UNUSED_VARS(y_scale_used, glu_op, glu_limit); 562 } 563 564 // ------------------------------------------------------------------------------------------------------------------------------------- 565 566 // TODO remove this struct and use ggml_cuda_mmq_sram_layout instead. 567 struct ggml_cuda_mmq_util_funcs { 568 int vdr; 569 ggml_cuda_mmq_load_tiles_t load_tiles; 570 ggml_cuda_mmq_vec_dot_t vec_dot; 571 ggml_cuda_mmq_write_back_t write_back; 572 573 constexpr __host__ __device__ ggml_cuda_mmq_util_funcs( 574 int vdr, ggml_cuda_mmq_load_tiles_t load_tiles, ggml_cuda_mmq_vec_dot_t vec_dot, ggml_cuda_mmq_write_back_t write_back) : 575 vdr(vdr), load_tiles(load_tiles), vec_dot(vec_dot), write_back(write_back) {} 576 }; 577 578 template 579 static constexpr __device__ ggml_cuda_mmq_util_funcs ggml_cuda_mmq_get_util_funcs() { 580 static_assert(!has_fusion || ggml_cuda_mmq_fusion_supported(type), "no fused loader for this type"); 581 if (!ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).use_mma_data_layout()) { 582 switch (type) { 583 case GGML_TYPE_Q1_0: 584 return ggml_cuda_mmq_util_funcs( 585 VDR_Q1_0_Q8_1_MMQ, 586 ggml_cuda_mmq_load_tiles_q1_0, 587 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 588 ggml_cuda_mmq_write_back_dp4a); 589 case GGML_TYPE_Q2_0: 590 return ggml_cuda_mmq_util_funcs( 591 VDR_Q2_0_Q8_1_MMQ, 592 ggml_cuda_mmq_load_tiles_q2_0, 593 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 594 ggml_cuda_mmq_write_back_dp4a); 595 case GGML_TYPE_Q4_0: 596 return ggml_cuda_mmq_util_funcs( 597 VDR_Q4_0_Q8_1_MMQ, 598 ggml_cuda_mmq_load_tiles_q4_0, 599 ggml_cuda_mmq_vec_dot_q4_0_q8_1_dp4a, 600 ggml_cuda_mmq_write_back_dp4a); 601 case GGML_TYPE_Q4_1: 602 return ggml_cuda_mmq_util_funcs( 603 VDR_Q4_1_Q8_1_MMQ, 604 ggml_cuda_mmq_load_tiles_q4_1, 605 ggml_cuda_mmq_vec_dot_q4_1_q8_1_dp4a, 606 ggml_cuda_mmq_write_back_dp4a); 607 case GGML_TYPE_Q5_0: 608 return ggml_cuda_mmq_util_funcs( 609 VDR_Q5_0_Q8_1_MMQ, 610 ggml_cuda_mmq_load_tiles_q5_0, 611 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 612 ggml_cuda_mmq_write_back_dp4a); 613 case GGML_TYPE_Q5_1: 614 return ggml_cuda_mmq_util_funcs( 615 VDR_Q5_1_Q8_1_MMQ, 616 ggml_cuda_mmq_load_tiles_q5_1, 617 ggml_cuda_mmq_vec_dot_q8_1_q8_1_dp4a, 618 ggml_cuda_mmq_write_back_dp4a); 619 case GGML_TYPE_Q8_0: 620 return ggml_cuda_mmq_util_funcs( 621 VDR_Q8_0_Q8_1_MMQ, 622 ggml_cuda_mmq_load_tiles_q8_0, 623 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 624 ggml_cuda_mmq_write_back_dp4a); 625 // --------------------------------------------------------------------------------------------- 626 case GGML_TYPE_Q2_K: 627 return ggml_cuda_mmq_util_funcs( 628 VDR_Q2_K_Q8_1_MMQ, 629 ggml_cuda_mmq_load_tiles_q2_K, 630 ggml_cuda_mmq_vec_dot_q2_K_q8_1_dp4a, 631 ggml_cuda_mmq_write_back_dp4a); 632 case GGML_TYPE_Q3_K: 633 return ggml_cuda_mmq_util_funcs( 634 VDR_Q3_K_Q8_1_MMQ, 635 ggml_cuda_mmq_load_tiles_q3_K, 636 ggml_cuda_mmq_vec_dot_q3_K_q8_1_dp4a, 637 ggml_cuda_mmq_write_back_dp4a); 638 case GGML_TYPE_Q4_K: 639 return ggml_cuda_mmq_util_funcs( 640 VDR_Q4_K_Q8_1_MMQ, 641 ggml_cuda_mmq_load_tiles_q4_K, 642 ggml_cuda_mmq_vec_dot_q4_K_q8_1_dp4a, 643 ggml_cuda_mmq_write_back_dp4a); 644 case GGML_TYPE_Q5_K: 645 return ggml_cuda_mmq_util_funcs( 646 VDR_Q5_K_Q8_1_MMQ, 647 ggml_cuda_mmq_load_tiles_q5_K, 648 ggml_cuda_mmq_vec_dot_q5_K_q8_1_dp4a, 649 ggml_cuda_mmq_write_back_dp4a); 650 case GGML_TYPE_Q6_K: 651 return ggml_cuda_mmq_util_funcs( 652 VDR_Q6_K_Q8_1_MMQ, 653 ggml_cuda_mmq_load_tiles_q6_K, 654 ggml_cuda_mmq_vec_dot_q6_K_q8_1_dp4a, 655 ggml_cuda_mmq_write_back_dp4a); 656 // --------------------------------------------------------------------------------------------- 657 case GGML_TYPE_IQ1_S: 658 return ggml_cuda_mmq_util_funcs( 659 VDR_IQ1_S_Q8_1_MMQ, 660 ggml_cuda_mmq_load_tiles_iq1_s, 661 ggml_cuda_mmq_vec_dot_q8_1_q8_1_dp4a, 662 ggml_cuda_mmq_write_back_dp4a); 663 case GGML_TYPE_IQ2_XXS: 664 return ggml_cuda_mmq_util_funcs( 665 VDR_IQ2_XXS_Q8_1_MMQ, 666 ggml_cuda_mmq_load_tiles_iq2_xxs, 667 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 668 ggml_cuda_mmq_write_back_dp4a); 669 case GGML_TYPE_IQ2_XS: 670 return ggml_cuda_mmq_util_funcs( 671 VDR_IQ2_XS_Q8_1_MMQ, 672 ggml_cuda_mmq_load_tiles_iq2_xs, 673 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_dp4a, 674 ggml_cuda_mmq_write_back_dp4a); 675 case GGML_TYPE_IQ2_S: 676 return ggml_cuda_mmq_util_funcs( 677 VDR_IQ2_S_Q8_1_MMQ, 678 ggml_cuda_mmq_load_tiles_iq2_s, 679 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_dp4a, 680 ggml_cuda_mmq_write_back_dp4a); 681 case GGML_TYPE_IQ3_XXS: 682 return ggml_cuda_mmq_util_funcs( 683 VDR_IQ3_XXS_Q8_1_MMQ, 684 ggml_cuda_mmq_load_tiles_iq3_xxs, 685 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 686 ggml_cuda_mmq_write_back_dp4a); 687 case GGML_TYPE_IQ3_S: 688 return ggml_cuda_mmq_util_funcs( 689 VDR_IQ3_S_Q8_1_MMQ, 690 ggml_cuda_mmq_load_tiles_iq3_s, 691 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 692 ggml_cuda_mmq_write_back_dp4a); 693 case GGML_TYPE_IQ4_XS: 694 return ggml_cuda_mmq_util_funcs( 695 VDR_IQ4_XS_Q8_1_MMQ, 696 ggml_cuda_mmq_load_tiles_iq4_xs, 697 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 698 ggml_cuda_mmq_write_back_dp4a); 699 case GGML_TYPE_IQ4_NL: 700 return ggml_cuda_mmq_util_funcs( 701 VDR_IQ4_NL_Q8_1_MMQ, 702 ggml_cuda_mmq_load_tiles_iq4_nl, 703 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 704 ggml_cuda_mmq_write_back_dp4a); 705 // --------------------------------------------------------------------------------------------- 706 case GGML_TYPE_MXFP4: 707 return ggml_cuda_mmq_util_funcs( 708 VDR_MXFP4_Q8_1_MMQ, 709 ggml_cuda_mmq_load_tiles_mxfp4, 710 ggml_cuda_mmq_vec_dot_q8_0_q8_1_dp4a, 711 ggml_cuda_mmq_write_back_dp4a); 712 case GGML_TYPE_NVFP4: 713 return ggml_cuda_mmq_util_funcs( 714 VDR_NVFP4_Q8_1_MMQ, 715 ggml_cuda_mmq_load_tiles_nvfp4, 716 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_dp4a, 717 ggml_cuda_mmq_write_back_dp4a); 718 default: 719 return ggml_cuda_mmq_util_funcs(1, nullptr, nullptr, nullptr); 720 } 721 } 722 723 // --------------------------------------------------------------------------------------------- 724 725 #ifdef BLACKWELL_MMA_AVAILABLE 726 switch (type) { 727 case GGML_TYPE_MXFP4: 728 if (prec_src1 == GGML_PREC_Q4) { 729 return ggml_cuda_mmq_util_funcs( 730 -1, 731 ggml_cuda_mmq_load_tiles_mxfp4_fp4, 732 ggml_cuda_mmq_vec_dot_fp4_fp4_mma, 733 ggml_cuda_mmq_write_back_mma); 734 } 735 break; 736 case GGML_TYPE_NVFP4: 737 if (prec_src1 == GGML_PREC_Q4) { 738 return ggml_cuda_mmq_util_funcs( 739 -1, 740 ggml_cuda_mmq_load_tiles_nvfp4_nvfp4, 741 ggml_cuda_mmq_vec_dot_fp4_fp4_mma, 742 ggml_cuda_mmq_write_back_mma); 743 } 744 break; 745 default: 746 break; 747 } 748 #endif // BLACKWELL_MMA_AVAILABLE 749 750 // --------------------------------------------------------------------------------------------- 751 752 switch (type) { 753 case GGML_TYPE_Q1_0: 754 return ggml_cuda_mmq_util_funcs( 755 -1, 756 ggml_cuda_mmq_load_tiles_q1_0, 757 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 758 ggml_cuda_mmq_write_back_mma); 759 case GGML_TYPE_Q2_0: 760 return ggml_cuda_mmq_util_funcs( 761 -1, 762 ggml_cuda_mmq_load_tiles_q2_0, 763 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 764 ggml_cuda_mmq_write_back_mma); 765 case GGML_TYPE_Q4_0: 766 return ggml_cuda_mmq_util_funcs( 767 -1, 768 ggml_cuda_mmq_load_tiles_q4_0, 769 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 770 ggml_cuda_mmq_write_back_mma); 771 case GGML_TYPE_Q4_1: 772 return ggml_cuda_mmq_util_funcs( 773 -1, 774 ggml_cuda_mmq_load_tiles_q4_1, 775 ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma, 776 ggml_cuda_mmq_write_back_mma); 777 case GGML_TYPE_Q5_0: 778 return ggml_cuda_mmq_util_funcs( 779 -1, 780 ggml_cuda_mmq_load_tiles_q5_0, 781 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 782 ggml_cuda_mmq_write_back_mma); 783 case GGML_TYPE_Q5_1: 784 return ggml_cuda_mmq_util_funcs( 785 -1, 786 ggml_cuda_mmq_load_tiles_q5_1, 787 ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma, 788 ggml_cuda_mmq_write_back_mma); 789 case GGML_TYPE_Q8_0: 790 return ggml_cuda_mmq_util_funcs( 791 -1, 792 ggml_cuda_mmq_load_tiles_q8_0, 793 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 794 ggml_cuda_mmq_write_back_mma); 795 // --------------------------------------------------------------------------------------------- 796 case GGML_TYPE_Q2_K: 797 return ggml_cuda_mmq_util_funcs( 798 -1, 799 ggml_cuda_mmq_load_tiles_q2_K, 800 ggml_cuda_mmq_vec_dot_q2_K_q8_1_mma, 801 ggml_cuda_mmq_write_back_mma); 802 case GGML_TYPE_Q3_K: 803 return ggml_cuda_mmq_util_funcs( 804 -1, 805 ggml_cuda_mmq_load_tiles_q3_K, 806 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_mma, 807 ggml_cuda_mmq_write_back_mma); 808 case GGML_TYPE_Q4_K: 809 return ggml_cuda_mmq_util_funcs( 810 -1, 811 ggml_cuda_mmq_load_tiles_q4_K, 812 ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma, 813 ggml_cuda_mmq_write_back_mma); 814 case GGML_TYPE_Q5_K: 815 return ggml_cuda_mmq_util_funcs( 816 -1, 817 ggml_cuda_mmq_load_tiles_q5_K, 818 ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma, 819 ggml_cuda_mmq_write_back_mma); 820 case GGML_TYPE_Q6_K: 821 return ggml_cuda_mmq_util_funcs( 822 -1, 823 ggml_cuda_mmq_load_tiles_q6_K, 824 ggml_cuda_mmq_vec_dot_q6_K_q8_1_mma, 825 ggml_cuda_mmq_write_back_mma); 826 // --------------------------------------------------------------------------------------------- 827 case GGML_TYPE_IQ1_S: 828 return ggml_cuda_mmq_util_funcs( 829 -1, 830 ggml_cuda_mmq_load_tiles_iq1_s, 831 ggml_cuda_mmq_vec_dot_q8_1_q8_1_mma, 832 ggml_cuda_mmq_write_back_mma); 833 case GGML_TYPE_IQ2_XXS: 834 return ggml_cuda_mmq_util_funcs( 835 -1, 836 ggml_cuda_mmq_load_tiles_iq2_xxs, 837 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 838 ggml_cuda_mmq_write_back_mma); 839 case GGML_TYPE_IQ2_XS: 840 return ggml_cuda_mmq_util_funcs( 841 -1, 842 ggml_cuda_mmq_load_tiles_iq2_xs, 843 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_mma, 844 ggml_cuda_mmq_write_back_mma); 845 case GGML_TYPE_IQ2_S: 846 return ggml_cuda_mmq_util_funcs( 847 -1, 848 ggml_cuda_mmq_load_tiles_iq2_s, 849 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_mma, 850 ggml_cuda_mmq_write_back_mma); 851 case GGML_TYPE_IQ3_XXS: 852 return ggml_cuda_mmq_util_funcs( 853 -1, 854 ggml_cuda_mmq_load_tiles_iq3_xxs, 855 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 856 ggml_cuda_mmq_write_back_mma); 857 case GGML_TYPE_IQ3_S: 858 return ggml_cuda_mmq_util_funcs( 859 -1, 860 ggml_cuda_mmq_load_tiles_iq3_s, 861 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 862 ggml_cuda_mmq_write_back_mma); 863 case GGML_TYPE_IQ4_XS: 864 return ggml_cuda_mmq_util_funcs( 865 -1, 866 ggml_cuda_mmq_load_tiles_iq4_xs, 867 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 868 ggml_cuda_mmq_write_back_mma); 869 case GGML_TYPE_IQ4_NL: 870 return ggml_cuda_mmq_util_funcs( 871 -1, 872 ggml_cuda_mmq_load_tiles_iq4_nl, 873 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 874 ggml_cuda_mmq_write_back_mma); 875 // --------------------------------------------------------------------------------------------- 876 case GGML_TYPE_MXFP4: 877 return ggml_cuda_mmq_util_funcs( 878 -1, 879 ggml_cuda_mmq_load_tiles_mxfp4, 880 ggml_cuda_mmq_vec_dot_q8_0_q8_1_mma, 881 ggml_cuda_mmq_write_back_mma); 882 case GGML_TYPE_NVFP4: 883 return ggml_cuda_mmq_util_funcs( 884 -1, 885 ggml_cuda_mmq_load_tiles_nvfp4, 886 ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_mma, 887 ggml_cuda_mmq_write_back_mma); 888 default: 889 return ggml_cuda_mmq_util_funcs(1, nullptr, nullptr, nullptr); 890 } 891 } 892 893 template 894 static constexpr __device__ int ggml_cuda_mmq_get_vdr() { 895 return ggml_cuda_mmq_get_util_funcs().vdr; 896 } 897 898 template 899 static constexpr __device__ ggml_cuda_mmq_load_tiles_t ggml_cuda_mmq_get_load_tiles() { 900 return ggml_cuda_mmq_get_util_funcs().load_tiles; 901 } 902 903 template 904 static constexpr __device__ ggml_cuda_mmq_vec_dot_t ggml_cuda_mmq_get_vec_dot() { 905 return ggml_cuda_mmq_get_util_funcs().vec_dot; 906 } 907 908 template 909 static constexpr __device__ ggml_cuda_mmq_write_back_t ggml_cuda_mmq_get_write_back() { 910 return ggml_cuda_mmq_get_util_funcs().write_back; 911 } 912 913 // --------------------------------------------------------------------------------------------- 914 915 // has_fusion: x and x_gate are the up and gate weights. 916 template 917 static __device__ __forceinline__ void mul_mat_q_process_tile( 918 const char * __restrict__ x, const char * __restrict__ x_gate, const int offset_x, const int * __restrict__ y, 919 const int * __restrict__ ids_dst, float * __restrict__ dst, float * __restrict__ tmp_fixup, 920 const float * __restrict__ y_scale, const ggml_glu_op glu_op, const float glu_limit, 921 const int stride_row_x, const int ncols_y, const int stride_col_dst, 922 const int tile_x_max_i, const int tile_y_max_j, const int kb0_start, const int kb0_stop) { 923 static_assert(!(fixup && has_fusion), "the fused gate/up path does not split K"); 924 static_assert(!(fallback && has_fusion), "the fused gate/up path needs nrows_x % 128 == 0"); 925 926 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 927 constexpr int nwarps = ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) / warp_size; 928 constexpr int qk = ggml_cuda_type_traits::qk; 929 constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, prec_src1); 930 constexpr ggml_cuda_mmq_load_tiles_t load_tiles = ggml_cuda_mmq_get_load_tiles(); 931 constexpr ggml_cuda_mmq_vec_dot_t vec_dot = ggml_cuda_mmq_get_vec_dot(); 932 constexpr ggml_cuda_mmq_write_back_t write_back = ggml_cuda_mmq_get_write_back(); 933 934 extern __shared__ int data_mul_mat_q[]; 935 int * tile_y = data_mul_mat_q + J; 936 int * tile_x = tile_y + GGML_PAD(J*MMQ_TILE_Y_K, nwarps*warp_size); 937 938 #if defined(BLACKWELL_MMA_AVAILABLE) 939 // FP4 tile stores 8 blocks. src1 above Q4 uses the generic 940 // Q8_1 tile layout instead of the packed FP4 tile. 941 constexpr int ne_block = ((type == GGML_TYPE_MXFP4 || type == GGML_TYPE_NVFP4) && prec_src1 == GGML_PREC_Q4) ? 942 QK_FP4_MMQ : QK8_1_MMQ; 943 #else 944 constexpr int ne_block = QK8_1_MMQ; 945 #endif // defined(BLACKWELL_MMA_AVAILABLE) 946 947 constexpr int ITER_K = ggml_cuda_mmq_get_K_vram(type, J, fallback, prec_src1); 948 constexpr int blocks_per_iter = ITER_K / qk; 949 950 float sum[J*I / (nwarps*warp_size)] = {0.0f}; 951 952 constexpr int sz = sizeof(block_q8_1_mmq) / sizeof(int); 953 954 for (int kb0 = kb0_start; kb0 < kb0_stop; kb0 += blocks_per_iter) { 955 load_tiles(x, x_gate, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x); 956 { 957 const int * by0 = y + ncols_y * (kb0 * qk / ne_block) * sz; 958 #pragma unroll 959 for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) { 960 int l = l0 + threadIdx.y*warp_size + threadIdx.x; 961 962 tile_y[l] = by0[l]; 963 } 964 } 965 966 __syncthreads(); 967 968 vec_dot(tile_x, tile_y, sum, 0); 969 970 __syncthreads(); 971 972 { 973 const int * by0 = y + ncols_y * ((kb0 * qk / ne_block) * sz + sz); 974 #pragma unroll 975 for (int l0 = 0; l0 < J * MMQ_TILE_Y_K; l0 += nwarps * warp_size) { 976 int l = l0 + threadIdx.y*warp_size + threadIdx.x; 977 978 tile_y[l] = by0[l]; 979 } 980 } 981 982 __syncthreads(); 983 984 vec_dot(tile_x, tile_y, sum, MMQ_TILE_NE_K); 985 986 __syncthreads(); 987 } 988 989 if (fixup) { 990 write_back(sum, ids_dst, tmp_fixup + blockIdx.x*(J*I), y_scale, I, I, J, glu_op, glu_limit); 991 } else { 992 write_back(sum, ids_dst, dst, y_scale, stride_col_dst, tile_x_max_i, tile_y_max_j, glu_op, glu_limit); 993 } 994 } 995 996 997 // The mul_mat_q kernel implements "stream-k" work partitioning as described in https://arxiv.org/abs/2301.03598 998 999 template 1000 __launch_bounds__(ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1), ggml_cuda_mmq_get_occupancy(type, J, fallback, prec_src1)) 1001 static __global__ void mul_mat_q( 1002 const char * __restrict__ x, const char * __restrict__ x_gate, const int * __restrict__ y, const int32_t * __restrict__ ids_dst, 1003 const int32_t * __restrict__ expert_bounds, float * __restrict__ dst, float * __restrict__ tmp_fixup, 1004 const float * __restrict__ y_scale, const ggml_glu_op glu_op, const float glu_limit, 1005 const uint3 blocks_per_ne00, const int nrows_x, const int ncols_dst, const int stride_row_x, const int ncols_y, const int stride_col_dst, 1006 const uint3 channel_ratio, const uint3 nchannels_y, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst, 1007 const uint3 sample_ratio, const uint3 nsamples_y, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst, 1008 const uint3 ntx) { 1009 1010 #ifdef TURING_MMA_AVAILABLE 1011 constexpr bool fusion_available = true; 1012 #else 1013 constexpr bool fusion_available = false; 1014 #endif // TURING_MMA_AVAILABLE 1015 1016 // Skip unused template specializations for faster compilation: 1017 if (ggml_cuda_mmq_get_config(type, J, fallback, prec_src1).type == GGML_TYPE_COUNT || (has_fusion && !fusion_available)) { 1018 NO_DEVICE_CODE; 1019 return; 1020 } 1021 1022 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 1023 constexpr int nwarps = ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) / warp_size; 1024 constexpr int qk = ggml_cuda_type_traits::qk; 1025 constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, prec_src1); 1026 1027 const uint32_t nty = (nrows_x + I - 1) / I; // Number of tiles y 1028 1029 // Initialize the ids for writing back data with just the index. 1030 // For regular matrix multiplications this is never changed. 1031 // For MoE the correct indices are loaded from ids_dst. 1032 extern __shared__ int ids_dst_shared[]; // Stored at beginning of shared memory. 1033 #pragma unroll 1034 for (int j0 = 0; j0 < J; j0 += nwarps*warp_size) { 1035 const int j = j0 + threadIdx.y*warp_size + threadIdx.x; 1036 1037 if (j0 + nwarps*warp_size > J && j >= J) { 1038 break; 1039 } 1040 1041 ids_dst_shared[j] = j; 1042 } 1043 __syncthreads(); 1044 1045 // The fused kernel needs the whole K range of a tile in one block, so it always uses tiling. 1046 if constexpr (has_fusion || !ggml_cuda_mmq_get_stream_k(type, J, fallback, prec_src1)) { 1047 // The fused kernel writes the GLU of I/2 up rows and I/2 gate rows per tile. 1048 constexpr int I_dst = has_fusion ? I/2 : I; 1049 1050 const uint2 tmp2 = fast_div_modulo(blockIdx.z, nchannels_y); 1051 const int wt = tmp2.x; 1052 const int zt = tmp2.y; 1053 const int jt = blockIdx.x; 1054 const int it = blockIdx.y; 1055 1056 // Defaults for regular matrix multiplication: 1057 int col_low = 0; 1058 int col_high = ncols_dst; 1059 int col_diff = ncols_dst; 1060 int offset_y = wt*stride_sample_y + zt*stride_channel_y; 1061 int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst; 1062 int offset_y_scale; 1063 if constexpr (type == GGML_TYPE_NVFP4) { 1064 offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y; 1065 } else { 1066 GGML_UNUSED(offset_y_scale); 1067 } 1068 1069 if (ids_dst) { 1070 col_low = expert_bounds[zt + 0]; 1071 col_high = expert_bounds[zt + 1]; 1072 col_diff = col_high - col_low; 1073 1074 offset_y = 0; 1075 offset_dst = 0; 1076 if constexpr (type == GGML_TYPE_NVFP4) { 1077 offset_y_scale = 0; 1078 } 1079 1080 if (jt*J >= col_diff) { 1081 return; 1082 } 1083 1084 // __syncthreads(); // There is no previous tile that could cause a race condition. 1085 #pragma unroll 1086 for (int j0 = 0; j0 < J; j0 += nwarps*warp_size) { 1087 const int j = j0 + threadIdx.y*warp_size + threadIdx.x; 1088 1089 if (j0 + nwarps*warp_size > J && j >= J) { 1090 break; 1091 } 1092 1093 ids_dst_shared[j] = ids_dst[col_low + jt*J + j]; 1094 } 1095 __syncthreads(); 1096 } 1097 1098 offset_y += (col_low + jt*J)*(sizeof(block_q8_1_mmq)/sizeof(int)); 1099 offset_dst += it*I_dst; 1100 const float * y_scale_tile = nullptr; 1101 if constexpr (type == GGML_TYPE_NVFP4) { 1102 offset_y_scale += col_low + jt*J; 1103 y_scale_tile = y_scale ? y_scale + offset_y_scale : nullptr; 1104 } 1105 1106 const int tile_x_max_i = nrows_x - it*I_dst - 1; 1107 const int tile_y_max_j = col_diff - jt*J - 1; 1108 1109 const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + fastdiv(zt, channel_ratio)*stride_channel_x + it*I_dst*stride_row_x; 1110 1111 constexpr bool fixup = false; 1112 mul_mat_q_process_tile 1113 (x, x_gate, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, y_scale_tile, 1114 glu_op, glu_limit, stride_row_x, ncols_y, stride_col_dst, 1115 tile_x_max_i, tile_y_max_j, 0, blocks_per_ne00.z); 1116 return; 1117 } 1118 1119 constexpr int ITER_K = ggml_cuda_mmq_get_K_vram(type, J, fallback, prec_src1); 1120 constexpr int blocks_per_iter = ITER_K / qk; 1121 1122 // kbc == k block continuous, current index in continuous ijk space. 1123 int kbc = int64_t(blockIdx.x) *(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x; 1124 int kbc_stop = int64_t(blockIdx.x + 1)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x; 1125 1126 kbc -= fastmodulo(kbc, blocks_per_ne00) % blocks_per_iter; 1127 kbc_stop -= fastmodulo(kbc_stop, blocks_per_ne00) % blocks_per_iter; 1128 1129 // kb0 == k index when doing the matrix multiplication for an output tile. 1130 int kb0_start = fastmodulo(kbc, blocks_per_ne00); 1131 int kb0_stop = min(blocks_per_ne00.z, uint32_t(kb0_start + kbc_stop - kbc)); 1132 while (kbc < kbc_stop && kb0_stop == int(blocks_per_ne00.z)) { 1133 int tmp = fastdiv(kbc, blocks_per_ne00); 1134 uint2 tmp2 = fast_div_modulo(tmp, ntx); 1135 const int jt = tmp2.y; 1136 tmp = tmp2.x; 1137 tmp2 = fast_div_modulo(tmp, nchannels_y); 1138 const int zt = tmp2.y; 1139 tmp = tmp2.x; 1140 tmp2 = fast_div_modulo(tmp, nsamples_y); 1141 const int wt = tmp2.y; 1142 const int it = tmp2.x; 1143 1144 // Defaults for regular matrix multiplication: 1145 int col_low = 0; 1146 int col_high = ncols_dst; 1147 int col_diff = ncols_dst; 1148 int offset_y = wt*stride_sample_y + zt*stride_channel_y; 1149 int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst; 1150 int offset_y_scale; 1151 if constexpr (type == GGML_TYPE_NVFP4) { 1152 offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y; 1153 } else { 1154 GGML_UNUSED(offset_y_scale); 1155 } 1156 1157 if (ids_dst) { 1158 col_low = expert_bounds[zt + 0]; 1159 col_high = expert_bounds[zt + 1]; 1160 col_diff = col_high - col_low; 1161 1162 offset_y = 0; 1163 offset_dst = 0; 1164 if constexpr (type == GGML_TYPE_NVFP4) { 1165 offset_y_scale = 0; 1166 } 1167 1168 if (jt*J >= col_diff) { 1169 kbc += blocks_per_ne00.z; 1170 kbc -= fastmodulo(kbc, blocks_per_ne00); 1171 1172 kb0_start = 0; 1173 kb0_stop = min(blocks_per_ne00.z, uint32_t(kbc_stop - kbc)); 1174 1175 continue; 1176 } 1177 1178 __syncthreads(); 1179 #pragma unroll 1180 for (int j0 = 0; j0 < J; j0 += nwarps*warp_size) { 1181 const int j = j0 + threadIdx.y*warp_size + threadIdx.x; 1182 1183 if (j0 + nwarps*warp_size > J && j >= J) { 1184 break; 1185 } 1186 1187 ids_dst_shared[j] = ids_dst[col_low + jt*J + j]; 1188 } 1189 __syncthreads(); 1190 } 1191 1192 offset_y += (col_low + jt * J) * (sizeof(block_q8_1_mmq) / sizeof(int)); 1193 offset_dst += it*I; 1194 const float * y_scale_tile = nullptr; 1195 if constexpr (type == GGML_TYPE_NVFP4) { 1196 offset_y_scale += col_low + jt * J; 1197 y_scale_tile = y_scale ? y_scale + offset_y_scale : nullptr; 1198 } 1199 1200 const int tile_x_max_i = nrows_x - it*I - 1; 1201 const int tile_y_max_j = col_diff - jt*J - 1; 1202 1203 const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + fastdiv(zt, channel_ratio)*stride_channel_x + it*I*stride_row_x; 1204 1205 constexpr bool fixup = false; // All but (potentially) the last iterations write their data to dst rather than the fixup buffer. 1206 mul_mat_q_process_tile 1207 (x, x_gate, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, y_scale_tile, 1208 glu_op, glu_limit, stride_row_x, ncols_y, stride_col_dst, 1209 tile_x_max_i, tile_y_max_j, kb0_start, kb0_stop); 1210 1211 kbc += blocks_per_ne00.z; 1212 kbc -= fastmodulo(kbc, blocks_per_ne00); 1213 1214 kb0_start = 0; 1215 kb0_stop = min(blocks_per_ne00.z, uint32_t(kbc_stop - kbc)); 1216 } 1217 1218 if (kbc >= kbc_stop) { 1219 return; 1220 } 1221 1222 int tmp = fastdiv(kbc, blocks_per_ne00); 1223 uint2 tmp2 = fast_div_modulo(tmp, ntx); 1224 const int jt = tmp2.y; 1225 tmp = tmp2.x; 1226 tmp2 = fast_div_modulo(tmp, nchannels_y); 1227 const int zt = tmp2.y; 1228 tmp = tmp2.x; 1229 tmp2 = fast_div_modulo(tmp, nsamples_y); 1230 const int wt = tmp2.y; 1231 const int it = tmp2.x; 1232 1233 // Defaults for regular matrix multiplication: 1234 int col_low = 0; 1235 int col_high = ncols_dst; 1236 int col_diff = ncols_dst; 1237 int offset_y = wt*stride_sample_y + zt*stride_channel_y; 1238 int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst; 1239 int offset_y_scale; 1240 if constexpr (type == GGML_TYPE_NVFP4) { 1241 offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y; 1242 } else { 1243 GGML_UNUSED(offset_y_scale); 1244 } 1245 1246 if (ids_dst) { 1247 col_low = expert_bounds[zt + 0]; 1248 col_high = expert_bounds[zt + 1]; 1249 col_diff = col_high - col_low; 1250 1251 offset_y = 0; 1252 offset_dst = 0; 1253 if constexpr (type == GGML_TYPE_NVFP4) { 1254 offset_y_scale = 0; 1255 } 1256 1257 if (jt*J >= col_diff) { 1258 return; 1259 } 1260 1261 // The memory layout for the fixup buffer is always contiguous, therefore reset ids: 1262 __syncthreads(); 1263 #pragma unroll 1264 for (int j0 = 0; j0 < J; j0 += nwarps*warp_size) { 1265 const int j = j0 + threadIdx.y*warp_size + threadIdx.x; 1266 1267 if (j0 + nwarps*warp_size > J && j >= J) { 1268 break; 1269 } 1270 1271 ids_dst_shared[j] = j; 1272 } 1273 __syncthreads(); 1274 } 1275 1276 offset_y += (col_low + jt * J) * (sizeof(block_q8_1_mmq) / sizeof(int)); 1277 offset_dst += it*I; 1278 const float * y_scale_tile = nullptr; 1279 if constexpr (type == GGML_TYPE_NVFP4) { 1280 offset_y_scale += col_low + jt * J; 1281 y_scale_tile = y_scale ? y_scale + offset_y_scale : nullptr; 1282 } 1283 1284 const int tile_x_max_i = nrows_x - it*I - 1; 1285 const int tile_y_max_j = col_diff - jt*J - 1; 1286 1287 const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + fastdiv(zt, channel_ratio)*stride_channel_x + it*I*stride_row_x; 1288 1289 constexpr bool fixup = true; // Last index writes its data to fixup buffer to avoid data races with other blocks. 1290 mul_mat_q_process_tile 1291 (x, x_gate, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, y_scale_tile, 1292 glu_op, glu_limit, stride_row_x, ncols_y, stride_col_dst, 1293 tile_x_max_i, tile_y_max_j, kb0_start, kb0_stop); 1294 } 1295 1296 template 1297 __launch_bounds__(ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1)/2, 1) 1298 static __global__ void mul_mat_q_stream_k_fixup( 1299 const int32_t * __restrict__ ids_dst, const int32_t * __restrict__ expert_bounds, float * __restrict__ dst, 1300 float * __restrict__ tmp_last_tile, const uint3 blocks_per_ne00, const int nrows_x, const int ncols_dst, 1301 const int stride_col_dst, const uint3 nchannels_y, const int stride_channel_dst, const uint3 nsamples_y, 1302 const int stride_sample_dst, const uint3 ntx) { 1303 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 1304 constexpr int nwarps = (ggml_cuda_mmq_get_nthreads(type, J, fallback, prec_src1) / 2) / warp_size; 1305 constexpr int I = ggml_cuda_mmq_get_I(type, J, fallback, prec_src1); 1306 constexpr int qk = ggml_cuda_type_traits::qk; 1307 constexpr int ITER_K = ggml_cuda_mmq_get_K_vram(type, J, fallback, prec_src1); 1308 constexpr int blocks_per_iter = ITER_K / qk; 1309 1310 float sum[J / nwarps] = {0.0f}; 1311 const int i = blockIdx.y*warp_size + threadIdx.x; 1312 1313 const int nty = (nrows_x + I - 1) / I; 1314 1315 const int bidx0 = blockIdx.x; 1316 1317 // kbc == k block continuous, current index in continuous ijk space. 1318 int kbc0 = int64_t(blockIdx.x) *(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x; 1319 int kbc0_stop = int64_t(blockIdx.x + 1)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x; 1320 1321 kbc0 -= fastmodulo(kbc0, blocks_per_ne00) % blocks_per_iter; 1322 kbc0_stop -= fastmodulo(kbc0_stop, blocks_per_ne00) % blocks_per_iter; 1323 1324 const bool did_not_have_any_data = kbc0 == kbc0_stop; 1325 const bool wrote_beginning_of_tile = fastmodulo(kbc0, blocks_per_ne00) == 0; 1326 const bool did_not_write_last = fastdiv(kbc0, blocks_per_ne00) == fastdiv(kbc0_stop, blocks_per_ne00) && fastmodulo(kbc0_stop, blocks_per_ne00) != 0; 1327 if (did_not_have_any_data || wrote_beginning_of_tile || did_not_write_last) { 1328 return; 1329 } 1330 1331 bool any_fixup = false; 1332 1333 // Iterate over previous blocks and sum up partial sums written to fixup buffer. 1334 // All CUDA blocks that get here must have a previous block that needs a fixup. 1335 int bidx = bidx0 - 1; 1336 int kbc_stop = kbc0; 1337 while(true) { 1338 int kbc = int64_t(bidx)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x; 1339 kbc -= fastmodulo(kbc, blocks_per_ne00) % blocks_per_iter; 1340 1341 if (kbc == kbc_stop) { // Did not have any data. 1342 bidx--; 1343 kbc_stop = kbc; 1344 continue; 1345 } 1346 1347 any_fixup = true; 1348 1349 1350 #pragma unroll 1351 for (int j0 = 0; j0 < J; j0 += nwarps) { 1352 const int j = j0 + threadIdx.y; 1353 1354 sum[j0/nwarps] += tmp_last_tile[bidx*(J*I) + j*I + i]; 1355 } 1356 1357 // If this block started in a previous tile we are done and don't need to combine additional partial results. 1358 if (fastmodulo(kbc, blocks_per_ne00) == 0 || fastdiv(kbc, blocks_per_ne00) < fastdiv(kbc0, blocks_per_ne00)) { 1359 break; 1360 } 1361 bidx--; 1362 kbc_stop = kbc; 1363 } 1364 1365 if (!any_fixup) { 1366 return; 1367 } 1368 1369 int tmp = fastdiv(kbc0, blocks_per_ne00); 1370 uint2 tmp2 = fast_div_modulo(tmp, ntx); 1371 const int jt = tmp2.y; 1372 tmp = tmp2.x; 1373 tmp2 = fast_div_modulo(tmp, nchannels_y); 1374 const int zt = tmp2.y; 1375 tmp = tmp2.x; 1376 tmp2 = fast_div_modulo(tmp, nsamples_y); 1377 const int wt = tmp2.y; 1378 const int it = tmp2.x; 1379 1380 if (!ids_dst) { 1381 const int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst + it*I; 1382 dst += offset_dst; 1383 1384 const int i_max = nrows_x - it*I - 1; 1385 const int j_max = ncols_dst - jt*J - 1; 1386 if (fallback && i > i_max) { 1387 return; 1388 } 1389 1390 #pragma unroll 1391 for (int j0 = 0; j0 < J; j0 += nwarps) { 1392 const int j = j0 + threadIdx.y; 1393 1394 if (j > j_max) { 1395 return; 1396 } 1397 1398 dst[j*stride_col_dst + i] += sum[j0/nwarps]; 1399 } 1400 return; 1401 } 1402 1403 __shared__ int ids_dst_shared[J]; 1404 const int col_low = expert_bounds[zt + 0]; 1405 const int col_high = expert_bounds[zt + 1]; 1406 const int col_diff = col_high - col_low; 1407 1408 for (int j = threadIdx.y*warp_size + threadIdx.x; j < J; j += nwarps*warp_size) { 1409 ids_dst_shared[j] = ids_dst[col_low + jt*J + j]; 1410 } 1411 __syncthreads(); 1412 1413 const int offset_dst = it*I; 1414 dst += offset_dst; 1415 1416 const int i_max = nrows_x - it*I - 1; 1417 const int j_max = col_diff - jt*J - 1; 1418 if (fallback && i > i_max) { 1419 return; 1420 } 1421 1422 #pragma unroll 1423 for (int j0 = 0; j0 < J; j0 += nwarps) { 1424 const int j = j0 + threadIdx.y; 1425 1426 if (j > j_max) { 1427 return; 1428 } 1429 1430 dst[ids_dst_shared[j]*stride_col_dst + i] += sum[j0/nwarps]; 1431 } 1432 } 1433 1434 struct mmq_args { 1435 const char * x; ggml_type type_x; const int * y; const int32_t * ids_dst; const int32_t * expert_bounds; float * dst; 1436 const float * y_scale; 1437 int64_t ncols_x; int64_t nrows_x; int64_t ncols_dst; int64_t stride_row_x; int64_t ncols_y; int64_t nrows_dst; 1438 int64_t nchannels_x; int64_t nchannels_y; int64_t stride_channel_x; int64_t stride_channel_y; int64_t stride_channel_dst; 1439 int64_t nsamples_x; int64_t nsamples_y; int64_t stride_sample_x; int64_t stride_sample_y; int64_t stride_sample_dst; 1440 int64_t ncols_max; 1441 int J_best; // Tile width in ne11(dense)/ne12(MoE) direction to use for optimal performance. 1442 ggml_cuda_mm_fusion_args_device fusion = {}; // gate != nullptr selects the fused gate/up/GLU path 1443 }; 1444 1445 static size_t mmq_get_nbytes_shared(const ggml_cuda_mmq_config & config, const int cc) { 1446 const size_t nbs_ids = config.J*sizeof(int); 1447 const size_t nbs_x = ggml_cuda_mmq_get_nbytes_shared_x(config, cc); 1448 const size_t nbs_y = config.J * (sizeof(block_q8_1_mmq)); 1449 return nbs_ids + nbs_x + GGML_PAD(nbs_y, config.nthreads*sizeof(int)); 1450 } 1451 1452 template 1453 static void launch_mul_mat_q(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) { 1454 const int id = ggml_cuda_get_device(); 1455 const int cc = ggml_cuda_info().devices[id].cc; 1456 const int nsm = ggml_cuda_info().devices[id].nsm; 1457 const int warp_size = ggml_cuda_info().devices[id].warp_size; 1458 1459 const ggml_cuda_mmq_config config = ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1); 1460 GGML_ASSERT(config.nthreads % warp_size == 0); 1461 const int nwarps = config.nthreads / warp_size; 1462 const int nbytes_shared = mmq_get_nbytes_shared(config, cc); 1463 1464 const dim3 block_dims(warp_size, nwarps, 1); 1465 1466 CUDA_SET_SHARED_MEMORY_LIMIT((mul_mat_q), nbytes_shared); 1467 if constexpr (!has_fusion) { 1468 CUDA_SET_SHARED_MEMORY_LIMIT((mul_mat_q), nbytes_shared); 1469 } 1470 1471 const int I_dst = has_fusion ? config.I/2 : config.I; // Rows of dst per tile, see mul_mat_q. 1472 const int nty = (args.nrows_x + I_dst - 1) / I_dst; 1473 const int ntx = (args.ncols_max + config.J - 1) / config.J; 1474 const int ntzw = args.nchannels_y * args.nsamples_y; 1475 // Column tiles innermost so that the concurrently running blocks share the same tile of x and read it from L2. 1476 const dim3 block_nums_xy_tiling(ntx, nty, ntzw); 1477 1478 GGML_ASSERT(args.nchannels_y % args.nchannels_x == 0); 1479 GGML_ASSERT(args.nsamples_y % args.nsamples_x == 0); 1480 const int channel_ratio = args.nchannels_y / args.nchannels_x; 1481 const int sample_ratio = args.nsamples_y / args.nsamples_x; 1482 1483 const uint3 blocks_per_ne00_fd = init_fastdiv_values(args.ncols_x / ggml_cuda_type_traits::qk); 1484 const uint3 ntx_fd = init_fastdiv_values(ntx); 1485 const uint3 nchannels_y_fd = init_fastdiv_values(args.nchannels_y); 1486 const uint3 nsamples_y_fd = init_fastdiv_values(args.nsamples_y); 1487 const uint3 channel_ratio_fd = init_fastdiv_values(channel_ratio); 1488 const uint3 sample_ratio_fd = init_fastdiv_values(sample_ratio); 1489 1490 if (has_fusion || !config.stream_k) { 1491 GGML_ASSERT(nty <= 65535); // gridDim.y limit 1492 mul_mat_q<<>> 1493 (args.x, (const char *) args.fusion.gate, args.y, args.ids_dst, args.expert_bounds, args.dst, nullptr, args.y_scale, args.fusion.glu_op, args.fusion.glu_limit, 1494 blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x, args.ncols_y, args.nrows_dst, 1495 channel_ratio_fd, nchannels_y_fd, args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst, 1496 sample_ratio_fd, nsamples_y_fd, args.stride_sample_x, args.stride_sample_y, args.stride_sample_dst, 1497 ntx_fd); 1498 return; 1499 } 1500 1501 // For the stream-k kernel it is possible to run it with tiling by setting the number of CUDA blocks equal to the number of tiles. 1502 // This is worthwhile if the efficiency of tiling is high and skipping the fixup kernel is more important. 1503 const int ntiles_dst = ntx * nty * ntzw; 1504 const int tiles_nwaves = (ntiles_dst + nsm - 1) / nsm; 1505 const int tiles_efficiency_percent = 100 * ntiles_dst / (nsm*tiles_nwaves); 1506 1507 GGML_ASSERT(ntiles_dst * blocks_per_ne00_fd.z < (1 << 30)); // Assert that variable kbc will not overflow. 1508 1509 const int nwork = ntiles_dst * ((args.ncols_x + config.K_vram - 1) / config.K_vram); // Maximum number of CUDA blocks that can do useful work. 1510 const dim3 block_nums_stream_k(GGML_CUDA_CC_IS_NVIDIA(cc) && tiles_efficiency_percent >= 90 ? ntiles_dst : std::min(nsm, nwork), 1, 1); 1511 1512 const bool fixup_needed = ntiles_dst % block_nums_stream_k.x != 0; 1513 1514 ggml_cuda_pool & pool = ctx.pool(id); 1515 ggml_cuda_pool_alloc tmp_fixup(pool); 1516 if (fixup_needed) { 1517 tmp_fixup.alloc(block_nums_stream_k.x * config.J*config.I); 1518 } 1519 1520 const dim3 block_nums_fixup(block_nums_stream_k.x, config.I/warp_size, 1); 1521 const dim3 block_dims_fixup(block_dims.x, block_dims.y/2, block_dims.z); 1522 1523 mul_mat_q<<>> 1524 (args.x, (const char *) args.fusion.gate, args.y, args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr, args.y_scale, args.fusion.glu_op, args.fusion.glu_limit, 1525 blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x, args.ncols_y, args.nrows_dst, 1526 channel_ratio_fd, nchannels_y_fd, args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst, 1527 sample_ratio_fd, nsamples_y_fd, args.stride_sample_x, args.stride_sample_y, args.stride_sample_dst, 1528 ntx_fd); 1529 1530 if (!fixup_needed) { 1531 return; 1532 } 1533 1534 CUDA_CHECK(cudaGetLastError()); 1535 mul_mat_q_stream_k_fixup<<>> 1536 (args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr, blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, 1537 args.nrows_dst, nchannels_y_fd, args.stride_channel_dst, nsamples_y_fd, args.stride_sample_dst, 1538 ntx_fd); 1539 } 1540 1541 template 1542 void mul_mat_q_switch_J(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) { 1543 switch (args.J_best) { 1544 case 8: 1545 launch_mul_mat_q(ctx, args, stream); 1546 break; 1547 case 16: 1548 launch_mul_mat_q(ctx, args, stream); 1549 break; 1550 case 24: 1551 launch_mul_mat_q(ctx, args, stream); 1552 break; 1553 case 32: 1554 launch_mul_mat_q(ctx, args, stream); 1555 break; 1556 case 40: 1557 launch_mul_mat_q(ctx, args, stream); 1558 break; 1559 case 48: 1560 launch_mul_mat_q(ctx, args, stream); 1561 break; 1562 case 56: 1563 launch_mul_mat_q(ctx, args, stream); 1564 break; 1565 case 64: 1566 launch_mul_mat_q(ctx, args, stream); 1567 break; 1568 case 72: 1569 launch_mul_mat_q(ctx, args, stream); 1570 break; 1571 case 80: 1572 launch_mul_mat_q(ctx, args, stream); 1573 break; 1574 case 88: 1575 launch_mul_mat_q(ctx, args, stream); 1576 break; 1577 case 96: 1578 launch_mul_mat_q(ctx, args, stream); 1579 break; 1580 case 104: 1581 launch_mul_mat_q(ctx, args, stream); 1582 break; 1583 case 112: 1584 launch_mul_mat_q(ctx, args, stream); 1585 break; 1586 case 120: 1587 launch_mul_mat_q(ctx, args, stream); 1588 break; 1589 case 128: 1590 launch_mul_mat_q(ctx, args, stream); 1591 break; 1592 default: 1593 fprintf(stderr, "J_best=%d\n", args.J_best); 1594 GGML_ABORT("fatal error"); 1595 } 1596 } 1597 1598 template 1599 static void mul_mat_q_switch_fusion(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) { 1600 if constexpr (!fallback && ggml_cuda_mmq_fusion_supported(type)) { 1601 if (args.fusion.gate) { 1602 mul_mat_q_switch_J(ctx, args, stream); 1603 return; 1604 } 1605 } 1606 GGML_ASSERT(!args.fusion.gate); 1607 mul_mat_q_switch_J(ctx, args, stream); 1608 } 1609 1610 template 1611 void mul_mat_q_case(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) { 1612 if (ggml_cuda_mmq_needs_fallback(args.nrows_x)) { 1613 constexpr bool fallback = true; 1614 mul_mat_q_switch_fusion(ctx, args, stream); 1615 } else { 1616 constexpr bool fallback = false; 1617 mul_mat_q_switch_fusion(ctx, args, stream); 1618 } 1619 } 1620 1621 #define DECL_MMQ_CASE(type) \ 1622 template void mul_mat_q_case(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) \ 1623 1624 // FP4 variant: uses native FP4 MMA instead of keeping src1 at Q8_1. 1625 #define DECL_MMQ_CASE_W4A4(type) \ 1626 template void mul_mat_q_case(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream) \ 1627 1628 extern DECL_MMQ_CASE(GGML_TYPE_Q1_0); 1629 extern DECL_MMQ_CASE(GGML_TYPE_Q2_0); 1630 extern DECL_MMQ_CASE(GGML_TYPE_Q4_0); 1631 extern DECL_MMQ_CASE(GGML_TYPE_Q4_1); 1632 extern DECL_MMQ_CASE(GGML_TYPE_Q5_0); 1633 extern DECL_MMQ_CASE(GGML_TYPE_Q5_1); 1634 extern DECL_MMQ_CASE(GGML_TYPE_Q8_0); 1635 // ----------------------------------------- 1636 extern DECL_MMQ_CASE(GGML_TYPE_Q2_K); 1637 extern DECL_MMQ_CASE(GGML_TYPE_Q3_K); 1638 extern DECL_MMQ_CASE(GGML_TYPE_Q4_K); 1639 extern DECL_MMQ_CASE(GGML_TYPE_Q5_K); 1640 extern DECL_MMQ_CASE(GGML_TYPE_Q6_K); 1641 // ----------------------------------------- 1642 extern DECL_MMQ_CASE(GGML_TYPE_IQ1_S); 1643 extern DECL_MMQ_CASE(GGML_TYPE_IQ2_XXS); 1644 extern DECL_MMQ_CASE(GGML_TYPE_IQ2_XS); 1645 extern DECL_MMQ_CASE(GGML_TYPE_IQ2_S); 1646 extern DECL_MMQ_CASE(GGML_TYPE_IQ3_XXS); 1647 extern DECL_MMQ_CASE(GGML_TYPE_IQ3_S); 1648 extern DECL_MMQ_CASE(GGML_TYPE_IQ4_NL); 1649 extern DECL_MMQ_CASE(GGML_TYPE_IQ4_XS); 1650 // ----------------------------------------- 1651 extern DECL_MMQ_CASE(GGML_TYPE_MXFP4); 1652 extern DECL_MMQ_CASE(GGML_TYPE_NVFP4); 1653 extern DECL_MMQ_CASE_W4A4(GGML_TYPE_MXFP4); 1654 extern DECL_MMQ_CASE_W4A4(GGML_TYPE_NVFP4); 1655 1656 // ------------------------------------------------------------------------------------------------------------------------- 1657 1658 void ggml_cuda_mul_mat_q( 1659 ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, 1660 const ggml_cuda_mm_fusion_args_host * fusion = nullptr); 1661 1662 bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t n_experts); 1663 1664 bool ggml_cuda_mmq_fusion_is_efficient(ggml_type type, int64_t nrows_x, int64_t ncols, int64_t nchannels, int device);