From b363b8dc03ff6576714013e19d00d17788ca7122 Mon Sep 17 00:00:00 2001 From: rafasmour <54501936+rafasmour@users.noreply.github.com> Date: Fri, 4 Dec 2026 11:27:26 +0000 Subject: [PATCH] fattn: v_perm dequant for q8_0/q4_0 tile loads, opt-in KV split cap and tile GQA padding for quantized decode - flash_attn_load_tile_quant: on HIP the half path (8 values per chunk) puts the bytes under 0x64 with v_perm and subtracts the offset, the values are bit-identical to the int8 -> half -> * d path - GGML_CUDA_FA_DECODE_SPLIT_CAP= (opt-in): at most n blocks per CU for RDNA3 decode (nb <= 8) with quantized K/V - GGML_CUDA_FA_TILE_GQA_PAD=1 (opt-in): GQA 3 -> 4 and 5/6/7 -> 8 in the direct quantized K/V tile path, padded Q columns are zeroed and their dst/meta/sink accesses skipped; the tile kernel derives head0 from gridDim.z at run time - test-backend-ops: eval cases nb 1..8 with GQA 3/6/7 (q8_0, q4_0, q8_0-q4_0, D256/512, sinks) and perf cases nb=1 at 32k/96k Assisted-by: Claude Sonnet 5.5 --- ggml/src/ggml-cuda/fattn-common.cuh | 65 ++++++++++++++++++++++++++--- ggml/src/ggml-cuda/fattn-tile.cuh | 52 +++++++++++++++++++---- tests/test-backend-ops.cpp | 35 ++++++++++++++++ 3 files changed, 139 insertions(+), 13 deletions(-) diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh index caa8ed8380f9..3660df134812 100644 --- a/ggml/src/ggml-cuda/fattn-common.cuh +++ b/ggml/src/ggml-cuda/fattn-common.cuh @@ -932,6 +932,39 @@ struct flash_attn_load_tile_impl half -> * d, with 1.75 instead of ~4 ALU ops per value. +template +static __device__ __forceinline__ void fattn_dequant8_f16(const char * __restrict__ row, const int i0, half2 * __restrict__ dst) { + uint32_t q[2]; + half d; + if constexpr (type_KV == GGML_TYPE_Q8_0) { + const block_q8_0 * x = (const block_q8_0 *) row + i0/QK8_0; + ggml_cuda_memcpy_1<8, 2>(q, x->qs + i0 % QK8_0); + q[0] ^= 0x80808080u; + q[1] ^= 0x80808080u; + d = x->d; + } else { + const block_q4_0 * x = (const block_q4_0 *) row + i0/QK4_0; + const int shift = 4*((i0 % QK4_0) / (QK4_0/2)); + ggml_cuda_memcpy_1<8, 2>(q, x->qs + i0 % (QK4_0/2)); + q[0] = (q[0] >> shift) & 0x0F0F0F0Fu; + q[1] = (q[1] >> shift) & 0x0F0F0F0Fu; + d = x->d; + } + const half2 d2 = __half2half2(d); + const half2 off2 = __float2half2_rn(type_KV == GGML_TYPE_Q8_0 ? -1152.0f : -1032.0f); +#pragma unroll + for (int j = 0; j < 2; ++j) { + const uint32_t lo = ggml_hip_perm(0x64646464u, q[j], 0x04010400u); + const uint32_t hi = ggml_hip_perm(0x64646464u, q[j], 0x04030402u); + dst[2*j + 0] = (*(const half2 *) &lo + off2) * d2; + dst[2*j + 1] = (*(const half2 *) &hi + off2) * d2; + } +} +#endif // defined(GGML_USE_HIP) + // Quantized K/V rows (Q8_0, Q4_0) dequantized while loading to the tile, used by the tile kernel on RDNA3. // KV points at row 0 and the column of the first element of the tile, a row is stride_KV blocks apart. template @@ -998,12 +1031,23 @@ struct flash_attn_load_tile_quant { tmp[l] = T_el(0.0f); } if (valid) { +#if defined(GGML_USE_HIP) + constexpr bool perm_f16 = std::is_same_v && ne_chunk == 8; +#else + constexpr bool perm_f16 = false; +#endif // defined(GGML_USE_HIP) + if constexpr (perm_f16) { +#if defined(GGML_USE_HIP) + fattn_dequant8_f16(row, k*ne_chunk, (half2 *) tmp); +#endif // defined(GGML_USE_HIP) + } else { #pragma unroll - for (int l = 0; l < ne_chunk; l += ne_1) { - if constexpr (type_KV == GGML_TYPE_Q8_0) { - dequantize_V_q8_0(row, tmp + l, k*ne_chunk + l); - } else { - dequantize_V_q4_0(row, tmp + l, k*ne_chunk + l); + for (int l = 0; l < ne_chunk; l += ne_1) { + if constexpr (type_KV == GGML_TYPE_Q8_0) { + dequantize_V_q8_0(row, tmp + l, k*ne_chunk + l); + } else { + dequantize_V_q4_0(row, tmp + l, k*ne_chunk + l); + } } } } @@ -1730,6 +1774,17 @@ void launch_fattn( } } + // GGML_CUDA_FA_DECODE_SPLIT_CAP=: at most n blocks per CU for decode with quantized K/V, fewer splits to combine (changes the summation order) + if (GGML_CUDA_CC_IS_RDNA3_0(cc) && Q->ne[1] <= 8 && !use_sparse && (ggml_is_quantized(K->type) || ggml_is_quantized(V->type))) { + static const int split_cap = [] { + const char * env = getenv("GGML_CUDA_FA_DECODE_SPLIT_CAP"); + return env != nullptr ? std::max(atoi(env), 0) : 0; + }(); + if (split_cap > 0) { + parallel_blocks = std::max(1, std::min(parallel_blocks, (split_cap*nsm) / ntiles_dst)); + } + } + blocks_num.x = ntiles_x; blocks_num.y = parallel_blocks; blocks_num.z = ntiles_z_gqa*K->ne[2]*Q->ne[3]; diff --git a/ggml/src/ggml-cuda/fattn-tile.cuh b/ggml/src/ggml-cuda/fattn-tile.cuh index 5d138079c3db..bc9733ab44f7 100644 --- a/ggml/src/ggml-cuda/fattn-tile.cuh +++ b/ggml/src/ggml-cuda/fattn-tile.cuh @@ -417,6 +417,15 @@ static inline bool ggml_cuda_fattn_tile_direct_kv(const int cc, const ggml_tenso (GGML_CUDA_FA_Q8_0_Q4_0 && K->type == GGML_TYPE_Q8_0 && V->type == GGML_TYPE_Q4_0); } +// GGML_CUDA_FA_TILE_GQA_PAD=1: pad GQA ratios 3, 5, 6, 7 to ncols2 4 or 8 for quantized K/V decode, so one block dequantizes each K/V head once +static inline bool ggml_cuda_fattn_tile_gqa_pad_enabled() { + static const bool enabled = [] { + const char * env = getenv("GGML_CUDA_FA_TILE_GQA_PAD"); + return env != nullptr && atoi(env) != 0; + }(); + return enabled; +} + // Function that performs a single iteration in for the KQ matrix multiplication: template @@ -826,18 +835,24 @@ static __global__ void flash_attn_tile( const int col_Q_0 = blockIdx.x * ncols1; // Index of the first Q column for this CUDA block to work on. - const int sequence = blockIdx.z / (ne02/ncols2); - const int head0 = blockIdx.z*ncols2 - sequence*ne02; // == blockIdx.z % (ne02/ncols2) + // gridDim.z == ntiles_z_gqa*ne12*ne03, if gqa_ratio % ncols2 != 0 the last tile of a K/V head has n_gqa_valid < ncols2 real Q heads + const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix. + const int ntiles_z_gqa = (gqa_ratio + ncols2 - 1) / ncols2; + const int sequence = blockIdx.z / (ne12*ntiles_z_gqa); + const int z_seq = blockIdx.z - sequence*(ne12*ntiles_z_gqa); + const int head_kv = z_seq / ntiles_z_gqa; + const int gqa_tile = z_seq - head_kv*ntiles_z_gqa; + const int head0 = head_kv*gqa_ratio + gqa_tile*ncols2; // == blockIdx.z % (ne02/ncols2) if gqa_ratio % ncols2 == 0 + const int n_gqa_valid = gqa_ratio - gqa_tile*ncols2; // >= ncols2 except for a padded tile // For sparse attention, ne11 is the capacity of each index list, not the KV cache length. const int64_t list = use_sparse ? int64_t(sequence % ne33)*gridDim.x + blockIdx.x : 0; const int32_t * rows = use_sparse ? KV_max + list*ne11 : use_kv_rows ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr; __builtin_assume(!use_kv_rows || rows != nullptr); const fattn_bias_indexed head_bias = bias_indexed ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias; - const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix. const float2 * Q_f2 = Q + s03*sequence + s02* head0; - const T_K * K_h2 = K + s13*sequence + s12*(head0 / gqa_ratio); - const T_V * V_h2 = V + s23*sequence + s22*(head0 / gqa_ratio); // K and V have same shape + const T_K * K_h2 = K + s13*sequence + s12*head_kv; + const T_V * V_h2 = V + s23*sequence + s22*head_kv; // K and V have same shape const half * maskh = bias_indexed || mask ? mask + s33*(sequence % ne33) + s32*(head0 % ne32) : nullptr; @@ -898,8 +913,11 @@ static __global__ void flash_attn_tile( for (int i0 = 0; i0 < DKQp; i0 += np*warp_size*cpy_ne_D) { if (i0 + np*warp_size*cpy_ne_D <= DKQ || i0 + (threadIdx.y % np)*(warp_size*cpy_ne_D) + threadIdx.x*cpy_ne_D < DKQ) { __align__(16) float tmp_f[cpy_ne_D] = {0.0f}; - ggml_cuda_memcpy_1 - (tmp_f, Q_f2 + c*s02 + fastmodulo(col_Q_0 + j, ne01)*s01 + i0/2 + (threadIdx.y % np)*(warp_size*(cpy_ne_D/2)) + threadIdx.x*(cpy_ne_D/2)); + // padded GQA columns stay zero + if (c < n_gqa_valid) { + ggml_cuda_memcpy_1 + (tmp_f, Q_f2 + c*s02 + fastmodulo(col_Q_0 + j, ne01)*s01 + i0/2 + (threadIdx.y % np)*(warp_size*(cpy_ne_D/2)) + threadIdx.x*(cpy_ne_D/2)); + } #pragma unroll for (int i1 = 0; i1 < cpy_ne_D; ++i1) { @@ -1036,7 +1054,7 @@ static __global__ void flash_attn_tile( #pragma unroll for (int jc0 = 0; jc0 < cpw; ++jc0) { const int jc = jc0 + (threadIdx.y/np)*cpw; - const float sink = sinks[head0 + jc % ncols2]; + const float sink = jc % ncols2 < n_gqa_valid ? sinks[head0 + jc % ncols2] : 0.0f; float KQ_max_new_j = fmaxf(KQ_max[jc0], sink); const float KQ_max_scale = expf(KQ_max[jc0] - KQ_max_new_j); @@ -1073,6 +1091,10 @@ static __global__ void flash_attn_tile( return; } + if (c >= n_gqa_valid) { + continue; // padded GQA column: no dst, no meta + } + const float rowsum = gridDim.y == 1 ? KQ_sum[jc0] : 1.0f; const int j_dst_unrolled = ((sequence*int(ne01.z) + col_Q_0 + j)*ne02 + head0 + c)*gridDim.y + blockIdx.y; @@ -1320,6 +1342,20 @@ static void launch_fattn_tile_switch_ncols2(ggml_backend_cuda_context & ctx, ggm return; } + // padded GQA groups, the kernel skips the columns >= gqa_ratio of the last tile + if constexpr (type_K != GGML_TYPE_F16 || type_V != GGML_TYPE_F16) { + if (use_gqa_opt && Q->ne[1] <= FATTN_TILE_DIRECT_KV_MAX_NE1 && ggml_cuda_fattn_tile_gqa_pad_enabled()) { + if (gqa_ratio == 3) { + launch_fattn_tile_switch_ncols1(ctx, dst); + return; + } + if (gqa_ratio >= 5 && gqa_ratio <= 7) { + launch_fattn_tile_switch_ncols1(ctx, dst); + return; + } + } + } + if (use_gqa_opt && gqa_ratio % 2 == 0) { launch_fattn_tile_switch_ncols1(ctx, dst); return; diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..9dd5bccc5bc4 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -16531,6 +16531,31 @@ static std::vector> make_test_cases_eval() { } } + // [w2 fa-decode-quant-kv] decode and verify batches nb 1..8 with q8_0/q4_0 K/V for GGML_CUDA_FA_TILE_GQA_PAD (GQA 3, 5, 6, 7 are padded to + // 4 or 8 Q columns per block, the padded columns must not be written) and GGML_CUDA_FA_DECODE_SPLIT_CAP (long kv, many KV splits). + for (auto [type_K, type_V] : {std::pair{GGML_TYPE_Q8_0, GGML_TYPE_Q8_0}, std::pair{GGML_TYPE_Q4_0, GGML_TYPE_Q4_0}, std::pair{GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}}) { + for (int64_t nr : {3, 6, 7}) { + for (int64_t nb : {1, 2, 3, 4, 5, 6, 7, 8}) { + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {nr, 1}, 4096, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + } + } + for (int64_t nb : {1, 4, 8}) { + // a single K/V head (the padded group is at the end of the Q tensor), a shorter kv, D = 512 with 2 K/V heads + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 1, {6, 1}, 4096, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 1280, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + test_cases.emplace_back(new test_flash_attn_ext(512, 512, 2, {6, 1}, 4096, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + // attention sinks, two sequences + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, nb, true, true, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {3, 2}, 4096, nb, true, true, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + } + for (int64_t nb : {1, 2, 3, 4, 5, 6, 7, 8}) { + // D = 512 with V a view of K (Gemma 4 global layers), GQA 6 and 8, a long kv for a capped split count + test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {6, 1}, 16384, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false, type_K == type_V)); + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 16384, nb, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + } + } + // [end w2 fa-decode-quant-kv] + // Sparse mask hint: supported decode/prefill layouts and dense fallbacks. test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512)); test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 2}, 4096, 3, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 768)); @@ -17654,6 +17679,16 @@ static std::vector> make_test_cases_perf() { for (int64_t nq : {256, 512, 1024, 2048}) { test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {8, 1}, 32768, nq, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false)); } + // [w2 fa-decode-quant-kv] single-token decode with quantized K/V at 32k and 96k: GQA 6 (Qwen3.8 27B), GQA 8 (Qwen4, Gemma 4 global layers, D = 512 with V a view of K), GQA 2 + for (auto [type_K, type_V] : {std::pair{GGML_TYPE_Q8_0, GGML_TYPE_Q8_0}, std::pair{GGML_TYPE_Q4_0, GGML_TYPE_Q4_0}, std::pair{GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}}) { + for (int64_t kv : {32768, 98304}) { + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {8, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {8, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false, type_K == type_V)); + test_cases.emplace_back(new test_flash_attn_ext(256, 256, 16, {2, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, type_K, type_V, {0, 2, 1, 3}, false)); + } + } + // [end w2 fa-decode-quant-kv] // prompt batches after 32k tokens: GQA 6 (Qwen3.8 27B), head size 512 with GQA 8 and V a view of K (Gemma 4 31B global layers), GQA 4 (Nemotron 3.5 Bolt E4B global layers) test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 32768 + 512, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false)); test_cases.emplace_back(new test_flash_attn_ext(512, 512, 4, {8, 1}, 32768 + 512, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false, true));