Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_openelm::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6 switch (hparams.n_layer()) {7 case 16: type = LLM_TYPE_270M; break;8 case 20: type = LLM_TYPE_450M; break;9 case 28: type = LLM_TYPE_1B; break;10 case 36: type = LLM_TYPE_3B; break;11 default: type = LLM_TYPE_UNKNOWN;12 }13}14 15void llama_model_openelm::load_arch_tensors(llama_model_loader &) {16 LLAMA_LOAD_LOCALS;17 18 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);19 20 // output21 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);22 // init output from the input tok embed23 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);24 25 for (int i = 0; i < n_layer; ++i) {26 const int64_t n_head = hparams.n_head(i);27 const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;28 const int64_t n_ff = hparams.n_ff(i);29 30 auto & layer = layers[i];31 32 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);33 34 layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);35 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);36 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);37 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);38 39 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);40 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);41 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);42 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);43 }44}45 46std::unique_ptr<llm_graph_context> llama_model_openelm::build_arch_graph(const llm_graph_params & params) const {47 return std::make_unique<graph>(*this, params);48}49 50llama_model_openelm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {51 const int64_t n_embd_head = hparams.n_embd_head_v();52 53 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());54 55 ggml_tensor * cur;56 ggml_tensor * inpL;57 inpL = build_inp_embd(model.tok_embd);58 59 // inp_pos - contains the positions60 ggml_tensor * inp_pos = build_inp_pos();61 62 auto * inp_attn = build_attn_inp_kv();63 64 ggml_tensor * inp_out_ids = build_inp_out_ids();65 66 for (int il = 0; il < n_layer; ++il) {67 const int64_t n_head = hparams.n_head(il);68 const int64_t n_head_kv = hparams.n_head_kv(il);69 const int64_t n_head_qkv = 2*n_head_kv + n_head;70 71 cur = inpL;72 ggml_tensor * residual = cur;73 74 // norm75 cur = build_norm(inpL,76 model.layers[il].attn_norm, NULL,77 LLM_NORM_RMS, il);78 cb(cur, "attn_norm", il);79 80 // self-attention81 {82 cur = build_lora_mm(model.layers[il].wqkv, cur);83 cb(cur, "wqkv", il);84 85 cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);86 87 ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);88 cb(Qcur, "Qcur", il);89 90 ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*n_head);91 cb(Kcur, "Kcur", il);92 93 ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*(n_head+n_head_kv));94 cb(Vcur, "Vcur", il);95 96 Qcur = build_norm(Qcur,97 model.layers[il].attn_q_norm, NULL,98 LLM_NORM_RMS, il);99 cb(Qcur, "Qcur", il);100 101 Kcur = build_norm(Kcur,102 model.layers[il].attn_k_norm, NULL,103 LLM_NORM_RMS, il);104 cb(Kcur, "Kcur", il);105 106 Qcur = ggml_rope_ext(107 ctx0, Qcur, inp_pos, NULL,108 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,109 ext_factor, attn_factor, beta_fast, beta_slow110 );111 112 Kcur = ggml_rope_ext(113 ctx0, Kcur, inp_pos, NULL,114 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,115 ext_factor, attn_factor, beta_fast, beta_slow116 );117 118 cb(Qcur, "Qcur", il);119 cb(Kcur, "Kcur", il);120 cb(Qcur, "Vcur", il);121 122 cur = build_attn(inp_attn,123 model.layers[il].wo, NULL, 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 residual = ggml_get_rows(ctx0, residual, inp_out_ids);128 cur = ggml_get_rows(ctx0, cur, inp_out_ids);129 }130 ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);131 cb(ffn_inp, "ffn_inp", il);132 133 // feed-forward network134 {135 cur = build_norm(ffn_inp,136 model.layers[il].ffn_norm, NULL,137 LLM_NORM_RMS, il);138 cb(cur, "ffn_norm", il);139 140 cur = build_ffn(cur,141 model.layers[il].ffn_up, NULL, NULL,142 model.layers[il].ffn_gate, NULL, NULL,143 model.layers[il].ffn_down, NULL, NULL,144 NULL,145 LLM_FFN_SILU, LLM_FFN_PAR, il);146 cb(cur, "ffn_out", il);147 }148 cur = ggml_add(ctx0, cur, ffn_inp);149 150 cur = build_cvec(cur, il);151 cb(cur, "l_out", il);152 153 inpL = cur;154 }155 cur = inpL;156 157 // norm158 cur = build_norm(cur,159 model.output_norm, NULL,160 LLM_NORM_RMS, -1);161 162 cb(cur, "result_norm", -1);163 res->t_embd = cur;164 165 cur = build_lora_mm(model.output, cur, model.output_s);166 167 cb(cur, "result_output", -1);168 res->t_logits = cur;169 170 ggml_build_forward_expand(gf, cur);171}172 