Commit 75bfc7de2 for llama.cpp

commit 75bfc7de2e3ef91254aae7dac51a382465b66e76
Author: Georgi Gerganov <ggerganov@gmail.com>
Date:   Tue Dec 1 12:37:31 2026 +0200

    metal : keep the MUL_MAT weight scale off skipped MoE slots (#33430)

    * metal : keep skipped MoE slots unscaled with a per-tensor scale

    kernel_mul_mat_scale reads the expert ids only when the weight scale is
    per-expert or per-channel-per-expert, so a per-tensor scale also scaled
    the slots of a skipped expert. The MoE matmul kernels zero those slots,
    which turns 0 * +inf into NaN and fails the MUL_MAT_ID scale_gran=3
    tests. Always pass the ids for MUL_MAT_ID and separate the skip check
    from the per-expert scale offset.

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)

    * metal : keep the matmul weight scale inside the matmul op

    The scale pass had to be chained by hand after ggml_metal_op_mul_mat and
    ggml_metal_op_mul_mat_id in the encoder switch, and the two cases dropped
    its return value on the way. Make it a private tail step of both ops, the
    same way kernel_mul_mm_id_map0 and kernel_mul_mm_id_zero belong to
    ggml_metal_op_mul_mat_id, and assert the invariant that made ignoring the
    fused result safe: a scaled matmul never fuses a following ADD.

    No functional change.

    Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)

diff --git a/ggml/src/ggml-metal/ggml-metal-impl.h b/ggml/src/ggml-metal/ggml-metal-impl.h
index c7e27dce1..8a5713709 100644
--- a/ggml/src/ggml-metal/ggml-metal-impl.h
+++ b/ggml/src/ggml-metal/ggml-metal-impl.h
@@ -302,6 +302,7 @@ typedef struct {
     uint64_t ids_nb0;
     uint64_t ids_nb1;
     int32_t  use_ids;
+    int32_t  has_ids;
 } ggml_metal_kargs_mul_mat_scale;

 typedef struct {
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index 162d53733..8856ef254 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -388,12 +388,10 @@ static int ggml_metal_op_encode_impl(ggml_metal_op_t ctx, int idx) {
         case GGML_OP_MUL_MAT:
             {
                 n_fuse = ggml_metal_op_mul_mat(ctx, idx);
-                ggml_metal_op_mul_mat_scale(ctx, idx);
             } break;
         case GGML_OP_MUL_MAT_ID:
             {
                 n_fuse = ggml_metal_op_mul_mat_id(ctx, idx);
-                ggml_metal_op_mul_mat_scale(ctx, idx);
             } break;
         case GGML_OP_GET_ROWS:
             {
@@ -2771,7 +2769,59 @@ static int ggml_metal_op_mul_mat_mv(ggml_metal_op_t ctx, int idx) {
     return 1;
 }

-int ggml_metal_op_mul_mat(ggml_metal_op_t ctx, int idx) {
+// apply the weight scale of a MUL_MAT or MUL_MAT_ID built by ggml_mul_mat_ext / ggml_mul_mat_id_ext
+// the matmul kernels write the unscaled result, so this pass runs after them with a barrier
+static void ggml_metal_op_mul_mat_scale(ggml_metal_op_t ctx, const ggml_tensor * op) {
+    const ggml_tensor * scale = ggml_get_src_scale(op, 0);
+    if (scale == nullptr) {
+        return;
+    }
+
+    ggml_metal_library_t lib = ctx->lib;
+    ggml_metal_encoder_t enc = ctx->enc;
+
+    GGML_ASSERT(scale->type == GGML_TYPE_F32);
+    GGML_ASSERT(op->type    == GGML_TYPE_F32);
+    GGML_ASSERT(ggml_is_contiguous_rows(op));
+
+    const ggml_tensor * src0 = op->src[0];
+    const ggml_tensor * ids  = op->op == GGML_OP_MUL_MAT_ID ? op->src[2] : nullptr;
+
+    const bool has_ids            = ids != nullptr;
+    const bool per_channel_expert = has_ids && scale->ne[0] == op->ne[0] && scale->ne[1] == src0->ne[2];
+    const bool per_expert         = has_ids && (ggml_nelements(scale) == src0->ne[2] || per_channel_expert);
+    const bool per_channel        = per_expert ? per_channel_expert : scale->ne[0] == op->ne[0] && ggml_nelements(scale) > 1;
+
+    ggml_metal_kargs_mul_mat_scale args = {
+        /*.ne0     =*/ op->ne[0],
+        /*.ne2     =*/ op->ne[2],
+        /*.nb1     =*/ op->nb[1],
+        /*.nb2     =*/ op->nb[2],
+        /*.nb3     =*/ op->nb[3],
+        /*.s_nb0   =*/ per_channel ? scale->nb[0] : 0,
+        /*.s_nb1   =*/ per_expert ? (per_channel_expert ? scale->nb[1] : scale->nb[0]) : 0,
+        /*.ids_nb0 =*/ has_ids ? ids->nb[0] : 0,
+        /*.ids_nb1 =*/ has_ids ? ids->nb[1] : 0,
+        /*.use_ids =*/ per_expert,
+        /*.has_ids =*/ has_ids,
+    };
+
+    ggml_metal_op_concurrency_reset(ctx);
+
+    auto pipeline = ggml_metal_library_get_pipeline_mul_mat_scale(lib);
+
+    ggml_metal_encoder_set_pipeline(enc, pipeline);
+    ggml_metal_encoder_set_bytes   (enc, &args, sizeof(args), 0);
+    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(scale),                 1);
+    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(has_ids ? ids : scale), 2);
+    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(op),                    3);
+
+    const int nth = std::min<int64_t>(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), op->ne[0]);
+
+    ggml_metal_encoder_dispatch_threadgroups(enc, op->ne[1], op->ne[2]*op->ne[3], 1, nth, 1, 1);
+}
+
+static int ggml_metal_op_mul_mat_impl(ggml_metal_op_t ctx, int idx) {
     ggml_tensor * op = ctx->node(idx);

     ggml_metal_library_t lib = ctx->lib;
@@ -2957,6 +3007,18 @@ int ggml_metal_op_mul_mat(ggml_metal_op_t ctx, int idx) {
     return 1;
 }

+int ggml_metal_op_mul_mat(ggml_metal_op_t ctx, int idx) {
+    ggml_tensor * op = ctx->node(idx);
+
+    const int n_fuse = ggml_metal_op_mul_mat_impl(ctx, idx);
+
+    GGML_ASSERT(ggml_get_src_scale(op, 0) == nullptr || n_fuse == 1);
+
+    ggml_metal_op_mul_mat_scale(ctx, op);
+
+    return n_fuse;
+}
+
 // upper bound on the number of entry tiles emitted by kernel_mul_mm_id_map0: the full tiles
 // over all entries plus at most one partial tile per expert
 static int64_t ggml_metal_op_mul_mat_id_max_tiles(const ggml_tensor * op) {
@@ -2992,7 +3054,7 @@ size_t ggml_metal_op_mul_mat_id_extra_ids(const ggml_tensor * op) {
     return ggml_type_size(GGML_TYPE_I32)*(ne20*ne21 + ne02);
 }

-int ggml_metal_op_mul_mat_id(ggml_metal_op_t ctx, int idx) {
+static int ggml_metal_op_mul_mat_id_impl(ggml_metal_op_t ctx, int idx) {
     ggml_tensor * op = ctx->node(idx);

     ggml_metal_library_t lib = ctx->lib;
@@ -3195,6 +3257,18 @@ int ggml_metal_op_mul_mat_id(ggml_metal_op_t ctx, int idx) {
     return 1;
 }

+int ggml_metal_op_mul_mat_id(ggml_metal_op_t ctx, int idx) {
+    ggml_tensor * op = ctx->node(idx);
+
+    const int n_fuse = ggml_metal_op_mul_mat_id_impl(ctx, idx);
+
+    GGML_ASSERT(ggml_get_src_scale(op, 0) == nullptr || n_fuse == 1);
+
+    ggml_metal_op_mul_mat_scale(ctx, op);
+
+    return n_fuse;
+}
+
 int ggml_metal_op_add_id(ggml_metal_op_t ctx, int idx) {
     ggml_tensor * op = ctx->node(idx);

@@ -3241,58 +3315,6 @@ int ggml_metal_op_add_id(ggml_metal_op_t ctx, int idx) {
     return 1;
 }

-int ggml_metal_op_mul_mat_scale(ggml_metal_op_t ctx, int idx) {
-    ggml_tensor * op = ctx->node(idx);
-
-    const ggml_tensor * scale = ggml_get_src_scale(op, 0);
-    if (scale == nullptr) {
-        return 1;
-    }
-
-    ggml_metal_library_t lib = ctx->lib;
-    ggml_metal_encoder_t enc = ctx->enc;
-
-    GGML_ASSERT(scale->type == GGML_TYPE_F32);
-    GGML_ASSERT(op->type    == GGML_TYPE_F32);
-    GGML_ASSERT(ggml_is_contiguous_rows(op));
-
-    const ggml_tensor * src0 = op->src[0];
-    const ggml_tensor * ids  = op->op == GGML_OP_MUL_MAT_ID ? op->src[2] : nullptr;
-
-    const bool per_channel_expert = ids && scale->ne[0] == op->ne[0] && scale->ne[1] == src0->ne[2];
-    const bool per_expert         = ids && (ggml_nelements(scale) == src0->ne[2] || per_channel_expert);
-    const bool per_channel        = per_expert ? per_channel_expert : scale->ne[0] == op->ne[0] && ggml_nelements(scale) > 1;
-
-    ggml_metal_kargs_mul_mat_scale args = {
-        /*.ne0     =*/ op->ne[0],
-        /*.ne2     =*/ op->ne[2],
-        /*.nb1     =*/ op->nb[1],
-        /*.nb2     =*/ op->nb[2],
-        /*.nb3     =*/ op->nb[3],
-        /*.s_nb0   =*/ per_channel ? scale->nb[0] : 0,
-        /*.s_nb1   =*/ per_expert ? (per_channel_expert ? scale->nb[1] : scale->nb[0]) : 0,
-        /*.ids_nb0 =*/ per_expert ? ids->nb[0] : 0,
-        /*.ids_nb1 =*/ per_expert ? ids->nb[1] : 0,
-        /*.use_ids =*/ per_expert,
-    };
-
-    ggml_metal_op_concurrency_reset(ctx);
-
-    auto pipeline = ggml_metal_library_get_pipeline_mul_mat_scale(lib);
-
-    ggml_metal_encoder_set_pipeline(enc, pipeline);
-    ggml_metal_encoder_set_bytes   (enc, &args, sizeof(args), 0);
-    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(scale),                  1);
-    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(per_expert ? ids : scale), 2);
-    ggml_metal_encoder_set_buffer  (enc, ggml_metal_get_buffer_id(op),                     3);
-
-    const int nth = std::min<int64_t>(ggml_metal_pipeline_max_theads_per_threadgroup(pipeline), op->ne[0]);
-
-    ggml_metal_encoder_dispatch_threadgroups(enc, op->ne[1], op->ne[2]*op->ne[3], 1, nth, 1, 1);
-
-    return 1;
-}
-
 bool ggml_metal_op_flash_attn_ext_use_vec(const ggml_tensor * op) {
     assert(op->op == GGML_OP_FLASH_ATTN_EXT);

diff --git a/ggml/src/ggml-metal/ggml-metal-ops.h b/ggml/src/ggml-metal/ggml-metal-ops.h
index a775e88ec..2330bff1e 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.h
+++ b/ggml/src/ggml-metal/ggml-metal-ops.h
@@ -83,7 +83,6 @@ int ggml_metal_op_pool_2d           (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_fwht              (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_mul_mat           (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_mul_mat_id        (ggml_metal_op_t ctx, int idx);
-int ggml_metal_op_mul_mat_scale     (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_add_id            (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_flash_attn_ext    (ggml_metal_op_t ctx, int idx);
 int ggml_metal_op_bin               (ggml_metal_op_t ctx, int idx);
diff --git a/ggml/src/ggml-metal/kernels/binbcast.metal b/ggml/src/ggml-metal/kernels/binbcast.metal
index bfc0f1cc8..955739728 100644
--- a/ggml/src/ggml-metal/kernels/binbcast.metal
+++ b/ggml/src/ggml-metal/kernels/binbcast.metal
@@ -219,12 +219,15 @@ kernel void kernel_mul_mat_scale(

     device const char * s = scale;

-    if (args.use_ids) {
+    if (args.has_ids) {
+        // a skipped expert (id < 0) has zeroed output, do not scale it
         const int32_t expert = *((device const int32_t *) (ids + i1*args.ids_nb0 + i2*args.ids_nb1));
         if (expert < 0) {
             return;
         }
-        s += expert*args.s_nb1;
+        if (args.use_ids) {
+            s += expert*args.s_nb1;
+        }
     }

     device float * dst_row = (device float *) (dst + i1*args.nb1 + i2*args.nb2 + i3*args.nb3);