Commit 115deee8e for llama.cpp
commit 115deee8ecfbbacb1ab8a6137b62aac1f2f0724d
Author: Georgi Gerganov <ggerganov@gmail.com>
Date: Sat Nov 28 17:42:15 2026 +0200
metal : keep ADD chain fusion independent of batch size (#33217)
The ADD chain fusion packs the addends only when they share a layout and a backend buffer. A bias add and a residual add share a layout only for single-token batches, and live in different buffers, so the packing and the following node reorder depend on the ubatch size. The scheduler reserves one graph and aborts on an unexpected reallocation when the node order changes, for example with nemotron_h.
Require all addends in a chain to live in the same backend buffer in both the structural and the full checks.
Tested with test-llama-context (nemotron_h-dense.gguf, GGML_SCHED_DEBUG_REALLOC=1), test-metal-graph-optimize, test-fusion against tests/fusion/MTL.csv, and llama-speculative on m5-1 at 4- and 16-row verification batches.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
diff --git a/ggml/src/ggml-metal/ggml-metal-fusion.cpp b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
index 397d8ce21..f5e18f25d 100644
--- a/ggml/src/ggml-metal/ggml-metal-fusion.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
@@ -73,8 +73,8 @@ ggml_metal_fusion_id ggml_metal_fusion_get_id(const ggml_metal_fusion * fusion)
// ---- helpers -------------------------------------------------------------
-// true if two tensors live in the same Metal buffer
-static bool ggml_metal_fusion_same_buffer(const ggml_tensor * a, const ggml_tensor * b) {
+// true if two tensors live in the same Metal buffer (in STRUCTURAL mode, only checks the backend buffer)
+static bool ggml_metal_fusion_same_buffer(const ggml_tensor * a, const ggml_tensor * b, ggml_metal_fusion_mode mode) {
if (!a || !b) {
return false;
}
@@ -82,6 +82,14 @@ static bool ggml_metal_fusion_same_buffer(const ggml_tensor * a, const ggml_tens
ggml_backend_buffer_t ba = a->view_src ? a->view_src->buffer : a->buffer;
ggml_backend_buffer_t bb = b->view_src ? b->view_src->buffer : b->buffer;
+ if (ba != bb) {
+ return false;
+ }
+
+ if (mode == GGML_METAL_FUSION_STRUCTURAL) {
+ return true;
+ }
+
ggml_metal_buffer_t ca = (ggml_metal_buffer_t) ba->context;
ggml_metal_buffer_t cb = (ggml_metal_buffer_t) bb->context;
@@ -188,8 +196,7 @@ static bool ggml_metal_fusion_check_ssm_conv_silu(
return true;
}
-// ADD x N: each ADD reads the previous ADD as src0, and all addends must share layout
-// (and, in FULL mode, live in the same Metal buffer)
+// ADD x N: each ADD reads the previous ADD as src0, and all addends must share layout and buffer
static bool ggml_metal_fusion_check_add_chain(
const ggml_metal_fusion * fusion,
const ggml_tensor * const * nodes,
@@ -211,10 +218,8 @@ static bool ggml_metal_fusion_check_add_chain(
return false;
}
- if (mode == GGML_METAL_FUSION_FULL) {
- if (!ggml_metal_fusion_same_buffer(nodes[j]->src[1], nodes[0]->src[1])) {
- return false;
- }
+ if (!ggml_metal_fusion_same_buffer(nodes[j]->src[1], nodes[0]->src[1], mode)) {
+ return false;
}
}