@@ -5966,7 +5966,30 @@ static void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx
5966
5966
if (src2->ne[1] == 1 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type))) {
5967
5967
ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, src0, src1, src2, dst, dryrun);
5968
5968
} 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
+ }
5970
5993
}
5971
5994
}
5972
5995
@@ -10135,9 +10158,15 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
10135
10158
ggml_type src0_type = op->src[0]->type;
10136
10159
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
10137
10160
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
+ }
10141
10170
}
10142
10171
switch (src0_type) {
10143
10172
case GGML_TYPE_F32:
0 commit comments