Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_mellum::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, false);7 8 if (hparams.n_swa > 0) {9 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;10 11 load_swa_pattern(ml, 4);12 13 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;14 hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;15 16 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);17 } else {18 hparams.swa_type = LLAMA_SWA_TYPE_NONE;19 }20 21 switch (hparams.n_layer()) {22 case 28: type = LLM_TYPE_12B_A2_5B; break;23 default: type = LLM_TYPE_UNKNOWN;24 }25}26 27void llama_model_mellum::load_arch_tensors(llama_model_loader &) {28 LLAMA_LOAD_LOCALS;29 30 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);31 32 // output33 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);34 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);35 36 for (int i = 0; i < n_layer; ++i) {37 auto & layer = layers[i];38 39 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);40 41 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);42 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);43 44 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);45 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);46 47 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);48 49 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);50 51 if (n_expert == 0) {52 throw std::runtime_error("n_expert must be > 0 for Mellum");53 }54 if (n_expert_used == 0) {55 throw std::runtime_error("n_expert_used must be > 0 for Mellum");56 }57 58 const int64_t n_ff_exp = hparams.n_ff_exp() ? hparams.n_ff_exp() : n_ff / n_expert_used;59 60 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);61 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);62 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);63 }64}65 66std::unique_ptr<llm_graph_context> llama_model_mellum::build_arch_graph(const llm_graph_params & params) const {67 if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {68 return std::make_unique<graph<true>>(*this, params);69 }70 return std::make_unique<graph<false>>(*this, params);71}72 73template <bool iswa>74llama_model_mellum::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {75 const int64_t n_embd_head = hparams.n_embd_head_v();76 77 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());78 GGML_ASSERT(n_embd_head == n_rot);79 80 ggml_tensor * cur;81 ggml_tensor * inpL;82 83 inpL = build_inp_embd(model.tok_embd);84 85 // inp_pos - contains the positions86 ggml_tensor * inp_pos = build_inp_pos();87 88 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;89 inp_attn_type * inp_attn = nullptr;90 91 if constexpr (iswa) {92 inp_attn = build_attn_inp_kv_iswa();93 } else {94 inp_attn = build_attn_inp_kv();95 }96 97 ggml_tensor * inp_out_ids = build_inp_out_ids();98 99 for (int il = 0; il < n_layer; ++il) {100 ggml_tensor * inpSA = inpL;101 102 // norm103 cur = build_norm(inpL,104 model.layers[il].attn_norm, nullptr,105 LLM_NORM_RMS, il);106 cb(cur, "attn_norm", il);107 108 // self_attention109 {110 // compute Q and K and RoPE them111 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,112 n_embd_head, n_head, n_head_kv, il);113 114 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);115 cb(Qcur, "Qcur_normed", il);116 117 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, nullptr, LLM_NORM_RMS, il);118 cb(Kcur, "Kcur_normed", il);119 120 const bool is_swa = hparams.is_swa(il);121 122 if (is_swa) {123 // For sliding window layers, use regular rope with no yarn rope scaling.124 // This is achieved here by setting freq_scale and attn_factor to 1.125 // We also set ext_factor to 0 to avoid a few unnecessary computations.126 Qcur = ggml_rope_ext(127 ctx0, Qcur, inp_pos, nullptr,128 n_rot, rope_type, n_ctx_orig, freq_base, 1.0,129 0.0, 1.0, beta_fast, beta_slow130 );131 132 Kcur = ggml_rope_ext(133 ctx0, Kcur, inp_pos, nullptr,134 n_rot, rope_type, n_ctx_orig, freq_base, 1.0,135 0.0, 1.0, beta_fast, beta_slow136 );137 } else {138 Qcur = ggml_rope_ext(139 ctx0, Qcur, inp_pos, nullptr,140 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,141 ext_factor, attn_factor, beta_fast, beta_slow142 );143 144 Kcur = ggml_rope_ext(145 ctx0, Kcur, inp_pos, nullptr,146 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,147 ext_factor, attn_factor, beta_fast, beta_slow148 );149 }150 151 cb(Qcur, "Qcur", il);152 cb(Kcur, "Kcur", il);153 cb(Vcur, "Vcur", il);154 155 cur = build_attn(inp_attn,156 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,157 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);158 }159 if (il == n_layer - 1 && inp_out_ids) {160 cur = ggml_get_rows(ctx0, cur, inp_out_ids);161 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);162 }163 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);164 cb(ffn_inp, "ffn_inp", il);165 166 // MoE167 cur = build_norm(ffn_inp,168 model.layers[il].ffn_norm, nullptr,169 LLM_NORM_RMS, il);170 cb(cur, "ffn_norm", il);171 172 ggml_tensor * moe_out =173 build_moe_ffn(cur,174 model.layers[il].ffn_gate_inp,175 model.layers[il].ffn_up_exps,176 model.layers[il].ffn_gate_exps,177 model.layers[il].ffn_down_exps,178 nullptr,179 n_expert, n_expert_used,180 LLM_FFN_SILU, true,181 hparams.expert_weights_scale,182 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,183 il,184 nullptr, nullptr,185 model.layers[il].ffn_up_exps_s,186 model.layers[il].ffn_gate_exps_s,187 model.layers[il].ffn_down_exps_s);188 cb(moe_out, "ffn_moe_out", il);189 cur = moe_out;190 191 cur = ggml_add(ctx0, cur, ffn_inp);192 cb(cur, "ffn_out", il);193 194 cur = build_cvec(cur, il);195 cb(cur, "l_out", il);196 197 // input for next layer198 inpL = cur;199 }200 cur = inpL;201 202 cur = build_norm(cur,203 model.output_norm, nullptr,204 LLM_NORM_RMS, -1);205 206 cb(cur, "result_norm", -1);207 res->t_embd = cur;208 209 // lm_head210 cur = build_lora_mm(model.output, cur, model.output_s);211 212 cb(cur, "result_output", -1);213 res->t_logits = cur;214 215 ggml_build_forward_expand(gf, cur);216}217 218template struct llama_model_mellum::graph<false>;219template struct llama_model_mellum::graph<true>;220 