Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions ggml/src/ggml-cuda/fattn-common.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,39 @@ struct flash_attn_load_tile_impl<warp_size, nwarps, I, J, stride_tile, false, oo
}
};

#if defined(GGML_USE_HIP)
// 8 consecutive values of a q8_0/q4_0 row as half. v_perm puts each byte below 0x64 (half 1024 + byte), then the offset
// is subtracted and the scale applied: the same values as int8 -> half -> * d, with 1.75 instead of ~4 ALU ops per value.
template <ggml_type type_KV>
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 <ggml_type type_KV, int warp_size, int nwarps, int I, int J, int stride_tile, bool oob_check, bool KV_indirection, typename T_dst>
Expand Down Expand Up @@ -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<T_el, half> && 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<type_KV>(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<T_el, ne_1>(row, tmp + l, k*ne_chunk + l);
} else {
dequantize_V_q4_0<T_el, ne_1>(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<T_el, ne_1>(row, tmp + l, k*ne_chunk + l);
} else {
dequantize_V_q4_0<T_el, ne_1>(row, tmp + l, k*ne_chunk + l);
}
}
}
}
Expand Down Expand Up @@ -1730,6 +1774,17 @@ void launch_fattn(
}
}

// GGML_CUDA_FA_DECODE_SPLIT_CAP=<n>: 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];
Expand Down
52 changes: 44 additions & 8 deletions ggml/src/ggml-cuda/fattn-tile.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int nbatch_fa, int nbatch_K,
bool use_logit_softcap, bool oob_check, bool bias_indexed, bool use_sparse, bool use_kv_rows, typename T_vec_dot, typename T_K>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<sizeof(tmp_f)>
(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<sizeof(tmp_f)>
(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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<DKQ, DV, 4, use_logit_softcap, type_K, type_V, false, rows_nobias>(ctx, dst);
return;
}
if (gqa_ratio >= 5 && gqa_ratio <= 7) {
launch_fattn_tile_switch_ncols1<DKQ, DV, 8, use_logit_softcap, type_K, type_V, false, rows_nobias>(ctx, dst);
return;
}
}
}

if (use_gqa_opt && gqa_ratio % 2 == 0) {
launch_fattn_tile_switch_ncols1<DKQ, DV, 2, use_logit_softcap, type_K, type_V, false, rows_nobias>(ctx, dst);
return;
Expand Down
35 changes: 35 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16531,6 +16531,31 @@ static std::vector<std::unique_ptr<test_case>> 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));
Expand Down Expand Up @@ -17654,6 +17679,16 @@ static std::vector<std::unique_ptr<test_case>> 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));
Expand Down
Loading