diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index 161e8e0cef47..877cfba5c7b6 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -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, diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp index 26b4a01c96eb..823ea192fa27 100644 --- a/ggml/src/ggml-cpu/ops.cpp +++ b/ggml/src/ggml-cpu/ops.cpp @@ -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, @@ -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) @@ -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 @@ -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 @@ -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) { @@ -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) { @@ -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) { diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh index a6261c7b058f..ffb7612c6288 100644 --- a/ggml/src/ggml-cuda/fattn-common.cuh +++ b/ggml/src/ggml-cuda/fattn-common.cuh @@ -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; } }; @@ -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 @@ -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]} diff --git a/ggml/src/ggml-cuda/fattn-tile.cuh b/ggml/src/ggml-cuda/fattn-tile.cuh index 6ac241198ace..34bee5cb319f 100644 --- a/ggml/src/ggml-cuda/fattn-tile.cuh +++ b/ggml/src/ggml-cuda/fattn-tile.cuh @@ -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; @@ -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; diff --git a/ggml/src/ggml-cuda/fattn-vec.cuh b/ggml/src/ggml-cuda/fattn-vec.cuh index a2c19746f1ea..3cb2748f5533 100644 --- a/ggml/src/ggml-cuda/fattn-vec.cuh +++ b/ggml/src/ggml-cuda/fattn-vec.cuh @@ -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); @@ -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; diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index 652a7c7d636c..5b61e2a3d830 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -5560,6 +5560,8 @@ 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; } @@ -5567,12 +5569,29 @@ void ggml_flash_attn_ext_add_indexed_bias( 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( diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index 033bc501d14e..3e54d70f327a 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -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); @@ -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]; @@ -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 { @@ -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); } diff --git a/src/llama-graph.h b/src/llama-graph.h index da69953df397..fc157d64d883 100644 --- a/src/llama-graph.h +++ b/src/llama-graph.h @@ -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 }; @@ -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; diff --git a/src/models/inkling2.cpp b/src/models/inkling2.cpp index 8b147b8a89b8..0cbb8e4514a4 100644 --- a/src/models/inkling2.cpp +++ b/src/models/inkling2.cpp @@ -393,24 +393,23 @@ 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]); - // project r on the bias table, the attention gathers it by distance: [extent + 1, n_head, n_tokens] - // the last bucket is a zero pad, for the cells that are farther than the extent - // the looped cache slots share the weights of their physical layer, pad each once - ggml_tensor *& rel_bias = rel_bias_padded[layer.attn_rel_bias]; - if (!rel_bias) { - rel_bias = ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0); + // project r on the bias table, the attention gathers it by distance: [extent, n_head, n_tokens] + // the cells farther than the extent get no table entry, only the offset below + ggml_tensor * table = ggml_mul_mat(ctx0, layer.attn_rel_bias, Rcur); + if (cparams.flash_attn && n_tokens >= 32) { + // prompt batches: the table is n_tokens x n_head x extent, F16 halves it + table = ggml_cast(ctx0, table, GGML_TYPE_F16); } - ggml_tensor * table = ggml_mul_mat(ctx0, rel_bias, 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)); cb(table, "rel_bias", il); - sinks = zero_sinks->ne[0] == n_head ? zero_sinks : ggml_fill(ctx0, ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, n_head), 0.0f); + // 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 + // -t is the offset of the bias of every cell + sinks = ggml_reshape_2d(ctx0, sinks, n_head, n_tokens); + ggml_tensor * zero = zero_sinks->ne[0] == n_head ? zero_sinks : ggml_fill(ctx0, ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, n_head), 0.0f); cur = build_attn(inp, nullptr, nullptr, nullptr, - Qcur, Kcur, Vcur, table, sinks, 1.0f/sqrtf(n_embd_head), il); + Qcur, Kcur, Vcur, table, sinks, -1.0f, zero, 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/models.h b/src/models/models.h index 754e2fd76bce..1792e9908bdd 100644 --- a/src/models/models.h +++ b/src/models/models.h @@ -2570,7 +2570,6 @@ struct llama_model_inkling2 : public llama_model_base { ggml_tensor * zero_sinks = nullptr; std::map, ggml_tensor *> stacks; // see stack() - std::map rel_bias_padded; // relative-position bias table + a zero bucket }; std::unique_ptr build_arch_graph(const llm_graph_params & params) const override; diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index 476687ff26f6..f45ea8b18bdb 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -10483,11 +10483,17 @@ struct test_flash_attn_ext_indexed_bias : public test_case { const bool kv_rows; // all streams read one K/V cache of 2*kv rows through kv_rows const bool sinks; const ggml_type type_KV; + const ggml_type type_table; + const bool offset; // with an offset, the index n_bins selects no table entry std::vector rows; std::string vars() override { - return VARS_TO_STR10(hs, nh, gqa, kv, nb, n_stream, n_bins, kv_rows, sinks, type_KV); + std::string s = VARS_TO_STR10(hs, nh, gqa, kv, nb, n_stream, n_bins, kv_rows, sinks, type_KV); + if (type_table != GGML_TYPE_F32 || offset) { + s += "," + VARS_TO_STR2(type_table, offset); + } + return s; } double max_nmse_err() override { @@ -10495,8 +10501,10 @@ struct test_flash_attn_ext_indexed_bias : public test_case { } test_flash_attn_ext_indexed_bias(int64_t hs = 128, int64_t nh = 2, int64_t gqa = 3, int64_t kv = 768, int64_t nb = 7, int64_t n_stream = 2, - int64_t n_bins = 17, bool kv_rows = false, bool sinks = true, ggml_type type_KV = GGML_TYPE_F16) - : 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) {} + int64_t n_bins = 17, bool kv_rows = false, bool sinks = true, ggml_type type_KV = GGML_TYPE_F16, + ggml_type type_table = GGML_TYPE_F32, bool offset = false) + : 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), + type_table(type_table), offset(offset) {} ggml_tensor * build_graph(ggml_context * ctx) override { ggml_tensor * q = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, hs, nb, nh*gqa, n_stream); @@ -10516,7 +10524,7 @@ 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_tensor * table = ggml_new_tensor_3d(ctx, type_table, n_bins, nh*gqa, nb*n_stream); ggml_set_name(table, "table"); ggml_tensor * indices = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, kv, nb*n_stream); @@ -10529,6 +10537,11 @@ struct test_flash_attn_ext_indexed_bias : public test_case { ggml_flash_attn_ext_add_sinks(out, s); } ggml_flash_attn_ext_add_indexed_bias(out, table, indices); + if (offset) { + ggml_tensor * off = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, nh*gqa, nb*n_stream); + ggml_set_name(off, "offset"); + ggml_flash_attn_ext_add_indexed_bias_offset(out, off, -1.0f); + } ggml_prec_set_acc(out, GGML_PREC_F32); ggml_set_name(out, "out"); @@ -10573,7 +10586,7 @@ struct test_flash_attn_ext_indexed_bias : public test_case { ggml_backend_tensor_set(t, h.data(), 0, h.size()*sizeof(ggml_fp16_t)); } else if (strcmp(t->name, "indices") == 0) { std::vector idx(ggml_nelements(t)); - std::uniform_int_distribution dis(0, n_bins - 1); + std::uniform_int_distribution dis(0, offset ? n_bins : n_bins - 1); for (auto & i : idx) { i = dis(gen); } @@ -14834,6 +14847,20 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_add_scale({2304, 1, 1, 1})); test_cases.emplace_back(new test_add_scale({1024, 7, 2, 1}, 0.2f, 0.5f)); + // indexed bias with an F16 table and with an offset (Inkling 2 sink logits) + for (ggml_type type_table : {GGML_TYPE_F32, GGML_TYPE_F16}) { + for (bool offset : {false, true}) { + if (type_table == GGML_TYPE_F32 && !offset) { + continue; + } + test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 1, 1, 17, false, true, GGML_TYPE_F16, type_table, offset)); + test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 7, 2, 17, false, true, GGML_TYPE_F16, type_table, offset)); + test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 8, 3, 512, 65, 1, 1025, false, false, GGML_TYPE_F16, type_table, offset)); + test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 7, 2, 17, true, true, GGML_TYPE_F16, type_table, offset)); + test_cases.emplace_back(new test_flash_attn_ext_indexed_bias(128, 2, 3, 768, 7, 2, 17, false, true, GGML_TYPE_Q8_0, type_table, offset)); + } + } + // flash attention with an indexed bias (Inkling 2 relative-position bias) for (bool kv_rows : {false, true}) { for (int64_t nb : {1, 7, 65}) {