Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_ernie4_5::load_arch_hparams(llama_model_loader & ml) {4 // paddleocr need mrope_section5 ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);6 7 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);8 if (arch == LLM_ARCH_ERNIE4_5_MOE) {9 ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all);10 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);11 ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);12 ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);13 }14 15 switch (hparams.n_layer()) {16 case 18: type = LLM_TYPE_0_3B; break;17 case 28: type = LLM_TYPE_21B_A3B; break;18 case 54: type = LLM_TYPE_300B_A47B; break;19 default: type = LLM_TYPE_UNKNOWN;20 }21}22 23void llama_model_ernie4_5::load_arch_tensors(llama_model_loader &) {24 LLAMA_LOAD_LOCALS;25 26 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);27 28 // output29 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);30 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);31 // if output is NULL, init from the input tok embed32 if (output == NULL) {33 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);34 }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 // optional bias tensors45 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);46 47 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);48 49 if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers50 int n_ff_exp = hparams.n_ff_exp();51 52 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);53 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);54 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);55 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);56 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);57 58 // Shared expert (if present)59 if (hparams.n_ff_shexp > 0) {60 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);61 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);62 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);63 }64 } else { // Dense layers65 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);66 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);67 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);68 }69 }70}71 72std::unique_ptr<llm_graph_context> llama_model_ernie4_5::build_arch_graph(const llm_graph_params & params) const {73 return std::make_unique<graph>(*this, params);74}75 76llama_model_ernie4_5::graph::graph(const llama_model & model, const llm_graph_params & params) :77 llm_graph_context(params) {78 const int64_t n_embd_head = hparams.n_embd_head_v();79 80 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());81 GGML_ASSERT(n_embd_head == n_rot);82 83 ggml_tensor * cur;84 ggml_tensor * inpL;85 86 inpL = build_inp_embd(model.tok_embd);87 88 // inp_pos - contains the positions89 ggml_tensor * inp_pos = build_inp_pos();90 91 auto * inp_attn = build_attn_inp_kv();92 93 ggml_tensor * inp_out_ids = build_inp_out_ids();94 95 for (int il = 0; il < n_layer; ++il) {96 ggml_tensor * inpSA = inpL;97 98 // norm99 {100 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 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,106 n_embd_head, n_head, n_head_kv, il);107 108 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,109 ext_factor, attn_factor, beta_fast, beta_slow);110 111 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,112 ext_factor, attn_factor, beta_fast, beta_slow);113 114 cb(Qcur, "Qcur", il);115 cb(Kcur, "Kcur", il);116 cb(Vcur, "Vcur", il);117 118 cur = build_attn(inp_attn,119 model.layers[il].wo, NULL, model.layers[il].wo_s,120 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);121 }122 if (il == n_layer - 1) {123 // skip computing output for unused tokens124 cur = ggml_get_rows(ctx0, cur, inp_out_ids);125 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);126 }127 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);128 cb(ffn_inp, "ffn_inp", il);129 130 // feed-forward network131 {132 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);133 cb(cur, "ffn_norm", il);134 135 cur = build_ffn(cur,136 model.layers[il].ffn_up, NULL, NULL,137 model.layers[il].ffn_gate, NULL, NULL,138 model.layers[il].ffn_down, NULL, NULL,139 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);140 cb(cur, "ffn_out", il);141 }142 cur = ggml_add(ctx0, cur, ffn_inp);143 144 cur = build_cvec(cur, il);145 cb(cur, "l_out", il);146 147 // input for next layer148 inpL = cur;149 }150 cur = inpL;151 152 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);153 154 cb(cur, "result_norm", -1);155 res->t_embd = cur;156 157 // lm_head158 cur = build_lora_mm(model.output, cur, model.output_s);159 160 cb(cur, "result_output", -1);161 res->t_logits = cur;162 163 ggml_build_forward_expand(gf, cur);164}165 