LLM inference in C/C++
C++ ggml
CUDA: fix Gemma E4B MTP FlashAttention (#25148)
Johannes Gäßlere495d1e748b4
diff --git a/ggml/src/ggml-cuda/fattn-mma-f16.cuh b/ggml/src/ggml-cuda/fattn-mma-f16.cuhindex 83478a02cb..7f4cfd5511 100644--- a/ggml/src/ggml-cuda/fattn-mma-f16.cuh+++ b/ggml/src/ggml-cuda/fattn-mma-f16.cuh@@ -2003,6 +2003,10 @@ DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(112, 112, 64)DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(128, 128, 64)DECL_FATTN_MMA_F16_CASE_ALL_NCOLS2(256, 256, 64)+extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 2);+extern DECL_FATTN_MMA_F16_CASE(512, 512, 8, 2);+extern DECL_FATTN_MMA_F16_CASE(512, 512, 16, 2);+extern DECL_FATTN_MMA_F16_CASE(512, 512, 32, 2);extern DECL_FATTN_MMA_F16_CASE(512, 512, 2, 4);extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 4);extern DECL_FATTN_MMA_F16_CASE(512, 512, 8, 4);diff --git a/ggml/src/ggml-cuda/fattn-tile.cuh b/ggml/src/ggml-cuda/fattn-tile.cuhindex 0a099810e1..3e07a9f7e0 100644--- a/ggml/src/ggml-cuda/fattn-tile.cuh+++ b/ggml/src/ggml-cuda/fattn-tile.cuh@@ -76,6 +76,7 @@ static constexpr __host__ __device__ uint32_t ggml_cuda_fattn_tile_get_config_nvGGML_CUDA_FATTN_TILE_CONFIG_CASE(320, 256, 16, 256, 2, 64, 64)+ GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 2, 64, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 4, 128, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 8, 256, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 16, 256, 2, 64, 64)@@ -144,6 +145,7 @@ static constexpr __host__ __device__ uint32_t ggml_cuda_fattn_tile_get_config_nvGGML_CUDA_FATTN_TILE_CONFIG_CASE(320, 256, 16, 256, 2, 32, 64)+ GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 2, 64, 2, 32, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 4, 128, 2, 32, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 8, 256, 2, 32, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 16, 256, 2, 32, 64)@@ -219,6 +221,7 @@ static constexpr __host__ __device__ uint32_t ggml_cuda_fattn_tile_get_config_amGGML_CUDA_FATTN_TILE_CONFIG_CASE(320, 256, 32, 512, 1, 128, 64)+ GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 2, 64, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 4, 128, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 8, 256, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 16, 256, 2, 64, 64)@@ -296,6 +299,7 @@ static constexpr __host__ __device__ uint32_t ggml_cuda_fattn_tile_get_config_amGGML_CUDA_FATTN_TILE_CONFIG_CASE(320, 256, 32, 256, 2, 128, 64)+ GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 2, 64, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 4, 128, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 8, 256, 2, 64, 64)GGML_CUDA_FATTN_TILE_CONFIG_CASE(512, 512, 16, 256, 4, 64, 64)@@ -1308,12 +1312,12 @@ static void launch_fattn_tile_switch_ncols2(ggml_backend_cuda_context & ctx, ggmreturn;}- if constexpr (DV <= 256) {- if (use_gqa_opt && gqa_ratio % 2 == 0) {- launch_fattn_tile_switch_ncols1<DKQ, DV, 2, use_logit_softcap>(ctx, dst);- return;- }+ if (use_gqa_opt && gqa_ratio % 2 == 0) {+ launch_fattn_tile_switch_ncols1<DKQ, DV, 2, use_logit_softcap>(ctx, dst);+ return;+ }+ if constexpr (DV <= 256) {launch_fattn_tile_switch_ncols1<DKQ, DV, 1, use_logit_softcap>(ctx, dst);return;}diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cuindex d6c501b1d7..c3039211c1 100644--- a/ggml/src/ggml-cuda/fattn.cu+++ b/ggml/src/ggml-cuda/fattn.cu@@ -99,12 +99,12 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_conreturn;}- if constexpr (DKQ <= 256) {- if (use_gqa_opt && gqa_ratio > 1) {- ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2>(ctx, dst);- return;- }+ if (use_gqa_opt && gqa_ratio > 1) {+ ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2>(ctx, dst);+ return;+ }+ if constexpr (DKQ <= 256) {ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1>(ctx, dst);} else {GGML_ABORT("fatal error");diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cuindex 2e1883af40..0c1a37fd00 100644--- a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu+++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu@@ -8,3 +8,4 @@ DECL_FATTN_MMA_F16_CASE(96, 96, 16, 2);DECL_FATTN_MMA_F16_CASE(112, 112, 16, 2);DECL_FATTN_MMA_F16_CASE(128, 128, 16, 2);DECL_FATTN_MMA_F16_CASE(256, 256, 16, 2);+DECL_FATTN_MMA_F16_CASE(512, 512, 16, 2);diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cuindex 407b6cf4c7..d5a4d160a4 100644--- a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu+++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu@@ -8,3 +8,4 @@ DECL_FATTN_MMA_F16_CASE(96, 96, 32, 2);DECL_FATTN_MMA_F16_CASE(112, 112, 32, 2);DECL_FATTN_MMA_F16_CASE(128, 128, 32, 2);DECL_FATTN_MMA_F16_CASE(256, 256, 32, 2);+DECL_FATTN_MMA_F16_CASE(512, 512, 32, 2);diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cuindex 5e46685024..d8e7588cfc 100644--- a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu+++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu@@ -8,3 +8,4 @@ DECL_FATTN_MMA_F16_CASE(96, 96, 4, 2);DECL_FATTN_MMA_F16_CASE(112, 112, 4, 2);DECL_FATTN_MMA_F16_CASE(128, 128, 4, 2);DECL_FATTN_MMA_F16_CASE(256, 256, 4, 2);+DECL_FATTN_MMA_F16_CASE(512, 512, 4, 2);diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cuindex a6e6f093dc..e48e28c4e4 100644--- a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu+++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu@@ -8,3 +8,4 @@ DECL_FATTN_MMA_F16_CASE(96, 96, 8, 2);DECL_FATTN_MMA_F16_CASE(112, 112, 8, 2);DECL_FATTN_MMA_F16_CASE(128, 128, 8, 2);DECL_FATTN_MMA_F16_CASE(256, 256, 8, 2);+DECL_FATTN_MMA_F16_CASE(512, 512, 8, 2);diff --git a/ggml/src/ggml-cuda/template-instances/generate_cu_files.py b/ggml/src/ggml-cuda/template-instances/generate_cu_files.pyindex af05a9eff7..614b1566c7 100755--- a/ggml/src/ggml-cuda/template-instances/generate_cu_files.py+++ b/ggml/src/ggml-cuda/template-instances/generate_cu_files.py@@ -92,7 +92,7 @@ for ncols in [8, 16, 32, 64]:continueif head_size_kq == 320 and ncols2 != 32: # Mistral Small 4continue- if head_size_kq == 512 and ncols2 not in (4, 8): # Gemma 4+ if head_size_kq == 512 and ncols2 not in (2, 4, 8): # Gemma 4 (+ MTP)continueif head_size_kq == 576 and ncols2 not in (4, 16, 32): # Deepseek, GLM 4.7 Flashcontinue