Commit c3e91d61a for llama.cpp
commit c3e91d61a262592ee475a3f8fd4c48253a0e4432
Author: Ruben Ortlam <rortlam@redhat.com>
Date: Fri Dec 4 19:04:51 2026 +0100
vulkan: tune Flash Attention flip shader for RDNA3/4 large ubatch (#33683)
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-common.h b/ggml/src/ggml-vulkan/ggml-vulkan-common.h
index 4941d47a6..a3908b2bf 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-common.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-common.h
@@ -34,7 +34,7 @@ bool ggml_vk_intel_windows_driver_in_range(uint32_t driver_version, uint32_t low
// shaders
void ggml_vk_destroy_pipeline(vk::Device& device, vk_pipeline& pipeline);
-vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip = true);
+vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, uint32_t n_kv_heads, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip = true);
vk_fa_pipeline_state get_fa_pipeline_state(const vk_device& device, const vk_fa_tuning_params& params, uint32_t hsk, uint32_t hsv, bool aligned, bool f32acc, bool use_mask, bool use_mask_opt, bool use_logit_softcap, bool use_sparse, bool use_kv_rows, ggml_type k_type, ggml_type v_type, uint32_t bias_bins, uint32_t gqa_tok);
uint32_t get_subgroup_size(const std::string &pipeline_name, const vk_device_architecture &arch);
void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested = nullptr);
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index f0b5442b4..bc9d80aac 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -1351,8 +1351,7 @@ static vk_fa_tuning_params get_fa_tuning_params_scalar(const vk_device& device,
return result;
}
-static vk_fa_tuning_params get_fa_tuning_params_coopmat1(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip) {
- GGML_UNUSED(n_kv);
+static vk_fa_tuning_params get_fa_tuning_params_coopmat1(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, uint32_t n_kv_heads, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip) {
vk_fa_tuning_params result{};
result.path = FA_COOPMAT1;
@@ -1401,7 +1400,10 @@ static vk_fa_tuning_params get_fa_tuning_params_coopmat1(const vk_device& device
hsk % 16 == 0 && hsv % 16 == 0 && hsk <= 256 && hsv <= 256 &&
k_type == v_type && (k_type == GGML_TYPE_F16 || k_type == GGML_TYPE_BF16)) {
result.flip = true;
- result.flip_direct = !device->uma && qk_ratio > 1 && device->vendor_id != VK_VENDOR_ID_NVIDIA;
+ // Direct loads re-read each K/V tile per subgroup, staged wins once K/V and workgroup drift outgrow the caches
+ const double kv_mib = (double)n_kv * n_kv_heads * (hsk + hsv) * ggml_type_size(k_type) / (1024.0 * 1024.0);
+ const bool kv_cached = kv_mib * std::sqrt((double)n_rows) < 6000.0;
+ result.flip_direct = !device->uma && qk_ratio > 1 && device->vendor_id != VK_VENDOR_ID_NVIDIA && kv_cached;
if (force_flip != nullptr && strcmp(force_flip, "kvsep") == 0) {
result.flip_direct = false;
} else if (force_flip != nullptr && strcmp(force_flip, "direct") == 0) {
@@ -1453,7 +1455,7 @@ static vk_fa_tuning_params get_fa_tuning_params_coopmat2(const vk_device& device
return result;
}
-vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip) {
+vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk, uint32_t hsv, uint32_t n_rows, uint32_t n_kv, uint32_t n_kv_heads, ggml_type k_type, ggml_type v_type, bool f32acc, uint32_t qk_ratio, bool kv_rows, bool allow_flip) {
FaCodePath path = device->coopmat2 ? FA_COOPMAT2 :
device->coopmat1_fa_support ? FA_COOPMAT1 : FA_SCALAR;
@@ -1472,11 +1474,11 @@ vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk,
if (path == FA_COOPMAT1) {
bool shape_ok = (f32acc && device->coopmat_support_16x16x16_f32acc) ||
(!f32acc && device->coopmat_support_16x16x16_f16acc);
- vk_fa_tuning_params params = get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
+ vk_fa_tuning_params params = get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, n_kv_heads, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
bool shmem_ok = ggml_vk_flash_attn_coopmat_shmem_support(device, params, hsk, hsv, f32acc, k_type, v_type);
if (!shmem_ok && params.flip) {
allow_flip = false;
- params = get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
+ params = get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, n_kv_heads, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
shmem_ok = ggml_vk_flash_attn_coopmat_shmem_support(device, params, hsk, hsv, f32acc, k_type, v_type);
}
@@ -1494,7 +1496,7 @@ vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk,
case FA_SCALAR:
return get_fa_tuning_params_scalar(device, hsk, hsv, n_rows, n_kv, k_type, v_type, f32acc, kv_rows);
case FA_COOPMAT1:
- return get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
+ return get_fa_tuning_params_coopmat1(device, hsk, hsv, n_rows, n_kv, n_kv_heads, k_type, v_type, f32acc, qk_ratio, kv_rows, allow_flip);
case FA_COOPMAT2:
return get_fa_tuning_params_coopmat2(device, hsk, hsv, n_rows, n_kv, k_type, v_type, f32acc, kv_rows);
default:
@@ -9527,7 +9529,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
const bool rows_compact = kv_rows && (xe_fa_rows_prefill || (neq1 >= 64 &&
(ctx->device->vendor_id == VK_VENDOR_ID_AMD ||
(ctx->device->vendor_id == VK_VENDOR_ID_NVIDIA &&
- get_fa_tuning_params(ctx->device, HSK, HSV, neq1, KV, k->type, v->type, f32acc, qk_ratio, kv_rows).path == FA_COOPMAT2))));
+ get_fa_tuning_params(ctx->device, HSK, HSV, neq1, KV, nek2, k->type, v->type, f32acc, qk_ratio, kv_rows).path == FA_COOPMAT2))));
// gated to AMD, except RDNA4, which regressed in benchmarks
const bool kv_raw_strided = (rows_compact && kv_raw && (HSK % 8) == 0 && (HSV % 8) == 0) ||
(ctx->device->vendor_id == VK_VENDOR_ID_AMD &&
@@ -9575,7 +9577,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
const ggml_type v_type_eff = (use_dequant_kv && !kv_raw_strided) ? GGML_TYPE_F16 : v->type;
// N=32 avoids the cm1 prefill block_rows doubling
- vk_fa_tuning_params tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, 32, KV, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
+ vk_fa_tuning_params tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, 32, KV, nek2, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
const uint32_t max_gqa = std::min(tuning_params.block_rows, 32u);
if (N <= 8 && qk_ratio > 1 && qk_ratio <= max_gqa &&
@@ -9588,7 +9590,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
workgroups_y /= gqa_ratio;
}
- tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, N, KV, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
+ tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, N, KV, nek2, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
float scale = 1.0f;
float max_bias = 0.0f;
@@ -9635,7 +9637,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
bool fold_pays = true;
if (tuning_params.path == FA_SCALAR) {
// the scalar tile size depends on the row count, so size it for all folded rows
- fold_params = get_fa_tuning_params(ctx->device, HSK, HSV, gqa_ratio * neq1, KV, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
+ fold_params = get_fa_tuning_params(ctx->device, HSK, HSV, gqa_ratio * neq1, KV, nek2, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows);
// scalar: fold if the unfolded dispatch gets no split_k (more tokens than rows), or if the fold adds no padded rows and each KV split keeps at least 16 blocks
const uint32_t fold_tok = std::max(1u, fold_params.block_rows / gqa_ratio);
const uint32_t fold_wgs = CEIL_DIV(neq1, fold_tok) * workgroups_y * workgroups_z;
@@ -9701,7 +9703,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
const bool kv_16b = use_dequant_kv ||
(((uintptr_t)k->data | (uintptr_t)v->data | nbk2_eff | nbk3_eff | nbv2_eff | nbv3_eff) % 16) == 0;
if (!aligned || use_sparse || (use_kv_rows && tuning_params.flip_direct) || gqa_ratio != 1 || !kv_16b) {
- tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, N, KV, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows, false);
+ tuning_params = get_fa_tuning_params(ctx->device, HSK, HSV, N, KV, nek2, k_type_eff, v_type_eff, f32acc, qk_ratio, use_kv_rows, false);
alignment = tuning_params.block_cols;
aligned = fa_aligned(tuning_params);
}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1_flip.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1_flip.comp
index e93f203bd..6c9573ca1 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1_flip.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1_flip.comp
@@ -224,6 +224,15 @@ void fl_rescale(inout coopmat<ACC_TYPE, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAc
void main() {
init_indices();
+ // Head-major workgroup order for many row tiles: co-resident workgroups share a row tile, so they stay in KV lockstep
+ if (FLIP_DIRECT && p.k_num == 1 && !USE_SPARSE && gl_NumWorkGroups.x >= 16) {
+ const uint32_t lin = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
+ i = lin / gl_NumWorkGroups.y;
+ iq2 = lin % gl_NumWorkGroups.y;
+ ik2 = iq2 / rk2;
+ iv2 = iq2 / rv2;
+ }
+
const uint32_t sg = gl_SubgroupID;
const uint32_t lane = gl_SubgroupInvocationID;
const uint32_t lr = lane / FL_LPR;