@@ -114,6 +114,7 @@ const char * llm_type_name(llm_type type) {
114
114
case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
115
115
case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
116
116
case LLM_TYPE_A13B: return "A13B";
117
+ case LLM_TYPE_8B_A1B: return "8B.A1B";
117
118
case LLM_TYPE_21B_A3B: return "21B.A3B";
118
119
case LLM_TYPE_30B_A3B: return "30B.A3B";
119
120
case LLM_TYPE_106B_A12B: return "106B.A12B";
@@ -1995,14 +1996,29 @@ void llama_model::load_hparams(llama_model_loader & ml) {
1995
1996
for (uint32_t il = 0; il < hparams.n_layer; ++il) {
1996
1997
hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
1997
1998
}
1999
+ hparams.n_layer_dense_lead = hparams.n_layer;
1998
2000
switch (hparams.n_ff()) {
1999
2001
case 4608: type = LLM_TYPE_350M; break;
2000
2002
case 6912: type = LLM_TYPE_700M; break;
2001
2003
case 8192: type = LLM_TYPE_1_2B; break;
2002
2004
case 10752: type = LLM_TYPE_2_6B; break;
2003
- default: type = LLM_TYPE_UNKNOWN;
2005
+ default: type = LLM_TYPE_UNKNOWN;
2004
2006
}
2005
2007
} 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;
2006
2022
case LLM_ARCH_SMALLTHINKER:
2007
2023
{
2008
2024
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) {
5814
5830
}
5815
5831
} break;
5816
5832
case LLM_ARCH_LFM2:
5833
+ case LLM_ARCH_LFM2MOE:
5817
5834
{
5818
5835
tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
5819
5836
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) {
5825
5842
5826
5843
for (int i = 0; i < n_layer; ++i) {
5827
5844
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
5829
5849
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
+ }
5833
5862
5834
5863
// for operator_norm
5835
5864
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 {
6310
6339
LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
6311
6340
}
6312
6341
6313
- if (arch == LLM_ARCH_SMALLTHINKER) {
6342
+ if (arch == LLM_ARCH_SMALLTHINKER || arch == LLM_ARCH_LFM2MOE ) {
6314
6343
LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
6315
6344
LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
6316
6345
}
@@ -18602,6 +18631,8 @@ struct llm_build_lfm2 : public llm_graph_context {
18602
18631
ggml_tensor * inp_out_ids = build_inp_out_ids();
18603
18632
18604
18633
for (int il = 0; il < n_layer; ++il) {
18634
+ const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
18635
+
18605
18636
auto * prev_cur = cur;
18606
18637
cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
18607
18638
cb(cur, "model.layers.{}.operator_norm", il);
@@ -18616,7 +18647,16 @@ struct llm_build_lfm2 : public llm_graph_context {
18616
18647
}
18617
18648
18618
18649
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);
18620
18660
}
18621
18661
18622
18662
cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
@@ -18631,23 +18671,32 @@ struct llm_build_lfm2 : public llm_graph_context {
18631
18671
ggml_build_forward_expand(gf, cur);
18632
18672
}
18633
18673
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
+ }
18638
18688
18689
+ ggml_tensor * build_dense_feed_forward(ggml_tensor * cur,
18690
+ int il) const {
18639
18691
GGML_ASSERT(!model.layers[il].ffn_up_b);
18640
18692
GGML_ASSERT(!model.layers[il].ffn_gate_b);
18641
18693
GGML_ASSERT(!model.layers[il].ffn_down_b);
18642
- cur = build_ffn(cur,
18694
+ return build_ffn(cur,
18643
18695
model.layers[il].ffn_up, NULL, NULL,
18644
18696
model.layers[il].ffn_gate, NULL, NULL,
18645
18697
model.layers[il].ffn_down, NULL, NULL,
18646
18698
NULL,
18647
18699
LLM_FFN_SILU, LLM_FFN_PAR, il);
18648
- cb(cur, "model.layers.{}.feed_forward.w2", il);
18649
-
18650
- return cur;
18651
18700
}
18652
18701
18653
18702
ggml_tensor * build_attn_block(ggml_tensor * cur,
@@ -19817,6 +19866,7 @@ ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
19817
19866
llm = std::make_unique<llm_build_falcon_h1>(*this, params);
19818
19867
} break;
19819
19868
case LLM_ARCH_LFM2:
19869
+ case LLM_ARCH_LFM2MOE:
19820
19870
{
19821
19871
llm = std::make_unique<llm_build_lfm2>(*this, params);
19822
19872
} break;
@@ -20039,6 +20089,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
20039
20089
case LLM_ARCH_OPENAI_MOE:
20040
20090
case LLM_ARCH_HUNYUAN_DENSE:
20041
20091
case LLM_ARCH_LFM2:
20092
+ case LLM_ARCH_LFM2MOE:
20042
20093
case LLM_ARCH_SMALLTHINKER:
20043
20094
case LLM_ARCH_GLM4_MOE:
20044
20095
case LLM_ARCH_SEED_OSS:
0 commit comments