Skip to content

Commit 6a746cf

Browse files
authored
vulkan: Split large mul_mat_id to fit in shared memory (#14451)
1 parent eff5e45 commit 6a746cf

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5966,7 +5966,30 @@ static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx
59665966
if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
59675967
ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
59685968
} else {
5969-
ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
5969+
// Split based on number of ids, to fit in shared memory
5970+
const uint32_t nei0 = (uint32_t)src2->ne[0];
5971+
const uint32_t nei1 = (uint32_t)src2->ne[1];
5972+
5973+
GGML_ASSERT(nei0 <= 4096);
5974+
const uint32_t split_size = std::min(nei1, 4096u / nei0);
5975+
5976+
ggml_tensor src1_copy = *src1;
5977+
ggml_tensor src2_copy = *src2;
5978+
ggml_tensor dst_copy = *dst;
5979+
5980+
for (uint32_t token_start = 0; token_start < nei1; token_start += split_size) {
5981+
const uint32_t n_tokens = std::min(split_size, nei1 - token_start);
5982+
5983+
src1_copy.view_offs = src1->view_offs + token_start * src1_copy.nb[2];
5984+
src2_copy.view_offs = src2->view_offs + token_start * src2_copy.nb[1];
5985+
dst_copy.view_offs = dst->view_offs + token_start * dst_copy.nb[2];
5986+
5987+
src1_copy.ne[2] = n_tokens;
5988+
src2_copy.ne[1] = n_tokens;
5989+
dst_copy.ne[2] = n_tokens;
5990+
5991+
ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, &src1_copy, &src2_copy, &dst_copy, dryrun);
5992+
}
59705993
}
59715994
}
59725995

@@ -10135,9 +10158,15 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
1013510158
ggml_type src0_type = op->src[0]->type;
1013610159
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
1013710160
const vk_device& device = ggml_vk_get_device(ctx->device);
10138-
if (op->op == GGML_OP_MUL_MAT_ID && !device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
10139-
// If there's not enough shared memory for row_ids and the result tile, fallback to CPU
10140-
return false;
10161+
if (op->op == GGML_OP_MUL_MAT_ID) {
10162+
if (!device->mul_mat_id_s[src0_type] && !device->mul_mat_id_m[src0_type] && !device->mul_mat_id_l[src0_type]) {
10163+
// If there's not enough shared memory for row_ids and the result tile, fallback to CPU
10164+
return false;
10165+
}
10166+
// Check against size of shared memory variable
10167+
if (op->src[2]->ne[0] > 4096) {
10168+
return false;
10169+
}
1014110170
}
1014210171
switch (src0_type) {
1014310172
case GGML_TYPE_F32:

tests/test-backend-ops.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,6 +4623,11 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
46234623
// this case is verified (pass) in Intel(R) Data Center GPU Max 1100 (sycl backend) and NV A30 (cuda backend)
46244624
// test_cases.emplace_back(new test_mul_mat(GGML_TYPE_F16, GGML_TYPE_F16, 512, 262144, 9216, {1, 1}, {1, 1}));
46254625

4626+
// test large experts*tokens
4627+
for (bool b : {false, true}) {
4628+
test_cases.emplace_back(new test_mul_mat_id(GGML_TYPE_F16, GGML_TYPE_F32, 16, 16, b, 32, 1024, 16));
4629+
}
4630+
46264631
for (ggml_type type_a : base_types) {
46274632
for (ggml_type type_b : {GGML_TYPE_F32 /*, GGML_TYPE_F16 */}) {
46284633
for (int n_mats : {4, 8}) {

0 commit comments

Comments
 (0)