/src/llama.cpp/src/models/qwen2moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_qwen2moe::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
5 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
6 | |
|
7 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
8 | |
|
9 | 0 | switch (hparams.n_layer()) { |
10 | 0 | case 24: type = LLM_TYPE_A2_7B; break; |
11 | 0 | case 28: type = LLM_TYPE_57B_A14B; break; |
12 | 0 | default: type = LLM_TYPE_UNKNOWN; |
13 | 0 | } |
14 | 0 | } |
15 | | |
16 | 0 | void llama_model_qwen2moe::load_arch_tensors(llama_model_loader &) { |
17 | 0 | LLAMA_LOAD_LOCALS; |
18 | |
|
19 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
20 | | |
21 | | // output |
22 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
23 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
24 | |
|
25 | 0 | for (int i = 0; i < n_layer; ++i) { |
26 | 0 | auto & layer = layers[i]; |
27 | |
|
28 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
29 | |
|
30 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0); |
31 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
32 | |
|
33 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
34 | |
|
35 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
36 | |
|
37 | 0 | if (n_expert == 0) { |
38 | 0 | throw std::runtime_error("n_expert must be > 0 for QWEN2MOE"); |
39 | 0 | } |
40 | 0 | if (n_expert_used == 0) { |
41 | 0 | throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE"); |
42 | 0 | } |
43 | | |
44 | | // MoE branch |
45 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
46 | |
|
47 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
48 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
49 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
50 | | |
51 | | // Shared expert branch |
52 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff; |
53 | |
|
54 | 0 | layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0); |
55 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
56 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
57 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | std::unique_ptr<llm_graph_context> llama_model_qwen2moe::build_arch_graph(const llm_graph_params & params) const { |
62 | 0 | return std::make_unique<graph>(*this, params); |
63 | 0 | } |
64 | | |
65 | 0 | llama_model_qwen2moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
66 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
67 | |
|
68 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
69 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
70 | |
|
71 | 0 | ggml_tensor * cur; |
72 | 0 | ggml_tensor * inpL; |
73 | |
|
74 | 0 | inpL = build_inp_embd(model.tok_embd); |
75 | | |
76 | | // inp_pos - contains the positions |
77 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
78 | |
|
79 | 0 | auto * inp_attn = build_attn_inp_kv(); |
80 | |
|
81 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
82 | |
|
83 | 0 | for (int il = 0; il < n_layer; ++il) { |
84 | 0 | ggml_tensor * inpSA = inpL; |
85 | | |
86 | | // norm |
87 | 0 | cur = build_norm(inpL, |
88 | 0 | model.layers[il].attn_norm, NULL, |
89 | 0 | LLM_NORM_RMS, il); |
90 | 0 | cb(cur, "attn_norm", il); |
91 | | |
92 | | // self_attention |
93 | 0 | { |
94 | | // compute Q and K and RoPE them |
95 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
96 | 0 | n_embd_head, n_head, n_head_kv, il); |
97 | |
|
98 | 0 | Qcur = ggml_rope_ext( |
99 | 0 | ctx0, Qcur, inp_pos, nullptr, |
100 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
101 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
102 | 0 | ); |
103 | |
|
104 | 0 | Kcur = ggml_rope_ext( |
105 | 0 | ctx0, Kcur, inp_pos, nullptr, |
106 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
107 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
108 | 0 | ); |
109 | |
|
110 | 0 | cb(Qcur, "Qcur", il); |
111 | 0 | cb(Kcur, "Kcur", il); |
112 | 0 | cb(Vcur, "Vcur", il); |
113 | |
|
114 | 0 | cur = build_attn(inp_attn, |
115 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
116 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
117 | 0 | } |
118 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
119 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
120 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
121 | 0 | } |
122 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
123 | 0 | cb(ffn_inp, "ffn_inp", il); |
124 | | |
125 | | // MoE branch |
126 | 0 | cur = build_norm(ffn_inp, |
127 | 0 | model.layers[il].ffn_norm, NULL, |
128 | 0 | LLM_NORM_RMS, il); |
129 | 0 | cb(cur, "ffn_norm", il); |
130 | |
|
131 | 0 | ggml_tensor * moe_out = |
132 | 0 | build_moe_ffn(cur, |
133 | 0 | model.layers[il].ffn_gate_inp, |
134 | 0 | model.layers[il].ffn_up_exps, |
135 | 0 | model.layers[il].ffn_gate_exps, |
136 | 0 | model.layers[il].ffn_down_exps, |
137 | 0 | nullptr, |
138 | 0 | n_expert, n_expert_used, |
139 | 0 | LLM_FFN_SILU, false, |
140 | 0 | hparams.expert_weights_scale, |
141 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
142 | 0 | il); |
143 | 0 | cb(moe_out, "ffn_moe_out", il); |
144 | | |
145 | | // FFN shared expert |
146 | 0 | { |
147 | 0 | ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur); |
148 | 0 | cb(cur_gate_inp, "ffn_shexp_gate_inp", il); |
149 | | |
150 | | // sigmoid |
151 | 0 | ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp); |
152 | 0 | cb(cur_gate, "ffn_shexp_gate", il); |
153 | |
|
154 | 0 | ggml_tensor * cur_ffn = build_ffn(cur, |
155 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
156 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
157 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
158 | 0 | NULL, |
159 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
160 | 0 | cb(cur_ffn, "ffn_shexp", il); |
161 | |
|
162 | 0 | ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate); |
163 | 0 | cb(ffn_shexp_out, "ffn_shexp_out", il); |
164 | |
|
165 | 0 | moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out); |
166 | 0 | cb(moe_out, "ffn_out", il); |
167 | |
|
168 | 0 | cur = moe_out; |
169 | 0 | } |
170 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
171 | |
|
172 | 0 | cur = build_cvec(cur, il); |
173 | 0 | cb(cur, "l_out", il); |
174 | | |
175 | | // input for next layer |
176 | 0 | inpL = cur; |
177 | 0 | } |
178 | 0 | cur = inpL; |
179 | |
|
180 | 0 | cur = build_norm(cur, |
181 | 0 | model.output_norm, NULL, |
182 | 0 | LLM_NORM_RMS, -1); |
183 | |
|
184 | 0 | cb(cur, "result_norm", -1); |
185 | 0 | res->t_embd = cur; |
186 | | |
187 | | // lm_head |
188 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
189 | |
|
190 | 0 | cb(cur, "result_output", -1); |
191 | 0 | res->t_logits = cur; |
192 | |
|
193 | 0 | ggml_build_forward_expand(gf, cur); |
194 | 0 | } |