From 0df00b3b946e48163280e2b8beb0ce3230bd2b4c Mon Sep 17 00:00:00 2001 From: Jun Yamog Date: Sun, 14 Jun 2026 12:23:20 +0000 Subject: [PATCH 1/3] Add preliminary MiniMax-M3 support --- src/CMakeLists.txt | 1 + src/graphs/build_minimaxm3.cpp | 132 +++++++++++++++++++++++++++++++++ src/llama-arch.cpp | 1 + src/llama-arch.h | 1 + src/llama-build-context.cpp | 8 +- src/llama-build-context.h | 1 + src/llama-hparams.cpp | 12 +++ src/llama-load-tensors.cpp | 43 +++++++++++ src/llama-model.cpp | 27 +++++++ src/llama.cpp | 2 + 10 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 src/graphs/build_minimaxm3.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7f25bf82..a525c666 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -120,6 +120,7 @@ add_library(llama graphs/build_openai.cpp graphs/build_bailingmoe2.cpp graphs/build_minimaxm2.cpp + graphs/build_minimaxm3.cpp graphs/build_smollm3.cpp ) diff --git a/src/graphs/build_minimaxm3.cpp b/src/graphs/build_minimaxm3.cpp new file mode 100644 index 00000000..7387e333 --- /dev/null +++ b/src/graphs/build_minimaxm3.cpp @@ -0,0 +1,132 @@ +#include "../llama-build-context.h" +#include "../llama-model.h" +#include "../llama-context.h" + +ggml_cgraph* llm_build_context::build_minimaxm3() { + ggml_cgraph * gf = new_graph_custom(); + const int64_t n_embd_head = hparams.n_embd_head_v(0); + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k(0)); + + constexpr float swiglu_alpha = 1.702f; + constexpr float swiglu_limit = 7.0f; + + ggml_tensor * cur; + ggml_tensor * inpL; + + inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb); + + ggml_tensor * inp_pos = build_inp_pos(); + ggml_tensor * inp_out_ids = build_inp_out_ids(); + ggml_tensor * KQ_mask = build_inp_KQ_mask(); + + for (int il = 0; il < n_layer; ++il) { + GGML_ASSERT(model.split_mode != LLAMA_SPLIT_MODE_GRAPH && model.split_mode != LLAMA_SPLIT_MODE_ATTN); + + ggml_tensor * inpSA = inpL; + + cur = llm_build_norm(ctx0, inpL, hparams, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, cb, il); + cb(cur, "attn_norm", il); + + ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur); + cb(Qcur, "Qcur", il); + + ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur); + cb(Kcur, "Kcur", il); + + ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur); + cb(Vcur, "Vcur", il); + + Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); + Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); + Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); + + Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, cb, il); + cb(Qcur, "Qcur_normed", il); + + Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, cb, il); + cb(Kcur, "Kcur_normed", il); + + Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, + ext_factor, attn_factor, beta_fast, beta_slow); + Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, + ext_factor, attn_factor, beta_fast, beta_slow); + cb(Qcur, "Qcur", il); + cb(Kcur, "Kcur", il); + cb(Vcur, "Vcur", il); + + cur = llm_build_kv(ctx0, lctx, kv_self, gf, + model.layers[il].wo, NULL, + Kcur, Vcur, Qcur, KQ_mask, + n_tokens, kv_head, n_kv, + 1.0f / sqrtf(float(n_embd_head)), cb, 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); + + cur = llm_build_norm(ctx0, ffn_inp, hparams, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, cb, il); + cb(cur, "ffn_norm", il); + + if ((uint32_t) il < hparams.n_layer_dense_lead) { + ggml_tensor * gate = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_gate, cur); + cb(gate, "ffn_gate", il); + + ggml_tensor * up = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_up, cur); + cb(up, "ffn_up", il); + + gate = ggml_swiglu_oai(ctx0, gate, up, swiglu_alpha, swiglu_limit); + cb(gate, "ffn_gate_par", il); + + cur = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_down, gate); + cb(cur, "ffn_down", il); + } else { + ggml_tensor * moe_out = llm_build_moe_ffn(ctx0, lctx, 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_SWIGLU_OAI_MOE, + hparams.expert_weights_norm, + hparams.expert_weights_scale != 0.0f, hparams.expert_weights_scale, + (llm_expert_gating_func_type) hparams.expert_gating_func, + cb, il, gf); + cb(moe_out, "ffn_moe_out", il); + + ggml_tensor * gate = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_gate_shexp, cur); + cb(gate, "ffn_shexp_gate", il); + + ggml_tensor * up = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_up_shexp, cur); + cb(up, "ffn_shexp_up", il); + + gate = ggml_swiglu_oai(ctx0, gate, up, swiglu_alpha, swiglu_limit); + cb(gate, "ffn_shexp_gate_par", il); + + ggml_tensor * ffn_shexp = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_down_shexp, gate); + cb(ffn_shexp, "ffn_shexp", il); + + cur = ggml_add(ctx0, moe_out, ffn_shexp); + cb(cur, "ffn_out", il); + } + + cur = ggml_add(ctx0, cur, ffn_inp); + + cur = lctx.cvec.apply_to(ctx0, cur, il); + cb(cur, "l_out", il); + + inpL = cur; + } + + cur = build_output(lctx, ctx0, inpL, model.output, model.output_norm, cb); + cb(cur, "result_output", -1); + + ggml_build_forward_expand(gf, cur); + return gf; +} diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp index 0a402fd1..e0710378 100644 --- a/src/llama-arch.cpp +++ b/src/llama-arch.cpp @@ -72,6 +72,7 @@ static const std::map LLM_ARCH_NAMES = { { LLM_ARCH_OPENAI_MOE, "gpt-oss" }, { LLM_ARCH_BAILINGMOE2, "bailingmoe2" }, { LLM_ARCH_MINIMAX_M2, "minimax-m2" }, + { LLM_ARCH_MINIMAX_M3, "minimax-m3" }, { LLM_ARCH_SMOLLM3, "smollm3" }, { LLM_ARCH_MISTRAL3, "mistral3" }, { LLM_ARCH_MIMO2, "mimo2" }, diff --git a/src/llama-arch.h b/src/llama-arch.h index da57b1af..0f01ca9a 100644 --- a/src/llama-arch.h +++ b/src/llama-arch.h @@ -71,6 +71,7 @@ enum llm_arch { LLM_ARCH_OPENAI_MOE, LLM_ARCH_BAILINGMOE2, LLM_ARCH_MINIMAX_M2, + LLM_ARCH_MINIMAX_M3, LLM_ARCH_SMOLLM3, LLM_ARCH_MISTRAL3, LLM_ARCH_MIMO2, diff --git a/src/llama-build-context.cpp b/src/llama-build-context.cpp index 1696812b..4263c85e 100644 --- a/src/llama-build-context.cpp +++ b/src/llama-build-context.cpp @@ -1149,7 +1149,7 @@ llm_expert_gating_func_type gating_op, ggml_tensor * par; if (can_use_fmoe && up_gate_exps) { - if (up_gate_exps_b) { + if (up_gate_exps_b || type_op == LLM_FFN_SWIGLU_OAI_MOE) { par = ggml_moe_up_gate_ext(ctx, up_gate_exps, nullptr, cur, selected_experts, up_gate_exps_b, nullptr, type_op == LLM_FFN_SILU ? GGML_UNARY_OP_SILU : type_op == LLM_FFN_GELU ? GGML_UNARY_OP_GELU : GGML_UNARY_OP_SWIGLU_OAI); @@ -1165,7 +1165,7 @@ llm_expert_gating_func_type gating_op, GGML_ASSERT(!up_gate_exps && !up_gate_exps_b); if (can_use_fmoe && lctx.cparams.fused_moe_up_gate && up_exps->type == gate_exps->type) { - if (up_exps_b || gate_exps_b) { + if (up_exps_b || gate_exps_b || type_op == LLM_FFN_SWIGLU_OAI_MOE) { par = ggml_moe_up_gate_ext(ctx, up_exps, gate_exps, cur, selected_experts, up_exps_b, gate_exps_b, type_op == LLM_FFN_SILU ? GGML_UNARY_OP_SILU : type_op == LLM_FFN_GELU ? GGML_UNARY_OP_GELU : GGML_UNARY_OP_SWIGLU_OAI); @@ -2520,6 +2520,10 @@ ggml_cgraph * llm_build_context::llama_build_graph( { result = llm.build_minimaxm2(); } break; + case LLM_ARCH_MINIMAX_M3: + { + result = llm.build_minimaxm3(); + } break; case LLM_ARCH_SMOLLM3: { result = llm.build_smollm3(); diff --git a/src/llama-build-context.h b/src/llama-build-context.h index 63a33545..df0c0dbe 100644 --- a/src/llama-build-context.h +++ b/src/llama-build-context.h @@ -312,6 +312,7 @@ struct llm_build_context { ggml_cgraph * build_bailingmoe2(); ggml_cgraph * build_minimaxm2(); + ggml_cgraph * build_minimaxm3(); ggml_cgraph * build_smollm3(); diff --git a/src/llama-hparams.cpp b/src/llama-hparams.cpp index f1a46046..8a61c4ed 100644 --- a/src/llama-hparams.cpp +++ b/src/llama-hparams.cpp @@ -1280,6 +1280,18 @@ void llm_load_hparams( default: model.type = e_model::MODEL_UNKNOWN; } } break; + case LLM_ARCH_MINIMAX_M3: + { + 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, false); + ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); + ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); + ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); + ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); + ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); + + model.type = e_model::MODEL_UNKNOWN; + } break; case LLM_ARCH_SMOLLM3: { ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); diff --git a/src/llama-load-tensors.cpp b/src/llama-load-tensors.cpp index 3e8a7992..7a6a8b6b 100644 --- a/src/llama-load-tensors.cpp +++ b/src/llama-load-tensors.cpp @@ -150,6 +150,7 @@ struct create_tensors_helper : public create_tensors_helper_interface { bool create_bailingmoe2_tensors(const LLM_TN & tn); bool create_minimaxm2_tensors(const LLM_TN & tn); + bool create_minimaxm3_tensors(const LLM_TN & tn); bool create_smollm3_tensors(const LLM_TN & tn); @@ -3601,6 +3602,46 @@ bool create_tensors_helper::create_minimaxm2_tensors(const LLM_TN & tn) { return use_mmap_buffer; } +bool create_tensors_helper::create_minimaxm3_tensors(const LLM_TN & tn) { + LOADING_PRELUDE + + const int64_t n_expert_shared = hparams.n_expert_shared; + const int64_t n_ff_exp = hparams.n_ff_exp; + + create_embd_output(tn, n_embd, n_vocab); + + for (int i = 0; i < n_layer; ++i) { + ggml_context* ctx_split = ctx_for_layer_split(i); + auto& layer = model.layers[i]; + + layer.wq = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); + layer.wk = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0); + layer.wv = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0); + layer.wo = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); + + layer.attn_norm = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); + layer.attn_q_norm = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0); + layer.attn_k_norm = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0); + + layer.ffn_norm = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); + + if (i < (int) hparams.n_layer_dense_lead) { + layer.ffn_gate = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0); + layer.ffn_down = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0); + layer.ffn_up = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0); + } else { + layer.ffn_gate_inp = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0); + layer.ffn_exp_probs_b = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, 0); + use_mmap_buffer &= !create_std_ffn_exps(n_embd, tn, i, 0, n_ff_exp); + + layer.ffn_gate_shexp = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_exp * n_expert_shared }, 0); + layer.ffn_down_shexp = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd }, 0); + layer.ffn_up_shexp = create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_exp * n_expert_shared }, 0); + } + } + return use_mmap_buffer; +} + bool create_tensors_helper::create_smollm3_tensors(const LLM_TN & tn) { LOADING_PRELUDE @@ -4412,6 +4453,8 @@ bool create_tensors_helper::create_tensors() { use_mmap_buffer = create_bailingmoe2_tensors(tn); break; case LLM_ARCH_MINIMAX_M2: use_mmap_buffer = create_minimaxm2_tensors(tn); break; + case LLM_ARCH_MINIMAX_M3: + use_mmap_buffer = create_minimaxm3_tensors(tn); break; case LLM_ARCH_SMOLLM3: use_mmap_buffer = create_smollm3_tensors(tn); break; case LLM_ARCH_MIMO2: diff --git a/src/llama-model.cpp b/src/llama-model.cpp index 9617fd54..253449da 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -1532,6 +1532,33 @@ static const std::map> LLM_TENSOR_NA { LLM_TENSOR_FFN_EXP_PROBS_B, "blk.%d.exp_probs_b" }, }, }, + { + LLM_ARCH_MINIMAX_M3, + { + { LLM_TENSOR_TOKEN_EMBD, "token_embd" }, + { LLM_TENSOR_OUTPUT_NORM, "output_norm" }, + { LLM_TENSOR_OUTPUT, "output" }, + { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" }, + { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" }, + { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" }, + { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" }, + { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" }, + { LLM_TENSOR_ATTN_Q_NORM, "blk.%d.attn_q_norm" }, + { LLM_TENSOR_ATTN_K_NORM, "blk.%d.attn_k_norm" }, + { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" }, + { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" }, + { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" }, + { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" }, + { LLM_TENSOR_FFN_GATE_INP, "blk.%d.ffn_gate_inp" }, + { LLM_TENSOR_FFN_GATE_EXPS, "blk.%d.ffn_gate_exps" }, + { LLM_TENSOR_FFN_DOWN_EXPS, "blk.%d.ffn_down_exps" }, + { LLM_TENSOR_FFN_UP_EXPS, "blk.%d.ffn_up_exps" }, + { LLM_TENSOR_FFN_EXP_PROBS_B, "blk.%d.exp_probs_b" }, + { LLM_TENSOR_FFN_GATE_SHEXP, "blk.%d.ffn_gate_shexp" }, + { LLM_TENSOR_FFN_DOWN_SHEXP, "blk.%d.ffn_down_shexp" }, + { LLM_TENSOR_FFN_UP_SHEXP, "blk.%d.ffn_up_shexp" }, + }, + }, { LLM_ARCH_SMOLLM3, { diff --git a/src/llama.cpp b/src/llama.cpp index d4aa28e8..d78a6e49 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -3071,6 +3071,7 @@ static bool is_model_split_supported(const llama_model & model) { LLM_ARCH_OPENAI_MOE, LLM_ARCH_ERNIE4_5_MOE, LLM_ARCH_MINIMAX_M2, + LLM_ARCH_MINIMAX_M3, LLM_ARCH_SEED_OSS, LLM_ARCH_STEP35, LLM_ARCH_LAGUNA, @@ -7439,6 +7440,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) { case LLM_ARCH_OPENAI_MOE: case LLM_ARCH_BAILINGMOE2: case LLM_ARCH_MINIMAX_M2: + case LLM_ARCH_MINIMAX_M3: case LLM_ARCH_MIMO2: case LLM_ARCH_SEED_OSS: case LLM_ARCH_STEP35: From c538210e6ddd499b0cd91b2bc1bd475ff71be8b1 Mon Sep 17 00:00:00 2001 From: Jun Yamog Date: Mon, 15 Jun 2026 01:29:13 +0000 Subject: [PATCH 2/3] Add MiniMax-M3 chat template --- models/templates/MiniMax-M3.jinja | 169 ++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 models/templates/MiniMax-M3.jinja diff --git a/models/templates/MiniMax-M3.jinja b/models/templates/MiniMax-M3.jinja new file mode 100644 index 00000000..ff04e450 --- /dev/null +++ b/models/templates/MiniMax-M3.jinja @@ -0,0 +1,169 @@ +{# MiniMax-M3 override. + Keep MiniMax-M2's PEG-compatible tool-call wrapper, but use M3 thinking tags. #} +{%- set toolcall_begin_token = '' -%} +{%- set toolcall_end_token = '' -%} +{%- set think_begin_token = '' -%} +{%- set think_end_token = '' -%} + +{#- Tool Rendering Functions ============================================== -#} +{%- macro render_tool_namespace(namespace_name, tool_list) -%} +{%- for tool in tool_list -%} +{{ tool.function | tojson(ensure_ascii=False) }} +{% endfor -%} +{%- endmacro -%} + +{%- macro visible_text(content) -%} + {%- if content is string -%} + {{ content }} + {%- elif content is iterable and content is not mapping -%} + {%- for item in content -%} + {%- if item is mapping and item.type == 'text' -%} + {{- item.text }} + {%- elif item is string -%} + {{- item }} + {%- endif -%} + {%- endfor -%} + {%- else -%} + {{- content }} + {%- endif -%} +{%- endmacro -%} + +{#- System Message Construction ============================================ -#} +{%- macro build_system_message(system_message) -%} + {%- if system_message and system_message.content -%} + {{- visible_text(system_message.content) }} + {%- else -%} + {%- if model_identity is not defined -%} + {%- set model_identity = "You are a helpful assistant." -%} + {%- endif -%} + {{- model_identity }} + {%- endif -%} + + {%- if system_message and system_message.current_date -%} + {{- '\n' ~ 'Current date: ' + system_message.current_date }} + {%- endif -%} + {%- if system_message and system_message.current_location -%} + {{- '\n' ~ 'Current location: ' + system_message.current_location }} + {%- endif -%} +{%- endmacro -%} + +{#- Main Template Logic ===================================================== -#} +{%- set system_message = none -%} +{%- set conversation_messages = messages -%} +{%- if messages and messages[0].role == "system" -%} + {%- set system_message = messages[0] -%} + {%- set conversation_messages = messages[1:] -%} +{%- endif -%} + +{#- Get the last user message turn, for interleaved thinking -#} +{%- set ns = namespace(last_user_index=-1) %} +{% for m in conversation_messages %} + {%- if m.role == 'user' %} + {% set ns.last_user_index = loop.index0 -%} + {%- endif %} +{%- endfor %} + +{#- Render system message -#} +{{- ']~!b[' ~ ']~b]system' ~ '\n' }} +{{- build_system_message(system_message) }} + +{#- Render tools if available -#} +{%- if tools -%} + {{- '\n\n' ~ '# Tools' ~ '\n' ~ 'You may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:' ~ '\n' }} + {{- '\n' ~ '' ~ '\n' }} + {{- render_tool_namespace("functions", tools) }} + {{- '' ~ '\n\n' }} + {{- 'When making tool calls, use XML format to invoke tools and pass parameters:' ~ '\n' }} + {{- '\n' ~ toolcall_begin_token }} + + param-value-1 + param-value-2 + ... + + {{- '\n' ~ toolcall_end_token }} +{%- endif -%} + +{{- '[e~[\n' }} + +{#- Render messages -#} +{%- set last_tool_call = namespace(name=none) -%} +{%- for message in conversation_messages -%} + {%- if message.role == 'assistant' -%} + {{- ']~b]ai' ~ '\n' }} + + {%- set reasoning_content = '' %} + {%- set content = visible_text(message.content) %} + {%- if message.reasoning_content is string %} + {%- set reasoning_content = message.reasoning_content %} + {%- else %} + {%- if think_end_token in content %} + {%- set reasoning_content = content.split(think_end_token)[0].strip('\n').split(think_begin_token)[-1].strip('\n') %} + {%- set content = content.split(think_end_token)[-1].strip('\n') %} + {%- elif '' in content %} + {%- set reasoning_content = content.split('')[0].strip('\n').split('')[-1].strip('\n') %} + {%- set content = content.split('')[-1].strip('\n') %} + {%- endif %} + {%- endif %} + {%- if reasoning_content and loop.index0 > ns.last_user_index -%} + {{- think_begin_token ~ '\n' ~ reasoning_content ~ '\n' ~ think_end_token ~ '\n\n' }} + {%- endif -%} + {%- if content -%} + {{- content }} + {%- endif -%} + + {%- if message.tool_calls -%} + {{- '\n' ~ toolcall_begin_token ~ '\n' }} + {%- for tool_call in message.tool_calls -%} + {%- if tool_call.function %} + {%- set tool_call = tool_call.function %} + {%- endif %} + {{- '' }} + {% set _args = tool_call.arguments %} + {%- for k, v in _args.items() %} + {{- '' }} + {{- v | tojson(ensure_ascii=False) if v is not string else v }} + {{- '' }} + {% endfor %} + {{- '' ~ '\n' }} + {%- endfor -%} + + {{- toolcall_end_token}} + {%- set last_tool_call.name = message.tool_calls[-1].function.name -%} + {%- else -%} + {%- set last_tool_call.name = none -%} + {%- endif -%} + {{- '[e~[' ~ '\n' }} + + {%- elif message.role == 'tool' -%} + {%- if last_tool_call.name is none -%} + {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }} + {%- endif -%} + {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%} + {{- ']~b]tool' }} + {%- endif -%} + {%- if message.content is string -%} + {{- '\n' }} + {{- message.content }} + {{- '' }} + {%- else -%} + {%- for tr in message.content -%} + {{- '\n' }} + {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }} + {{- '\n' }} + {%- endfor -%} + {%- endif -%} + {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%} + {{- '[e~[\n' -}} + {%- endif -%} + + {%- elif message.role == 'user' -%} + {{- ']~b]user' ~ '\n' }} + {{- visible_text(message.content) }} + {{- '[e~[' ~ '\n' }} + {%- endif -%} +{%- endfor -%} + +{#- Generation prompt -#} +{%- if add_generation_prompt -%} +{{- ']~b]ai' ~ '\n' ~ think_begin_token ~ '\n' }} +{%- endif -%} From c08d194eddf9a4fdf47adb14da0eb7fcdafe3eb2 Mon Sep 17 00:00:00 2001 From: Jun Yamog Date: Mon, 15 Jun 2026 01:57:09 +0000 Subject: [PATCH 3/3] Use standard graph helpers for MiniMax-M3 --- src/graphs/build_minimaxm3.cpp | 111 +++++++-------------------------- src/llama-build-context.cpp | 8 +++ src/llama-build-context.h | 1 + 3 files changed, 33 insertions(+), 87 deletions(-) diff --git a/src/graphs/build_minimaxm3.cpp b/src/graphs/build_minimaxm3.cpp index 7387e333..c0a1409e 100644 --- a/src/graphs/build_minimaxm3.cpp +++ b/src/graphs/build_minimaxm3.cpp @@ -7,117 +7,54 @@ ggml_cgraph* llm_build_context::build_minimaxm3() { const int64_t n_embd_head = hparams.n_embd_head_v(0); GGML_ASSERT(n_embd_head == hparams.n_embd_head_k(0)); - constexpr float swiglu_alpha = 1.702f; - constexpr float swiglu_limit = 7.0f; - ggml_tensor * cur; ggml_tensor * inpL; inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb); ggml_tensor * inp_pos = build_inp_pos(); - ggml_tensor * inp_out_ids = build_inp_out_ids(); + ggml_tensor * inp_out_ids = n_tokens > 1 ? build_inp_out_ids() : nullptr; ggml_tensor * KQ_mask = build_inp_KQ_mask(); for (int il = 0; il < n_layer; ++il) { - GGML_ASSERT(model.split_mode != LLAMA_SPLIT_MODE_GRAPH && model.split_mode != LLAMA_SPLIT_MODE_ATTN); - - ggml_tensor * inpSA = inpL; - - cur = llm_build_norm(ctx0, inpL, hparams, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, cb, il); - cb(cur, "attn_norm", il); - - ggml_tensor * Qcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wq, cur); - cb(Qcur, "Qcur", il); - - ggml_tensor * Kcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wk, cur); - cb(Kcur, "Kcur", il); - - ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur); - cb(Vcur, "Vcur", il); - - Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); - Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); - Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); - - Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, cb, il); - cb(Qcur, "Qcur_normed", il); - - Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, cb, il); - cb(Kcur, "Kcur_normed", il); - - Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, - n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, - ext_factor, attn_factor, beta_fast, beta_slow); - Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, - n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, - ext_factor, attn_factor, beta_fast, beta_slow); - cb(Qcur, "Qcur", il); - cb(Kcur, "Kcur", il); - cb(Vcur, "Vcur", il); - - cur = llm_build_kv(ctx0, lctx, kv_self, gf, - model.layers[il].wo, NULL, - Kcur, Vcur, Qcur, KQ_mask, - n_tokens, kv_head, n_kv, - 1.0f / sqrtf(float(n_embd_head)), cb, 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); - - cur = llm_build_norm(ctx0, ffn_inp, hparams, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, cb, il); - cb(cur, "ffn_norm", il); + ggml_tensor * ffn_inp = build_std_attention(gf, model.layers[il].attn_norm, inpL, + inp_pos, il == n_layer - 1 ? inp_out_ids : nullptr, nullptr, + KQ_mask, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), 0.0f, 0, + il, true, false, true); if ((uint32_t) il < hparams.n_layer_dense_lead) { - ggml_tensor * gate = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_gate, cur); - cb(gate, "ffn_gate", il); - - ggml_tensor * up = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_up, cur); - cb(up, "ffn_up", il); - - gate = ggml_swiglu_oai(ctx0, gate, up, swiglu_alpha, swiglu_limit); - cb(gate, "ffn_gate_par", il); - - cur = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_down, gate); - cb(cur, "ffn_down", il); + cur = llm_build_ffn(ctx0, lctx, model.layers[il].ffn_norm, ffn_inp, + model.layers[il].ffn_up, nullptr, nullptr, + model.layers[il].ffn_gate, nullptr, nullptr, + model.layers[il].ffn_down, nullptr, nullptr, + nullptr, + LLM_FFN_SWIGLU_OAI, LLM_FFN_PAR, cb, il, gf, true); } else { - ggml_tensor * moe_out = llm_build_moe_ffn(ctx0, lctx, cur, + cur = llm_build_std_moe_ffn(ctx0, lctx, model.layers[il].ffn_norm, ffn_inp, model.layers[il].ffn_gate_inp, + nullptr, model.layers[il].ffn_up_exps, + nullptr, model.layers[il].ffn_gate_exps, + nullptr, model.layers[il].ffn_down_exps, + nullptr, model.layers[il].ffn_exp_probs_b, + model.layers[il].ffn_up_shexp, + nullptr, + model.layers[il].ffn_gate_shexp, + nullptr, + model.layers[il].ffn_down_shexp, + nullptr, n_expert, n_expert_used, LLM_FFN_SWIGLU_OAI_MOE, hparams.expert_weights_norm, hparams.expert_weights_scale != 0.0f, hparams.expert_weights_scale, (llm_expert_gating_func_type) hparams.expert_gating_func, - cb, il, gf); - cb(moe_out, "ffn_moe_out", il); - - ggml_tensor * gate = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_gate_shexp, cur); - cb(gate, "ffn_shexp_gate", il); - - ggml_tensor * up = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_up_shexp, cur); - cb(up, "ffn_shexp_up", il); - - gate = ggml_swiglu_oai(ctx0, gate, up, swiglu_alpha, swiglu_limit); - cb(gate, "ffn_shexp_gate_par", il); - - ggml_tensor * ffn_shexp = llm_build_lora_mm(lctx, ctx0, model.layers[il].ffn_down_shexp, gate); - cb(ffn_shexp, "ffn_shexp", il); - - cur = ggml_add(ctx0, moe_out, ffn_shexp); - cb(cur, "ffn_out", il); + LLM_FFN_SWIGLU_OAI, + cb, il, gf, true); } - cur = ggml_add(ctx0, cur, ffn_inp); - cur = lctx.cvec.apply_to(ctx0, cur, il); cb(cur, "l_out", il); diff --git a/src/llama-build-context.cpp b/src/llama-build-context.cpp index 4263c85e..17a06b1a 100644 --- a/src/llama-build-context.cpp +++ b/src/llama-build-context.cpp @@ -962,6 +962,14 @@ ggml_tensor * llm_build_context::llm_build_ffn( cur = ggml_swiglu(ctx, cur); cb(cur, "ffn_swiglu", il); } break; + case LLM_FFN_SWIGLU_OAI: + { + constexpr float alpha = 1.702f; + constexpr float limit = 7.0f; + cur = ggml_swiglu_oai(ctx, cur, tmp, alpha, limit); + cb(cur, "ffn_swiglu_oai", il); + type_gate = LLM_FFN_SEQ; + } break; default: GGML_ABORT("fatal error"); } diff --git a/src/llama-build-context.h b/src/llama-build-context.h index df0c0dbe..aeadbbed 100644 --- a/src/llama-build-context.h +++ b/src/llama-build-context.h @@ -24,6 +24,7 @@ enum llm_ffn_op_type { LLM_FFN_RELU, LLM_FFN_RELU_SQR, LLM_FFN_SWIGLU, + LLM_FFN_SWIGLU_OAI, LLM_FFN_SWIGLU_OAI_MOE, };