Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_mpt::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5 ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);6 ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false);7 8 switch (hparams.n_layer()) {9 case 32: type = LLM_TYPE_7B; break;10 case 48: type = LLM_TYPE_30B; break;11 default: type = LLM_TYPE_UNKNOWN;12 }13}14 15void llama_model_mpt::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 pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);20 21 // output22 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);23 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);24 25 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);26 if (!output) {27 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU28 }29 30 for (int i = 0; i < n_layer; ++i) {31 auto & layer = layers[i];32 33 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);34 layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);35 36 layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);37 layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);38 39 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);40 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);41 42 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);43 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);44 45 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);46 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);47 48 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);49 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);50 51 // FIXME test-llama-archs crashes if q_norm is created52 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL);53 layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL);54 55 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);56 layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);57 58 // AWQ ScaleActivation layer59 layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);60 }61}62 63std::unique_ptr<llm_graph_context> llama_model_mpt::build_arch_graph(const llm_graph_params & params) const {64 return std::make_unique<graph>(*this, params);65}66 67llama_model_mpt::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {68 const int64_t n_embd_head = hparams.n_embd_head_v();69 70 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());71 72 ggml_tensor * cur;73 ggml_tensor * pos;74 ggml_tensor * inpL;75 76 inpL = build_inp_embd(model.tok_embd);77 78 auto * inp_attn = build_attn_inp_kv();79 80 if (model.pos_embd) {81 // inp_pos - contains the positions82 ggml_tensor * inp_pos = build_inp_pos();83 pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);84 cb(pos, "pos_embd", -1);85 86 inpL = ggml_add(ctx0, inpL, pos);87 cb(inpL, "inpL", -1);88 }89 90 ggml_tensor * inp_out_ids = build_inp_out_ids();91 92 for (int il = 0; il < n_layer; ++il) {93 ggml_tensor * attn_norm;94 95 attn_norm = build_norm(inpL, model.layers[il].attn_norm, model.layers[il].attn_norm_b, LLM_NORM, il);96 cb(attn_norm, "attn_norm", il);97 98 // self-attention99 {100 cur = attn_norm;101 102 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,103 n_embd_head, n_head, n_head_kv, il);104 105 // Q/K Layernorm106 if (model.layers[il].attn_q_norm) {107 Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head * n_head, n_tokens);108 Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head * n_head_kv, n_tokens);109 110 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, model.layers[il].attn_q_norm_b, LLM_NORM, il);111 112 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, model.layers[il].attn_k_norm_b, LLM_NORM, il);113 114 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);115 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);116 }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 127 if (il == n_layer - 1 && inp_out_ids) {128 cur = ggml_get_rows(ctx0, cur, inp_out_ids);129 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);130 }131 132 // Add the input133 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);134 cb(ffn_inp, "ffn_inp", il);135 136 // feed forward137 {138 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, model.layers[il].ffn_norm_b, LLM_NORM, il);139 cb(cur, "ffn_norm", il);140 cur = build_ffn(cur,141 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,142 NULL, NULL, NULL,143 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,144 model.layers[il].ffn_act, LLM_FFN_GELU, LLM_FFN_SEQ, il);145 cb(cur, "ffn_out", il);146 }147 148 cur = ggml_add(ctx0, cur, ffn_inp);149 150 cur = build_cvec(cur, il);151 cb(cur, "l_out", il);152 153 // input for next layer154 inpL = cur;155 }156 157 cur = inpL;158 159 cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);160 161 cb(cur, "result_norm", -1);162 res->t_embd = cur;163 164 cur = build_lora_mm(model.output, cur, model.output_s);165 166 cb(cur, "result_output", -1);167 res->t_logits = cur;168 169 ggml_build_forward_expand(gf, cur);170}171 