Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_mistral3::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_SCALE, hparams.f_attn_temp_scale, false);6 7 ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST, hparams.yarn_beta_fast, false);8 ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW, hparams.yarn_beta_slow, false);9 ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false);10 11 hparams.f_attn_temp_offset = 0.0f;12 13 // TODO: maybe add n_attn_temp_floor_scale as a separate KV?14 if (hparams.f_attn_temp_scale != 0.0f) {15 hparams.n_attn_temp_floor_scale = hparams.n_ctx_orig_yarn;16 if (hparams.n_attn_temp_floor_scale == 0) {17 throw std::runtime_error("invalid n_ctx_orig_yarn for attention temperature scaling");18 }19 }20 21 switch (hparams.n_layer()) {22 case 26: type = LLM_TYPE_3B; break;23 case 34: type = LLM_TYPE_8B; break;24 case 40: type = LLM_TYPE_14B; break;25 default: type = LLM_TYPE_UNKNOWN;26 }27}28 29void llama_model_mistral3::load_arch_tensors(llama_model_loader &) {30 LLAMA_LOAD_LOCALS;31 32 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);33 34 // output35 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);36 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);37 38 // if output is NULL, init from the input tok embed39 if (output == NULL) {40 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);41 }42 43 for (int i = 0; i < n_layer; ++i) {44 auto & layer = layers[i];45 46 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);47 48 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);49 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);50 51 // optional bias tensors52 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);53 54 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);55 56 if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {57 layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));58 layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));59 }60 else {61 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));62 }63 64 if (n_expert == 0) {65 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);66 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);67 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);68 69 // optional MLP bias70 layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);71 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);72 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);73 } else {74 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);75 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);76 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);77 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);78 79 // For Granite MoE Shared80 if (hparams.n_ff_shexp > 0) {81 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);82 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);83 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);84 }85 }86 }87}88 89std::unique_ptr<llm_graph_context> llama_model_mistral3::build_arch_graph(const llm_graph_params & params) const {90 return std::make_unique<graph>(*this, params);91}92 93llama_model_mistral3::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {94 const int64_t n_embd_head = hparams.n_embd_head_v();95 96 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());97 GGML_ASSERT(n_embd_head == n_rot);98 99 ggml_tensor * cur;100 ggml_tensor * inpL;101 102 inpL = build_inp_embd(model.tok_embd);103 104 // inp_pos - contains the positions105 ggml_tensor * inp_pos = build_inp_pos();106 107 // (optional) temperature tuning108 ggml_tensor * inp_attn_scale = nullptr;109 if (hparams.f_attn_temp_scale != 0.0f) {110 inp_attn_scale = build_inp_attn_scale();111 }112 113 auto * inp_attn = build_attn_inp_kv();114 115 const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;116 117 ggml_tensor * inp_out_ids = build_inp_out_ids();118 119 for (int il = 0; il < n_layer; ++il) {120 ggml_tensor * inpSA = inpL;121 122 // norm123 cur = build_norm(inpL,124 model.layers[il].attn_norm, NULL,125 LLM_NORM_RMS, il);126 cb(cur, "attn_norm", il);127 128 // self-attention129 {130 // rope freq factors for llama3; may return nullptr for llama2 and other models131 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);132 133 // compute Q and K and RoPE them134 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,135 n_embd_head, n_head, n_head_kv, il);136 137 Qcur = ggml_rope_ext(138 ctx0, Qcur, inp_pos, rope_factors,139 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,140 ext_factor, attn_factor, beta_fast, beta_slow141 );142 143 Kcur = ggml_rope_ext(144 ctx0, Kcur, inp_pos, rope_factors,145 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,146 ext_factor, attn_factor, beta_fast, beta_slow147 );148 149 cb(Qcur, "Qcur", il);150 cb(Kcur, "Kcur", il);151 cb(Vcur, "Vcur", il);152 153 if (inp_attn_scale) {154 // apply llama 4 temperature scaling155 Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);156 cb(Qcur, "Qcur_attn_temp_scaled", il);157 }158 159 cur = build_attn(inp_attn,160 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,161 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);162 cb(cur, "attn_out", il);163 }164 if (il == n_layer - 1 && inp_out_ids) {165 cur = ggml_get_rows(ctx0, cur, inp_out_ids);166 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);167 }168 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);169 cb(ffn_inp, "ffn_inp", il);170 171 // feed-forward network (non-MoE)172 if (model.layers[il].ffn_gate_inp == nullptr) {173 174 cur = build_norm(ffn_inp,175 model.layers[il].ffn_norm, NULL,176 LLM_NORM_RMS, il);177 cb(cur, "ffn_norm", il);178 179 cur = build_ffn(cur,180 model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,181 model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s,182 model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,183 NULL,184 LLM_FFN_SILU, LLM_FFN_PAR, il);185 cb(cur, "ffn_out", il);186 } else {187 // MoE branch188 cur = build_norm(ffn_inp,189 model.layers[il].ffn_norm, NULL,190 LLM_NORM_RMS, il);191 cb(cur, "ffn_norm", il);192 193 cur = build_moe_ffn(cur,194 model.layers[il].ffn_gate_inp,195 model.layers[il].ffn_up_exps,196 model.layers[il].ffn_gate_exps,197 model.layers[il].ffn_down_exps,198 nullptr,199 n_expert, n_expert_used,200 LLM_FFN_SILU, true,201 hparams.expert_weights_scale,202 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,203 il,204 nullptr, nullptr,205 model.layers[il].ffn_up_exps_s,206 model.layers[il].ffn_gate_exps_s,207 model.layers[il].ffn_down_exps_s);208 cb(cur, "ffn_moe_out", il);209 }210 cur = ggml_add(ctx0, cur, ffn_inp);211 cb(cur, "ffn_out", il);212 213 cur = build_cvec(cur, il);214 cb(cur, "l_out", il);215 216 // input for next layer217 inpL = cur;218 }219 cur = inpL;220 221 cur = build_norm(cur,222 model.output_norm, NULL,223 LLM_NORM_RMS, -1);224 225 cb(cur, "result_norm", -1);226 res->t_embd = cur;227 228 // lm_head229 cur = build_lora_mm(model.output, cur, model.output_s);230 231 cb(cur, "result_output", -1);232 res->t_logits = cur;233 234 ggml_build_forward_expand(gf, cur);235}236 