/src/llama.cpp/src/models/hunyuan-moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_hunyuan_moe::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
6 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
7 | |
|
8 | 0 | switch (hparams.n_layer()) { |
9 | 0 | case 32: type = LLM_TYPE_A13B; break; |
10 | 0 | default: type = LLM_TYPE_UNKNOWN; |
11 | 0 | } |
12 | 0 | } |
13 | | |
14 | 0 | void llama_model_hunyuan_moe::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 | 0 | const uint32_t n_ff_shexp = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : hparams.n_ff(i); |
30 | |
|
31 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
32 | |
|
33 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0); |
34 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
35 | |
|
36 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
37 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
38 | |
|
39 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
40 | |
|
41 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
42 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
43 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
44 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
45 | |
|
46 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
47 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
48 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | 0 | std::unique_ptr<llm_graph_context> llama_model_hunyuan_moe::build_arch_graph(const llm_graph_params & params) const { |
53 | 0 | return std::make_unique<graph>(*this, params); |
54 | 0 | } |
55 | | |
56 | 0 | llama_model_hunyuan_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
57 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
58 | |
|
59 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
60 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
61 | |
|
62 | 0 | ggml_tensor * cur; |
63 | 0 | ggml_tensor * inpL; |
64 | |
|
65 | 0 | inpL = build_inp_embd(model.tok_embd); |
66 | | |
67 | | // inp_pos - contains the positions |
68 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
69 | |
|
70 | 0 | auto * inp_attn = build_attn_inp_kv(); |
71 | |
|
72 | 0 | const float kq_scale = 1.0f / sqrtf(float(n_embd_head)); |
73 | |
|
74 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
75 | |
|
76 | 0 | for (int il = 0; il < n_layer; ++il) { |
77 | 0 | ggml_tensor * inpSA = inpL; |
78 | | |
79 | | // norm |
80 | 0 | cur = build_norm(inpL, |
81 | 0 | model.layers[il].attn_norm, NULL, |
82 | 0 | LLM_NORM_RMS, il); |
83 | 0 | cb(cur, "attn_norm", il); |
84 | | |
85 | | // self-attention |
86 | 0 | { |
87 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
88 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
89 | | |
90 | | // compute Q and K and RoPE them |
91 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
92 | 0 | n_embd_head, n_head, n_head_kv, il); |
93 | |
|
94 | 0 | Qcur = ggml_rope_ext( |
95 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
96 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
97 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
98 | 0 | ); |
99 | |
|
100 | 0 | cb(Qcur, "Qcur", il); |
101 | 0 | cb(Kcur, "Kcur", il); |
102 | 0 | cb(Vcur, "Vcur", il); |
103 | |
|
104 | 0 | Kcur = ggml_rope_ext( |
105 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
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 | Kcur = build_norm(Kcur, |
111 | 0 | model.layers[il].attn_k_norm, nullptr, |
112 | 0 | LLM_NORM_RMS, il); |
113 | 0 | cb(Kcur, "Kcur_norm", il); |
114 | |
|
115 | 0 | Qcur = build_norm(Qcur, |
116 | 0 | model.layers[il].attn_q_norm, nullptr, |
117 | 0 | LLM_NORM_RMS, il); |
118 | 0 | cb(Qcur, "Qcur_norm", il); |
119 | |
|
120 | 0 | cur = build_attn(inp_attn, |
121 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
122 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
123 | 0 | cb(cur, "attn_out", il); |
124 | 0 | } |
125 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
126 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
127 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
128 | 0 | } |
129 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
130 | 0 | cb(ffn_inp, "ffn_inp", il); |
131 | |
|
132 | 0 | cur = build_norm(ffn_inp, |
133 | 0 | model.layers[il].ffn_norm, NULL, |
134 | 0 | LLM_NORM_RMS, il); |
135 | 0 | cb(cur, "ffn_norm", il); |
136 | | |
137 | | // feed-forward network (non-MoE) |
138 | 0 | ggml_tensor * cur_mlp = build_ffn(cur, |
139 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
140 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
141 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
142 | 0 | NULL, |
143 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
144 | 0 | cb(cur_mlp, "ffn_mlp", il); |
145 | | |
146 | | // MoE branch |
147 | 0 | ggml_tensor * cur_moe = build_moe_ffn(cur, |
148 | 0 | model.layers[il].ffn_gate_inp, |
149 | 0 | model.layers[il].ffn_up_exps, |
150 | 0 | model.layers[il].ffn_gate_exps, |
151 | 0 | model.layers[il].ffn_down_exps, |
152 | 0 | nullptr, |
153 | 0 | n_expert, n_expert_used, |
154 | 0 | LLM_FFN_SILU, |
155 | 0 | true, // norm_topk_prob |
156 | 0 | hparams.expert_weights_scale, |
157 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
158 | 0 | il); |
159 | 0 | cb(cur_moe, "ffn_moe_out", il); |
160 | |
|
161 | 0 | ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp); |
162 | 0 | cb(ffn_out, "ffn_out", il); |
163 | |
|
164 | 0 | cur = ggml_add(ctx0, ffn_out, ffn_inp); |
165 | |
|
166 | 0 | cur = build_cvec(cur, il); |
167 | 0 | cb(cur, "l_out", il); |
168 | | |
169 | | // input for next layer |
170 | 0 | inpL = cur; |
171 | 0 | } |
172 | 0 | cur = inpL; |
173 | |
|
174 | 0 | cur = build_norm(cur, |
175 | 0 | model.output_norm, NULL, |
176 | 0 | LLM_NORM_RMS, -1); |
177 | |
|
178 | 0 | cb(cur, "result_norm", -1); |
179 | 0 | res->t_embd = cur; |
180 | | |
181 | | // lm_head |
182 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
183 | 0 | cb(cur, "result_output", -1); |
184 | 0 | res->t_logits = cur; |
185 | |
|
186 | 0 | ggml_build_forward_expand(gf, cur); |
187 | 0 | } |