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/mmvq.cu # 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 / mmvq.cu` 1618 lines ยท 76442 bytes Raw Blame History 1 #include "mmvq.cuh" 2 #include "quantize.cuh" 3 #include "unary.cuh" 4 #include "vecdotq.cuh" 5 6 #include 7 #include 8 9 // only enabled on DGX Spark, where it is a gain on every type below. On the higher-bandwidth parts the kernel 10 // has little exposed latency left to hide and the extra requests cost more than they save. 11 // For perf data, see https://github.com/ggml-org/llama.cpp/pull/26705#issuecomment-`5569335031` 12 #if __CUDA_ARCH__ == GGML_CUDA_CC_DGX_SPARK 13 // returns true only for those quants that benefit from prefetch and false otherwise 14 static constexpr __host__ __device__ bool mmvq_should_prefetch(ggml_type type) { 15 switch (type) { 16 case GGML_TYPE_Q4_0: 17 case GGML_TYPE_Q5_0: 18 case GGML_TYPE_Q8_0: 19 case GGML_TYPE_MXFP4: 20 case GGML_TYPE_Q3_K: 21 case GGML_TYPE_Q4_K: 22 case GGML_TYPE_Q5_K: 23 case GGML_TYPE_Q6_K: 24 case GGML_TYPE_IQ1_M: 25 case GGML_TYPE_IQ4_NL: 26 case GGML_TYPE_IQ4_XS: 27 return true; 28 default: 29 return false; 30 } 31 } 32 33 static __device__ __forceinline__ void mmvq_prefetch_l2(const void * p) { 34 asm volatile("prefetch.global.L2 [%0];" :: "l"(p)); 35 } 36 #endif 37 38 typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs); 39 40 static constexpr __device__ vec_dot_q_cuda_t get_vec_dot_q_cuda(ggml_type type) { 41 switch (type) { 42 case GGML_TYPE_Q1_0: return vec_dot_q1_0_q8_1; 43 case GGML_TYPE_Q2_0: return vec_dot_q2_0_q8_1; 44 case GGML_TYPE_Q4_0: return vec_dot_q4_0_q8_1; 45 case GGML_TYPE_Q4_1: return vec_dot_q4_1_q8_1; 46 case GGML_TYPE_Q5_0: return vec_dot_q5_0_q8_1; 47 case GGML_TYPE_Q5_1: return vec_dot_q5_1_q8_1; 48 case GGML_TYPE_Q8_0: return vec_dot_q8_0_q8_1; 49 case GGML_TYPE_MXFP4: return vec_dot_mxfp4_q8_1; 50 case GGML_TYPE_NVFP4: return vec_dot_nvfp4_q8_1; 51 case GGML_TYPE_Q2_K: return vec_dot_q2_K_q8_1; 52 case GGML_TYPE_Q3_K: return vec_dot_q3_K_q8_1; 53 case GGML_TYPE_Q4_K: return vec_dot_q4_K_q8_1; 54 case GGML_TYPE_Q5_K: return vec_dot_q5_K_q8_1; 55 case GGML_TYPE_Q6_K: return vec_dot_q6_K_q8_1; 56 case GGML_TYPE_IQ2_XXS: return vec_dot_iq2_xxs_q8_1; 57 case GGML_TYPE_IQ2_XS: return vec_dot_iq2_xs_q8_1; 58 case GGML_TYPE_IQ2_S: return vec_dot_iq2_s_q8_1; 59 case GGML_TYPE_IQ3_XXS: return vec_dot_iq3_xxs_q8_1; 60 case GGML_TYPE_IQ1_S: return vec_dot_iq1_s_q8_1; 61 case GGML_TYPE_IQ1_M: return vec_dot_iq1_m_q8_1; 62 case GGML_TYPE_IQ4_NL: return vec_dot_iq4_nl_q8_1; 63 case GGML_TYPE_IQ4_XS: return vec_dot_iq4_xs_q8_1; 64 case GGML_TYPE_IQ3_S: return vec_dot_iq3_s_q8_1; 65 default: return nullptr; 66 } 67 } 68 69 static constexpr __host__ __device__ int get_vdr_mmvq(ggml_type type) { 70 switch (type) { 71 case GGML_TYPE_Q1_0: return VDR_Q1_0_Q8_1_MMVQ; 72 case GGML_TYPE_Q2_0: return VDR_Q2_0_Q8_1_MMVQ; 73 case GGML_TYPE_Q4_0: return VDR_Q4_0_Q8_1_MMVQ; 74 case GGML_TYPE_Q4_1: return VDR_Q4_1_Q8_1_MMVQ; 75 case GGML_TYPE_Q5_0: return VDR_Q5_0_Q8_1_MMVQ; 76 case GGML_TYPE_Q5_1: return VDR_Q5_1_Q8_1_MMVQ; 77 case GGML_TYPE_Q8_0: return VDR_Q8_0_Q8_1_MMVQ; 78 case GGML_TYPE_MXFP4: return VDR_MXFP4_Q8_1_MMVQ; 79 case GGML_TYPE_NVFP4: return VDR_NVFP4_Q8_1_MMVQ; 80 case GGML_TYPE_Q2_K: return VDR_Q2_K_Q8_1_MMVQ; 81 case GGML_TYPE_Q3_K: return VDR_Q3_K_Q8_1_MMVQ; 82 case GGML_TYPE_Q4_K: return VDR_Q4_K_Q8_1_MMVQ; 83 case GGML_TYPE_Q5_K: return VDR_Q5_K_Q8_1_MMVQ; 84 case GGML_TYPE_Q6_K: return VDR_Q6_K_Q8_1_MMVQ; 85 case GGML_TYPE_IQ2_XXS: return VDR_IQ2_XXS_Q8_1_MMVQ; 86 case GGML_TYPE_IQ2_XS: return VDR_IQ2_XS_Q8_1_MMVQ; 87 case GGML_TYPE_IQ2_S: return VDR_IQ2_S_Q8_1_MMVQ; 88 case GGML_TYPE_IQ3_XXS: return VDR_IQ3_XXS_Q8_1_MMVQ; 89 case GGML_TYPE_IQ3_S: return VDR_IQ3_S_Q8_1_MMVQ; 90 case GGML_TYPE_IQ4_NL: return VDR_IQ4_NL_Q8_1_MMVQ; 91 case GGML_TYPE_IQ4_XS: return VDR_IQ4_XS_Q8_1_MMVQ; 92 default: return 1; 93 } 94 } 95 96 enum mmvq_parameter_table_id { 97 MMVQ_PARAMETERS_GENERIC = 0, 98 MMVQ_PARAMETERS_TURING, 99 MMVQ_PARAMETERS_GCN, 100 MMVQ_PARAMETERS_RDNA2, 101 MMVQ_PARAMETERS_RDNA3_0, 102 MMVQ_PARAMETERS_RDNA4, 103 MMVQ_PARAMETERS_GB10 104 }; 105 106 static constexpr __device__ mmvq_parameter_table_id get_device_table_id() { 107 #if defined(RDNA4) 108 return MMVQ_PARAMETERS_RDNA4; 109 #elif defined(RDNA3_0) 110 return MMVQ_PARAMETERS_RDNA3_0; 111 #elif defined(RDNA2) || defined(RDNA3_5) || defined(RDNA4_M) 112 return MMVQ_PARAMETERS_RDNA2; 113 #elif defined(GCN) || defined(CDNA) 114 return MMVQ_PARAMETERS_GCN; 115 #elif __CUDA_ARCH__ >= GGML_CUDA_CC_VOLTA && __CUDA_ARCH__ < GGML_CUDA_CC_AMPERE 116 return MMVQ_PARAMETERS_TURING; 117 #elif __CUDA_ARCH__ == GGML_CUDA_CC_DGX_SPARK 118 return MMVQ_PARAMETERS_GB10; 119 #else 120 return MMVQ_PARAMETERS_GENERIC; 121 #endif 122 } 123 124 static __host__ mmvq_parameter_table_id get_device_table_id(int cc) { 125 if (GGML_CUDA_CC_IS_RDNA4(cc)) { 126 return MMVQ_PARAMETERS_RDNA4; 127 } 128 if (GGML_CUDA_CC_IS_RDNA3_0(cc)) { 129 return MMVQ_PARAMETERS_RDNA3_0; 130 } 131 if (GGML_CUDA_CC_IS_RDNA2(cc) || GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc)) { 132 return MMVQ_PARAMETERS_RDNA2; 133 } 134 if (GGML_CUDA_CC_IS_GCN(cc) || GGML_CUDA_CC_IS_CDNA(cc)) { 135 return MMVQ_PARAMETERS_GCN; 136 } 137 if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_VOLTA && ggml_cuda_highest_compiled_arch(cc) < GGML_CUDA_CC_AMPERE) { 138 return MMVQ_PARAMETERS_TURING; 139 } 140 if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) == GGML_CUDA_CC_DGX_SPARK) { 141 return MMVQ_PARAMETERS_GB10; 142 } 143 return MMVQ_PARAMETERS_GENERIC; 144 } 145 146 // Per-architecture maximum batch size for which MMVQ should be used for MUL_MAT_ID. 147 // Returns a value <= MMVQ_MAX_BATCH_SIZE. Default is MMVQ_MAX_BATCH_SIZE. 148 // Check https://github.com/ggml-org/llama.cpp/pull/20905#issuecomment-`4145835627` for details 149 150 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_pascal_older(ggml_type type) { 151 switch (type) { 152 case GGML_TYPE_IQ1_S: return 6; 153 case GGML_TYPE_IQ1_M: return 6; 154 case GGML_TYPE_IQ2_S: return 4; 155 case GGML_TYPE_IQ2_XS: return 5; 156 case GGML_TYPE_IQ2_XXS: return 5; 157 case GGML_TYPE_IQ3_S: return 4; 158 case GGML_TYPE_IQ3_XXS: return 4; 159 case GGML_TYPE_IQ4_NL: return 6; 160 case GGML_TYPE_IQ4_XS: return 5; 161 case GGML_TYPE_MXFP4: return 4; 162 case GGML_TYPE_NVFP4: return 4; 163 case GGML_TYPE_Q2_K: return 4; 164 case GGML_TYPE_Q3_K: return 4; 165 case GGML_TYPE_Q4_0: return 6; 166 case GGML_TYPE_Q4_1: return 6; 167 case GGML_TYPE_Q4_K: return 5; 168 case GGML_TYPE_Q5_0: return 6; 169 case GGML_TYPE_Q5_1: return 6; 170 case GGML_TYPE_Q5_K: return 5; 171 case GGML_TYPE_Q6_K: return 4; 172 case GGML_TYPE_Q8_0: return 4; 173 default: return MMVQ_MAX_BATCH_SIZE; 174 } 175 } 176 177 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_turing_plus(ggml_type type) { 178 switch (type) { 179 case GGML_TYPE_IQ2_S: return 7; 180 case GGML_TYPE_IQ3_S: return 6; 181 case GGML_TYPE_IQ3_XXS: return 7; 182 case GGML_TYPE_MXFP4: return 7; 183 case GGML_TYPE_NVFP4: return 8; 184 case GGML_TYPE_Q2_K: return 7; 185 case GGML_TYPE_Q3_K: return 5; 186 default: return MMVQ_MAX_BATCH_SIZE; 187 } 188 } 189 190 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_gcn(ggml_type type) { 191 switch (type) { 192 case GGML_TYPE_IQ1_S: return 5; 193 case GGML_TYPE_IQ1_M: return 5; 194 case GGML_TYPE_IQ2_S: return 4; 195 case GGML_TYPE_IQ2_XS: return 4; 196 case GGML_TYPE_IQ2_XXS: return 4; 197 case GGML_TYPE_IQ3_S: return 4; 198 case GGML_TYPE_IQ3_XXS: return 4; 199 case GGML_TYPE_IQ4_NL: return 6; 200 case GGML_TYPE_IQ4_XS: return 4; 201 case GGML_TYPE_Q2_K: return 4; 202 case GGML_TYPE_Q3_K: return 4; 203 case GGML_TYPE_Q4_0: return 5; 204 case GGML_TYPE_Q4_1: return 5; 205 case GGML_TYPE_Q4_K: return 4; 206 case GGML_TYPE_Q5_K: return 4; 207 case GGML_TYPE_Q6_K: return 4; 208 case GGML_TYPE_Q8_0: return 4; 209 default: return MMVQ_MAX_BATCH_SIZE; 210 } 211 } 212 213 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_cdna(ggml_type type) { 214 switch (type) { 215 case GGML_TYPE_IQ2_S: return 5; 216 case GGML_TYPE_IQ2_XS: return 5; 217 case GGML_TYPE_IQ2_XXS: return 5; 218 case GGML_TYPE_IQ3_S: return 4; 219 case GGML_TYPE_IQ3_XXS: return 5; 220 default: return MMVQ_MAX_BATCH_SIZE; 221 } 222 } 223 224 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_rdna1_rdna2(ggml_type type) { 225 switch (type) { 226 case GGML_TYPE_IQ2_S: return 4; 227 case GGML_TYPE_IQ2_XS: return 4; 228 case GGML_TYPE_IQ2_XXS: return 4; 229 case GGML_TYPE_IQ3_S: return 4; 230 case GGML_TYPE_IQ3_XXS: return 4; 231 case GGML_TYPE_Q2_K: return 7; 232 case GGML_TYPE_Q3_K: return 4; 233 case GGML_TYPE_Q4_K: return 5; 234 case GGML_TYPE_Q5_K: return 6; 235 case GGML_TYPE_Q6_K: return 5; 236 default: return MMVQ_MAX_BATCH_SIZE; 237 } 238 } 239 240 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_rdna3(ggml_type type) { 241 switch (type) { 242 case GGML_TYPE_IQ1_S: return 6; 243 case GGML_TYPE_IQ1_M: return 6; 244 case GGML_TYPE_IQ2_S: return 4; 245 case GGML_TYPE_IQ2_XS: return 4; 246 case GGML_TYPE_IQ2_XXS: return 4; 247 case GGML_TYPE_IQ3_S: return 4; 248 case GGML_TYPE_IQ3_XXS: return 4; 249 case GGML_TYPE_IQ4_NL: return 6; 250 case GGML_TYPE_IQ4_XS: return 6; 251 case GGML_TYPE_Q4_K: return 4; 252 case GGML_TYPE_Q5_K: return 4; 253 case GGML_TYPE_Q6_K: return 4; 254 default: return MMVQ_MAX_BATCH_SIZE; 255 } 256 } 257 258 static constexpr __host__ __device__ int get_mmvq_mmid_max_batch_rdna4(ggml_type type) { 259 switch (type) { 260 case GGML_TYPE_IQ1_S: return 7; 261 case GGML_TYPE_IQ1_M: return 7; 262 case GGML_TYPE_IQ2_S: return 4; 263 case GGML_TYPE_IQ2_XS: return 4; 264 case GGML_TYPE_IQ2_XXS: return 4; 265 case GGML_TYPE_IQ3_S: return 4; 266 case GGML_TYPE_IQ3_XXS: return 4; 267 case GGML_TYPE_IQ4_NL: return 7; 268 case GGML_TYPE_IQ4_XS: return 5; 269 case GGML_TYPE_MXFP4: return 5; 270 case GGML_TYPE_NVFP4: return 5; 271 case GGML_TYPE_Q3_K: return 4; 272 case GGML_TYPE_Q4_0: return 7; 273 case GGML_TYPE_Q4_1: return 7; 274 case GGML_TYPE_Q4_K: return 4; 275 case GGML_TYPE_Q5_0: return 7; 276 case GGML_TYPE_Q5_1: return 7; 277 case GGML_TYPE_Q5_K: return 5; 278 case GGML_TYPE_Q6_K: return 5; 279 case GGML_TYPE_Q8_0: return 7; 280 default: return MMVQ_MAX_BATCH_SIZE; 281 } 282 } 283 284 // Host function: returns the max batch size for the current arch+type at runtime. 285 int get_mmvq_mmid_max_batch(ggml_type type, int cc) { 286 // NVIDIA: P100, Volta, Ada Lovelace, and Blackwell always use MMVQ for MUL_MAT_ID. 287 if (GGML_CUDA_CC_IS_NVIDIA(cc)) { 288 if (cc == GGML_CUDA_CC_PASCAL || cc == GGML_CUDA_CC_VOLTA || cc >= GGML_CUDA_CC_ADA_LOVELACE) { 289 return MMVQ_MAX_BATCH_SIZE; 290 } 291 if (cc >= GGML_CUDA_CC_TURING) { 292 return get_mmvq_mmid_max_batch_turing_plus(type); 293 } 294 return get_mmvq_mmid_max_batch_pascal_older(type); 295 } 296 297 // AMD 298 if (GGML_CUDA_CC_IS_AMD(cc)) { 299 if (GGML_CUDA_CC_IS_RDNA4(cc)) { 300 return get_mmvq_mmid_max_batch_rdna4(type); 301 } 302 if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc)) { 303 return get_mmvq_mmid_max_batch_rdna3(type); 304 } 305 if (GGML_CUDA_CC_IS_RDNA1(cc) || GGML_CUDA_CC_IS_RDNA2(cc)) { 306 return get_mmvq_mmid_max_batch_rdna1_rdna2(type); 307 } 308 if (GGML_CUDA_CC_IS_CDNA(cc)) { 309 return get_mmvq_mmid_max_batch_cdna(type); 310 } 311 if (GGML_CUDA_CC_IS_GCN(cc)) { 312 return get_mmvq_mmid_max_batch_gcn(type); 313 } 314 } 315 return MMVQ_MAX_BATCH_SIZE; 316 } 317 318 bool ggml_cuda_should_use_mmvq(enum ggml_type type, int cc, int64_t ne11) { 319 if (!ggml_is_quantized(type)) { 320 return false; 321 } 322 // k-quants cost more to decode and mvq redoes that per column, so MMQ wins sooner. 323 // Only list quant-types MMQ supports, others would fall back to cuBLAS. 324 if (GGML_CUDA_CC_IS_NVIDIA(cc) && cc == GGML_CUDA_CC_ADA_LOVELACE) { 325 switch (type) { // tuned on RTX 4090 326 case GGML_TYPE_Q2_K: 327 return ne11 <= 4; 328 case GGML_TYPE_Q3_K: 329 return ne11 <= 6; 330 default: 331 return ne11 <= MMVQ_MAX_BATCH_SIZE; 332 } 333 } 334 if (GGML_CUDA_CC_IS_NVIDIA(cc) && cc == GGML_CUDA_CC_BLACKWELL) { 335 switch (type) { // tuned on RTX 5090 336 case GGML_TYPE_Q2_K: 337 case GGML_TYPE_Q3_K: 338 case GGML_TYPE_Q4_K: 339 return ne11 <= 5; 340 case GGML_TYPE_Q5_K: 341 return ne11 <= 6; 342 case GGML_TYPE_Q6_K: 343 return ne11 <= 7; 344 default: 345 return ne11 <= MMVQ_MAX_BATCH_SIZE; 346 } 347 } 348 if (GGML_CUDA_CC_IS_NVIDIA(cc) && cc == GGML_CUDA_CC_DGX_SPARK) { 349 switch (type) { // tuned on DGX Spark GB10 350 case GGML_TYPE_Q2_K: 351 return ne11 <= 6; 352 default: 353 return ne11 <= MMVQ_MAX_BATCH_SIZE; 354 } 355 } 356 if (GGML_CUDA_CC_IS_NVIDIA(cc) && cc == GGML_CUDA_CC_ORIN) { 357 switch (type) { // tuned for Jetson Orin 358 case GGML_TYPE_Q2_K: 359 case GGML_TYPE_Q3_K: 360 case GGML_TYPE_Q4_K: 361 case GGML_TYPE_Q5_K: 362 case GGML_TYPE_Q6_K: 363 return ne11 <= 1; 364 default: 365 return ne11 <= MMVQ_MAX_BATCH_SIZE; 366 } 367 } 368 if (GGML_CUDA_CC_IS_NVIDIA(cc) && cc == GGML_CUDA_CC_VOLTA) { 369 switch (type) { 370 case GGML_TYPE_Q2_K: 371 return ne11 <= 4; 372 case GGML_TYPE_Q3_K: 373 return ne11 <= 6; 374 case GGML_TYPE_Q4_K: 375 return ne11 <= 5; 376 case GGML_TYPE_Q5_K: 377 return ne11 <= 6; 378 case GGML_TYPE_Q6_K: 379 return ne11 <= 7; 380 default: 381 return ne11 <= MMVQ_MAX_BATCH_SIZE; 382 } 383 } 384 if (GGML_CUDA_CC_IS_CDNA(cc)) { 385 if (GGML_CUDA_CC_IS_CDNA1(cc)) { 386 switch (type) { 387 case GGML_TYPE_Q4_0: 388 case GGML_TYPE_Q4_1: 389 return ne11 <= 7; 390 case GGML_TYPE_Q5_1: 391 return ne11 <= 7; 392 case GGML_TYPE_Q8_0: 393 return ne11 <= 6; 394 case GGML_TYPE_Q2_K: 395 return ne11 <= 4; 396 case GGML_TYPE_Q3_K: 397 return ne11 <= 3; 398 case GGML_TYPE_Q4_K: 399 return ne11 <= 2; 400 case GGML_TYPE_Q5_K: 401 return ne11 <= 3; 402 case GGML_TYPE_Q6_K: 403 return ne11 <= 4; 404 case GGML_TYPE_IQ1_S: 405 return ne11 <= 5; 406 case GGML_TYPE_IQ2_XXS: 407 case GGML_TYPE_IQ3_S: 408 case GGML_TYPE_IQ4_XS: 409 return ne11 <= 6; 410 default: 411 return ne11 <= MMVQ_MAX_BATCH_SIZE; 412 } 413 } 414 switch (type) { // tuned for CDNA2 415 case GGML_TYPE_Q2_K: 416 return ne11 <= 5; 417 case GGML_TYPE_Q3_K: 418 case GGML_TYPE_Q4_K: 419 case GGML_TYPE_Q5_K: 420 return ne11 <= 3; 421 case GGML_TYPE_Q6_K: 422 return ne11 <= 5; 423 default: 424 return ne11 <= MMVQ_MAX_BATCH_SIZE; 425 } 426 } 427 return ne11 <= MMVQ_MAX_BATCH_SIZE; 428 } 429 430 // Device constexpr: returns the max batch size for the current arch+type at compile time. 431 template 432 static constexpr __device__ int get_mmvq_mmid_max_batch_for_device() { 433 #if defined(RDNA4) 434 return get_mmvq_mmid_max_batch_rdna4(type); 435 #elif defined(RDNA3) || defined(RDNA4_M) 436 return get_mmvq_mmid_max_batch_rdna3(type); 437 #elif defined(RDNA2) || defined(RDNA1) 438 return get_mmvq_mmid_max_batch_rdna1_rdna2(type); 439 #elif defined(CDNA) 440 return get_mmvq_mmid_max_batch_cdna(type); 441 #elif defined(GCN) 442 return get_mmvq_mmid_max_batch_gcn(type); 443 #elif !defined(GGML_USE_MUSA) && (__CUDA_ARCH__ == GGML_CUDA_CC_PASCAL || __CUDA_ARCH__ == GGML_CUDA_CC_VOLTA || __CUDA_ARCH__ >= GGML_CUDA_CC_ADA_LOVELACE) 444 return MMVQ_MAX_BATCH_SIZE; 445 #elif !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= GGML_CUDA_CC_TURING 446 return get_mmvq_mmid_max_batch_turing_plus(type); 447 #else 448 return get_mmvq_mmid_max_batch_pascal_older(type); 449 #endif 450 } 451 452 static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_dst, mmvq_parameter_table_id table_id, bool small_k = false, bool halve_iters = false) { 453 if (table_id == MMVQ_PARAMETERS_GENERIC) { 454 switch (ncols_dst) { 455 case 1: 456 case 2: 457 case 3: 458 case 4: 459 return 4; 460 case 5: 461 case 6: 462 case 7: 463 case 8: 464 return 2; 465 default: 466 return 1; 467 } 468 } else if (table_id == MMVQ_PARAMETERS_GCN) { 469 switch (ncols_dst) { 470 case 1: 471 return 2; 472 case 2: 473 case 3: 474 return type == GGML_TYPE_Q3_K ? 1 : 2; 475 case 4: 476 return type == GGML_TYPE_Q6_K? 1 : 2; 477 case 5: 478 case 6: 479 case 7: 480 case 8: 481 default: 482 return 1; 483 } 484 } 485 if (table_id == MMVQ_PARAMETERS_RDNA4) { 486 // nwarps=8 benefits types with simple vec_dot on RDNA4 (ncols_dst=1). 487 // Types with complex vec_dot (Q3_K, IQ2_*, IQ3_*) regress due to register 488 // pressure and lookup table contention at higher thread counts. 489 if (ncols_dst == 1) { 490 switch (type) { 491 case GGML_TYPE_Q4_0: 492 case GGML_TYPE_Q4_1: 493 case GGML_TYPE_Q5_0: 494 case GGML_TYPE_Q5_1: 495 case GGML_TYPE_Q8_0: 496 case GGML_TYPE_Q2_K: 497 case GGML_TYPE_Q4_K: 498 case GGML_TYPE_Q5_K: 499 case GGML_TYPE_Q6_K: 500 case GGML_TYPE_IQ4_NL: 501 case GGML_TYPE_IQ4_XS: 502 return 8; 503 default: 504 return 1; 505 } 506 } 507 return 1; 508 } 509 if (table_id == MMVQ_PARAMETERS_RDNA3_0) { 510 // RDNA3 (W7900): stricter whitelist than RDNA4. 511 // Q2_K / Q5_K / IQ4_XS regress in full quant sweeps. 512 if (ncols_dst == 1) { 513 switch (type) { 514 case GGML_TYPE_Q4_0: 515 case GGML_TYPE_Q4_1: 516 case GGML_TYPE_Q5_0: 517 case GGML_TYPE_Q5_1: 518 case GGML_TYPE_Q8_0: 519 return 8; 520 case GGML_TYPE_Q6_K: 521 return 2; 522 case GGML_TYPE_IQ4_NL: 523 return 8; 524 default: 525 return 1; 526 } 527 } 528 return 1; 529 } 530 if (table_id == MMVQ_PARAMETERS_TURING) { 531 if (ncols_dst == 1) { 532 switch (type) { 533 case GGML_TYPE_Q2_K: 534 case GGML_TYPE_Q3_K: 535 case GGML_TYPE_Q4_K: 536 case GGML_TYPE_Q5_K: 537 case GGML_TYPE_Q6_K: 538 return 2; 539 default: 540 return 4; 541 } 542 } 543 switch (ncols_dst) { 544 case 2: 545 case 3: 546 case 4: 547 return 4; 548 case 5: 549 case 6: 550 case 7: 551 case 8: 552 return 2; 553 default: 554 return 1; 555 } 556 } 557 if (table_id == MMVQ_PARAMETERS_GB10) { 558 const int generic = calc_nwarps(type, ncols_dst, MMVQ_PARAMETERS_GENERIC); 559 // Only worth the wider block when it actually retires the K loop in half the trips (Observation) 560 if (ncols_dst == 1 && !small_k && halve_iters) { 561 switch (type) { 562 case GGML_TYPE_Q4_0: 563 case GGML_TYPE_Q4_1: 564 case GGML_TYPE_Q5_0: 565 case GGML_TYPE_Q5_1: 566 case GGML_TYPE_Q8_0: 567 case GGML_TYPE_Q4_K: 568 case GGML_TYPE_Q5_K: 569 case GGML_TYPE_Q6_K: 570 case GGML_TYPE_IQ4_NL: 571 return 2 * generic; 572 default: 573 break; 574 } 575 } 576 return generic; 577 } 578 return 1; 579 } 580 581 // Low bit types load few weight bytes per thread, a second row per block shares the q8_1 loads and adds memory parallelism. 582 static constexpr __host__ __device__ bool mmvq_two_rows_ncols1(ggml_type type) { 583 switch (type) { 584 case GGML_TYPE_Q1_0: 585 case GGML_TYPE_Q2_K: 586 case GGML_TYPE_Q3_K: 587 case GGML_TYPE_IQ1_S: 588 case GGML_TYPE_IQ1_M: 589 return true; 590 default: 591 return false; 592 } 593 } 594 595 static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int ncols_dst, int table_id, bool small_k = false, int nwarps = 1) { 596 if (table_id == MMVQ_PARAMETERS_GENERIC || table_id == MMVQ_PARAMETERS_GCN || table_id == MMVQ_PARAMETERS_TURING || table_id == MMVQ_PARAMETERS_GB10) { 597 switch (ncols_dst) { 598 case 1: 599 if (small_k) { 600 return nwarps; 601 } 602 if (table_id == MMVQ_PARAMETERS_GB10) { 603 return type == GGML_TYPE_IQ2_S || type == GGML_TYPE_IQ3_S ? 2 : 1; 604 } 605 return table_id == MMVQ_PARAMETERS_GENERIC && mmvq_two_rows_ncols1(type) ? 2 : 1; 606 case 2: 607 case 3: 608 case 4: 609 case 5: 610 case 6: 611 case 7: 612 case 8: 613 return 2; 614 default: 615 return 1; 616 } 617 } 618 return 1; 619 } 620 621 template 622 __launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, halve_iters)*ggml_cuda_get_physical_warp_size(), 1) 623 static __global__ void mul_mat_vec_q( 624 const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr, 625 const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, 626 uint32_t stride_col_dst, const uint3 channel_ratio, const uint32_t stride_channel_x, 627 const uint32_t stride_channel_y, const uint32_t stride_channel_dst, const uint3 sample_ratio, 628 const uint32_t stride_sample_x, const uint32_t stride_sample_y, const uint32_t stride_sample_dst, 629 const uint32_t ids_stride) { 630 const void * GGML_CUDA_RESTRICT vx = vx_ptr; 631 const void * GGML_CUDA_RESTRICT vy = vy_ptr; 632 const int32_t * GGML_CUDA_RESTRICT ids = ids_ptr; 633 float * GGML_CUDA_RESTRICT dst = dst_ptr; 634 635 constexpr int qk = ggml_cuda_type_traits::qk; 636 constexpr int qi = ggml_cuda_type_traits::qi; 637 constexpr int vdr = get_vdr_mmvq(type); 638 constexpr mmvq_parameter_table_id table_id = get_device_table_id(); 639 constexpr int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters); 640 constexpr int rows_per_cuda_block = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps); 641 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 642 643 constexpr vec_dot_q_cuda_t vec_dot_q_cuda = get_vec_dot_q_cuda(type); 644 645 const int tid = warp_size*threadIdx.y + threadIdx.x; 646 const int row0 = rows_per_cuda_block*blockIdx.x; 647 const int blocks_per_row_x = ncols_x / qk; 648 constexpr int blocks_per_iter = vdr * nwarps*warp_size / qi; 649 650 const bool shared_expert = has_fusion && fusion.shared_up && blockIdx.y == gridDim.y - 1; 651 const uint32_t channel_dst = shared_expert ? 0 : blockIdx.y; 652 if (shared_expert) { 653 vx = fusion.shared_up; 654 dst = fusion.shared_dst; 655 stride_col_dst = fusion.shared_stride_col_dst; 656 } 657 658 uint32_t channel_x; 659 uint32_t channel_y; 660 uint32_t sample_dst; 661 662 ggml_cuda_pdl_sync(); 663 channel_x = shared_expert ? 0 : ncols_dst == 1 && ids ? ids[channel_dst] : fastdiv(channel_dst, channel_ratio); 664 channel_y = ncols_dst == 1 && ids ? fastmodulo(channel_dst, nchannels_y) : channel_dst; 665 sample_dst = blockIdx.z; 666 667 const uint32_t sample_x = fastdiv(sample_dst, sample_ratio); 668 const uint32_t sample_y = sample_dst; 669 670 bool use_gate = false; 671 bool use_bias = false; 672 bool use_gate_bias = false; 673 bool use_scale = false; 674 bool use_gate_scale = false; 675 [[maybe_unused]] const void * vgate = nullptr; 676 const float * x_bias = nullptr; 677 const float * gate_bias = nullptr; 678 const float * x_scale = nullptr; 679 const float * gate_scale = nullptr; 680 ggml_glu_op active_glu; 681 float glu_limit = 0.0f; 682 683 if constexpr (has_fusion) { 684 use_gate = fusion.gate != nullptr; 685 use_bias = fusion.x_bias != nullptr; 686 use_gate_bias = fusion.gate_bias != nullptr && use_gate; 687 vgate = shared_expert ? fusion.shared_gate : fusion.gate; 688 x_bias = (const float *) fusion.x_bias; 689 gate_bias = (const float *) fusion.gate_bias; 690 active_glu = fusion.glu_op; 691 glu_limit = fusion.glu_limit; 692 if constexpr (type == GGML_TYPE_NVFP4) { 693 use_scale = fusion.x_scale != nullptr; 694 use_gate_scale = fusion.gate_scale != nullptr && use_gate; 695 x_scale = (const float *) fusion.x_scale; 696 gate_scale = (const float *) fusion.gate_scale; 697 } 698 } 699 700 701 [[maybe_unused]] float x_biases[ncols_dst] = { 0.0f }; 702 [[maybe_unused]] float gate_biases[ncols_dst] = { 0.0f }; 703 [[maybe_unused]] float x_scales = 1.0f; 704 [[maybe_unused]] float gate_scales = 1.0f; 705 if constexpr (has_fusion) { 706 // 1. Hide latency by prefetching bias, gates and scales here 707 // 2. load only on threads that won't die after partial sum calculation 708 const uint32_t channel_bias = ids ? channel_x : channel_dst; 709 if (threadIdx.x < rows_per_cuda_block && threadIdx.y == 0 && 710 (rows_per_cuda_block == 1 || uint32_t(row0 + threadIdx.x) < stride_col_dst)) { 711 if (use_bias) { 712 x_bias = x_bias + sample_dst * stride_sample_dst + channel_bias * stride_channel_dst + row0; 713 #pragma unroll 714 for (int j = 0; j < ncols_dst; ++j) { 715 x_biases[j] = x_bias[j * stride_col_dst + threadIdx.x]; 716 } 717 } 718 if (use_gate_bias) { 719 gate_bias = gate_bias + sample_dst * stride_sample_dst + channel_bias * stride_channel_dst + row0; 720 #pragma unroll 721 for (int j = 0; j < ncols_dst; ++j) { 722 gate_biases[j] = gate_bias[j * stride_col_dst + threadIdx.x]; 723 } 724 } 725 if constexpr (type == GGML_TYPE_NVFP4) { 726 if (use_scale) { 727 x_scales = x_scale[ids ? channel_x : 0]; 728 } 729 if (use_gate_scale) { 730 gate_scales = gate_scale[ids ? channel_x : 0]; 731 } 732 } 733 } 734 } 735 736 // partial sum for each thread 737 float tmp[ncols_dst][rows_per_cuda_block] = {{0.0f}}; 738 float tmp_gate[ncols_dst][rows_per_cuda_block] = {{0.0f}}; 739 740 const block_q8_1 * y = ((const block_q8_1 *) vy) + sample_y*stride_sample_y + channel_y*stride_channel_y; 741 const int kbx_offset = sample_x*stride_sample_x + channel_x*stride_channel_x + row0*stride_row_x; 742 743 for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) { 744 const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx 745 746 // x block quant index when casting the quants to int 747 const int kqs = vdr * (tid % (qi/vdr)); 748 749 #if __CUDA_ARCH__ == GGML_CUDA_CC_DGX_SPARK 750 // start the next iterations' weight loads early 751 if constexpr (mmvq_should_prefetch(type)) { 752 // loop iterations, not blocks; q3_K and q6_K keep the distance in blocks they had with vdr 1 753 constexpr int pf_dist = type == GGML_TYPE_Q3_K || type == GGML_TYPE_Q6_K ? 1 : 2; 754 const int kbx_pf = kbx + pf_dist*blocks_per_iter; 755 if (kbx_pf < blocks_per_row_x) { 756 #pragma unroll 757 for (int i = 0; i < rows_per_cuda_block; ++i) { 758 const size_t off = (size_t)(kbx_offset + i*stride_row_x + kbx_pf) * ggml_cuda_type_traits::bs; 759 mmvq_prefetch_l2((const char *) vx + off); 760 if constexpr (has_fusion) { 761 if (use_gate) { 762 mmvq_prefetch_l2((const char *) vgate + off); 763 } 764 } 765 } 766 } 767 } 768 #endif 769 770 #pragma unroll 771 for (int j = 0; j < ncols_dst; ++j) { 772 #pragma unroll 773 for (int i = 0; i < rows_per_cuda_block; ++i) { 774 tmp[j][i] += vec_dot_q_cuda( 775 vx, &y[j*stride_col_y + kby], kbx_offset + i*stride_row_x + kbx, kqs); 776 if constexpr (has_fusion) { 777 if (use_gate) { 778 tmp_gate[j][i] += vec_dot_q_cuda( 779 vgate, &y[j*stride_col_y + kby], kbx_offset + i*stride_row_x + kbx, kqs); 780 } 781 } 782 } 783 } 784 } 785 786 __shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_dst][rows_per_cuda_block][warp_size]; 787 [[maybe_unused]] __shared__ float tmp_shared_gate[(has_fusion && (nwarps-1 > 0)) ? nwarps-1 : 1][ncols_dst][rows_per_cuda_block][warp_size]; 788 789 if (threadIdx.y > 0) { 790 #pragma unroll 791 for (int j = 0; j < ncols_dst; ++j) { 792 #pragma unroll 793 for (int i = 0; i < rows_per_cuda_block; ++i) { 794 tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i]; 795 if constexpr (has_fusion) { 796 if (use_gate) { 797 tmp_shared_gate[threadIdx.y-1][j][i][threadIdx.x] = tmp_gate[j][i]; 798 } 799 } 800 } 801 } 802 } 803 __syncthreads(); 804 if (threadIdx.y > 0) { 805 return; 806 } 807 808 dst += sample_dst*stride_sample_dst + channel_dst*stride_channel_dst + row0; 809 810 // sum up partial sums and write back result 811 #pragma unroll 812 for (int j = 0; j < ncols_dst; ++j) { 813 #pragma unroll 814 for (int i = 0; i < rows_per_cuda_block; ++i) { 815 #pragma unroll 816 for (int l = 0; l < nwarps-1; ++l) { 817 tmp[j][i] += tmp_shared[l][j][i][threadIdx.x]; 818 if constexpr (has_fusion) { 819 if (use_gate) { 820 tmp_gate[j][i] += tmp_shared_gate[l][j][i][threadIdx.x]; 821 } 822 } 823 } 824 tmp[j][i] = warp_reduce_sum(tmp[j][i]); 825 if constexpr (has_fusion) { 826 if (use_gate) { 827 tmp_gate[j][i] = warp_reduce_sum(tmp_gate[j][i]); 828 } 829 } 830 831 if (threadIdx.x == i && (rows_per_cuda_block == 1 || uint32_t(row0 + i) < stride_col_dst)) { 832 float result = tmp[j][i]; 833 if constexpr (has_fusion) { 834 if constexpr (type == GGML_TYPE_NVFP4) { 835 result *= x_scales; 836 } 837 result += x_biases[j]; 838 if (use_gate) { 839 float gate_value = tmp_gate[j][i]; 840 if constexpr (type == GGML_TYPE_NVFP4) { 841 gate_value *= gate_scales; 842 } 843 gate_value += gate_biases[j]; 844 switch (active_glu) { 845 case GGML_GLU_OP_SWIGLU: 846 result *= ggml_cuda_op_silu_single(gate_value); 847 break; 848 case GGML_GLU_OP_GEGLU: 849 result *= ggml_cuda_op_gelu_single(gate_value); 850 break; 851 case GGML_GLU_OP_SWIGLU_OAI: 852 result = ggml_cuda_op_swiglu_oai_single(gate_value, result); 853 break; 854 case GGML_GLU_OP_SWIGLU_CLAMP: 855 result = ggml_cuda_op_swiglu_clamp_single(gate_value, result, glu_limit); 856 break; 857 default: 858 result = result * gate_value; 859 break; 860 } 861 } 862 } 863 dst[j*stride_col_dst + i] = result; 864 } 865 } 866 } 867 868 if constexpr (!has_fusion) { 869 GGML_UNUSED_VARS(use_gate, use_bias, use_gate_bias, use_scale, use_gate_scale, active_glu, glu_limit, gate_bias, x_bias, x_scale, gate_scale, tmp_gate); 870 } 871 if constexpr (type != GGML_TYPE_NVFP4) { 872 GGML_UNUSED_VARS(use_scale, use_gate_scale, x_scale, gate_scale, x_scales, gate_scales); 873 } 874 } 875 876 // Dedicated MoE multi-token kernel. 877 // Grid: (ceil(nrows_x / c_rows_per_block), nchannels_dst) 878 // Block: (warp_size, ncols_dst) - each warp handles one token independently. 879 // No shared memory reduction needed since each warp works alone. 880 template 881 __launch_bounds__(get_mmvq_mmid_max_batch_for_device()*ggml_cuda_get_physical_warp_size(), 1) 882 static __global__ void mul_mat_vec_q_moe( 883 const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, 884 float * dst_ptr, 885 const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t nrows_x, 886 const uint32_t stride_row_x, const uint32_t stride_col_y, uint32_t stride_col_dst, 887 const uint32_t stride_channel_x, const uint32_t stride_channel_y, const uint32_t stride_channel_dst, 888 const uint32_t ncols_dst, const uint32_t ids_stride) { 889 const void * GGML_CUDA_RESTRICT vx = vx_ptr; 890 const void * GGML_CUDA_RESTRICT vy = vy_ptr; 891 const int32_t * GGML_CUDA_RESTRICT ids = ids_ptr; 892 float * GGML_CUDA_RESTRICT dst = dst_ptr; 893 894 constexpr int qk = ggml_cuda_type_traits::qk; 895 constexpr int qi = ggml_cuda_type_traits::qi; 896 constexpr int vdr = get_vdr_mmvq(type); 897 constexpr int warp_size = ggml_cuda_get_physical_warp_size(); 898 899 constexpr vec_dot_q_cuda_t vec_dot_q_cuda = get_vec_dot_q_cuda(type); 900 901 const bool shared_expert = has_fusion && fusion.shared_up && blockIdx.y == gridDim.y - 1; 902 if (shared_expert) { 903 vx = fusion.shared_up; 904 dst = fusion.shared_dst; 905 stride_col_dst = fusion.shared_stride_col_dst; 906 } 907 908 // fuse gate, bias, scales, and glu_op into the up projection 909 bool use_gate = false; 910 const void * vgate = nullptr; 911 const float * x_bias = nullptr; 912 const float * gate_bias = nullptr; 913 const float * x_scale = nullptr; 914 const float * gate_scale = nullptr; 915 ggml_glu_op active_glu = GGML_GLU_OP_SWIGLU; 916 float glu_limit = 0.0f; 917 918 if constexpr (has_fusion) { 919 use_gate = fusion.gate != nullptr; 920 vgate = shared_expert ? fusion.shared_gate : fusion.gate; 921 x_bias = (const float *) fusion.x_bias; 922 gate_bias = (const float *) fusion.gate_bias; 923 active_glu = fusion.glu_op; 924 glu_limit = fusion.glu_limit; 925 if constexpr (type == GGML_TYPE_NVFP4) { 926 x_scale = (const float *) fusion.x_scale; 927 gate_scale = (const float *) fusion.gate_scale; 928 } 929 } 930 931 const uint32_t token_idx = threadIdx.y; 932 const int row0 = c_rows_per_block*blockIdx.x; 933 const int blocks_per_row_x = ncols_x / qk; 934 constexpr int blocks_per_iter = vdr * warp_size / qi; 935 936 const uint32_t channel_dst = shared_expert ? 0 : blockIdx.y; 937 938 if (token_idx >= ncols_dst) { 939 return; 940 } 941 942 ggml_cuda_pdl_sync(); 943 const uint32_t channel_x = shared_expert ? 0 : ids[channel_dst + token_idx * ids_stride]; 944 const uint32_t channel_y = fastmodulo(channel_dst, nchannels_y); 945 946 const block_q8_1 * y = ((const block_q8_1 *) vy) + channel_y*stride_channel_y + token_idx*stride_col_y; 947 const int kbx_offset = channel_x*stride_channel_x + row0*stride_row_x; 948 949 // partial sum for each thread 950 float tmp[c_rows_per_block] = {0.0f}; 951 float tmp_gate[c_rows_per_block] = {0.0f}; 952 953 for (int kbx = threadIdx.x / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) { 954 const int kby = kbx * (qk/QK8_1); 955 const int kqs = vdr * (threadIdx.x % (qi/vdr)); 956 957 #pragma unroll 958 for (int i = 0; i < c_rows_per_block; ++i) { 959 tmp[i] += vec_dot_q_cuda(vx, &y[kby], kbx_offset + i*stride_row_x + kbx, kqs); 960 if constexpr (has_fusion) { 961 if (use_gate) { 962 tmp_gate[i] += vec_dot_q_cuda(vgate, &y[kby], kbx_offset + i*stride_row_x + kbx, kqs); 963 } 964 } 965 } 966 } 967 968 ggml_cuda_pdl_lc(); 969 970 // Warp-level reduction only - no shared memory needed 971 #pragma unroll 972 for (int i = 0; i < c_rows_per_block; ++i) { 973 tmp[i] = warp_reduce_sum(tmp[i]); 974 if constexpr (has_fusion) { 975 if (use_gate) { 976 tmp_gate[i] = warp_reduce_sum(tmp_gate[i]); 977 } 978 } 979 } 980 981 // Write results 982 if (threadIdx.x < c_rows_per_block && (c_rows_per_block == 1 || uint32_t(row0 + threadIdx.x) < nrows_x)) { 983 float result = tmp[threadIdx.x]; 984 if constexpr (has_fusion) { 985 const uint32_t bias_idx = channel_x*stride_channel_dst + row0 + threadIdx.x; 986 987 if constexpr (type == GGML_TYPE_NVFP4) { 988 if (x_scale) { 989 result *= x_scale[channel_x]; 990 } 991 } 992 if (x_bias) { 993 result += x_bias[bias_idx]; 994 } 995 if (use_gate) { 996 float gate_value = tmp_gate[threadIdx.x]; 997 if constexpr (type == GGML_TYPE_NVFP4) { 998 if (gate_scale) { 999 gate_value *= gate_scale[channel_x]; 1000 } 1001 } 1002 if (gate_bias) { 1003 gate_value += gate_bias[bias_idx]; 1004 } 1005 switch (active_glu) { 1006 case GGML_GLU_OP_SWIGLU: 1007 result *= ggml_cuda_op_silu_single(gate_value); 1008 break; 1009 case GGML_GLU_OP_GEGLU: 1010 result *= ggml_cuda_op_gelu_single(gate_value); 1011 break; 1012 case GGML_GLU_OP_SWIGLU_OAI: 1013 result = ggml_cuda_op_swiglu_oai_single(gate_value, result); 1014 break; 1015 case GGML_GLU_OP_SWIGLU_CLAMP: 1016 result = ggml_cuda_op_swiglu_clamp_single(gate_value, result, glu_limit); 1017 break; 1018 default: 1019 result = result * gate_value; 1020 break; 1021 } 1022 } 1023 } 1024 dst[channel_dst*stride_channel_dst + token_idx*stride_col_dst + row0 + threadIdx.x] = result; 1025 } 1026 1027 if constexpr (!has_fusion) { 1028 GGML_UNUSED_VARS(use_gate, tmp_gate, vgate, x_bias, gate_bias, active_glu, glu_limit, x_scale, gate_scale); 1029 } else if constexpr (type != GGML_TYPE_NVFP4) { 1030 GGML_UNUSED_VARS(x_scale, gate_scale); 1031 } 1032 } 1033 1034 template 1035 static std::pair calc_launch_params( 1036 const int ncols_dst, const int nrows_x, const int nchannels_dst, const int nsamples_or_ntokens, 1037 const int warp_size, const mmvq_parameter_table_id table_id, const bool small_k = false, const bool halve_iters = false) { 1038 const int nwarps = calc_nwarps(type, ncols_dst, table_id, small_k, halve_iters); 1039 const int rpb = calc_rows_per_block(type, ncols_dst, table_id, small_k, nwarps); 1040 const int64_t nblocks = (nrows_x + rpb - 1) / rpb; 1041 const dim3 block_nums(nblocks, nchannels_dst, nsamples_or_ntokens); 1042 const dim3 block_dims(warp_size, nwarps, 1); 1043 return {block_nums, block_dims}; 1044 } 1045 1046 template 1047 static void mul_mat_vec_q_switch_fusion( 1048 const void * vx, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, 1049 const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y, 1050 const uint32_t stride_col_dst, const uint3 channel_ratio, const uint32_t stride_channel_x, 1051 const uint32_t stride_channel_y, const uint32_t stride_channel_dst, const uint3 sample_ratio, 1052 const uint32_t stride_sample_x, const uint32_t stride_sample_y, const uint32_t stride_sample_dst, 1053 const dim3 & block_nums, const dim3 & block_dims, const int nbytes_shared, 1054 const uint32_t ids_stride, cudaStream_t stream) { 1055 1056 const bool has_fusion = fusion.gate != nullptr || fusion.x_bias != nullptr || fusion.gate_bias != nullptr || 1057 fusion.x_scale != nullptr || fusion.gate_scale != nullptr; 1058 if constexpr (c_ncols_dst == 1) { 1059 if (has_fusion) { 1060 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, nbytes_shared, stream); 1061 ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, 1062 vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, 1063 channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, 1064 sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); 1065 return; 1066 } 1067 } 1068 1069 GGML_ASSERT(!has_fusion && "fusion only supported for ncols_dst=1"); 1070 1071 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, nbytes_shared, stream); 1072 ggml_cuda_kernel_launch(mul_mat_vec_q, launch_params, 1073 vx, vy, ids, fusion, dst, ncols_x, nchannels_y, stride_row_x, stride_col_y, stride_col_dst, 1074 channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst, 1075 sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride); 1076 } 1077 1078 template 1079 static void mul_mat_vec_q_moe_launch( 1080 const void * vx, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, 1081 const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t nrows_x, 1082 const uint32_t stride_row_x, const uint32_t stride_col_y, const uint32_t stride_col_dst, 1083 const uint32_t stride_channel_x, const uint32_t stride_channel_y, const uint32_t stride_channel_dst, 1084 const uint32_t ncols_dst, const uint32_t ids_stride, 1085 const int warp_size, const int nchannels_dst, cudaStream_t stream) { 1086 1087 constexpr int rows_per_block = 2; // 2 gives best perf based on tuning 1088 const int64_t nblocks_rows = (nrows_x + rows_per_block - 1) / rows_per_block; 1089 const dim3 block_nums(nblocks_rows, nchannels_dst + (fusion.shared_up != nullptr)); 1090 const dim3 block_dims(warp_size, ncols_dst); 1091 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream); 1092 1093 const bool has_fusion = fusion.gate != nullptr || fusion.x_bias != nullptr || fusion.gate_bias != nullptr || 1094 fusion.x_scale != nullptr || fusion.gate_scale != nullptr; 1095 1096 if (has_fusion) { 1097 ggml_cuda_kernel_launch(mul_mat_vec_q_moe, launch_params, 1098 vx, vy, ids, fusion, dst, ncols_x, nchannels_y, nrows_x, 1099 stride_row_x, stride_col_y, stride_col_dst, 1100 stride_channel_x, stride_channel_y, stride_channel_dst, 1101 ncols_dst, ids_stride); 1102 } else { 1103 ggml_cuda_kernel_launch(mul_mat_vec_q_moe, launch_params, 1104 vx, vy, ids, fusion, dst, ncols_x, nchannels_y, nrows_x, 1105 stride_row_x, stride_col_y, stride_col_dst, 1106 stride_channel_x, stride_channel_y, stride_channel_dst, 1107 ncols_dst, ids_stride); 1108 } 1109 } 1110 1111 template 1112 static void mul_mat_vec_q_switch_ncols_dst( 1113 const void * vx, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, 1114 const int ncols_x, const int nrows_x, const int ncols_dst, 1115 const int stride_row_x, const int stride_col_y, const int stride_col_dst, 1116 const int nchannels_x, const int nchannels_y, const int nchannels_dst, 1117 const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst, 1118 const int nsamples_x, const int nsamples_dst, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst, 1119 const int ids_stride, cudaStream_t stream) { 1120 1121 GGML_ASSERT(ncols_x % ggml_blck_size(type) == 0); 1122 GGML_ASSERT(ncols_dst <= MMVQ_MAX_BATCH_SIZE); 1123 1124 const uint3 nchannels_y_fd = ids ? init_fastdiv_values(nchannels_y) : make_uint3(0, 0, 0); 1125 const uint3 channel_ratio_fd = ids ? make_uint3(0, 0, 0) : init_fastdiv_values(nchannels_dst / nchannels_x); 1126 const uint3 sample_ratio_fd = init_fastdiv_values(nsamples_dst / nsamples_x); 1127 1128 const int device = ggml_cuda_get_device(); 1129 const int cc = ggml_cuda_info().devices[device].cc; 1130 const int warp_size = ggml_cuda_info().devices[device].warp_size; 1131 const mmvq_parameter_table_id table_id = get_device_table_id(cc); 1132 1133 const bool has_ids = ids != nullptr; 1134 1135 // How the K loop divides up at the baseline block width, both decisions below use these. 1136 constexpr int qk = ggml_cuda_type_traits::qk; 1137 constexpr int qi = ggml_cuda_type_traits::qi; 1138 constexpr int vdr = get_vdr_mmvq(type); 1139 const int blocks_per_row_x = ncols_x / qk; 1140 const int blocks_per_iter_1warp = vdr * warp_size / qi; 1141 1142 const auto should_use_small_k = [&](int c_ncols_dst) { 1143 // When K is small, increase rows_per_block to match nwarps so each warp has more work to do 1144 // Trigger when the full thread block covers all K blocks in a single loop iteration and few threads remain idle. 1145 const int nwarps = calc_nwarps(type, c_ncols_dst, table_id); 1146 bool use = nwarps > 1 && blocks_per_row_x < nwarps * blocks_per_iter_1warp; 1147 1148 constexpr std::array iq_slow_turing = { 1149 GGML_TYPE_IQ3_XXS, 1150 GGML_TYPE_IQ3_S, 1151 }; 1152 constexpr std::array iq_slow_other = { 1153 GGML_TYPE_IQ1_S, GGML_TYPE_IQ1_M, GGML_TYPE_IQ2_XXS, GGML_TYPE_IQ2_XS, 1154 GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_XS, 1155 }; 1156 constexpr std::array slow_pascal = { 1157 GGML_TYPE_IQ3_S, 1158 GGML_TYPE_Q2_K, 1159 GGML_TYPE_Q3_K, 1160 }; 1161 1162 const bool is_nvidia_turing_plus = GGML_CUDA_CC_IS_NVIDIA(cc) && cc >= GGML_CUDA_CC_TURING; 1163 const bool is_nvidia_pascal_older = GGML_CUDA_CC_IS_NVIDIA(cc) && cc < GGML_CUDA_CC_VOLTA; 1164 1165 if (is_nvidia_turing_plus) { 1166 if (ncols_dst == 1 && 1167 std::find(iq_slow_turing.begin(), iq_slow_turing.end(), type) != iq_slow_turing.end()) { 1168 use = false; 1169 } 1170 } else if ((ncols_dst == 1 && std::find(iq_slow_other.begin(), iq_slow_other.end(), type) != iq_slow_other.end()) || 1171 (is_nvidia_pascal_older && std::find(slow_pascal.begin(), slow_pascal.end(), type) != slow_pascal.end()) || 1172 GGML_CUDA_CC_IS_RDNA(cc)) { 1173 use = false; 1174 } 1175 1176 return use; 1177 }; 1178 1179 // Whether doubling nwarps pays off on the ncols_dst == 1 path, where K sets the K loop trip count. 1180 const auto should_halve_iters = [&] { 1181 if (table_id != MMVQ_PARAMETERS_GB10) { 1182 return false; 1183 } 1184 1185 // Expert rows are gathered per token, so a wider block adds reduction work without reuse. 1186 if (has_ids) { 1187 return false; 1188 } 1189 1190 const int blocks_per_iter = calc_nwarps(type, 1, table_id) * blocks_per_iter_1warp; 1191 const int iters = (blocks_per_row_x + blocks_per_iter - 1) / blocks_per_iter; 1192 const int iters_wide = (blocks_per_row_x + blocks_per_iter * 2 - 1) / (blocks_per_iter * 2); 1193 1194 // An odd trip count leaves half the wider block idle for its last iteration, that tail is 1195 // only affordable once the loop is long enough to dilute it to an eighth of the work (observation). 1196 const int idle = iters_wide * 2 - iters; 1197 1198 return idle * 8 <= iters_wide * 2; 1199 }; 1200 1201 if (has_ids && ncols_dst > 1) { 1202 // Multi-token MUL_MAT_ID path - dedicated MoE kernel 1203 mul_mat_vec_q_moe_launch( 1204 vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, nrows_x, 1205 stride_row_x, stride_col_y, stride_col_dst, 1206 stride_channel_x, stride_channel_y, stride_channel_dst, 1207 ncols_dst, ids_stride, warp_size, nchannels_dst, stream); 1208 return; 1209 } 1210 1211 switch (ncols_dst) { 1212 case 1: { 1213 // static, else MSVC lambda capture breaks the constexpr uses below 1214 static constexpr int c_ncols_dst = 1; 1215 1216 // Tag types keep the flags compile-time, so __launch_bounds__ matches what is launched. 1217 const auto launch = [&](auto small_k_tag, auto halve_iters_tag) { 1218 constexpr bool c_small_k = decltype(small_k_tag)::value; 1219 // Types the table does not promote would compile a second, identical kernel. 1220 constexpr bool c_promoted = 1221 calc_nwarps(type, c_ncols_dst, MMVQ_PARAMETERS_GB10, false, true) != 1222 calc_nwarps(type, c_ncols_dst, MMVQ_PARAMETERS_GB10, false, false); 1223 1224 constexpr bool c_halve_iters = decltype(halve_iters_tag)::value && c_promoted; 1225 1226 const std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst + (fusion.shared_up != nullptr), 1227 nsamples_dst, warp_size, table_id, c_small_k, c_halve_iters); 1228 mul_mat_vec_q_switch_fusion( 1229 vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1230 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio_fd, 1231 stride_sample_x, stride_sample_y, stride_sample_dst, dims.first, dims.second, 0, ids_stride, 1232 stream); 1233 }; 1234 1235 if (should_use_small_k(c_ncols_dst)) { 1236 launch(std::true_type{}, std::false_type{}); 1237 } else if (should_halve_iters()) { 1238 launch(std::false_type{}, std::true_type{}); 1239 } else { 1240 launch(std::false_type{}, std::false_type{}); 1241 } 1242 } break; 1243 case 2: { 1244 constexpr int c_ncols_dst = 2; 1245 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1246 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1247 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1248 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1249 dims.first, dims.second, 0, ids_stride, stream); 1250 } break; 1251 case 3: { 1252 constexpr int c_ncols_dst = 3; 1253 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1254 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1255 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1256 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1257 dims.first, dims.second, 0, ids_stride, stream); 1258 } break; 1259 case 4: { 1260 constexpr int c_ncols_dst = 4; 1261 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1262 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1263 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1264 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1265 dims.first, dims.second, 0, ids_stride, stream); 1266 } break; 1267 case 5: { 1268 constexpr int c_ncols_dst = 5; 1269 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1270 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1271 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1272 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1273 dims.first, dims.second, 0, ids_stride, stream); 1274 } break; 1275 case 6: { 1276 constexpr int c_ncols_dst = 6; 1277 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1278 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1279 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1280 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1281 dims.first, dims.second, 0, ids_stride, stream); 1282 } break; 1283 case 7: { 1284 constexpr int c_ncols_dst = 7; 1285 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1286 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1287 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1288 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1289 dims.first, dims.second, 0, ids_stride, stream); 1290 } break; 1291 case 8: { 1292 constexpr int c_ncols_dst = 8; 1293 std::pair dims = calc_launch_params(c_ncols_dst, nrows_x, nchannels_dst, nsamples_dst, warp_size, table_id); 1294 mul_mat_vec_q_switch_fusion(vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst, 1295 channel_ratio_fd, stride_channel_x, stride_channel_y, stride_channel_dst, 1296 sample_ratio_fd, stride_sample_x, stride_sample_y, stride_sample_dst, 1297 dims.first, dims.second, 0, ids_stride, stream); 1298 } break; 1299 default: 1300 GGML_ABORT("fatal error"); 1301 } 1302 } 1303 static void mul_mat_vec_q_switch_type( 1304 const void * vx, const ggml_type type_x, const void * vy, const int32_t * ids, const ggml_cuda_mm_fusion_args_device fusion, float * dst, 1305 const int ncols_x, const int nrows_x, const int ncols_dst, 1306 const int stride_row_x, const int stride_col_y, const int stride_col_dst, 1307 const int nchannels_x, const int nchannels_y, const int nchannels_dst, 1308 const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst, 1309 const int nsamples_x, const int nsamples_dst, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst, 1310 const int ids_stride, cudaStream_t stream) { 1311 switch (type_x) { 1312 case GGML_TYPE_Q1_0: 1313 mul_mat_vec_q_switch_ncols_dst 1314 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1315 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1316 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1317 break; 1318 case GGML_TYPE_Q2_0: 1319 mul_mat_vec_q_switch_ncols_dst 1320 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1321 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1322 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1323 break; 1324 case GGML_TYPE_Q4_0: 1325 mul_mat_vec_q_switch_ncols_dst 1326 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1327 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1328 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1329 break; 1330 case GGML_TYPE_Q4_1: 1331 mul_mat_vec_q_switch_ncols_dst 1332 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1333 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1334 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1335 break; 1336 case GGML_TYPE_Q5_0: 1337 mul_mat_vec_q_switch_ncols_dst 1338 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1339 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1340 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1341 break; 1342 case GGML_TYPE_Q5_1: 1343 mul_mat_vec_q_switch_ncols_dst 1344 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1345 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1346 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1347 break; 1348 case GGML_TYPE_Q8_0: 1349 mul_mat_vec_q_switch_ncols_dst 1350 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1351 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1352 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1353 break; 1354 case GGML_TYPE_MXFP4: 1355 mul_mat_vec_q_switch_ncols_dst 1356 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1357 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1358 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1359 break; 1360 case GGML_TYPE_NVFP4: 1361 mul_mat_vec_q_switch_ncols_dst 1362 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1363 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1364 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1365 break; 1366 case GGML_TYPE_Q2_K: 1367 mul_mat_vec_q_switch_ncols_dst 1368 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1369 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1370 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1371 break; 1372 case GGML_TYPE_Q3_K: 1373 mul_mat_vec_q_switch_ncols_dst 1374 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1375 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1376 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1377 break; 1378 case GGML_TYPE_Q4_K: 1379 mul_mat_vec_q_switch_ncols_dst 1380 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1381 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1382 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1383 break; 1384 case GGML_TYPE_Q5_K: 1385 mul_mat_vec_q_switch_ncols_dst 1386 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1387 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1388 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1389 break; 1390 case GGML_TYPE_Q6_K: 1391 mul_mat_vec_q_switch_ncols_dst 1392 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1393 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1394 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1395 break; 1396 case GGML_TYPE_IQ2_XXS: 1397 mul_mat_vec_q_switch_ncols_dst 1398 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1399 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1400 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1401 break; 1402 case GGML_TYPE_IQ2_XS: 1403 mul_mat_vec_q_switch_ncols_dst 1404 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1405 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1406 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1407 break; 1408 case GGML_TYPE_IQ2_S: 1409 mul_mat_vec_q_switch_ncols_dst 1410 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1411 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1412 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1413 break; 1414 case GGML_TYPE_IQ3_XXS: 1415 mul_mat_vec_q_switch_ncols_dst 1416 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1417 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1418 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1419 break; 1420 case GGML_TYPE_IQ1_S: 1421 mul_mat_vec_q_switch_ncols_dst 1422 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1423 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1424 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1425 break; 1426 case GGML_TYPE_IQ1_M: 1427 mul_mat_vec_q_switch_ncols_dst 1428 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1429 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1430 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1431 break; 1432 case GGML_TYPE_IQ4_NL: 1433 mul_mat_vec_q_switch_ncols_dst 1434 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1435 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1436 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1437 break; 1438 case GGML_TYPE_IQ4_XS: 1439 mul_mat_vec_q_switch_ncols_dst 1440 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1441 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1442 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1443 break; 1444 case GGML_TYPE_IQ3_S: 1445 mul_mat_vec_q_switch_ncols_dst 1446 (vx, vy, ids, fusion, dst, ncols_x, nrows_x, ncols_dst, stride_row_x, stride_col_y, stride_col_dst, 1447 nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst, 1448 nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, ids_stride, stream); 1449 break; 1450 default: 1451 GGML_ABORT("fatal error"); 1452 } 1453 } 1454 1455 void ggml_cuda_mul_mat_vec_q( 1456 ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, 1457 const ggml_cuda_mm_fusion_args_host * fusion) { 1458 GGML_ASSERT( src1->type == GGML_TYPE_F32); 1459 GGML_ASSERT( dst->type == GGML_TYPE_F32); 1460 GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32); // Optional, used for batched GGML_MUL_MAT_ID. 1461 1462 GGML_TENSOR_BINARY_OP_LOCALS; 1463 1464 cudaStream_t stream = ctx.stream(); 1465 1466 const size_t ts_src0 = ggml_type_size(src0->type); 1467 const size_t ts_src1 = ggml_type_size(src1->type); 1468 const size_t ts_dst = ggml_type_size(dst->type); 1469 1470 GGML_ASSERT( nb00 == ts_src0); 1471 GGML_ASSERT( nb10 == ts_src1); 1472 GGML_ASSERT( nb0 == ts_dst); 1473 GGML_ASSERT(!ids || ids->nb[0] == ggml_type_size(ids->type)); 1474 1475 GGML_ASSERT(!ids || ne12 <= MMVQ_MAX_BATCH_SIZE); 1476 1477 const float * src1_d = (const float *) src1->data; 1478 const int32_t * ids_d = ids ? (const int32_t *) ids->data : nullptr; 1479 float * dst_d = (float *) dst->data; 1480 1481 ggml_cuda_mm_fusion_args_device fusion_local{}; 1482 1483 if (fusion) { 1484 const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc; 1485 GGML_ASSERT( !ids || dst->ne[2] <= get_mmvq_mmid_max_batch(src0->type, cc)); 1486 GGML_ASSERT( ids || dst->ne[1] == 1); 1487 // Scale fusion is only allowed for NVFP4 currently as the cost of checking this at run-time in the prologue is 1488 // non-negligible for some models such as gpt-oss-20b 1489 GGML_ASSERT((fusion->x_scale == nullptr && fusion->gate_scale == nullptr) || src0->type == GGML_TYPE_NVFP4); 1490 1491 if (fusion->shared_up) { 1492 GGML_ASSERT(ids && fusion->gate && fusion->shared_gate && fusion->shared_dst); 1493 GGML_ASSERT(!fusion->x_bias && !fusion->gate_bias && !fusion->x_scale && !fusion->gate_scale); 1494 GGML_ASSERT(ne11 == 1 && ne03 == 1 && ne13 == 1); 1495 GGML_ASSERT(fusion->shared_up->type == src0->type && fusion->shared_gate->type == src0->type); 1496 GGML_ASSERT(ggml_are_same_shape(fusion->shared_up, fusion->shared_gate)); 1497 GGML_ASSERT(ggml_is_contiguous(fusion->shared_up) && ggml_is_contiguous(fusion->shared_gate)); 1498 GGML_ASSERT(fusion->shared_up->ne[0] == ne00 && fusion->shared_up->ne[1] == ne01); 1499 GGML_ASSERT(fusion->shared_up->nb[1] == nb01 && ggml_is_matrix(fusion->shared_up)); 1500 GGML_ASSERT(fusion->shared_dst->type == GGML_TYPE_F32 && ggml_is_contiguous(fusion->shared_dst)); 1501 GGML_ASSERT(fusion->shared_dst->ne[0] == ne0 && fusion->shared_dst->ne[1] == ne2); 1502 fusion_local.shared_up = fusion->shared_up->data; 1503 fusion_local.shared_gate = fusion->shared_gate->data; 1504 fusion_local.shared_dst = (float *) fusion->shared_dst->data; 1505 fusion_local.shared_stride_col_dst = fusion->shared_dst->nb[1] / ts_dst; 1506 } 1507 1508 if (fusion->x_bias) { 1509 GGML_ASSERT(fusion->x_bias->type == GGML_TYPE_F32); 1510 GGML_ASSERT(fusion->x_bias->ne[0] == dst->ne[0]); 1511 GGML_ASSERT(!ids || fusion->x_bias->ne[1] == src0->ne[2]); 1512 fusion_local.x_bias = fusion->x_bias->data; 1513 } 1514 if (fusion->gate) { 1515 GGML_ASSERT(fusion->gate->type == src0->type && ggml_are_same_stride(fusion->gate, src0)); 1516 fusion_local.gate = fusion->gate->data; 1517 } 1518 if (fusion->gate_bias) { 1519 GGML_ASSERT(fusion->gate_bias->type == GGML_TYPE_F32); 1520 GGML_ASSERT(fusion->gate_bias->ne[0] == dst->ne[0]); 1521 GGML_ASSERT(!ids || fusion->gate_bias->ne[1] == src0->ne[2]); 1522 fusion_local.gate_bias = fusion->gate_bias->data; 1523 } 1524 if (fusion->x_scale) { 1525 GGML_ASSERT(fusion->x_scale->type == GGML_TYPE_F32); 1526 GGML_ASSERT(ggml_is_contiguous(fusion->x_scale)); 1527 GGML_ASSERT(ggml_nelements(fusion->x_scale) == (ids ? src0->ne[2] : 1)); 1528 fusion_local.x_scale = fusion->x_scale->data; 1529 } 1530 if (fusion->gate_scale) { 1531 GGML_ASSERT(fusion->gate_scale->type == GGML_TYPE_F32); 1532 GGML_ASSERT(ggml_is_contiguous(fusion->gate_scale)); 1533 GGML_ASSERT(ggml_nelements(fusion->gate_scale) == (ids ? src0->ne[2] : 1)); 1534 fusion_local.gate_scale = fusion->gate_scale->data; 1535 } 1536 fusion_local.glu_op = fusion->glu_op; 1537 fusion_local.glu_limit = fusion->glu_limit; 1538 } 1539 1540 // If src0 is a temporary compute buffer, clear any potential padding. 1541 if (ggml_backend_buffer_get_usage(src0->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE) { 1542 const size_t size_data = ggml_nbytes(src0); 1543 const size_t size_alloc = ggml_backend_buffer_get_alloc_size(src0->buffer, src0); 1544 if (size_alloc > size_data) { 1545 GGML_ASSERT(ggml_is_contiguously_allocated(src0)); 1546 GGML_ASSERT(!src0->view_src); 1547 CUDA_CHECK(cudaMemsetAsync((char *) src0->data + size_data, 0, size_alloc - size_data, stream)); 1548 } 1549 } 1550 1551 const int64_t ne10_padded = GGML_PAD(ne10, MATRIX_ROW_PADDING); 1552 ggml_cuda_pool_alloc src1_q8_1(ctx.pool(), ne13*ne12 * ne11*ne10_padded * sizeof(block_q8_1)/QK8_1); 1553 { 1554 const int64_t s11 = src1->nb[1] / ts_src1; 1555 const int64_t s12 = src1->nb[2] / ts_src1; 1556 const int64_t s13 = src1->nb[3] / ts_src1; 1557 quantize_row_q8_1_cuda(src1_d, nullptr, src1_q8_1.get(), src0->type, ne10, s11, s12, s13, ne10_padded, ne11, ne12, ne13, stream); 1558 } 1559 1560 const int64_t s01 = src0->nb[1] / ts_src0; 1561 const int64_t s11 = ne10_padded / QK8_1; 1562 const int64_t s1 = dst->nb[1] / ts_dst; 1563 const int64_t s02 = src0->nb[2] / ts_src0; 1564 const int64_t s2 = dst->nb[2] / ts_dst; 1565 const int64_t s03 = src0->nb[3] / ts_src0; 1566 const int64_t s3 = dst->nb[3] / ts_dst; 1567 1568 const int64_t s12 = ne11*s11; 1569 const int64_t s13 = ne12*s12; 1570 1571 // For MUL_MAT_ID the memory layout is different than for MUL_MAT: 1572 const int64_t ncols_dst = ids ? ne2 : ne1; 1573 const int64_t nchannels_y = ids ? ne11 : ne12; 1574 const int64_t nchannels_dst = ids ? ne1 : ne2; 1575 const int64_t stride_col_dst = ids ? s2 : s1; 1576 const int64_t stride_col_y = ids ? s12 : s11; 1577 const int64_t stride_channel_dst = ids ? s1 : s2; 1578 const int64_t stride_channel_y = ids ? s11 : s12; 1579 1580 const int64_t ids_stride = ids ? ids->nb[1] / ggml_type_size(ids->type) : 0; 1581 1582 mul_mat_vec_q_switch_type( 1583 src0->data, src0->type, src1_q8_1.get(), ids_d, fusion_local, dst_d, ne00, 1584 ne01, ncols_dst, s01, stride_col_y, stride_col_dst, 1585 ne02, nchannels_y, nchannels_dst, s02, stride_channel_y, stride_channel_dst, 1586 ne03, ne3, s03, s13, s3, ids_stride, stream); 1587 } 1588 1589 void ggml_cuda_op_mul_mat_vec_q( 1590 ggml_backend_cuda_context & ctx, 1591 const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i, 1592 const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols, 1593 const int64_t src1_padded_row_size, cudaStream_t stream) { 1594 1595 const int64_t ne00 = src0->ne[0]; 1596 const int64_t row_diff = row_high - row_low; 1597 1598 const int64_t ne10 = src1->ne[0]; 1599 GGML_ASSERT(ne10 % QK8_1 == 0); 1600 1601 const int64_t ne0 = dst->ne[0]; 1602 1603 int id = ggml_cuda_get_device(); 1604 1605 // the main device has a larger memory buffer to hold the results from all GPUs 1606 // nrows_dst == nrows of the matrix that the kernel writes into 1607 const int64_t nrows_dst = id == ctx.device ? ne0 : row_diff; 1608 1609 const int stride_row_x = ne00 / ggml_blck_size(src0->type); 1610 const int stride_col_y = src1_padded_row_size / QK8_1; 1611 1612 ggml_cuda_mm_fusion_args_device fusion_local{}; 1613 mul_mat_vec_q_switch_type( 1614 src0_dd_i, src0->type, src1_ddq_i, nullptr, fusion_local, dst_dd_i, ne00, row_diff, src1_ncols, stride_row_x, stride_col_y, nrows_dst, 1615 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, stream); 1616 1617 GGML_UNUSED_VARS(src1, dst, src1_ddf_i, src1_ncols, src1_padded_row_size); 1618 }