From f0076dc5a0c898ea42d544950cde4337877c4f79 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sun, 19 Oct 2025 17:20:54 +0300 Subject: [PATCH] metal : adjust .get_alloc_size to be alloc friendly --- ggml/src/ggml-metal/ggml-metal.cpp | 9 +++++++++ src/llama-graph.cpp | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-metal/ggml-metal.cpp b/ggml/src/ggml-metal/ggml-metal.cpp index 7afc881fa7..a8597895d7 100644 --- a/ggml/src/ggml-metal/ggml-metal.cpp +++ b/ggml/src/ggml-metal/ggml-metal.cpp @@ -191,6 +191,15 @@ static size_t ggml_backend_metal_buffer_type_get_alloc_size(ggml_backend_buffer_ res += ggml_metal_op_mul_mat_id_extra_tpe(tensor); res += ggml_metal_op_mul_mat_id_extra_ids(tensor); } break; + case GGML_OP_MUL: + case GGML_OP_ADD_ID: + { + // TODO: ideally this should not be necessary + // ref: https://github.com/ggml-org/llama.cpp/issues/16646#issuecomment-3419232927 + if (tensor->src[0]->op == GGML_OP_ADD_ID || tensor->src[0]->op == GGML_OP_MUL_MAT_ID) { + res = ggml_backend_metal_buffer_type_get_alloc_size(buft, tensor->src[0]); + } + } break; case GGML_OP_FLASH_ATTN_EXT: { res += ggml_metal_op_flash_attn_ext_extra_pad(tensor); diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index f29a1e98c9..9da0912e96 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -1071,7 +1071,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn( if (!weight_before_ffn) { experts = ggml_mul(ctx0, experts, weights); - cb(cur, "ffn_moe_weighted", il); + cb(experts, "ffn_moe_weighted", il); } ggml_tensor * cur_experts[LLAMA_MAX_EXPERTS] = { nullptr };