Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_stablelm::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5 6 switch (hparams.n_layer()) {7 case 24: type = LLM_TYPE_1B; break;8 case 32: type = LLM_TYPE_3B; break;9 case 40: type = LLM_TYPE_12B; break;10 default: type = LLM_TYPE_UNKNOWN;11 }12}13 14void llama_model_stablelm::load_arch_tensors(llama_model_loader &) {15 LLAMA_LOAD_LOCALS;16 17 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);18 19 // output20 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);21 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);22 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);23 24 for (int i = 0; i < n_layer; ++i) {25 auto & layer = layers[i];26 27 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);28 layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);29 30 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);31 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);32 33 // optional q and k layernorms, present in StableLM 2 12B34 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);35 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);36 37 // optional FFN norm, not present in StableLM 2 12B which uses parallel residual38 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);39 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);40 41 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);42 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);43 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);44 }45}46 47std::unique_ptr<llm_graph_context> llama_model_stablelm::build_arch_graph(const llm_graph_params & params) const {48 return std::make_unique<graph>(*this, params);49}50 51llama_model_stablelm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {52 const int64_t n_embd_head = hparams.n_embd_head_v();53 54 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());55 56 ggml_tensor * cur;57 ggml_tensor * inpL;58 59 inpL = build_inp_embd(model.tok_embd);60 61 // inp_pos - contains the positions62 ggml_tensor * inp_pos = build_inp_pos();63 64 auto * inp_attn = build_attn_inp_kv();65 66 ggml_tensor * inp_out_ids = build_inp_out_ids();67 68 for (int il = 0; il < n_layer; ++il) {69 // norm70 cur = build_norm(inpL,71 model.layers[il].attn_norm,72 model.layers[il].attn_norm_b,73 LLM_NORM, il);74 cb(cur, "attn_norm", il);75 76 ggml_tensor * inpSA = cur;77 78 // self-attention79 {80 // compute Q and K and RoPE them81 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,82 n_embd_head, n_head, n_head_kv, il);83 84 if (model.layers[il].attn_q_norm) {85 Qcur = build_norm(Qcur,86 model.layers[il].attn_q_norm,87 NULL,88 LLM_NORM, il);89 cb(Qcur, "Qcur", il);90 }91 if (model.layers[il].attn_k_norm) {92 Kcur = build_norm(Kcur,93 model.layers[il].attn_k_norm,94 NULL,95 LLM_NORM, il);96 cb(Kcur, "Kcur", il);97 }98 99 Qcur = ggml_rope_ext(100 ctx0, Qcur, inp_pos, nullptr,101 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,102 ext_factor, attn_factor, beta_fast, beta_slow103 );104 105 Kcur = ggml_rope_ext(106 ctx0, Kcur, inp_pos, nullptr,107 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,108 ext_factor, attn_factor, beta_fast, beta_slow109 );110 111 cb(Qcur, "Qcur", il);112 cb(Kcur, "Kcur", il);113 cb(Vcur, "Vcur", il);114 115 cur = build_attn(inp_attn,116 model.layers[il].wo, NULL, model.layers[il].wo_s,117 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);118 }119 if (il == n_layer - 1 && inp_out_ids) {120 cur = ggml_get_rows(ctx0, cur, inp_out_ids);121 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);122 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);123 }124 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);125 cb(ffn_inp, "ffn_inp", il);126 127 // feed-forward network128 {129 if (model.layers[il].ffn_norm) {130 cur = build_norm(ffn_inp,131 model.layers[il].ffn_norm,132 model.layers[il].ffn_norm_b,133 LLM_NORM, il);134 cb(cur, "ffn_norm", il);135 } else {136 // parallel residual137 cur = inpSA;138 }139 cur = build_ffn(cur,140 model.layers[il].ffn_up, NULL, NULL,141 model.layers[il].ffn_gate, NULL, NULL,142 model.layers[il].ffn_down, NULL, NULL,143 NULL,144 LLM_FFN_SILU, LLM_FFN_PAR, il);145 cb(cur, "ffn_out", il);146 }147 cur = ggml_add(ctx0, cur, ffn_inp);148 149 cur = build_cvec(cur, il);150 cb(cur, "l_out", il);151 152 // input for next layer153 inpL = cur;154 }155 cur = inpL;156 157 cur = build_norm(cur,158 model.output_norm,159 model.output_norm_b,160 LLM_NORM, -1);161 162 cb(cur, "result_norm", -1);163 res->t_embd = cur;164 165 // lm_head166 cur = build_lora_mm(model.output, cur, model.output_s);167 168 cb(cur, "result_output", -1);169 res->t_logits = cur;170 171 ggml_build_forward_expand(gf, cur);172}173 