Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_phi3::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 24: type = LLM_TYPE_1B; break;8 case 32: type = LLM_TYPE_3B; break;9 case 40: type = LLM_TYPE_14B; break;10 default: type = LLM_TYPE_UNKNOWN;11 }12 13 const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);14 15 if (found_swa && hparams.n_swa > 0) {16 LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",17 __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");18 19 // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`20 hparams.swa_type = LLAMA_SWA_TYPE_NONE;21 22 hparams.n_swa = 0;23 hparams.set_swa_pattern(1);24 }25}26 27void llama_model_phi3::load_arch_tensors(llama_model_loader &) {28 LLAMA_LOAD_LOCALS;29 30 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);31 32 // output33 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);34 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);35 36 // if output is NULL, init from the input tok embed37 if (output == NULL) {38 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);39 }40 41 for (int i = 0; i < n_layer; ++i) {42 auto & layer = layers[i];43 44 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);45 46 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, TENSOR_NOT_REQUIRED);47 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);48 49 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);50 51 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);52 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);53 54 layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));55 layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));56 }57}58 59std::unique_ptr<llm_graph_context> llama_model_phi3::build_arch_graph(const llm_graph_params & params) const {60 if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {61 return std::make_unique<graph<true>> (*this, params);62 } else {63 return std::make_unique<graph<false>>(*this, params);64 }65}66 67template<bool iswa>68llama_model_phi3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {69 const int64_t n_embd_head = hparams.n_embd_head_v();70 71 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());72 73 ggml_tensor * cur;74 ggml_tensor * inpL;75 76 inpL = build_inp_embd(model.tok_embd);77 78 // inp_pos - contains the positions79 ggml_tensor * inp_pos = build_inp_pos();80 81 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;82 inp_attn_type * inp_attn = nullptr;83 84 if constexpr (iswa) {85 inp_attn = build_attn_inp_kv_iswa();86 } else {87 inp_attn = build_attn_inp_kv();88 }89 ggml_tensor * inp_out_ids = build_inp_out_ids();90 91 for (int il = 0; il < n_layer; ++il) {92 auto * residual = inpL;93 94 // self-attention95 {96 // rope freq factors for 128k context97 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);98 99 ggml_tensor* attn_norm_output = build_norm(inpL,100 model.layers[il].attn_norm,101 model.layers[il].attn_norm_b,102 LLM_NORM_RMS, il);103 cb(attn_norm_output, "attn_norm", il);104 105 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], attn_norm_output,106 n_embd_head, n_head, n_head_kv, il);107 Qcur = ggml_rope_ext(108 ctx0, Qcur, inp_pos, rope_factors,109 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,110 ext_factor, attn_factor, beta_fast, beta_slow111 );112 113 Kcur = ggml_rope_ext(114 ctx0, Kcur, inp_pos, rope_factors,115 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,116 ext_factor, attn_factor, beta_fast, beta_slow117 );118 119 cb(Qcur, "Qcur", il);120 cb(Kcur, "Kcur", il);121 cb(Vcur, "Vcur", il);122 123 Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));124 cb(Qcur, "Qcur", il);125 126 cur = build_attn(inp_attn,127 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,128 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);129 }130 if (il == n_layer - 1 && inp_out_ids) {131 cur = ggml_get_rows(ctx0, cur, inp_out_ids);132 residual = ggml_get_rows(ctx0, residual, inp_out_ids);133 }134 cur = ggml_add(ctx0, cur, residual);135 residual = cur;136 137 cur = build_norm(cur,138 model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,139 LLM_NORM_RMS, il);140 cb(cur, "ffn_norm", il);141 142 // feed-forward network143 if (model.layers[il].ffn_gate_inp == nullptr) {144 cur = build_ffn(cur,145 model.layers[il].ffn_up, NULL, NULL,146 NULL, NULL, NULL,147 model.layers[il].ffn_down, NULL, NULL,148 NULL,149 LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);150 cb(cur, "ffn_out", il);151 } else {152 // MoE branch153 cur = build_moe_ffn(cur,154 model.layers[il].ffn_gate_inp,155 model.layers[il].ffn_up_exps,156 model.layers[il].ffn_gate_exps,157 model.layers[il].ffn_down_exps,158 nullptr,159 n_expert, n_expert_used,160 LLM_FFN_SILU, true,161 hparams.expert_weights_scale,162 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,163 il);164 cb(cur, "ffn_moe_out", il);165 }166 cur = ggml_add(ctx0, residual, cur);167 168 cur = build_cvec(cur, il);169 cb(cur, "l_out", il);170 171 // input for next layer172 inpL = cur;173 }174 cur = build_norm(inpL,175 model.output_norm,176 model.output_norm_b,177 LLM_NORM_RMS, -1);178 179 cb(cur, "result_norm", -1);180 res->t_embd = cur;181 182 cur = build_lora_mm(model.output, cur, model.output_s);183 184 if (model.output_b != nullptr) {185 cb(cur, "result_output_no_bias", -1);186 cur = ggml_add(ctx0, cur, model.output_b);187 }188 cb(cur, "result_output", -1);189 res->t_logits = cur;190 191 ggml_build_forward_expand(gf, cur);192}193 194// Explicit template instantiations195template struct llama_model_phi3::graph<false>;196template struct llama_model_phi3::graph<true>;197 