From c08d194eddf9a4fdf47adb14da0eb7fcdafe3eb2 Mon Sep 17 00:00:00 2001 From: Jun Yamog Date: Mon, 15 Jun 2026 01:57:09 +0000 Subject: [PATCH] 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, };