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
23 changes: 18 additions & 5 deletions ggml/src/ggml-vulkan/ggml-vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10629,11 +10629,24 @@ static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_conte
}
}

static bool ggml_vk_use_mul_mat_vec_id(const struct ggml_cgraph * cgraph, int node_idx) {
static bool ggml_vk_use_mul_mat_vec_id(const ggml_backend_vk_context * ctx, const struct ggml_cgraph * cgraph, int node_idx) {
ggml_tensor * dst = cgraph->nodes[node_idx];
ggml_tensor * src0 = dst->src[0];
ggml_tensor * src2 = dst->src[2];
return (src2->ne[1] <= 8) && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type));

bool use_vec_id = src2->ne[1] <= 8;
if (!ctx->device->coopmat2) {
// The tiled path is slow at small batch without coopmat2 (e.g. the
// decode cliff at 9+ sequences on gfx1013/gfx1100/gfx1151 and NV
// coopmat1). Keep the vector path while the routed density is low.
// coopmat2 (Blackwell) handles small batches fine - keep the cutoff.
const int64_t n_tokens = src2->ne[1];
const int64_t n_per_tok = src2->ne[0];
const int64_t n_experts = src0->ne[2];
use_vec_id = (n_tokens * n_per_tok <= 2 * n_experts) && (n_tokens <= 64);
}

return use_vec_id && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type));
}

static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) {
Expand All @@ -10642,7 +10655,7 @@ static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx
ggml_tensor * src1 = dst->src[1];
ggml_tensor * src2 = dst->src[2];
VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
if (ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
if (ggml_vk_use_mul_mat_vec_id(ctx, cgraph, node_idx)) {
ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, cgraph, node_idx);
} else {
ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst);
Expand Down Expand Up @@ -16480,7 +16493,7 @@ static bool ggml_vk_can_fuse(const ggml_backend_vk_context * ctx, const struct g
return false;
}
// mat-vec only
if (!ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
if (!ggml_vk_use_mul_mat_vec_id(ctx, cgraph, node_idx)) {
return false;
}
// shaders assume the types match
Expand Down Expand Up @@ -16515,7 +16528,7 @@ static bool ggml_vk_can_fuse(const ggml_backend_vk_context * ctx, const struct g
return false;
}
// mat-vec only
if (!ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
if (!ggml_vk_use_mul_mat_vec_id(ctx, cgraph, node_idx)) {
return false;
}
// shaders assume the types match
Expand Down
Loading