|
|
@@ -107,8 +107,10 @@ const char * llm_type_name(llm_type type) {
|
|
|
case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
|
|
|
case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
|
|
|
case LLM_TYPE_A13B: return "A13B";
|
|
|
+ case LLM_TYPE_21B_A3B: return "21B.A3B";
|
|
|
case LLM_TYPE_30B_A3B: return "30B.A3B";
|
|
|
case LLM_TYPE_235B_A22B: return "235B.A22B";
|
|
|
+ case LLM_TYPE_300B_A47B: return "300B.A47B";
|
|
|
case LLM_TYPE_E2B: return "E2B";
|
|
|
case LLM_TYPE_E4B: return "E4B";
|
|
|
default: return "?B";
|
|
|
@@ -1649,10 +1651,20 @@ void llama_model::load_hparams(llama_model_loader & ml) {
|
|
|
}
|
|
|
} break;
|
|
|
case LLM_ARCH_ERNIE4_5:
|
|
|
+ case LLM_ARCH_ERNIE4_5_MOE:
|
|
|
{
|
|
|
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
|
|
|
+ if (arch == LLM_ARCH_ERNIE4_5_MOE) {
|
|
|
+ ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
|
|
|
+ ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
|
|
|
+ ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
|
|
|
+ ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
|
|
|
+ }
|
|
|
+
|
|
|
switch (hparams.n_layer) {
|
|
|
case 18: type = LLM_TYPE_0_3B; break;
|
|
|
+ case 28: type = LLM_TYPE_21B_A3B; break;
|
|
|
+ case 54: type = LLM_TYPE_300B_A47B; break;
|
|
|
default: type = LLM_TYPE_UNKNOWN;
|
|
|
}
|
|
|
} break;
|
|
|
@@ -4858,6 +4870,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
|
|
|
}
|
|
|
} break;
|
|
|
case LLM_ARCH_ERNIE4_5:
|
|
|
+ case LLM_ARCH_ERNIE4_5_MOE:
|
|
|
{
|
|
|
tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
|
|
|
|
|
|
@@ -4886,9 +4899,27 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
|
|
|
layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
|
|
|
|
|
|
layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
|
|
|
- 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);
|
|
|
+
|
|
|
+ if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
|
|
|
+ int n_ff_exp = hparams.n_ff_exp;
|
|
|
+
|
|
|
+ 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}, TENSOR_NOT_REQUIRED);
|
|
|
+ layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
|
|
|
+ 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 present)
|
|
|
+ if (hparams.n_ff_shexp > 0) {
|
|
|
+ layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
|
|
|
+ layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);
|
|
|
+ layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.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);
|
|
|
+ }
|
|
|
}
|
|
|
} break;
|
|
|
case LLM_ARCH_FALCON_H1:
|
|
|
@@ -15569,6 +15600,176 @@ struct llm_build_ernie4_5 : public llm_graph_context {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+struct llm_build_ernie4_5_moe : public llm_graph_context {
|
|
|
+ llm_build_ernie4_5_moe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
|
|
|
+ const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
+
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
|
|
|
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
|
|
|
+
|
|
|
+ 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_unified();
|
|
|
+
|
|
|
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
+
|
|
|
+ GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
|
|
|
+ for (int il = 0; il < n_layer; ++il) {
|
|
|
+ ggml_tensor * inpSA = inpL;
|
|
|
+ // norm
|
|
|
+ {
|
|
|
+ cur = build_norm(inpL,
|
|
|
+ model.layers[il].attn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "attn_norm", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ // self-attention
|
|
|
+ {
|
|
|
+ // compute Q and K and RoPE them
|
|
|
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ if (model.layers[il].bq) {
|
|
|
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ if (model.layers[il].bk) {
|
|
|
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
|
|
|
+ cb(Vcur, "Vcur", il);
|
|
|
+ if (model.layers[il].bv) {
|
|
|
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
|
|
|
+ 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 = 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 = build_attn(inp_attn, gf,
|
|
|
+ model.layers[il].wo, NULL,
|
|
|
+ Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
|
|
|
+ cb(cur, "attn_out", 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);
|
|
|
+
|
|
|
+ // feed-forward network
|
|
|
+ bool is_moe_layer = static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
|
|
|
+
|
|
|
+ if (!is_moe_layer) {
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ cur = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ } else {
|
|
|
+ // MoE branch
|
|
|
+ cur = build_norm(ffn_inp,
|
|
|
+ model.layers[il].ffn_norm, NULL,
|
|
|
+ LLM_NORM_RMS, il);
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
+
|
|
|
+ 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, true,
|
|
|
+ false, 0.0,
|
|
|
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
|
|
|
+ il);
|
|
|
+ cb(moe_out, "ffn_moe_out", il);
|
|
|
+
|
|
|
+ // Shared expert (if present)
|
|
|
+ if (hparams.n_ff_shexp > 0) {
|
|
|
+ ggml_tensor * ffn_shexp = build_ffn(cur,
|
|
|
+ model.layers[il].ffn_up_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
|
|
|
+ model.layers[il].ffn_down_shexp, NULL, NULL,
|
|
|
+ NULL,
|
|
|
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
|
|
|
+ cb(ffn_shexp, "ffn_shexp", il);
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
|
|
+ } else {
|
|
|
+ cur = moe_out;
|
|
|
+ }
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+ }
|
|
|
+
|
|
|
+ cur = ggml_add(ctx0, cur, ffn_inp);
|
|
|
+ cb(cur, "ffn_out", il);
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ ggml_build_forward_expand(gf, cur);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
struct llm_build_falcon_h1 : public llm_graph_context_mamba {
|
|
|
llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context_mamba(params) {
|
|
|
const int64_t n_embd_head = hparams.n_embd_head_v;
|
|
|
@@ -17034,6 +17235,10 @@ ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
|
|
|
{
|
|
|
llm = std::make_unique<llm_build_ernie4_5>(*this, params, gf);
|
|
|
} break;
|
|
|
+ case LLM_ARCH_ERNIE4_5_MOE:
|
|
|
+ {
|
|
|
+ llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params, gf);
|
|
|
+ } break;
|
|
|
case LLM_ARCH_HUNYUAN_MOE:
|
|
|
{
|
|
|
llm = std::make_unique<llm_build_hunyuan_moe>(*this, params, gf);
|
|
|
@@ -17206,6 +17411,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
|
|
|
case LLM_ARCH_SMOLLM3:
|
|
|
case LLM_ARCH_ARCEE:
|
|
|
case LLM_ARCH_ERNIE4_5:
|
|
|
+ case LLM_ARCH_ERNIE4_5_MOE:
|
|
|
return LLAMA_ROPE_TYPE_NORM;
|
|
|
|
|
|
// the pairs of head values are offset by n_rot/2
|