diff --git a/ggml/src/ggml-cuda/fattn-mma-f16.cuh b/ggml/src/ggml-cuda/fattn-mma-f16.cuh index 7aaf89da0c52..7d7418f502d0 100644 --- a/ggml/src/ggml-cuda/fattn-mma-f16.cuh +++ b/ggml/src/ggml-cuda/fattn-mma-f16.cuh @@ -680,7 +680,7 @@ static __device__ __forceinline__ void flash_attn_ext_f16_iter( const int k0_diff = min(k0_stop, DKQ/2) - k0_start; // Only load the real head size from global memory. constexpr bool use_cp_async = nstages == 1; flash_attn_load_tile - (K_h2 + k0_start, tile_K, k0_diff, stride_K, k_VKQ_0, k_VKQ_sup, indices); + (K_h2 + fattn_kv_col_offset(2*k0_start), tile_K, k0_diff, stride_K, k_VKQ_0, k_VKQ_sup, indices); if (use_cp_async) { cp_async_wait_all(); } @@ -1088,7 +1088,7 @@ static __device__ __forceinline__ void flash_attn_ext_f16_iter( if (!V_is_K_view || i0_stop > 2*nbatch_K2) { constexpr bool use_cp_async = nstages == 1; flash_attn_load_tile - (V_h2 + i0_start/2, tile_V, i0_diff/2, stride_V, k_VKQ_0, k_VKQ_sup, indices); + (V_h2 + fattn_kv_col_offset(i0_start), tile_V, i0_diff/2, stride_V, k_VKQ_0, k_VKQ_sup, indices); if (use_cp_async) { cp_async_wait_all(); } @@ -2340,7 +2340,7 @@ void ggml_cuda_flash_attn_ext_mma_f16_case(ggml_backend_cuda_context & ctx, ggml } else if (logit_softcap == 0.0f) { constexpr bool use_logit_softcap = false; #if !defined(GGML_USE_MUSA) - if constexpr (ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(DKQ, DV, ncols1, ncols2)) { + if constexpr (ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(DKQ, DV, ncols1, ncols2) && type_K == GGML_TYPE_F16 && type_V == GGML_TYPE_F16) { if (ggml_cuda_flash_attn_ext_mma_f16_shall_use_sparse(cc, dst, ncols1, ncols2)) { constexpr bool use_sparse_kernel = true; fattn_kernel = flash_attn_ext_f16; @@ -2387,7 +2387,7 @@ void ggml_cuda_flash_attn_ext_mma_f16_case(ggml_backend_cuda_context & ctx, ggml const bool async_kv_preload = nstages == 2 && !use_sparse; launch_fattn - (ctx, dst, fattn_kernel, nwarps, nbytes_shared_total, nbatch_fa, true, true, true, use_sparse, warp_size_host, async_kv_preload); + (ctx, dst, fattn_kernel, nwarps, nbytes_shared_total, nbatch_fa, type_K == GGML_TYPE_F16, type_V == GGML_TYPE_F16, true, use_sparse, warp_size_host, async_kv_preload); } @@ -2460,6 +2460,18 @@ extern DECL_FATTN_MMA_F16_CASE(576, 512, 4, 16, GGML_TYPE_F16, GGML_TYPE_F16); extern DECL_FATTN_MMA_F16_CASE(320, 256, 1, 32, GGML_TYPE_F16, GGML_TYPE_F16); extern DECL_FATTN_MMA_F16_CASE(320, 256, 2, 32, GGML_TYPE_F16, GGML_TYPE_F16); +// RDNA3: q8_0/q4_0 K/V dequantized while loading the tiles for speculative verify batches (ggml_cuda_fattn_use_direct_mma in fattn.cu). +// Only the shape this selection reaches: head size 512 with 8 Q heads per K/V head and 4 queries per tile (the only head size 512 +// configuration of the RDNA3 kernel, 32 columns). +#if defined(GGML_USE_HIP) +#define DECL_FATTN_MMA_DIRECT_KV_CASES(type_K, type_V) \ + extern DECL_FATTN_MMA_F16_CASE(512, 512, 4, 8, type_K, type_V); +DECL_FATTN_MMA_DIRECT_KV_CASES(GGML_TYPE_Q8_0, GGML_TYPE_Q8_0) +DECL_FATTN_MMA_DIRECT_KV_CASES(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0) +DECL_FATTN_MMA_DIRECT_KV_CASES(GGML_TYPE_Q8_0, GGML_TYPE_Q4_0) +#undef DECL_FATTN_MMA_DIRECT_KV_CASES +#endif // defined(GGML_USE_HIP) + // For GLM 4.7 Flash extern DECL_FATTN_MMA_F16_CASE(576, 512, 4, 4, GGML_TYPE_F16, GGML_TYPE_F16); extern DECL_FATTN_MMA_F16_CASE(576, 512, 8, 4, GGML_TYPE_F16, GGML_TYPE_F16); diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cu index 09e3d9f33010..93d4a336ff0b 100644 --- a/ggml/src/ggml-cuda/fattn.cu +++ b/ggml/src/ggml-cuda/fattn.cu @@ -488,10 +488,93 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_head_size(ggml_backend_cuda_ } } +// RDNA3: speculative verify batches with q8_0/q4_0 K/V through the MMA kernel, which dequantizes K/V while loading the tiles +// (no F16 copy of the cache). The tile kernel with direct K/V (ggml_cuda_fattn_tile_direct_kv) is faster for fewer columns. +// Opt-in, GGML_CUDA_FA_DIRECT_MMA=1. GGML_CUDA_FA_DIRECT_MMA_MIN_COLS (default 17, at least 16) is the least number of columns +// (Q->ne[1] * Q heads per K/V head) from which this kernel is used. +// Only head size 512 with 8 Q heads per K/V head (Gemma 4 global layers) is built: head size 512 has a single RDNA3 configuration +// (32 columns), and head size 256 keeps Q in registers below 64 columns and spills (see ggml_cuda_fattn_mma_get_config_rdna3). +static bool ggml_cuda_fattn_use_direct_mma(const int cc, const ggml_tensor * dst) { +#if defined(GGML_USE_HIP) + static const bool enabled = [] { + const char * env = getenv("GGML_CUDA_FA_DIRECT_MMA"); + return env != nullptr && atoi(env) != 0; + }(); + static const int min_cols = [] { + const char * env = getenv("GGML_CUDA_FA_DIRECT_MMA_MIN_COLS"); + return std::max(env ? atoi(env) : 17, 16); + }(); + + if (!enabled) { + return false; + } + + const ggml_tensor * Q = dst->src[0]; + const ggml_tensor * K = dst->src[1]; + const ggml_tensor * V = dst->src[2]; + const ggml_tensor * mask = dst->src[3]; + + if (!amd_wmma_available(cc) || !GGML_CUDA_CC_IS_RDNA3_0(cc)) { + return false; + } + if (!mask || dst->src[5] != nullptr || ggml_flash_attn_ext_has_bias(dst) || ggml_get_op_params_i32(dst, 4) > 0) { + return false; // no mask, K/V rows (kv_rows), bias or sparse attention (n_kv_max) is left to the other kernels + } + + float max_bias = 0.0f; + float logit_softcap = 0.0f; + memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); + memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float)); + if (max_bias != 0.0f || logit_softcap != 0.0f || mask->ne[2] != 1 || K->ne[1] % FATTN_KQ_STRIDE != 0) { + return false; + } + + const bool types_ok = + (GGML_CUDA_FA_Q8_0_Q8_0 && K->type == GGML_TYPE_Q8_0 && V->type == GGML_TYPE_Q8_0) || + (GGML_CUDA_FA_Q4_0_Q4_0 && K->type == GGML_TYPE_Q4_0 && V->type == GGML_TYPE_Q4_0) || + (GGML_CUDA_FA_Q8_0_Q4_0 && K->type == GGML_TYPE_Q8_0 && V->type == GGML_TYPE_Q4_0); + if (!types_ok || Q->ne[0] != 512 || V->ne[0] != 512 || Q->ne[1] > FATTN_TILE_DIRECT_KV_MAX_NE1 || Q->ne[2] % K->ne[2] != 0) { + return false; + } + + // ncols2 > 1 implies the GQA optimization of the MMA kernel (mask shared by the Q heads, padded K/V). + const int ncols2 = ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(cc, dst); + return ncols2 == 8 && Q->ne[1]*ncols2 >= min_cols; +#else + GGML_UNUSED_VARS(cc, dst); + return false; +#endif // defined(GGML_USE_HIP) +} + static void ggml_cuda_flash_attn_ext_mma_f16(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { const ggml_tensor * K = dst->src[1]; const ggml_tensor * V = dst->src[2]; +#if defined(GGML_USE_HIP) + if (ggml_cuda_fattn_use_direct_mma(ggml_cuda_info().devices[ggml_cuda_get_device()].cc, dst)) { + // head size 512, 8 Q heads per K/V head, 4 queries per tile (up to 8 queries take two tiles) + if constexpr (GGML_CUDA_FA_Q8_0_Q8_0) { + if (K->type == GGML_TYPE_Q8_0 && V->type == GGML_TYPE_Q8_0) { + ggml_cuda_flash_attn_ext_mma_f16_case<512, 512, 4, 8, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0>(ctx, dst); + return; + } + } + if constexpr (GGML_CUDA_FA_Q4_0_Q4_0) { + if (K->type == GGML_TYPE_Q4_0 && V->type == GGML_TYPE_Q4_0) { + ggml_cuda_flash_attn_ext_mma_f16_case<512, 512, 4, 8, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0>(ctx, dst); + return; + } + } + if constexpr (GGML_CUDA_FA_Q8_0_Q4_0) { + if (K->type == GGML_TYPE_Q8_0 && V->type == GGML_TYPE_Q4_0) { + ggml_cuda_flash_attn_ext_mma_f16_case<512, 512, 4, 8, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0>(ctx, dst); + return; + } + } + GGML_ABORT("fatal error"); // the predicate only accepts the compiled type combinations + } +#endif // defined(GGML_USE_HIP) + if constexpr (GGML_CUDA_FA_F16_F16) { if (K->type == GGML_TYPE_F16 && V->type == GGML_TYPE_F16) { ggml_cuda_flash_attn_ext_mma_f16_switch_head_size(ctx, dst); @@ -867,6 +950,11 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const } } + // Speculative verify batches with enough columns: the MMA kernel dequantizing K/V while loading beats the direct tile kernel (opt-in). + if (gqa_opt_applies && ggml_cuda_fattn_use_direct_mma(cc, dst)) { + return BEST_FATTN_KERNEL_MMA_F16; + } + // The tile kernel reads quantized K/V directly for small batches and shares the reads between the Q heads of one KV head. if (gqa_opt_applies && ggml_cuda_fattn_tile_direct_kv(cc, dst)) { return BEST_FATTN_KERNEL_TILE; @@ -931,8 +1019,9 @@ size_t ggml_cuda_flash_attn_ext_get_alloc_size(int device, const ggml_tensor * d need_f16_V = true; break; case BEST_FATTN_KERNEL_MMA_F16: - need_f16_K = true; - need_f16_V = true; + // the direct K/V MMA kernel reads the quantized cache, no F16 copy + need_f16_K = !ggml_cuda_fattn_use_direct_mma(ggml_cuda_info().devices[device].cc, dst); + need_f16_V = need_f16_K; break; case BEST_FATTN_KERNEL_VEC: { const bool f16_fallback = ggml_cuda_get_fattn_vec_case(Q->ne[0], K->type, V->type) == nullptr; diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q4_0-q4_0-ncols1_4-ncols2_8.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q4_0-q4_0-ncols1_4-ncols2_8.cu new file mode 100644 index 000000000000..0ff5770db98d --- /dev/null +++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q4_0-q4_0-ncols1_4-ncols2_8.cu @@ -0,0 +1,7 @@ +// This file has been autogenerated by generate_cu_files.py, do not edit manually. + +#include "../fattn-mma-f16.cuh" + +#if defined(GGML_USE_HIP) +DECL_FATTN_MMA_F16_CASE(512, 512, 4, 8, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0); +#endif // defined(GGML_USE_HIP) diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q4_0-ncols1_4-ncols2_8.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q4_0-ncols1_4-ncols2_8.cu new file mode 100644 index 000000000000..58ac5362d661 --- /dev/null +++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q4_0-ncols1_4-ncols2_8.cu @@ -0,0 +1,7 @@ +// This file has been autogenerated by generate_cu_files.py, do not edit manually. + +#include "../fattn-mma-f16.cuh" + +#if defined(GGML_USE_HIP) +DECL_FATTN_MMA_F16_CASE(512, 512, 4, 8, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0); +#endif // defined(GGML_USE_HIP) diff --git a/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q8_0-ncols1_4-ncols2_8.cu b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q8_0-ncols1_4-ncols2_8.cu new file mode 100644 index 000000000000..cc5e012000a9 --- /dev/null +++ b/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-q8_0-q8_0-ncols1_4-ncols2_8.cu @@ -0,0 +1,7 @@ +// This file has been autogenerated by generate_cu_files.py, do not edit manually. + +#include "../fattn-mma-f16.cuh" + +#if defined(GGML_USE_HIP) +DECL_FATTN_MMA_F16_CASE(512, 512, 4, 8, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0); +#endif // defined(GGML_USE_HIP) diff --git a/ggml/src/ggml-cuda/template-instances/generate_cu_files.py b/ggml/src/ggml-cuda/template-instances/generate_cu_files.py index eeabf416328d..c43a8fd2362e 100755 --- a/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +++ b/ggml/src/ggml-cuda/template-instances/generate_cu_files.py @@ -113,6 +113,21 @@ def get_short_name(long_quant_name): ncols1=ncols1, ncols2=ncols2, head_size_kq=head_size_kq, head_size_v=head_size_v, type_k=type_k, type_v=type_v)) +# RDNA3: q8_0/q4_0 K/V dequantized while loading the tiles for speculative verify batches (fattn.cu, ggml_cuda_fattn_use_direct_mma). +# Only built for HIP. Head size 512 with 8 Q heads per K/V head (Gemma 4 global layers), 32 columns: the only head size 512 +# configuration of the RDNA3 mma kernel. Head size 256 is not built: with 16 or 32 columns its configuration keeps Q in registers and spills. +TYPE_PAIRS_DIRECT_KV = [("GGML_TYPE_Q8_0", "GGML_TYPE_Q8_0"), ("GGML_TYPE_Q4_0", "GGML_TYPE_Q4_0"), ("GGML_TYPE_Q8_0", "GGML_TYPE_Q4_0")] +CASES_DIRECT_KV = [(512, 4, 8)] # head size, ncols1, ncols2 + +for type_k, type_v in TYPE_PAIRS_DIRECT_KV: + for head_size, ncols1, ncols2 in CASES_DIRECT_KV: + with open(f"fattn-mma-f16-instance-{get_short_name(type_k)}-{get_short_name(type_v)}-ncols1_{ncols1}-ncols2_{ncols2}.cu", "w") as f: + f.write(SOURCE_FATTN_MMA_START) + f.write("#if defined(GGML_USE_HIP)\n") + f.write(SOURCE_FATTN_MMA_CASE.format( + ncols1=ncols1, ncols2=ncols2, head_size_kq=head_size, head_size_v=head_size, type_k=type_k, type_v=type_v)) + f.write("#endif // defined(GGML_USE_HIP)\n") + for type in TYPES_MMQ: with open(f"mmq-instance-{get_short_name(type)}.cu", "w") as f: f.write(SOURCE_MMQ.format(type=type)) diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..894b27e43ab7 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -16385,6 +16385,25 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {nr2, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16)); } + // fa-verify-direct-mma: speculative verify batches (2-8 tokens) with q8_0/q4_0 K/V, head sizes 256 and 512, 2-8 Q heads per K/V head, + // layout of the KV cache. On RDNA3 with GGML_CUDA_FA_DIRECT_MMA=1 the MMA kernel dequantizing K/V while loading the tiles takes the + // cases with enough columns (head size 512 with gqa 8 and nb >= 3), the others check that the fallbacks still match. + // kv is a multiple of 256 (the kernel needs a padded KV cache), 33000 only checks that an unpadded cache still works. + for (std::pair type_KV : { std::make_pair(GGML_TYPE_Q8_0, GGML_TYPE_Q8_0), std::make_pair(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0), + std::make_pair(GGML_TYPE_Q8_0, GGML_TYPE_Q4_0) }) { + for (int hs : { 256, 512 }) { + for (int gqa : { 2, 4, 8 }) { + for (int kv : { 4096, 33000, 33024 }) { + if (kv == 33000 && gqa != 8) continue; + for (int nb : { 2, 3, 4, 5, 6, 7, 8 }) { + test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {gqa, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, + type_KV.first, type_KV.second, {0, 2, 1, 3}, true, hs == 512 && type_KV.first == type_KV.second)); + } + } + } + } + } + for (int hsk : { 40, 64, 72, 80, 96, 128, 192, 256, 320, 512, 576 }) { for (int hsv : { 40, 64, 72, 80, 96, 128, 192, 256, 512 }) { if (hsk != 96 && hsk != 192 && hsk != 320 && hsk != 576 && hsk != hsv) continue; @@ -17639,6 +17658,17 @@ static std::vector> make_test_cases_perf() { test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 0)); test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {16, 1}, 49152, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 2048)); + // fa-verify-direct-mma: verify batches at depth with q8_0/q4_0 K/V (Gemma 4 global layers, head size 512, GQA 8) + for (std::pair type_KV : { std::make_pair(GGML_TYPE_Q8_0, GGML_TYPE_Q8_0), std::make_pair(GGML_TYPE_Q4_0, GGML_TYPE_Q4_0), + std::make_pair(GGML_TYPE_Q8_0, GGML_TYPE_Q4_0) }) { + for (int64_t kv : {32768, 98304}) { + for (int64_t nb : {3, 4, 7}) { + test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {8, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, + type_KV.first, type_KV.second, {0, 2, 1, 3}, true, type_KV.first == type_KV.second)); + } + } + } + // GLM 4.7 Flash gqa20 test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {20, 1}, 8192, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true)); test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {20, 1}, 8192, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true));