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