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