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: