/src/llama.cpp/src/models/lfm2moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | #include "../llama-memory-hybrid-iswa.h" |
3 | | #include "../llama-memory-hybrid.h" |
4 | | |
5 | 0 | void llama_model_lfm2moe::load_arch_hparams(llama_model_loader & ml) { |
6 | 0 | ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache); |
7 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
8 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
9 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
10 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func); |
11 | |
|
12 | 0 | for (uint32_t il = 0; il < hparams.n_layer(); ++il) { |
13 | 0 | hparams.is_recr_impl[il] = hparams.n_head_kv(il) == 0; |
14 | 0 | } |
15 | |
|
16 | 0 | switch (hparams.n_layer()) { |
17 | 0 | case 24: type = LLM_TYPE_8B_A1B; break; |
18 | 0 | case 40: type = LLM_TYPE_24B_A2B; break; |
19 | 0 | default: type = LLM_TYPE_UNKNOWN; |
20 | 0 | } |
21 | 0 | } |
22 | | |
23 | 0 | void llama_model_lfm2moe::load_arch_tensors(llama_model_loader &) { |
24 | 0 | LLAMA_LOAD_LOCALS; |
25 | |
|
26 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
27 | |
|
28 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM_LFM2, "weight"), {n_embd}, 0); |
29 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
30 | |
|
31 | 0 | if (output == NULL) { |
32 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
33 | 0 | } |
34 | |
|
35 | 0 | for (int i = 0; i < n_layer; ++i) { |
36 | 0 | auto & layer = layers[i]; |
37 | |
|
38 | 0 | const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead); |
39 | | |
40 | | // ffn/moe is same for transformer and conv layers |
41 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
42 | 0 | if (is_moe_layer) { |
43 | 0 | GGML_ASSERT(n_expert && n_expert_used); |
44 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
45 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0); |
46 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp, n_embd, n_expert}, 0); |
47 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0); |
48 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
49 | 0 | } else { // dense |
50 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
51 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
52 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
53 | 0 | } |
54 | | |
55 | | // for operator_norm |
56 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
57 | |
|
58 | 0 | if (!hparams.is_recr(i)) { |
59 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
60 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
61 | 0 | GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa); |
62 | |
|
63 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd, hparams.n_embd_k_gqa(i), hparams.n_embd_v_gqa(i), 0); |
64 | |
|
65 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
66 | 0 | } else { |
67 | 0 | layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0); |
68 | 0 | layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0); |
69 | 0 | layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0); |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | // for LFM2-ColBert-350M |
74 | 0 | dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.n_embd_out()}, TENSOR_NOT_REQUIRED); |
75 | 0 | dense_2_out_layers_b = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "bias"), {hparams.n_embd_out() }, TENSOR_NOT_REQUIRED); |
76 | 0 | } |
77 | | |
78 | 0 | std::unique_ptr<llm_graph_context> llama_model_lfm2moe::build_arch_graph(const llm_graph_params & params) const { |
79 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
80 | 0 | return std::make_unique<graph<true>>(*this, params); |
81 | 0 | } else { |
82 | 0 | return std::make_unique<graph<false>>(*this, params); |
83 | 0 | } |
84 | 0 | } |
85 | | |