@@ -114,6 +114,7 @@ const char * llm_type_name(llm_type type) {
114114 case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
115115 case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
116116 case LLM_TYPE_A13B: return "A13B";
117+ case LLM_TYPE_8B_A1B: return "8B.A1B";
117118 case LLM_TYPE_21B_A3B: return "21B.A3B";
118119 case LLM_TYPE_30B_A3B: return "30B.A3B";
119120 case LLM_TYPE_106B_A12B: return "106B.A12B";
@@ -1995,14 +1996,29 @@ void llama_model::load_hparams(llama_model_loader & ml) {
19951996 for (uint32_t il = 0; il < hparams.n_layer; ++il) {
19961997 hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
19971998 }
1999+ hparams.n_layer_dense_lead = hparams.n_layer;
19982000 switch (hparams.n_ff()) {
19992001 case 4608: type = LLM_TYPE_350M; break;
20002002 case 6912: type = LLM_TYPE_700M; break;
20012003 case 8192: type = LLM_TYPE_1_2B; break;
20022004 case 10752: type = LLM_TYPE_2_6B; break;
2003- default: type = LLM_TYPE_UNKNOWN;
2005+ default: type = LLM_TYPE_UNKNOWN;
20042006 }
20052007 } break;
2008+ case LLM_ARCH_LFM2MOE:
2009+ {
2010+ ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
2011+ ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
2012+ ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
2013+ ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
2014+ ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func);
2015+
2016+ for (uint32_t il = 0; il < hparams.n_layer; ++il) {
2017+ hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
2018+ }
2019+
2020+ type = LLM_TYPE_8B_A1B;
2021+ } break;
20062022 case LLM_ARCH_SMALLTHINKER:
20072023 {
20082024 const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
@@ -5814,6 +5830,7 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
58145830 }
58155831 } break;
58165832 case LLM_ARCH_LFM2:
5833+ case LLM_ARCH_LFM2MOE:
58175834 {
58185835 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
58195836 tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
@@ -5825,11 +5842,23 @@ bool llama_model::load_tensors(llama_model_loader & ml) {
58255842
58265843 for (int i = 0; i < n_layer; ++i) {
58275844 auto & layer = layers[i];
5828- // ffn is same for transformer and conv layers
5845+
5846+ const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead);
5847+
5848+ // ffn/moe is same for transformer and conv layers
58295849 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
5830- layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
5831- layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
5832- layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
5850+ if (is_moe_layer) {
5851+ GGML_ASSERT(n_expert && n_expert_used);
5852+ layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
5853+ layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);
5854+ layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp, n_embd, n_expert}, 0);
5855+ layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);
5856+ layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);
5857+ } else { // dense
5858+ layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
5859+ layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
5860+ layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
5861+ }
58335862
58345863 // for operator_norm
58355864 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
@@ -6310,7 +6339,7 @@ void llama_model::print_info() const {
63106339 LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
63116340 }
63126341
6313- if (arch == LLM_ARCH_SMALLTHINKER) {
6342+ if (arch == LLM_ARCH_SMALLTHINKER || arch == LLM_ARCH_LFM2MOE ) {
63146343 LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
63156344 LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
63166345 }
@@ -18602,6 +18631,8 @@ struct llm_build_lfm2 : public llm_graph_context {
1860218631 ggml_tensor * inp_out_ids = build_inp_out_ids();
1860318632
1860418633 for (int il = 0; il < n_layer; ++il) {
18634+ const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
18635+
1860518636 auto * prev_cur = cur;
1860618637 cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
1860718638 cb(cur, "model.layers.{}.operator_norm", il);
@@ -18616,7 +18647,16 @@ struct llm_build_lfm2 : public llm_graph_context {
1861618647 }
1861718648
1861818649 cur = ggml_add(ctx0, prev_cur, cur);
18619- cur = ggml_add(ctx0, cur, build_feed_forward(cur, il));
18650+
18651+ auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
18652+ cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);
18653+
18654+ ggml_tensor * ffn_out = is_moe_layer ?
18655+ build_moe_feed_forward(ffn_norm_out, il) :
18656+ build_dense_feed_forward(ffn_norm_out, il);
18657+ cb(ffn_norm_out, "model.layers.{}.ffn_out", il);
18658+
18659+ cur = ggml_add(ctx0, cur, ffn_out);
1862018660 }
1862118661
1862218662 cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
@@ -18631,23 +18671,32 @@ struct llm_build_lfm2 : public llm_graph_context {
1863118671 ggml_build_forward_expand(gf, cur);
1863218672 }
1863318673
18634- ggml_tensor * build_feed_forward(ggml_tensor * cur,
18635- int il) const {
18636- cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
18637- cb(cur, "model.layers.{}.ffn_norm", il);
18674+ ggml_tensor * build_moe_feed_forward(ggml_tensor * cur,
18675+ int il) const {
18676+ return build_moe_ffn(cur,
18677+ model.layers[il].ffn_gate_inp,
18678+ model.layers[il].ffn_up_exps,
18679+ model.layers[il].ffn_gate_exps,
18680+ model.layers[il].ffn_down_exps,
18681+ model.layers[il].ffn_exp_probs_b,
18682+ n_expert, n_expert_used,
18683+ LLM_FFN_SILU, true,
18684+ false, 0.0,
18685+ static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
18686+ il);
18687+ }
1863818688
18689+ ggml_tensor * build_dense_feed_forward(ggml_tensor * cur,
18690+ int il) const {
1863918691 GGML_ASSERT(!model.layers[il].ffn_up_b);
1864018692 GGML_ASSERT(!model.layers[il].ffn_gate_b);
1864118693 GGML_ASSERT(!model.layers[il].ffn_down_b);
18642- cur = build_ffn(cur,
18694+ return build_ffn(cur,
1864318695 model.layers[il].ffn_up, NULL, NULL,
1864418696 model.layers[il].ffn_gate, NULL, NULL,
1864518697 model.layers[il].ffn_down, NULL, NULL,
1864618698 NULL,
1864718699 LLM_FFN_SILU, LLM_FFN_PAR, il);
18648- cb(cur, "model.layers.{}.feed_forward.w2", il);
18649-
18650- return cur;
1865118700 }
1865218701
1865318702 ggml_tensor * build_attn_block(ggml_tensor * cur,
@@ -19817,6 +19866,7 @@ ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
1981719866 llm = std::make_unique<llm_build_falcon_h1>(*this, params);
1981819867 } break;
1981919868 case LLM_ARCH_LFM2:
19869+ case LLM_ARCH_LFM2MOE:
1982019870 {
1982119871 llm = std::make_unique<llm_build_lfm2>(*this, params);
1982219872 } break;
@@ -20039,6 +20089,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
2003920089 case LLM_ARCH_OPENAI_MOE:
2004020090 case LLM_ARCH_HUNYUAN_DENSE:
2004120091 case LLM_ARCH_LFM2:
20092+ case LLM_ARCH_LFM2MOE:
2004220093 case LLM_ARCH_SMALLTHINKER:
2004320094 case LLM_ARCH_GLM4_MOE:
2004420095 case LLM_ARCH_SEED_OSS:
0 commit comments