Skip to content

Commit 2d5b3dd

Browse files
authored
model: use the indexed bias in the Inkling 2 graph (ggml-org#33053)
* use the indexed bias in the Inkling 2 graph * Remove RPC check
1 parent 94a1e4b commit 2d5b3dd

4 files changed

Lines changed: 46 additions & 37 deletions

File tree

‎src/llama-graph.cpp‎

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,7 +3148,9 @@ ggml_tensor * llm_graph_context::build_attn_mha(
31483148
ggml_tensor * v_mla,
31493149
int64_t n_kv_max,
31503150
float kq_scale,
3151-
int il) const {
3151+
int il,
3152+
ggml_tensor * bias_table,
3153+
ggml_tensor * bias_indices) const {
31523154
const bool v_trans = v->nb[1] > v->nb[2];
31533155

31543156
// split the batch into streams if needed
@@ -3163,12 +3165,28 @@ ggml_tensor * llm_graph_context::build_attn_mha(
31633165
k = ggml_permute(ctx0, k, 0, 2, 1, 3);
31643166
v = ggml_permute(ctx0, v, 0, 2, 1, 3);
31653167

3168+
auto dense_bias_mask = [&]() {
3169+
ggml_tensor * table = ggml_cont(ctx0, ggml_permute(ctx0, bias_table, 1, 0, 2, 3));
3170+
ggml_tensor * bias = ggml_get_rows(ctx0, table, bias_indices);
3171+
bias = ggml_reshape_4d(ctx0, bias, q->ne[2], bias_indices->ne[0], q->ne[1], n_stream);
3172+
bias = ggml_cont(ctx0, ggml_permute(ctx0, bias, 2, 0, 1, 3));
3173+
cb(bias, "kq_b", il);
3174+
3175+
ggml_tensor * mask = kq_mask->type == GGML_TYPE_F32 ? kq_mask : ggml_cast(ctx0, kq_mask, GGML_TYPE_F32);
3176+
mask = ggml_add(ctx0, bias, mask);
3177+
if (cparams.flash_attn) {
3178+
mask = ggml_cast(ctx0, mask, GGML_TYPE_F16);
3179+
}
3180+
cb(mask, "kq_mask_rel_pos", il);
3181+
return mask;
3182+
};
3183+
31663184
ggml_tensor * cur;
31673185

31683186
const bool use_flash_attn = cparams.flash_attn && kq_b == nullptr;
31693187
GGML_ASSERT(use_flash_attn || kv_rows == nullptr);
31703188
if (use_flash_attn) {
3171-
GGML_ASSERT(kq_b == nullptr && "Flash attention does not support KQ bias yet");
3189+
GGML_ASSERT(kq_b == nullptr && "Flash attention does not support dense KQ bias");
31723190

31733191
if (v_trans) {
31743192
v = ggml_transpose(ctx0, v);
@@ -3197,6 +3215,16 @@ ggml_tensor * llm_graph_context::build_attn_mha(
31973215
ggml_flash_attn_ext_set_n_kv_max(cur, static_cast<int32_t>(n_kv_max));
31983216
ggml_prec_set_acc(cur, GGML_PREC_F32);
31993217

3218+
if (bias_table) {
3219+
ggml_flash_attn_ext_add_indexed_bias(cur, bias_table, bias_indices);
3220+
auto * dev = model.dev_layer(il);
3221+
if ((!cparams.offload_kqv && ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_CPU) || !ggml_backend_dev_supports_op(dev, cur)) {
3222+
// Keep ordinary flash attention on the layer device.
3223+
ggml_flash_attn_ext_add_indexed_bias(cur, nullptr, nullptr);
3224+
cur->src[3] = dense_bias_mask();
3225+
}
3226+
}
3227+
32003228
if (v_mla) {
32013229
#if 0
32023230
// 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(
32163244

32173245
cur = ggml_reshape_2d(ctx0, cur, cur->ne[0]*cur->ne[1], cur->ne[2]*cur->ne[3]);
32183246
} else {
3247+
if (bias_table) {
3248+
kq_mask = dense_bias_mask();
3249+
}
32193250
ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
32203251
cb(kq, "kq", il);
32213252

@@ -3725,7 +3756,9 @@ static ggml_tensor * build_attn_iswa_impl(
37253756
ggml_tensor * sinks,
37263757
ggml_tensor * v_mla,
37273758
float kq_scale,
3728-
int il) {
3759+
int il,
3760+
ggml_tensor * bias_table = nullptr,
3761+
ggml_tensor * bias_indices = nullptr) {
37293762
ggml_context * ctx0 = gctx.ctx0;
37303763
ggml_cgraph * gf = gctx.gf;
37313764

@@ -3791,7 +3824,7 @@ static ggml_tensor * build_attn_iswa_impl(
37913824
ggml_tensor * k = use_kv_cur ? k_cur : mctx_cur->get_k(ctx0, il);
37923825
ggml_tensor * v = use_kv_cur ? v_cur : mctx_cur->get_v(ctx0, il);
37933826

3794-
ggml_tensor * cur = gctx.build_attn_mha(q, k, v, kq_b, kq_mask, kv_rows, sinks, v_mla, 0, kq_scale, il);
3827+
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);
37953828
cur = build_attn_scale_kv_output(ctx0, mctx_cur, cur, il);
37963829
gctx.cb(cur, "kqv_out", il);
37973830

@@ -4454,7 +4487,7 @@ ggml_tensor * llm_graph_context::build_attn(
44544487
ggml_tensor * q_cur,
44554488
ggml_tensor * k_cur,
44564489
ggml_tensor * v_cur,
4457-
ggml_tensor * kq_b,
4490+
ggml_tensor * bias_table,
44584491
ggml_tensor * sinks,
44594492
float kq_scale,
44604493
int il) const {
@@ -4464,22 +4497,11 @@ ggml_tensor * llm_graph_context::build_attn(
44644497

44654498
const auto & attn_mask = is_swa ? inp_attn->self_attn_mask_swa : inp_attn->self_attn_mask;
44664499

4467-
// TODO: support kv_rows - fill the distances over the kv_rows cell lists and pass the rows to build_attn_mha,
4468-
// needs a kv_rows flash attention with a per-head mask (CUDA has none)
44694500
GGML_ASSERT(attn_mask.rows == nullptr);
4501+
ggml_tensor * indices = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos;
44704502

4471-
// a mask per head, in the dense layout of the input mask, which flash attention takes in F16
4472-
ggml_tensor * kq_mask = attn_mask.raw;
4473-
if (kq_mask->type != GGML_TYPE_F32) {
4474-
kq_mask = ggml_cast(ctx0, kq_mask, GGML_TYPE_F32);
4475-
}
4476-
kq_mask = ggml_add(ctx0, kq_b, kq_mask);
4477-
if (cparams.flash_attn) {
4478-
kq_mask = ggml_cast(ctx0, kq_mask, GGML_TYPE_F16);
4479-
}
4480-
cb(kq_mask, "kq_mask_rel_pos", il);
4481-
4482-
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);
4503+
return build_attn_iswa_impl(*this, inp_attn, wo, wo_b, wo_s, q_cur, k_cur, v_cur, nullptr, attn_mask.raw, nullptr,
4504+
sinks, nullptr, kq_scale, il, bias_table, indices);
44834505
}
44844506

44854507
void llm_graph_context::build_dense_out(

‎src/llama-graph.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,9 @@ struct llm_graph_context {
13491349
ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
13501350
int64_t n_kv_max,
13511351
float kq_scale,
1352-
int il) const;
1352+
int il,
1353+
ggml_tensor * bias_table = nullptr, // [n_bins, n_head_q, n_tokens]
1354+
ggml_tensor * bias_indices = nullptr) const;
13531355

13541356
llm_graph_input_attn_no_cache * build_attn_inp_no_cache() const;
13551357

@@ -1547,7 +1549,6 @@ struct llm_graph_context {
15471549
uint32_t extent,
15481550
uint32_t extent_swa) const;
15491551

1550-
// kq_b is added to the KQ mask, after the KQ scale
15511552
ggml_tensor * build_attn(
15521553
llm_graph_input_mem_hybrid_iswa_rel * inp,
15531554
ggml_tensor * wo,
@@ -1556,7 +1557,7 @@ struct llm_graph_context {
15561557
ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
15571558
ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
15581559
ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1559-
ggml_tensor * kq_b, // [n_kv, n_tokens/n_stream, n_head_q, n_stream]
1560+
ggml_tensor * bias_table, // [n_bins, n_head_q, n_tokens]
15601561
ggml_tensor * sinks, // [n_head_q]
15611562
float kq_scale,
15621563
int il) const;

‎src/models/inkling2.cpp‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ ggml_tensor * llama_model_inkling2::graph::control(ggml_tensor * cur, ggml_tenso
266266

267267
ggml_tensor * llama_model_inkling2::graph::build_inputs() {
268268
inp = build_inp_mem_hybrid_iswa_rel(hparams.n_relative_extent, hparams.n_swa);
269-
attn = inp->get_attn();
270269
recr = inp->get_recr();
271270

272271
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
363362
cb(Vcur, "Vcur", il);
364363
cb(Rcur, "Rcur", il);
365364

366-
ggml_tensor * rel_pos = is_swa ? inp->self_rel_pos_swa : inp->self_rel_pos;
367-
368-
const int64_t n_stream = (is_swa ? attn->self_attn_mask_swa.raw : attn->self_attn_mask.raw)->ne[3];
369-
const int64_t n_kv = rel_pos->ne[0];
370-
371365
ggml_tensor * sinks = build_lora_mm(layer.attn_sink_proj, inputs[0]);
372366

373-
// project r on the bias table first, then gather by distance: only the gathered bias is n_kv x n_tokens x n_head
374367
ggml_tensor * table = ggml_mul_mat(ctx0, ggml_pad(ctx0, layer.attn_rel_bias, 0, 1, 0, 0), Rcur);
375368

376369
// 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
377370
table = ggml_sub(ctx0, table, ggml_reshape_3d(ctx0, sinks, 1, n_head, n_tokens));
378-
table = ggml_cont(ctx0, ggml_permute(ctx0, table, 1, 0, 2, 3));
379-
380-
ggml_tensor * kq_b = ggml_get_rows(ctx0, table, rel_pos);
381-
kq_b = ggml_reshape_4d(ctx0, kq_b, n_head, n_kv, n_tokens/n_stream, n_stream);
382-
kq_b = ggml_cont(ctx0, ggml_permute(ctx0, kq_b, 2, 0, 1, 3));
383-
cb(kq_b, "kq_b", il);
384371

385372
// one zero tensor per head count for the whole graph
386373
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
391378

392379
cur = build_attn(inp,
393380
nullptr, nullptr, nullptr,
394-
Qcur, Kcur, Vcur, kq_b, sinks, 1.0f/sqrtf(n_embd_head), il);
381+
Qcur, Kcur, Vcur, table, sinks, 1.0f/sqrtf(n_embd_head), il);
395382

396383
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, build_lora_mm(layer.wqkv_gate, inputs[0])));
397384
cur = convolve(build_lora_mm(layer.wo, cur), layer.attn_post_conv, nullptr, state, history, recr, 2*n_embd_k_gqa);

‎src/models/models.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,6 @@ struct llama_model_inkling2 : public llama_model_base {
26682668
void build_output(ggml_tensor * cur, const std::vector<ggml_tensor *> & blocks);
26692669

26702670
llm_graph_input_mem_hybrid_iswa_rel * inp = nullptr;
2671-
llm_graph_input_attn_kv_iswa * attn = nullptr;
26722671
llm_graph_input_rs * recr = nullptr;
26732672

26742673
ggml_tensor * inp_out_ids = nullptr;

0 commit comments

Comments
 (0)