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
85 changes: 76 additions & 9 deletions ggml/src/ggml-cuda/fattn-common.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,60 @@ static __device__ __forceinline__ void dequantize_V_q8_0(const void * __restrict
}
}

// RDNA3 sparse flash attention (mask -> index list compaction, K/V read through the list), opt-in.
// GGML_CUDA_FA_SPARSE_RDNA3=1 enable (default 0, the result differs from the dense kernels by the accumulation order only)
// GGML_CUDA_FA_SPARSE_MIN_KV=<n> minimum number of K/V cells (default 8192)
// GGML_CUDA_FA_SPARSE_MAX_FRAC=<d> require n_kv_max <= n_kv/d (default 4)
#ifdef GGML_USE_HIP
struct ggml_cuda_fattn_sparse_rdna3_config {
bool enabled;
int64_t min_kv;
int64_t max_frac;
};

static inline const ggml_cuda_fattn_sparse_rdna3_config & ggml_cuda_fattn_sparse_rdna3_get_config() {
static const ggml_cuda_fattn_sparse_rdna3_config config = [] {
ggml_cuda_fattn_sparse_rdna3_config c = {false, 8192, 4};
const char * env_on = getenv("GGML_CUDA_FA_SPARSE_RDNA3");
const char * env_kv = getenv("GGML_CUDA_FA_SPARSE_MIN_KV");
const char * env_frac = getenv("GGML_CUDA_FA_SPARSE_MAX_FRAC");
c.enabled = env_on != nullptr && atoi(env_on) != 0;
if (env_kv != nullptr && atoll(env_kv) >= 0) {
c.min_kv = atoll(env_kv);
}
if (env_frac != nullptr && atoll(env_frac) > 0) {
c.max_frac = atoll(env_frac);
}
return c;
}();
return config;
}

enum ggml_cuda_fattn_sparse_rdna3_path {
GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_F16 = 0,
GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q8_0,
GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q4_0,
GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q8_0_Q4_0,
GGML_CUDA_FATTN_SPARSE_RDNA3_WMMA_8x8,
GGML_CUDA_FATTN_SPARSE_RDNA3_N_PATHS,
};

// log once per path so that the logs show which path was taken
static inline void ggml_cuda_fattn_sparse_rdna3_log_once(const ggml_cuda_fattn_sparse_rdna3_path path, const ggml_tensor * dst) {
static bool logged[GGML_CUDA_FATTN_SPARSE_RDNA3_N_PATHS] = {};
if (logged[path]) {
return;
}
logged[path] = true;

static const char * const names[GGML_CUDA_FATTN_SPARSE_RDNA3_N_PATHS] = {
"tile f16", "tile-direct q8_0", "tile-direct q4_0", "tile-direct q8_0/q4_0", "wmma 8x8",
};
GGML_LOG_INFO("%s: RDNA3 sparse FA: %s (D=%d, n_kv=%d, n_kv_max=%d, n_q=%d)\n", __func__, names[path],
int(dst->src[0]->ne[0]), int(dst->src[1]->ne[1]), int(ggml_get_op_params_i32(dst, 4)), int(dst->src[0]->ne[1]));
}
#endif // GGML_USE_HIP

template <int warp_size, int nwarps, int I, int J, int stride_tile,
bool use_cp_async, bool oob_check, bool use_sparse, bool KV_indirection, bool use_swizzle,
typename T_src, typename T_dst>
Expand Down Expand Up @@ -822,10 +876,28 @@ struct flash_attn_load_tile_impl<warp_size, nwarps, I, J, stride_tile, false, oo
for (int k0 = k0_start; k0 < k0_stop; k0 += stride_k) {
const int k = k0 + (stride_k == warp_size ? threadIdx.x : threadIdx.x % stride_k);

half2 * dst;
if constexpr (use_swizzle) {
dst = ggml_cuda_mma::swizzle<stride_tile>(tile_KV, i*stride_tile + k*cpy_ne, i);
} else {
dst = tile_KV + i*stride_tile + k*cpy_ne;
}

const half2 * src;
if constexpr (use_sparse) {
// zero-fill from registers: a runtime choice between a global pointer and the local zero array forces scratch memory
const int32_t index = i < i_sup ? indices[k_VKQ_0 + i] : -1;
src = index >= 0 ? KV + int64_t(index)*stride_KV + k*cpy_ne : zero;
if (index >= 0) {
ggml_cuda_memcpy_1<cpy_nb>(dst, KV + int64_t(index)*stride_KV + k*cpy_ne);
} else {
half2 tmp_zero[cpy_ne];
#pragma unroll
for (int l = 0; l < cpy_ne; ++l) {
tmp_zero[l] = make_half2(0.0f, 0.0f);
}
ggml_cuda_memcpy_1<cpy_nb>(dst, tmp_zero);
}
continue;
} else if (!oob_check || i < i_sup) {
if constexpr (KV_indirection) {
const int64_t i_KV = indices ? max(indices[k_VKQ_0 + i], 0) : k_VKQ_0 + i;
Expand All @@ -837,13 +909,6 @@ struct flash_attn_load_tile_impl<warp_size, nwarps, I, J, stride_tile, false, oo
src = zero;
}

half2 * dst;
if constexpr (use_swizzle) {
dst = ggml_cuda_mma::swizzle<stride_tile>(tile_KV, i*stride_tile + k*cpy_ne, i);
} else {
dst = tile_KV + i*stride_tile + k*cpy_ne;
}

ggml_cuda_memcpy_1<cpy_nb>(dst, src);
}
}
Expand Down Expand Up @@ -934,6 +999,8 @@ struct flash_attn_load_tile_impl<warp_size, nwarps, I, J, stride_tile, false, oo

// 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.
// With a sparse index list (KV_indirection) row k_VKQ_0 + i is indices[k_VKQ_0 + i]; rows at or past the list length (i >= i_sup,
// only in the oob_check iteration) are not loaded and read as zero, the caller masks them out of the softmax.
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>
struct flash_attn_load_tile_quant {
using T_blk = std::conditional_t<type_KV == GGML_TYPE_Q8_0, block_q8_0, block_q4_0>;
Expand Down Expand Up @@ -1019,7 +1086,7 @@ struct flash_attn_load_tile_quant {
template <int warp_size, int nwarps, int I, int J, int stride_tile, bool oob_check, bool use_sparse, bool KV_indirection> \
struct flash_attn_load_tile_impl<warp_size, nwarps, I, J, stride_tile, false, oob_check, use_sparse, KV_indirection, false, \
T_BLK, T_DST> { \
static_assert(!use_sparse, "sparse attention reads F16 K/V only"); \
static_assert(!use_sparse || KV_indirection, "use_sparse requires KV_indirection"); \
static __device__ __forceinline__ void load( \
const T_BLK * const __restrict__ KV, T_DST * const __restrict__ tile_KV, const int D2, const int stride_KV, \
const int k_VKQ_0, const int i_sup, const int32_t * const __restrict__ indices) { \
Expand Down
5 changes: 5 additions & 0 deletions ggml/src/ggml-cuda/fattn-mma-f16.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,11 @@ void ggml_cuda_flash_attn_ext_mma_f16_case(ggml_backend_cuda_context & ctx, ggml
constexpr bool use_sparse_kernel = true;
fattn_kernel = flash_attn_ext_f16<DKQ, DV, ncols1, ncols2, use_logit_softcap, V_is_K_view, use_sparse_kernel, type_K, type_V>;
use_sparse = true;
#ifdef GGML_USE_HIP
if (GGML_CUDA_CC_IS_RDNA3_0(cc)) {
ggml_cuda_fattn_sparse_rdna3_log_once(GGML_CUDA_FATTN_SPARSE_RDNA3_WMMA_8x8, dst);
}
#endif // GGML_USE_HIP

static bool shared_memory_limit_raised[GGML_CUDA_MAX_DEVICES] = {false};
if (!shared_memory_limit_raised[id]) {
Expand Down
30 changes: 26 additions & 4 deletions ggml/src/ggml-cuda/fattn-tile.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,19 @@ static __global__ void flash_attn_tile(

// Skip unused kernel variants for faster compilation:

#ifndef GCN
#if !defined(GCN) && !defined(RDNA3_0)
if constexpr (use_sparse) {
NO_DEVICE_CODE;
return;
}
#endif // GCN
#endif // !defined(GCN) && !defined(RDNA3_0)
#if defined(RDNA3_0)
// the D512 sparse variants spill (256 VGPRs, ~1.2 kB of scratch per thread), RDNA3 uses D256 only
if constexpr (use_sparse && DKQ != 256) {
NO_DEVICE_CODE;
return;
}
#endif // defined(RDNA3_0)

static_assert(!use_sparse || (ncols1 == 1 && !bias_indexed && !use_kv_rows), "bad sparse configuration");

Expand Down Expand Up @@ -1142,13 +1149,28 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr size_t nbytes_shared = 0;

#ifdef GGML_USE_HIP
if constexpr (type_K == GGML_TYPE_F16 && type_V == GGML_TYPE_F16 && !bias_indexed && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
// Quantized K/V (direct dequantization while loading, RDNA3 only) are compiled for D256 with ncols2 2/4/8.
constexpr bool kv_quant_direct = (type_K == GGML_TYPE_Q8_0 && type_V == GGML_TYPE_Q8_0) ||
(type_K == GGML_TYPE_Q4_0 && type_V == GGML_TYPE_Q4_0) ||
(type_K == GGML_TYPE_Q8_0 && type_V == GGML_TYPE_Q4_0);
constexpr bool sparse_kv_type = (type_K == GGML_TYPE_F16 && type_V == GGML_TYPE_F16) ||
(kv_quant_direct && DKQ == 256 && DV == 256 && (ncols2 == 2 || ncols2 == 4 || ncols2 == 8));
if constexpr (sparse_kv_type && !bias_indexed && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
if (ggml_cuda_flash_attn_ext_tile_shall_use_sparse(cc, dst, DKQ, DV)) {
const int nwarps = ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols2, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, ncols2, cc);
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, 1, ncols2, use_logit_softcap, false, type_K, type_V, false, true>;
// quantized K/V are read in place, no f16 conversion
constexpr bool need_f16 = !kv_quant_direct;
if (GGML_CUDA_CC_IS_RDNA3_0(cc)) {
constexpr ggml_cuda_fattn_sparse_rdna3_path log_path =
type_K == GGML_TYPE_F16 ? GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_F16 :
type_V == GGML_TYPE_Q4_0 ? (type_K == GGML_TYPE_Q8_0 ? GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q8_0_Q4_0 : GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q4_0) :
GGML_CUDA_FATTN_SPARSE_RDNA3_TILE_Q8_0;
ggml_cuda_fattn_sparse_rdna3_log_once(log_path, dst);
}
launch_fattn<DV, 1, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, true, warp_size);
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, need_f16, need_f16, false, true, warp_size);
return;
}
}
Expand Down
48 changes: 48 additions & 0 deletions ggml/src/ggml-cuda/fattn.cu
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void ggml_cuda_flash_attn_ext_compact_mask(
bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_tensor * dst, const int DKQ, const int DV) {
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];

const int n_kv_max = ggml_get_op_params_i32(dst, 4);
Expand All @@ -144,6 +145,31 @@ bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_ten
return false;
}

if (GGML_CUDA_CC_IS_RDNA3_0(cc)) {
// Opt-in, tile kernel with one index list per query: decode and verification batches (D256, GQA, F16 K/V or the
// quantized K/V that the tile kernel reads directly). Larger batches use the WMMA kernel, see the mma predicate.
// D512 is not enabled: its sparse tile kernels spill (~1.2 kB scratch per thread).
const ggml_cuda_fattn_sparse_rdna3_config & config = ggml_cuda_fattn_sparse_rdna3_get_config();
if (!config.enabled || DKQ != 256 || DV != 256 || Q->ne[1] > FATTN_TILE_DIRECT_KV_MAX_NE1 ||
n_kv % FATTN_KQ_STRIDE != 0) {
return false;
}

const int gqa_ratio = Q->ne[2] / K->ne[2];
if (gqa_ratio % 2 != 0) {
return false;
}

if (K->type != GGML_TYPE_F16 || V->type != GGML_TYPE_F16) {
if (!ggml_cuda_fattn_tile_direct_kv(cc, dst)) {
return false;
}
}

// Sparse compaction is not beneficial when the selected positions exceed 1/max_frac of the KV cache.
return n_kv >= config.min_kv && n_kv_max <= n_kv/config.max_frac;
}

if (GGML_CUDA_CC_IS_GCN(cc)) {
// Sparse compaction is not beneficial when the selected positions exceed 25% of the KV cache.
if (n_kv_max > n_kv/4) {
Expand Down Expand Up @@ -214,6 +240,21 @@ bool ggml_cuda_flash_attn_ext_mma_f16_shall_use_sparse(const int cc, const ggml_

const int32_t n_kv_max = ggml_get_op_params_i32(dst, 4);

#ifdef GGML_USE_HIP
// RDNA3 (opt-in): only the 8x8 tile of D256 GQA8 is compiled for WMMA, (1, 8) has too few columns (NO_DEVICE_CODE).
// The gather unions the visible columns of the 8 queries of a tile, the list is capped at 8*n_kv_max cells.
if (GGML_CUDA_CC_IS_RDNA3_0(cc) && amd_wmma_available(cc)) {
const ggml_cuda_fattn_sparse_rdna3_config & config = ggml_cuda_fattn_sparse_rdna3_get_config();
if (!config.enabled || ncols1*ncols2 < 16 ||
!ggml_cuda_flash_attn_ext_mma_f16_may_use_sparse(int(Q->ne[0]), int(V->ne[0]), ncols1, ncols2)) {
return false;
}
return dst->src[5] == nullptr && mask != nullptr && n_kv_max > 0 && max_bias == 0.0f && logit_softcap == 0.0f &&
mask->ne[0] == K->ne[1] && mask->ne[1] >= Q->ne[1] && mask->ne[2] == 1 &&
K->ne[1] >= std::max<int64_t>(config.min_kv, 2LL*n_kv_max) && n_kv_max <= K->ne[1]/config.max_frac;
}
#endif // GGML_USE_HIP

// the dense kernel handles up to 64/ncols2 queries per K/V pass, the single-query gather has to beat that
const int64_t n_gather = (ncols1 == 1 ? std::min<int64_t>(Q->ne[1], 64/ncols2) : ncols1) * (int64_t) n_kv_max;

Expand Down Expand Up @@ -872,6 +913,13 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
return BEST_FATTN_KERNEL_TILE;
}

#ifdef GGML_USE_HIP
// RDNA3 sparse decode/verification (opt-in): one index list per query, tile kernel, K/V read through the list.
if (gqa_opt_applies && GGML_CUDA_CC_IS_RDNA3_0(cc) && ggml_cuda_flash_attn_ext_tile_shall_use_sparse(cc, dst, K->ne[0], V->ne[0])) {
return tile_kernel;
}
#endif // GGML_USE_HIP

// 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] * ncols2 > (Q->ne[0] <= 128 ? 8 : 16)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16659,6 +16659,19 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 2, {12, 1}, 32768, 8, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 2}, 16384, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));

// RDNA3 sparse flash attention, Qwen4 QSA shape: D256, 2 KV heads, GQA 8, tile (nb <= 8, F16 and direct q8_0/q4_0 K/V) and WMMA 8x8 (nb > 8).
// The RDNA3 sparse path is opt-in: run with GGML_CUDA_FA_SPARSE_RDNA3=1, and GGML_CUDA_FA_SPARSE_MIN_KV=0 to reach it at small kv.
// (n_kv_max 2051 at kv 8192 exceeds the default n_kv/4 limit and stays dense.)
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
for (int64_t kv : {8192, 16384}) {
for (int64_t nkvmax : {2051, 4096}) {
for (int64_t nb : {1, 2, 3, 8, 64, 512}) {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {8, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, type, type, {0, 1, 2, 3}, true, false, nkvmax));
}
}
}
}

// Sparse gather: native block sizes, padded slots, and head/batch strides.
for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, GGML_TYPE_Q8_0, GGML_TYPE_IQ4_NL}) {
test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, type, type, {0, 1, 2, 3}, true, false, 257));
Expand Down Expand Up @@ -17623,6 +17636,16 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 1, 2, 3}, true, false, 0));
}

// RDNA3 sparse flash attention, Qwen4 QSA shape (2 KV heads, GQA 8, n_kv_max = 512 pools * 4 + tail): decode, MTP verify, prompt batch.
// Compare GGML_CUDA_FA_SPARSE_RDNA3=0 and 1.
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_Q8_0}) {
for (int64_t kv : {32768, 98304, 131072}) {
for (int64_t nb : {1, 3, 512}) {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {8, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, type, type, {0, 1, 2, 3}, true, false, 2112));
}
}
}

test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 8, {8, 1}, 7680, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q4_0, GGML_TYPE_Q4_0));
Expand Down
Loading