/src/llama.cpp/src/models/bailingmoe2.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | | |
5 | | llm_build_bailingmoe2::llm_build_bailingmoe2(const llama_model & model, const llm_graph_params & params) : |
6 | 0 | llm_graph_context(params) { |
7 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
8 | 0 | const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
9 | |
|
10 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
11 | |
|
12 | 0 | ggml_tensor * cur; |
13 | 0 | ggml_tensor * inpL; |
14 | |
|
15 | 0 | inpL = build_inp_embd(model.tok_embd); |
16 | | |
17 | | // inp_pos - contains the positions |
18 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
19 | |
|
20 | 0 | auto * inp_attn = build_attn_inp_kv(); |
21 | |
|
22 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
23 | |
|
24 | 0 | const int n_transformer_layers = n_layer - hparams.nextn_predict_layers; |
25 | 0 | for (int il = 0; il < n_transformer_layers; ++il) { |
26 | 0 | ggml_tensor * inpSA = inpL; |
27 | | |
28 | | // norm |
29 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
30 | 0 | cb(cur, "attn_norm", il); |
31 | | |
32 | | // self_attention |
33 | 0 | { |
34 | 0 | cur = build_lora_mm(model.layers[il].wqkv, cur); |
35 | 0 | cb(cur, "wqkv", il); |
36 | |
|
37 | 0 | ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), |
38 | 0 | cur->nb[1], 0 * sizeof(float) * (n_embd)); |
39 | 0 | ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
40 | 0 | cur->nb[1], 1 * sizeof(float) * (n_embd)); |
41 | 0 | ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
42 | 0 | cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)); |
43 | |
|
44 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
45 | 0 | cb(Qcur, "Qcur_normed", il); |
46 | |
|
47 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
48 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
49 | |
|
50 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
51 | 0 | cb(Kcur, "Kcur_normed", il); |
52 | |
|
53 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
54 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
55 | |
|
56 | 0 | cb(Qcur, "Qcur", il); |
57 | 0 | cb(Kcur, "Kcur", il); |
58 | 0 | cb(Vcur, "Vcur", il); |
59 | |
|
60 | 0 | cur = build_attn(inp_attn, |
61 | 0 | model.layers[il].wo, model.layers[il].bo, |
62 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
63 | 0 | } |
64 | |
|
65 | 0 | if (il == n_transformer_layers - 1 && inp_out_ids) { |
66 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
67 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
68 | 0 | } |
69 | |
|
70 | 0 | ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA); |
71 | 0 | cb(sa_out, "sa_out", il); |
72 | | |
73 | | // MoE branch |
74 | 0 | cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
75 | 0 | cb(cur, "ffn_norm", il); |
76 | |
|
77 | 0 | if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) { |
78 | 0 | cur = build_ffn(cur, |
79 | 0 | model.layers[il].ffn_up, NULL, NULL, |
80 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
81 | 0 | model.layers[il].ffn_down, NULL, NULL, |
82 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
83 | 0 | cb(cur, "ffn_out", il); |
84 | 0 | } else { |
85 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
86 | 0 | model.layers[il].ffn_gate_inp, |
87 | 0 | model.layers[il].ffn_up_exps, |
88 | 0 | model.layers[il].ffn_gate_exps, |
89 | 0 | model.layers[il].ffn_down_exps, |
90 | 0 | model.layers[il].ffn_exp_probs_b, |
91 | 0 | n_expert, n_expert_used, |
92 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
93 | 0 | true, hparams.expert_weights_scale, |
94 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
95 | 0 | il); |
96 | 0 | cb(moe_out, "ffn_moe_out", il); |
97 | |
|
98 | 0 | { |
99 | 0 | ggml_tensor * ffn_shexp = |
100 | 0 | build_ffn(cur, |
101 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
102 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
103 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
104 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
105 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
106 | |
|
107 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
108 | 0 | cb(cur, "ffn_out", il); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | 0 | cur = ggml_add(ctx0, cur, sa_out); |
113 | |
|
114 | 0 | cur = build_cvec(cur, il); |
115 | 0 | cb(cur, "l_out", il); |
116 | | |
117 | | // input for next layer |
118 | 0 | inpL = cur; |
119 | 0 | } |
120 | |
|
121 | 0 | cur = inpL; |
122 | |
|
123 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
124 | |
|
125 | 0 | cb(cur, "result_norm", -1); |
126 | 0 | res->t_embd = cur; |
127 | | |
128 | | // lm_head |
129 | 0 | cur = build_lora_mm(model.output, cur); |
130 | |
|
131 | 0 | cb(cur, "result_output", -1); |
132 | 0 | res->t_logits = cur; |
133 | |
|
134 | 0 | ggml_build_forward_expand(gf, cur); |
135 | 0 | } |