Commit f89a243d5 for llama.cpp
commit f89a243d53c741cecc2b3630e491de61f459294c
Author: Ruben Ortlam <rortlam@redhat.com>
Date: Fri Nov 27 07:45:08 2026 +0100
vulkan: add FA compact bias support for Inkling 2 Mini (#32898)
* vulkan: add FA compact bias support for Inkling 2 Mini
* fix xe condition and function signature
* fix RDNA regression
* batch loads to allow overlap and coalescing
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-common.h b/ggml/src/ggml-vulkan/ggml-vulkan-common.h
index ea3b3b4b8..262fa8876 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-common.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-common.h
@@ -35,7 +35,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 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, ggml_type k_type, ggml_type v_type);
+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, ggml_type k_type, ggml_type v_type, uint32_t bias_bins);
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);
bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const vk_fa_tuning_params& params, uint32_t hsk, uint32_t hsv, bool f32acc, ggml_type k_type, ggml_type v_type);
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index b71e73dd0..dd6838031 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -450,10 +450,11 @@ struct vk_fa_pipeline_state {
uint32_t limit_occupancy_shmem;
ggml_type k_type;
ggml_type v_type;
+ uint32_t bias_bins;
bool operator<(const vk_fa_pipeline_state &b) const {
- return std::tie(HSK, HSV, Br, Bc, D_split, row_split, shmem_staging, path, workgroup_size, subgroup_size, aligned, f32acc, flags, limit_occupancy_shmem, k_type, v_type) <
- std::tie(b.HSK, b.HSV, b.Br, b.Bc, b.D_split, b.row_split, b.shmem_staging, b.path, b.workgroup_size, b.subgroup_size, b.aligned, b.f32acc, b.flags, b.limit_occupancy_shmem, b.k_type, b.v_type);
+ return std::tie(HSK, HSV, Br, Bc, D_split, row_split, shmem_staging, path, workgroup_size, subgroup_size, aligned, f32acc, flags, limit_occupancy_shmem, k_type, v_type, bias_bins) <
+ std::tie(b.HSK, b.HSV, b.Br, b.Bc, b.D_split, b.row_split, b.shmem_staging, b.path, b.workgroup_size, b.subgroup_size, b.aligned, b.f32acc, b.flags, b.limit_occupancy_shmem, b.k_type, b.v_type, b.bias_bins);
}
};
@@ -465,10 +466,11 @@ struct vk_xe_fa_pipeline_state {
bool unaligned_q;
bool partial_row_mask;
bool multi_head_mask;
+ uint32_t bias_bins;
bool operator<(const vk_xe_fa_pipeline_state &b) const {
- return std::tie(HSK, HSV, qk_ratio, pv_ratio, decode_n, unaligned_seq_len, unaligned_q, partial_row_mask, multi_head_mask) <
- std::tie(b.HSK, b.HSV, b.qk_ratio, b.pv_ratio, b.decode_n, b.unaligned_seq_len, b.unaligned_q, b.partial_row_mask, b.multi_head_mask);
+ return std::tie(HSK, HSV, qk_ratio, pv_ratio, decode_n, unaligned_seq_len, unaligned_q, partial_row_mask, multi_head_mask, bias_bins) <
+ std::tie(b.HSK, b.HSV, b.qk_ratio, b.pv_ratio, b.decode_n, b.unaligned_seq_len, b.unaligned_q, b.partial_row_mask, b.multi_head_mask, b.bias_bins);
}
};
@@ -810,6 +812,7 @@ struct vk_device_struct {
bool coopmat2_bf16_support {};
bool coopmat2_gdn_support {};
bool coopmat2_decode_vector;
+ uint32_t coopmat2_reserved_shmem {};
bool dot2_f16 {};
bool ocp_fp4 {};
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index e6649b86c..ba1a648a8 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -1446,8 +1446,12 @@ vk_fa_tuning_params get_fa_tuning_params(const vk_device& device, uint32_t hsk,
}
}
+// cm2 only: selects the flash_attn_cm2 SPIR-V built with FA_BIAS_STAGE
+static constexpr uint32_t FA_FLAG_BIAS_STAGE = 256;
+
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, ggml_type k_type, ggml_type v_type) {
+ bool use_mask, bool use_mask_opt, bool use_logit_softcap, bool use_sparse, ggml_type k_type, ggml_type v_type,
+ uint32_t bias_bins) {
const bool old_amd_windows = device->vendor_id == VK_VENDOR_ID_AMD && device->driver_id == vk::DriverId::eAmdProprietary &&
(device->architecture == AMD_GCN || device->architecture == AMD_RDNA1 || device->architecture == AMD_RDNA2);
@@ -1462,7 +1466,7 @@ vk_fa_pipeline_state get_fa_pipeline_state(const vk_device& device, const vk_fa_
const uint32_t subgroup_size = params.disable_subgroups ? 0 : params.subgroup_size;
- return vk_fa_pipeline_state{hsk, hsv, params.block_rows, params.block_cols, params.d_split, params.row_split, params.shmem_staging, params.path, params.workgroup_size, subgroup_size, aligned, f32acc, flags, params.limit_occupancy_shmem, k_type, v_type};
+ return vk_fa_pipeline_state{hsk, hsv, params.block_rows, params.block_cols, params.d_split, params.row_split, params.shmem_staging, params.path, params.workgroup_size, subgroup_size, aligned, f32acc, flags, params.limit_occupancy_shmem, k_type, v_type, bias_bins};
}
static uint32_t fa_block_bytes(ggml_type t) {
@@ -1490,6 +1494,7 @@ static std::vector<uint32_t> get_fa_spec_constants(const vk_fa_pipeline_state& s
/*13 FaTypeV */ static_cast<uint32_t>(state.v_type),
/*14 FaBlockBytesK */ fa_block_bytes(state.k_type),
/*15 FaBlockBytesV */ fa_block_bytes(state.v_type),
+ /*16 BiasBins */ state.bias_bins,
};
}
@@ -2371,7 +2376,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
}
name = aligned ? "flash_attn_f32_f16_aligned" : "flash_attn_f32_f16";
}
- ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 8,
+ ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 10,
sizeof(vk_flash_attn_push_constants), {Br, 1, 1},
get_fa_spec_constants(fa.first), aligned ? Bc : 1, true,
!fa_ds, !fa_ds ? fa_sgs : 0);
@@ -2418,7 +2423,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
else { spv_data = flash_attn_f32_f16_f16acc_cm1_data; spv_size = flash_attn_f32_f16_f16acc_cm1_len; }
name = aligned ? "flash_attn_f32_f16_aligned_cm1" : "flash_attn_f32_f16_cm1";
}
- ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 8,
+ ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 10,
sizeof(vk_flash_attn_push_constants), {Br, 1, 1},
get_fa_spec_constants(fa.first), aligned ? Bc : 1, true,
!fa_ds, !fa_ds ? fa_sgs : 0);
@@ -2436,18 +2441,23 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
const bool f32acc = fa.first.f32acc;
const bool bf16_kv = fa.first.k_type == GGML_TYPE_BF16;
+ const bool bias_stage = (fa.first.flags & FA_FLAG_BIAS_STAGE) != 0;
const void * spv_data;
size_t spv_size;
const char * name;
if (bf16_kv) {
#if defined(VK_KHR_shader_bfloat16) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
if (!device->coopmat2_bf16_support) continue;
- spv_data = flash_attn_f32_f16_bf16_cm2_data;
- spv_size = flash_attn_f32_f16_bf16_cm2_len;
- name = aligned ? "flash_attn_f32_bf16_aligned_cm2" : "flash_attn_f32_bf16_cm2";
+ spv_data = bias_stage ? flash_attn_f32_f16_bf16_cm2_bias_data : flash_attn_f32_f16_bf16_cm2_data;
+ spv_size = bias_stage ? flash_attn_f32_f16_bf16_cm2_bias_len : flash_attn_f32_f16_bf16_cm2_len;
+ name = bias_stage ? (aligned ? "flash_attn_f32_bf16_aligned_cm2_bias" : "flash_attn_f32_bf16_cm2_bias") :
+ (aligned ? "flash_attn_f32_bf16_aligned_cm2" : "flash_attn_f32_bf16_cm2");
#else
continue;
#endif
+ } else if (bias_stage) {
+ if (f32acc) { spv_data = flash_attn_f32_f16_cm2_bias_data; spv_size = flash_attn_f32_f16_cm2_bias_len; name = aligned ? "flash_attn_f32_f16_aligned_f32acc_cm2_bias" : "flash_attn_f32_f16_f32acc_cm2_bias"; }
+ else { spv_data = flash_attn_f32_f16_f16acc_cm2_bias_data; spv_size = flash_attn_f32_f16_f16acc_cm2_bias_len; name = aligned ? "flash_attn_f32_f16_aligned_f16acc_cm2_bias" : "flash_attn_f32_f16_f16acc_cm2_bias"; }
} else if (aligned) {
if (f32acc) { spv_data = flash_attn_f32_f16_cm2_data; spv_size = flash_attn_f32_f16_cm2_len; name = "flash_attn_f32_f16_aligned_f32acc_cm2"; }
else { spv_data = flash_attn_f32_f16_f16acc_cm2_data; spv_size = flash_attn_f32_f16_f16acc_cm2_len; name = "flash_attn_f32_f16_aligned_f16acc_cm2"; }
@@ -2455,7 +2465,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
if (f32acc) { spv_data = flash_attn_f32_f16_cm2_data; spv_size = flash_attn_f32_f16_cm2_len; name = "flash_attn_f32_f16_f32acc_cm2"; }
else { spv_data = flash_attn_f32_f16_f16acc_cm2_data; spv_size = flash_attn_f32_f16_f16acc_cm2_len; name = "flash_attn_f32_f16_f16acc_cm2"; }
}
- ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 8,
+ ggml_vk_create_pipeline(device, fa.second, name, spv_size, spv_data, "main", 10,
sizeof(vk_flash_attn_push_constants), {Br, 1, 1},
get_fa_spec_constants(fa.first), aligned ? Bc : 1, true, false, 0);
}
@@ -3616,12 +3626,12 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
std::vector<uint32_t> sc_ph1 = { xe_fa_params.workgroup_size[0], pipeline_state.qk_ratio, pipeline_state.HSK,
xe_fa_params.subgroup_size[0], xe_fa_params.decode_params.p_chunk_ph1,
xe_fa_params.decode_params.out_per_wg_ph1, xe_fa_params.decode_params.p_chunk_ph1 / xe_fa_params.tm,
- xe_fa_params.tm, xe_fa_params.tn, shmem_sz };
+ xe_fa_params.tm, xe_fa_params.tn, shmem_sz, pipeline_state.bias_bins };
std::vector<uint32_t> sc_ph2 = { xe_fa_params.workgroup_size[1], pipeline_state.pv_ratio, pipeline_state.HSV,
xe_fa_params.decode_params.out_row_per_wg_ph2, xe_fa_params.subgroup_size[1], xe_fa_params.decode_params.split_p_per_iter_ph2,
xe_fa_params.decode_params.p_chunk_ph1, xe_fa_params.decode_params.out_col_per_wg_ph2,
xe_fa_params.tm, xe_fa_params.tn, xe_fa_params.decode_params.pipeline_flag_ph2 };
- ggml_vk_create_pipeline(device, pipelines.first, "xe_fa_decode_ph1", fa_decode_ph1_cm1_len, fa_decode_ph1_cm1_data, "main", 5, sizeof(vk_xe_fa_push_constants), { 1, xe_fa_params.decode_params.p_chunk_ph1, 1 }, sc_ph1, 1, false, true, xe_fa_params.subgroup_size[0]);
+ ggml_vk_create_pipeline(device, pipelines.first, "xe_fa_decode_ph1", fa_decode_ph1_cm1_len, fa_decode_ph1_cm1_data, "main", 7, sizeof(vk_xe_fa_push_constants), { 1, xe_fa_params.decode_params.p_chunk_ph1, 1 }, sc_ph1, 1, false, true, xe_fa_params.subgroup_size[0]);
ggml_vk_create_pipeline(device, pipelines.second, "xe_fa_decode_ph2", fa_decode_ph2_cm1_len, fa_decode_ph2_cm1_data, "main", 5, sizeof(vk_xe_fa_push_constants), { 1, 1, 1 }, sc_ph2, 1, false, true, xe_fa_params.subgroup_size[0]);
}
@@ -3633,8 +3643,9 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
std::vector<uint32_t> sc = { xe_fa_params.workgroup_size[0], xe_fa_params.prefill_params.wg_row, xe_fa_params.prefill_params.sg_col,
pipeline_state.HSK, pipeline_state.HSV, pipeline_state.qk_ratio, xe_fa_params.prefill_params.p_per_iter,
xe_fa_params.tm, xe_fa_params.tn, xe_fa_params.prefill_params.q_in_reg, shmem_sz, xe_fa_params.prefill_params.acc_count,
- xe_fa_params.prefill_params.pipeline_flag, xe_fa_params.subgroup_size[0], xe_fa_params.prefill_params.gqa_fold };
- ggml_vk_create_pipeline(device, pipelines, "xe_fa_prefill", fa_prefill_cm1_len, fa_prefill_cm1_data, "main", 6, sizeof(vk_xe_fa_push_constants), { xe_fa_params.prefill_params.gqa_fold, xe_fa_params.prefill_params.wg_row / xe_fa_params.prefill_params.gqa_fold, 1 }, sc, 1, false, true, xe_fa_params.subgroup_size[0]);
+ xe_fa_params.prefill_params.pipeline_flag, xe_fa_params.subgroup_size[0], xe_fa_params.prefill_params.gqa_fold,
+ pipeline_state.bias_bins };
+ ggml_vk_create_pipeline(device, pipelines, "xe_fa_prefill", fa_prefill_cm1_len, fa_prefill_cm1_data, "main", 8, sizeof(vk_xe_fa_push_constants), { xe_fa_params.prefill_params.gqa_fold, xe_fa_params.prefill_params.wg_row / xe_fa_params.prefill_params.gqa_fold, 1 }, sc, 1, false, true, xe_fa_params.subgroup_size[0]);
}
}
#endif
@@ -5312,6 +5323,7 @@ vk_device ggml_vk_get_device(size_t idx) {
device->coopmat2 = true;
device->coopmat2_bf16_support = found_bf16_128 && found_bf16_256;
device->coopmat2_decode_vector = coopmat2_decode_vector_support && coopmat2_decode_vector_features.cooperativeMatrixDecodeVector;
+ device->coopmat2_reserved_shmem = coopmat2_props.cooperativeMatrixWorkgroupScopeReservedSharedMemory;
}
}
#endif
@@ -9024,6 +9036,15 @@ bool ggml_vk_flash_attn_coopmat_shmem_support(const vk_device& device, const vk_
return supported;
}
+static bool ggml_vk_flash_attn_cm2_bias_stage_shmem_support(const vk_device& device, const vk_fa_tuning_params& params, bool f32acc) {
+ // Needs to be kept up to date on shader changes
+ const uint32_t acctype = f32acc ? 4 : 2;
+ const uint32_t bias_sh = params.block_rows * params.block_cols * acctype;
+ const uint32_t iq_shmem = 16 * sizeof(ggml_fp16_t);
+
+ return uint64_t(bias_sh) + iq_shmem + device->coopmat2_reserved_shmem <= device->properties.limits.maxComputeSharedMemorySize;
+}
+
void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * q, const ggml_tensor * k, const ggml_tensor * v, const ggml_tensor * mask, const ggml_tensor * sinks, ggml_tensor * dst) {
VK_LOG_DEBUG("ggml_vk_flash_attn((" << q << ", name=" << q->name << ", type=" << q->type << ", ne0=" << q->ne[0] << ", ne1=" << q->ne[1] << ", ne2=" << q->ne[2] << ", ne3=" << q->ne[3] << ", nb0=" << q->nb[0] << ", nb1=" << q->nb[1] << ", nb2=" << q->nb[2] << ", nb3=" << q->nb[3];
std::cerr << "), (" << k << ", name=" << k->name << ", type=" << k->type << ", ne0=" << k->ne[0] << ", ne1=" << k->ne[1] << ", ne2=" << k->ne[2] << ", ne3=" << k->ne[3] << ", nb0=" << k->nb[0] << ", nb1=" << k->nb[1] << ", nb2=" << k->nb[2] << ", nb3=" << k->nb[3];
@@ -9048,6 +9069,11 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
const uint32_t nem2 = mask ? mask->ne[2] : 0;
const uint32_t nem3 = mask ? mask->ne[3] : 0;
+ const bool has_bias = ggml_flash_attn_ext_has_indexed_bias(dst);
+ const ggml_tensor * bias_table = has_bias ? dst->src[6] : nullptr;
+ const ggml_tensor * bias_idx = has_bias ? dst->src[7] : nullptr;
+ const uint32_t bias_bins = has_bias ? (uint32_t)bias_table->ne[0] : 0;
+
const uint32_t HSK = nek0;
const uint32_t HSV = nev0;
uint32_t N = neq1;
@@ -9226,7 +9252,13 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
bool use_mask_opt = mask && !use_sparse && nem1 >= 32 && nem0 * nem1 > 32768 && nem0 >= tuning_params.block_cols * 16
&& (ctx->device->architecture != vk_device_architecture::AMD_GCN || HSK > 256 || HSV > 256);
vk_fa_pipeline_state fa_pipeline_state = get_fa_pipeline_state(ctx->device, tuning_params, HSK, HSV, aligned, f32acc,
- mask != nullptr, use_mask_opt, logit_softcap != 0, use_sparse, k_type_eff, v_type_eff);
+ mask != nullptr, use_mask_opt, logit_softcap != 0, use_sparse, k_type_eff, v_type_eff,
+ bias_bins);
+ // GQA rows share one index row, the per-element bias is cheaper there
+ if (bias_bins && gqa_ratio == 1 && tuning_params.path == FA_COOPMAT2 &&
+ ggml_vk_flash_attn_cm2_bias_stage_shmem_support(ctx->device, tuning_params, f32acc)) {
+ fa_pipeline_state.flags |= FA_FLAG_BIAS_STAGE;
+ }
vk_pipeline pipeline = nullptr;
@@ -9258,7 +9290,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
if (neq1 > 8 || pv_ratio > 64) {
uint32_t head_stride_q = uint32_t(nbq2 / ggml_type_size(q->type));
uint32_t batch_stride_q = uint32_t(nbq3 / ggml_type_size(q->type));
- vk_xe_fa_pipeline_state prefill_state = { (uint32_t)neq0, (uint32_t)nev0, qk_ratio, pv_ratio, 0, false, false, false, false };
+ vk_xe_fa_pipeline_state prefill_state = { (uint32_t)neq0, (uint32_t)nev0, qk_ratio, pv_ratio, 0, false, false, false, false, bias_bins };
prefill_state.unaligned_seq_len = nek1 % 32 != 0;
prefill_state.unaligned_q = head_stride_q % 8 != 0 || q_stride % 8 != 0 || batch_stride_q % 8 != 0;
prefill_state.partial_row_mask = neq1 > nem1;
@@ -9276,7 +9308,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
xe_fa_opt = false;
}
} else {
- vk_xe_fa_pipeline_state decode_state = { (uint32_t)neq0, (uint32_t)nev0, qk_ratio, pv_ratio, (uint32_t)neq1, false, false, false, false };
+ vk_xe_fa_pipeline_state decode_state = { (uint32_t)neq0, (uint32_t)nev0, qk_ratio, pv_ratio, (uint32_t)neq1, false, false, false, false, bias_bins };
decode_state.unaligned_seq_len = nek1 % 256 != 0;
xe_fa_params = get_xe_fa_tuning_params(vk_dev, decode_state, false);
const uint32_t split_p_size = xe_fa_params.decode_params.p_chunk_ph1;
@@ -9454,6 +9486,8 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
vk_subbuffer dst_buf = ggml_vk_tensor_subbuffer(ctx, dst);
vk_subbuffer mask_buf = mask ? ggml_vk_tensor_subbuffer(ctx, mask) : q_buf;
vk_subbuffer sinks_buf = sinks ? ggml_vk_tensor_subbuffer(ctx, sinks) : q_buf;
+ vk_subbuffer bias_table_buf = bias_table ? ggml_vk_tensor_subbuffer(ctx, bias_table) : q_buf;
+ vk_subbuffer bias_idx_buf = bias_idx ? ggml_vk_tensor_subbuffer(ctx, bias_idx) : q_buf;
vk_subbuffer mask_opt_buf = use_mask_opt ? ggml_vk_subbuffer(ctx, ctx->prealloc_y, 0) : q_buf;
vk_subbuffer sparse_buf = use_sparse ? ggml_vk_subbuffer(ctx, ctx->prealloc_y, 0) : q_buf;
if (use_mask_opt || use_sparse) {
@@ -9608,7 +9642,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
ggml_pipeline_request_descriptor_sets(ctx, xe_fa_pipeline_dual_phases.first, 1);
ggml_vk_dispatch_pipeline(ctx, subctx, xe_fa_pipeline_dual_phases.first,
- { q_temp_buf, k_buf, mask_buf, p_temp_buf, max_temp_buf },
+ { q_temp_buf, k_buf, mask_buf, p_temp_buf, max_temp_buf, bias_table_buf, bias_idx_buf },
pc_xe_fa, { (uint32_t)ph1_wg, (uint32_t)nek1, (uint32_t)neq3 });
ggml_vk_sync_buffers(ctx, subctx);
@@ -9621,7 +9655,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
} else {
ggml_pipeline_request_descriptor_sets(ctx, xe_fa_pipeline, 1);
ggml_vk_dispatch_pipeline(ctx, subctx, xe_fa_pipeline,
- { q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf },
+ { q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, bias_table_buf, bias_idx_buf },
pc_xe_fa, { (uint32_t)neq2, (uint32_t)neq1, (uint32_t)neq3 });
}
} else if (split_k > 1) {
@@ -9643,7 +9677,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
vk_subbuffer split_k_buf = ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0);
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
- {q_buf, k_buf, v_buf, mask_buf, sinks_buf, split_k_buf, mask_opt_buf, sparse_buf},
+ {q_buf, k_buf, v_buf, mask_buf, sinks_buf, split_k_buf, mask_opt_buf, sparse_buf, bias_table_buf, bias_idx_buf},
pc, { dispatch_x, workgroups_y, workgroups_z });
ggml_vk_sync_buffers(ctx, subctx);
@@ -9658,7 +9692,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
workgroups_x *= pipeline->wg_denoms[0];
}
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
- {q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, mask_opt_buf, sparse_buf},
+ {q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, mask_opt_buf, sparse_buf, bias_table_buf, bias_idx_buf},
pc, { workgroups_x, workgroups_y, workgroups_z });
}
@@ -16838,7 +16872,11 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
}
case GGML_OP_FLASH_ATTN_EXT:
{
- if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
+ if (op->src[5] != nullptr) {
+ return false;
+ }
+ if (ggml_flash_attn_ext_has_indexed_bias(op) &&
+ ((uint64_t)ggml_nelements(op->src[6]) > UINT32_MAX || (uint64_t)ggml_nelements(op->src[7]) > UINT32_MAX)) {
return false;
}
bool coopmat2 = device->coopmat2;
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
index c1e4606cd..5bcc996ab 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
@@ -32,6 +32,7 @@ const uint32_t rows_per_thread = Br / row_split;
const uint32_t cols_per_iter = WorkGroupSize / D_split / row_split;
const uint32_t cols_per_thread = Bc / cols_per_iter;
const uint32_t num_subgroups = SubGroupSize == 0 ? 0 : WorkGroupSize / SubGroupSize;
+const uint32_t mask_iters = (Bc * Br + WorkGroupSize - 1) / WorkGroupSize;
layout (binding = 0) readonly buffer Q {float data_q[];};
@@ -201,11 +202,39 @@ void main() {
// skip this block
continue;
}
- // Only load if the block is not all zeros
- if (mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ // Skip all-zero blocks unless a bias has to be staged
+ if (mask_opt_bits != MASK_OPT_ALL_ZERO || USE_BIAS) {
+ const bool mask_zero = USE_BIAS && mask_opt_bits == MASK_OPT_ALL_ZERO;
bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0;
float max_mask = NEG_FLT_MAX_OVER_2;
+
+ // Bias: all mask, index and table loads before the barrier; the max keeps them there
+ FLOAT_TYPE mb[mask_iters];
+ if (USE_BIAS) {
+ bool mact[mask_iters], bact[mask_iters];
+ FLOAT_TYPE mv[mask_iters];
+ uint32_t bin_base[mask_iters];
+ int32_t bin[mask_iters];
+ [[unroll]] for (uint32_t it = 0; it < mask_iters; ++it) {
+ const uint32_t c = (it * WorkGroupSize + tid) % Bc;
+ const uint32_t r = (it * WorkGroupSize + tid) / Bc;
+ uint32_t kcol, bq, bh;
+ const bool kv_active = fa_kv_index(j * Bc + c, kcol);
+ mact[it] = it * WorkGroupSize + tid < Bc * Br && kv_active && (!nem1_bounds_check || i * Br + r < p.nem1);
+ bact[it] = fa_bias_row(r, bq, bh) && mact[it];
+ mv[it] = FLOAT_TYPE(data_m[mact[it] && !mask_zero ? m_offset + (i * Br + r) * m_stride + kcol : 0]);
+ bin_base[it] = (bq * p.neq2 + bh) * BiasBins;
+ bin[it] = data_bias_idx[bact[it] ? bq * p.KV + kcol : 0];
+ }
+ [[unroll]] for (uint32_t it = 0; it < mask_iters; ++it) {
+ const float t = data_bias_table[bact[it] ? bin_base[it] + uint32_t(bin[it]) : 0];
+ const float m = mask_zero ? 0.0f : float(mv[it]);
+ mb[it] = FLOAT_TYPE(bact[it] ? m + t : m);
+ max_mask = mact[it] ? max(max_mask, float(mb[it])) : max_mask;
+ }
+ }
+
barrier();
[[unroll]] for (uint32_t idx = 0; idx < Bc * Br; idx += gl_WorkGroupSize.x) {
uint32_t c = (idx + tid) % Bc;
@@ -214,9 +243,9 @@ void main() {
uint32_t kcol;
bool kv_active = fa_kv_index(j * Bc + c, kcol);
if (kv_active && (!nem1_bounds_check || i * Br + r < p.nem1)) {
- FLOAT_TYPE m = FLOAT_TYPE(data_m[m_offset + (i * Br + r) * m_stride + kcol]);
- masksh[c * masksh_stride + r] = m;
+ FLOAT_TYPE m = USE_BIAS ? mb[idx / WorkGroupSize] : FLOAT_TYPE(data_m[m_offset + (i * Br + r) * m_stride + kcol]);
max_mask = max(max_mask, float(m));
+ masksh[c * masksh_stride + r] = m;
} else {
#if defined(FLOAT16)
masksh[c * masksh_stride + r] = USE_SPARSE ? FLOAT_TYPE(NEG_FLT16_MAX) : FLOAT_TYPE(0);
@@ -452,7 +481,7 @@ void main() {
}
}
- if (MASK_ENABLE && mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ if (MASK_ENABLE && (mask_opt_bits != MASK_OPT_ALL_ZERO || USE_BIAS)) {
[[unroll]] for (uint32_t c = 0; c < cols_per_thread; ++c) {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
FLOAT_TYPE mvf = masksh[(c * cols_per_iter + col_tid) * masksh_stride + tile_row(r)];
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl
index 072edfa68..30e08615d 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl
@@ -19,6 +19,8 @@ layout (constant_id = 13) const uint32_t FaTypeV = 0;
// sizeof(decode buffer): quants -> ggml block size; F32 -> 16 (decodeBufF32 vec4).
layout (constant_id = 14) const uint32_t FaBlockBytesK = 2;
layout (constant_id = 15) const uint32_t FaBlockBytesV = 2;
+// Indexed bias: table->ne[0], 0 = no bias
+layout (constant_id = 16) const uint32_t BiasBins = 0;
const bool USE_MASK_OPT = (Flags & 1) != 0;
const bool MASK_ENABLE = (Flags & 2) != 0;
@@ -30,6 +32,7 @@ const bool Q_IN_REGS = (Flags & 32) != 0;
// coopmat1: subgroups own Q rows, O in coopmat accumulators. K/V staged in LDS, or loaded from global memory with FLIP_DIRECT.
const bool FLIP = (Flags & 64) != 0;
const bool FLIP_DIRECT = (Flags & 128) != 0;
+const bool USE_BIAS = BiasBins != 0;
// Round up head sizes to a multiple of 16, for coopmat1/coopmat2 paths
const uint32_t HSK_pad = (HSK + 15) & ~15;
@@ -90,6 +93,9 @@ layout (binding = 6) readonly buffer MO {uint32_t data_mask_opt[];};
layout (binding = 7) readonly buffer SP {int32_t data_sparse[];};
+layout (binding = 8) readonly buffer BT {float data_bias_table[];};
+layout (binding = 9) readonly buffer BI {int32_t data_bias_idx[];};
+
// Use -FLT_MAX/2 rather than -inf to reduce the possibility of NaNs, e.g. when computing Mold-M.
#define NEG_FLT_MAX_OVER_2 uintBitsToFloat(0xFEFFFFFF)
// Lowest finite f16, mask value for inactive sparse KV slots
@@ -250,6 +256,23 @@ bool fa_kv_index(uint lin, out uint kv_col) {
return !KV_bounds_check || lin < KV;
}
+// Flattened query and Q head of tile row r. With GQA collapse rows are heads. False for padding rows.
+bool fa_bias_row(const in uint32_t r, out uint32_t query, out uint32_t head) {
+ if (p.gqa_ratio > 1) {
+ query = iq3 * p.nem1 + gqa_iq1;
+ head = iq2 + r;
+ return r < N;
+ }
+ query = iq3 * p.nem1 + i * Br + r;
+ head = iq2;
+ return i * Br + r < N;
+}
+
+float fa_bias(const in uint32_t query, const in uint32_t head, const in uint32_t kv_col) {
+ const uint32_t bin = uint32_t(data_bias_idx[query * p.KV + kv_col]);
+ return data_bias_table[(query * p.neq2 + head) * BiasBins + bin];
+}
+
// Bias applied to softmax to stay in fp16 range.
// Based on ggml-cuda issue https://github.com/ggml-org/llama.cpp/issues/18606
const float FATTN_KQ_MAX_OFFSET = 3.0f*0.6931f;
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp
index ea945ed07..85ca98d36 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp
@@ -230,8 +230,8 @@ void main() {
// skip this block
continue;
}
- // Only load if the block is not all zeros
- if (mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ // Skip all-zero blocks unless a bias has to be staged
+ if (mask_opt_bits != MASK_OPT_ALL_ZERO || USE_BIAS) {
bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0;
float max_mask = NEG_FLT_MAX_OVER_2;
@@ -241,9 +241,27 @@ void main() {
if (idx + tid < Bc * Br / 4 || idx + gl_WorkGroupSize.x <= Bc * Br / 4) {
uint32_t kcol;
bool kv_active = fa_kv_index(j * Bc + c, kcol);
- if (kv_active) {
+ if (USE_BIAS && kv_active && p.gqa_ratio > 1 && mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ // GQA: the rows share one mask value and one bias index
+ uint32_t bq, bh0;
+ fa_bias_row(0, bq, bh0);
+ const float mv = float(data_m[m_offset + kcol]);
+ const uint32_t bin = uint32_t(data_bias_idx[bq * p.KV + kcol]);
+ vec4 mb = vec4(mv);
+ [[unroll]] for (uint32_t k = 0; k < 4; ++k) {
+ uint32_t q, h;
+ const bool v = fa_bias_row(r * 4 + k, q, h);
+ const float b = data_bias_table[(bq * p.neq2 + (v ? h : bh0)) * BiasBins + bin];
+ mb[k] += v ? b : 0.0;
+ }
+ max_mask = max(max_mask, mv);
+ mask_cache[idx / WorkGroupSize] = f16vec4(mb);
+ } else if (kv_active) {
f16vec4 m;
- if (USE_SPARSE) {
+ if (USE_BIAS && mask_opt_bits == MASK_OPT_ALL_ZERO) {
+ m = f16vec4(0.0);
+ max_mask = 0.0;
+ } else if (USE_SPARSE) {
// sparse is gqa-gated (m_stride == 0): all four rows share the value
FLOAT_TYPE mv = FLOAT_TYPE(data_m[m_offset + kcol]);
m = f16vec4(mv);
@@ -275,6 +293,26 @@ void main() {
} else {
m = f16vec4(0.0);
}
+ uint32_t bq0, bh0;
+ if (USE_BIAS && fa_bias_row(r * 4, bq0, bh0)) {
+ vec4 mb = vec4(m);
+ uint32_t bq[4], bh[4];
+ bool bv[4];
+ [[unroll]] for (uint32_t k = 0; k < 4; ++k) {
+ bv[k] = fa_bias_row(r * 4 + k, bq[k], bh[k]);
+ bq[k] = bv[k] ? bq[k] : bq0;
+ bh[k] = bv[k] ? bh[k] : bh0;
+ }
+ uint32_t bin[4];
+ [[unroll]] for (uint32_t k = 0; k < 4; ++k) {
+ bin[k] = uint32_t(data_bias_idx[bq[k] * p.KV + kcol]);
+ }
+ [[unroll]] for (uint32_t k = 0; k < 4; ++k) {
+ const float b = data_bias_table[(bq[k] * p.neq2 + bh[k]) * BiasBins + bin[k]];
+ mb[k] += bv[k] ? b : 0.0;
+ }
+ m = f16vec4(mb);
+ }
mask_cache[idx / WorkGroupSize] = m;
} else if (USE_SPARSE) {
mask_cache[idx / WorkGroupSize] = f16vec4(NEG_FLT16_MAX);
@@ -416,7 +454,7 @@ void main() {
barrier();
}
- if (MASK_ENABLE && mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ if (MASK_ENABLE && (mask_opt_bits != MASK_OPT_ALL_ZERO || USE_BIAS)) {
[[unroll]] for (uint32_t idx = 0; idx < Bc * Br / 4; idx += gl_WorkGroupSize.x) {
uint32_t c = (idx + tid) / (Br / 4);
uint32_t r = (idx + tid) % (Br / 4);
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 6d8641a91..2c38dbc7e 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
@@ -307,6 +307,25 @@ void main() {
}
}
+ float bv[FL_CPL];
+ [[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
+ bv[k] = 0.0;
+ }
+ {
+ 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 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]);
+ }
+ [[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
+ bv[k] = data_bias_table[toff + bin[k]];
+ }
+ }
+ }
+
coopmat<ACC_TYPE, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> Sacc[Bc / 16];
[[unroll]] for (uint32_t c = 0; c < Bc / 16; ++c) {
Sacc[c] = coopmat<ACC_TYPE, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(0);
@@ -342,10 +361,15 @@ void main() {
}
}
- if (MASK_ENABLE && mask_bits != MASK_OPT_ALL_ZERO && myrow < p.nem1) {
+ 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;
+ 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]);
+ }
+ }
[[unroll]] for (uint32_t k = 0; k < FL_CPL; ++k) {
- s[k] += slope_v * float(data_m[moff + k]);
+ s[k] += slope_v * bv[k];
}
}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp
index 9ddb4eb82..cfc967000 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp
@@ -203,9 +203,49 @@ ACC_TYPE faAddSparseMask(const uint32_t row, const uint32_t col, const ACC_TYPE
const uint32_t kvslot = jblk * Bc + col;
if (kvslot >= p.split_kv) { return ACC_TYPE_NEG; }
const int r = data_sparse[sparse_base + kvslot];
- return r < 0 ? ACC_TYPE_NEG : elem + ACC_TYPE(data_mf16[g_m_off_elem + row * m_stride + uint(r)]);
+ if (r < 0) { return ACC_TYPE_NEG; }
+ ACC_TYPE mv = ACC_TYPE(data_mf16[g_m_off_elem + row * m_stride + uint(r)]);
+ uint32_t bq, bh;
+ if (USE_BIAS && fa_bias_row(row, bq, bh)) {
+ mv += ACC_TYPE(fa_bias(bq, bh, uint(r)));
+ }
+ return elem + mv;
+}
+
+ACC_TYPE faAddBias(const uint32_t row, const uint32_t col, const ACC_TYPE elem, const uint32_t jblk) {
+ const uint32_t kv_col = jblk * Bc + col;
+ uint32_t bq, bh;
+ if ((KV_bounds_check && kv_col >= KV) || !fa_bias_row(row, bq, bh)) {
+ return elem;
+ }
+ return elem + ACC_TYPE(fa_bias(bq, bh, kv_col));
}
+#ifdef FA_BIAS_STAGE
+const uint32_t BIAS_ROW_STEP = WorkGroupSize / Bc;
+const uint32_t BIAS_ROWS = Br / BIAS_ROW_STEP;
+shared ACC_TYPE bias_sh[Br * Bc];
+
+// Subgroups read contiguous columns of one row, so the index and table loads coalesce.
+void faStageBias(const uint32_t jblk) {
+ const uint32_t col = gl_LocalInvocationIndex % Bc;
+ const uint32_t r0 = gl_LocalInvocationIndex / Bc;
+ const uint32_t kv_col = jblk * Bc + col;
+ int32_t bins[BIAS_ROWS];
+ uint32_t boff[BIAS_ROWS];
+ bool ok[BIAS_ROWS];
+ [[unroll]] for (uint32_t k = 0; k < BIAS_ROWS; ++k) {
+ uint32_t bq, bh;
+ ok[k] = (!KV_bounds_check || kv_col < KV) && fa_bias_row(r0 + k * BIAS_ROW_STEP, bq, bh);
+ boff[k] = (bq * p.neq2 + bh) * BiasBins;
+ bins[k] = ok[k] ? data_bias_idx[bq * p.KV + kv_col] : 0;
+ }
+ [[unroll]] for (uint32_t k = 0; k < BIAS_ROWS; ++k) {
+ bias_sh[(r0 + k * BIAS_ROW_STEP) * Bc + col] = ok[k] ? ACC_TYPE(data_bias_table[boff[k] + uint32_t(bins[k])]) : ACC_TYPE(0);
+ }
+}
+#endif
+
ACC_TYPE maxReduce(const in ACC_TYPE x, const in ACC_TYPE y) {
return max(x, y);
}
@@ -408,6 +448,14 @@ void main() {
}
}
+#ifdef FA_BIAS_STAGE
+ if (MASK_ENABLE) {
+ barrier();
+ faStageBias(j);
+ barrier();
+ }
+#endif
+
coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> S = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(0);
coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, HSK_pad, Bc, gl_MatrixUseB> K_T;
@@ -435,7 +483,17 @@ void main() {
if (MASK_ENABLE && USE_SPARSE) {
coopMatPerElementNV(S, S, faAddSparseMask, j);
} else if (MASK_ENABLE) {
- S += slopeMat*coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(mv);
+ coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> mva = coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator>(mv);
+#ifdef FA_BIAS_STAGE
+ coopmat<ACC_TYPE, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> bias;
+ coopMatLoad(bias, bias_sh, 0, Bc, gl_CooperativeMatrixLayoutRowMajor);
+ mva += bias;
+#else
+ if (USE_BIAS) {
+ coopMatPerElementNV(mva, mva, faAddBias, j);
+ }
+#endif
+ S += slopeMat*mva;
}
// Clear padding elements to -inf, so they don't contribute to rowmax
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_decode_phase_1.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_decode_phase_1.comp
index 53beab149..293749fc6 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_decode_phase_1.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_decode_phase_1.comp
@@ -16,6 +16,8 @@ layout (binding = 1) readonly buffer KV4 {f16vec4 data_kv4[];};
layout (binding = 2) readonly buffer M {float16_t data_m[];};
layout (binding = 3) buffer P {float16_t data_p[];};
layout (binding = 4) buffer MAX {float data_max[];};
+layout (binding = 5) readonly buffer BT {float data_bias_table[];};
+layout (binding = 6) readonly buffer BI {int data_bias_idx[];};
layout (push_constant) uniform parameter
{
@@ -54,6 +56,7 @@ layout (constant_id = 6) const uint pmat_count = 4;
layout (constant_id = 7) const uint TM = 8;
layout (constant_id = 8) const uint TN = 16;
layout (constant_id = 9) const uint shmem_size = 49152;
+layout (constant_id = 10) const uint BiasBins = 0;
const uint TK = 16;
const uint sg_count = WorkGroupSize / SubGroupSize;
@@ -295,6 +298,17 @@ void main() {
Of[r][idx] = Of[r][idx] + Maskf[idx];
}
+ if (BiasBins != 0 && sg * warp_o_row + r < gqa_ratio) {
+ const uint bias_q = batch_idx * p.neq1 + out_r + out_idx * Br;
+ const uint bias_t = (bias_q * p.neq2 + head_q_idx) * BiasBins;
+ [[unroll]] for (uint idx = 0; idx < p_reduce / SubGroupSize; ++idx) {
+ const uint mask_offset = idx * SubGroupSize + mask_base;
+ if (mask_offset < p.nek1) {
+ Of[r][idx] += data_bias_table[bias_t + data_bias_idx[bias_q * p.nek1 + mask_offset]];
+ }
+ }
+ }
+
float Mtempf = NEG_FLT_MAX_OVER_2;
[[unroll]] for (uint idx = 0; idx < p_reduce / SubGroupSize; ++idx) {
const uint p_col_offset = idx * SubGroupSize + mask_base;
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_prefill.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_prefill.comp
index 407654ca5..721755466 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_prefill.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_prefill.comp
@@ -24,6 +24,8 @@ layout (binding = 3) readonly buffer MV2 {f16vec2 data_mv2[];};
layout (binding = 3) readonly buffer MV4 {f16vec4 data_mv4[];};
layout (binding = 4) readonly buffer S {float data_s[];};
layout (binding = 5) buffer OV4 {vec4 data_ov4[];};
+layout (binding = 6) readonly buffer BT {float data_bias_table[];};
+layout (binding = 7) readonly buffer BI {int data_bias_idx[];};
layout (push_constant) uniform parameter
{
@@ -67,6 +69,7 @@ layout (constant_id = 11) const uint acc_count = 1;
layout (constant_id = 12) const uint Flags = 0;
layout (constant_id = 13) const uint SubGroupSize = 16;
layout (constant_id = 14) const uint gqa_fold = 1;
+layout (constant_id = 15) const uint BiasBins = 0;
#define UNALIGNED_SEQ_LEN_BIT (1 << 0)
#define UNALIGNED_Q_STRIDE_BIT (1 << 1)
@@ -532,6 +535,20 @@ void main() {
}
}
+ if (BiasBins != 0) {
+ const uint bias_row = out_r * folded_row_wg + sg_o_r_row * TN + lane_c;
+ if (bias_row < p.neq1) {
+ const uint bias_q = batch_idx * p.neq1 + bias_row;
+ const uint bias_t = (bias_q * p.neq2 + head_q_idx * gqa_fold + sg_o_r_h) * BiasBins;
+ const uint bias_col = loop * step_p + lane_r * softmax_loops;
+ [[unroll]] for (uint idx = 0; idx < softmax_loops; ++idx) {
+ if (!seq_boundary_check || bias_col + idx < p.nek1) {
+ Pf[idx] += data_bias_table[bias_t + data_bias_idx[bias_q * p.nek1 + bias_col + idx]];
+ }
+ }
+ }
+ }
+
if (seq_boundary_check && loop + 1 == loop_count) {
[[unroll]] for (uint idx = 0; idx < softmax_loops; ++idx) {
if (loop * step_p + idx + lane_r * softmax_loops < p.nek1) {
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
index fb99f8f48..bac0fcfc0 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
@@ -736,6 +736,8 @@ void process_shaders() {
#if defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
string_to_spv("flash_attn_f32_f16", "flash_attn_cm2.comp",
merge_maps(fa_base_dict, {{"Q_TYPE", "float"}, {"D_TYPE", "float"}, {"D_TYPEV4", "vec4"}}), fp16, false, true, f16acc);
+ string_to_spv("flash_attn_f32_f16", "flash_attn_cm2.comp",
+ merge_maps(fa_base_dict, {{"Q_TYPE", "float"}, {"D_TYPE", "float"}, {"D_TYPEV4", "vec4"}, {"FA_BIAS_STAGE", "1"}}), fp16, false, true, f16acc, "_bias");
#endif
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
@@ -787,6 +789,9 @@ void process_shaders() {
string_to_spv("flash_attn_f32_f16_bf16", "flash_attn_cm2.comp",
merge_maps(fa_bf16_dict, {{"Q_TYPE", "float"}, {"D_TYPE", "float"}, {"D_TYPEV4", "vec4"}}),
true, false, true, false);
+ string_to_spv("flash_attn_f32_f16_bf16", "flash_attn_cm2.comp",
+ merge_maps(fa_bf16_dict, {{"Q_TYPE", "float"}, {"D_TYPE", "float"}, {"D_TYPEV4", "vec4"}, {"FA_BIAS_STAGE", "1"}}),
+ true, false, true, false, "_bias");
#endif
std::map<std::string, std::string> base_dict = {{"FLOAT_TYPE", "float"}, {"FLOAT_TYPEV2", "vec2"}};
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 164a0d95b..aeb29585a 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -10466,11 +10466,14 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
const ggml_type type_KV;
const float logit_softcap;
const bool mask_row; // query 0 of each stream sees no key
+ const bool causal; // prefix of 0 per query, -inf after, distance bins
+ const int64_t n_kv_max; // sparse hint
std::vector<int32_t> rows;
+ std::vector<int64_t> visible;
std::string vars() override {
- return VARS_TO_STR12(hs, nh, gqa, kv, nb, n_stream, n_bins, kv_rows, sinks, type_KV, logit_softcap, mask_row);
+ return VARS_TO_STR14(hs, nh, gqa, kv, nb, n_stream, n_bins, kv_rows, sinks, type_KV, logit_softcap, mask_row, causal, n_kv_max);
}
double max_nmse_err() override {
@@ -10479,9 +10482,9 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
test_flash_attn_ext_indexed_bias(int64_t hs = 128, int64_t nh = 2, int64_t gqa = 3, int64_t kv = 768, int64_t nb = 7, int64_t n_stream = 2,
int64_t n_bins = 17, bool kv_rows = false, bool sinks = true, ggml_type type_KV = GGML_TYPE_F16,
- float logit_softcap = 0.0f, bool mask_row = false)
+ float logit_softcap = 0.0f, bool mask_row = false, bool causal = false, int64_t n_kv_max = 0)
: hs(hs), nh(nh), gqa(gqa), kv(kv), nb(nb), n_stream(n_stream), n_bins(n_bins), kv_rows(kv_rows), sinks(sinks), type_KV(type_KV),
- logit_softcap(logit_softcap), mask_row(mask_row) {}
+ logit_softcap(logit_softcap), mask_row(mask_row), causal(causal), n_kv_max(n_kv_max) {}
ggml_tensor * build_graph(ggml_context * ctx) override {
ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs, nb, nh*gqa, n_stream);
@@ -10514,6 +10517,7 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
ggml_flash_attn_ext_add_sinks(out, s);
}
ggml_flash_attn_ext_add_indexed_bias(out, table, indices);
+ ggml_flash_attn_ext_set_n_kv_max(out, n_kv_max);
ggml_prec_set_acc(out, GGML_PREC_F32);
ggml_set_name(out, "out");
@@ -10538,18 +10542,33 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
}
}
+ visible.assign(nb*n_stream, kv);
+ if (causal) {
+ for (int64_t s = 0; s < n_stream; ++s) {
+ for (int64_t j = 0; j < nb; ++j) {
+ visible[s*nb + j] = kv/2 + (j + 1)*(kv/2)/nb - 37*s;
+ }
+ }
+ }
+
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
if (strcmp(t->name, "r") == 0) {
ggml_backend_tensor_set(t, rows.data(), 0, rows.size()*sizeof(int32_t));
+ } else if (strcmp(t->name, "m") == 0 && n_kv_max > 0) {
+ init_tensor_kq_mask_sparse(t, n_kv_max);
} else if (strcmp(t->name, "m") == 0) {
std::vector<float> f(ggml_nelements(t));
std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
for (int64_t s = 0; s < n_stream; ++s) {
for (int64_t j = 0; j < nb; ++j) {
for (int64_t i = 0; i < kv; ++i) {
- const bool pad = kv_rows && rows[s*kv + i] < 0;
- const bool drop = (i + 3*j + 5*s) % 11 == 0 || (mask_row && j == 0);
- f[(s*nb + j)*kv + i] = pad || drop ? -INFINITY : dis(gen);
+ const bool masked = (kv_rows && rows[s*kv + i] < 0) || (mask_row && j == 0);
+ if (causal) {
+ f[(s*nb + j)*kv + i] = masked || i >= visible[s*nb + j] ? -INFINITY : 0.0f;
+ continue;
+ }
+ const bool drop = (i + 3*j + 5*s) % 11 == 0;
+ f[(s*nb + j)*kv + i] = masked || drop ? -INFINITY : dis(gen);
}
}
}
@@ -10562,6 +10581,14 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
for (auto & i : idx) {
i = dis(gen);
}
+ if (causal) {
+ for (int64_t q = 0; q < nb*n_stream; ++q) {
+ for (int64_t i = 0; i < kv; ++i) {
+ const int64_t dist = visible[q] - 1 - i;
+ idx[q*kv + i] = dist < 0 ? n_bins - 1 : (int32_t) std::min<int64_t>(dist, n_bins - 1);
+ }
+ }
+ }
ggml_backend_tensor_set(t, idx.data(), 0, idx.size()*sizeof(int32_t));
} else if (strcmp(t->name, "table") == 0) {
init_tensor_uniform(t, -4.0f, 4.0f);
@@ -14866,6 +14893,22 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, nb, 2, 17, kv_rows, false, GGML_TYPE_F16, 0.0f, true));
}
}
+ for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0}) {
+ for (int64_t nb : {1, 4, 64}) {
+ test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 2048, nb, 2, 33, false, true, type, 0.0f, false, true));
+ }
+ }
+ for (int64_t nb : {1, 4}) {
+ test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 4096, nb, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, false, 512));
+ }
+ test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(256, 2, 3, 768, 65, 2, 17));
+ for (int64_t hs : {128, 256}) {
+ for (int64_t nb : {1, 7, 65}) {
+ test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(hs, 2, 3, 1000, nb, 2, 17));
+ }
+ }
+ test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(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_indexed_bias(128, 2, 3, 1100, 7, 2, 33, false, true, GGML_TYPE_Q8_0));
// 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));