Commit aa631e031 for llama.cpp
commit aa631e031e8043809cc9e79ad0a1488dcd80c4db
Author: Georgi Gerganov <ggerganov@gmail.com>
Date: Mon Nov 30 09:39:56 2026 +0200
ggml : clean up FA bias API (#33178)
* ggml : validate indexed bias at op construction only
The indexed-bias check already runs in ggml_flash_attn_ext_add_indexed_bias(), so the backend checks were redundant. Make the validator static in ggml.c, drop the ggml-impl.h declaration, and remove the CPU supports_op case, the CPU compute assert, and the CUDA kernel-selection check.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : rename FA indexed bias to bias_indexed
Add internal ggml_flash_attn_ext_has_bias() and ggml_flash_attn_ext_bias_is_indexed() helpers and derive the bias state from src[6]/src[7] instead of op_params[5]. Rename ggml_flash_attn_ext_add_indexed_bias() to ggml_flash_attn_ext_add_bias_indexed() and keep the old public names as deprecated wrappers.
Rename the remaining indexed_bias identifiers to bias_indexed, including the CUDA kernel templates and the HIP metrics script. Reject non-indexed bias in the CPU, CUDA and Vulkan backends; backends without bias support reject any bias.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : rename FA indexed bias validator
Name the static validator after the function it validates: ggml_flash_attn_ext_add_bias_indexed_validate().
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : keep src order in FA bias validation
Reorder the src tensor declarations in the indexed-bias validator to follow the src index order.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : mark FA bias wrappers as deprecated
Add the deprecated marker above the old indexed-bias wrapper implementations.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : generalize FA bias API
Rename ggml_flash_attn_ext_add_bias_indexed() to ggml_flash_attn_ext_add_bias() and rename its arguments to bias and bias_idxs. When bias_idxs is NULL, the bias is validated as a dense K*Q tensor with shape [n_kv, n_batch, n_head_kv, n_stream]; otherwise it remains an indexed bias table.
Update the deprecated wrapper to stay indexed-only, and align the table/indices naming in llama-graph, CPU, CUDA, Vulkan and the tests.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : move FA bias args after kv_rows
Reorder the bias and bias_idxs arguments of build_attn_mha() to follow kv_rows, and update all call sites.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : deduplicate FA kq_b and bias
Remove the kq_b argument from build_attn_mha() and build_attn_iswa_impl() and pass the dense KQ bias through bias with bias_idxs == nullptr. The FA path now rejects a dense bias and adds an indexed bias without backend checks. The non-FA path adds a dense bias directly to kq.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : inline dense bias mask
Inline the dense_bias_mask lambda into the non-FA path, now its only use.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : add FA bias to mask in non-FA path
Add both indexed and dense FA biases to the mask in the non-FA path so they are scaled by the ALiBi slope, matching the FA path. Remove the kq-side addition for dense bias.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : remove redundant non-FA mask cast
The non-FA path already implies soft_max_ext can consume an F32 mask, so drop the cparams.flash_attn check and the F16 cast.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : cast FA bias to mask type
Compute the non-FA bias in F32 and cast it to the mask type before adding, instead of casting the mask to F32.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : remove FA bias TODO
Remove the TODO after confirming that adding the bias to the mask is correct.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : guard FA dense bias in validation
Add a CPU compute assert for the indexed-bias assumption, reject the dense bias in ggml_flash_attn_ext_add_bias_validate with a TODO, and update the backend comments to say bias.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : document FA bias scaling
Note that the bias is added after the KQ scaling.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : rename FA add sinks/bias to set
Rename ggml_flash_attn_ext_add_sinks to set_sinks and deprecate the old name. Rename ggml_flash_attn_ext_add_bias to set_bias. Allow set_sinks to override existing sinks.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* llama : set FA bias unconditionally
Call ggml_flash_attn_ext_set_bias() unconditionally and move it before ggml_flash_attn_ext_set_sinks().
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml-cuda : fix sparse bias template names
Rename the remaining indexed_bias references in the sparse FA code added by master.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h
index 4666b82d9..80eb71f28 100644
--- a/ggml/include/ggml.h
+++ b/ggml/include/ggml.h
@@ -2309,18 +2309,32 @@ extern "C" {
struct ggml_tensor * a,
int32_t n_kv_max);
- GGML_API void ggml_flash_attn_ext_add_sinks(
+ GGML_API void ggml_flash_attn_ext_set_sinks(
struct ggml_tensor * a,
struct ggml_tensor * sinks);
- GGML_API bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a);
+ GGML_DEPRECATED(GGML_API void ggml_flash_attn_ext_add_sinks(
+ struct ggml_tensor * a,
+ struct ggml_tensor * sinks),
+ "use ggml_flash_attn_ext_set_sinks instead");
+
+ // Add bias[bias_idxs[key, query], head, query] to the mask, in F32.
+ // The bias is added after the KQ scaling.
+ // Queries from all streams are flattened in stream order. Indices must be in [0, bias->ne[0]).
+ // If bias_idxs is NULL, bias is a dense K*Q bias with shape [n_kv, n_batch, n_head_kv, n_stream].
+ GGML_API void ggml_flash_attn_ext_set_bias(
+ struct ggml_tensor * a,
+ struct ggml_tensor * bias,
+ struct ggml_tensor * bias_idxs);
+
+ GGML_DEPRECATED(GGML_API bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a),
+ "use ggml_flash_attn_ext_bias_is_indexed instead");
- // Add table[indices[key, query], head, query] to the mask, in F32.
- // Queries from all streams are flattened in stream order. Indices must be in [0, table->ne[0]).
- GGML_API void ggml_flash_attn_ext_add_indexed_bias(
+ GGML_DEPRECATED(GGML_API void ggml_flash_attn_ext_add_indexed_bias(
struct ggml_tensor * a,
- struct ggml_tensor * table,
- struct ggml_tensor * indices);
+ struct ggml_tensor * bias,
+ struct ggml_tensor * bias_idxs),
+ "use ggml_flash_attn_ext_set_bias instead");
// TODO: needs to be adapted to ggml_flash_attn_ext
GGML_API struct ggml_tensor * ggml_flash_attn_back(
diff --git a/ggml/src/ggml-backend-meta.cpp b/ggml/src/ggml-backend-meta.cpp
index a41682c5e..815488f88 100644
--- a/ggml/src/ggml-backend-meta.cpp
+++ b/ggml/src/ggml-backend-meta.cpp
@@ -153,7 +153,7 @@ static ggml_backend_buffer_type_t ggml_backend_meta_device_get_buffer_type(ggml_
static ggml_backend_buffer_type_t ggml_backend_meta_device_get_host_buffer_type(ggml_backend_dev_t dev);
static bool ggml_backend_meta_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
- if (ggml_flash_attn_ext_has_indexed_bias(op)) {
+ if (ggml_flash_attn_ext_has_bias(op)) {
return false;
}
diff --git a/ggml/src/ggml-cann/ggml-cann.cpp b/ggml/src/ggml-cann/ggml-cann.cpp
index d0e220c73..91f835af3 100644
--- a/ggml/src/ggml-cann/ggml-cann.cpp
+++ b/ggml/src/ggml-cann/ggml-cann.cpp
@@ -2686,8 +2686,8 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
return true;
case GGML_OP_FLASH_ATTN_EXT:
{
- // K/V row lists and indexed bias are not supported
- if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
+ // K/V row lists and bias are not supported
+ if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
return false;
}
#ifdef ASCEND_310P
diff --git a/ggml/src/ggml-cpu/ggml-cpu.cpp b/ggml/src/ggml-cpu/ggml-cpu.cpp
index ddb86ed13..34fe2b5b3 100644
--- a/ggml/src/ggml-cpu/ggml-cpu.cpp
+++ b/ggml/src/ggml-cpu/ggml-cpu.cpp
@@ -442,7 +442,7 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
switch (op->op) {
case GGML_OP_FLASH_ATTN_EXT:
- return ggml_validate_flash_attn_ext_indexed_bias(op);
+ return !ggml_flash_attn_ext_has_bias(op) || ggml_flash_attn_ext_bias_is_indexed(op);
case GGML_OP_CPY:
case GGML_OP_SET_ROWS:
return
diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp
index d01b6c11a..1e9b3c2b0 100644
--- a/ggml/src/ggml-cpu/ops.cpp
+++ b/ggml/src/ggml-cpu/ops.cpp
@@ -8981,8 +8981,8 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
const ggml_tensor * mask = dst->src[3];
const ggml_tensor * sinks = dst->src[4];
const ggml_tensor * kv_rows = dst->src[5];
- const ggml_tensor * bias_table = dst->src[6];
- const ggml_tensor * bias_indices = dst->src[7];
+ const ggml_tensor * bias = dst->src[6];
+ const ggml_tensor * bias_idxs = dst->src[7];
GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
@@ -9024,7 +9024,7 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
const int64_t rv2 = neq2/nev2;
const int64_t rv3 = neq3/nev3;
- const int64_t n_bins = bias_table ? bias_table->ne[0] : 0;
+ const int64_t n_bins = bias ? bias->ne[0] : 0;
// parallelize by q rows using ggml_vec_dot_f32
@@ -9090,8 +9090,8 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
const int32_t * rows = kv_rows ? (const int32_t *) ((const char *) kv_rows->data + iq3*kv_rows->nb[1]) : nullptr;
// bias rows of this query and head, queries of all streams are flattened
- const float * bias = bias_table ? (const float *) ((const char *) bias_table->data + (iq3*neq1 + iq1)*bias_table->nb[2] + iq2*bias_table->nb[1]) : nullptr;
- const int32_t * bins = bias_indices ? (const int32_t *) ((const char *) bias_indices->data + (iq3*neq1 + iq1)*bias_indices->nb[1]) : nullptr;
+ const float * bias_row = bias ? (const float *) ((const char *) bias->data + (iq3*neq1 + iq1)*bias->nb[2] + iq2*bias->nb[1]) : nullptr;
+ const int32_t * bins = bias_idxs ? (const int32_t *) ((const char *) bias_idxs->data + (iq3*neq1 + iq1)*bias_idxs->nb[1]) : nullptr;
// online softmax / attention
// loop over n_kv and n_head_kv
@@ -9099,9 +9099,9 @@ static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
for (int64_t ic = ic_start; ic < ic_end; ++ic) {
float mv = mp ? GGML_CPU_FP16_TO_FP32(mp[ic]) : 0.0f;
- if (bias) {
+ if (bias_row) {
GGML_ASSERT(bins[ic] >= 0 && bins[ic] < n_bins);
- mv += bias[bins[ic]];
+ mv += bias_row[bins[ic]];
}
mv *= slope;
if (mv == -INFINITY) {
@@ -9281,9 +9281,9 @@ static void ggml_compute_forward_flash_attn_ext_tiled(
const ggml_tensor * kv_rows = dst->src[5];
const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : nek1;
- const ggml_tensor * bias_table = dst->src[6];
- const ggml_tensor * bias_indices = dst->src[7];
- const int64_t n_bins = bias_table ? bias_table->ne[0] : 0;
+ const ggml_tensor * bias = dst->src[6];
+ const ggml_tensor * bias_idxs = dst->src[7];
+ const int64_t n_bins = bias ? bias->ne[0] : 0;
int ir = ir0;
while (ir < ir1) {
@@ -9363,13 +9363,13 @@ static void ggml_compute_forward_flash_attn_ext_tiled(
bool can_skip = true;
for (int tq = 0; tq < tile_rows; tq++) {
const ggml_fp16_t * mp_row = (const ggml_fp16_t *)((const char *) mask->data + (iq1 + tq)*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]);
- const float * bias = bias_table ? (const float *) ((const char *) bias_table->data + (iq3*neq1 + iq1 + tq)*bias_table->nb[2] + iq2*bias_table->nb[1]) : nullptr;
- const int32_t * bins = bias_indices ? (const int32_t *) ((const char *) bias_indices->data + (iq3*neq1 + iq1 + tq)*bias_indices->nb[1]) : nullptr;
+ const float * bias_row = bias ? (const float *) ((const char *) bias->data + (iq3*neq1 + iq1 + tq)*bias->nb[2] + iq2*bias->nb[1]) : nullptr;
+ const int32_t * bins = bias_idxs ? (const int32_t *) ((const char *) bias_idxs->data + (iq3*neq1 + iq1 + tq)*bias_idxs->nb[1]) : nullptr;
for (int tk = 0; tk < kv_tile; tk++) {
float m = GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]);
- if (bias) {
+ if (bias_row) {
GGML_ASSERT(bins[ic + tk] >= 0 && bins[ic + tk] < n_bins);
- m += bias[bins[ic + tk]];
+ m += bias_row[bins[ic + tk]];
}
mask32[tq * KV_TILE_SZ + tk] = slope * m;
if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) {
@@ -9722,7 +9722,7 @@ static void ggml_compute_forward_flash_attn_ext_f16(
void ggml_compute_forward_flash_attn_ext(
const ggml_compute_params * params,
ggml_tensor * dst) {
- GGML_ASSERT(ggml_validate_flash_attn_ext_indexed_bias(dst));
+ GGML_ASSERT(!ggml_flash_attn_ext_has_bias(dst) || ggml_flash_attn_ext_bias_is_indexed(dst));
switch (ggml_prec_get_acc(dst)) {
case GGML_PREC_UNDEFINED:
case GGML_PREC_F32:
diff --git a/ggml/src/ggml-cpu/spacemit/ime.cpp b/ggml/src/ggml-cpu/spacemit/ime.cpp
index 0486d2861..380ff6199 100644
--- a/ggml/src/ggml-cpu/spacemit/ime.cpp
+++ b/ggml/src/ggml-cpu/spacemit/ime.cpp
@@ -1047,8 +1047,8 @@ class tensor_traits_common : public tensor_traits_base {
return true;
}
case GGML_OP_FLASH_ATTN_EXT:
- // K/V row lists and the indexed bias are handled by the generic CPU path
- if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
+ // K/V row lists and bias are handled by the generic CPU path
+ if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
return false;
}
forward_flash_attn_ext_f16(params, op);
diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh
index c9f212fbe..a3a99c86e 100644
--- a/ggml/src/ggml-cuda/fattn-common.cuh
+++ b/ggml/src/ggml-cuda/fattn-common.cuh
@@ -20,20 +20,20 @@
// The macro on the following line shifts it by a factor of 2**3=8, as was needed to fix https://github.com/ggml-org/llama.cpp/issues/18606 .
#define FATTN_KQ_MAX_OFFSET (3.0f*0.6931f)
-struct fattn_indexed_bias {
- const float * table;
- const int32_t * indices;
+struct fattn_bias_indexed {
+ const float * bias;
+ const int32_t * bias_idxs;
int64_t n_bins;
int64_t n_keys;
int64_t query_stride;
- __device__ fattn_indexed_bias for_head(int64_t first_query, int head) const {
- return {table + first_query*query_stride + head*n_bins, indices + first_query*n_keys, n_bins, n_keys, query_stride};
+ __device__ fattn_bias_indexed for_head(int64_t first_query, int head) const {
+ return {bias + first_query*query_stride + head*n_bins, bias_idxs + first_query*n_keys, n_bins, n_keys, query_stride};
}
__device__ float add(float mask, int query, int key) const {
- const int bin = indices[query*n_keys + key];
- return bin >= 0 && bin < n_bins ? mask + table[query*query_stride + bin] : mask;
+ const int bin = bias_idxs[query*n_keys + key];
+ return bin >= 0 && bin < n_bins ? mask + bias[query*query_stride + bin] : mask;
}
};
@@ -65,7 +65,7 @@ using fattn_kernel_t = void (*)(
const int32_t s21, const int32_t s22, const int64_t s23,
const int32_t ne31, const int32_t ne32, const int32_t ne33,
const int32_t s31, const int64_t s32, const int64_t s33,
- const fattn_indexed_bias bias);
+ const fattn_bias_indexed bias);
typedef float (*vec_dot_KQ_t)(
const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds);
@@ -1324,9 +1324,9 @@ void launch_fattn(
const ggml_tensor * sinks = dst->src[4];
const ggml_tensor * kv_rows = dst->src[5];
- const bool has_bias = ggml_flash_attn_ext_has_indexed_bias(dst);
- const ggml_tensor * bias_table = has_bias ? dst->src[6] : nullptr;
- const ggml_tensor * bias_indices = has_bias ? dst->src[7] : nullptr;
+ const bool bias_indexed = ggml_flash_attn_ext_bias_is_indexed(dst);
+ const ggml_tensor * bias = bias_indexed ? dst->src[6] : nullptr;
+ const ggml_tensor * bias_idxs = bias_indexed ? dst->src[7] : nullptr;
// with kv_rows the kernel iterates over the mask columns and reads K/V rows through kv_rows
const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : K->ne[1];
@@ -1461,7 +1461,7 @@ void launch_fattn(
// Only worth the overhead if there is at lease one FATTN_KQ_STRIDE x FATTN_KQ_STRIDE square to be skipped or
// multiple sequences of possibly different lengths, or kv_rows lists that end with padding.
// The scan reads ncols1 mask rows per tile, including the last tile.
- const bool scan_mask = !has_bias && !use_sparse && mask && mask->ne[2] == 1 && mask->ne[1]/ncols1 >= ntiles_x &&
+ const bool scan_mask = !bias_indexed && !use_sparse && mask && mask->ne[2] == 1 && mask->ne[1]/ncols1 >= ntiles_x &&
n_kv_cols % FATTN_KQ_STRIDE == 0 && (Q->ne[1] >= 1024 || Q->ne[3] > 1 || kv_rows);
if (scan_mask) {
const int64_t s31 = mask->nb[1] / sizeof(half2);
@@ -1621,11 +1621,11 @@ void launch_fattn(
mask ? int32_t(mask->nb[1] / sizeof(half)) : 0,
mask ? int64_t(mask->nb[2] / sizeof(half)) : 0,
mask ? int64_t(mask->nb[3] / sizeof(half)) : 0,
- fattn_indexed_bias{has_bias ? (const float *) bias_table->data : nullptr,
- has_bias ? (const int32_t *) bias_indices->data : nullptr,
- has_bias ? bias_table->ne[0] : 0,
- has_bias ? bias_indices->ne[0] : 0,
- has_bias ? bias_table->ne[0]*bias_table->ne[1] : 0}
+ fattn_bias_indexed{bias_indexed ? (const float *) bias->data : nullptr,
+ bias_indexed ? (const int32_t *) bias_idxs->data : nullptr,
+ bias_indexed ? bias->ne[0] : 0,
+ bias_indexed ? bias_idxs->ne[0] : 0,
+ bias_indexed ? bias->ne[0]*bias->ne[1] : 0}
);
CUDA_CHECK(cudaGetLastError());
diff --git a/ggml/src/ggml-cuda/fattn-mma-f16.cuh b/ggml/src/ggml-cuda/fattn-mma-f16.cuh
index f2118d5fe..8c0e9328c 100644
--- a/ggml/src/ggml-cuda/fattn-mma-f16.cuh
+++ b/ggml/src/ggml-cuda/fattn-mma-f16.cuh
@@ -1866,7 +1866,7 @@ static __global__ void flash_attn_ext_f16(
const int32_t s21, const int32_t s22, const int64_t s23,
const int32_t ne31, const int32_t ne32, const int32_t ne33,
const int32_t s31, const int64_t s32, const int64_t s33,
- const fattn_indexed_bias bias) {
+ const fattn_bias_indexed bias) {
GGML_UNUSED(bias); // biased ops are never dispatched to the mma kernel
ggml_cuda_pdl_sync(); // TODO optimize placement
#if defined(FLASH_ATTN_AVAILABLE) && (defined(VOLTA_MMA_AVAILABLE) || defined(TURING_MMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE))
diff --git a/ggml/src/ggml-cuda/fattn-tile.cuh b/ggml/src/ggml-cuda/fattn-tile.cuh
index d224c1561..83eb95eda 100644
--- a/ggml/src/ggml-cuda/fattn-tile.cuh
+++ b/ggml/src/ggml-cuda/fattn-tile.cuh
@@ -387,7 +387,7 @@ bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_ten
// Function that performs a single iteration in for the KQ matrix multiplication:
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int nbatch_fa, int nbatch_K,
- bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_K>
+ bool use_logit_softcap, bool oob_check, bool bias_indexed, bool use_sparse, typename T_vec_dot, typename T_K>
static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
T_vec_dot * const Q_tmp,
const T_K * const __restrict__ K_h2,
@@ -406,7 +406,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
constexpr int np = nwarps > ncols ? nwarps/ncols : 1; // number of parallel warps per Q column
constexpr int stride_tile_K = std::is_same_v<T_vec_dot, half2> ? nbatch_K/2 + cpy_ne : nbatch_K + cpy_ne;
- flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_K, T_vec_dot>
+ flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, use_sparse, bias_indexed || use_sparse, false, T_K, T_vec_dot>
(K_h2 + k_KQ_0/2, KV_tmp, nbatch_K/2, stride_K2, k_VKQ_0, k_VKQ_sup, rows);
__syncthreads();
@@ -464,7 +464,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
// Function that performs a single iteration of the main loop over up to nbatch_fa tokens.
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int DV, int nbatch_fa, int nbatch_K,
- bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
+ bool use_logit_softcap, bool oob_check, bool bias_indexed, bool use_sparse, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
static __device__ __forceinline__ void flash_attn_tile_iter(
T_vec_dot * const Q_tmp,
const T_K * const __restrict__ K_h2,
@@ -484,7 +484,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
const int k_VKQ_0,
const int k_VKQ_max,
const int col_Q_0,
- const fattn_indexed_bias bias,
+ const fattn_bias_indexed bias,
const int32_t * const __restrict__ rows) {
constexpr int cpy_nb = 16;
constexpr int cpy_ne = cpy_nb / 4;
@@ -517,12 +517,12 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
constexpr int nbatch_K_last = DKQ % nbatch_K;
#pragma unroll
for (int k_KQ_0 = 0; k_KQ_0 < DKQ - nbatch_K_last; k_KQ_0 += nbatch_K) {
- flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
+ flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>(
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
}
if (nbatch_K_last > 0) {
constexpr int k_KQ_0 = DKQ - nbatch_K_last;
- flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
+ flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, bias_indexed, use_sparse>(
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
}
@@ -546,11 +546,11 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
}
if (!oob_check || i_KQ < k_VKQ_sup) {
- if (indexed_bias || ncols2 > 1 || mask) {
+ if (bias_indexed || ncols2 > 1 || mask) {
const int k = use_sparse ? rows[k_VKQ_0 + i_KQ] : k_VKQ_0 + i_KQ;
__builtin_assume(k >= 0); // avoid sign extension and register spills on RDNA
const float m = __half2float(mask[int64_t(j)*stride_mask + k]);
- KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*(indexed_bias ? bias.add(m, j, k) : m);
+ KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*(bias_indexed ? bias.add(m, j, k) : m);
}
KQ_max_new[jc0] = fmaxf(KQ_max_new[jc0], KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] + FATTN_KQ_MAX_OFFSET);
@@ -633,7 +633,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
constexpr int stride_tile_V = std::is_same_v<T_vec_dot, half2> ? DV/2 : DV;
#pragma unroll
for (int k0 = 0; k0 < nbatch_fa; k0 += nbatch_V) {
- flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_V, T_vec_dot>
+ flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, use_sparse, bias_indexed || use_sparse, false, T_V, T_vec_dot>
(V_h2, KV_tmp, DV/2, stride_V2, k_VKQ_0 + k0, k_VKQ_sup - k0, rows);
__syncthreads();
@@ -703,7 +703,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
}
}
-template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool indexed_bias, ggml_type type_K, ggml_type type_V, bool use_kv_rows, bool use_sparse = false> // D == head size
+template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool bias_indexed, ggml_type type_K, ggml_type type_V, bool use_kv_rows, bool use_sparse = false> // D == head size
__launch_bounds__(ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols1*ncols2), ggml_cuda_fattn_tile_get_occupancy(DKQ, DV, ncols1*ncols2))
static __global__ void flash_attn_tile(
const float2 * Q_ptr,
@@ -728,7 +728,7 @@ static __global__ void flash_attn_tile(
const int32_t s21, const int32_t s22, const int64_t s23,
const int32_t ne31, const int32_t ne32, const int32_t ne33,
const int32_t s31, const int64_t s32, const int64_t s33,
- const fattn_indexed_bias bias) {
+ const fattn_bias_indexed bias) {
#ifdef FLASH_ATTN_AVAILABLE
using T_K = decltype(ggml_cuda_fattn_kv_dtype<type_K>());
using T_V = decltype(ggml_cuda_fattn_kv_dtype<type_V>());
@@ -751,7 +751,7 @@ static __global__ void flash_attn_tile(
}
#endif // GCN
- static_assert(!use_sparse || (ncols1 == 1 && !indexed_bias && !use_kv_rows), "bad sparse configuration");
+ static_assert(!use_sparse || (ncols1 == 1 && !bias_indexed && !use_kv_rows), "bad sparse configuration");
if ((use_logit_softcap && !(DV == 128 || DV == 256 || DV == 512))) {
GGML_UNUSED_VARS(Q, K, V, mask, sinks, KV_max, dst, dst_meta, scale,
@@ -793,13 +793,13 @@ static __global__ void flash_attn_tile(
const int32_t * rows = use_sparse ? KV_max + list*ne11 :
use_kv_rows ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
__builtin_assume(!use_kv_rows || rows != nullptr);
- const fattn_indexed_bias head_bias = indexed_bias ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias;
+ const fattn_bias_indexed head_bias = bias_indexed ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias;
const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
const float2 * Q_f2 = Q + s03*sequence + s02* head0;
const T_K * K_h2 = K + s13*sequence + s12*(head0 / gqa_ratio);
const T_V * V_h2 = V + s23*sequence + s22*(head0 / gqa_ratio); // K and V have same shape
- const half * maskh = indexed_bias || mask ? mask + s33*(sequence % ne33) + s32*(head0 % ne32) : nullptr;
+ const half * maskh = bias_indexed || mask ? mask + s33*(sequence % ne33) + s32*(head0 % ne32) : nullptr;
const int stride_K2 = s11;
const int stride_V2 = s21;
@@ -899,14 +899,14 @@ static __global__ void flash_attn_tile(
int k_VKQ_0 = blockIdx.y*nbatch_fa;
while (k_VKQ_0 <= k_VKQ_max - nbatch_fa) {
constexpr bool oob_check = false;
- flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
+ flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
k_VKQ_0 += gridDim.y*nbatch_fa;
}
if (k_VKQ_0 < k_VKQ_max) {
constexpr bool oob_check = true;
- flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
+ flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
}
@@ -914,7 +914,7 @@ static __global__ void flash_attn_tile(
// Branch without out-of-bounds checks.
for (int k_VKQ_0 = blockIdx.y*nbatch_fa; k_VKQ_0 < k_VKQ_max; k_VKQ_0 += gridDim.y*nbatch_fa) {
constexpr bool oob_check = false;
- flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
+ flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, bias_indexed, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
}
@@ -1088,7 +1088,7 @@ static __global__ void flash_attn_tile(
#endif // FLASH_ATTN_AVAILABLE
}
-template <int DKQ, int DV, int ncols2, bool use_logit_softcap, ggml_type type_K, ggml_type type_V, bool indexed_bias, bool use_kv_rows>
+template <int DKQ, int DV, int ncols2, bool use_logit_softcap, ggml_type type_K, ggml_type type_V, bool bias_indexed, bool use_kv_rows>
static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
using T_K = decltype(ggml_cuda_fattn_kv_dtype<type_K>());
using T_V = decltype(ggml_cuda_fattn_kv_dtype<type_V>());
@@ -1102,7 +1102,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr size_t nbytes_shared = 0;
#ifdef GGML_USE_HIP
- if constexpr (!indexed_bias && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
+ if constexpr (!bias_indexed && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
if (ggml_cuda_flash_attn_ext_tile_shall_use_sparse(cc, dst, DKQ, DV)) {
const int nwarps = ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols2, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, ncols2, cc);
@@ -1115,13 +1115,13 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
// Limit indexed bias tile widths to avoid spilling registers on AMD.
// TODO: select the tile width in one function.
- constexpr int max_cols = !indexed_bias ? 64 : DKQ == 64 ? 32 : use_kv_rows ? 16 : 8;
+ constexpr int max_cols = !bias_indexed ? 64 : DKQ == 64 ? 32 : use_kv_rows ? 16 : 8;
if constexpr (DKQ <= 128 && max_cols >= 64) {
if (!(amd_mfma_available(cc) && ncols2 == 1) && Q->ne[1] > 32/ncols2) {
constexpr int cols_per_block = 64;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1136,7 +1136,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr int cols_per_block = 32;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1148,7 +1148,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr int cols_per_block = 16;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1160,7 +1160,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr int cols_per_block = 8;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1172,7 +1172,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr int cols_per_block = 4;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1183,7 +1183,7 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr int cols_per_block = 2;
const int nwarps = ggml_cuda_fattn_tile_get_nthreads (DKQ, DV, cols_per_block, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, cols_per_block, cc);
- fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, indexed_bias, type_K, type_V, use_kv_rows>;
+ fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, cols_per_block/ncols2, ncols2, use_logit_softcap, bias_indexed, type_K, type_V, use_kv_rows>;
launch_fattn<DV, cols_per_block/ncols2, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, false, warp_size);
return;
@@ -1213,7 +1213,7 @@ static void launch_fattn_tile_switch_ncols2(ggml_backend_cuda_context & ctx, ggm
const bool use_gqa_opt = mask && mask->ne[2] == 1 && max_bias == 0.0f && Q->ne[1] <= gqa_limit && K->ne[1] % FATTN_KQ_STRIDE == 0;
if constexpr (DKQ == 64 || DKQ == 128) {
- if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
+ if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
if (dst->src[5]) {
launch_fattn_tile_switch_ncols1<DKQ, DV, 1, use_logit_softcap, type_K, type_V, true, true>(ctx, dst);
} else {
diff --git a/ggml/src/ggml-cuda/fattn-vec.cuh b/ggml/src/ggml-cuda/fattn-vec.cuh
index bbe6901ee..1811e8b37 100644
--- a/ggml/src/ggml-cuda/fattn-vec.cuh
+++ b/ggml/src/ggml-cuda/fattn-vec.cuh
@@ -16,7 +16,7 @@ static constexpr __device__ int ggml_cuda_fattn_vec_get_nthreads_device() {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpass-failed"
#endif // __clang__
-template<int D, int ncols, ggml_type type_K, ggml_type type_V, bool use_logit_softcap, bool indexed_bias, bool use_kv_rows> // D == head size
+template<int D, int ncols, ggml_type type_K, ggml_type type_V, bool use_logit_softcap, bool bias_indexed, bool use_kv_rows> // D == head size
__launch_bounds__(ggml_cuda_fattn_vec_get_nthreads_device(), 1)
static __global__ void flash_attn_ext_vec(
const float2 * Q_ptr,
@@ -41,7 +41,7 @@ static __global__ void flash_attn_ext_vec(
const int32_t s21, const int32_t s22, const int64_t s23,
const int32_t ne31, const int32_t ne32, const int32_t ne33,
const int32_t s31, const int64_t s32, const int64_t s33,
- const fattn_indexed_bias bias) {
+ const fattn_bias_indexed bias) {
ggml_cuda_pdl_lc();
#ifdef FLASH_ATTN_AVAILABLE
const float2 * GGML_CUDA_RESTRICT Q = Q_ptr;
@@ -75,7 +75,7 @@ static __global__ void flash_attn_ext_vec(
#ifdef GGML_USE_HIP
#ifdef RDNA
- constexpr int nthreads_KQ_q = indexed_bias && type_K == GGML_TYPE_F8_E4M3 ? 16 : 2;
+ constexpr int nthreads_KQ_q = bias_indexed && type_K == GGML_TYPE_F8_E4M3 ? 16 : 2;
#else
constexpr int nthreads_KQ_q = type_K == GGML_TYPE_F8_E4M3 && D == 256 && ncols == 2 ? 16 : 4;
#endif // RDNA
@@ -107,13 +107,13 @@ static __global__ void flash_attn_ext_vec(
const int sequence = blockIdx.z / ne02;
const int head = blockIdx.z - sequence*ne02;
- const fattn_indexed_bias head_bias = indexed_bias ? bias.for_head(int64_t(sequence)*ne01.z, head) : bias;
+ const fattn_bias_indexed head_bias = bias_indexed ? bias.for_head(int64_t(sequence)*ne01.z, head) : bias;
const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
Q += s03*sequence + s02* head + s01*ic0;
K += s13*sequence + s12*(head / gqa_ratio);
V += s23*sequence + s22*(head / gqa_ratio);
- const half * maskh = indexed_bias || mask ? mask + s33*(sequence % ne33) + s32*(head % ne32) + s31*ic0 : nullptr;
+ const half * maskh = bias_indexed || mask ? mask + s33*(sequence % ne33) + s32*(head % ne32) + s31*ic0 : nullptr;
const float slope = get_alibi_slope(max_bias, head, n_head_log2, m0, m1);
@@ -305,7 +305,7 @@ static __global__ void flash_attn_ext_vec(
sum = logit_softcap*tanhf(sum);
}
- if constexpr (!indexed_bias) {
+ if constexpr (!bias_indexed) {
if (mask && (ncols == 1 || ic0 + j < int(ne01.z))) {
sum += slope*__half2float(maskh[j*ne11 + i_KQ]);
}
@@ -321,14 +321,14 @@ static __global__ void flash_attn_ext_vec(
#pragma unroll
for (int j = 0; j < ncols; ++j) {
- if constexpr (indexed_bias) {
+ if constexpr (bias_indexed) {
const int k = k_VKQ_0 + tid;
const float m = head_bias.add(__half2float(maskh[j*ne11 + tid]), ic0 + j, k);
KQ_reg[j] += slope*m;
KQ_max_new[j] = fmaxf(KQ_max[j], KQ_reg[j] + FATTN_KQ_MAX_OFFSET);
}
#pragma unroll
- for (int offset = indexed_bias ? 1 : nthreads_KQ; offset < WARP_SIZE; offset <<= 1) {
+ for (int offset = bias_indexed ? 1 : nthreads_KQ; offset < WARP_SIZE; offset <<= 1) {
KQ_max_new[j] = fmaxf(KQ_max_new[j], ggml_cuda_shfl_xor(KQ_max_new[j], offset, WARP_SIZE));
}
const float KQ_max_scale = expf(KQ_max[j] - KQ_max_new[j]);
@@ -575,7 +575,7 @@ void ggml_cuda_flash_attn_ext_vec_case_impl(ggml_backend_cuda_context & ctx, ggm
const int nwarps = nthreads / WARP_SIZE;
fattn_kernel_t fattn_kernel = flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, false, false>;
if constexpr (cols_per_block == 1 && (D == 64 || D == 128) && !use_logit_softcap) {
- if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
+ if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
fattn_kernel = dst->src[5] ? flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, true, true>
: flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, true, false>;
}
diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cu
index f53881d58..a0a58ef99 100644
--- a/ggml/src/ggml-cuda/fattn.cu
+++ b/ggml/src/ggml-cuda/fattn.cu
@@ -654,7 +654,7 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
const ggml_tensor * mask = dst->src[3];
const ggml_tensor * kv_rows = dst->src[5];
- if (!ggml_validate_flash_attn_ext_indexed_bias(dst)) {
+ if (ggml_flash_attn_ext_has_bias(dst) && !ggml_flash_attn_ext_bias_is_indexed(dst)) {
return BEST_FATTN_KERNEL_NONE;
}
@@ -739,7 +739,7 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
}
// the tile and vec kernels implement the indexed bias, the mma kernel does not
- if (ggml_flash_attn_ext_has_indexed_bias(dst)) {
+ if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
float logit_softcap;
memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
if (Q->ne[0] != 128 && !(Q->ne[0] == 64 && logit_softcap == 0.0f)) {
diff --git a/ggml/src/ggml-et/ggml-et.cpp b/ggml/src/ggml-et/ggml-et.cpp
index 7abf7641a..2e5c6de7a 100644
--- a/ggml/src/ggml-et/ggml-et.cpp
+++ b/ggml/src/ggml-et/ggml-et.cpp
@@ -1281,7 +1281,7 @@ static bool ggml_backend_et_device_supports_op(ggml_backend_dev_t dev, const ggm
if (op->type == GGML_TYPE_F32 && op->src[0] && op->src[0]->type == GGML_TYPE_F32 && op->src[1] &&
(op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) && op->src[2] &&
(op->src[2]->type == GGML_TYPE_F32 || op->src[2]->type == GGML_TYPE_F16) && op->src[4] == nullptr && op->src[5] == nullptr &&
- !ggml_flash_attn_ext_has_indexed_bias(op) &&
+ !ggml_flash_attn_ext_has_bias(op) &&
ggml_is_contiguous_rows(op) && ggml_is_contiguous_rows(op->src[0])) {
float max_bias = 0.0f;
float logit_softcap = 0.0f;
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index 1baa6fe63..3fa04be92 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -331,8 +331,8 @@ static bool htp_kparams_solve_flash_attn(
return false;
}
- // K/V row lists and indexed bias are not supported
- if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
+ // K/V row lists and bias are not supported
+ if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
return false;
}
diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h
index 0499b7660..d7e7ed807 100644
--- a/ggml/src/ggml-impl.h
+++ b/ggml/src/ggml-impl.h
@@ -114,6 +114,22 @@ static inline const struct ggml_tensor * ggml_get_src_scale(const struct ggml_te
}
}
+static inline bool ggml_flash_attn_ext_has_bias(const struct ggml_tensor * a) {
+ if (a->op != GGML_OP_FLASH_ATTN_EXT) {
+ return false;
+ }
+
+ return a->src[6] != NULL;
+}
+
+static inline bool ggml_flash_attn_ext_bias_is_indexed(const struct ggml_tensor * a) {
+ if (!ggml_flash_attn_ext_has_bias(a)) {
+ return false;
+ }
+
+ return a->src[7] != NULL;
+}
+
static inline float ggml_compute_softplus_f32(float input) {
return (input > 20.0f) ? input : log1pf(expf(input));
}
@@ -124,8 +140,6 @@ GGML_API void ggml_rope_yarn_corr_dims(
GGML_API bool ggml_rope_get_truncate(const struct ggml_tensor * a);
-GGML_API bool ggml_validate_flash_attn_ext_indexed_bias(const struct ggml_tensor * a);
-
//
// logging
//
diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m
index f7f05b6bb..e87187a12 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.m
+++ b/ggml/src/ggml-metal/ggml-metal-device.m
@@ -1751,7 +1751,7 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
case GGML_OP_ROLL:
return ggml_is_contiguous(op->src[0]);
case GGML_OP_FLASH_ATTN_EXT:
- if (ggml_flash_attn_ext_has_indexed_bias(op)) {
+ if (ggml_flash_attn_ext_has_bias(op)) {
return false;
}
// K/V row lists (ggml_flash_attn_ext_rows): one list of cache rows per q slice
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cpp
index 40d2934f1..d980591ef 100644
--- a/ggml/src/ggml-opencl/ggml-opencl.cpp
+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp
@@ -13222,8 +13222,8 @@ static bool ggml_opencl_supports_op(ggml_backend_dev_t dev, const struct ggml_te
case GGML_OP_MEAN:
return op->src[0]->type == GGML_TYPE_F32;
case GGML_OP_FLASH_ATTN_EXT: {
- // K/V row lists and indexed bias are not supported
- if (op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op)) {
+ // K/V row lists and bias are not supported
+ if (op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op)) {
return false;
}
#ifdef GGML_OPENCL_USE_ADRENO_KERNELS
diff --git a/ggml/src/ggml-openvino/ggml-openvino.cpp b/ggml/src/ggml-openvino/ggml-openvino.cpp
index f018c271e..7f592b3c4 100644
--- a/ggml/src/ggml-openvino/ggml-openvino.cpp
+++ b/ggml/src/ggml-openvino/ggml-openvino.cpp
@@ -1902,8 +1902,8 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
return {false, "FLASH_ATTN_EXT gemma3n pattern is not supported"};
}
- if (ggml_flash_attn_ext_has_indexed_bias(op)) {
- return {false, "FLASH_ATTN_EXT with indexed bias is not supported"};
+ if (ggml_flash_attn_ext_has_bias(op)) {
+ return {false, "FLASH_ATTN_EXT with bias is not supported"};
}
// K/V row lists (ggml_flash_attn_ext_rows) are not supported
diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp
index 359b9ccda..8dfcebf41 100644
--- a/ggml/src/ggml-sycl/ggml-sycl.cpp
+++ b/ggml/src/ggml-sycl/ggml-sycl.cpp
@@ -7789,8 +7789,8 @@ static bool do_ggml_backend_sycl_device_supports_op(ggml_backend_dev_t dev, cons
case GGML_OP_SOLVE_TRI:
return op->src[0]->ne[0] <= SYCL_SOLVE_TRI_MAX_N && op->src[1]->ne[0] <= SYCL_SOLVE_TRI_MAX_K;
case GGML_OP_FLASH_ATTN_EXT:
- // K/V row lists and indexed bias are not supported
- return op->src[5] == nullptr && !ggml_flash_attn_ext_has_indexed_bias(op) && ggml_sycl_flash_attn_ext_supported(device, op);
+ // K/V row lists and bias are not supported
+ return op->src[5] == nullptr && !ggml_flash_attn_ext_has_bias(op) && ggml_sycl_flash_attn_ext_supported(device, op);
default:
return false;
}
diff --git a/ggml/src/ggml-virtgpu/ggml-backend-device.cpp b/ggml/src/ggml-virtgpu/ggml-backend-device.cpp
index e4d84fc98..1449c1872 100644
--- a/ggml/src/ggml-virtgpu/ggml-backend-device.cpp
+++ b/ggml/src/ggml-virtgpu/ggml-backend-device.cpp
@@ -30,7 +30,7 @@ static void ggml_backend_remoting_device_get_memory(ggml_backend_dev_t dev, size
}
static bool ggml_backend_remoting_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
- if (ggml_flash_attn_ext_has_indexed_bias(op)) {
+ if (ggml_flash_attn_ext_has_bias(op)) {
return false;
}
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-debug.cpp b/ggml/src/ggml-vulkan/ggml-vulkan-debug.cpp
index 42f058bd8..b2cdb87b7 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-debug.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-debug.cpp
@@ -1016,7 +1016,7 @@ void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph
const float * params = (const float *)tensor->op_params;
tensor_clone = ggml_flash_attn_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], params[0], params[1], params[2]);
if (src_clone[4]) {
- ggml_flash_attn_ext_add_sinks(tensor_clone, src_clone[4]);
+ ggml_flash_attn_ext_set_sinks(tensor_clone, src_clone[4]);
}
} else if (tensor->op == GGML_OP_MUL_MAT) {
tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index a32f93eb0..afb5107f6 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -9070,10 +9070,10 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
const uint32_t nem2 = mask ? mask->ne[2] : 0;
const uint32_t nem3 = mask ? mask->ne[3] : 0;
- const bool has_bias = ggml_flash_attn_ext_has_indexed_bias(dst);
- const ggml_tensor * bias_table = has_bias ? dst->src[6] : nullptr;
- const ggml_tensor * bias_idx = has_bias ? dst->src[7] : nullptr;
- const uint32_t bias_bins = has_bias ? (uint32_t)bias_table->ne[0] : 0;
+ const bool bias_indexed = ggml_flash_attn_ext_bias_is_indexed(dst);
+ const ggml_tensor * bias = bias_indexed ? dst->src[6] : nullptr;
+ const ggml_tensor * bias_idxs = bias_indexed ? dst->src[7] : nullptr;
+ const uint32_t bias_bins = bias_indexed ? (uint32_t)bias->ne[0] : 0;
const uint32_t HSK = nek0;
const uint32_t HSV = nev0;
@@ -9514,8 +9514,8 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
vk_subbuffer dst_buf = ggml_vk_tensor_subbuffer(ctx, dst);
vk_subbuffer mask_buf = mask ? ggml_vk_tensor_subbuffer(ctx, mask) : q_buf;
vk_subbuffer sinks_buf = sinks ? ggml_vk_tensor_subbuffer(ctx, sinks) : q_buf;
- vk_subbuffer bias_table_buf = bias_table ? ggml_vk_tensor_subbuffer(ctx, bias_table) : q_buf;
- vk_subbuffer bias_idx_buf = bias_idx ? ggml_vk_tensor_subbuffer(ctx, bias_idx) : q_buf;
+ vk_subbuffer bias_buf = bias ? ggml_vk_tensor_subbuffer(ctx, bias) : q_buf;
+ vk_subbuffer bias_idxs_buf = bias_idxs ? ggml_vk_tensor_subbuffer(ctx, bias_idxs) : q_buf;
vk_subbuffer mask_opt_buf = use_mask_opt ? ggml_vk_subbuffer(ctx, ctx->prealloc_y, 0) : q_buf;
vk_subbuffer sparse_buf = use_sparse ? ggml_vk_subbuffer(ctx, ctx->prealloc_y, 0) : q_buf;
if (use_mask_opt || use_sparse) {
@@ -9670,7 +9670,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
ggml_pipeline_request_descriptor_sets(ctx, xe_fa_pipeline_dual_phases.first, 1);
ggml_vk_dispatch_pipeline(ctx, subctx, xe_fa_pipeline_dual_phases.first,
- { q_temp_buf, k_buf, mask_buf, p_temp_buf, max_temp_buf, bias_table_buf, bias_idx_buf },
+ { q_temp_buf, k_buf, mask_buf, p_temp_buf, max_temp_buf, bias_buf, bias_idxs_buf },
pc_xe_fa, { (uint32_t)ph1_wg, (uint32_t)nek1, (uint32_t)neq3 });
ggml_vk_sync_buffers(ctx, subctx);
@@ -9683,7 +9683,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
} else {
ggml_pipeline_request_descriptor_sets(ctx, xe_fa_pipeline, 1);
ggml_vk_dispatch_pipeline(ctx, subctx, xe_fa_pipeline,
- { q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, bias_table_buf, bias_idx_buf },
+ { q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, bias_buf, bias_idxs_buf },
pc_xe_fa, { (uint32_t)neq2, (uint32_t)neq1, (uint32_t)neq3 });
}
} else if (split_k > 1) {
@@ -9705,7 +9705,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
vk_subbuffer split_k_buf = ggml_vk_subbuffer(ctx, ctx->prealloc_split_k, 0);
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
- {q_buf, k_buf, v_buf, mask_buf, sinks_buf, split_k_buf, mask_opt_buf, sparse_buf, bias_table_buf, bias_idx_buf},
+ {q_buf, k_buf, v_buf, mask_buf, sinks_buf, split_k_buf, mask_opt_buf, sparse_buf, bias_buf, bias_idxs_buf},
pc, { dispatch_x, workgroups_y, workgroups_z });
ggml_vk_sync_buffers(ctx, subctx);
@@ -9720,7 +9720,7 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const
workgroups_x *= pipeline->wg_denoms[0];
}
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
- {q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, mask_opt_buf, sparse_buf, bias_table_buf, bias_idx_buf},
+ {q_buf, k_buf, v_buf, mask_buf, sinks_buf, dst_buf, mask_opt_buf, sparse_buf, bias_buf, bias_idxs_buf},
pc, { workgroups_x, workgroups_y, workgroups_z });
}
@@ -16914,7 +16914,10 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
if (op->src[5] != nullptr) {
return false;
}
- if (ggml_flash_attn_ext_has_indexed_bias(op) &&
+ if (ggml_flash_attn_ext_has_bias(op) && !ggml_flash_attn_ext_bias_is_indexed(op)) {
+ return false;
+ }
+ if (ggml_flash_attn_ext_bias_is_indexed(op) &&
((uint64_t)ggml_nelements(op->src[6]) > UINT32_MAX || (uint64_t)ggml_nelements(op->src[7]) > UINT32_MAX)) {
return false;
}
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
index 62c571b0b..b95419322 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
@@ -5275,8 +5275,8 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
{
// conservative support checks for whether the more resource-intensive shader paths
// can be used, to avoid cases where flash_attn is assigned to the CPU later on
- // K/V row lists and indexed bias are not supported
- supports_op = op->src[5] == nullptr && !ggml_flash_attn_ext_has_indexed_bias(op) &&
+ // K/V row lists and bias are not supported
+ supports_op = op->src[5] == nullptr && !ggml_flash_attn_ext_has_bias(op) &&
src0->type == GGML_TYPE_F32 &&
(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16 ||
src1->type == GGML_TYPE_Q4_0 || src1->type == GGML_TYPE_Q8_0) &&
diff --git a/ggml/src/ggml-zdnn/fattn.cpp b/ggml/src/ggml-zdnn/fattn.cpp
index 5e74e67a4..4749c7f4b 100644
--- a/ggml/src/ggml-zdnn/fattn.cpp
+++ b/ggml/src/ggml-zdnn/fattn.cpp
@@ -21,7 +21,7 @@ bool ggml_zdnn_supports_flash_attn_ext(const ggml_backend_zdnn_device_context *
memcpy(&logit_softcap, (const float *) op->op_params + 2, sizeof(float));
// sinks, alibi, softcap, sparse k/v sets and k/v row lists are not implemented
- if (op->src[4] != nullptr || op->src[5] != nullptr || ggml_flash_attn_ext_has_indexed_bias(op) || max_bias != 0.0f || logit_softcap != 0.0f || ggml_get_op_params_i32(op, 4) != 0) {
+ if (op->src[4] != nullptr || op->src[5] != nullptr || ggml_flash_attn_ext_has_bias(op) || max_bias != 0.0f || logit_softcap != 0.0f || ggml_get_op_params_i32(op, 4) != 0) {
return false;
}
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index 7da6637bd..6a0835b62 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -5615,7 +5615,7 @@ void ggml_flash_attn_ext_set_n_kv_max(
ggml_set_op_params_i32(a, 4, n_kv_max);
}
-void ggml_flash_attn_ext_add_sinks(
+void ggml_flash_attn_ext_set_sinks(
struct ggml_tensor * a,
struct ggml_tensor * sinks) {
if (!sinks) {
@@ -5624,49 +5624,100 @@ void ggml_flash_attn_ext_add_sinks(
}
GGML_ASSERT(a->op == GGML_OP_FLASH_ATTN_EXT);
- GGML_ASSERT(a->src[4] == NULL);
GGML_ASSERT(a->src[0]->ne[2] == sinks->ne[0]);
GGML_ASSERT(sinks->type == GGML_TYPE_F32);
a->src[4] = sinks;
}
+// deprecated
+void ggml_flash_attn_ext_add_sinks(
+ struct ggml_tensor * a,
+ struct ggml_tensor * sinks) {
+ ggml_flash_attn_ext_set_sinks(a, sinks);
+}
+
+// deprecated
bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a) {
- return a->op == GGML_OP_FLASH_ATTN_EXT && ggml_get_op_params_i32(a, 5) != 0;
+ return ggml_flash_attn_ext_bias_is_indexed(a);
}
-bool ggml_validate_flash_attn_ext_indexed_bias(const struct ggml_tensor * a) {
+static bool ggml_flash_attn_ext_set_bias_validate(const struct ggml_tensor * a) {
+ GGML_ASSERT(a->op == GGML_OP_FLASH_ATTN_EXT);
+
+ if (!ggml_flash_attn_ext_has_bias(a)) {
+ return a->src[7] == NULL;
+ }
+
+ const struct ggml_tensor * q = a->src[0];
+ const struct ggml_tensor * k = a->src[1];
+ const struct ggml_tensor * mask = a->src[3];
+ const struct ggml_tensor * rows = a->src[5];
+ const struct ggml_tensor * bias = a->src[6];
+ const struct ggml_tensor * bias_idxs = a->src[7];
+
+ if (!q || !k || !a->src[2] || !mask) {
+ return false;
+ }
+ if (mask->type != GGML_TYPE_F16 || !ggml_is_contiguous(mask)) {
+ return false;
+ }
+ if (mask->ne[0] != (rows ? rows->ne[0] : k->ne[1]) || mask->ne[1] != q->ne[1]) {
+ return false;
+ }
+ if (mask->ne[2] != 1 || mask->ne[3] != q->ne[3]) {
+ return false;
+ }
+ if (rows && (rows->type != GGML_TYPE_I32 || !ggml_is_contiguous(rows) ||
+ !ggml_is_matrix(rows) || rows->ne[1] != q->ne[3])) {
+ return false;
+ }
+ if (bias->type != GGML_TYPE_F32 || !ggml_is_contiguous(bias)) {
+ return false;
+ }
+
+ if (!bias_idxs) {
+ // TODO: support dense bias in the backends
+ GGML_ASSERT(false);
+ return false;
+ }
+
+ if (bias->ne[0] == 0 || bias->ne[1] != q->ne[2] || bias->ne[3] != 1) {
+ return false;
+ }
+ if (q->ne[1] != 0 && q->ne[3] > INT64_MAX/q->ne[1]) {
+ return false;
+ }
+ if (bias->ne[2] != q->ne[1]*q->ne[3]) {
+ return false;
+ }
+ if (bias_idxs->type != GGML_TYPE_I32 || !ggml_is_contiguous(bias_idxs) || !ggml_is_matrix(bias_idxs)) {
+ return false;
+ }
+ if (bias_idxs->ne[0] != mask->ne[0] || bias_idxs->ne[1] != bias->ne[2]) {
+ return false;
+ }
+ return true;
+}
+
+void ggml_flash_attn_ext_set_bias(
+ struct ggml_tensor * a,
+ struct ggml_tensor * bias,
+ struct ggml_tensor * bias_idxs) {
GGML_ASSERT(a->op == GGML_OP_FLASH_ATTN_EXT);
- const struct ggml_tensor * table = a->src[6];
- const struct ggml_tensor * indices = a->src[7];
- if (!ggml_flash_attn_ext_has_indexed_bias(a)) {
- return !table && !indices;
- }
-
- const struct ggml_tensor * q = a->src[0];
- const struct ggml_tensor * k = a->src[1];
- const struct ggml_tensor * mask = a->src[3];
- const struct ggml_tensor * rows = a->src[5];
- return q && k && a->src[2] && mask && table && indices &&
- mask->type == GGML_TYPE_F16 && ggml_is_contiguous(mask) &&
- mask->ne[0] == (rows ? rows->ne[0] : k->ne[1]) && mask->ne[1] == q->ne[1] &&
- mask->ne[2] == 1 && mask->ne[3] == q->ne[3] &&
- (!rows || (rows->type == GGML_TYPE_I32 && ggml_is_contiguous(rows) && ggml_is_matrix(rows) && rows->ne[1] == q->ne[3])) &&
- table->type == GGML_TYPE_F32 && ggml_is_contiguous(table) && table->ne[0] > 0 && table->ne[1] == q->ne[2] &&
- (q->ne[1] == 0 || q->ne[3] <= INT64_MAX/q->ne[1]) && table->ne[2] == q->ne[1]*q->ne[3] && table->ne[3] == 1 &&
- indices->type == GGML_TYPE_I32 && ggml_is_contiguous(indices) && ggml_is_matrix(indices) &&
- indices->ne[0] == mask->ne[0] && indices->ne[1] == table->ne[2];
+ a->src[6] = bias;
+ a->src[7] = bias_idxs;
+ GGML_ASSERT(ggml_flash_attn_ext_set_bias_validate(a));
}
+// deprecated
void ggml_flash_attn_ext_add_indexed_bias(
struct ggml_tensor * a,
- struct ggml_tensor * table,
- struct ggml_tensor * indices) {
+ struct ggml_tensor * bias,
+ struct ggml_tensor * bias_idxs) {
GGML_ASSERT(a->op == GGML_OP_FLASH_ATTN_EXT);
- a->src[6] = table;
- a->src[7] = indices;
- ggml_set_op_params_i32(a, 5, table != NULL);
- GGML_ASSERT(ggml_validate_flash_attn_ext_indexed_bias(a));
+ GGML_ASSERT((bias == NULL) == (bias_idxs == NULL));
+ ggml_flash_attn_ext_set_bias(a, bias, bias_idxs);
}
// ggml_flash_attn_back
diff --git a/scripts/hip/gcn-cdna-metrics.py b/scripts/hip/gcn-cdna-metrics.py
index ef4f295a1..a1feeb8e1 100644
--- a/scripts/hip/gcn-cdna-metrics.py
+++ b/scripts/hip/gcn-cdna-metrics.py
@@ -32,28 +32,28 @@ METRIC_NAMES = {'occupancy': 'Occupancy [waves/SIMD]', 'vgpr_spill': 'VGPRs Spil
IGNORED_VGPR = {
'_ZL13rwkv_wkv7_f32ILi128EEviiiiPKfS1_S1_S1_S1_S1_S1_Pf',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi2ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi2ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi4ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi4ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi32ELi2ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi32ELi2ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi4ELi8ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi4ELi8ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi4ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi4ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi8ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi8ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi16ELi4ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi1ELi16ELb0ELb1ELb1EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi1ELi32ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi2ELi16ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi2ELi32ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi4ELi16ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_f16ILi576ELi512ELi8ELi4ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type2ELS0_2ELb0ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type43ELS0_43ELb0ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
- '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type43ELS0_43ELb1ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_indexed_bias',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi2ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi2ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi4ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi16ELi4ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi32ELi2ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi32ELi2ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi4ELi8ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi4ELi8ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi4ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi4ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi8ELb0ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi512ELi512ELi8ELi8ELb1ELb0ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi16ELi4ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi1ELi16ELb0ELb1ELb1EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi1ELi32ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi2ELi16ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi2ELi32ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi4ELi16ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_f16ILi576ELi512ELi8ELi4ELb0ELb1ELb0EL9ggml_type1ELS0_1EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type2ELS0_2ELb0ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type43ELS0_43ELb0ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
+ '_ZL18flash_attn_ext_vecILi256ELi1EL9ggml_type43ELS0_43ELb1ELb0ELb0EEvPK15HIP_vector_typeIfLj2EEPKcS6_PK6__halfPKfPKiSD_PfPS2_ffffjfiS1_IjLj3EEiiiiiiiiiiiliiliiiill18fattn_bias_indexed',
'_ZL21gated_linear_attn_f32ILi128EEviiiifPKfS1_S1_S1_S1_Pf',
}
diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp
index 31200d0d1..948a02ce7 100644
--- a/src/llama-graph.cpp
+++ b/src/llama-graph.cpp
@@ -3147,16 +3147,15 @@ ggml_tensor * llm_graph_context::build_attn_mha(
ggml_tensor * q,
ggml_tensor * k,
ggml_tensor * v,
- ggml_tensor * kq_b,
ggml_tensor * kq_mask,
ggml_tensor * kv_rows,
+ ggml_tensor * bias,
+ ggml_tensor * bias_idxs,
ggml_tensor * sinks,
ggml_tensor * v_mla,
int64_t n_kv_max,
float kq_scale,
int il,
- ggml_tensor * bias_table,
- ggml_tensor * bias_indices,
ggml_tensor * k_scale,
ggml_tensor * v_scale) const {
const bool v_trans = v->nb[1] > v->nb[2];
@@ -3173,28 +3172,12 @@ ggml_tensor * llm_graph_context::build_attn_mha(
k = ggml_permute(ctx0, k, 0, 2, 1, 3);
v = ggml_permute(ctx0, v, 0, 2, 1, 3);
- auto dense_bias_mask = [&]() {
- ggml_tensor * table = ggml_cont(ctx0, ggml_permute(ctx0, bias_table, 1, 0, 2, 3));
- ggml_tensor * bias = ggml_get_rows(ctx0, table, bias_indices);
- bias = ggml_reshape_4d(ctx0, bias, q->ne[2], bias_indices->ne[0], q->ne[1], n_stream);
- bias = ggml_cont(ctx0, ggml_permute(ctx0, bias, 2, 0, 1, 3));
- cb(bias, "kq_b", il);
-
- ggml_tensor * mask = kq_mask->type == GGML_TYPE_F32 ? kq_mask : ggml_cast(ctx0, kq_mask, GGML_TYPE_F32);
- mask = ggml_add(ctx0, bias, mask);
- if (cparams.flash_attn) {
- mask = ggml_cast(ctx0, mask, GGML_TYPE_F16);
- }
- cb(mask, "kq_mask_rel_pos", il);
- return mask;
- };
-
ggml_tensor * cur;
- const bool use_flash_attn = cparams.flash_attn && kq_b == nullptr;
+ const bool use_flash_attn = cparams.flash_attn && (!bias || bias_idxs);
GGML_ASSERT(use_flash_attn || kv_rows == nullptr);
if (use_flash_attn) {
- GGML_ASSERT(kq_b == nullptr && "Flash attention does not support dense KQ bias");
+ GGML_ASSERT(!bias || bias_idxs);
if (k_scale) {
q = ggml_mul(ctx0, q, k_scale);
@@ -3222,21 +3205,12 @@ ggml_tensor * llm_graph_context::build_attn_mha(
}
res->add_fused_node({kv_rows ? LLM_FUSED_OP_FLASH_ATTN_KV_ROWS : LLM_FUSED_OP_FLASH_ATTN, cur, il});
- ggml_flash_attn_ext_add_sinks(cur, sinks);
+ ggml_flash_attn_ext_set_bias(cur, bias, bias_idxs);
+ ggml_flash_attn_ext_set_sinks(cur, sinks);
GGML_ASSERT(n_kv_max >= 0 && n_kv_max <= INT32_MAX);
ggml_flash_attn_ext_set_n_kv_max(cur, static_cast<int32_t>(n_kv_max));
ggml_prec_set_acc(cur, GGML_PREC_F32);
- if (bias_table) {
- ggml_flash_attn_ext_add_indexed_bias(cur, bias_table, bias_indices);
- auto * dev = model.dev_layer(il);
- if ((!cparams.offload_kqv && ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) || !ggml_backend_dev_supports_op(dev, cur)) {
- // Keep ordinary flash attention on the layer device.
- ggml_flash_attn_ext_add_indexed_bias(cur, nullptr, nullptr);
- cur->src[3] = dense_bias_mask();
- }
- }
-
if (v_mla) {
#if 0
// v_mla can be applied as a matrix-vector multiplication with broadcasting across dimension 3 == n_tokens.
@@ -3259,8 +3233,20 @@ ggml_tensor * llm_graph_context::build_attn_mha(
cur = ggml_mul(ctx0, cur, v_scale);
}
} else {
- if (bias_table) {
- kq_mask = dense_bias_mask();
+ // add the bias to the mask
+ if (bias) {
+ ggml_tensor * bias_dense = bias;
+ if (bias_idxs) {
+ // convert the indexed bias to dense
+ ggml_tensor * bias_table = ggml_cont(ctx0, ggml_permute(ctx0, bias, 1, 0, 2, 3));
+ bias_dense = ggml_get_rows(ctx0, bias_table, bias_idxs);
+ bias_dense = ggml_reshape_4d(ctx0, bias_dense, q->ne[2], bias_idxs->ne[0], q->ne[1], n_stream);
+ bias_dense = ggml_permute(ctx0, bias_dense, 2, 0, 1, 3);
+ }
+ bias_dense = ggml_cast(ctx0, bias_dense, kq_mask->type);
+ cb(bias_dense, "kq_bias", il);
+ kq_mask = ggml_add(ctx0, bias_dense, kq_mask);
+ cb(kq_mask, "kq_mask_bias", il);
}
// A cache without an exported scale uses a unit scale.
if ((ggml_needs_scale(k->type) && !k_scale) || (ggml_needs_scale(v->type) && !v_scale)) {
@@ -3299,11 +3285,6 @@ ggml_tensor * llm_graph_context::build_attn_mha(
cb(kq, "kq_scaled_2", il);
}
- if (kq_b) {
- kq = ggml_add(ctx0, kq, kq_b);
- cb(kq, "kq_plus_kq_b", il);
- }
-
kq = ggml_soft_max_ext(ctx0, kq, kq_mask, kq_scale, hparams.f_max_alibi_bias);
ggml_soft_max_add_sinks(kq, sinks);
cb(kq, "kq_soft_max", il);
@@ -3406,7 +3387,7 @@ ggml_tensor * llm_graph_context::build_attn(
kq_b = ggml_pad(ctx0, kq_b, n_kv_pad, 0, 0, 0);
}
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask, kv_rows, kq_b, nullptr, sinks, v_mla, 0, kq_scale, il);
cb(cur, "kqv_out", il);
if (wo) {
@@ -3534,7 +3515,7 @@ ggml_tensor * llm_graph_context::build_attn(
const auto & kq_mask = inp->self_attn_mask.raw;
const auto & kv_rows = inp->self_attn_mask.rows;
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il, nullptr, nullptr,
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask, kv_rows, kq_b, nullptr, sinks, v_mla, 0, kq_scale, il,
mctx_cur->get_k_scale(il), mctx_cur->get_v_scale(il));
cb(cur, "kqv_out", il);
@@ -3616,7 +3597,7 @@ ggml_tensor * llm_graph_context::build_attn(
ggml_tensor * k = mctx_cur->get_k(ctx0, il);
ggml_tensor * v = ggml_view_4d(ctx0, k, v_cur->ne[0], k->ne[1], k->ne[2], k->ne[3], k->nb[1], k->nb[2], k->nb[3], 0);
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask, kv_rows, kq_b, nullptr, sinks, v_mla, 0, kq_scale, il);
cb(cur, "kqv_out", il);
if (wo) {
@@ -3729,7 +3710,7 @@ ggml_tensor * llm_graph_context::build_attn(
const int64_t n_embd_v = v_cur ? v_cur->ne[0] : v_mla->ne[0];
ggml_tensor * v = ggml_view_4d(ctx0, k, n_embd_v, k->ne[1], k->ne[2], k->ne[3], k->nb[1], k->nb[2], k->nb[3], 0);
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask_top_k, nullptr, sinks, v_mla, top_k->ne[0], kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask_top_k, nullptr, kq_b, nullptr, sinks, v_mla, top_k->ne[0], kq_scale, il);
cb(cur, "kqv_out", il);
if (wo) {
@@ -3752,15 +3733,14 @@ static ggml_tensor * build_attn_iswa_impl(
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
- ggml_tensor * kq_b,
ggml_tensor * kq_mask,
ggml_tensor * kv_rows,
ggml_tensor * sinks,
ggml_tensor * v_mla,
float kq_scale,
int il,
- ggml_tensor * bias_table = nullptr,
- ggml_tensor * bias_indices = nullptr) {
+ ggml_tensor * bias = nullptr,
+ ggml_tensor * bias_idxs = nullptr) {
ggml_context * ctx0 = gctx.ctx0;
ggml_cgraph * gf = gctx.gf;
@@ -3826,7 +3806,7 @@ static ggml_tensor * build_attn_iswa_impl(
ggml_tensor * k = use_kv_cur ? k_cur : mctx_cur->get_k(ctx0, il);
ggml_tensor * v = use_kv_cur ? v_cur : mctx_cur->get_v(ctx0, il);
- ggml_tensor * cur = gctx.build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il, bias_table, bias_indices,
+ ggml_tensor * cur = gctx.build_attn_mha(q, k, v, kq_mask, kv_rows, bias, bias_idxs, sinks, v_mla, 0, kq_scale, il,
mctx_cur->get_k_scale(il), mctx_cur->get_v_scale(il));
gctx.cb(cur, "kqv_out", il);
@@ -3866,7 +3846,7 @@ ggml_tensor * llm_graph_context::build_attn(
const auto & kq_mask = is_swa ? inp->self_attn_mask_swa.raw : inp->self_attn_mask.raw;
const auto & kv_rows = is_swa ? inp->self_attn_mask_swa.rows : inp->self_attn_mask.rows;
- return build_attn_iswa_impl(*this, inp, wo, wo_b, q_cur, k_cur, v_cur, kq_b, kq_mask, kv_rows, sinks, v_mla, kq_scale, il);
+ return build_attn_iswa_impl(*this, inp, wo, wo_b, q_cur, k_cur, v_cur, kq_mask, kv_rows, sinks, v_mla, kq_scale, il, kq_b, nullptr);
}
ggml_tensor * llm_graph_context::build_attn(
@@ -3918,7 +3898,7 @@ ggml_tensor * llm_graph_context::build_attn(
ggml_tensor * k = mctx_cur->get_k(ctx0, il);
ggml_tensor * v = ggml_view_4d(ctx0, k, v_cur->ne[0], k->ne[1], k->ne[2], k->ne[3], k->nb[1], k->nb[2], k->nb[3], 0);
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask, kv_rows, kq_b, nullptr, sinks, v_mla, 0, kq_scale, il);
cb(cur, "kqv_out", il);
if (k_rot) {
@@ -3975,7 +3955,7 @@ ggml_tensor * llm_graph_context::build_attn(
ggml_tensor * k = k_cur;
ggml_tensor * v = v_cur;
- ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, kq_mask, kv_rows, kq_b, nullptr, sinks, v_mla, 0, kq_scale, il);
cb(cur, "kqv_out", il);
if (wo) {
@@ -4510,7 +4490,7 @@ ggml_tensor * llm_graph_context::build_attn(
ggml_tensor * q_cur,
ggml_tensor * k_cur,
ggml_tensor * v_cur,
- ggml_tensor * bias_table,
+ ggml_tensor * bias,
ggml_tensor * sinks,
float kq_scale,
int il) const {
@@ -4521,10 +4501,10 @@ ggml_tensor * llm_graph_context::build_attn(
const auto & attn_mask = is_swa ? inp_attn->self_attn_mask_swa : inp_attn->self_attn_mask;
GGML_ASSERT(attn_mask.rows == nullptr);
- ggml_tensor * indices = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos;
+ ggml_tensor * bias_idxs = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos;
- return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, q_cur, k_cur, v_cur, nullptr, attn_mask.raw, nullptr,
- sinks, nullptr, kq_scale, il, bias_table, indices);
+ return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, q_cur, k_cur, v_cur, attn_mask.raw, nullptr,
+ sinks, nullptr, kq_scale, il, bias, bias_idxs);
}
void llm_graph_context::build_dense_out(
diff --git a/src/llama-graph.h b/src/llama-graph.h
index 4a1d497b7..a9e155012 100644
--- a/src/llama-graph.h
+++ b/src/llama-graph.h
@@ -1350,16 +1350,15 @@ struct llm_graph_context {
ggml_tensor * q, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v, // [n_embd_head_v, n_head_v, n_tokens] (v_trans = false)
- ggml_tensor * kq_b,
ggml_tensor * kq_mask,
ggml_tensor * kv_rows, // [n_kv, n_stream], k and v are shared by the streams
+ ggml_tensor * bias, // [n_bins, n_head_q, n_tokens] or [n_kv, n_tokens, n_head_kv, n_stream]
+ ggml_tensor * bias_idxs,
ggml_tensor * sinks, // [n_head_q]
ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
int64_t n_kv_max,
float kq_scale,
int il,
- ggml_tensor * bias_table = nullptr, // [n_bins, n_head_q, n_tokens]
- ggml_tensor * bias_indices = nullptr,
ggml_tensor * k_scale = nullptr,
ggml_tensor * v_scale = nullptr) const;
@@ -1566,7 +1565,7 @@ struct llm_graph_context {
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
- ggml_tensor * bias_table, // [n_bins, n_head_q, n_tokens]
+ ggml_tensor * bias, // [n_bins, n_head_q, n_tokens] or [n_kv, n_tokens, n_head_kv, n_stream]
ggml_tensor * sinks, // [n_head_q]
float kq_scale,
int il) const;
diff --git a/src/models/deepseek4.cpp b/src/models/deepseek4.cpp
index fb7fb3048..00cb596c1 100644
--- a/src/models/deepseek4.cpp
+++ b/src/models/deepseek4.cpp
@@ -740,7 +740,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_csa_lid_attention(
cb(kq_mask, "csa_lid_kq_mask", il);
const int64_t n_kv_max = std::min<int64_t>(raw_mask->ne[0], hparams.n_swa) + top_k->ne[0];
- ggml_tensor * out = build_attn_mha(q, k_all, k_all, nullptr, kq_mask, nullptr, sinks, nullptr, n_kv_max, kq_scale, il);
+ ggml_tensor * out = build_attn_mha(q, k_all, k_all, kq_mask, nullptr, nullptr, nullptr, sinks, nullptr, n_kv_max, kq_scale, il);
if (k_rot) {
out = llama_mul_mat_hadamard(ctx0, out, k_rot);
}
@@ -795,7 +795,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_hca_attention(
ggml_tensor * kq_mask = ggml_concat(ctx0, raw_mask, hca_mask, 0);
cb(kq_mask, "hca_kq_mask", il);
- ggml_tensor * out = build_attn_mha(q, k_all, k_all, nullptr, kq_mask, nullptr, sinks, nullptr, 0, kq_scale, il);
+ ggml_tensor * out = build_attn_mha(q, k_all, k_all, kq_mask, nullptr, nullptr, nullptr, sinks, nullptr, 0, kq_scale, il);
if (k_rot) {
out = llama_mul_mat_hadamard(ctx0, out, k_rot);
}
@@ -831,7 +831,7 @@ ggml_tensor * llama_model_deepseek4::graph::build_raw_attention(
ggml_tensor * k = mctx_cur->get_k(ctx0, il);
- ggml_tensor * out = build_attn_mha(q, k, k, nullptr, kq_mask, nullptr, sinks, nullptr, 0, kq_scale, il);
+ ggml_tensor * out = build_attn_mha(q, k, k, kq_mask, nullptr, nullptr, nullptr, sinks, nullptr, 0, kq_scale, il);
if (k_rot) {
out = llama_mul_mat_hadamard(ctx0, out, k_rot);
}
diff --git a/src/models/deepseek41.cpp b/src/models/deepseek41.cpp
index 9cc21d429..dccb067cb 100644
--- a/src/models/deepseek41.cpp
+++ b/src/models/deepseek41.cpp
@@ -714,7 +714,7 @@ ggml_tensor * llama_model_deepseek41::graph::build_attention41(
cb(kq_mask, "comp_kq_mask_sel", il);
}
- out = build_attn_mha(q, k_all, k_all, nullptr, kq_mask, nullptr, layer.attn_sinks, nullptr, 0, kq_scale, il);
+ out = build_attn_mha(q, k_all, k_all, kq_mask, nullptr, nullptr, nullptr, layer.attn_sinks, nullptr, 0, kq_scale, il);
} else {
// dense attention over [window | all compressed rows], selection applied as a mask
ggml_tensor * comp_k = ggml_view_4d(ctx0, comp_all, n_embd_head, 1, n_comp, n_stream,
@@ -741,7 +741,7 @@ ggml_tensor * llama_model_deepseek41::graph::build_attention41(
// at most window + top-k visible columns per query: lets CUDA FA gather only those
const int64_t n_kv_max = sparse ? std::min<int64_t>(raw_mask->ne[0], hparams.n_swa) + k_sel : 0;
- out = build_attn_mha(q, k_all, k_all, nullptr, kq_mask, nullptr, layer.attn_sinks, nullptr, n_kv_max, kq_scale, il);
+ out = build_attn_mha(q, k_all, k_all, kq_mask, nullptr, nullptr, nullptr, layer.attn_sinks, nullptr, n_kv_max, kq_scale, il);
}
if (k_rot) {
out = llama_mul_mat_hadamard(ctx0, out, k_rot);
diff --git a/src/models/glm5-next.cpp b/src/models/glm5-next.cpp
index a2d67b164..1d8c8d705 100644
--- a/src/models/glm5-next.cpp
+++ b/src/models/glm5-next.cpp
@@ -1065,7 +1065,7 @@ llama_model_glm5_next::graph::build_dsa_layer(
cb(sel, "kq_mask_dsa", il);
- ggml_tensor * out = build_attn_mha(q_absorbed, k, v, nullptr, sel, nullptr, nullptr, layer.wv_b, inp_kpool->n_sel, kq_scale, il);
+ ggml_tensor * out = build_attn_mha(q_absorbed, k, v, sel, nullptr, nullptr, nullptr, nullptr, layer.wv_b, inp_kpool->n_sel, kq_scale, il);
cb(out, "kqv_out", il);
out = build_lora_mm(layer.wo, out);
diff --git a/src/models/hy-v4-flash.cpp b/src/models/hy-v4-flash.cpp
index 063b7aea4..ff340a41f 100644
--- a/src/models/hy-v4-flash.cpp
+++ b/src/models/hy-v4-flash.cpp
@@ -895,7 +895,7 @@ ggml_tensor * llama_model_hy_v4_flash::graph::build_attention(
ggml_tensor * k = mctx_mla->get_k(ctx0, il);
ggml_tensor * v = ggml_view_4d(ctx0, k, kv_lora_rank, k->ne[1], k->ne[2], k->ne[3], k->nb[1], k->nb[2], k->nb[3], 0);
- attn = build_attn_mha(Qcur, k, v, nullptr, sel, nullptr, layer.attn_sinks, layer.wv_b, n_sel, kq_scale, il);
+ attn = build_attn_mha(Qcur, k, v, sel, nullptr, nullptr, nullptr, layer.attn_sinks, layer.wv_b, n_sel, kq_scale, il);
}
cb(attn, "attn_kqv", il);
diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp
index 4ccc84694..87cb21a3d 100644
--- a/src/models/inkling2.cpp
+++ b/src/models/inkling2.cpp
@@ -373,10 +373,10 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens
ggml_tensor * sinks = build_lora_mm(layer.attn_sink_proj, inputs[0]);
- ggml_tensor * table = ggml_mul_mat(ctx0, ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0), Rcur);
+ ggml_tensor * bias = ggml_mul_mat(ctx0, ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0), Rcur);
// the sink logit t is per query: exp(s - t)/(1 + sum(exp(s - t))) = exp(s)/(exp(t) + sum(exp(s))), with a zero sink
- table = ggml_sub(ctx0, table, ggml_reshape_3d(ctx0, sinks, 1, n_head, n_tokens));
+ bias = ggml_sub(ctx0, bias, ggml_reshape_3d(ctx0, sinks, 1, n_head, n_tokens));
// one zero tensor per head count for the whole graph
ggml_tensor *& zero = zero_sinks[n_head];
@@ -387,7 +387,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens
cur = build_attn(inp,
nullptr, nullptr,
- Qcur, Kcur, Vcur, table, sinks, 1.0f/sqrtf(n_embd_head), il);
+ Qcur, Kcur, Vcur, bias, sinks, 1.0f/sqrtf(n_embd_head), il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, inputs[0])));
cur = convolve(build_lora_mm(layer.wo, cur), layer.attn_post_conv, nullptr, state, history, recr, 2*n_embd_k_gqa);
diff --git a/src/models/kimi-k31.cpp b/src/models/kimi-k31.cpp
index 62719ba15..a37f37ef0 100644
--- a/src/models/kimi-k31.cpp
+++ b/src/models/kimi-k31.cpp
@@ -618,7 +618,7 @@ llama_model_kimi_k31::graph::build_dsa_layer(
// the cache holds at least 4096 cells. on the dense fallbacks the mask keeps the result correct
// but the kq and kqv products still run over the whole cache, and the indexer is pure overhead
const int64_t n_sel = std::min<int64_t>(mctx_mla->get_n_kv(), hparams.indexer_top_k);
- ggml_tensor * out = build_attn_mha(Q, k, v, nullptr, sel, nullptr, nullptr, layer.wv_b,
+ ggml_tensor * out = build_attn_mha(Q, k, v, sel, nullptr, nullptr, nullptr, nullptr, layer.wv_b,
n_sel, kq_scale, il);
cb(out, "kqv_out", il);
diff --git a/src/models/qwen4exp.cpp b/src/models/qwen4exp.cpp
index 6f672e9e2..be02ac07a 100644
--- a/src/models/qwen4exp.cpp
+++ b/src/models/qwen4exp.cpp
@@ -996,7 +996,7 @@ ggml_tensor * llama_model_qwen4exp::graph::build_attn_qsa(
sel = ggml_reshape_4d(ctx0, sel, sel->ne[0], sel->ne[1]/n_stream, 1, n_stream);
cb(sel, "kq_mask_qsa", il);
- ggml_tensor * cur = build_attn_mha(q, k, v, nullptr, sel, nullptr, nullptr, nullptr, n_sel, kq_scale, il);
+ ggml_tensor * cur = build_attn_mha(q, k, v, sel, nullptr, nullptr, nullptr, nullptr, nullptr, n_sel, kq_scale, il);
cb(cur, "kqv_out", il);
// the rotation is its own inverse, so undo it on the value side of the output
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 18350feff..4896fb23e 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -10353,7 +10353,7 @@ struct test_flash_attn_ext : public test_case {
}
ggml_tensor * out = ggml_flash_attn_ext(ctx, q, k, v, m, 1.0f/sqrtf(hsk), max_bias, logit_softcap);
- ggml_flash_attn_ext_add_sinks(out, s);
+ ggml_flash_attn_ext_set_sinks(out, s);
ggml_flash_attn_ext_set_n_kv_max(out, n_kv_max);
ggml_prec_set_acc(out, prec);
ggml_set_name(out, "out");
@@ -10429,7 +10429,7 @@ struct test_flash_attn_ext_mask_bcast : public test_flash_attn_ext {
ggml_set_name(m, "m");
ggml_tensor * out = ggml_flash_attn_ext(ctx, ref->src[0], ref->src[1], ref->src[2], m, 1.0f/sqrtf(hsk), max_bias, logit_softcap);
- ggml_flash_attn_ext_add_sinks(out, ref->src[4]);
+ ggml_flash_attn_ext_set_sinks(out, ref->src[4]);
ggml_prec_set_acc(out, prec);
ggml_set_name(out, "out");
@@ -10533,8 +10533,8 @@ struct test_flash_attn_ext_kv_rows : public test_case {
}
};
-// GGML_OP_FLASH_ATTN_EXT with an indexed bias: mask[key, query] += table[indices[key, query], head, query]
-struct test_flash_attn_ext_indexed_bias : public test_case {
+// GGML_OP_FLASH_ATTN_EXT with an indexed bias: mask[key, query] += bias[bias_idxs[key, query], head, query]
+struct test_flash_attn_ext_bias_indexed : public test_case {
const int64_t hs;
const int64_t nh; // K/V heads
const int64_t gqa;
@@ -10561,7 +10561,7 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
return 5e-4;
}
- test_flash_attn_ext_indexed_bias(int64_t hs = 128, int64_t nh = 2, int64_t gqa = 3, int64_t kv = 768, int64_t nb = 7, int64_t n_stream = 2,
+ test_flash_attn_ext_bias_indexed(int64_t hs = 128, int64_t nh = 2, int64_t gqa = 3, int64_t kv = 768, int64_t nb = 7, int64_t n_stream = 2,
int64_t n_bins = 17, bool kv_rows = false, bool sinks = true, ggml_type type_KV = GGML_TYPE_F16,
float logit_softcap = 0.0f, bool mask_row = false, bool causal = false, int64_t n_kv_max = 0)
: hs(hs), nh(nh), gqa(gqa), kv(kv), nb(nb), n_stream(n_stream), n_bins(n_bins), kv_rows(kv_rows), sinks(sinks), type_KV(type_KV),
@@ -10585,19 +10585,19 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
ggml_set_name(r, "r");
}
- ggml_tensor * table = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_bins, nh*gqa, nb*n_stream);
- ggml_set_name(table, "table");
+ ggml_tensor * bias = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_bins, nh*gqa, nb*n_stream);
+ ggml_set_name(bias, "bias");
- ggml_tensor * indices = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, kv, nb*n_stream);
- ggml_set_name(indices, "indices");
+ ggml_tensor * bias_idxs = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, kv, nb*n_stream);
+ ggml_set_name(bias_idxs, "bias_idxs");
ggml_tensor * out = ggml_flash_attn_ext_rows(ctx, q, k, v, m, r, 1.0f/sqrtf(hs), 0.0f, logit_softcap);
if (sinks) {
ggml_tensor * s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, nh*gqa);
ggml_set_name(s, "s");
- ggml_flash_attn_ext_add_sinks(out, s);
+ ggml_flash_attn_ext_set_sinks(out, s);
}
- ggml_flash_attn_ext_add_indexed_bias(out, table, indices);
+ ggml_flash_attn_ext_set_bias(out, bias, bias_idxs);
ggml_flash_attn_ext_set_n_kv_max(out, n_kv_max);
ggml_prec_set_acc(out, GGML_PREC_F32);
ggml_set_name(out, "out");
@@ -10656,7 +10656,7 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
std::vector<ggml_fp16_t> h(f.size());
ggml_fp32_to_fp16_row(f.data(), h.data(), f.size());
ggml_backend_tensor_set(t, h.data(), 0, h.size()*sizeof(ggml_fp16_t));
- } else if (strcmp(t->name, "indices") == 0) {
+ } else if (strcmp(t->name, "bias_idxs") == 0) {
std::vector<int32_t> idx(ggml_nelements(t));
std::uniform_int_distribution<int32_t> dis(0, n_bins - 1);
for (auto & i : idx) {
@@ -10671,7 +10671,7 @@ struct test_flash_attn_ext_indexed_bias : public test_case {
}
}
ggml_backend_tensor_set(t, idx.data(), 0, idx.size()*sizeof(int32_t));
- } else if (strcmp(t->name, "table") == 0) {
+ } else if (strcmp(t->name, "bias") == 0) {
init_tensor_uniform(t, -4.0f, 4.0f);
} else if (strcmp(t->name, "s") == 0) {
init_tensor_uniform(t, -10.0f, 10.0f);
@@ -14994,38 +14994,38 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
for (bool kv_rows : {false, true}) {
for (int64_t nb : {1, 7, 65}) {
for (int64_t n_stream : {1, 2}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, nb, n_stream, 17, kv_rows));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, nb, n_stream, 17, kv_rows));
}
}
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias( 64, 2, 3, 768, 7, 2, 17, kv_rows, false));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 8, 3, 512, 65, 1, 1025, kv_rows));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 1, 1, 17, kv_rows, true, GGML_TYPE_F8_E4M3));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed( 64, 2, 3, 768, 7, 2, 17, kv_rows, false));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 8, 3, 512, 65, 1, 1025, kv_rows));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, 1, 1, 17, kv_rows, true, GGML_TYPE_F8_E4M3));
for (ggml_type type : {GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 1, 1, 17, kv_rows, true, type));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 7, 2, 17, kv_rows, true, type));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, 1, 1, 17, kv_rows, true, type));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, 7, 2, 17, kv_rows, true, type));
}
for (int64_t nb : {1, 7}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 700, nb, 2, 17, kv_rows));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, nb, 2, 17, kv_rows, true, GGML_TYPE_F16, 10.0f));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, nb, 2, 17, kv_rows, false, GGML_TYPE_F16, 0.0f, true));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 700, nb, 2, 17, kv_rows));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, nb, 2, 17, kv_rows, true, GGML_TYPE_F16, 10.0f));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 768, nb, 2, 17, kv_rows, false, GGML_TYPE_F16, 0.0f, true));
}
}
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0}) {
for (int64_t nb : {1, 4, 64}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 2048, nb, 2, 33, false, true, type, 0.0f, false, true));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 2048, nb, 2, 33, false, true, type, 0.0f, false, true));
}
}
for (int64_t nb : {1, 4}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 4096, nb, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, false, 512));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 4096, nb, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, false, 512));
}
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(256, 2, 3, 768, 65, 2, 17));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(256, 2, 3, 768, 65, 2, 17));
for (int64_t hs : {128, 256}) {
for (int64_t nb : {1, 7, 65}) {
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(hs, 2, 3, 1000, nb, 2, 17));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(hs, 2, 3, 1000, nb, 2, 17));
}
}
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 8, 3, 1100, 256, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, true));
- test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 1100, 7, 2, 33, false, true, GGML_TYPE_Q8_0));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 8, 3, 1100, 256, 2, 33, false, true, GGML_TYPE_F16, 0.0f, false, true));
+ test_cases.emplace_back(new test_flash_attn_ext_bias_indexed(128, 2, 3, 1100, 7, 2, 33, false, true, GGML_TYPE_Q8_0));
// sparse mask with large batch size
test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, { 8, 1}, 4096, 64, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512));
diff --git a/tools/mtmd/mtmd-graph.cpp b/tools/mtmd/mtmd-graph.cpp
index 576f27b83..6d0a19f6d 100644
--- a/tools/mtmd/mtmd-graph.cpp
+++ b/tools/mtmd/mtmd-graph.cpp
@@ -591,7 +591,7 @@ ggml_tensor * mtmd_graph::build_attn(
cur = ggml_flash_attn_ext(ctx0, q, k, v, kq_mask, kq_scale, 0.0f, 0.0f);
ggml_prec_set_acc(cur, GGML_PREC_F32);
if (sinks != nullptr) {
- ggml_flash_attn_ext_add_sinks(cur, sinks);
+ ggml_flash_attn_ext_set_sinks(cur, sinks);
}
cur = ggml_reshape_2d(ctx0, cur, cur->ne[0]*cur->ne[1], cur->ne[2]*cur->ne[3]);