Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03k
1#include "models.h"2 3void llama_model_gemma3::load_arch_hparams(llama_model_loader & ml) {4 const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);5 if (found_swa && hparams.n_swa > 0) {6 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;7 uint32_t swa_period = 6;8 ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);9 hparams.set_swa_pattern(swa_period);10 11 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);12 } else {13 hparams.swa_type = LLAMA_SWA_TYPE_NONE;14 }15 16 hparams.f_final_logit_softcapping = 0.0f;17 ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);18 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);19 20 switch (hparams.n_layer()) {21 case 18: type = LLM_TYPE_270M; break;22 case 26: type = LLM_TYPE_1B; break;23 case 32: type = LLM_TYPE_8B; break; // Rnj-124 case 34: type = LLM_TYPE_4B; break;25 case 48: type = LLM_TYPE_12B; break;26 case 62: type = LLM_TYPE_27B; break;27 default: type = LLM_TYPE_UNKNOWN;28 }29 30 // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L28931 hparams.f_attention_scale = type == LLM_TYPE_27B32 ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))33 : 1.0f / std::sqrt(float(hparams.n_embd_head_k()));34}35 36void llama_model_gemma3::load_arch_tensors(llama_model_loader &) {37 LLAMA_LOAD_LOCALS;38 39 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);40 41 // output42 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);43 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);44 45 // if output is NULL, init from the input tok embed46 if (output == NULL) {47 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);48 }49 50 // Dense linear weights51 dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.dense_2_feat_out}, TENSOR_NOT_REQUIRED);52 dense_3_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_3_OUT, "weight"), {hparams.dense_3_feat_in, n_embd}, TENSOR_NOT_REQUIRED);53 54 55 for (int i = 0; i < n_layer; ++i) {56 auto & layer = layers[i];57 58 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);59 60 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);61 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);62 63 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);64 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);65 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);66 67 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);68 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);69 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);70 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);71 layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);72 }73}74 75std::unique_ptr<llm_graph_context> llama_model_gemma3::build_arch_graph(const llm_graph_params & params) const {76 if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {77 return std::make_unique<graph<true>>(*this, params);78 } else {79 return std::make_unique<graph<false>>(*this, params);80 }81}82 83template <bool iswa>84llama_model_gemma3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {85 const int64_t n_embd_head = hparams.n_embd_head_k();86 87 ggml_tensor * cur;88 ggml_tensor * inpL;89 90 inpL = build_inp_embd(model.tok_embd);91 92 // important: do not normalize weights for raw embeddings input (i.e. encoded image embeddings)93 inpL = ggml_scale(ctx0, inpL, ubatch.token ? sqrtf(n_embd) : 1.0f);94 cb(inpL, "inp_scaled", -1);95 96 // inp_pos - contains the positions97 ggml_tensor * inp_pos = build_inp_pos();98 99 // TODO: is causal == true correct? might need some changes100 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;101 inp_attn_type * inp_attn = nullptr;102 103 if constexpr (iswa) {104 inp_attn = build_attn_inp_kv_iswa();105 } else {106 inp_attn = build_attn_inp_kv();107 }108 109 ggml_tensor * inp_out_ids = build_inp_out_ids();110 111 for (int il = 0; il < n_layer; ++il) {112 float freq_base_l = 0.0f;113 float freq_scale_l = 0.0f;114 115 if constexpr (iswa) {116 freq_base_l = model.get_rope_freq_base (cparams, il);117 freq_scale_l = model.get_rope_freq_scale(cparams, il);118 } else {119 freq_base_l = freq_base;120 freq_scale_l = freq_scale;121 }122 123 // norm124 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);125 cb(cur, "attn_norm", il);126 127 // self-attention128 {129 // compute Q and K and RoPE them130 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,131 n_embd_head, n_head, n_head_kv, il);132 133 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);134 cb(Qcur, "Qcur_normed", il);135 136 Qcur = ggml_rope_ext(137 ctx0, Qcur, inp_pos, nullptr,138 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,139 ext_factor, attn_factor, beta_fast, beta_slow);140 141 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);142 cb(Kcur, "Kcur_normed", il);143 144 Kcur = ggml_rope_ext(145 ctx0, Kcur, inp_pos, nullptr,146 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,147 ext_factor, attn_factor, beta_fast, beta_slow);148 149 cb(Qcur, "Qcur", il);150 cb(Kcur, "Kcur", il);151 cb(Vcur, "Vcur", il);152 153 // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315154 Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);155 156 cur = build_attn(inp_attn,157 model.layers[il].wo, NULL, model.layers[il].wo_s,158 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);159 }160 if (il == n_layer - 1 && inp_out_ids) {161 cur = ggml_get_rows(ctx0, cur, inp_out_ids);162 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);163 }164 cur = build_norm(cur,165 model.layers[il].attn_post_norm, NULL,166 LLM_NORM_RMS, il);167 cb(cur, "attn_post_norm", il);168 169 ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);170 cb(sa_out, "sa_out", il);171 172 cur = build_norm(sa_out,173 model.layers[il].ffn_norm, NULL,174 LLM_NORM_RMS, il);175 cb(cur, "ffn_norm", il);176 177 // feed-forward network178 {179 cur = build_ffn(cur,180 model.layers[il].ffn_up, NULL, NULL,181 model.layers[il].ffn_gate, NULL, NULL,182 model.layers[il].ffn_down, NULL, NULL,183 NULL,184 LLM_FFN_GELU, LLM_FFN_PAR, il);185 cb(cur, "ffn_out", il);186 }187 cur = build_norm(cur,188 model.layers[il].ffn_post_norm, NULL,189 LLM_NORM_RMS, -1);190 cb(cur, "ffn_post_norm", il);191 192 cur = ggml_add(ctx0, cur, sa_out);193 194 cur = build_cvec(cur, il);195 cb(cur, "l_out", il);196 197 // input for next layer198 inpL = cur;199 }200 cur = inpL;201 202 cur = build_norm(cur,203 model.output_norm, NULL,204 LLM_NORM_RMS, -1);205 206 cb(cur, "result_norm", -1);207 res->t_embd = cur;208 209 // lm_head210 cur = build_lora_mm(model.output, cur, model.output_s);211 212 if (hparams.f_final_logit_softcapping) {213 cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);214 cur = ggml_tanh(ctx0, cur);215 cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);216 }217 218 cb(cur, "result_output", -1);219 res->t_logits = cur;220 221 ggml_build_forward_expand(gf, cur);222}223 224template struct llama_model_gemma3::graph<false>;225template struct llama_model_gemma3::graph<true>;226 