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.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 / mmq.cu` 533 lines ยท 22696 bytes Raw Blame History 1 #include "common.cuh" 2 #include "mmq.cuh" 3 #include "quantize.cuh" 4 #include "mmid.cuh" 5 6 #include 7 8 static void ggml_cuda_mul_mat_q_switch_type(ggml_backend_cuda_context & ctx, const mmq_args & args, cudaStream_t stream, const ggml_prec prec_src1) { 9 switch (args.type_x) { 10 case GGML_TYPE_Q1_0: 11 mul_mat_q_case(ctx, args, stream); 12 break; 13 case GGML_TYPE_Q2_0: 14 mul_mat_q_case(ctx, args, stream); 15 break; 16 case GGML_TYPE_Q4_0: 17 mul_mat_q_case(ctx, args, stream); 18 break; 19 case GGML_TYPE_Q4_1: 20 mul_mat_q_case(ctx, args, stream); 21 break; 22 case GGML_TYPE_Q5_0: 23 mul_mat_q_case(ctx, args, stream); 24 break; 25 case GGML_TYPE_Q5_1: 26 mul_mat_q_case(ctx, args, stream); 27 break; 28 case GGML_TYPE_Q8_0: 29 mul_mat_q_case(ctx, args, stream); 30 break; 31 // ----------------------------------------------------------------------- 32 case GGML_TYPE_Q2_K: 33 mul_mat_q_case(ctx, args, stream); 34 break; 35 case GGML_TYPE_Q3_K: 36 mul_mat_q_case(ctx, args, stream); 37 break; 38 case GGML_TYPE_Q4_K: 39 mul_mat_q_case(ctx, args, stream); 40 break; 41 case GGML_TYPE_Q5_K: 42 mul_mat_q_case(ctx, args, stream); 43 break; 44 case GGML_TYPE_Q6_K: 45 mul_mat_q_case(ctx, args, stream); 46 break; 47 // ----------------------------------------------------------------------- 48 case GGML_TYPE_IQ1_S: 49 mul_mat_q_case(ctx, args, stream); 50 break; 51 case GGML_TYPE_IQ2_XXS: 52 mul_mat_q_case(ctx, args, stream); 53 break; 54 case GGML_TYPE_IQ2_XS: 55 mul_mat_q_case(ctx, args, stream); 56 break; 57 case GGML_TYPE_IQ2_S: 58 mul_mat_q_case(ctx, args, stream); 59 break; 60 case GGML_TYPE_IQ3_XXS: 61 mul_mat_q_case(ctx, args, stream); 62 break; 63 case GGML_TYPE_IQ3_S: 64 mul_mat_q_case(ctx, args, stream); 65 break; 66 case GGML_TYPE_IQ4_XS: 67 mul_mat_q_case(ctx, args, stream); 68 break; 69 case GGML_TYPE_IQ4_NL: 70 mul_mat_q_case(ctx, args, stream); 71 break; 72 // ----------------------------------------------------------------------- 73 case GGML_TYPE_MXFP4: 74 // src1 at Q4 uses the native FP4 instructions, which are Blackwell-only 75 if (prec_src1 == GGML_PREC_Q4) { 76 mul_mat_q_case(ctx, args, stream); 77 break; 78 } 79 mul_mat_q_case(ctx, args, stream); 80 break; 81 case GGML_TYPE_NVFP4: 82 if (prec_src1 == GGML_PREC_Q4) { 83 mul_mat_q_case(ctx, args, stream); 84 break; 85 } 86 mul_mat_q_case(ctx, args, stream); 87 break; 88 default: 89 GGML_ABORT("fatal error"); 90 } 91 } 92 93 // overrides the src1 precision requested by the graph, "auto" keeps the requested one 94 static ggml_prec ggml_cuda_mmq_get_prec_env() { 95 const char * env_c = getenv("GGML_CUDA_MMQ_PREC"); 96 if (env_c == nullptr) { 97 return GGML_PREC_UNDEFINED; 98 } 99 std::string env_cpp = env_c; 100 for (char & c : env_cpp) { 101 c = std::tolower(c); 102 } 103 if (env_cpp == "q4") { 104 return GGML_PREC_Q4; 105 } 106 if (env_cpp == "q8") { 107 return GGML_PREC_Q8; 108 } 109 if (env_cpp != "auto") { 110 GGML_LOG_WARN("%s: Unknown value for GGML_CUDA_MMQ_PREC: '%s'. Available: 'q4', 'q8', 'auto'.\n", __func__, env_cpp.c_str()); 111 } 112 return GGML_PREC_UNDEFINED; 113 } 114 115 // src1 is quantized to Q8_1 unless the FP4 types can use 4-bit activations, in which case they 116 // default to the native W4A4 instructions on Blackwell. 117 static ggml_prec ggml_cuda_mmq_get_prec_src1(const ggml_tensor * src0, const ggml_tensor * dst, const int cc) { 118 static const ggml_prec prec_env = ggml_cuda_mmq_get_prec_env(); 119 120 ggml_prec prec = prec_env; 121 if (prec == GGML_PREC_UNDEFINED) { 122 prec = (ggml_prec) ggml_get_op_params_i32(dst, 3); 123 } 124 125 // Q4 only for the FP4 types on Blackwell 126 GGML_ASSERT(prec == GGML_PREC_UNDEFINED || prec == GGML_PREC_Q8 || prec == GGML_PREC_Q4); 127 const bool can_use_q4 = (src0->type == GGML_TYPE_NVFP4 || src0->type == GGML_TYPE_MXFP4) && blackwell_mma_available(cc); 128 if (prec == GGML_PREC_Q8 || !can_use_q4) { 129 return GGML_PREC_Q8; 130 } 131 return GGML_PREC_Q4; 132 } 133 134 // Tile width in ne11 (dense) / ne12 (MoE) direction with the fewest tiles that fits into shared memory, 0 if there is none. 135 static int ggml_cuda_mmq_get_J_best( 136 const ggml_type type, const int64_t ncols, const bool fallback, const int cc, const size_t smpbo, const ggml_prec prec_src1, 137 int * nthreads_best = nullptr) { 138 int J_best = 0; 139 int ntiles_J_best = INT_MAX; 140 141 for (int J = 8; J <= 128 && ntiles_J_best > 1; J += 8) { 142 const ggml_cuda_mmq_config config = ggml_cuda_mmq_get_config(type, J, fallback, cc, prec_src1); 143 if (config.type == GGML_TYPE_COUNT) { 144 continue; 145 } 146 147 if (mmq_get_nbytes_shared(config, cc) > smpbo) { 148 continue; 149 } 150 151 const int ntiles_x = (ncols + config.J - 1) / config.J; 152 153 if (ntiles_x < ntiles_J_best) { 154 J_best = J; 155 if (nthreads_best) { 156 *nthreads_best = config.nthreads; 157 } 158 ntiles_J_best = ntiles_x; 159 } 160 } 161 return J_best; 162 } 163 164 void ggml_cuda_mul_mat_q( 165 ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst, 166 const ggml_cuda_mm_fusion_args_host * fusion) { 167 GGML_ASSERT( src1->type == GGML_TYPE_F32); 168 GGML_ASSERT( dst->type == GGML_TYPE_F32); 169 GGML_ASSERT(!ids || ids->type == GGML_TYPE_I32); // Optional, used for batched GGML_MUL_MAT_ID. 170 171 // Fused gate/up/GLU: src0 is the up weight, dst the GLU output. 172 const ggml_tensor * gate = fusion ? fusion->gate : nullptr; 173 ggml_cuda_mm_fusion_args_device fusion_device = {}; 174 if (fusion) { 175 GGML_ASSERT(gate && !fusion->x_bias && !fusion->gate_bias && !fusion->x_scale && !fusion->gate_scale); 176 GGML_ASSERT(gate->type == src0->type && ggml_are_same_shape(gate, src0) && ggml_are_same_stride(gate, src0)); 177 fusion_device.gate = gate->data; 178 fusion_device.glu_op = fusion->glu_op; 179 fusion_device.glu_limit = fusion->glu_limit; 180 } 181 182 GGML_TENSOR_BINARY_OP_LOCALS; 183 184 cudaStream_t stream = ctx.stream(); 185 186 const int id = ggml_cuda_get_device(); 187 const int cc = ggml_cuda_info().devices[id].cc; 188 const size_t smpbo = ggml_cuda_info().devices[id].smpbo; 189 190 const size_t ts_src0 = ggml_type_size(src0->type); 191 const size_t ts_src1 = ggml_type_size(src1->type); 192 const size_t ts_dst = ggml_type_size(dst->type); 193 194 GGML_ASSERT( nb00 == ts_src0); 195 GGML_ASSERT( nb10 == ts_src1); 196 GGML_ASSERT( nb0 == ts_dst); 197 GGML_ASSERT(!ids || ids->nb[0] == ggml_type_size(ids->type)); 198 199 const char * src0_d = (const char *) src0->data; 200 const float * src1_d = (const float *) src1->data; 201 float * dst_d = (float *) dst->data; 202 203 // If src0 (or the fused gate weight) is a temporary compute buffer, clear any potential padding. 204 for (const ggml_tensor * t : { src0, gate }) { 205 if (t && ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_COMPUTE) { 206 const size_t size_data = ggml_nbytes(t); 207 const size_t size_alloc = ggml_backend_buffer_get_alloc_size(t->buffer, t); 208 if (size_alloc > size_data) { 209 GGML_ASSERT(ggml_is_contiguously_allocated(t)); 210 GGML_ASSERT(!t->view_src); 211 CUDA_CHECK(cudaMemsetAsync((char *) t->data + size_data, 0, size_alloc - size_data, stream)); 212 } 213 } 214 } 215 216 const int64_t ne10_padded = GGML_PAD(ne10, MATRIX_ROW_PADDING); 217 218 const int64_t s01 = src0->nb[1] / ts_src0; 219 const int64_t s1 = dst->nb[1] / ts_dst; 220 const int64_t s02 = src0->nb[2] / ts_src0; 221 const int64_t s2 = dst->nb[2] / ts_dst; 222 const int64_t s03 = src0->nb[3] / ts_src0; 223 const int64_t s3 = dst->nb[3] / ts_dst; 224 225 const bool fallback = ggml_cuda_mmq_needs_fallback(ne01); 226 227 // With fusion dst is the GLU output, not a MUL_MAT; the fused types always quantize src1 to Q8_1. 228 // TODO support for Q4 precision if both tensors allow it 229 const ggml_prec prec_src1 = gate ? GGML_PREC_Q8 : ggml_cuda_mmq_get_prec_src1(src0, dst, cc); 230 231 const bool use_native_fp4 = prec_src1 == GGML_PREC_Q4; 232 const size_t y_block_size = use_native_fp4 ? sizeof(block_fp4_mmq) : sizeof(block_q8_1_mmq); 233 const size_t y_values_per_block = use_native_fp4 ? QK_FP4_MMQ : QK8_1_MMQ; 234 235 int J_best = 0; 236 int nthreads_best = 0; 237 { 238 int64_t ncols_opt = ne11; 239 if (ids) { 240 const int64_t n_expert_used = ids->ne[0]; 241 ncols_opt = ne12; 242 243 // Each expert only sees ne12*n_expert_used/ne02 tokens on average. 244 // On RDNA3 and RDNA4 it is faster to pick the tile size against this value instead of ne12. 245 if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || GGML_CUDA_CC_IS_RDNA4(cc)) { 246 ncols_opt = (ne12*n_expert_used + ne02 - 1) / ne02; 247 } 248 } 249 250 J_best = ggml_cuda_mmq_get_J_best(src0->type, ncols_opt, fallback, cc, smpbo, prec_src1, &nthreads_best); 251 } 252 GGML_ASSERT(J_best > 0); 253 254 // A tile of size J can read in at most J - 1 extra columns. 255 // For simplicity, round up the padding of a full tile to a multiple of the number of bytes that nthreads can load in parallel. 256 const size_t src1_load_chunk_size = nthreads_best * sizeof(int); 257 const size_t src1_q8_1_padding = ((J_best * sizeof(block_q8_1_mmq) + src1_load_chunk_size - 1) / src1_load_chunk_size) 258 * src1_load_chunk_size; 259 260 if (!ids) { 261 const size_t nbytes_src1_q8_1 = ne13*ne12 * ne11*ne10_padded * y_block_size/y_values_per_block + src1_q8_1_padding; 262 ggml_cuda_pool_alloc src1_q8_1(ctx.pool(), nbytes_src1_q8_1); 263 ggml_cuda_pool_alloc src1_scale(ctx.pool()); 264 if (src0->type == GGML_TYPE_NVFP4 && use_native_fp4) { 265 src1_scale.alloc(ne13*ne12*ne11); 266 } 267 268 { 269 const int64_t s11 = src1->nb[1] / ts_src1; 270 const int64_t s12 = src1->nb[2] / ts_src1; 271 const int64_t s13 = src1->nb[3] / ts_src1; 272 if (use_native_fp4) { 273 static constexpr size_t align_float8 = 32; 274 const bool use_aligned_float8 = ggml_cuda_is_aligned(src1, align_float8); 275 static_assert(sizeof(block_fp4_mmq) == 4 * sizeof(block_q8_1)); 276 quantize_mmq_fp4_cuda(src1_d, nullptr, src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13, ne10_padded, 277 ne11, ne12, ne13, stream); 278 279 } else { 280 quantize_mmq_q8_1_cuda(src1_d, nullptr, src1_q8_1.get(), src0->type, ne10, s11, s12, s13, ne10_padded, 281 ne11, ne12, ne13, stream); 282 } 283 CUDA_CHECK(cudaGetLastError()); 284 } 285 286 // Stride depends on quantization format 287 const int64_t s12 = use_native_fp4 ? 288 ne11 * ne10_padded * sizeof(block_fp4_mmq) / (QK_FP4_MMQ * sizeof(int)) : 289 ne11 * ne10_padded * sizeof(block_q8_1) / (QK8_1 * sizeof(int)); 290 const int64_t s13 = ne12*s12; 291 292 const mmq_args args = { 293 src0_d, src0->type, (const int *) src1_q8_1.ptr, nullptr, nullptr, dst_d, 294 src0->type == GGML_TYPE_NVFP4 && use_native_fp4 ? src1_scale.ptr : nullptr, 295 ne00, ne01, ne1, s01, ne11, s1, 296 ne02, ne12, s02, s12, s2, 297 ne03, ne13, s03, s13, s3, 298 ne1, J_best, fusion_device}; 299 ggml_cuda_mul_mat_q_switch_type(ctx, args, stream, prec_src1); 300 return; 301 } 302 303 GGML_ASSERT(ne13 == 1); 304 GGML_ASSERT(nb12 % nb11 == 0); 305 GGML_ASSERT(nb2 % nb1 == 0); 306 307 const int64_t n_expert_used = ids->ne[0]; 308 const int64_t ne_get_rows = ne12 * n_expert_used; 309 GGML_ASSERT(ne1 == n_expert_used); 310 311 ggml_cuda_pool_alloc ids_src1(ctx.pool(), ne_get_rows); 312 ggml_cuda_pool_alloc ids_dst(ctx.pool(), ne_get_rows + J_best-1); // Needs to be padded for unconditional memory access. 313 ggml_cuda_pool_alloc expert_bounds(ctx.pool(), ne02 + 1); 314 315 // gate/up activations are broadcast across experts (ne11 == 1): quantize each token once and 316 // scatter to its slots. ids_src1 then holds the inverse map (token slot -> compact row). 317 const bool dedup_bcast = ne11 == 1 && n_expert_used > 1; 318 319 { 320 GGML_ASSERT(ids->nb[0] == ggml_element_size(ids)); 321 const int si1 = ids->nb[1] / ggml_element_size(ids); 322 const int sis1 = nb12 / nb11; 323 324 ggml_cuda_launch_mm_ids_helper((const int32_t *) ids->data, ids_src1.get(), ids_dst.get(), expert_bounds.get(), 325 ne02, ne12, n_expert_used, ne11, si1, sis1, /*write_inverse =*/ dedup_bcast, stream); 326 CUDA_CHECK(cudaGetLastError()); 327 } 328 329 const size_t nbytes_src1_q8_1 = ne12*n_expert_used*ne10_padded * y_block_size/y_values_per_block + src1_q8_1_padding; 330 ggml_cuda_pool_alloc src1_q8_1(ctx.pool(), nbytes_src1_q8_1); 331 ggml_cuda_pool_alloc src1_scale(ctx.pool()); 332 if (src0->type == GGML_TYPE_NVFP4 && use_native_fp4) { 333 src1_scale.alloc(ne12*n_expert_used); 334 } 335 336 const int64_t ne11_flat = ne12*n_expert_used; 337 const int64_t ne12_flat = 1; 338 const int64_t ne13_flat = 1; 339 340 { 341 const int64_t s11 = src1->nb[1] / ts_src1; 342 const int64_t s12 = src1->nb[2] / ts_src1; 343 const int64_t s13 = src1->nb[3] / ts_src1; 344 345 if (use_native_fp4) { 346 static constexpr size_t align_float8 = 32; 347 const bool use_aligned_float8 = ggml_cuda_is_aligned(src1, align_float8); 348 if (dedup_bcast) { 349 quantize_scatter_mmq_fp4_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, 350 /*stride_token=*/s12, ne10_padded, ne12, ne11_flat, n_expert_used, stream); 351 } else { 352 quantize_mmq_fp4_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src1_scale.ptr, src0->type, use_aligned_float8, ne10, s11, s12, s13, 353 ne10_padded, ne11_flat, ne12_flat, ne13_flat, stream); 354 } 355 } else if (dedup_bcast) { 356 quantize_scatter_mmq_q8_1_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src0->type, ne10, 357 /*stride_token=*/s12, ne10_padded, ne12, ne11_flat, n_expert_used, stream); 358 } else { 359 quantize_mmq_q8_1_cuda(src1_d, ids_src1.get(), src1_q8_1.get(), src0->type, ne10, s11, s12, s13, 360 ne10_padded, ne11_flat, ne12_flat, ne13_flat, stream); 361 } 362 CUDA_CHECK(cudaGetLastError()); 363 } 364 365 static_assert(QK_FP4_MMQ == 8 * QK_MXFP4, "QK_FP4_MMQ needs to be 8 * QK_MXFP4"); 366 const int64_t s12 = use_native_fp4 ? ne11 * ne10_padded * sizeof(block_fp4_mmq) / (QK_FP4_MMQ * sizeof(int)) : 367 ne11 * ne10_padded * sizeof(block_q8_1) / (QK8_1 * sizeof(int)); 368 const int64_t s13 = ne12*s12; 369 370 // Note that ne02 is used instead of ne12 because the number of y channels determines the z dimension of the CUDA grid. 371 const mmq_args args = { 372 src0_d, src0->type, (const int *) src1_q8_1.get(), ids_dst.get(), expert_bounds.get(), dst_d, 373 src1_scale.ptr, 374 ne00, ne01, ne_get_rows, s01, ne_get_rows, s1, 375 ne02, ne02, s02, s12, s2, 376 ne03, ne13, s03, s13, s3, 377 ne12, J_best, fusion_device}; 378 379 ggml_cuda_mul_mat_q_switch_type(ctx, args, stream, prec_src1); 380 } 381 382 bool ggml_cuda_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t n_experts) { 383 #ifdef GGML_CUDA_FORCE_CUBLAS 384 return false; 385 #endif // GGML_CUDA_FORCE_CUBLAS 386 387 bool mmq_supported; 388 389 switch (type) { 390 case GGML_TYPE_Q1_0: 391 case GGML_TYPE_Q2_0: 392 case GGML_TYPE_Q4_0: 393 case GGML_TYPE_Q4_1: 394 case GGML_TYPE_Q5_0: 395 case GGML_TYPE_Q5_1: 396 case GGML_TYPE_Q8_0: 397 // ------------------------------------------------- 398 case GGML_TYPE_Q2_K: 399 case GGML_TYPE_Q3_K: 400 case GGML_TYPE_Q4_K: 401 case GGML_TYPE_Q5_K: 402 case GGML_TYPE_Q6_K: 403 // ------------------------------------------------- 404 case GGML_TYPE_IQ1_S: 405 case GGML_TYPE_IQ2_XXS: 406 case GGML_TYPE_IQ2_XS: 407 case GGML_TYPE_IQ2_S: 408 case GGML_TYPE_IQ3_XXS: 409 case GGML_TYPE_IQ3_S: 410 case GGML_TYPE_IQ4_XS: 411 case GGML_TYPE_IQ4_NL: 412 // ------------------------------------------------- 413 case GGML_TYPE_MXFP4: 414 case GGML_TYPE_NVFP4: 415 mmq_supported = true; 416 break; 417 default: 418 mmq_supported = false; 419 break; 420 } 421 422 if (!mmq_supported) { 423 return false; 424 } 425 426 // MMQ tiles require at least 48 KiB per-block shared memory; fall back to BLAS otherwise. 427 { 428 const int id = ggml_cuda_get_device(); 429 const size_t smpbo = ggml_cuda_info().devices[id].smpbo; 430 if (smpbo < 48 * 1024) { 431 return false; 432 } 433 } 434 435 if (turing_mma_available(cc)) { 436 return true; 437 } 438 439 if (ggml_cuda_highest_compiled_arch(cc) < GGML_CUDA_CC_DP4A) { 440 // for MoE, mmq is faster even without native dp4a 441 // TODO: check if cards older than pascal might benefit from this as well 442 return cc >= GGML_CUDA_CC_PASCAL && n_experts > 0; 443 } 444 445 #ifdef GGML_CUDA_FORCE_MMQ 446 return true; 447 #endif //GGML_CUDA_FORCE_MMQ 448 449 if (GGML_CUDA_CC_IS_NVIDIA(cc)) { 450 return !fp16_mma_hardware_available(cc) || ne11 < MMQ_DP4A_MAX_BATCH_SIZE; 451 } 452 453 if (amd_mfma_available(cc)) { 454 // As of ROCM 7.0 rocblas/tensile performs very poorly on CDNA3 and hipblaslt (via ROCBLAS_USE_HIPBLASLT) 455 // performs better but is currently suffering from a crash on this architecture. 456 // TODO: Revisit when hipblaslt is fixed on CDNA3 457 if (GGML_CUDA_CC_IS_CDNA3(cc)) { 458 return true; 459 } 460 if (n_experts > 64 || ne11 <= 128) { 461 return true; 462 } 463 if (type == GGML_TYPE_Q4_0 || type == GGML_TYPE_Q4_1 || type == GGML_TYPE_Q5_0 || type == GGML_TYPE_Q5_1) { 464 return true; 465 } 466 if (ne11 <= 256 && (type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K)) { 467 return true; 468 } 469 return false; 470 } 471 472 if (amd_wmma_available(cc)) { 473 if (GGML_CUDA_CC_IS_RDNA3(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc)) { 474 // High expert counts are almost always better on MMQ due to 475 // the synchronization overhead in the cuBLAS/hipBLAS path: 476 // https://github.com/ggml-org/llama.cpp/pull/18202 477 if (n_experts >= 64) { 478 return true; 479 } 480 481 // For some quantization types MMQ can have lower peak TOPS than hipBLAS 482 // so it's only faster for sufficiently small batch sizes: 483 switch (type) { 484 case GGML_TYPE_Q2_K: 485 return ne11 <= 128; 486 case GGML_TYPE_Q6_K: 487 return ne11 <= (GGML_CUDA_CC_IS_RDNA3_0(cc) ? 128 : 256); 488 case GGML_TYPE_IQ2_XS: 489 case GGML_TYPE_IQ2_S: 490 return GGML_CUDA_CC_IS_RDNA3_5(cc) || GGML_CUDA_CC_IS_RDNA4_M(cc) || ne11 <= 128; 491 default: 492 return true; 493 } 494 } 495 496 // For RDNA4 MMQ is consistently faster than dequantization + hipBLAS: 497 // https://github.com/ggml-org/llama.cpp/pull/18537#issuecomment-`3706422301` 498 return true; 499 } 500 501 // gfx900 (Vega 10), gfx909, and gfx90c lack native dp4a, losing to dequant + hipBLAS 502 // for dense matrices; keep MMQ only for MoE, where the 503 // hipBLAS path is much slower. 504 if (cc == GGML_CUDA_CC_VEGA || GGML_CUDA_CC_IS_GCN_APU(cc)) { 505 return n_experts > 0; 506 } 507 508 // MUSA: the MMQ kernels compute wrong values on PH1 (MTT S5000). 509 if (cc == GGML_CUDA_CC_PH1) { 510 return false; 511 } 512 513 return (!GGML_CUDA_CC_IS_CDNA(cc)) || ne11 < MMQ_DP4A_MAX_BATCH_SIZE; 514 } 515 516 bool ggml_cuda_mmq_fusion_is_efficient(const ggml_type type, const int64_t nrows_x, const int64_t ncols, const int64_t nchannels, const int device) { 517 const int cc = ggml_cuda_info().devices[device].cc; 518 const int nsm = ggml_cuda_info().devices[device].nsm; 519 const size_t smpbo = ggml_cuda_info().devices[device].smpbo; 520 521 // Same tile width as ggml_cuda_mul_mat_q; the fused kernel has no fallback, quantizes src1 to Q8_1 and writes I/2 output rows per tile. 522 const bool fallback = false; 523 const int J_best = ggml_cuda_mmq_get_J_best(type, ncols, fallback, cc, smpbo, GGML_PREC_Q8); 524 if (J_best == 0) { 525 return false; 526 } 527 const int64_t nrows_tile = ggml_cuda_mmq_get_config(type, J_best, fallback, cc, GGML_PREC_Q8).I / 2; 528 const int64_t ntiles = (ncols + J_best - 1)/J_best * ((nrows_x + nrows_tile - 1)/nrows_tile) * nchannels; 529 // Same condition as launch_mul_mat_q uses to run the stream-k kernel with one block per tile. 530 const int64_t tiles_nwaves = (ntiles + nsm - 1) / nsm; 531 const int64_t tiles_efficiency_percent = 100 * ntiles / (nsm*tiles_nwaves); 532 return tiles_efficiency_percent >= 90; 533 }