Skip to content

LLM inference in C/C++

C++ ggml

metal : fix moe_reduce fusion race when dst aliases its inputs (#30795)

Georgi Gerganov098ce526b546
diff --git a/ggml/src/ggml-metal/ggml-metal-fusion.cpp b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
index d78ca7c4b7..020cad29d1 100644
--- a/ggml/src/ggml-metal/ggml-metal-fusion.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-fusion.cpp
@@ -636,6 +636,11 @@ static bool ggml_metal_fusion_check_moe_reduce(
if (!match.experts->data || !match.weights->data || !match.dst->data) {
return false;
}
+
+ // the fused kernel reads experts and weights while writing dst, so dst must not overlap either input
+ if (ggml_metal_fusion_overlap(match.dst, match.experts) || ggml_metal_fusion_overlap(match.dst, match.weights)) {
+ return false;
+ }
}
return true;