Commit 786da8594 for llama.cpp
commit 786da85942d22520a5384a7a8f447422a6b4994e
Author: Johannes Gäßler <johannesg@5d6.de>
Date: Sat Nov 28 08:57:29 2026 +0100
CUDA: consistent ncols2 selection for mma FA (#32626)
diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cu
index 6e8372791..b1afc3387 100644
--- a/ggml/src/ggml-cuda/fattn.cu
+++ b/ggml/src/ggml-cuda/fattn.cu
@@ -198,9 +198,7 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1(ggml_backend_cuda_con
ggml_cuda_flash_attn_ext_mma_f16_case<DKQ, DV, 64/ncols2, ncols2, type_K, type_V>(ctx, dst);
}
-template <int DKQ, int DV, ggml_type type_K, ggml_type type_V>
-static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
- const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
+static int ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(const int cc, const ggml_tensor * dst) {
const ggml_tensor * KQV = dst;
const ggml_tensor * Q = dst->src[0];
const ggml_tensor * K = dst->src[1];
@@ -230,71 +228,56 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_con
}
}
- GGML_ASSERT(Q->ne[2] % K->ne[2] == 0);
- const int gqa_ratio = Q->ne[2] / K->ne[2];
-
- // On Volta the GQA optimizations aren't as impactful vs. minimizing wasted compute:
- if (cc == GGML_CUDA_CC_VOLTA) {
- if (use_gqa_opt && gqa_ratio % 8 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
- return;
- }
-
- if (use_gqa_opt && gqa_ratio % 4 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
- return;
- }
-
- if constexpr (DKQ <= 256) {
- if (use_gqa_opt && gqa_ratio % 2 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
- return;
- }
-
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
- return;
- } else {
- GGML_ABORT("fatal error");
- }
+ if (!use_gqa_opt) {
+ return 1;
}
- // On RDNA it is preferable to minimize wasted compute vs. duplicate I/O for the mask.
- if (amd_wmma_available(cc)) {
- if (use_gqa_opt && gqa_ratio % 8 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
- return;
- }
+ GGML_ASSERT(Q->ne[2] % K->ne[2] == 0);
+ const int gqa_ratio = Q->ne[2] / K->ne[2];
- if (use_gqa_opt && gqa_ratio % 4 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
- return;
- }
+ const int ncols2_max = Q->ne[0] == 320 ? 32 : ((Q->ne[0] == 576 || Q->ne[0] == 192) ? 16 : 8);
- if (use_gqa_opt && gqa_ratio % 2 == 0) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
- return;
+ // On Volta and RDNA it is preferable to minimize wasted compute vs. duplicate I/O for the mask:
+ if (cc == GGML_CUDA_CC_VOLTA || (amd_wmma_available(cc) && gqa_ratio % 2 == 0)) {
+ int ncols2 = 1;
+ while (gqa_ratio % (2*ncols2) == 0 && ncols2 < ncols2_max) {
+ ncols2 *= 2;
}
+ return ncols2;
}
- if (use_gqa_opt && gqa_ratio > 4) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
- return;
+ int ncols2 = 1;
+ while (gqa_ratio > ncols2 && ncols2 < ncols2_max) {
+ ncols2 *= 2;
}
+ return ncols2;
+}
- if (use_gqa_opt && gqa_ratio > 2) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
- return;
- }
+template <int DKQ, int DV, ggml_type type_K, ggml_type type_V>
+static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+ const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
- if (use_gqa_opt && gqa_ratio > 1) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
- return;
- }
+ const int ncols2 = ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(cc, dst);
- if constexpr (DKQ <= 256) {
- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
- } else {
- GGML_ABORT("fatal error");
+ switch (ncols2) {
+ case 8:
+ ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
+ break;
+ case 4:
+ ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
+ break;
+ case 2:
+ ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
+ break;
+ case 1:
+ if constexpr (DKQ <= 256) {
+ ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
+ break;
+ } else {
+ GGML_ABORT("fatal error");
+ }
+ default:
+ GGML_ABORT("fatal error");
}
}
@@ -757,20 +740,16 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
return BEST_FATTN_KERNEL_MMA_F16;
}
- const int ncols2_max = Q->ne[0] == 320 ? 32 : ((Q->ne[0] == 576 || Q->ne[0] == 192) ? 16 : 8);
- int gqa_ratio_eff = 1;
- while (max_bias == 0.0f && (!mask || mask->ne[2] == 1) && gqa_ratio % (2*gqa_ratio_eff) == 0 && gqa_ratio_eff < ncols2_max) {
- gqa_ratio_eff *= 2;
- }
+ const int ncols2 = ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(cc, dst);
// the tile kernel for DKQ=576 only has the ncols2 = 4 and 16 variants
const best_fattn_kernel tile_kernel = Q->ne[0] == 576 && gqa_ratio % 4 != 0 ? BEST_FATTN_KERNEL_NONE : BEST_FATTN_KERNEL_TILE;
if (volta_mma_available(cc) && Q->ne[0] != 40 && Q->ne[0] != 72) {
- if (can_use_vector_kernel && Q->ne[1] * gqa_ratio_eff <= 2) {
+ if (can_use_vector_kernel && Q->ne[1] * ncols2 <= 2) {
return BEST_FATTN_KERNEL_VEC;
}
- if (Q->ne[1] * gqa_ratio_eff <= 16) {
+ if (Q->ne[1] * ncols2 <= 16) {
return tile_kernel; // On Volta tensor cores are only faster for sufficiently large matrices.
}
return BEST_FATTN_KERNEL_MMA_F16;
@@ -783,23 +762,23 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
// AMD MFMA needs a certain minimum batch size to outscale the tile kernel for large head sizes.
if (amd_mfma_available(cc) && Q->ne[0] != 40 && Q->ne[0] != 72) {
- if ((Q->ne[0] <= 64 && Q->ne[1] * gqa_ratio_eff > 8)) {
+ if ((Q->ne[0] <= 64 && Q->ne[1] * ncols2 > 8)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
- if ((Q->ne[0] <= 128 && Q->ne[1] * gqa_ratio_eff > 16)) {
+ if ((Q->ne[0] <= 128 && Q->ne[1] * ncols2 > 16)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
- if ((Q->ne[0] <= 256 && Q->ne[1] * gqa_ratio_eff > 64)) {
+ if ((Q->ne[0] <= 256 && Q->ne[1] * ncols2 > 64)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
- if (Q->ne[0] > 256 && gqa_opt_applies && Q->ne[1] * gqa_ratio_eff > 128) {
+ if (Q->ne[0] > 256 && gqa_opt_applies && Q->ne[1] * ncols2 > 128) {
return BEST_FATTN_KERNEL_MMA_F16;
}
}
// AMD WMMA is faster than the tile kernel if the wide tiles with high arithmetic intensity can be utilized.
if ((amd_wmma_available(cc) && gqa_opt_applies && Q->ne[0] <= 256) && Q->ne[0] != 40 && Q->ne[0] != 72 &&
- Q->ne[1] * gqa_ratio_eff > (Q->ne[0] <= 128 ? 8 : 16)) {
+ Q->ne[1] * ncols2 > (Q->ne[0] <= 128 ? 8 : 16)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 383eca0cd..d8e24b2f5 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -15040,6 +15040,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {2, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {4, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, { 3, 1}, 512, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
// dense-allocated (non-view) quant K/V at batch >= 64, in cache and native layouts
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false));