Commit a4e5552db for llama.cpp
commit a4e5552dbbb8128d5a3335b7dafba40a45a24eac
Author: Georgi Gerganov <ggerganov@gmail.com>
Date: Fri Dec 4 11:18:34 2026 +0200
metal : support bf16 K/V and fix the FA kv_rows partial chunks (#33608)
* metal : accumulate the bf16 flash attention O in bfloat
The bf16 prefill kernel kept the O accumulator in half while Q/K/V are
bfloat, which mixed the accumulator type with the operands. Accumulate
in bfloat and rescale the O matrix in float, since Metal does not allow
bfloat4 *= float.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : fix flash attention kv_rows with partial K/V chunks
The kv_rows path assumed the row lists are padded to the kernel chunk
(256/64/32) and asserted it, so a partial last chunk was mishandled:
- the pad kernel copied cache columns instead of the gathered rows, and
the pad buffer was sized/indexed per cache stream (ne13) instead of
per q slice (ne03), so slices >= 1 read the wrong region
- the vec path did not gather the partial chunk through the row list and
read V at the wrong pad offset, dropping the last chunk
- direct row reads applied the cache stream stride to a shared cache
Gather the partial chunk through the row list, size the pad per slice,
and zero the stream stride for direct row reads. Also allow BF16 K/V with
kv_rows: the non-vec gather converts to F16 and the tensor path uses the
f16 kernel over the gathered rows.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use bfloat shared memory for the bf16 vec FA kernels
The bf16 vec instantiations used the half shared-memory type set, so
K/V were converted bfloat->half on load. Add FA_TYPES_BF with bfloat
Q/K/V in shared memory so the vec kernels read bfloat directly through
the direct K/V path, and convert Q from f32 to bfloat as the prefill
bf16 kernel does.
Add the tuned _q%d_ne%d bf16 variants to match the f16/quantized sets,
since the fa_vec tuning selects them, and use a float4 broadcast for the
zero-fill casts that bfloat4 does not accept. Add bf16 to the FA vec
slice test types to cover the new path.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : gather the kv_rows K/V in their own type
The non-vec kv_rows path always gathered the referenced rows into an F16
buffer, so a BF16 cache was converted to F16 and the attention ran the
f16 kernels. Replace the use_kv_f16 flag with the type of the buffer that
the attention reads: quantized K/V is still dequantized to F16, but the
cache type is otherwise kept, so BF16 stays BF16 through the gather and
the partial-chunk pad.
The gather kernel is now same-type (template on T, no cast), the gather
and pad sizes/strides use ggml_type_size(kv_type), and the tensor,
half8x8 and vec pipeline selectors take kv_type instead of the boolean.
Add BF16 gather tests for the half8x8 path with and without the pad.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : use clearer names for the FA kernel paths
Replace the historical half8x8/half4x4 labels with the 8x8 simdgroup
kernel and the vec kernel, matching the actual kernel families. Comment
only, no functional change.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : trim redundant flash attention comments
Update the kv_rows support check that still claimed a contiguous F16 copy
with BF16 converted to F16, drop the duplicated kv_type, pad-per-slice,
dense-op and column-order explanations, and keep only the comments that
explain non-obvious behavior. Comment only, no functional change.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* metal : simplify the flash attention K/V type helpers
Make kv_type the single source of truth for the type the flash attention
kernels read, and derive dequant_kv from it (quantized cache and kv_type
F16). Remove use_kv_f16 - its heuristic moves into kv_type - and use
kv_type in the tensor path check and the is_q flag. This also enables the
tensor path for a quantized rows gather whose shape fills the GPU.
Remove the stale commented-out use_kv_f16/use_vec lines as well.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index 7f3cb70b0..6af80a175 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -1839,6 +1839,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_p
ggml_metal_library_t lib,
const struct ggml_tensor * op,
bool has_mask,
+ bool has_rows,
int32_t ncpsg) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
GGML_UNUSED(op);
@@ -1849,9 +1850,10 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_p
snprintf(base, 256, "kernel_%s",
"flash_attn_ext_pad");
- snprintf(name, 256, "%s_mask=%d_ncpsg=%d",
+ snprintf(name, 256, "%s_mask=%d_rows=%d_ncpsg=%d",
base,
has_mask,
+ has_rows,
ncpsg);
ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
@@ -1859,6 +1861,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_p
ggml_metal_cv_t cv = ggml_metal_cv_init();
ggml_metal_cv_set_bool(cv, has_mask, FC_FLASH_ATTN_EXT_PAD + 0);
+ ggml_metal_cv_set_bool(cv, has_rows, FC_FLASH_ATTN_EXT_PAD + 1);
//ggml_metal_cv_set_bool(cv, has_sinks, FC_FLASH_ATTN_EXT_PAD + 1);
//ggml_metal_cv_set_bool(cv, has_bias, FC_FLASH_ATTN_EXT_PAD + 2);
//ggml_metal_cv_set_bool(cv, has_scap, FC_FLASH_ATTN_EXT_PAD + 3);
@@ -1944,7 +1947,8 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_t
bool has_mask,
bool has_sinks,
bool has_bias,
- bool has_scap) {
+ bool has_scap,
+ enum ggml_type kv_type) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
char base[256];
@@ -1953,7 +1957,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_t
const int32_t dk = (int32_t) op->src[1]->ne[0];
const int32_t dv = (int32_t) op->src[2]->ne[0];
- const char * type = op->src[1]->type == GGML_TYPE_BF16 ? "bf16" : "f16";
+ const char * type = ggml_type_name(kv_type);
snprintf(base, 256, "kernel_flash_attn_ext_tensor_%s_dk%d_dv%d", type, dk, dv);
snprintf(name, 256, "%s_mask=%d_sinks=%d_bias=%d_scap=%d", base, has_mask, has_sinks, has_bias, has_scap);
@@ -1984,7 +1988,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
bool has_scap,
bool has_kvpad,
int32_t nsg,
- bool use_kv_f16,
+ enum ggml_type kv_type,
int32_t ns10,
int32_t ns20) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
@@ -1995,7 +1999,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
const int32_t dk = (int32_t) op->src[1]->ne[0];
const int32_t dv = (int32_t) op->src[2]->ne[0];
- const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type);
+ const char * type = ggml_type_name(kv_type);
// do bounds checks for the mask?
const bool bc_mask = op->src[3] && (op->src[3]->ne[1] % 8 != 0);
@@ -2044,18 +2048,23 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_rows(
ggml_metal_library_t lib,
- const ggml_tensor * op) {
+ const ggml_tensor * op,
+ enum ggml_type kv_type) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
- const char * base = "kernel_flash_attn_ext_rows";
+ char base[256];
+
+ const char * type = ggml_type_name(kv_type);
+
+ snprintf(base, 256, "kernel_flash_attn_ext_rows_%s", type);
+
+ GGML_UNUSED(op);
ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, base);
if (!res.pipeline) {
res = ggml_metal_library_compile_pipeline(lib, base, base, nullptr);
}
- GGML_UNUSED(op);
-
return res;
}
@@ -2093,7 +2102,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_v
int32_t ne,
int32_t nsg,
int32_t nwg,
- bool use_kv_f16,
+ enum ggml_type kv_type,
int32_t ns10,
int32_t ns20) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
@@ -2104,7 +2113,7 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_v
const int32_t dk = (int32_t) op->src[1]->ne[0];
const int32_t dv = (int32_t) op->src[2]->ne[0];
- const char * type = use_kv_f16 ? "f16" : ggml_type_name(op->src[1]->type);
+ const char * type = ggml_type_name(kv_type);
char qne_suffix[16] = {0};
if (!(nqpsg == 1 && ne == ggml_metal_tuning::fa_vec_baseline_ne(dk, dv))) {
diff --git a/ggml/src/ggml-metal/ggml-metal-device.h b/ggml/src/ggml-metal/ggml-metal-device.h
index c370ce9a7..32ac36ee1 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.h
+++ b/ggml/src/ggml-metal/ggml-metal-device.h
@@ -189,6 +189,7 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_att
ggml_metal_library_t lib,
const struct ggml_tensor * op,
bool has_mask,
+ bool has_rows,
int32_t ncpsg);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_kv_f16(
@@ -207,7 +208,8 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_att
bool has_mask,
bool has_sinks,
bool has_bias,
- bool has_scap);
+ bool has_scap,
+ enum ggml_type kv_type);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext(
ggml_metal_library_t lib,
@@ -218,13 +220,14 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_att
bool has_scap,
bool has_kvpad,
int32_t nsg,
- bool use_kv_f16,
+ enum ggml_type kv_type,
int32_t ns10,
int32_t ns20);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_rows(
ggml_metal_library_t lib,
- const struct ggml_tensor * op);
+ const struct ggml_tensor * op,
+ enum ggml_type kv_type);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_attn_ext_vec_idx(
ggml_metal_library_t lib,
@@ -244,7 +247,7 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_flash_att
int32_t ne,
int32_t nsg,
int32_t nwg,
- bool use_kv_f16,
+ enum ggml_type kv_type,
int32_t ns10,
int32_t ns20);
diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m
index 50e8fd938..3db087e4a 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.m
+++ b/ggml/src/ggml-metal/ggml-metal-device.m
@@ -1779,8 +1779,8 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
return false;
}
- // the non-vec kernels attend over a contiguous F16 copy of the referenced rows:
- // - F16 K/V is gathered directly
+ // the non-vec kernels attend over a contiguous copy of the referenced rows:
+ // - F16/BF16 K/V is gathered directly in the K/V cache type
// - quantized K/V is first dequantized with kernel_flash_attn_ext_kv_*_f16
// - other types keep attending to the whole cache (dense mask)
switch (op->src[1]->type) {
@@ -1791,11 +1791,16 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
case GGML_TYPE_Q5_1:
case GGML_TYPE_Q8_0:
break;
+ case GGML_TYPE_BF16:
+ if (!has_bfloat) {
+ return false;
+ }
+ break;
default:
return false;
}
- // the row gather copies F16 values in groups of 4
+ // the row gather copies 4 values at a time
if (op->src[2]->ne[0] % 4 != 0) {
return false;
}
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index 8856ef254..ed4a52f9b 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -3325,31 +3325,7 @@ bool ggml_metal_op_flash_attn_ext_use_vec(const ggml_tensor * op) {
return (ne01 < 20) && (ne00 % 32 == 0);
}
-// ref: https://github.com/ggml-org/llama.cpp/pull/27390
-// dequantize the quantized KV cache to F16 before running the F16 flash attention kernels
-static bool ggml_metal_op_flash_attn_ext_use_kv_f16(const ggml_tensor * op) {
- assert(op->op == GGML_OP_FLASH_ATTN_EXT);
-
- // depending on compute/bandwidth ratio, dequant to f16 kv is not always beneficial
- // ref: https://github.com/ggml-org/llama.cpp/pull/27390#issuecomment-5355152767
- // TODO: tune per device
- // large heads need the upfront dequant to fit the non-vec threadgroup memory
- if (op->src[0]->ne[1] < 32 &&
- (op->src[0]->ne[0] < 512 || ggml_metal_op_flash_attn_ext_use_vec(op))) {
- return false;
- }
-
- switch (op->src[1]->type) {
- case GGML_TYPE_Q4_0:
- case GGML_TYPE_Q4_1:
- case GGML_TYPE_Q5_0:
- case GGML_TYPE_Q5_1:
- case GGML_TYPE_Q8_0:
- return true;
- default:
- return false;
- }
-}
+static ggml_type ggml_metal_op_flash_attn_ext_kv_type(const ggml_tensor * op);
static bool ggml_metal_op_flash_attn_ext_use_tensor(const ggml_tensor * op, bool has_tensor) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
@@ -3379,13 +3355,14 @@ static bool ggml_metal_op_flash_attn_ext_use_tensor(const ggml_tensor * op, bool
// large heads use fewer queries per threadgroup, so that the queries fit in threadgroup memory
const int64_t nqptg = dk >= 512 ? OP_FLASH_ATTN_EXT_TENSOR_NQPSG_LARGE : OP_FLASH_ATTN_EXT_TENSOR_NQPSG;
- // few heads and small batches do not fill the GPU - the half8x8 kernel is faster there
+ // few heads and small batches do not fill the GPU - the 8x8 simdgroup kernel is faster there
// TODO: tune per device
if (((ne01 + nqptg - 1)/nqptg)*ne02*ne03*dk < 8192) {
return false;
}
- if (op->src[1]->type != GGML_TYPE_F16 && op->src[1]->type != GGML_TYPE_BF16 && !ggml_metal_op_flash_attn_ext_use_kv_f16(op)) {
+ const ggml_type kv_type = ggml_metal_op_flash_attn_ext_kv_type(op);
+ if (kv_type != GGML_TYPE_F16 && kv_type != GGML_TYPE_BF16) {
return false;
}
@@ -3473,6 +3450,56 @@ static bool ggml_metal_op_flash_attn_ext_v_is_view_of_k(const ggml_tensor * op)
return V->view_src && (V->view_src == K || (V->view_src == K->view_src && V->view_offs == K->view_offs));
}
+// the type of the K/V buffer that the flash attention kernels read
+static ggml_type ggml_metal_op_flash_attn_ext_kv_type(const ggml_tensor * op) {
+ assert(op->op == GGML_OP_FLASH_ATTN_EXT);
+
+ const ggml_type type = op->src[1]->type;
+
+ if (!ggml_is_quantized(type)) {
+ return type;
+ }
+
+ const bool has_rows = op->src[5] != nullptr;
+ const bool rows_gather = has_rows && !ggml_metal_op_flash_attn_ext_use_vec(op);
+
+ if (rows_gather) {
+ return GGML_TYPE_F16;
+ }
+
+ // the sparse path dequantizes the KV inline in the vec kernel
+ if (ggml_metal_op_flash_attn_ext_n_kv_max_sparse(op) != 0) {
+ return type;
+ }
+
+ // depending on compute/bandwidth ratio, dequant to f16 kv is not always beneficial
+ // ref: https://github.com/ggml-org/llama.cpp/pull/27390#issuecomment-5355152767
+ // TODO: tune per device
+ // large heads need the upfront dequant to fit the non-vec threadgroup memory
+ if (op->src[0]->ne[1] < 32 &&
+ (op->src[0]->ne[0] < 512 || ggml_metal_op_flash_attn_ext_use_vec(op))) {
+ return type;
+ }
+
+ switch (type) {
+ case GGML_TYPE_Q4_0:
+ case GGML_TYPE_Q4_1:
+ case GGML_TYPE_Q5_0:
+ case GGML_TYPE_Q5_1:
+ case GGML_TYPE_Q8_0:
+ return GGML_TYPE_F16;
+ default:
+ return type;
+ }
+}
+
+static bool ggml_metal_op_flash_attn_ext_dequant_kv(const ggml_tensor * op) {
+ assert(op->op == GGML_OP_FLASH_ATTN_EXT);
+
+ return ggml_is_quantized(op->src[1]->type) &&
+ ggml_metal_op_flash_attn_ext_kv_type(op) == GGML_TYPE_F16;
+}
+
// size of the F16 dequantized K tensor; the dequantized V tensor follows it in the same scratch buffer
static size_t ggml_metal_op_flash_attn_ext_kv_f16_k_size(const ggml_tensor * op) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
@@ -3497,19 +3524,25 @@ size_t ggml_metal_op_flash_attn_ext_extra_pad(const ggml_tensor * op) {
size_t res = 0;
const bool has_mask = op->src[3] != nullptr;
- const bool use_kv_f16 = ggml_metal_op_flash_attn_ext_use_kv_f16(op);
- // when the KV is dequantized to F16, the pad kernel copies the tail chunk from the F16 scratch buffer
- // note: when V is a view of K, the dequantized V is read from the dequantized K with K's row stride
- const bool v_is_view_of_k = use_kv_f16 && ggml_metal_op_flash_attn_ext_v_is_view_of_k(op);
+ // the KV strides below must match the ones used by ggml_metal_op_flash_attn_ext
+ const bool has_rows = op->src[5] != nullptr;
+ const bool rows_gather = has_rows && !ggml_metal_op_flash_attn_ext_use_vec(op);
+ const bool dequant_kv = ggml_metal_op_flash_attn_ext_dequant_kv(op);
+ const ggml_type kv_type = dequant_kv ? GGML_TYPE_F16 : op->src[1]->type;
+
+ // note: when V is a view of K, the compact V is read from the compact K with K's row stride
+ const bool v_is_view_of_k = (rows_gather || dequant_kv) && ggml_metal_op_flash_attn_ext_v_is_view_of_k(op);
uint64_t nb11_pad = nb11;
uint64_t nb21_pad = nb21;
- if (use_kv_f16) {
- nb11_pad = sizeof(ggml_fp16_t)*ne10;
- nb21_pad = sizeof(ggml_fp16_t)*(v_is_view_of_k ? ne10 : ne20);
+ if (rows_gather || dequant_kv) {
+ nb11_pad = ggml_type_size(kv_type)*ne10;
+ nb21_pad = ggml_type_size(kv_type)*(v_is_view_of_k ? ne10 : ne20);
}
+ const int64_t n_kv_pad = has_rows ? ne03 : ne13;
+
// note: the non-vec kernel requires more extra memory, so always reserve for it
GGML_ASSERT(OP_FLASH_ATTN_EXT_NCPSG >= OP_FLASH_ATTN_EXT_VEC_NCPSG);
@@ -3521,8 +3554,8 @@ size_t ggml_metal_op_flash_attn_ext_extra_pad(const ggml_tensor * op) {
if (has_kvpad) {
res += OP_FLASH_ATTN_EXT_VEC_NCPSG*(
- nb11_pad*ne12*ne13 +
- nb21_pad*ne22*ne23 +
+ nb11_pad*ne12*n_kv_pad +
+ nb21_pad*ne22*n_kv_pad +
(has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0));
}
} else {
@@ -3531,8 +3564,8 @@ size_t ggml_metal_op_flash_attn_ext_extra_pad(const ggml_tensor * op) {
if (has_kvpad) {
res += OP_FLASH_ATTN_EXT_NCPSG*(
- nb11_pad*ne12*ne13 +
- nb21_pad*ne22*ne23 +
+ nb11_pad*ne12*n_kv_pad +
+ nb21_pad*ne22*n_kv_pad +
(has_mask ? ggml_type_size(GGML_TYPE_F16)*ne31*ne32*ne33 : 0));
}
}
@@ -3594,7 +3627,6 @@ size_t ggml_metal_op_flash_attn_ext_extra_tmp(const ggml_tensor * op) {
size_t res = 0;
// note: always reserve the temp buffer to avoid graph reallocations
- //if (ggml_metal_op_flash_attn_ext_use_vec(op)) {
if (true) {
const int64_t nwg = 32;
const int64_t ne01_max = std::min(ne01, 32);
@@ -3612,9 +3644,6 @@ size_t ggml_metal_op_flash_attn_ext_extra_kv_f16(const ggml_tensor * op) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
// note: always reserve the temp buffer to avoid graph reallocations
- //if (!ggml_metal_op_flash_attn_ext_use_kv_f16(op)) {
- // return 0;
- //}
GGML_TENSOR_LOCALS( int32_t, ne2, op->src[2], ne);
@@ -3649,8 +3678,7 @@ size_t ggml_metal_op_flash_attn_ext_extra_idx(const ggml_tensor * op) {
return GGML_PAD(sizeof(int32_t)*(size_t) n_kv_max_padded*ne31*ne32*ne33, 16);
}
-// size of the contiguous F16 buffers holding the K/V rows referenced by the kv_rows lists
-// the attention over these buffers is a standard dense op (no per-tile gather in the main kernel)
+// size of the contiguous buffers holding the K/V rows referenced by the kv_rows lists
size_t ggml_metal_op_flash_attn_ext_extra_rows(const ggml_tensor * op) {
assert(op->op == GGML_OP_FLASH_ATTN_EXT);
@@ -3669,8 +3697,10 @@ size_t ggml_metal_op_flash_attn_ext_extra_rows(const ggml_tensor * op) {
// note: when V is a view of K, the gathered V is read from the gathered K, so only K is gathered
const bool v_is_view_of_k = ggml_metal_op_flash_attn_ext_v_is_view_of_k(op);
- const size_t k_size = GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne10*n_rows*ne12*n_slices, 16);
- const size_t v_size = v_is_view_of_k ? 0 : GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne20*n_rows*ne22*n_slices, 16);
+ const ggml_type kv_type = ggml_metal_op_flash_attn_ext_kv_type(op);
+
+ const size_t k_size = GGML_PAD(ggml_type_size(kv_type)*(size_t) ne10*n_rows*ne12*n_slices, 16);
+ const size_t v_size = v_is_view_of_k ? 0 : GGML_PAD(ggml_type_size(kv_type)*(size_t) ne20*n_rows*ne22*n_slices, 16);
return k_size + v_size;
}
@@ -3719,9 +3749,11 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
// the loop bound over the mask columns / K/V row lists
const int64_t ne11_attn = has_rows ? kv_rows->ne[0] : ne11;
- // the row lists are padded by the KV cache to a multiple of 256, so the pad path never triggers
- GGML_ASSERT(!has_rows || ne11_attn % OP_FLASH_ATTN_EXT_NCPSG == 0);
- GGML_ASSERT(!has_rows || ne11_attn % OP_FLASH_ATTN_EXT_VEC_NCPSG == 0);
+ // with kv_rows the pad buffer holds one gathered copy per q slice, not one per cache stream
+ const int64_t ne_12_3_attn = has_rows ? ne03 : ne13;
+
+ // the vec path gathers the rows of the partial last chunk with the pad kernel
+ const bool pad_gather_rows = has_rows && ggml_metal_op_flash_attn_ext_use_vec(op);
float scale;
float max_bias;
@@ -3777,15 +3809,13 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
const bool use_vec = ggml_metal_op_flash_attn_ext_use_vec(op);
- // with kv_rows, the non-vec path gathers the referenced rows into a contiguous F16 buffer and
- // attends over it with the F16 kernel - quantized KV is first dequantized with the F16 pass
+ // with kv_rows, the non-vec path gathers the referenced rows into a contiguous buffer of the
+ // attention type - quantized KV is first dequantized with the F16 pass
const bool rows_gather = has_rows && !use_vec;
// the vec kernels dequantize the KV inline; no need for the F16 dequant pass in the sparse path
- const bool dequant_kv = !use_sparse && (rows_gather ?
- ggml_is_quantized(op->src[1]->type) : ggml_metal_op_flash_attn_ext_use_kv_f16(op));
-
- const bool use_kv_f16 = !use_sparse && (rows_gather ? true : dequant_kv);
+ const bool dequant_kv = ggml_metal_op_flash_attn_ext_dequant_kv(op);
+ const ggml_type kv_type = dequant_kv ? GGML_TYPE_F16 : op->src[1]->type;
ggml_metal_buffer_id bid_idx = bid_kv_f16;
bid_idx.offs += ggml_metal_op_flash_attn_ext_extra_kv_f16(op);
@@ -3894,8 +3924,6 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
}
}
- // with kv_rows, gather the cache rows referenced by the lists into contiguous F16 buffers
- // the attention then runs as a standard dense op over the gathered rows (tensor or half8x8 kernel)
if (rows_gather) {
assert(ggml_metal_op_flash_attn_ext_extra_rows(op) != 0);
@@ -3903,7 +3931,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
GGML_ASSERT(ne20 % 4 == 0);
- const size_t k_size = GGML_PAD(sizeof(ggml_fp16_t)*(size_t) ne10*ne11_attn*ne12*ne03, 16);
+ const size_t k_size = GGML_PAD(ggml_type_size(kv_type)*(size_t) ne10*ne11_attn*ne12*ne03, 16);
ggml_metal_kargs_flash_attn_ext_rows args0 = {
/*.ne11 =*/ (int32_t) ne11_attn,
@@ -3917,7 +3945,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.nb22 =*/ nb22_attn,
};
- auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_rows(lib, op);
+ auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_rows(lib, op, kv_type);
const int nth = std::min(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline0), 64);
ggml_metal_encoder_set_pipeline(enc, pipeline0);
@@ -3931,10 +3959,10 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_op_concurrency_reset(ctx);
- // contiguous F16 layout of the gathered rows: [DK, n_rows, n_head_kv, n_streams]
+ // compact layout of the gathered rows: [DK, n_rows, n_head_kv, n_streams]
bid_k = bid_rows;
- nb10_attn = sizeof(ggml_fp16_t);
+ nb10_attn = ggml_type_size(kv_type);
nb11_attn = nb10_attn*ne10;
nb12_attn = nb11_attn*ne11_attn;
nb13_attn = nb12_attn*ne12;
@@ -3951,11 +3979,15 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
bid_v = bid_rows;
bid_v.offs += k_size;
- nb20_attn = sizeof(ggml_fp16_t);
+ nb20_attn = ggml_type_size(kv_type);
nb21_attn = nb20_attn*ne20;
nb22_attn = nb21_attn*ne11_attn;
nb23_attn = nb22_attn*ne22;
}
+ } else if (has_rows) {
+ // the row lists select the rows from the K/V cache shared by all slices
+ nb13_attn = 0;
+ nb23_attn = 0;
}
if (!use_sparse && ggml_metal_op_flash_attn_ext_use_tensor(op, props_dev->has_tensor)) {
@@ -4034,7 +4066,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
// shared memory layout: queries (16-bit), scores (float), probabilities (16-bit), row scale (float), rescale flag (int)
const size_t smem = GGML_PAD(nqptg*ne00*sizeof(ggml_fp16_t) + nqptg*ncpsg*(sizeof(float) + sizeof(ggml_fp16_t)) + nqptg*sizeof(float) + sizeof(int32_t), 16);
- auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_tensor(lib, op, has_mask, has_sinks, has_bias, has_scap);
+ auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_tensor(lib, op, has_mask, has_sinks, has_bias, has_scap, kv_type);
GGML_ASSERT(nsg*32 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
GGML_ASSERT(smem <= props_dev->max_theadgroup_memory_size);
@@ -4053,7 +4085,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, ne02, ne03, 32, nsg, 1);
} else if (!use_sparse && !ggml_metal_op_flash_attn_ext_use_vec(op)) {
- // half8x8 kernel
+ // 8x8 simdgroup kernel
const int nqptg = OP_FLASH_ATTN_EXT_NQPSG; // queries per threadgroup
const int ncpsg = OP_FLASH_ATTN_EXT_NCPSG; // cache values per simdgroup
@@ -4069,9 +4101,9 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0);
ggml_metal_kargs_flash_attn_ext_pad args0 = {
- /*.ne11 =*/ne11,
+ /*.ne11 =*/(int32_t) ne11_attn,
/*.ne_12_2 =*/ne12,
- /*.ne_12_3 =*/ne13,
+ /*.ne_12_3 =*/(int32_t) ne_12_3_attn,
/*.nb11 =*/nb11_attn,
/*.nb12 =*/nb12_attn,
/*.nb13 =*/nb13_attn,
@@ -4086,7 +4118,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.nb33 =*/nb33,
};
- auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg);
+ auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, pad_gather_rows, ncpsg);
ggml_metal_encoder_set_pipeline(enc, pipeline0);
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
@@ -4094,11 +4126,12 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_encoder_set_buffer (enc, bid_v, 2);
ggml_metal_encoder_set_buffer (enc, bid_src3, 3);
ggml_metal_encoder_set_buffer (enc, bid_pad, 4);
+ ggml_metal_encoder_set_buffer (enc, pad_gather_rows ? ggml_metal_get_buffer_id(kv_rows) : bid_src0, 5);
assert(ne12 == ne22);
assert(ne13 == ne23);
- ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1);
+ ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max<int64_t>(ne_12_3_attn, ne33), 32, 1, 1);
need_sync = true;
}
@@ -4136,7 +4169,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_op_concurrency_reset(ctx);
}
- const int is_q = !use_kv_f16 && ggml_is_quantized(op->src[1]->type) ? 1 : 0;
+ const int is_q = ggml_is_quantized(kv_type) ? 1 : 0;
// shared memory layout (halfs unless noted):
// queries/attn/result: Q*(DK + 2*PAD2(DV,64) + 4*C)
@@ -4191,7 +4224,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.logit_softcap =*/ logit_softcap,
};
- auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, nsg, use_kv_f16, ns10, ns20);
+ auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, nsg, kv_type, ns10, ns20);
ggml_metal_encoder_set_pipeline(enc, pipeline);
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
@@ -4210,7 +4243,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_encoder_dispatch_threadgroups(enc, (ne01 + nqptg - 1)/nqptg, ne02, ne03, 32, nsg, 1);
} else {
- // half4x4 kernel
+ // vec kernel
// sparse: the index lists are per query row, so a threadgroup can share KV with Q == 1 only
auto cfg = use_sparse
? ggml_metal_tuning::fa_vec_baseline_cfg((int) ne00, (int) ne20)
@@ -4269,9 +4302,9 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
assert(ggml_metal_op_flash_attn_ext_extra_pad(op) != 0);
ggml_metal_kargs_flash_attn_ext_pad args0 = {
- /*.ne11 =*/ne11,
+ /*.ne11 =*/(int32_t) ne11_attn,
/*.ne_12_2 =*/ne12,
- /*.ne_12_3 =*/ne13,
+ /*.ne_12_3 =*/(int32_t) ne_12_3_attn,
/*.nb11 =*/nb11_attn,
/*.nb12 =*/nb12_attn,
/*.nb13 =*/nb13_attn,
@@ -4286,7 +4319,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.nb33 =*/nb33,
};
- auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, ncpsg);
+ auto pipeline0 = ggml_metal_library_get_pipeline_flash_attn_ext_pad(lib, op, has_mask, pad_gather_rows, ncpsg);
ggml_metal_encoder_set_pipeline(enc, pipeline0);
ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
@@ -4294,11 +4327,12 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
ggml_metal_encoder_set_buffer (enc, bid_v, 2);
ggml_metal_encoder_set_buffer (enc, bid_src3, 3);
ggml_metal_encoder_set_buffer (enc, bid_pad, 4);
+ ggml_metal_encoder_set_buffer (enc, pad_gather_rows ? ggml_metal_get_buffer_id(kv_rows) : bid_src0, 5);
assert(ne12 == ne22);
assert(ne13 == ne23);
- ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max(ne13, ne33), 32, 1, 1);
+ ggml_metal_encoder_dispatch_threadgroups(enc, ncpsg, std::max(ne12, ne32), std::max<int64_t>(ne_12_3_attn, ne33), 32, 1, 1);
need_sync = true;
}
@@ -4374,7 +4408,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.nb03 =*/ nb03,
/*.ne11 =*/ use_sparse ? n_kv_max_padded : (int32_t) ne11_attn,
/*.ne_12_2 =*/ ne12,
- /*.ne_12_3 =*/ ne13,
+ /*.ne_12_3 =*/ has_rows ? ne03 : ne13,
/*.ns10 =*/ ns10,
/*.nb11 =*/ nb11_attn,
/*.nb12 =*/ nb12_attn,
@@ -4401,7 +4435,7 @@ int ggml_metal_op_flash_attn_ext(ggml_metal_op_t ctx, int idx) {
/*.n_kv_max_padded =*/ n_kv_max_padded,
};
- auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_vec(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, use_sparse, has_rows, nqptg, cfg.NE, nsg, nwg, use_kv_f16, ns10, ns20);
+ auto pipeline = ggml_metal_library_get_pipeline_flash_attn_ext_vec(lib, op, has_mask, has_sinks, has_bias, has_scap, has_kvpad, use_sparse, has_rows, nqptg, cfg.NE, nsg, nwg, kv_type, ns10, ns20);
GGML_ASSERT(nsg*32 <= ggml_metal_pipeline_max_theads_per_threadgroup(pipeline));
diff --git a/ggml/src/ggml-metal/kernels/fa_aux.metal b/ggml/src/ggml-metal/kernels/fa_aux.metal
index 6fe47470f..32de8f2f9 100644
--- a/ggml/src/ggml-metal/kernels/fa_aux.metal
+++ b/ggml/src/ggml-metal/kernels/fa_aux.metal
@@ -48,10 +48,12 @@ template [[host_name("kernel_flash_attn_ext_kv_q5_0_f16")]] kernel kernel_flash_
template [[host_name("kernel_flash_attn_ext_kv_q5_1_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q5_1, 32, dequantize_q5_1>;
template [[host_name("kernel_flash_attn_ext_kv_q8_0_f16")]] kernel kernel_flash_attn_ext_kv_f16_t kernel_flash_attn_ext_kv_f16<block_q8_0, 32, dequantize_q8_0>;
-// gather the K/V cache rows referenced by the kv_rows lists into contiguous F16 buffers
+// gather the K/V cache rows referenced by the kv_rows lists into contiguous buffers
// - one threadgroup per (row, stream); the attention is then run as a standard dense op over the
// gathered rows, which avoids the per-tile K/V gather in the main kernels
// - negative rows are padding: filled with zeros, their mask entries are -INF so they contribute nothing
+// - T is the K/V cache type, or F16 for the dequantized scratch
+template <typename T>
kernel void kernel_flash_attn_ext_rows(
constant ggml_metal_kargs_flash_attn_ext_rows & args,
device const char * k_src,
@@ -61,17 +63,19 @@ kernel void kernel_flash_attn_ext_rows(
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiitg[[thread_index_in_threadgroup]],
ushort3 ntg[[threads_per_threadgroup]]) {
+ using T4 = typename value_type_4<T>::type;
+
const int64_t i1 = tgpig[0]; // row index
const int64_t i3 = tgpig[1]; // stream
const int64_t row = rows[i1 + i3*args.ne11];
- // gathered layout: [DK, n_rows, n_head_kv, n_streams] and [DV, n_rows, n_head_kv, n_streams] F16
- device half4 * k_dst = (device half4 *) (dst + i3*args.ne_12_2*args.ne11*args.ne10*sizeof(half));
- device half4 * v_dst = (device half4 *) (dst + args.v_off + i3*args.ne_12_2*args.ne11*args.ne20*sizeof(half));
+ // gathered layout: [DK, n_rows, n_head_kv, n_streams] and [DV, n_rows, n_head_kv, n_streams]
+ device T4 * k_dst = (device T4 *) (dst + i3*args.ne_12_2*args.ne11*args.ne10*sizeof(T));
+ device T4 * v_dst = (device T4 *) (dst + args.v_off + i3*args.ne_12_2*args.ne11*args.ne20*sizeof(T));
// note: the K/V cache is shared by all streams (ne13 = ne23 = 1), the row list selects the rows
- // each thread copies up to 4 half4 values (32 bytes) to keep the memory transactions large
+ // each thread copies up to 4 T4 values (32 bytes) to keep the memory transactions large
// K: all heads of this row
{
@@ -82,16 +86,16 @@ kernel void kernel_flash_attn_ext_rows(
const int64_t i0 = i%per_head;
const int n = (int) min((int64_t) 4, per_head - i0);
- device half4 * dst4 = k_dst + (i2*args.ne11 + i1)*per_head + i0;
+ device T4 * dst4 = k_dst + (i2*args.ne11 + i1)*per_head + i0;
if (row >= 0) {
- device const half4 * src4 = (device const half4 *) (k_src + row*args.nb11 + i2*args.nb12) + i0;
+ device const T4 * src4 = (device const T4 *) (k_src + row*args.nb11 + i2*args.nb12) + i0;
for (int j = 0; j < n; ++j) {
dst4[j] = src4[j];
}
} else {
for (int j = 0; j < n; ++j) {
- dst4[j] = half4(0.0h);
+ dst4[j] = (T4) ((float4) 0.0f);
}
}
}
@@ -106,33 +110,43 @@ kernel void kernel_flash_attn_ext_rows(
const int64_t i0 = i%per_head;
const int n = (int) min((int64_t) 4, per_head - i0);
- device half4 * dst4 = v_dst + (i2*args.ne11 + i1)*per_head + i0;
+ device T4 * dst4 = v_dst + (i2*args.ne11 + i1)*per_head + i0;
if (row >= 0) {
- device const half4 * src4 = (device const half4 *) (v_src + row*args.nb21 + i2*args.nb22) + i0;
+ device const T4 * src4 = (device const T4 *) (v_src + row*args.nb21 + i2*args.nb22) + i0;
for (int j = 0; j < n; ++j) {
dst4[j] = src4[j];
}
} else {
for (int j = 0; j < n; ++j) {
- dst4[j] = half4(0.0h);
+ dst4[j] = (T4) ((float4) 0.0f);
}
}
}
}
}
+typedef decltype(kernel_flash_attn_ext_rows<half>) kernel_flash_attn_ext_rows_t;
+
+template [[host_name("kernel_flash_attn_ext_rows_f16")]] kernel kernel_flash_attn_ext_rows_t kernel_flash_attn_ext_rows<half>;
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_flash_attn_ext_rows_bf16")]] kernel kernel_flash_attn_ext_rows_t kernel_flash_attn_ext_rows<bfloat>;
+#endif
+
constant bool FC_flash_attn_ext_pad_has_mask [[function_constant(FC_FLASH_ATTN_EXT_PAD + 0)]];
+constant bool FC_flash_attn_ext_pad_has_rows [[function_constant(FC_FLASH_ATTN_EXT_PAD + 1)]];
constant int32_t FC_flash_attn_ext_pad_ncpsg [[function_constant(FC_FLASH_ATTN_EXT_PAD + 25)]];
// pad the last chunk of C elements of k and v into a an extra pad buffer
+// - with kv_rows, the rows of the last chunk are gathered through the row list (the K/V cache is shared by all streams)
kernel void kernel_flash_attn_ext_pad(
constant ggml_metal_kargs_flash_attn_ext_pad & args,
device const char * k,
device const char * v,
device const char * mask,
device char * dst,
+ device const int * rows,
uint3 tgpig[[threadgroup_position_in_grid]],
ushort tiitg[[thread_index_in_threadgroup]],
ushort3 ntg[[threads_per_threadgroup]]) {
@@ -150,25 +164,39 @@ kernel void kernel_flash_attn_ext_pad(
const int32_t i3 = tgpig[2];
if (i2 < args.ne_12_2 && i3 < args.ne_12_3) {
- device const char * k_src = k + args.nb11*(ic0 + i1) + args.nb12*i2 + args.nb13*i3;
- device const char * v_src = v + args.nb21*(ic0 + i1) + args.nb22*i2 + args.nb23*i3;
-
device char * k_dst = k_pad + args.nb11*i1 + args.nb11*C*i2 + args.nb11*C*args.ne_12_2*i3;
device char * v_dst = v_pad + args.nb21*i1 + args.nb21*C*i2 + args.nb21*C*args.ne_12_2*i3;
- if (i1 >= icp) {
+ // the row list of this stream selects the K/V cache rows of the last chunk
+ // note: the cache rows can span all K/V heads, so only the per-head bytes are copied
+ const int64_t ic = ic0 + i1;
+ const int64_t row = FC_flash_attn_ext_pad_has_rows && ic < args.ne11 ? rows[ic + (int64_t) i3*args.ne11] : -1;
+
+ const bool pad = FC_flash_attn_ext_pad_has_rows ? row < 0 : i1 >= icp;
+
+ const uint64_t nb_k = FC_flash_attn_ext_pad_has_rows && args.nb12 < args.nb11 ? args.nb12 : args.nb11;
+ const uint64_t nb_v = FC_flash_attn_ext_pad_has_rows && args.nb22 < args.nb21 ? args.nb22 : args.nb21;
+
+ if (pad) {
// here it is not important the exact value that will be used as we rely on masking out the scores in the attention
- for (uint64_t i = tiitg; i < args.nb11; i += ntg.x) {
+ for (uint64_t i = tiitg; i < nb_k; i += ntg.x) {
k_dst[i] = 0;
}
- for (uint64_t i = tiitg; i < args.nb21; i += ntg.x) {
+ for (uint64_t i = tiitg; i < nb_v; i += ntg.x) {
v_dst[i] = 0;
}
} else {
- for (uint64_t i = tiitg; i < args.nb11; i += ntg.x) {
+ device const char * k_src = FC_flash_attn_ext_pad_has_rows
+ ? k + (int64_t) row*args.nb11 + args.nb12*i2
+ : k + args.nb11*(ic0 + i1) + args.nb12*i2 + args.nb13*i3;
+ device const char * v_src = FC_flash_attn_ext_pad_has_rows
+ ? v + (int64_t) row*args.nb21 + args.nb22*i2
+ : v + args.nb21*(ic0 + i1) + args.nb22*i2 + args.nb23*i3;
+
+ for (uint64_t i = tiitg; i < nb_k; i += ntg.x) {
k_dst[i] = k_src[i];
}
- for (uint64_t i = tiitg; i < args.nb21; i += ntg.x) {
+ for (uint64_t i = tiitg; i < nb_v; i += ntg.x) {
v_dst[i] = v_src[i];
}
}
diff --git a/ggml/src/ggml-metal/kernels/fa_common.metal b/ggml/src/ggml-metal/kernels/fa_common.metal
index 0a2148bda..3a2d539c5 100644
--- a/ggml/src/ggml-metal/kernels/fa_common.metal
+++ b/ggml/src/ggml-metal/kernels/fa_common.metal
@@ -449,11 +449,11 @@ void kernel_flash_attn_ext_impl(
FOR_UNROLL (short ii = 0; ii < DV4/NW; ++ii) {
const short i = ii*NW + tiisg;
- so4[j*PV4 + i] *= ms;
+ so4[j*PV4 + i] = (o4_t) ((float4) so4[j*PV4 + i]*ms);
}
} else {
for (short i = tiisg; i < DV4; i += NW) {
- so4[j*PV4 + i] *= ms;
+ so4[j*PV4 + i] = (o4_t) ((float4) so4[j*PV4 + i]*ms);
}
}
}
@@ -625,7 +625,7 @@ void kernel_flash_attn_ext_impl(
S[jj] = S[jj]*ms + simd_sum(vs);
for (short i = tiisg; i < DV4; i += NW) {
- so4[j*PV4 + i] *= ms;
+ so4[j*PV4 + i] = (o4_t) ((float4) so4[j*PV4 + i]*ms);
}
}
}
diff --git a/ggml/src/ggml-metal/kernels/fa_f16.metal b/ggml/src/ggml-metal/kernels/fa_f16.metal
index 616b9377a..e6820d3d4 100644
--- a/ggml/src/ggml-metal/kernels/fa_f16.metal
+++ b/ggml/src/ggml-metal/kernels/fa_f16.metal
@@ -19,7 +19,7 @@
bfloat, \
float, \
float, \
- half
+ bfloat
#define FA_TYPES_F32 \
half, \
diff --git a/ggml/src/ggml-metal/kernels/fa_vec_common.metal b/ggml/src/ggml-metal/kernels/fa_vec_common.metal
index cf37e4924..f30ec3f7e 100644
--- a/ggml/src/ggml-metal/kernels/fa_vec_common.metal
+++ b/ggml/src/ggml-metal/kernels/fa_vec_common.metal
@@ -124,12 +124,12 @@ kernel void kernel_flash_attn_ext_vec(
if (i < DK4) {
sq4[qq*PK4 + i] = (q4_t) q4[i];
} else {
- sq4[qq*PK4 + i] = (q4_t) 0.0f;
+ sq4[qq*PK4 + i] = (q4_t) ((float4) 0.0f);
}
}
} else {
for (short i = tiisg; i < PK4; i += NW) {
- sq4[qq*PK4 + i] = (q4_t) 0.0f;
+ sq4[qq*PK4 + i] = (q4_t) ((float4) 0.0f);
}
}
}
@@ -208,7 +208,9 @@ kernel void kernel_flash_attn_ext_vec(
}
// the last partial chunk uses the pad buffer as source
- if (FC_flash_attn_ext_vec_has_kvpad && ic + C > args.ne11) {
+ const bool use_pad = FC_flash_attn_ext_vec_has_kvpad && ic + C > args.ne11;
+
+ if (use_pad) {
k = pad;
v = k + args.nb11*C*args.ne_12_2*args.ne_12_3;
mask = v + args.nb21*C*args.ne_12_2*args.ne_12_3;
@@ -238,7 +240,8 @@ kernel void kernel_flash_attn_ext_vec(
}
// load the KV row indices for the current block into shared memory
- if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) {
+ // note: with kv_rows the pad buffer holds the gathered rows in column order, so no list is needed
+ if (FC_flash_attn_ext_vec_has_sparse || (FC_flash_attn_ext_vec_has_rows && !use_pad)) {
FOR_UNROLL (short ii = 0; ii < C/NW; ++ii) {
const short i = ii*NW + tiisg;
@@ -317,7 +320,7 @@ kernel void kernel_flash_attn_ext_vec(
FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) {
if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) {
// the KV rows are gathered from the index list; -1 entries are padding
- const int i11 = spidx[NE*cc + ty];
+ const int i11 = (FC_flash_attn_ext_vec_has_rows && use_pad) ? (ic + NE*cc + ty) : spidx[NE*cc + ty];
if (i11 >= 0) {
if (is_same<kd4_t, k4_t>::value) {
device const k4_t * pk4s = (device const k4_t *) (k + i11*args.nb11) + tx;
@@ -461,7 +464,7 @@ kernel void kernel_flash_attn_ext_vec(
if (FC_flash_attn_ext_vec_has_sparse || FC_flash_attn_ext_vec_has_rows) {
FOR_UNROLL (short cc = 0; cc < C/NE; ++cc) {
// the KV rows are gathered from the index list; -1 entries are padding
- const int i11 = spidx[NE*cc + ty];
+ const int i11 = (FC_flash_attn_ext_vec_has_rows && use_pad) ? (ic + NE*cc + ty) : spidx[NE*cc + ty];
if (i11 >= 0) {
if (is_same<vd4_t, v4_t>::value) {
device const v4_t * pv4 = (device const v4_t *) (v + i11*args.nb21);
diff --git a/ggml/src/ggml-metal/kernels/fa_vec_f16.metal b/ggml/src/ggml-metal/kernels/fa_vec_f16.metal
index 94377e39f..4431819a5 100644
--- a/ggml/src/ggml-metal/kernels/fa_vec_f16.metal
+++ b/ggml/src/ggml-metal/kernels/fa_vec_f16.metal
@@ -10,6 +10,14 @@
float, \
float
+#define FA_TYPES_BF \
+ bfloat, \
+ bfloat, \
+ bfloat, \
+ float, \
+ float, \
+ float
+
#define FA_TYPES_F32 \
half, \
float, \
@@ -24,7 +32,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk32_dv32")]] kernel flas
template [[host_name("kernel_flash_attn_ext_vec_f16_dk32_dv32_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 32, 32, 4, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk32_dv32_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 32, 32, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk32_dv32")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 32, 32, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk32_dv32")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 32, 32, 4>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk64_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 64, 64, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk64_dv64_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 64, 64, 4, 1>;
@@ -33,19 +41,19 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk64_dv64_q2_ne4")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk64_dv64_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 64, 64, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk64_dv64_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 64, 64, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 2>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 96, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv96_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 96, 4, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv96_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 96, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 96, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 96, 4>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 64, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv64_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 64, 4, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk96_dv64_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 96, 64, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 64, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv64")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 64, 4>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk128_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 128, 128, 1>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk128_dv128_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 128, 128, 2, 1>;
@@ -60,8 +68,8 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk128_dv96")]] kernel fla
template [[host_name("kernel_flash_attn_ext_vec_f16_dk128_dv96_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 128, 96, 4, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk128_dv96_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 128, 96, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 1>;
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 96, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv96")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 96, 4>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv192")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 192, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv192_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 192, 4, 1>;
@@ -70,7 +78,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv192_q2_ne4")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv192_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 192, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv192_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 192, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 2>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 128, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv128_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 128, 4, 1>;
@@ -79,7 +87,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv128_q2_ne4")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv128_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 128, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk192_dv128_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 192, 128, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 2>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk256_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 256, 256, 1>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk256_dv256_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 256, 256, 2, 1>;
@@ -91,7 +99,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk256_dv256_q4_ne1")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk256_dv256_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 256, 256, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk256_dv256_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 256, 256, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 1>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk320_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 320, 256, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk320_dv256_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 320, 256, 4, 1>;
@@ -100,7 +108,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk320_dv256_q2_ne4")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk320_dv256_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 320, 256, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk320_dv256_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 320, 256, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 2>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk512_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 512, 512, 1>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk512_dv512_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 512, 512, 2, 1>;
@@ -112,7 +120,7 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk512_dv512_q4_ne1")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk512_dv512_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 512, 512, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk512_dv512_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 512, 512, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 1>;
#endif
template [[host_name("kernel_flash_attn_ext_vec_f16_dk576_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 576, 512, 2>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk576_dv512_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 576, 512, 4, 1>;
@@ -121,9 +129,70 @@ template [[host_name("kernel_flash_attn_ext_vec_f16_dk576_dv512_q2_ne4")]] kerne
template [[host_name("kernel_flash_attn_ext_vec_f16_dk576_dv512_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 576, 512, 2, 4>;
template [[host_name("kernel_flash_attn_ext_vec_f16_dk576_dv512_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, half4, 1, dequantize_f16_t4, half4, 1, dequantize_f16_t4, 576, 512, 4, 4>;
#if defined(GGML_METAL_HAS_BF16)
-template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 2>;
+#endif
+
+#if defined(GGML_METAL_HAS_BF16)
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk32_dv32_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 32, 32, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk32_dv32_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 32, 32, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk64_dv64_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 64, 64, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv96_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 96, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv96_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 96, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv64_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 64, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk96_dv64_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 96, 64, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 2, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q2_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 1, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q4_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 1, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv128_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 128, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv96_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 96, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk128_dv96_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 128, 96, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv192_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 192, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk192_dv128_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 192, 128, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 2, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q2_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 1, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q4_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 1, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk256_dv256_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 256, 256, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk320_dv256_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 320, 256, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q1_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 2, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q2_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 1, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q4_ne1")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 1, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk512_dv512_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 512, 512, 4, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512_q1_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 4, 1>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512_q2_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 2, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512_q2_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 4, 2>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512_q4_ne2")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 2, 4>;
+template [[host_name("kernel_flash_attn_ext_vec_bf16_dk576_dv512_q4_ne4")]] kernel flash_attn_ext_vec_t kernel_flash_attn_ext_vec<FA_TYPES_BF, bfloat4, 1, dequantize_bf16_t4, bfloat4, 1, dequantize_bf16_t4, 576, 512, 4, 4>;
#endif
#undef FA_TYPES
+#undef FA_TYPES_BF
#undef FA_TYPES_F32
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 325f727a0..ee481f5f3 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -15372,6 +15372,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext_kv_rows(64, 2, 1, 1000, 257, 256, 2, GGML_TYPE_F16, true));
test_cases.emplace_back(new test_flash_attn_ext_kv_rows(128, 4, 1, 1000, 256, 8, 2, GGML_TYPE_BF16));
test_cases.emplace_back(new test_flash_attn_ext_kv_rows(128, 4, 1, 1000, 256, 256, 2, GGML_TYPE_BF16));
+ test_cases.emplace_back(new test_flash_attn_ext_kv_rows(64, 2, 1, 1000, 257, 256, 2, GGML_TYPE_BF16));
+ test_cases.emplace_back(new test_flash_attn_ext_kv_rows(96, 2, 1, 1000, 512, 256, 2, GGML_TYPE_BF16));
for (int64_t nb : {1, 3, 8, 64, 512}) {
for (int64_t n_slices : {1, 4}) {
test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 2, 8, 4096, 1024, nb, n_slices));
@@ -16867,7 +16869,7 @@ static bool run_fa_vec_slice(ggml_backend_t backend, ggml_backend_t backend_cpu,
const shape_t shapes[] = { { 128, 128 }, { 576, 512 } }; // mainstream head size + MLA shared K/V view
const int ne01_pts[] = { 1, 3 }; // decode, and padded rows for Q=2 and Q=4
const int ne11_pts[] = { 512, 4097 }; // nsg=1, and nsg>=2 together with kvpad
- const ggml_type types[] = { GGML_TYPE_F16, GGML_TYPE_Q4_0 };
+ const ggml_type types[] = { GGML_TYPE_F16, GGML_TYPE_Q4_0, GGML_TYPE_BF16 };
int n_run = 0;
int n_fail = 0;