Commit f5e106b4e for llama.cpp
commit f5e106b4e28cd3d25dbb394bf8fd4bd1db02e49a
Author: Ruben Ortlam <rortlam@redhat.com>
Date: Sat Dec 5 18:00:21 2026 +0100
vulkan: optimize vision encoder performance on AMD RDNA3/4 (#33800)
* vulkan: extend FA flip shader to handle head widths divisible by eight and partial KV tiles
* optimize fp16 matmul for vision decoder shapes
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index be7d5bfba..4887152cf 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -298,6 +298,7 @@ struct vk_matmul_pipeline_key {
struct vk_matmul_pipeline_pair {
vk_pipeline unaligned;
vk_pipeline aligned;
+ vk_pipeline aligned_k_tail;
uint32_t align;
uint32_t narrow_n_max = 0;
};
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index bc9d80aac..bab6faabe 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -1397,7 +1397,7 @@ static vk_fa_tuning_params get_fa_tuning_params_coopmat1(const vk_device& device
device->driver_id != vk::DriverId::eAmdProprietary);
if (allow_flip && !disable_flip && flip_arch && n_rows >= 64 &&
(result.subgroup_size == 32 || result.subgroup_size == 64) &&
- hsk % 16 == 0 && hsv % 16 == 0 && hsk <= 256 && hsv <= 256 &&
+ hsk % 8 == 0 && hsv % 8 == 0 && hsk <= 256 && hsv <= 256 &&
k_type == v_type && (k_type == GGML_TYPE_F16 || k_type == GGML_TYPE_BF16)) {
result.flip = true;
// Direct loads re-read each K/V tile per subgroup, staged wins once K/V and workgroup drift outgrow the caches
@@ -1411,6 +1411,8 @@ static vk_fa_tuning_params get_fa_tuning_params_coopmat1(const vk_device& device
}
result.block_rows = num_subgroups * coopmat_block_rows;
result.block_cols = (device->uma && (hsk >= 256 || hsv >= 256)) ? 16 : 32;
+ // Direct coopmat loads require full 16x16 matrices in global memory.
+ result.flip_direct = result.flip_direct && (hsk | hsv) % 16 == 0 && n_kv % result.block_cols == 0;
result.q_in_regs = false;
result.shmem_staging = 0;
}
@@ -2130,6 +2132,8 @@ static void ggml_vk_conv_halo_cm1_tile(uint32_t bs_k, uint32_t nt, uint32_t fn,
}
}
+static constexpr uint32_t matmul_fp16_vector_width = 8;
+
void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
VK_LOG_DEBUG("ggml_vk_load_shaders(" << device->name << ")");
@@ -2686,7 +2690,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
uint32_t push_constant_size, uint32_t param_count,
const spec_fn_t& spec_fn,
bool disable_robustness = false, bool require_full_subgroups = false, uint32_t required_subgroup_size = 0,
- bool create_aligned = true, bool pin_subgroup_to_warp = false
+ bool create_aligned = true, bool pin_subgroup_to_warp = false, bool create_aligned_k_tail = false
) {
GGML_ASSERT(!key.stream_k || (!key.mul_mat_id && param_count == 3 && push_constant_size == sizeof(vk_mat_mat_push_constants) && create_aligned));
// Non-ID matmul shaders declare the Stream-K scratch binding in all specializations.
@@ -2716,6 +2720,9 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
if (create_aligned) {
pair.aligned = std::make_shared<vk_pipeline_struct>();
}
+ if (create_aligned_k_tail) {
+ pair.aligned_k_tail = std::make_shared<vk_pipeline_struct>();
+ }
if (key.stream_k) {
// Tile selection reads unaligned even when selecting an aligned pipeline.
pair.unaligned = pair.aligned;
@@ -2746,6 +2753,16 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
tc.wg_denoms, spec, tc.align,
disable_robustness, rfs, rsgs);
}
+ if (vec[i].aligned_k_tail) {
+ auto spec = spec_fn(tc.warptile, true);
+ spec[11] = 2; // ALIGNED_K_TAIL
+ const std::string name = shader_name + "_aligned_k_tail_" + std::to_string(i);
+ ggml_vk_create_pipeline(device, vec[i].aligned_k_tail,
+ vec[i].aligned_k_tail->name.empty() ? name.c_str() : vec[i].aligned_k_tail->name.c_str(),
+ spv_len, spv_data, "main", param_count, push_constant_size,
+ tc.wg_denoms, spec, matmul_fp16_vector_width,
+ disable_robustness, rfs, rsgs);
+ }
}
};
@@ -2962,7 +2979,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
const std::string& name, size_t len, const void* data, uint32_t pc_size, uint32_t pc) {
auto tc = filter_tc(tc_base, key.type_a, key.mul_mat_id);
add_narrow_n_cm1(tc, key.type_a, key.mul_mat_id);
- if (!tc.empty()) create_mm_pipelines(key, tc, name, len, data, pc_size, pc, cm1_spec, false, true, 0, true, cm1_pin);
+ const bool aligned_k_tail = key.type_a == GGML_TYPE_F16 && !key.mul_mat_id;
+ if (!tc.empty()) create_mm_pipelines(key, tc, name, len, data, pc_size, pc, cm1_spec, false, true, 0, true, cm1_pin, aligned_k_tail);
};
auto cm1_create_quant = [&](vk_matmul_pipeline_key key, const std::vector<vk_tile_config>& tc_base,
const std::string& name, size_t len, const void* data, uint32_t pc_size, uint32_t pc) {
@@ -7205,11 +7223,14 @@ static const std::vector<vk_matmul_pipeline_pair> * ggml_vk_get_mul_mat_mat_pipe
static vk_pipeline ggml_vk_guess_matmul_pipeline_map(ggml_backend_vk_context * ctx,
const std::vector<vk_matmul_pipeline_pair>& configs,
- uint32_t m, uint32_t n, bool aligned, bool mul_mat_id) {
+ uint32_t m, uint32_t n, bool aligned, bool mul_mat_id, bool aligned_k_tail = false) {
auto& selector = mul_mat_id ? ctx->device->matmul_id_tile_selector : ctx->device->matmul_tile_selector;
uint32_t idx = selector(m, n, 0, ctx->device->shader_core_count, configs);
if (idx >= configs.size()) idx = (uint32_t)configs.size() - 1;
- return (aligned && configs[idx].aligned) ? configs[idx].aligned : configs[idx].unaligned;
+ if (aligned && configs[idx].aligned) {
+ return configs[idx].aligned;
+ }
+ return aligned_k_tail && configs[idx].aligned_k_tail ? configs[idx].aligned_k_tail : configs[idx].unaligned;
}
static uint32_t ggml_vk_guess_matmul_pipeline_align_map(ggml_backend_vk_context * ctx,
@@ -7598,15 +7619,25 @@ static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& sub
}
}
+ uint32_t stride_batch_x = qx_needs_dequant ? ne00*ne01 : ggml_vk_batch_stride(src0);
+ uint32_t stride_batch_y = (qy_needs_dequant || quantize_y) ? ne10*ne11 : ggml_vk_batch_stride(src1);
+
+ // The aligned F16 coopmat1 shader indexes each batch in vectors of up to eight elements.
+ const bool f16_cm1 = ctx->device->coopmat_support && !ctx->device->coopmat2 && mmp_type_a == GGML_TYPE_F16;
+ const bool batch_aligned = !f16_cm1 ||
+ ((ne02*ne03 == 1 || stride_batch_x % matmul_fp16_vector_width == 0) &&
+ (ne12*ne13 == 1 || stride_batch_y % matmul_fp16_vector_width == 0));
+ const bool can_align = !quantize_y && ne01 > 8 && ne11 > 8 && batch_aligned;
const uint32_t kpad = quantize_y ? 0 : ggml_vk_align_size(ne10, ggml_vk_guess_matmul_pipeline_align_map(ctx, *mmp_map, ne01, ne11, false));
- const bool aligned = !quantize_y && ne10 == kpad && ne01 > 8 && ne11 > 8;
+ const bool aligned = can_align && ne10 == kpad;
+ const bool aligned_k_tail = can_align && f16_cm1 && ne10 % matmul_fp16_vector_width == 0;
const bool needs_64b_indexing = ggml_nbytes(src0) > ctx->device->properties.limits.maxStorageBufferRange;
if (use_stream_k && !aligned) {
mmp_map = regular_mmp_map;
use_stream_k = false;
}
- vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline_map(ctx, *mmp_map, ne01, ne11, aligned, false);
+ vk_pipeline pipeline = ggml_vk_guess_matmul_pipeline_map(ctx, *mmp_map, ne01, ne11, aligned, false, aligned_k_tail);
const bool narrow_n = mmp_map->back().narrow_n_max != 0 && ne11 <= mmp_map->back().narrow_n_max;
const uint32_t split_k = ggml_vk_guess_split_k(ctx, ne01, ne11, ne10, disable_split_k, pipeline, narrow_n);
@@ -7773,9 +7804,6 @@ static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& sub
const uint64_t x_range = qx_needs_dequant ? x_sz : ggml_nbytes(src0);
const uint64_t y_range = (qy_needs_dequant || quantize_y) ? y_sz : ggml_nbytes(src1);
- uint32_t stride_batch_x = qx_needs_dequant ? ne00*ne01 : ggml_vk_batch_stride(src0);
- uint32_t stride_batch_y = (qy_needs_dequant || quantize_y) ? ne10*ne11 : ggml_vk_batch_stride(src1);
-
if (!ggml_vk_dim01_contiguous(src0) && !qx_needs_dequant) {
stride_batch_x = src0->nb[0] / ggml_type_size(src0->type);
}
@@ -9702,7 +9730,8 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
// flipped coopmat1 path reads K/V rows with 16-byte loads
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) {
+ const bool flip_strides_aligned = (q_stride_eff | k_stride | v_stride) % 8 == 0 && (nbq2 | nbq3) % 16 == 0;
+ if (!flip_strides_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, 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 6c9573ca1..dc1242218 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
@@ -30,7 +30,7 @@ const uint32_t D_pad = HSK_pad > HSV_pad ? HSK_pad : HSV_pad;
// Flipped coopmat1 FA: each subgroup owns 16 Q rows and the full head dim. K/V tiles are either
// staged once per workgroup in LDS or, with FLIP_DIRECT, loaded by each subgroup from global memory.
-// Requires gqa_ratio == 1, aligned KV, f16/bf16 K/V, HSK/HSV multiples of 16, Br == row_split * 16.
+// Requires gqa_ratio == 1, f16/bf16 K/V, HSK/HSV multiples of 8, Br == row_split * 16.
const uint32_t FL_LPR = SubGroupSize / 16;
const uint32_t FL_CPL = Bc / FL_LPR;
@@ -135,7 +135,7 @@ void fl_load_k(const uint32_t j, const uint32_t k_offset) {
const uint32_t c = idx / (HSK_pad / 8);
const uint32_t d = idx % (HSK_pad / 8);
uint32_t row;
- fl_kr[l] = fa_kv_index(j * Bc + c, row) ? data_ku[(k_offset + row * k_stride) / 8 + d] : uvec4(0);
+ fl_kr[l] = (HSK == HSK_pad || d < HSK / 8) && fa_kv_index(j * Bc + c, row) ? data_ku[(k_offset + row * k_stride) / 8 + d] : uvec4(0);
}
}
}
@@ -150,7 +150,7 @@ void fl_load_v(const uint32_t j, const uint32_t v_offset) {
c = (gl_LocalInvocationIndex / (HSV_pad / 8)) * FL_VLD + l;
}
uint32_t row;
- fl_vr[l] = fa_kv_index(j * Bc + c, row) ? data_vu[(v_offset + row * v_stride) / 8 + d] : uvec4(0);
+ fl_vr[l] = (HSV == HSV_pad || d < HSV / 8) && fa_kv_index(j * Bc + c, row) ? data_vu[(v_offset + row * v_stride) / 8 + d] : uvec4(0);
}
}
}
@@ -306,6 +306,8 @@ void main() {
while (j < end_j) {
const uint32_t jn = fl_next_tile(j + 1);
const uint32_t mask_bits = fl_mask_bits(j);
+ const uint32_t col0 = j * Bc + lc * FL_CPL;
+ const uint32_t valid_cols = KV_bounds_check ? min(FL_CPL, KV - min(KV, col0)) : FL_CPL;
if (!FLIP_DIRECT) {
barrier();
@@ -325,11 +327,11 @@ void main() {
{
uint32_t bq, bh;
if (USE_BIAS && myrow < p.nem1 && fa_bias_row(sg * 16 + lr, bq, bh)) {
- const uint32_t ioff = bq * p.KV + j * Bc + lc * FL_CPL;
+ const uint32_t ioff = bq * p.KV + col0;
const uint32_t toff = (bq * p.neq2 + bh) * BiasBins;
uint32_t bin[FL_CPL];
[[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
- bin[k] = uint32_t(data_bias_idx[ioff + k]);
+ bin[k] = k < valid_cols ? uint32_t(data_bias_idx[ioff + k]) : 0;
}
[[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
bv[k] = data_bias_table[toff + bin[k]];
@@ -373,10 +375,12 @@ void main() {
}
if (MASK_ENABLE && (mask_bits != MASK_OPT_ALL_ZERO || USE_BIAS) && myrow < p.nem1) {
- const uint32_t moff = m_offset + myrow * KV + j * Bc + lc * FL_CPL;
+ const uint32_t moff = m_offset + myrow * KV + col0;
if (!USE_BIAS || mask_bits != MASK_OPT_ALL_ZERO) {
[[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
- bv[k] += float(data_m[moff + k]);
+ if (k < valid_cols) {
+ bv[k] += float(data_m[moff + k]);
+ }
}
}
[[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
@@ -384,6 +388,14 @@ void main() {
}
}
+ if (KV_bounds_check) {
+ [[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
+ if (k >= valid_cols) {
+ s[k] = -1.0 / 0.0;
+ }
+ }
+ }
+
float mx = s[0];
[[unroll]] for (uint32_t k = 1; k < FL_CPL; ++k) {
mx = max(mx, s[k]);
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp b/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp
index c9a281994..970ea7a59 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp
@@ -50,6 +50,7 @@ layout (constant_id = 12) const uint MmTypeA = 0;
#endif
layout (constant_id = 11) const uint ALIGNED = 0;
+const uint ALIGNED_K_TAIL = 2;
#ifdef MULMAT_QUANT
@@ -207,6 +208,7 @@ layout(constant_id = 13) const bool APPLY_SLM_A_RESHAPE = false;
const uint SHMEM_STRIDE_PAD = 1;
const bool APPLY_SLM_A_RESHAPE = false;
#endif
+
#ifndef MUL_MAT_ID
layout(constant_id = 15) const bool stream_k = false;
#endif
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl b/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl
index 81e72015a..f373bd2cc 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl
@@ -23,7 +23,8 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin
if (ALIGNED != 0) {
const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row;
const uint k_pair = row * LOAD_VEC_A / 2;
- FLOAT_TYPEV8 aa = FLOAT_TYPEV8(data_a[idx]);
+ const bool valid = ALIGNED != ALIGNED_K_TAIL || block + row * LOAD_VEC_A < end_k;
+ FLOAT_TYPEV8 aa = valid ? FLOAT_TYPEV8(data_a[idx]) : FLOAT_TYPEV8(0.0);
store_a(col, k_pair, aa[0].xy);
store_a(col, k_pair + 1, aa[0].zw);
store_a(col, k_pair + 2, aa[1].xy);
@@ -34,7 +35,8 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin
if (ALIGNED != 0) {
const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row;
const uint k_pair = row * LOAD_VEC_A / 2;
- FLOAT_TYPEV4 aa = FLOAT_TYPEV4(data_a[idx]);
+ const bool valid = ALIGNED != ALIGNED_K_TAIL || block + row * LOAD_VEC_A < end_k;
+ FLOAT_TYPEV4 aa = valid ? FLOAT_TYPEV4(data_a[idx]) : FLOAT_TYPEV4(0.0);
store_a(col, k_pair, aa.xy);
store_a(col, k_pair + 1, aa.zw);
return;
@@ -663,7 +665,8 @@ void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uin
// Not supported for b_type bf16 because bf16mat2x4 does not exist
const uint idx = pos_b + col * p.stride_b / LOAD_VEC_B + row;
const uint buf_idx = col * SHMEM_STRIDE + row * LOAD_VEC_B / 2;
- FLOAT_TYPEV8 bb = FLOAT_TYPEV8(data_b[idx]);
+ const bool valid = ALIGNED != ALIGNED_K_TAIL || block + row * LOAD_VEC_B < end_k;
+ FLOAT_TYPEV8 bb = valid ? FLOAT_TYPEV8(data_b[idx]) : FLOAT_TYPEV8(0.0);
buf_b[buf_idx + 0] = bb[0].xy;
buf_b[buf_idx + 1] = bb[0].zw;
buf_b[buf_idx + 2] = bb[1].xy;
@@ -677,7 +680,8 @@ void load_b_to_shmem(const uint pos_b, const uint row, const uint col, const uin
#if defined(DATA_B_BF16)
FLOAT_TYPEV4 bb = FLOAT_TYPEV4(TO_FLOAT_TYPE(data_b[idx]));
#else
- FLOAT_TYPEV4 bb = FLOAT_TYPEV4(data_b[idx]);
+ const bool valid = ALIGNED != ALIGNED_K_TAIL || block + row * LOAD_VEC_B < end_k;
+ FLOAT_TYPEV4 bb = valid ? FLOAT_TYPEV4(data_b[idx]) : FLOAT_TYPEV4(0.0);
#endif
buf_b[buf_idx + 0] = bb.xy;
buf_b[buf_idx + 1] = bb.zw;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 93234deca..e15eabedd 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -14436,6 +14436,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 65, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 80, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 64, 32, 80, {1, 1}, {1, 1}));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 131, 137, 80, {1, 1}, {1, 1}));
+ test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F32, 131, 137, 80, {2, 1}, {2, 1}, {0, 1, 2, 3}, 0, 1, false, 133, 4));
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F32, GGML_TYPE_F32, 64, 32, 80, {1, 1}, {1, 1}));
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 588, {1, 1}, {1, 1})); // 14*14*3, e.g. conv_2d im2col
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 64, 32, 80, {4, 1}, {1, 1}));
@@ -15499,6 +15501,12 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 8, 3, 1100, 256, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, true));
test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 1100, 7, 2, 33, false, true, GGML_TYPE_Q8_0));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(72, 2, 3, 545, 65, 1, 17, false, true, GGML_TYPE_F16, 10.0f, true));
+
+ // Head and KV tails in non-causal vision attention.
+ test_cases.emplace_back(new test_flash_attn_ext(72, 72, 4, {1, 1}, 64, 65, false, false));
+ test_cases.emplace_back(new test_flash_attn_ext(80, 80, 4, {1, 1}, 65, 65, false, false));
+ test_cases.emplace_back(new test_flash_attn_ext(72, 72, 4, {1, 1}, 65, 65, false, false));
// sparse mask with large batch size
test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512));