Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_openai_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_ATTENTION_SLIDING_WINDOW, hparams.n_swa);7 8 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;9 load_swa_pattern(ml, 2);10 11 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;12 hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;13 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);14 15 switch (hparams.n_layer()) {16 case 24: type = LLM_TYPE_20B; break;17 case 36: type = LLM_TYPE_120B; break;18 default: type = LLM_TYPE_UNKNOWN;19 }20}21 22void llama_model_openai_moe::load_arch_tensors(llama_model_loader &) {23 LLAMA_LOAD_LOCALS;24 25 const int64_t n_ff_exp = hparams.n_ff_exp();26 27 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);28 29 // output30 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);31 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);32 33 for (int i = 0; i < n_layer; ++i) {34 auto & layer = layers[i];35 36 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);37 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);38 39 create_tensor_qkv(layer, i, n_embd, n_head * n_rot, n_head_kv * n_rot, n_head_kv * n_rot, 0);40 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);41 42 layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);43 44 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);45 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);46 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);47 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);48 49 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);50 51 layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);52 layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);53 layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);54 layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);55 }56}57 58std::unique_ptr<llm_graph_context> llama_model_openai_moe::build_arch_graph(const llm_graph_params & params) const {59 return std::make_unique<graph>(*this, params);60}61 62llama_model_openai_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {63 ggml_tensor * cur;64 ggml_tensor * inpL;65 66 inpL = build_inp_embd(model.tok_embd);67 68 // inp_pos - contains the positions69 ggml_tensor * inp_pos = build_inp_pos();70 71 auto * inp_attn = build_attn_inp_kv_iswa();72 73 ggml_tensor * inp_out_ids = build_inp_out_ids();74 75 for (int il = 0; il < n_layer; ++il) {76 res->t_layer_inp[il] = inpL;77 78 const float freq_base_l = model.get_rope_freq_base (cparams, il);79 const float freq_scale_l = model.get_rope_freq_scale(cparams, il);80 81 ggml_tensor * inpSA = inpL;82 83 // norm84 cur = build_norm(inpL,85 model.layers[il].attn_norm, nullptr,86 LLM_NORM_RMS, il);87 cb(cur, "attn_norm", il);88 89 // self-attention90 {91 // compute Q and K and RoPE them92 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,93 n_rot, n_head, n_head_kv, il);94 95 Qcur = ggml_rope_ext(96 ctx0, Qcur, inp_pos, nullptr,97 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,98 ext_factor, attn_factor, beta_fast, beta_slow99 );100 101 Kcur = ggml_rope_ext(102 ctx0, Kcur, inp_pos, nullptr,103 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,104 ext_factor, attn_factor, beta_fast, beta_slow105 );106 107 cb(Qcur, "Qcur", il);108 cb(Kcur, "Kcur", il);109 cb(Vcur, "Vcur", il);110 111 cur = build_attn(inp_attn,112 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,113 Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);114 115 cb(cur, "attn_out", il);116 }117 if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) {118 // skip computing output for unused tokens119 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 cur = ffn_inp;126 cur = build_norm(cur,127 model.layers[il].attn_post_norm, nullptr,128 LLM_NORM_RMS, il);129 cb(cur, "attn_post_norm", il);130 131 // MoE branch132 cur = build_moe_ffn(cur,133 model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,134 model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,135 model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,136 model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,137 nullptr,138 n_expert, n_expert_used,139 LLM_FFN_SWIGLU_OAI_MOE, false,140 hparams.expert_weights_scale,141 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,142 il);143 cb(cur, "ffn_moe_out", il);144 145 cur = ggml_add(ctx0, cur, ffn_inp);146 147 cur = build_cvec(cur, il);148 cb(cur, "l_out", il);149 150 // input for next layer151 inpL = cur;152 }153 cur = inpL;154 155 res->t_h_nextn = cur;156 157 if (!cparams.embeddings_nextn_masked && inp_out_ids) {158 cur = ggml_get_rows(ctx0, cur, inp_out_ids);159 }160 161 cur = build_norm(cur,162 model.output_norm, NULL,163 LLM_NORM_RMS, -1);164 165 cb(cur, "result_norm", -1);166 res->t_embd = cur;167 168 // lm_head169 cur = build_lora_mm(model.output, cur, model.output_s);170 171 cb(cur, "result_output", -1);172 res->t_logits = cur;173 174 ggml_build_forward_expand(gf, cur);175}176 