Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_dots1::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 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);10 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);11 12 switch (hparams.n_layer()) {13 case 62: type = LLM_TYPE_142B; break;14 default: type = LLM_TYPE_UNKNOWN;15 }16}17 18void llama_model_dots1::load_arch_tensors(llama_model_loader &) {19 LLAMA_LOAD_LOCALS;20 const int64_t n_expert_shared = hparams.n_expert_shared;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 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);27 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);28 29 for (int i = 0; i < n_layer; ++i) {30 auto & layer = layers[i];31 32 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);33 34 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_head_k * n_head, n_embd_head_k * n_head, 0);35 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);36 37 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);38 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);39 40 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);41 42 if (i < (int) hparams.n_layer_dense_lead) {43 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);44 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);45 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);46 } else {47 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);48 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);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_dots1::build_arch_graph(const llm_graph_params & params) const {71 return std::make_unique<graph>(*this, params);72}73 74llama_model_dots1::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 ggml_tensor * inp_out_ids = build_inp_out_ids();92 93 for (int il = 0; il < n_layer; ++il) {94 ggml_tensor * inpSA = inpL;95 96 // norm97 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);98 cb(cur, "attn_norm", il);99 100 // self_attention101 {102 // compute Q and K and RoPE them103 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,104 n_embd_head, n_head, n_head_kv, il);105 106 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);107 cb(Qcur, "Qcur_normed", il);108 109 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,110 ext_factor, attn_factor, beta_fast, beta_slow);111 112 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);113 cb(Kcur, "Kcur_normed", il);114 115 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, 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, 1.0f / sqrtf(float(n_embd_head)), 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 // MoE branch134 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);135 cb(cur, "ffn_norm", il);136 137 if ((uint32_t) il < hparams.n_layer_dense_lead) {138 cur = build_ffn(cur,139 model.layers[il].ffn_up, NULL, NULL,140 model.layers[il].ffn_gate, NULL, NULL,141 model.layers[il].ffn_down, NULL, NULL,142 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);143 cb(cur, "ffn_out", il);144 } else {145 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 model.layers[il].ffn_exp_probs_b,151 n_expert, n_expert_used,152 LLM_FFN_SILU, hparams.expert_weights_norm,153 hparams.expert_weights_scale,154 (llama_expert_gating_func_type) hparams.expert_gating_func,155 il);156 cb(moe_out, "ffn_moe_out", il);157 158 {159 ggml_tensor * ffn_shexp =160 build_ffn(cur,161 model.layers[il].ffn_up_shexp, NULL, NULL,162 model.layers[il].ffn_gate_shexp, NULL, NULL,163 model.layers[il].ffn_down_shexp, NULL, NULL,164 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);165 cb(ffn_shexp, "ffn_shexp", il);166 167 cur = ggml_add(ctx0, moe_out, ffn_shexp);168 cb(cur, "ffn_out", il);169 }170 }171 cur = ggml_add(ctx0, cur, ffn_inp);172 173 cur = build_cvec(cur, il);174 cb(cur, "l_out", il);175 176 // input for next layer177 inpL = cur;178 }179 cur = inpL;180 181 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);182 183 cb(cur, "result_norm", -1);184 res->t_embd = cur;185 186 // lm_head187 cur = build_lora_mm(model.output, cur, model.output_s);188 189 cb(cur, "result_output", -1);190 res->t_logits = cur;191 192 ggml_build_forward_expand(gf, cur);193}194 