Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_llama::load_arch_hparams(llama_model_loader & ml) {4 uint32_t n_vocab = 0;5 ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);6 7 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);8 9 if (hparams.n_expert == 8) {10 switch (hparams.n_layer()) {11 case 32: type = LLM_TYPE_8x7B; break;12 case 56: type = LLM_TYPE_8x22B; break;13 default: type = LLM_TYPE_UNKNOWN;14 }15 } else {16 switch (hparams.n_layer()) {17 case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B18 case 22: type = LLM_TYPE_1B; break;19 case 26: type = LLM_TYPE_3B; break;20 case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B21 case 30: type = LLM_TYPE_256M; break; // smoldocling 256M22 // granite uses a vocab with len 4915223 case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;24 case 36: type = LLM_TYPE_8B; break; // granite25 case 40: type = LLM_TYPE_13B; break;26 case 48: type = LLM_TYPE_34B; break;27 case 60: type = LLM_TYPE_30B; break;28 case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;29 default: type = LLM_TYPE_UNKNOWN;30 }31 }32}33 34void llama_model_llama::load_arch_tensors(llama_model_loader &) {35 LLAMA_LOAD_LOCALS;36 37 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);38 39 // output40 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);41 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);42 43 // if output is NULL, init from the input tok embed44 if (output == NULL) {45 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);46 }47 48 for (int i = 0; i < n_layer; ++i) {49 auto & layer = layers[i];50 51 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);52 53 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);54 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);55 56 // optional bias tensors57 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);58 59 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);60 61 if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {62 layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));63 layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));64 }65 else {66 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));67 }68 69 if (n_expert == 0) {70 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);71 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);72 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);73 74 // optional MLP bias75 layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);76 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);77 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);78 } else {79 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);80 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);81 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);82 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);83 84 // For Granite MoE Shared85 if (hparams.n_ff_shexp > 0) {86 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);87 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);88 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);89 }90 }91 }92}93 94std::unique_ptr<llm_graph_context> llama_model_llama::build_arch_graph(const llm_graph_params & params) const {95 return std::make_unique<graph<false>>(*this, params);96}97 98template <bool embed>99llama_model_llama::graph<embed>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {100 const int64_t n_embd_head = hparams.n_embd_head_v();101 102 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());103 GGML_ASSERT(n_embd_head == n_rot);104 105 ggml_tensor * cur;106 ggml_tensor * inpL;107 108 inpL = build_inp_embd(model.tok_embd);109 110 // inp_pos - contains the positions111 ggml_tensor * inp_pos = build_inp_pos();112 113 using inp_attn_type = std::conditional_t<embed, llm_graph_input_attn_no_cache, llm_graph_input_attn_kv>;114 115 inp_attn_type * inp_attn = nullptr;116 if constexpr (embed) {117 inp_attn = build_attn_inp_no_cache();118 } else {119 inp_attn = build_attn_inp_kv();120 }121 122 const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;123 124 ggml_tensor * inp_out_ids = build_inp_out_ids();125 126 for (int il = 0; il < n_layer; ++il) {127 res->t_layer_inp[il] = inpL;128 129 ggml_tensor * inpSA = inpL;130 131 // norm132 cur = build_norm(inpL,133 model.layers[il].attn_norm, NULL,134 LLM_NORM_RMS, il);135 cb(cur, "attn_norm", il);136 137 // self-attention138 {139 // rope freq factors for llama3; may return nullptr for llama2 and other models140 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);141 142 // compute Q and K and RoPE them143 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,144 n_embd_head, n_head, n_head_kv, il);145 146 Qcur = ggml_rope_ext(147 ctx0, Qcur, inp_pos, rope_factors,148 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,149 ext_factor, attn_factor, beta_fast, beta_slow150 );151 152 Kcur = ggml_rope_ext(153 ctx0, Kcur, inp_pos, rope_factors,154 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,155 ext_factor, attn_factor, beta_fast, beta_slow156 );157 158 cb(Qcur, "Qcur", il);159 cb(Kcur, "Kcur", il);160 cb(Vcur, "Vcur", il);161 162 if (hparams.use_kq_norm) {163 // Llama4TextL2Norm164 Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);165 Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);166 cb(Qcur, "Qcur_normed", il);167 cb(Kcur, "Kcur_normed", il);168 }169 cur = build_attn(inp_attn,170 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,171 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);172 cb(cur, "attn_out", il);173 }174 if (il == n_layer - 1 && inp_out_ids) {175 cur = ggml_get_rows(ctx0, cur, inp_out_ids);176 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);177 }178 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);179 cb(ffn_inp, "ffn_inp", il);180 181 // feed-forward network (non-MoE)182 if (model.layers[il].ffn_gate_inp == nullptr) {183 184 cur = build_norm(ffn_inp,185 model.layers[il].ffn_norm, NULL,186 LLM_NORM_RMS, il);187 cb(cur, "ffn_norm", il);188 189 cur = build_ffn(cur,190 model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,191 model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,192 model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,193 NULL,194 LLM_FFN_SILU, LLM_FFN_PAR, il);195 cb(cur, "ffn_out", il);196 } else {197 // MoE branch198 cur = build_norm(ffn_inp,199 model.layers[il].ffn_norm, NULL,200 LLM_NORM_RMS, il);201 cb(cur, "ffn_norm", il);202 203 cur = build_moe_ffn(cur,204 model.layers[il].ffn_gate_inp,205 model.layers[il].ffn_up_exps,206 model.layers[il].ffn_gate_exps,207 model.layers[il].ffn_down_exps,208 nullptr,209 n_expert, n_expert_used,210 LLM_FFN_SILU, true,211 hparams.expert_weights_scale,212 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,213 il,214 nullptr, nullptr,215 model.layers[il].ffn_up_exps_s,216 model.layers[il].ffn_gate_exps_s,217 model.layers[il].ffn_down_exps_s);218 cb(cur, "ffn_moe_out", il);219 }220 cur = ggml_add(ctx0, cur, ffn_inp);221 cb(cur, "ffn_out", il);222 223 cur = build_cvec(cur, il);224 cb(cur, "l_out", il);225 226 // input for next layer227 inpL = cur;228 }229 cur = inpL;230 231 cur = build_norm(cur,232 model.output_norm, NULL,233 LLM_NORM_RMS, -1);234 235 cb(cur, "result_norm", -1);236 res->t_embd = cur;237 238 if constexpr (!embed) {239 // lm_head240 cur = build_lora_mm(model.output, cur, model.output_s);241 242 cb(cur, "result_output", -1);243 res->t_logits = cur;244 }245 246 ggml_build_forward_expand(gf, cur);247}248 249template struct llama_model_llama::graph<false>;250template struct llama_model_llama::graph<true>;251 