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