Commit 2f94007cc for llama.cpp
commit 2f94007cc16c6f9ae07909949c0061aafe77e0d0
Author: ravel7524 <58877666+ravel7524@users.noreply.github.com>
Date: Mon Nov 30 07:08:15 2026 +0100
vulkan: fold verify tokens and GQA heads into one flash-attention row tile (#30190)
* vulkan: fold verify tokens and GQA heads into one flash-attention row tile
* vulkan: enable shape-gated query-token folding
* vulkan: port folding to CM2 fa shader
* Update scaling of accumulator in flash_attn.comp
* Enhance ggml_vk_flash_attn function with scalar shared memory support and add new VARS_TO_STR macros for extended variable representation in tests
* tests : remove .DS_Store
* Update test cases in test-backend-ops.cpp
Refactor test cases for GQA with query tokens and folded tiles.
* Removed Whitespace
---------
Co-authored-by: Jeff Bolz <jbolz@nvidia.com>
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-common.h b/ggml/src/ggml-vulkan/ggml-vulkan-common.h
index 262fa8876..301df8d95 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, uint32_t bias_bins);
+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 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);
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 dd6838031..62fea45f9 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -451,10 +451,11 @@ struct vk_fa_pipeline_state {
ggml_type k_type;
ggml_type v_type;
uint32_t bias_bins;
+ uint32_t gqa_tok;
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, 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);
+ 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, gqa_tok) <
+ 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, b.gqa_tok);
}
};
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 0bfefc36e..a32f93eb0 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -1451,7 +1451,7 @@ 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,
- uint32_t bias_bins) {
+ uint32_t bias_bins, uint32_t gqa_tok) {
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);
@@ -1466,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, bias_bins};
+ 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, gqa_tok};
}
static uint32_t fa_block_bytes(ggml_type t) {
@@ -1495,6 +1495,7 @@ static std::vector<uint32_t> get_fa_spec_constants(const vk_fa_pipeline_state& s
/*14 FaBlockBytesK */ fa_block_bytes(state.k_type),
/*15 FaBlockBytesV */ fa_block_bytes(state.v_type),
/*16 BiasBins */ state.bias_bins,
+ /*17 GQA_TOK */ state.gqa_tok,
};
}
@@ -9197,6 +9198,39 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
(int64_t)KV >= std::max<int64_t>(4096, min_ratio * (int64_t)n_kv_max) &&
(gqa_ratio > 1 || (tuning_params.path == FA_SCALAR && N == 1));
+ // Intel Alchemist prefers more workgroups
+ const uint32_t shader_core_count_multiplier = (ctx->device->vendor_id == VK_VENDOR_ID_INTEL && ctx->device->architecture != INTEL_XE2) ? 2 : 1;
+
+ // Use a placeholder core count if one isn't available. split_k is a big help for perf.
+ const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count * shader_core_count_multiplier : 16;
+
+ // Fold several query tokens into one GQA row tile, so each K/V block is loaded once per gqa_tok tokens
+ uint32_t gqa_tok = 1;
+ static const bool disable_qtok_fold = getenv("GGML_VK_FA_QTOK_FOLD_DISABLE") != nullptr;
+ if (!disable_qtok_fold && gqa_ratio > 1 && neq1 > 1 && !use_sparse) {
+ vk_fa_tuning_params fold_params = tuning_params;
+ 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);
+ // 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;
+ const uint32_t fold_split = std::max(1u, shader_core_count * 2 / fold_wgs);
+ const uint32_t split_blocks = ROUNDUP_POW2(std::max(1u, KV / fold_split), fold_params.block_cols) / fold_params.block_cols;
+ fold_pays = neq1 > tuning_params.block_rows ||
+ (CEIL_DIV(neq1, fold_tok) * fold_params.block_rows <= neq1 * tuning_params.block_rows && split_blocks >= 16);
+ fold_pays = fold_pays && ggml_vk_flash_attn_scalar_shmem_support(ctx->device, fold_params, HSK, HSV, f32acc, k_type_eff, v_type_eff);
+ }
+ // the mask opt bits are read for row block 0, so it has to hold every token
+ if (fold_pays && fold_params.path == tuning_params.path && fold_params.block_rows >= 2 * gqa_ratio && fold_params.block_rows >= neq1) {
+ tuning_params = fold_params;
+ gqa_tok = tuning_params.block_rows / gqa_ratio;
+ N = gqa_tok * gqa_ratio;
+ workgroups_x = (uint32_t)CEIL_DIV(neq1, gqa_tok);
+ }
+ }
+
const uint32_t q_stride = (uint32_t)(nbq1 / ggml_type_size(q->type));
const uint32_t q_stride_eff = gqa_ratio > 1 ? (uint32_t)(nbq2 / ggml_type_size(q->type)) : q_stride;
uint32_t k_stride = (uint32_t)(nbk1 / ggml_type_size(k->type));
@@ -9253,7 +9287,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
&& (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,
- bias_bins);
+ bias_bins, gqa_tok);
// 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)) {
@@ -9358,12 +9392,6 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
uint32_t split_kv = KV;
uint32_t split_k = 1;
- // Intel Alchemist prefers more workgroups
- const uint32_t shader_core_count_multiplier = (ctx->device->vendor_id == VK_VENDOR_ID_INTEL && ctx->device->architecture != INTEL_XE2) ? 2 : 1;
-
- // Use a placeholder core count if one isn't available. split_k is a big help for perf.
- const uint32_t shader_core_count = ctx->device->shader_core_count ? ctx->device->shader_core_count * shader_core_count_multiplier : 16;
-
const uint32_t Br = fa_pipeline_state.Br;
const uint32_t Bc = fa_pipeline_state.Bc;
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
index 5bcc996ab..b56665f61 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp
@@ -113,16 +113,18 @@ void main() {
[[unroll]] for (uint32_t idx = 0; idx < Br * HSK / 4; idx += gl_WorkGroupSize.x) {
uint32_t d = (idx + tid) % (HSK / 4);
uint32_t r = (idx + tid) / (HSK / 4);
- const bool is_in_bounds = r < Br && d < HSK / 4 && i * Br + r < N;
+ const bool is_in_bounds = r < Br && d < HSK / 4 && (GQA_TOK > 1 ? row_valid(r) : i * Br + r < N);
+ const uint32_t q_idx = GQA_TOK > 1 ? (row_tok(r) * p.nb01 + (row_head(r) * p.nb02 + iq3 * p.nb03) / 4) / 4 + d
+ : q_offset / 4 + (i * Br + r) * q_stride / 4 + d;
#ifndef MMQ
if (is_in_bounds) {
- Qf[r * qf_stride + d] = FLOAT_TYPEV4(data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale);
+ Qf[r * qf_stride + d] = FLOAT_TYPEV4(data_qv4[q_idx] * p.scale);
}
#else
const uint buf_ib = r * qf_stride + d / 8;
const uint buf_iqs = d % 8;
- vec4 vals = is_in_bounds ? data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale : vec4(0.0f);
+ vec4 vals = is_in_bounds ? data_qv4[q_idx] * p.scale : vec4(0.0f);
const vec4 abs_vals = abs(vals);
const float thread_max = max(max(abs_vals.x, abs_vals.y), max(abs_vals.z, abs_vals.w));
@@ -180,7 +182,7 @@ void main() {
// FaBlockBytesK/V == 2 for f16, 16 for f32, ggml block byte size for quants.
uint32_t k_offset = (ik2*p.nb12 + ik3*p.nb13) / FaBlockBytesK;
uint32_t v_offset = (iv2*p.nb22 + iv3*p.nb23) / FaBlockBytesV;
- uint32_t m_offset = gqa_iq1*KV;
+ uint32_t m_offset = (GQA_TOK > 1 ? 0u : gqa_iq1)*KV;
if (p.nem2 != 1 || p.nem3 != 1) {
m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV;
mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride;
@@ -223,7 +225,8 @@ void main() {
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]);
+ const uint32_t m_row = GQA_TOK > 1 ? min(row_tok(r), p.nem1 - 1) * KV : (i * Br + r) * m_stride;
+ mv[it] = FLOAT_TYPE(data_m[mact[it] && !mask_zero ? m_offset + m_row + kcol : 0]);
bin_base[it] = (bq * p.neq2 + bh) * BiasBins;
bin[it] = data_bias_idx[bact[it] ? bq * p.KV + kcol : 0];
}
@@ -243,7 +246,8 @@ 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 = USE_BIAS ? mb[idx / WorkGroupSize] : FLOAT_TYPE(data_m[m_offset + (i * Br + r) * m_stride + kcol]);
+ const uint32_t m_row = GQA_TOK > 1 ? min(row_tok(r), p.nem1 - 1) * KV : (i * Br + r) * m_stride;
+ FLOAT_TYPE m = USE_BIAS ? mb[idx / WorkGroupSize] : FLOAT_TYPE(data_m[m_offset + m_row + kcol]);
max_mask = max(max_mask, float(m));
masksh[c * masksh_stride + r] = m;
} else {
@@ -455,7 +459,8 @@ void main() {
acc += dotPacked4x8EXT(Qf[qib].qs[qiqs + d], k_quants[d]);
}
- Sf[r][c] += ACC_TYPE(acc) * ACC_TYPE(Qf[qib].ds.x) * k_dm.x;
+ // acc can be far above the fp16 range: scale it in fp32 before it is narrowed to ACC_TYPE
+ Sf[r][c] += ACC_TYPE(float(acc) * float(Qf[qib].ds.x) * float(k_dm.x));
if ((d_tid * (HSK_per_thread / 4) + d_block) % 8 == 0) {
Sf[r][c] += k_dot_correction(qib, k_dm);
}
@@ -695,7 +700,7 @@ void main() {
// If there is split_k, then the split_k resolve shader does the final
// division by L. Store the intermediate O value and per-row m and L values.
if (p.k_num > 1) {
- if (p.gqa_ratio > 1) {
+ if (p.gqa_ratio > 1 && GQA_TOK == 1) {
// note: O and Q have swapped coord 1,2.
uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)) / 4;
@@ -719,20 +724,22 @@ void main() {
} else {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
const uint row = tile_row(r);
- const uint global_row = i * Br + row;
+ const uint global_row = GQA_TOK > 1 ? row_tok(row) : i * Br + row;
+ const uint head = GQA_TOK > 1 ? row_head(row) : iq2;
+ const bool row_ok = GQA_TOK > 1 ? row_valid(row) : global_row < N;
- if (global_row < N) {
+ if (row_ok) {
uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)) / 4;
[[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) {
- data_ov4[o_offset + iq2 * HSV/4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]);
+ data_ov4[o_offset + head * HSV/4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]);
}
}
- if (global_row < N && d_tid == 0 && col_tid == 0) {
+ if (row_ok && d_tid == 0 && col_tid == 0) {
uint32_t lm_offset = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 + p.ne1 * 2 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3));
- data_o[lm_offset + iq2] = D_TYPE(Lf[r]);
- data_o[lm_offset + p.ne1 + iq2] = D_TYPE(Mf[r]);
+ data_o[lm_offset + head] = D_TYPE(Lf[r]);
+ data_o[lm_offset + p.ne1 + head] = D_TYPE(Mf[r]);
}
}
}
@@ -774,9 +781,9 @@ void main() {
}
}
- uint32_t o_offset = (gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4;
+ uint32_t o_offset = ((GQA_TOK > 1 ? 0u : gqa_iq1)*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4;
- if (p.gqa_ratio > 1) {
+ if (p.gqa_ratio > 1 && GQA_TOK == 1) {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
const uint row = tile_row(r);
if (row < N) {
@@ -788,9 +795,11 @@ void main() {
} else {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
const uint row = tile_row(r);
- if (i * Br + row < N) {
+ const uint global_row = GQA_TOK > 1 ? row_tok(row) : i * Br + row;
+ const uint head = GQA_TOK > 1 ? row_head(row) : iq2;
+ if (GQA_TOK > 1 ? row_valid(row) : global_row < N) {
[[unroll]] for (uint32_t d = 0; d < HSV_per_thread / 4; ++d) {
- data_ov4[o_offset + (iq2 * HSV + (i * Br + row) * p.ne1 * HSV) / 4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]);
+ data_ov4[o_offset + (head * HSV + global_row * p.ne1 * HSV) / 4 + d * D_split + d_tid] = D_TYPEV4(Of[r][d]);
}
}
}
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 30e08615d..156372919 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl
@@ -21,6 +21,8 @@ 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;
+// GQA path: query tokens per row tile; 1 = one token per tile
+layout (constant_id = 17) const uint32_t GQA_TOK = 1;
const bool USE_MASK_OPT = (Flags & 1) != 0;
const bool MASK_ENABLE = (Flags & 2) != 0;
@@ -241,6 +243,17 @@ void init_indices()
}
}
+// GQA_TOK > 1: tile row r holds token gqa_iq1 * GQA_TOK + r / gqa_ratio and head iq2 + r % gqa_ratio
+uint32_t row_tok(const in uint32_t r) {
+ return gqa_iq1 * GQA_TOK + r / p.gqa_ratio;
+}
+uint32_t row_head(const in uint32_t r) {
+ return iq2 + r % p.gqa_ratio;
+}
+bool row_valid(const in uint32_t r) {
+ return r < N && row_tok(r) < p.ne2;
+}
+
// Resolve a linear KV slot to a real column; false for inactive (sparse padding/-1, or dense OOB).
bool fa_kv_index(uint lin, out uint kv_col) {
if (USE_SPARSE) {
@@ -256,12 +269,13 @@ 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.
+// Flattened query and Q head of tile row r. With GQA collapse rows are heads, with GQA_TOK > 1 they are
+// GQA_TOK tokens x gqa_ratio heads (row_tok / row_head). 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 + (GQA_TOK > 1 ? row_tok(r) : gqa_iq1);
+ head = GQA_TOK > 1 ? row_head(r) : iq2 + r;
+ return GQA_TOK > 1 ? row_valid(r) : r < N;
}
query = iq3 * p.nem1 + i * Br + r;
head = iq2;
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 85ca98d36..14e4409ee 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp
@@ -161,7 +161,13 @@ void main() {
uint32_t r = (idx + tid) / (HSK / 4);
if (r < Br && d < HSK / 4 &&
i * Br + r < N) {
- Qf[r * qstride + d] = FLOAT_TYPEV4(data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale);
+ if (GQA_TOK > 1) {
+ if (row_valid(r)) {
+ Qf[r * qstride + d] = FLOAT_TYPEV4(data_qv4[(row_tok(r) * p.nb01 + (row_head(r) * p.nb02 + iq3 * p.nb03) / 4) / 4 + d] * p.scale);
+ }
+ } else {
+ Qf[r * qstride + d] = FLOAT_TYPEV4(data_qv4[q_offset / 4 + (i * Br + r) * q_stride / 4 + d] * p.scale);
+ }
}
}
barrier();
@@ -202,12 +208,20 @@ void main() {
// FaBlockBytesK/V == 2 for f16 (sizeof f16) and == 16 for f32 (vec4) and == ggml block size for quants.
uint32_t k_offset = (ik2*p.nb12 + ik3*p.nb13) / FaBlockBytesK;
uint32_t v_offset = (iv2*p.nb22 + iv3*p.nb23) / FaBlockBytesV;
- uint32_t m_offset = gqa_iq1*KV;
+ uint32_t m_offset = (GQA_TOK > 1 ? 0u : gqa_iq1)*KV;
if (p.nem2 != 1 || p.nem3 != 1) {
m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV;
mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride;
}
+ // GQA_TOK > 1: mask row of each row in this thread's 4-row mask group (the group is tid % (Br / 4) for every idx)
+ uint32_t fold_m_offset[4];
+ if (GQA_TOK > 1) {
+ [[unroll]] for (uint32_t k = 0; k < 4; ++k) {
+ fold_m_offset[k] = m_offset + min(row_tok((tid % (Br / 4)) * 4 + k), p.nem1 - 1) * KV;
+ }
+ }
+
uint32_t mask_opt = 0;
uint32_t mask_opt_idx = ~0;
uint32_t mask_opt_bits = 0;
@@ -241,8 +255,8 @@ 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 (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
+ if (USE_BIAS && kv_active && p.gqa_ratio > 1 && GQA_TOK == 1 && mask_opt_bits != MASK_OPT_ALL_ZERO) {
+ // GQA, one token per tile: 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]);
@@ -261,6 +275,12 @@ void main() {
if (USE_BIAS && mask_opt_bits == MASK_OPT_ALL_ZERO) {
m = f16vec4(0.0);
max_mask = 0.0;
+ } else if (GQA_TOK > 1) {
+ m = f16vec4(data_m[fold_m_offset[0] + (j * Bc + c)],
+ data_m[fold_m_offset[1] + (j * Bc + c)],
+ data_m[fold_m_offset[2] + (j * Bc + c)],
+ data_m[fold_m_offset[3] + (j * Bc + c)]);
+ max_mask = max(max(max(max(max_mask, float(m[0])), float(m[1])), float(m[2])), float(m[3]));
} 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]);
@@ -673,7 +693,7 @@ void main() {
// If there is split_k, then the split_k resolve shader does the final
// division by L. Store the intermediate O value and per-row m and L values.
if (p.k_num > 1) {
- if (p.gqa_ratio > 1) {
+ if (p.gqa_ratio > 1 && GQA_TOK == 1) {
// note: O and Q have swapped coord 1,2.
uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3)) / 4;
@@ -698,22 +718,24 @@ void main() {
} else {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
const uint row = tile_row(r);
- const uint global_row = i * Br + row;
+ const uint global_row = GQA_TOK > 1 ? row_tok(row) : i * Br + row;
+ const uint head = GQA_TOK > 1 ? row_head(row) : iq2;
+ const bool row_ok = GQA_TOK > 1 ? row_valid(row) : global_row < N;
- if (global_row < N) {
+ if (row_ok) {
uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3)) / 4;
[[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) {
const uint d = d0 + col_tid;
if (d >= HSV/4) break;
- data_ov4[o_offset + iq2 * HSV/4 + d] = D_TYPEV4(Of[r][d/threads_per_rowgroup]);
+ data_ov4[o_offset + head * HSV/4 + d] = D_TYPEV4(Of[r][d/threads_per_rowgroup]);
}
}
- if (global_row < N && col_tid == 0) {
+ if (row_ok && col_tid == 0) {
uint32_t lm_offset = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3 + p.ne1 * 2 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3));
- data_o[lm_offset + iq2] = D_TYPE(Lf[r]);
- data_o[lm_offset + p.ne1 + iq2] = D_TYPE(Mf[r]);
+ data_o[lm_offset + head] = D_TYPE(Lf[r]);
+ data_o[lm_offset + p.ne1 + head] = D_TYPE(Mf[r]);
}
}
}
@@ -758,9 +780,9 @@ void main() {
}
}
- uint32_t o_offset = (gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4;
+ uint32_t o_offset = ((GQA_TOK > 1 ? 0u : gqa_iq1)*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV) / 4;
- if (p.gqa_ratio > 1) {
+ if (p.gqa_ratio > 1 && GQA_TOK == 1) {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
if (tile_row(r) < N) {
[[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) {
@@ -773,12 +795,15 @@ void main() {
}
} else {
[[unroll]] for (uint32_t r = 0; r < rows_per_thread; ++r) {
- if (i * Br + tile_row(r) < N) {
+ const uint row = tile_row(r);
+ const uint global_row = GQA_TOK > 1 ? row_tok(row) : i * Br + row;
+ const uint head = GQA_TOK > 1 ? row_head(row) : iq2;
+ if (GQA_TOK > 1 ? row_valid(row) : global_row < N) {
[[unroll]] for (uint32_t d0 = 0; d0 < HSV / 4; d0 += threads_per_rowgroup) {
const uint d = d0 + col_tid;
if (d >= HSV / 4) break;
const uint d_local = d0 / threads_per_rowgroup;
- data_ov4[o_offset + (iq2 * HSV + (i * Br + tile_row(r)) * p.ne1 * HSV) / 4 + d] = D_TYPEV4(Of[r][d_local]);
+ data_ov4[o_offset + (head * HSV + global_row * p.ne1 * HSV) / 4 + d] = D_TYPEV4(Of[r][d_local]);
}
}
}
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 cfc967000..692ccdc4b 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp
@@ -114,6 +114,7 @@ f16vec4 faDecodeVVector(const decodeBufFA_V bl_in, const uint blockCoords[2], co
#endif
layout (binding = 0) readonly buffer Q {uint8_t data_q[];};
+layout (binding = 0) readonly buffer QF32 {float data_qf32[];};
layout (binding = 1) readonly buffer K {uint8_t data_k[];};
layout (binding = 2) readonly buffer V {uint8_t data_v[];};
layout (binding = 3) readonly buffer M {uint8_t data_m[];};
@@ -276,35 +277,54 @@ ACC_TYPE Max(const in uint32_t row, const in uint32_t col, const in ACC_TYPE ele
return max(elem0, elem1);
}
+Q_TYPE perElemOpFoldedLoadQ(const in uint32_t r, const in uint32_t c, const in Q_TYPE elem) {
+ if (row_valid(r) && c < HSK) {
+ return Q_TYPE(data_qf32[row_tok(r) * p.nb01 + (row_head(r) * p.nb02 + iq3 * p.nb03) / 4 + c]);
+ }
+ return Q_TYPE(0.0);
+}
+
+float16_t perElemOpFoldedLoadMask(const in uint32_t r, const in uint32_t c, const in float16_t elem, const in uint32_t j) {
+ const uint32_t col = j * Bc + c;
+ if (row_valid(r) && col < KV) {
+ return data_mf16[g_m_off_elem + min(row_tok(r), p.nem1 - 1) * KV + col];
+ }
+ return float16_t(uintBitsToFloat(0xFF800000));
+}
+
// Store the output when doing grouped query attention.
-// Rows index by Q's dimension 2, and the first N rows are valid.
+// Folded rows index both the query token and its GQA head.
D_TYPE perElemOpGqaStore(const in uint32_t r, const in uint32_t c, const in D_TYPE elem, const in uint32_t o_offset, const in uint32_t iq2, const in uint32_t N)
{
- if (r < N && c < HSV) {
- uint32_t offset = (iq2 + r) * HSV + c;
+ if ((GQA_TOK > 1 ? row_valid(r) : r < N) && c < HSV) {
+ const uint32_t head = GQA_TOK > 1 ? row_head(r) : iq2 + r;
+ const uint32_t token_offset = GQA_TOK > 1 ? row_tok(r) * p.ne1 * HSV : 0u;
+ uint32_t offset = token_offset + head * HSV + c;
data_o[o_offset + offset] = D_TYPE(elem);
}
return elem;
}
-// Store O values for non-GQA split_k. Rows are tokens, not heads.
+// Store O values for non-GQA or folded split_k.
D_TYPE perElemOpNonGqaSplitKStore(const in uint32_t r, const in uint32_t c, const in D_TYPE elem, const in uint32_t unused, const in uint32_t iq2, const in uint32_t N) {
- uint32_t global_row = i * Br + r;
- if (global_row < N && c < HSV) {
+ uint32_t global_row = GQA_TOK > 1 ? row_tok(r) : i * Br + r;
+ const uint32_t head = GQA_TOK > 1 ? row_head(r) : iq2;
+ if ((GQA_TOK > 1 ? row_valid(r) : global_row < N) && c < HSV) {
uint32_t o_off = HSV * p.ne1
* (split_k_index + p.k_num * (global_row + p.ne2 * iq3));
- data_o[o_off + iq2 * HSV + c] = D_TYPE(elem);
+ data_o[o_off + head * HSV + c] = D_TYPE(elem);
}
return elem;
}
-// Store L/M values for non-GQA split_k.
+// Store L/M values for non-GQA or folded split_k.
ACC_TYPE perElemOpNonGqaSplitKStoreCol0(const in uint32_t r, const in uint32_t c, const in ACC_TYPE elem, const in uint32_t lm_base, const in uint32_t iq2, const in uint32_t N) {
- uint32_t global_row = i * Br + r;
- if (global_row < N && c == 0) {
+ uint32_t global_row = GQA_TOK > 1 ? row_tok(r) : i * Br + r;
+ const uint32_t head = GQA_TOK > 1 ? row_head(r) : iq2;
+ if ((GQA_TOK > 1 ? row_valid(r) : global_row < N) && c == 0) {
uint32_t lm_off = HSV * p.ne1 * p.k_num * p.ne2 * p.ne3
+ p.ne1 * 2 * (split_k_index + p.k_num * (global_row + p.ne2 * iq3));
- data_o[lm_off + lm_base + iq2] = D_TYPE(elem);
+ data_o[lm_off + lm_base + head] = D_TYPE(elem);
}
return elem;
}
@@ -355,7 +375,12 @@ void main() {
coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseA> Qf16;
uint32_t q_offset = gqa_iq1*p.nb01*4/*sizeof(float)*/ + iq2*p.nb02+iq3*p.nb03;
- coopMatLoadTensorNV(Q, data_q, q_offset, sliceTensorLayoutNV(tensorLayoutQ, i * Br, Br, 0, HSK_pad));
+ if (GQA_TOK > 1) {
+ Q = coopmat<Q_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseAccumulator>(0.0);
+ coopMatPerElementNV(Q, Q, perElemOpFoldedLoadQ);
+ } else {
+ coopMatLoadTensorNV(Q, data_q, q_offset, sliceTensorLayoutNV(tensorLayoutQ, i * Br, Br, 0, HSK_pad));
+ }
Q *= Q_TYPE(p.scale);
Qf16 = coopmat<FLOAT_TYPE, gl_ScopeWorkgroup, Br, HSK_pad, gl_MatrixUseA>(Q);
@@ -383,7 +408,7 @@ void main() {
// mo_offset will point to the tile starting at row i*Br and col 0
uint32_t mo_offset = mo_stride * i;
- uint32_t m_offset = gqa_iq1*KV * 2 /*sizeof(float16_t)*/;
+ uint32_t m_offset = (GQA_TOK > 1 ? 0u : gqa_iq1)*KV * 2 /*sizeof(float16_t)*/;
if (p.nem2 != 1 || p.nem3 != 1) {
m_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * p.nem1 * KV * 2 /*sizeof(float16_t)*/;
mo_offset += ((iq3 % p.nem3) * p.nem2 + (iq2 % p.nem2)) * CEIL_DIV(p.nem1, Br) * mo_stride;
@@ -415,7 +440,14 @@ void main() {
if (mask_opt_bits != MASK_OPT_ALL_ZERO) {
bool nem1_bounds_check = !(p.gqa_ratio > 1) && (p.nem1 % Br) != 0;
- if (nem1_bounds_check) {
+ if (GQA_TOK > 1) {
+ coopMatPerElementNV(mv, mv, perElemOpFoldedLoadMask, j);
+ coopmat<float16_t, gl_ScopeWorkgroup, Br, Bc, gl_MatrixUseAccumulator> mvmax;
+ coopMatReduceNV(mvmax, mv, gl_CooperativeMatrixReduceRowAndColumnNV, maxReduceFp16);
+ if (mvmax[0] <= NEG_FLT_MAX_OVER_2) {
+ continue;
+ }
+ } else if (nem1_bounds_check) {
tensorLayoutNV<2, gl_CooperativeMatrixClampModeConstantNV> tensorLayoutM = createTensorLayoutNV(2, gl_CooperativeMatrixClampModeConstantNV);
tensorLayoutM = setTensorLayoutDimensionNV(tensorLayoutM, p.nem1, KV);
tensorLayoutM = setTensorLayoutStrideNV(tensorLayoutM, m_stride, 1);
@@ -572,7 +604,7 @@ void main() {
if (p.k_num > 1) {
coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator> O_D = coopmat<D_TYPE, gl_ScopeWorkgroup, Br, HSV_pad, gl_MatrixUseAccumulator>(O);
- if (p.gqa_ratio > 1) {
+ if (p.gqa_ratio > 1 && GQA_TOK == 1) {
// note: O and Q have swapped coord 1,2.
uint32_t o_offset = HSV * p.ne1 * (split_k_index + p.k_num * (gqa_iq1 + p.ne2 * iq3));
coopMatPerElementNV(O_D, O_D, perElemOpGqaStore, o_offset, iq2, N);
@@ -634,7 +666,7 @@ void main() {
[[unroll]] for (uint i = 0; i < O_D.length(); ++i) { O_D[i] = clamp(O_D[i], D_TYPE(-ACC_TYPE_MAX), D_TYPE(ACC_TYPE_MAX)); }
#endif
- uint32_t o_offset = gqa_iq1*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV;
+ uint32_t o_offset = (GQA_TOK > 1 ? 0u : gqa_iq1)*p.ne1*HSV + iq3*p.ne2*p.ne1*HSV;
if (p.gqa_ratio > 1) {
coopMatPerElementNV(O_D, O_D, perElemOpGqaStore, o_offset, iq2, N);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index ec61bb60b..18350feff 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -494,6 +494,8 @@ static std::string var_to_str(ggml_scale_mode mode) {
#define VARS_TO_STR16(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) VAR_TO_STR(a) + "," + VARS_TO_STR15(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
#define VARS_TO_STR17(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) VAR_TO_STR(a) + "," + VARS_TO_STR16(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)
#define VARS_TO_STR18(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r) VAR_TO_STR(a) + "," + VARS_TO_STR17(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)
+#define VARS_TO_STR19(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s) VAR_TO_STR(a) + "," + VARS_TO_STR18(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)
+#define VARS_TO_STR20(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t) VAR_TO_STR(a) + "," + VARS_TO_STR19(b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)
// accept FLT_MAX as infinity
static bool isinf_or_max(float f) {
@@ -10266,9 +10268,10 @@ struct test_flash_attn_ext : public test_case {
const int64_t n_kv_max;
const int64_t q_head_padding;
const int64_t mask_heads;
+ const bool mask_padded;
std::string vars() override {
- return VARS_TO_STR18(hsk, hsv, nh, nr23, kv, nb, mask, sinks, max_bias, logit_softcap, prec, type_K, type_V, permute, kv_view, v_is_view_of_k, n_kv_max, q_head_padding) + ",mask_heads=" + std::to_string(mask_heads);
+ return VARS_TO_STR20(hsk, hsv, nh, nr23, kv, nb, mask, sinks, max_bias, logit_softcap, prec, type_K, type_V, permute, kv_view, v_is_view_of_k, n_kv_max, q_head_padding, mask_heads, mask_padded);
}
double max_nmse_err() override {
@@ -10285,9 +10288,9 @@ struct test_flash_attn_ext : public test_case {
test_flash_attn_ext(int64_t hsk = 128, int64_t hsv = 128, int64_t nh = 32, std::array<int64_t, 2> nr23 = {1, 1}, int64_t kv = 96, int64_t nb = 8,
bool mask = true, bool sinks = false, float max_bias = 0.0f, float logit_softcap = 0.0f, ggml_prec prec = GGML_PREC_F32,
ggml_type type_K = GGML_TYPE_F16, ggml_type type_V = GGML_TYPE_F16, std::array<int32_t, 4> permute = {0, 1, 2, 3},
- bool kv_view = true, bool v_is_view_of_k = false, int64_t n_kv_max = 0, int64_t q_head_padding = 0, int64_t mask_heads = 1)
+ bool kv_view = true, bool v_is_view_of_k = false, int64_t n_kv_max = 0, int64_t q_head_padding = 0, int64_t mask_heads = 1, bool mask_padded = false)
: hsk(hsk), hsv(hsv), nh(nh), nr23(nr23), kv(kv), nb(nb), mask(mask), sinks(sinks), max_bias(max_bias), logit_softcap(logit_softcap), prec(prec),
- type_K(type_K), type_V(type_V), permute(permute), kv_view(kv_view), v_is_view_of_k(v_is_view_of_k), n_kv_max(n_kv_max), q_head_padding(q_head_padding), mask_heads(mask_heads) {}
+ type_K(type_K), type_V(type_V), permute(permute), kv_view(kv_view), v_is_view_of_k(v_is_view_of_k), n_kv_max(n_kv_max), q_head_padding(q_head_padding), mask_heads(mask_heads), mask_padded(mask_padded) {}
ggml_tensor * build_graph(ggml_context * ctx) override {
const int64_t hsk_padded = GGML_PAD(hsk, ggml_blck_size(type_K));
@@ -10339,7 +10342,7 @@ struct test_flash_attn_ext : public test_case {
ggml_tensor * m = nullptr;
if (mask) {
- m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, nb, mask_heads, nr23[1]);
+ m = ggml_new_tensor_4d(ctx, GGML_TYPE_F16, kv, mask_padded ? GGML_PAD(nb, 32) : nb, mask_heads, nr23[1]);
ggml_set_name(m, "m");
}
@@ -15114,6 +15117,25 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {2, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {4, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ // GQA with 2-8 query tokens (speculative decoding verify); Vulkan puts several tokens in one GQA row tile
+ for (int nr2 : { 2, 3, 4, 5, 8 }) {
+ for (int nb : { 5, 8 }) {
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {nr2, 1}, 1024, nb, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ }
+ }
+ // folded tiles without a mask, with ALiBi, with sinks
+ test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 4, false, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {6, 1}, 4096, 4, true, false, 8.0f, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {3, 1}, 4096, 5, true, true, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ // many KV heads: split_k is 0 or 1 on small GPUs, so folded rows take the direct store
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 80, {4, 1}, 512, 8, true, true, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+
+ // Folded CM2 rows: large GQA groups, padded masks, permuted Q, and padded head dimensions.
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {16, 1}, 4096, 5, true, true, 8.0f, 10.0f, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 64, {16, 1}, 512, 5, true, true, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(72, 64, 4, {3, 1}, 113, 7, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {4, 2}, 4096, 5, true, false, 0, 0, GGML_PREC_UNDEFINED, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, {4, 1}, 4096, 5, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, { 3, 1}, 512, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
// dense-allocated (non-view) quant K/V at batch >= 64, in cache and native layouts
@@ -15984,6 +16006,23 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
}
}
+ // speculative decoding verify: a few query tokens against a long KV cache
+ for (int hs : { 128, 256 }) {
+ for (int nr2 : { 2, 4, 6, 8 }) {
+ for (int nb : { 2, 5, 8 }) {
+ test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {nr2, 1}, 32768, nb, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
+ }
+ }
+ }
+
+ // Padded masks also exercise mask-opt with folded CM2 rows.
+ for (int hs : {128, 256}) {
+ test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {3, 1}, 32768, 7, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {16, 1}, 32768, 5, true, false, 0, 0, GGML_PREC_UNDEFINED, GGML_TYPE_F16, GGML_TYPE_F16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {4, 1}, 32768, 5, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 4, {4, 1}, 32768, 5, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_BF16, GGML_TYPE_BF16, {0, 2, 1, 3}, true, false, 0, 0, 1, true));
+ }
+
for (int col : {8192, 16384, 32768, 65536, 131072, 262144, 524288}) {
for (int rows : {1, 4, 16}){
test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {col, rows, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f));