/src/llama.cpp/src/models/bailingmoe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | 0 | llm_build_bailingmoe::llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
5 | 0 | ggml_tensor * cur; |
6 | 0 | ggml_tensor * inpL; |
7 | |
|
8 | 0 | inpL = build_inp_embd(model.tok_embd); |
9 | | |
10 | | // inp_pos - contains the positions |
11 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
12 | |
|
13 | 0 | auto * inp_attn = build_attn_inp_kv(); |
14 | |
|
15 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
16 | |
|
17 | 0 | for (int il = 0; il < n_layer; ++il) { |
18 | 0 | ggml_tensor * inpSA = inpL; |
19 | | |
20 | | // norm |
21 | 0 | cur = build_norm(inpL, |
22 | 0 | model.layers[il].attn_norm, NULL, |
23 | 0 | LLM_NORM_RMS, il); |
24 | 0 | cb(cur, "attn_norm", il); |
25 | | |
26 | | // self-attention |
27 | 0 | { |
28 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
29 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
30 | | |
31 | | // compute Q and K and RoPE them |
32 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
33 | 0 | cb(Qcur, "Qcur", il); |
34 | 0 | if (model.layers[il].bq) { |
35 | 0 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
36 | 0 | cb(Qcur, "Qcur", il); |
37 | 0 | } |
38 | |
|
39 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
40 | 0 | cb(Kcur, "Kcur", il); |
41 | 0 | if (model.layers[il].bk) { |
42 | 0 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
43 | 0 | cb(Kcur, "Kcur", il); |
44 | 0 | } |
45 | |
|
46 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
47 | 0 | cb(Vcur, "Vcur", il); |
48 | 0 | if (model.layers[il].bv) { |
49 | 0 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
50 | 0 | cb(Vcur, "Vcur", il); |
51 | 0 | } |
52 | |
|
53 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens); |
54 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens); |
55 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens); |
56 | |
|
57 | 0 | Qcur = ggml_rope_ext( |
58 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
59 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
60 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
61 | 0 | ); |
62 | |
|
63 | 0 | Kcur = ggml_rope_ext( |
64 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
65 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
66 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
67 | 0 | ); |
68 | |
|
69 | 0 | cb(Qcur, "Qcur", il); |
70 | 0 | cb(Kcur, "Kcur", il); |
71 | 0 | cb(Vcur, "Vcur", il); |
72 | |
|
73 | 0 | cur = build_attn(inp_attn, |
74 | 0 | model.layers[il].wo, model.layers[il].bo, |
75 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il); |
76 | 0 | } |
77 | |
|
78 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
79 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
80 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
81 | 0 | } |
82 | |
|
83 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
84 | 0 | cb(ffn_inp, "ffn_inp", il); |
85 | |
|
86 | 0 | cur = build_norm(ffn_inp, |
87 | 0 | model.layers[il].ffn_norm, NULL, |
88 | 0 | LLM_NORM_RMS, il); |
89 | 0 | cb(cur, "ffn_norm", il); |
90 | |
|
91 | 0 | ggml_tensor * moe_out = |
92 | 0 | build_moe_ffn(cur, |
93 | 0 | model.layers[il].ffn_gate_inp, |
94 | 0 | model.layers[il].ffn_up_exps, |
95 | 0 | model.layers[il].ffn_gate_exps, |
96 | 0 | model.layers[il].ffn_down_exps, |
97 | 0 | nullptr, |
98 | 0 | n_expert, n_expert_used, |
99 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
100 | 0 | false, hparams.expert_weights_scale, |
101 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
102 | 0 | il); |
103 | 0 | cb(moe_out, "ffn_moe_out", il); |
104 | | |
105 | | // FFN shared expert |
106 | 0 | { |
107 | 0 | ggml_tensor * ffn_shexp = build_ffn(cur, |
108 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
109 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
110 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
111 | 0 | NULL, |
112 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
113 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
114 | |
|
115 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
116 | 0 | cb(cur, "ffn_out", il); |
117 | 0 | } |
118 | |
|
119 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
120 | |
|
121 | 0 | cur = build_cvec(cur, il); |
122 | 0 | cb(cur, "l_out", il); |
123 | | |
124 | | // input for next layer |
125 | 0 | inpL = cur; |
126 | 0 | } |
127 | |
|
128 | 0 | cur = inpL; |
129 | |
|
130 | 0 | cur = build_norm(cur, |
131 | 0 | model.output_norm, NULL, |
132 | 0 | LLM_NORM_RMS, -1); |
133 | |
|
134 | 0 | cb(cur, "result_norm", -1); |
135 | 0 | res->t_embd = cur; |
136 | | |
137 | | // lm_head |
138 | 0 | cur = build_lora_mm(model.output, cur); |
139 | |
|
140 | 0 | cb(cur, "result_output", -1); |
141 | 0 | res->t_logits = cur; |
142 | |
|
143 | 0 | ggml_build_forward_expand(gf, cur); |
144 | 0 | } |