Commit e18d1b215 for llama.cpp

commit e18d1b21505423004e48b85403e3c699407e97c1
Author: cyxu0401 <162444998+cyxu0401@users.noreply.github.com>
Date:   Mon Nov 30 01:38:42 2026 +0800

    models : add MiniCPM5 MoE support (#32644)

    * models: add MiniCPM5 MoE support

    Assisted-by: Codex
    (cherry picked from commit 7f63c7e3e3258dd3c769c7cc9dfa03ae9a23a1f4)

    * docs: clarify MiniCPM5 MoE DSpark startup options

    Assisted-by: Codex

    * models: preserve MiniCPM5 MoE HF BOS setting

    * convert: use the released MiniCPM5 MoE tokenizer settings

    Assisted-by: Codex

    * tests: remove MiniCPM5 MoE conversion tests

    Assisted-by: Codex

    * models: address MiniCPM5 MoE converter and graph review

    Assisted-by: Codex

    * models: default unset MiniCPM5 MoE gating to sigmoid

    Assisted-by: Codex

    * tests: update the MiniCPM5 MoE Metal fusion baseline

    Drop the model and speculative decoding documentation changes from this PR.

    Assisted-by: Codex

    ---------

    Co-authored-by: xuchengyang <xuchengyang@modelbest.cn>

diff --git a/conversion/__init__.py b/conversion/__init__.py
index 679682010..6b54c88e8 100644
--- a/conversion/__init__.py
+++ b/conversion/__init__.py
@@ -215,6 +215,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
     "MiMoV2FlashForCausalLM": "mimo",
     "MiMoV2ForCausalLM": "mimo",
     "MiniCPM3ForCausalLM": "minicpm",
+    "MiniCPM5MoEForCausalLM": "minicpm",
     "MiniCPMForCausalLM": "minicpm",
     "MiniCPMV4_6ForConditionalGeneration": "minicpm",
     "MiniCPMV4_7ForConditionalGeneration": "minicpm",
diff --git a/conversion/minicpm.py b/conversion/minicpm.py
index 2b239437c..d230521dc 100644
--- a/conversion/minicpm.py
+++ b/conversion/minicpm.py
@@ -61,6 +61,56 @@ class MiniCPMModel(TextModel):
         yield from super().modify_tensors(data_torch, name, bid)


+@ModelBase.register("MiniCPM5MoEForCausalLM")
+class MiniCPM5MoEModel(TextModel):
+    model_arch = gguf.MODEL_ARCH.MINICPM5_MOE
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        if not self.hparams.get("use_gated_attention", False):
+            raise ValueError("MiniCPM5 MoE requires gated attention")
+        self._experts: list[dict[str, Tensor]] = [{} for _ in range(self.block_count)]
+
+    def set_gguf_parameters(self):
+        super().set_gguf_parameters()
+        self.gguf_writer.add_vocab_size(self.hparams["vocab_size"])
+        head_dim = self.hparams.get("head_dim", self.hparams["hidden_size"] // self.hparams["num_attention_heads"])
+        self.gguf_writer.add_rope_dimension_count(head_dim)
+        self.gguf_writer.add_expert_feed_forward_length(self.hparams["moe_intermediate_size"])
+        self.gguf_writer.add_expert_shared_count(self.hparams["n_shared_experts"])
+        self.gguf_writer.add_leading_dense_block_count(self.hparams["first_k_dense_replace"])
+        self.gguf_writer.add_expert_weights_norm(self.hparams.get("norm_topk_prob", True))
+        self.gguf_writer.add_expert_weights_scale(self.hparams["routed_scaling_factor"])
+
+    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
+        if name.endswith("self_attn.q_proj.weight"):
+            n_head = self.hparams["num_attention_heads"]
+            head_dim = self.hparams.get("head_dim", self.hparams["hidden_size"] // n_head)
+            # Each HF head stores its query rows followed by its gate rows.
+            query, gate = data_torch.reshape(n_head, 2 * head_dim, -1).chunk(2, dim=1)
+            query = query.reshape(n_head * head_dim, -1)
+            gate = gate.reshape(n_head * head_dim, -1)
+            yield self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_Q, bid), query
+            yield self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_GATE, bid), gate
+            return
+
+        if ".mlp.experts." in name:
+            n_experts = self.hparams["n_routed_experts"]
+            assert bid is not None
+            self._experts[bid][name] = data_torch
+            if len(self._experts[bid]) == n_experts * 3:
+                for projection in ("gate_proj", "up_proj", "down_proj"):
+                    tensors = [
+                        self._experts[bid].pop(f"model.layers.{bid}.mlp.experts.{eid}.{projection}.weight")
+                        for eid in range(n_experts)
+                    ]
+                    merged = torch.stack(tensors, dim=0)
+                    yield from super().modify_tensors(merged, f"model.layers.{bid}.mlp.experts.{projection}.weight", bid)
+            return
+
+        yield from super().modify_tensors(data_torch, name, bid)
+
+
 @ModelBase.register("MiniCPM3ForCausalLM")
 @ModelBase.example("openbmb/MiniCPM3-4B")
 class MiniCPM3Model(TextModel):
diff --git a/gguf-py/gguf/constants.py b/gguf-py/gguf/constants.py
index 347378e61..61caa8ad5 100644
--- a/gguf-py/gguf/constants.py
+++ b/gguf-py/gguf/constants.py
@@ -733,6 +733,7 @@ class MODEL_ARCH(IntEnum):
     DOTS3NOTE        = auto()
     ARCEE            = auto()
     AFMOE            = auto()
+    MINICPM5_MOE     = auto()
     LAGUNA           = auto()
     RICURSIVE        = auto()
     ERNIE4_5         = auto()
@@ -1763,6 +1764,7 @@ MODEL_ARCH_NAMES: dict[MODEL_ARCH, str] = {
     MODEL_ARCH.DOTS3NOTE:        "dots3note",
     MODEL_ARCH.ARCEE:            "arcee",
     MODEL_ARCH.AFMOE:            "afmoe",
+    MODEL_ARCH.MINICPM5_MOE:     "minicpm5_moe",
     MODEL_ARCH.LAGUNA:           "laguna",
     MODEL_ARCH.RICURSIVE:        "ricursive",
     MODEL_ARCH.ERNIE4_5:         "ernie4_5",
@@ -5901,6 +5903,30 @@ MODEL_TENSORS: dict[MODEL_ARCH, list[MODEL_TENSOR]] = {
         MODEL_TENSOR.FFN_UP,
         MODEL_TENSOR.FFN_POST_NORM,
     ],
+    MODEL_ARCH.MINICPM5_MOE: [
+        MODEL_TENSOR.TOKEN_EMBD,
+        MODEL_TENSOR.OUTPUT_NORM,
+        MODEL_TENSOR.OUTPUT,
+        MODEL_TENSOR.ATTN_NORM,
+        MODEL_TENSOR.ATTN_QKV,
+        MODEL_TENSOR.ATTN_Q,
+        MODEL_TENSOR.ATTN_K,
+        MODEL_TENSOR.ATTN_V,
+        MODEL_TENSOR.ATTN_OUT,
+        MODEL_TENSOR.ATTN_GATE,
+        MODEL_TENSOR.FFN_GATE,
+        MODEL_TENSOR.FFN_DOWN,
+        MODEL_TENSOR.FFN_UP,
+        MODEL_TENSOR.FFN_GATE_INP,
+        MODEL_TENSOR.FFN_GATE_EXP,
+        MODEL_TENSOR.FFN_DOWN_EXP,
+        MODEL_TENSOR.FFN_UP_EXP,
+        MODEL_TENSOR.FFN_GATE_SHEXP,
+        MODEL_TENSOR.FFN_UP_SHEXP,
+        MODEL_TENSOR.FFN_DOWN_SHEXP,
+        MODEL_TENSOR.FFN_NORM,
+        MODEL_TENSOR.FFN_EXP_PROBS_B,
+    ],
     MODEL_ARCH.LAGUNA: [
         MODEL_TENSOR.TOKEN_EMBD,
         MODEL_TENSOR.OUTPUT_NORM,
diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index fe0b808b3..e2fb51cdd 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -126,6 +126,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
     { LLM_ARCH_DOTS3NOTE,        "dots3note"        },
     { LLM_ARCH_ARCEE,            "arcee"            },
     { LLM_ARCH_AFMOE,            "afmoe"            },
+    { LLM_ARCH_MINICPM5_MOE,     "minicpm5_moe"     },
     { LLM_ARCH_LAGUNA,           "laguna"           },
     { LLM_ARCH_ERNIE4_5,         "ernie4_5"         },
     { LLM_ARCH_ERNIE4_5_MOE,     "ernie4_5-moe"     },
diff --git a/src/llama-arch.h b/src/llama-arch.h
index 305aae22a..50e3bc839 100644
--- a/src/llama-arch.h
+++ b/src/llama-arch.h
@@ -130,6 +130,7 @@ enum llm_arch {
     LLM_ARCH_DOTS3NOTE,
     LLM_ARCH_ARCEE,
     LLM_ARCH_AFMOE,
+    LLM_ARCH_MINICPM5_MOE,
     LLM_ARCH_LAGUNA,
     LLM_ARCH_ERNIE4_5,
     LLM_ARCH_ERNIE4_5_MOE,
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index f06ea2ec0..f391b89da 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -301,6 +301,8 @@ static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params
             return new llama_model_arcee(params);
         case LLM_ARCH_AFMOE:
             return new llama_model_afmoe(params);
+        case LLM_ARCH_MINICPM5_MOE:
+            return new llama_model_minicpm5_moe(params);
         case LLM_ARCH_LAGUNA:
             return new llama_model_laguna(params);
         case LLM_ARCH_RICURSIVE:
@@ -3908,6 +3910,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
         case LLM_ARCH_COGVLM:
         case LLM_ARCH_PANGU_EMBED:
         case LLM_ARCH_AFMOE:
+        case LLM_ARCH_MINICPM5_MOE:
         case LLM_ARCH_LAGUNA:
         case LLM_ARCH_QWEN3NEXT:
         case LLM_ARCH_CLOVER_MOE:
diff --git a/src/models/minicpm5-moe.cpp b/src/models/minicpm5-moe.cpp
new file mode 100644
index 000000000..cf0ceba0d
--- /dev/null
+++ b/src/models/minicpm5-moe.cpp
@@ -0,0 +1,221 @@
+#include "models.h"
+
+void llama_model_minicpm5_moe::load_arch_hparams(llama_model_loader & ml) {
+    if (hparams.n_embd_head_k_full != hparams.n_embd_head_v_full) {
+        throw std::runtime_error("MiniCPM5 MoE requires equal key and value head sizes");
+    }
+
+    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
+    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,   hparams.n_layer_dense_lead);
+    ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all);
+    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,         hparams.n_expert_shared);
+    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,        hparams.expert_weights_scale);
+    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,         hparams.expert_weights_norm);
+    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,         hparams.expert_gating_func, false);
+
+    if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
+        hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
+    }
+    type = LLM_TYPE_UNKNOWN;
+}
+
+void llama_model_minicpm5_moe::load_arch_tensors(llama_model_loader &) {
+    LLAMA_LOAD_LOCALS;
+    const int64_t n_expert_shared = hparams.n_expert_shared;
+
+    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
+
+    // output
+    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
+    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
+
+    // if output is NULL, init from the input tok embed
+    if (output == NULL) {
+        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
+    }
+
+    const int64_t n_ff_exp = hparams.n_ff_exp();
+
+    for (int i = 0; i < n_layer; ++i) {
+        auto & layer = layers[i];
+
+        // attention pre-normalization
+        layer.attn_norm      = create_tensor(tn(LLM_TENSOR_ATTN_NORM,      "weight", i), {n_embd}, 0);
+
+        // attention projections
+        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
+        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
+
+        // attention gating
+        layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
+
+        // FFN pre-normalization
+        layer.ffn_norm      = create_tensor(tn(LLM_TENSOR_FFN_NORM,      "weight", i), {n_embd}, 0);
+
+        if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) {
+            // MoE layers
+            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
+            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);
+
+            // grouped expert weights
+            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
+            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
+            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
+
+            // shared expert
+            if (n_expert_shared > 0) {
+                const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;
+                layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0);
+                layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
+                layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, 0);
+            }
+        } else {
+            // Dense layers
+            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
+            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
+            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
+        }
+    }
+}
+
+std::unique_ptr<llm_graph_context> llama_model_minicpm5_moe::build_arch_graph(const llm_graph_params & params) const {
+    return std::make_unique<graph>(params);
+}
+
+llama_model_minicpm5_moe::graph::graph(const llm_graph_params & params) : llm_graph_context(params) {
+    const int64_t n_embd_head = hparams.n_embd_head_v();
+
+    ggml_tensor * cur;
+    ggml_tensor * inpL;
+
+    inpL = build_inp_embd(model.tok_embd);
+
+    // inp_pos - contains the positions
+    ggml_tensor * inp_pos = build_inp_pos();
+    auto * inp_attn = build_attn_inp_kv();
+    ggml_tensor * inp_out_ids = build_inp_out_ids();
+
+    const float kq_scale = 1.0f/sqrtf(float(n_embd_head));
+
+    for (int il = 0; il < n_layer; ++il) {
+        res->t_layer_inp[il] = inpL;
+
+        ggml_tensor * inpSA = inpL;
+
+        // attention pre-normalization (pre)
+        cur = build_norm(inpL,
+                model.layers[il].attn_norm, NULL,
+                LLM_NORM_RMS, il);
+        cb(cur, "attn_norm", il);
+
+        // self-attention
+        {
+            ggml_tensor * attn_inp = cur;  // save input for gate computation
+
+            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
+                    n_embd_head, n_head, n_head_kv, il);
+
+            // compute gate from input
+            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
+            cb(gate, "attn_gate_proj", il);
+
+            Qcur = build_rope(Qcur, inp_pos, il);
+            cb(Qcur, "Qcur_rope", il);
+
+            Kcur = build_rope(Kcur, inp_pos, il);
+            cb(Kcur, "Kcur_rope", il);
+
+            cur = build_attn(inp_attn,
+                    NULL, NULL,  // wo will be applied after gating
+                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
+            cb(cur, "attn_out", il);
+
+            // attention gating: attn_out * sigmoid(gate) BEFORE o_proj
+            gate = ggml_sigmoid(ctx0, gate);
+            cb(gate, "attn_gate_sig", il);
+            cur = ggml_mul(ctx0, cur, gate);
+            cb(cur, "attn_gated", il);
+
+            // now apply output projection
+            cur = build_lora_mm(model.layers[il].wo, cur);
+            cb(cur, "attn_o_proj", il);
+        }
+
+        if (il == n_layer - 1 && inp_out_ids) {
+            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
+            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
+        }
+
+        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
+        cb(ffn_inp, "ffn_inp", il);
+
+        // FFN pre-normalization (pre)
+        cur = build_norm(ffn_inp,
+                model.layers[il].ffn_norm, NULL,
+                LLM_NORM_RMS, il);
+        cb(cur, "ffn_norm", il);
+
+        // MoE or dense FFN
+        if ((uint32_t)il >= hparams.n_layer_dense_lead) {
+            ggml_tensor * moe_out = build_moe_ffn(cur,
+                    model.layers[il].ffn_gate_inp,
+                    model.layers[il].ffn_up_exps,
+                    model.layers[il].ffn_gate_exps,
+                    model.layers[il].ffn_down_exps,
+                    model.layers[il].ffn_exp_probs_b,
+                    n_expert, n_expert_used,
+                    LLM_FFN_SILU,
+                    hparams.expert_weights_norm,
+                    hparams.expert_weights_scale,
+                    (llama_expert_gating_func_type) hparams.expert_gating_func,
+                    il);
+            cb(moe_out, "ffn_moe_out", il);
+
+            // shared expert
+            if (hparams.n_expert_shared > 0) {
+                ggml_tensor * ffn_shexp = build_ffn(cur,
+                        model.layers[il].ffn_up_shexp,   NULL,
+                        model.layers[il].ffn_gate_shexp, NULL,
+                        model.layers[il].ffn_down_shexp, NULL,
+                        NULL,
+                        LLM_FFN_SILU, LLM_FFN_PAR, il);
+                cb(ffn_shexp, "ffn_shexp", il);
+
+                cur = ggml_add(ctx0, moe_out, ffn_shexp);
+                cb(cur, "ffn_out", il);
+            } else {
+                cur = moe_out;
+            }
+        } else {
+            // dense layer
+            cur = build_ffn(cur,
+                    model.layers[il].ffn_up,   NULL,
+                    model.layers[il].ffn_gate, NULL,
+                    model.layers[il].ffn_down, NULL,
+                    NULL,
+                    LLM_FFN_SILU, LLM_FFN_PAR, il);
+            cb(cur, "ffn_out", il);
+        }
+
+        cur = ggml_add(ctx0, cur, ffn_inp);
+        cur = build_cvec(cur, il);
+        cb(cur, "l_out", il);
+
+        // input for next layer
+        inpL = cur;
+    }
+
+    cur = inpL;
+
+    cur = build_norm(cur,
+            model.output_norm, NULL,
+            LLM_NORM_RMS, -1);
+    cb(cur, "result_norm", -1);
+
+    res->t_embd = cur;
+
+    // lm_head
+    cur = build_lora_mm(model.output, cur);
+    cb(cur, "result_output", -1);
+    res->t_logits = cur;
+}
diff --git a/src/models/models.h b/src/models/models.h
index 128722e69..75446b0de 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -2311,6 +2311,18 @@ struct llama_model_afmoe : public llama_model_base {
     std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
 };

+struct llama_model_minicpm5_moe : public llama_model_base {
+    llama_model_minicpm5_moe(const struct llama_model_params & params) : llama_model_base(params) {}
+    void load_arch_hparams(llama_model_loader & ml) override;
+    void load_arch_tensors(llama_model_loader & ml) override;
+
+    struct graph : public llm_graph_context {
+        graph(const llm_graph_params & params);
+    };
+
+    std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
+};
+
 struct llama_model_beam : public llama_model_base {
     llama_model_beam(const struct llama_model_params & params) : llama_model_base(params) {}
     void load_arch_hparams(llama_model_loader & ml) override;
diff --git a/tests/fusion/MTL.csv b/tests/fusion/MTL.csv
index 57bf77b5e..d33dee2a4 100644
--- a/tests/fusion/MTL.csv
+++ b/tests/fusion/MTL.csv
@@ -241,6 +241,10 @@ minicpm             ,1   ,any     ,MUL+ADD                     ,      4
 minicpm             ,1   ,any     ,RMS_NORM+MUL                ,      5
 minicpm             ,1   ,any     ,SOFT_MAX+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV,      2
 minicpm3            ,0   ,any     ,RMS_NORM+MUL                ,      9
+minicpm5_moe        ,1   ,any     ,ADD+ADD                     ,      1
+minicpm5_moe        ,1   ,any     ,MUL+ADD                     ,      2
+minicpm5_moe        ,1   ,any     ,RMS_NORM+MUL                ,      5
+minicpm5_moe        ,1   ,any     ,UNARY+ADD+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV+SCALE,      1
 minimax-01          ,1   ,any     ,MUL+ADD                     ,      4
 minimax-01          ,1   ,any     ,RMS_NORM+MUL                ,      6
 minimax-01          ,1   ,any     ,SOFT_MAX+ARGSORT+GET_ROWS+SUM_ROWS+CLAMP+DIV,      2
diff --git a/tests/test-llama-archs.cpp b/tests/test-llama-archs.cpp
index d7ecf29bf..dc4eeaba4 100644
--- a/tests/test-llama-archs.cpp
+++ b/tests/test-llama-archs.cpp
@@ -289,7 +289,7 @@ static gguf_context_ptr get_gguf_ctx(const llm_arch arch, const bool moe, const
         n_layer = 6;
     } else if (arch == LLM_ARCH_STEP35 || arch == LLM_ARCH_LAGUNA) {
         n_embd = 160; // exercise per-head tensor split granularity with head size 80
-    } else if (arch == LLM_ARCH_QWEN3 || arch == LLM_ARCH_MUSE_GLIMMER || arch == LLM_ARCH_AFMOE || arch == LLM_ARCH_RICURSIVE) {
+    } else if (arch == LLM_ARCH_QWEN3 || arch == LLM_ARCH_MUSE_GLIMMER || arch == LLM_ARCH_AFMOE || arch == LLM_ARCH_MINICPM5_MOE || arch == LLM_ARCH_RICURSIVE) {
         n_head = 4;
     } else if (arch == LLM_ARCH_LIMITE) {
         n_head  = 4;
@@ -345,7 +345,7 @@ static gguf_context_ptr get_gguf_ctx(const llm_arch arch, const bool moe, const
         n_head_kv = 1; // MQA coverage
     } else if (arch == LLM_ARCH_MUSE_GLIMMER || arch == LLM_ARCH_MUSE_SPARK || arch == LLM_ARCH_AFMOE || arch == LLM_ARCH_MAION_CODER ||
             arch == LLM_ARCH_KOLIBRI1 || arch == LLM_ARCH_LIMITE || arch == LLM_ARCH_GEMMA4 || arch == LLM_ARCH_RICURSIVE ||
-            arch == LLM_ARCH_ZAYA1) {
+            arch == LLM_ARCH_ZAYA1 || arch == LLM_ARCH_MINICPM5_MOE) {
         n_head_kv = 2; // GQA coverage
     }
     if (arch == LLM_ARCH_BEAM) {
@@ -354,7 +354,7 @@ static gguf_context_ptr get_gguf_ctx(const llm_arch arch, const bool moe, const
         n_layer   = 4;
     }
     n_layer = std::max(n_layer, n_layer_min);
-    const uint32_t n_embd_head = n_embd / n_head;
+    const uint32_t n_embd_head = (arch == LLM_ARCH_MINICPM5_MOE ? 2 : 1) * n_embd / n_head;

     ms.add_kv(LLM_KV_GENERAL_ARCHITECTURE,      llama_arch_name(arch));
     ms.add_kv(LLM_KV_VOCAB_SIZE,                n_vocab);
@@ -866,6 +866,11 @@ static gguf_context_ptr get_gguf_ctx(const llm_arch arch, const bool moe, const
         }
     }

+    if (arch == LLM_ARCH_MINICPM5_MOE) {
+        ms.add_kv(LLM_KV_EXPERT_WEIGHTS_SCALE, 3.66f);
+        ms.add_kv(LLM_KV_EXPERT_WEIGHTS_NORM,  true);
+    }
+
     if (arch == LLM_ARCH_BEAM) {
         ms.add_kv(LLM_KV_EXPERT_COUNT,            uint32_t(8));
         ms.add_kv(LLM_KV_EXPERT_GROUP_COUNT,      uint32_t(4));
@@ -1201,6 +1206,7 @@ static bool moe_mandatory(const llm_arch arch) {
         case LLM_ARCH_BAILINGMOE3:
         case LLM_ARCH_DOTS1:
         case LLM_ARCH_AFMOE:
+        case LLM_ARCH_MINICPM5_MOE:
         case LLM_ARCH_BEAM:
         case LLM_ARCH_ERNIE4_5:
         case LLM_ARCH_ERNIE4_5_MOE: