Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_deepseek::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(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);6 ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all);7 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);8 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);9 10 switch (hparams.n_ff_exp()) {11 case 1408: type = LLM_TYPE_16B; break;12 case 1792: type = LLM_TYPE_20B; break;13 default: type = LLM_TYPE_UNKNOWN;14 }15}16 17void llama_model_deepseek::load_arch_tensors(llama_model_loader &) {18 LLAMA_LOAD_LOCALS;19 const int64_t n_expert_shared = hparams.n_expert_shared;20 21 22 const int64_t n_ff_exp = hparams.n_ff_exp();23 24 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);25 26 // output27 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);28 // try to load output.weight, if not found, use token_embd (tied embeddings)29 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);30 if (!output) {31 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);32 }33 34 for (int i = 0; i < n_layer; ++i) {35 auto & layer = layers[i];36 37 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);38 39 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);40 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);41 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);42 43 if (i < (int) hparams.n_layer_dense_lead) {44 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);45 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);46 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);47 } else {48 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);49 50 if (n_expert == 0) {51 throw std::runtime_error("n_expert must be > 0");52 }53 if (n_expert_used == 0) {54 throw std::runtime_error("n_expert_used must be > 0");55 }56 57 // MoE branch58 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);59 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);60 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);61 62 // Shared expert branch63 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);64 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);65 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);66 }67 }68}69 70std::unique_ptr<llm_graph_context> llama_model_deepseek::build_arch_graph(const llm_graph_params & params) const {71 return std::make_unique<graph>(*this, params);72}73 74llama_model_deepseek::graph::graph(const llama_model & model, const llm_graph_params & params) :75 llm_graph_context(params) {76 const int64_t n_embd_head = hparams.n_embd_head_v();77 78 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());79 GGML_ASSERT(n_embd_head == n_rot);80 81 ggml_tensor * cur;82 ggml_tensor * inpL;83 84 inpL = build_inp_embd(model.tok_embd);85 86 // inp_pos - contains the positions87 ggml_tensor * inp_pos = build_inp_pos();88 89 auto * inp_attn = build_attn_inp_kv();90 91 const float kq_scale =92 hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;93 94 ggml_tensor * inp_out_ids = build_inp_out_ids();95 96 for (int il = 0; il < n_layer; ++il) {97 ggml_tensor * inpSA = inpL;98 99 // norm100 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);101 cb(cur, "attn_norm", il);102 103 // self-attention104 {105 // rope freq factors for llama3; may return nullptr for llama2 and other models106 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);107 108 // compute Q and K and RoPE them109 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,110 n_embd_head, n_head, n_head_kv, il);111 112 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,113 ext_factor, attn_factor, beta_fast, beta_slow);114 115 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,116 ext_factor, attn_factor, beta_fast, beta_slow);117 118 cb(Qcur, "Qcur", il);119 cb(Kcur, "Kcur", il);120 cb(Vcur, "Vcur", il);121 122 cur = build_attn(inp_attn,123 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,124 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);125 }126 if (il == n_layer - 1 && inp_out_ids) {127 cur = ggml_get_rows(ctx0, cur, inp_out_ids);128 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);129 }130 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);131 cb(ffn_inp, "ffn_inp", il);132 133 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);134 cb(cur, "ffn_norm", il);135 136 if ((uint32_t) il < hparams.n_layer_dense_lead) {137 cur = build_ffn(cur,138 model.layers[il].ffn_up, NULL, NULL,139 model.layers[il].ffn_gate, NULL, NULL,140 model.layers[il].ffn_down, NULL, NULL,141 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);142 cb(cur, "ffn_out", il);143 } else {144 // MoE branch145 ggml_tensor * moe_out = build_moe_ffn(cur,146 model.layers[il].ffn_gate_inp,147 model.layers[il].ffn_up_exps,148 model.layers[il].ffn_gate_exps,149 model.layers[il].ffn_down_exps,150 nullptr,151 n_expert, n_expert_used,152 LLM_FFN_SILU, false,153 hparams.expert_weights_scale,154 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,155 il);156 cb(moe_out, "ffn_moe_out", il);157 158 // FFN shared expert159 {160 ggml_tensor * ffn_shexp =161 build_ffn(cur,162 model.layers[il].ffn_up_shexp, NULL, NULL,163 model.layers[il].ffn_gate_shexp, NULL, NULL,164 model.layers[il].ffn_down_shexp, NULL, NULL,165 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);166 cb(ffn_shexp, "ffn_shexp", il);167 168 cur = ggml_add(ctx0, moe_out, ffn_shexp);169 cb(cur, "ffn_out", il);170 }171 }172 cur = ggml_add(ctx0, cur, ffn_inp);173 174 cur = build_cvec(cur, il);175 cb(cur, "l_out", il);176 177 // input for next layer178 inpL = cur;179 }180 cur = inpL;181 182 cur = build_norm(cur, model.output_norm, NULL, 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 