Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_phi2::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 default: type = LLM_TYPE_UNKNOWN;10 }11}12 13void llama_model_phi2::load_arch_tensors(llama_model_loader &) {14 LLAMA_LOAD_LOCALS;15 16 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);17 18 // output19 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);20 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);21 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);22 output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {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 32 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);33 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);34 35 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);36 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);37 38 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);39 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);40 }41}42 43std::unique_ptr<llm_graph_context> llama_model_phi2::build_arch_graph(const llm_graph_params & params) const {44 return std::make_unique<graph>(*this, params);45}46 47llama_model_phi2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {48 const int64_t n_embd_head = hparams.n_embd_head_v();49 50 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());51 52 ggml_tensor * cur;53 ggml_tensor * attn_norm_output;54 ggml_tensor * ffn_output;55 ggml_tensor * inpL;56 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 attn_norm_output = build_norm(inpL,68 model.layers[il].attn_norm,69 model.layers[il].attn_norm_b,70 LLM_NORM, il);71 cb(attn_norm_output, "attn_norm", il);72 73 // self-attention74 {75 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], attn_norm_output,76 n_embd_head, n_head, n_head_kv, il);77 Qcur = ggml_rope_ext(78 ctx0, Qcur, inp_pos, nullptr,79 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,80 ext_factor, attn_factor, beta_fast, beta_slow81 );82 83 Kcur = ggml_rope_ext(84 ctx0, Kcur, inp_pos, nullptr,85 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,86 ext_factor, attn_factor, beta_fast, beta_slow87 );88 89 cb(Qcur, "Qcur", il);90 cb(Kcur, "Kcur", il);91 cb(Vcur, "Vcur", il);92 93 // with phi2, we scale the Q to avoid precision issues94 // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L6695 Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));96 97 cur = build_attn(inp_attn,98 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,99 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);100 }101 if (il == n_layer - 1 && inp_out_ids) {102 cur = ggml_get_rows(ctx0, cur, inp_out_ids);103 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);104 attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);105 }106 // FF107 {108 ffn_output = build_ffn(attn_norm_output,109 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,110 NULL, NULL, NULL,111 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,112 NULL,113 LLM_FFN_GELU, LLM_FFN_SEQ, il);114 cb(ffn_output, "ffn_out", il);115 }116 cur = ggml_add(ctx0, cur, ffn_output);117 cur = ggml_add(ctx0, cur, inpL);118 119 cur = build_cvec(cur, il);120 cb(cur, "l_out", il);121 122 // input for next layer123 inpL = cur;124 }125 cur = build_norm(inpL,126 model.output_norm,127 model.output_norm_b,128 LLM_NORM, -1);129 130 cb(cur, "result_norm", -1);131 res->t_embd = cur;132 133 cur = build_lora_mm(model.output, cur, model.output_s);134 cb(cur, "result_output_no_bias", -1);135 136 cur = ggml_add(ctx0, cur, model.output_b);137 138 cb(cur, "result_output", -1);139 res->t_logits = cur;140 141 ggml_build_forward_expand(gf, cur);142}143 