Skip to content
Open
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
81 changes: 74 additions & 7 deletions src/core/ggml_extend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "core/ggml_extend.h"

#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdexcept>
Expand Down Expand Up @@ -629,6 +630,46 @@ ggml_tensor* ggml_ext_cast_f32(GGMLRunnerContext* runner_ctx, ggml_tensor* a) {
}
}

// Halves chunk until the backend accepts the chunk's ops (Vulkan rejects tensors over its buffer limit and would run
// them on the CPU); keeps chunk if nothing fits.
static int64_t attention_query_chunk_for_backend(ggml_backend_t backend,
const ggml_tensor* q,
const ggml_tensor* k,
const ggml_tensor* v,
int64_t chunk) {
if (backend == nullptr || chunk <= 1) {
return chunk;
}
ggml_init_params params = {ggml_tensor_overhead() * 16 + 1024, nullptr, true};
ggml_context* tmp = ggml_init(params);
if (tmp == nullptr) {
return chunk;
}
int64_t candidate = chunk;
int64_t chosen = chunk;
while (true) {
ggml_tensor* k_t = ggml_new_tensor(tmp, k->type, 4, k->ne);
ggml_tensor* v_t = ggml_new_tensor(tmp, v->type, 4, v->ne);
int64_t q_ne[4] = {q->ne[0], candidate, q->ne[2], q->ne[3]};
ggml_tensor* q_t = ggml_new_tensor(tmp, q->type, 4, q_ne);
ggml_tensor* kq = ggml_mul_mat(tmp, k_t, q_t);
ggml_tensor* sm = ggml_soft_max(tmp, kq);
ggml_tensor* out = ggml_mul_mat(tmp, v_t, sm);
if (ggml_backend_supports_op(backend, kq) && ggml_backend_supports_op(backend, sm) &&
ggml_backend_supports_op(backend, out)) {
chosen = candidate;
break;
}
if (candidate <= 1) {
break;
}
candidate = (candidate + 1) / 2;
ggml_reset(tmp);
}
ggml_free(tmp);
return chosen;
}

ggml_tensor* ggml_ext_attention_ext(GGMLRunnerContext* runner_ctx,
ggml_tensor* q,
ggml_tensor* k,
Expand Down Expand Up @@ -810,15 +851,41 @@ ggml_tensor* ggml_ext_attention_ext(GGMLRunnerContext* runner_ctx,
v = ggml_ext_cont(runner_ctx, ggml_permute(ctx, v, 1, 2, 0, 3)); // [N, n_kv_head, d_head, L_k]
v = ggml_reshape_3d(ctx, v, L_k, d_head, n_kv_head * N); // [N * n_kv_head, d_head, L_k]

auto kq = ggml_mul_mat(ctx, k, q); // [N * n_head, L_q, L_k]
ggml_prec_set_acc(kq, GGML_PREC_F32);
kq = ggml_scale_inplace(ctx, kq, scale);
if (mask) {
kq = ggml_add_inplace(ctx, kq, mask);
// Query chunks of <= 2^29 score elements: the full VAE mid-block score matrix (16 GiB at 2048px) passes the
// 32-bit indexing of some kernels. Rows are independent, so a single chunk builds the same graph as before.
int64_t chunk = L_q;
if (mask == nullptr) {
const int64_t row_elems = L_k * q->ne[2] * q->ne[3];
chunk = std::max<int64_t>(1, (int64_t(1) << 29) / std::max<int64_t>(1, row_elems));
chunk = std::min(chunk, L_q);
chunk = attention_query_chunk_for_backend(runner_ctx->backend, q, k, v, chunk);
}
kq = ggml_soft_max_inplace(ctx, kq);
chunk = std::max<int64_t>(1, chunk);
// Each chunk writes its output over its own query rows in a copy of q and the next chunk slices that copy, so
// chunks run in order: independent branches let Vulkan's graph optimizer keep several score chunks live.
// ggml_set packs strides and offset into int32 op params, hence the 1 GiB bound.
const bool serial = chunk < L_q && q->type == GGML_TYPE_F32 && q->ne[0] == v->ne[1] &&
ggml_nbytes(q) < (size_t(1) << 30);
ggml_tensor* acc = serial ? ggml_dup(ctx, q) : nullptr;
std::vector<ggml_tensor*> outs;
for (int64_t i = 0; i == 0 || i < L_q; i += chunk) { // L_q == 0 still builds one (empty) chunk, as before
auto q_i = chunk < L_q ? ggml_ext_slice(runner_ctx, serial ? acc : q, 1, i, std::min(i + chunk, L_q)) : q;
auto kq = ggml_mul_mat(ctx, k, q_i); // [N * n_head, chunk, L_k]
ggml_prec_set_acc(kq, GGML_PREC_F32);
kq = ggml_scale_inplace(ctx, kq, scale);
if (mask) {
kq = ggml_add_inplace(ctx, kq, mask);
}
kq = ggml_soft_max_inplace(ctx, kq);

kqv = ggml_mul_mat(ctx, v, kq); // [N * n_head, L_q, d_head]
auto out = ggml_mul_mat(ctx, v, kq); // [N * n_head, chunk, d_head]
if (serial) {
acc = ggml_set_inplace(ctx, acc, out, acc->nb[1], acc->nb[2], acc->nb[3], i * acc->nb[1]);
} else {
outs.push_back(out);
}
}
kqv = serial ? acc : ggml_ext_vec_concat(runner_ctx, outs, 1); // [N * n_head, L_q, d_head]

kqv = ggml_reshape_4d(ctx, kqv, d_head, L_q, n_head, N); // [N, n_head, L_q, d_head]
kqv = ggml_permute(ctx, kqv, 0, 2, 1, 3); // [N, L_q, n_head, d_head]
Expand Down
Loading