Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ggml/include/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -2294,13 +2294,20 @@ extern "C" {

GGML_API bool ggml_flash_attn_ext_has_indexed_bias(const struct ggml_tensor * a);

// Add table[indices[key, query], head, query] to the mask, in F32.
// Add table[indices[key, query], head, query] to the mask, in F32. The table is F32 or F16.
// 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(
struct ggml_tensor * a,
struct ggml_tensor * table,
struct ggml_tensor * indices);

// Add scale*offset[head, query] to the indexed bias of every key, in F32. offset is F32 [n_head, n_queries].
// With an offset an index equal to table->ne[0] selects no table entry (the bias is the offset alone).
GGML_API void ggml_flash_attn_ext_add_indexed_bias_offset(
struct ggml_tensor * a,
struct ggml_tensor * offset,
float scale);

// TODO: needs to be adapted to ggml_flash_attn_ext
GGML_API struct ggml_tensor * ggml_flash_attn_back(
struct ggml_context * ctx,
Expand Down
42 changes: 28 additions & 14 deletions ggml/src/ggml-cpu/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8967,6 +8967,28 @@ void ggml_compute_forward_top_k(
}
}

// indexed bias of FLASH_ATTN_EXT for the flattened query qf, head h and key ic: table[indices[ic, qf], h, qf] (F32 or F16)
// plus scale*offset[h, qf]; with an offset the index n_bins selects no table entry
static inline float ggml_fa_indexed_bias(const ggml_tensor * dst, const int64_t qf, const int64_t h, const int64_t ic) {
const ggml_tensor * table = dst->src[6];
const ggml_tensor * indices = dst->src[7];
const ggml_tensor * offset = dst->src[8];

const int32_t bin = ((const int32_t *) ((const char *) indices->data + qf*indices->nb[1]))[ic];
float v = 0.0f;
if (bin < table->ne[0]) {
GGML_ASSERT(bin >= 0);
const char * row = (const char *) table->data + qf*table->nb[2] + h*table->nb[1];
v = table->type == GGML_TYPE_F16 ? GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) row)[bin]) : ((const float *) row)[bin];
} else {
GGML_ASSERT(offset && bin == table->ne[0]);
}
if (offset) {
v += ggml_get_op_params_f32(dst, 6) * ((const float *) offset->data)[qf*offset->ne[0] + h];
}
return v;
}

static void ggml_compute_forward_flash_attn_ext_f16_one_chunk(
const ggml_compute_params * params,
ggml_tensor * dst,
Expand All @@ -8981,8 +9003,7 @@ 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_table = dst->src[6]; // see ggml_fa_indexed_bias()

GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
Expand Down Expand Up @@ -9024,7 +9045,6 @@ 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;

// parallelize by q rows using ggml_vec_dot_f32

Expand Down Expand Up @@ -9090,8 +9110,7 @@ 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 bool bias = bias_table != nullptr;

// online softmax / attention
// loop over n_kv and n_head_kv
Expand All @@ -9100,8 +9119,7 @@ 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) {
GGML_ASSERT(bins[ic] >= 0 && bins[ic] < n_bins);
mv += bias[bins[ic]];
mv += ggml_fa_indexed_bias(dst, iq3*neq1 + iq1, iq2, ic);
}
mv *= slope;
if (mv == -INFINITY) {
Expand Down Expand Up @@ -9281,9 +9299,7 @@ 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_table = dst->src[6]; // see ggml_fa_indexed_bias()

int ir = ir0;
while (ir < ir1) {
Expand Down Expand Up @@ -9363,13 +9379,11 @@ 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 bool bias = bias_table != nullptr;
for (int tk = 0; tk < kv_tile; tk++) {
float m = GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]);
if (bias) {
GGML_ASSERT(bins[ic + tk] >= 0 && bins[ic + tk] < n_bins);
m += bias[bins[ic + tk]];
m += ggml_fa_indexed_bias(dst, iq3*neq1 + iq1 + tq, iq2, ic + tk);
}
mask32[tq * KV_TILE_SZ + tk] = slope * m;
if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) {
Expand Down
35 changes: 28 additions & 7 deletions ggml/src/ggml-cuda/fattn-common.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,39 @@
// 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)

// optional bias added to the mask: table[indices[query, key], head, query], see ggml_flash_attn_ext_add_indexed_bias
// table is offset to the first query of the sequence and to the first head of the block
// optional bias added to the mask: table[indices[query, key], head, query] (F32 or F16) + offset_scale*offset[head, query],
// see ggml_flash_attn_ext_add_indexed_bias; offset() moves the pointers to the first query of the sequence and the first head
struct fattn_indexed_bias {
const float * table;
const float * table; // F32 table or nullptr
const half * table_h; // F16 table or nullptr
const int32_t * indices;
const float * bias_offset;
float offset_scale;
int32_t n_bins;
int32_t n_keys;
int32_t n_head;

__device__ __forceinline__ bool enabled() const {
return table || table_h;
}

__device__ __forceinline__ fattn_indexed_bias offset(const int64_t query, const int head) const {
return {table + (query*n_head + head)*n_bins, indices + query*n_keys, n_bins, n_keys, n_head};
const int64_t t0 = (query*n_head + head)*n_bins;
return {table ? table + t0 : nullptr, table_h ? table_h + t0 : nullptr, indices + query*n_keys,
bias_offset ? bias_offset + query*n_head + head : nullptr, offset_scale, n_bins, n_keys, n_head};
}

__device__ __forceinline__ float get(const int query, const int head, const int key) const {
const int bin = indices[int64_t(query)*n_keys + key];
return uint32_t(bin) < uint32_t(n_bins) ? table[(int64_t(query)*n_head + head)*n_bins + bin] : 0.0f;
const int bin = indices[int64_t(query)*n_keys + key];
const int64_t t = (int64_t(query)*n_head + head)*n_bins + bin;
float v = 0.0f;
if (uint32_t(bin) < uint32_t(n_bins)) {
v = table ? table[t] : __half2float(table_h[t]);
}
if (bias_offset) {
v += offset_scale*bias_offset[int64_t(query)*n_head + head];
}
return v;
}
};

Expand Down Expand Up @@ -1089,6 +1106,7 @@ void launch_fattn(

const ggml_tensor * bias_table = dst->src[6];
const ggml_tensor * bias_indices = dst->src[7];
const ggml_tensor * bias_offset = dst->src[8];
GGML_ASSERT(!bias_table || (!use_sparse && ncols2 == 1 && mask && mask->ne[2] == 1));

// with kv_rows the kernel iterates over the mask columns and reads K/V rows through kv_rows
Expand Down Expand Up @@ -1371,8 +1389,11 @@ void launch_fattn(
mask ? mask->ne[1] : 0, mask ? mask->ne[2] : 0, mask ? mask->ne[3] : 0,
mask ? mask->nb[1] : 0, mask ? mask->nb[2] : 0, mask ? mask->nb[3] : 0,
fattn_indexed_bias{
bias_table ? (const float *) bias_table->data : nullptr,
bias_table && bias_table->type == GGML_TYPE_F32 ? (const float *) bias_table->data : nullptr,
bias_table && bias_table->type == GGML_TYPE_F16 ? (const half *) bias_table->data : nullptr,
bias_indices ? (const int32_t *) bias_indices->data : nullptr,
bias_offset ? (const float *) bias_offset->data : nullptr,
bias_offset ? ggml_get_op_params_f32(KQV, 6) : 0.0f,
bias_table ? (int32_t) bias_table->ne[0] : 0,
bias_indices ? (int32_t) bias_indices->ne[0] : 0,
(int32_t) Q->ne[2]}
Expand Down
4 changes: 2 additions & 2 deletions ggml/src/ggml-cuda/fattn-tile.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
if (!oob_check || i_KQ < k_VKQ_sup) {
if (ncols2 > 1 || mask) {
float m = __half2float(mask[j*stride_mask + k_VKQ_0 + i_KQ]);
if (bias.table) {
if (bias.enabled()) {
m += bias.get(j, 0, k_VKQ_0 + i_KQ);
}
KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*m;
Expand Down Expand Up @@ -964,7 +964,7 @@ static __global__ void flash_attn_tile(
const half2 * V_h2 = (const half2 *) (V + nb23*sequence + nb22*(head0 / gqa_ratio)); // K and V have same shape

const half * maskh = mask ? (const half *) (mask + nb33*(sequence % ne33) + nb32*(head0 % ne32)) : nullptr;
const fattn_indexed_bias head_bias = bias.table ? bias.offset(int64_t(sequence)*ne01.z, head0) : bias;
const fattn_indexed_bias head_bias = bias.enabled() ? bias.offset(int64_t(sequence)*ne01.z, head0) : bias;

// K/V rows of this sequence, one per mask column
const int32_t * rows = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions ggml/src/ggml-cuda/fattn-vec.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static __global__ void flash_attn_ext_vec(
const char * V0 = V;

const half * maskh = mask ? (const half *) (mask + nb33*(sequence % ne33) + nb32*(head % ne32) + nb31*ic0) : nullptr;
const fattn_indexed_bias head_bias = bias.table ? bias.offset(int64_t(sequence)*ne01.z + ic0, head) : bias;
const fattn_indexed_bias head_bias = bias.enabled() ? bias.offset(int64_t(sequence)*ne01.z + ic0, head) : bias;

const float slope = get_alibi_slope(max_bias, head, n_head_log2, m0, m1);

Expand Down Expand Up @@ -315,7 +315,7 @@ static __global__ void flash_attn_ext_vec(

if (mask && (ncols == 1 || ic0 + j < int(ne01.z))) {
float m = __half2float(maskh[j*ne11 + i_KQ]);
if (head_bias.table) {
if (head_bias.enabled()) {
m += head_bias.get(j, 0, k_VKQ_0 + i_KQ);
}
sum += slope*m;
Expand Down
21 changes: 20 additions & 1 deletion ggml/src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -5560,19 +5560,38 @@ void ggml_flash_attn_ext_add_indexed_bias(
a->src[7] = indices;
ggml_set_op_params_i32(a, 5, table != NULL);
if (!table) {
a->src[8] = NULL;
ggml_set_op_params_f32(a, 6, 0.0f);
return;
}

const struct ggml_tensor * q = a->src[0];
const struct ggml_tensor * mask = a->src[3];
GGML_ASSERT(mask && mask->ne[0] == (a->src[5] ? a->src[5]->ne[0] : a->src[1]->ne[1]) && mask->ne[1] == q->ne[1]);
GGML_ASSERT(mask->ne[2] == 1 && mask->ne[3] == q->ne[3]);
GGML_ASSERT(table->type == GGML_TYPE_F32 && ggml_is_contiguous(table));
GGML_ASSERT((table->type == GGML_TYPE_F32 || table->type == GGML_TYPE_F16) && ggml_is_contiguous(table));
GGML_ASSERT(table->ne[1] == q->ne[2] && table->ne[0] > 0 && table->ne[2] == q->ne[1]*q->ne[3] && table->ne[3] == 1);
GGML_ASSERT(indices->type == GGML_TYPE_I32 && ggml_is_contiguous(indices) && ggml_is_matrix(indices));
GGML_ASSERT(indices->ne[0] == mask->ne[0] && indices->ne[1] == table->ne[2]);
}

void ggml_flash_attn_ext_add_indexed_bias_offset(
struct ggml_tensor * a,
struct ggml_tensor * offset,
float scale) {
GGML_ASSERT(a->op == GGML_OP_FLASH_ATTN_EXT);
GGML_ASSERT(offset == NULL || ggml_flash_attn_ext_has_indexed_bias(a));
a->src[8] = offset;
ggml_set_op_params_f32(a, 6, offset ? scale : 0.0f);
if (!offset) {
return;
}

const struct ggml_tensor * q = a->src[0];
GGML_ASSERT(offset->type == GGML_TYPE_F32 && ggml_is_contiguous(offset));
GGML_ASSERT(offset->ne[0] == q->ne[2] && offset->ne[1] == q->ne[1]*q->ne[3] && offset->ne[2] == 1 && offset->ne[3] == 1);
}

// ggml_flash_attn_back

struct ggml_tensor * ggml_flash_attn_back(
Expand Down
13 changes: 11 additions & 2 deletions src/llama-graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,7 @@ ggml_tensor * llm_graph_context::build_attn_mha(
if (rel_bias) {
GGML_ASSERT(kv_rows == nullptr);
ggml_flash_attn_ext_add_indexed_bias(cur, rel_bias->table, rel_bias->indices);
ggml_flash_attn_ext_add_indexed_bias_offset(cur, rel_bias->offset, rel_bias->offset_scale);
ggml_backend_dev_t dev = model.dev_layer(il);
if (!cparams.offload_kqv || !dev || !ggml_backend_dev_supports_op(dev, cur)) {
ggml_flash_attn_ext_add_indexed_bias(cur, nullptr, nullptr);
Expand Down Expand Up @@ -4454,8 +4455,14 @@ llm_graph_input_mem_hybrid_iswa_rel * llm_graph_context::build_inp_mem_hybrid_is
}

ggml_tensor * llm_graph_context::build_attn_rel_mask(ggml_tensor * mask, const llm_graph_rel_bias & rel_bias, int il) const {
// the dense mask gathers from a table with a bin for the cells farther than the extent, and the offset in every bin
ggml_tensor * table = rel_bias.table->type == GGML_TYPE_F32 ? rel_bias.table : ggml_cast(ctx0, rel_bias.table, GGML_TYPE_F32);
table = ggml_pad(ctx0, table, 1, 0, 0, 0);
table = ggml_add(ctx0, table, ggml_scale(ctx0,
ggml_reshape_3d(ctx0, rel_bias.offset, 1, rel_bias.offset->ne[0], rel_bias.offset->ne[1]), rel_bias.offset_scale));

// [n_bins, n_head, n_tokens] -> [n_head, n_bins, n_tokens]
ggml_tensor * table = ggml_cont(ctx0, ggml_permute(ctx0, rel_bias.table, 1, 0, 2, 3));
table = ggml_cont(ctx0, ggml_permute(ctx0, table, 1, 0, 2, 3));
ggml_tensor * rel_pos = rel_bias.indices; // [n_kv, n_tokens]

const int64_t n_kv = mask->ne[0];
Expand Down Expand Up @@ -4522,6 +4529,8 @@ ggml_tensor * llm_graph_context::build_attn(
ggml_tensor * k_cur,
ggml_tensor * v_cur,
ggml_tensor * rel_bias,
ggml_tensor * rel_offset,
float rel_offset_scale,
ggml_tensor * sinks,
float kq_scale,
int il) const {
Expand All @@ -4534,7 +4543,7 @@ ggml_tensor * llm_graph_context::build_attn(
// TODO: support kv_rows - fill the distances over the kv_rows cell lists and pass the rows to build_attn_mha
GGML_ASSERT(attn_mask.rows == nullptr);

const llm_graph_rel_bias bias = { rel_bias, is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos, inp->self_rows };
const llm_graph_rel_bias bias = { rel_bias, is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos, rel_offset, rel_offset_scale, inp->self_rows };

return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, wo_s, q_cur, k_cur, v_cur, nullptr, attn_mask.raw, nullptr, sinks, nullptr, kq_scale, il, &bias);
}
Expand Down
11 changes: 8 additions & 3 deletions src/llama-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1095,10 +1095,13 @@ struct llm_graph_amax_src1 {
ggml_tensor * scale_s; // scale to undo on the matmul result, or nullptr
};

// relative-position bias of llm_graph_input_mem_hybrid_iswa_rel: table[indices[key, query], head, query] is added to the KQ mask
// relative-position bias of llm_graph_input_mem_hybrid_iswa_rel: table[indices[key, query], head, query] + offset_scale*offset[head, query]
// is added to the KQ mask; the index n_bins (a cell farther than the extent) selects the offset alone
struct llm_graph_rel_bias {
ggml_tensor * table; // [n_bins, n_head_q, n_tokens], F32
ggml_tensor * table; // [n_bins, n_head_q, n_tokens], F32 or F16
ggml_tensor * indices; // [n_kv, n_tokens], I32
ggml_tensor * offset; // [n_head_q, n_tokens], F32
float offset_scale;
ggml_tensor * rows; // [n_tokens], I32, row indices for the chunked dense mask
};

Expand Down Expand Up @@ -1575,7 +1578,9 @@ 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 * rel_bias, // [extent + 1, n_head_q, n_tokens]
ggml_tensor * rel_bias, // [extent, n_head_q, n_tokens], F32 or F16
ggml_tensor * rel_offset, // [n_head_q, n_tokens], added with rel_offset_scale to the bias of every cell
float rel_offset_scale,
ggml_tensor * sinks, // [n_head_q]
float kq_scale,
int il) const;
Expand Down
Loading
Loading