upstream-carry #32740: ggml: compact bias on the existing FLASH_ATTN_EXT for CUDA, ROCm and Vulkan
Temporary carry of the open upstream PR https://github.com/ggml-org/llama.cpp/pull/32740 at head 808fd1c4e, its four commits (1fa93f902, d29f9d489, ff91e4f84, 808fd1c4e) squashed. Inkling's per-query bias table is applied inside flash attention instead of a dense [n_kv, n_tokens, n_head] bias tensor, which cuts Inkling's compute buffers and long-context prefill time on the RX 580. Drop this commit when the PR is merged upstream (FORK.md, "Temporary upstream carries"). One conflict, ggml-vulkan.cpp use_mask_opt: kept the fork's GCN `HSK >= 256 || HSV >= 256` clause and its comment (F2) and upstream's `!use_sparse`, added the PR's `!bias &&`. Original commit trailer: Assisted-by: Codex Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ
This commit is contained in:
1 parent
d2396e4e36
commit
72da62cea0
32 files changed
+505
-145
No files matched your search
+54
-32
@@ -1415,7 +1415,7 @@ void llm_graph_input_mem_hybrid_iswa_rel::set_input(const llama_ubatch * ubatch)
|
||||
}
|
||||
|
||||
if (self_rel_pos->buffer) {
|
||||
attn_ctx->get_base()->set_input_relative_pos(self_rel_pos, ubatch, extent);
|
||||
attn_ctx->get_base()->set_input_relative_pos(self_rel_pos, inp_attn->self_attn_mask.rows, ubatch, extent);
|
||||
}
|
||||
|
||||
// swa tensors may not be allocated if there are no SWA attention layers
|
||||
@@ -1429,7 +1429,7 @@ void llm_graph_input_mem_hybrid_iswa_rel::set_input(const llama_ubatch * ubatch)
|
||||
}
|
||||
|
||||
if (self_rel_pos_swa->buffer) {
|
||||
attn_ctx->get_swa()->set_input_relative_pos(self_rel_pos_swa, ubatch, extent_swa);
|
||||
attn_ctx->get_swa()->set_input_relative_pos(self_rel_pos_swa, inp_attn->self_attn_mask_swa.rows, ubatch, extent_swa);
|
||||
}
|
||||
|
||||
const int64_t n_rs = mctx->get_recr()->get_n_rs();
|
||||
@@ -1454,16 +1454,13 @@ bool llm_graph_input_mem_hybrid_iswa_rel::can_reuse(const llm_graph_params & par
|
||||
|
||||
const auto * attn_ctx = mctx->get_attn();
|
||||
|
||||
// the masks are always dense, see build_inp_mem_hybrid_iswa_rel()
|
||||
|
||||
// base tensors may not be allocated if there are no non-SWA attention layers
|
||||
if (inp_attn->self_k_idxs && inp_attn->self_k_idxs->buffer) {
|
||||
res &= inp_attn->self_k_idxs->ne[0] == params.ubatch.n_tokens;
|
||||
}
|
||||
|
||||
res &= can_reuse_kq_mask(inp_attn->self_attn_mask.raw, attn_ctx->get_base(), params.ubatch, params.cparams);
|
||||
res &= can_reuse_attn_mask(inp_attn->self_attn_mask, attn_ctx->get_base(), params.ubatch, params.cparams);
|
||||
|
||||
res &= self_rel_pos->ne[0] == attn_ctx->get_base()->get_n_kv();
|
||||
res &= self_rel_pos->ne[1] == params.ubatch.n_tokens;
|
||||
|
||||
// swa tensors may not be allocated if there are no SWA attention layers
|
||||
@@ -1471,9 +1468,8 @@ bool llm_graph_input_mem_hybrid_iswa_rel::can_reuse(const llm_graph_params & par
|
||||
res &= inp_attn->self_k_idxs_swa->ne[0] == params.ubatch.n_tokens;
|
||||
}
|
||||
|
||||
res &= can_reuse_kq_mask(inp_attn->self_attn_mask_swa.raw, attn_ctx->get_swa(), params.ubatch, params.cparams);
|
||||
res &= can_reuse_attn_mask(inp_attn->self_attn_mask_swa, attn_ctx->get_swa(), params.ubatch, params.cparams);
|
||||
|
||||
res &= self_rel_pos_swa->ne[0] == attn_ctx->get_swa()->get_n_kv();
|
||||
res &= self_rel_pos_swa->ne[1] == params.ubatch.n_tokens;
|
||||
|
||||
res &= inp_rs->s_copy->ne[0] == mctx->get_recr()->get_n_rs();
|
||||
@@ -3114,7 +3110,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
|
||||
@@ -3129,6 +3127,21 @@ 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;
|
||||
@@ -3162,6 +3175,23 @@ ggml_tensor * llm_graph_context::build_attn_mha(
|
||||
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);
|
||||
const auto 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);
|
||||
cur->src[3] = dense_bias_mask();
|
||||
} else if (kv_rows) {
|
||||
// Looped layers can project Q and the bias on a different device from their KV cache.
|
||||
for (int i = 0; i < ggml_backend_sched_get_n_backends(sched); ++i) {
|
||||
auto backend = ggml_backend_sched_get_backend(sched, i);
|
||||
if (ggml_backend_get_device(backend) == dev) {
|
||||
ggml_backend_sched_set_tensor_backend(sched, cur, backend);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (v_mla) {
|
||||
#if 0
|
||||
@@ -3182,6 +3212,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);
|
||||
|
||||
@@ -3691,7 +3724,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;
|
||||
|
||||
@@ -3757,7 +3792,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);
|
||||
|
||||
@@ -4351,19 +4386,18 @@ llm_graph_input_mem_hybrid_iswa_rel * llm_graph_context::build_inp_mem_hybrid_is
|
||||
|
||||
auto inp_attn = std::make_unique<llm_graph_input_attn_kv_iswa>(hparams, cparams, attn_ctx);
|
||||
|
||||
// dense masks: the bias is gathered by the distance to each cell of the K/V view
|
||||
{
|
||||
inp_attn->self_k_idxs = attn_ctx->get_base()->build_input_k_idxs(ctx0, ubatch);
|
||||
inp_attn->self_v_idxs = attn_ctx->get_base()->build_input_v_idxs(ctx0, ubatch);
|
||||
|
||||
inp_attn->self_attn_mask.raw = build_attn_inp_kq_mask(ctx0, attn_ctx->get_base(), ubatch, cparams);
|
||||
inp_attn->self_attn_mask = build_attn_inp_attn_mask(ctx0, attn_ctx->get_base(), ubatch, cparams);
|
||||
}
|
||||
|
||||
{
|
||||
inp_attn->self_k_idxs_swa = attn_ctx->get_swa()->build_input_k_idxs(ctx0, ubatch);
|
||||
inp_attn->self_v_idxs_swa = attn_ctx->get_swa()->build_input_v_idxs(ctx0, ubatch);
|
||||
|
||||
inp_attn->self_attn_mask_swa.raw = build_attn_inp_kq_mask(ctx0, attn_ctx->get_swa(), ubatch, cparams);
|
||||
inp_attn->self_attn_mask_swa = build_attn_inp_attn_mask(ctx0, attn_ctx->get_swa(), ubatch, cparams);
|
||||
}
|
||||
|
||||
inp_attn->self_k_rot = attn_ctx->get_base()->get_k_rot();
|
||||
@@ -4374,11 +4408,11 @@ llm_graph_input_mem_hybrid_iswa_rel * llm_graph_context::build_inp_mem_hybrid_is
|
||||
|
||||
auto inp = std::make_unique<llm_graph_input_mem_hybrid_iswa_rel>(cparams, std::move(inp_attn), std::move(inp_rs), mctx_cur, extent, extent_swa);
|
||||
|
||||
inp->self_rel_pos = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, attn_ctx->get_base()->get_n_kv(), ubatch.n_tokens);
|
||||
inp->self_rel_pos = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, inp->get_attn()->self_attn_mask.raw->ne[0], ubatch.n_tokens);
|
||||
ggml_set_input(inp->self_rel_pos);
|
||||
ggml_set_name(inp->self_rel_pos, "attn_inp_rel_pos");
|
||||
|
||||
inp->self_rel_pos_swa = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, attn_ctx->get_swa()->get_n_kv(), ubatch.n_tokens);
|
||||
inp->self_rel_pos_swa = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, inp->get_attn()->self_attn_mask_swa.raw->ne[0], ubatch.n_tokens);
|
||||
ggml_set_input(inp->self_rel_pos_swa);
|
||||
ggml_set_name(inp->self_rel_pos_swa, "attn_inp_rel_pos_swa");
|
||||
|
||||
@@ -4393,7 +4427,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 {
|
||||
@@ -4403,22 +4437,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;
|
||||
|
||||
// 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, attn_mask.rows,
|
||||
sinks, nullptr, kq_scale, il, bias_table, indices);
|
||||
}
|
||||
|
||||
void llm_graph_context::build_dense_out(
|
||||
|
||||
Reference in new issue
Block a user