diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index 9213d2a0ed..a96577f11c 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -3148,7 +3148,9 @@ ggml_tensor * llm_graph_context::build_attn_mha( ggml_tensor * v_mla, int64_t n_kv_max, float kq_scale, - int il) const { + int il, + ggml_tensor * bias_table, + ggml_tensor * bias_indices) const { const bool v_trans = v->nb[1] > v->nb[2]; // split the batch into streams if needed @@ -3163,12 +3165,28 @@ 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; GGML_ASSERT(use_flash_attn || kv_rows == nullptr); if (use_flash_attn) { - GGML_ASSERT(kq_b == nullptr && "Flash attention does not support KQ bias yet"); + GGML_ASSERT(kq_b == nullptr && "Flash attention does not support dense KQ bias"); if (v_trans) { v = ggml_transpose(ctx0, v); @@ -3197,6 +3215,16 @@ ggml_tensor * llm_graph_context::build_attn_mha( ggml_flash_attn_ext_set_n_kv_max(cur, static_cast(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. @@ -3216,6 +3244,9 @@ ggml_tensor * llm_graph_context::build_attn_mha( cur = ggml_reshape_2d(ctx0, cur, cur->ne[0]*cur->ne[1], cur->ne[2]*cur->ne[3]); } else { + if (bias_table) { + kq_mask = dense_bias_mask(); + } ggml_tensor * kq = ggml_mul_mat(ctx0, k, q); cb(kq, "kq", il); @@ -3725,7 +3756,9 @@ static ggml_tensor * build_attn_iswa_impl( ggml_tensor * sinks, ggml_tensor * v_mla, float kq_scale, - int il) { + int il, + ggml_tensor * bias_table = nullptr, + ggml_tensor * bias_indices = nullptr) { ggml_context * ctx0 = gctx.ctx0; ggml_cgraph * gf = gctx.gf; @@ -3791,7 +3824,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); + 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); cur = build_attn_scale_kv_output(ctx0, mctx_cur, cur, il); gctx.cb(cur, "kqv_out", il); @@ -4454,7 +4487,7 @@ ggml_tensor * llm_graph_context::build_attn( ggml_tensor * q_cur, ggml_tensor * k_cur, ggml_tensor * v_cur, - ggml_tensor * kq_b, + ggml_tensor * bias_table, ggml_tensor * sinks, float kq_scale, int il) const { @@ -4464,22 +4497,11 @@ ggml_tensor * llm_graph_context::build_attn( const auto & attn_mask = is_swa ? inp_attn->self_attn_mask_swa : inp_attn->self_attn_mask; - // TODO: support kv_rows - fill the distances over the kv_rows cell lists and pass the rows to build_attn_mha, - // needs a kv_rows flash attention with a per-head mask (CUDA has none) GGML_ASSERT(attn_mask.rows == nullptr); + ggml_tensor * indices = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos; - // a mask per head, in the dense layout of the input mask, which flash attention takes in F16 - ggml_tensor * kq_mask = attn_mask.raw; - if (kq_mask->type != GGML_TYPE_F32) { - kq_mask = ggml_cast(ctx0, kq_mask, GGML_TYPE_F32); - } - kq_mask = ggml_add(ctx0, kq_b, kq_mask); - if (cparams.flash_attn) { - kq_mask = ggml_cast(ctx0, kq_mask, GGML_TYPE_F16); - } - cb(kq_mask, "kq_mask_rel_pos", il); - - return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, wo_s, q_cur, k_cur, v_cur, nullptr, kq_mask, nullptr, sinks, nullptr, kq_scale, il); + 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_table, indices); } void llm_graph_context::build_dense_out( diff --git a/src/llama-graph.h b/src/llama-graph.h index ff155f14d2..508d055315 100644 --- a/src/llama-graph.h +++ b/src/llama-graph.h @@ -1349,7 +1349,9 @@ struct llm_graph_context { 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) const; + int il, + ggml_tensor * bias_table = nullptr, // [n_bins, n_head_q, n_tokens] + ggml_tensor * bias_indices = nullptr) const; llm_graph_input_attn_no_cache * build_attn_inp_no_cache() const; @@ -1547,7 +1549,6 @@ struct llm_graph_context { uint32_t extent, uint32_t extent_swa) const; - // kq_b is added to the KQ mask, after the KQ scale ggml_tensor * build_attn( llm_graph_input_mem_hybrid_iswa_rel * inp, ggml_tensor * wo, @@ -1556,7 +1557,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 * kq_b, // [n_kv, n_tokens/n_stream, n_head_q, n_stream] + ggml_tensor * bias_table, // [n_bins, n_head_q, n_tokens] 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 bdd1f18d67..d5834717bf 100644 --- a/src/models/inkling2.cpp +++ b/src/models/inkling2.cpp @@ -266,7 +266,6 @@ ggml_tensor * llama_model_inkling2::graph::control(ggml_tensor * cur, ggml_tenso ggml_tensor * llama_model_inkling2::graph::build_inputs() { inp = build_inp_mem_hybrid_iswa_rel(hparams.n_relative_extent, hparams.n_swa); - attn = inp->get_attn(); recr = inp->get_recr(); ggml_tensor * inp_pos = build_inp_pos(); @@ -363,24 +362,12 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens cb(Vcur, "Vcur", il); cb(Rcur, "Rcur", il); - ggml_tensor * rel_pos = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos; - - const int64_t n_stream = (is_swa ? attn->self_attn_mask_swa.raw : attn->self_attn_mask.raw)->ne[3]; - const int64_t n_kv = rel_pos->ne[0]; - ggml_tensor * sinks = build_lora_mm(layer.attn_sink_proj, inputs[0]); - // project r on the bias table first, then gather by distance: only the gathered bias is n_kv x n_tokens x n_head ggml_tensor * table = 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)); - table = ggml_cont(ctx0, ggml_permute(ctx0, table, 1, 0, 2, 3)); - - ggml_tensor * kq_b = ggml_get_rows(ctx0, table, rel_pos); - kq_b = ggml_reshape_4d(ctx0, kq_b, n_head, n_kv, n_tokens/n_stream, n_stream); - kq_b = ggml_cont(ctx0, ggml_permute(ctx0, kq_b, 2, 0, 1, 3)); - cb(kq_b, "kq_b", il); // one zero tensor per head count for the whole graph ggml_tensor *& zero = zero_sinks[n_head]; @@ -391,7 +378,7 @@ ggml_tensor * llama_model_inkling2::graph::build_layer(int il, int ip, ggml_tens cur = build_attn(inp, nullptr, nullptr, nullptr, - Qcur, Kcur, Vcur, kq_b, sinks, 1.0f/sqrtf(n_embd_head), il); + Qcur, Kcur, Vcur, table, 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/models.h b/src/models/models.h index 9f8e7cdcb6..dbf5f977ac 100644 --- a/src/models/models.h +++ b/src/models/models.h @@ -2668,7 +2668,6 @@ struct llama_model_inkling2 : public llama_model_base { void build_output(ggml_tensor * cur, const std::vector & blocks); llm_graph_input_mem_hybrid_iswa_rel * inp = nullptr; - llm_graph_input_attn_kv_iswa * attn = nullptr; llm_graph_input_rs * recr = nullptr; ggml_tensor * inp_out_ids = nullptr;