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_olmo2::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6 const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);7 if (found_swa && hparams.n_swa > 0) {8 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;9 uint32_t swa_period = 4;10 ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);11 hparams.set_swa_pattern(swa_period);12 13 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;14 hparams.rope_freq_scale_train_swa = 1.0; // See olmo2.cpp15 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);16 } else {17 hparams.swa_type = LLAMA_SWA_TYPE_NONE;18 }19 20 switch (hparams.n_layer()) {21 case 16: type = LLM_TYPE_1B; break;22 case 32: type = LLM_TYPE_7B; break;23 case 40: type = LLM_TYPE_13B; break;24 case 64: type = LLM_TYPE_32B; break;25 default: type = LLM_TYPE_UNKNOWN;26 }27}28 29void llama_model_olmo2::load_arch_tensors(llama_model_loader &) {30 LLAMA_LOAD_LOCALS;31 32 const int64_t n_embd_head = n_embd / n_head;33 34 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);35 36 // output37 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);38 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);39 40 for (int i = 0; i < n_layer; ++i) {41 auto & layer = layers[i];42 43 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);44 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);45 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);46 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);47 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);48 49 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);50 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);51 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);52 layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);53 }54}55 56std::unique_ptr<llm_graph_context> llama_model_olmo2::build_arch_graph(const llm_graph_params & params) const {57 if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {58 return std::make_unique<graph<true>>(*this, params);59 } else {60 return std::make_unique<graph<false>>(*this, params);61 }62}63 64template <bool iswa>65llama_model_olmo2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {66 const int64_t n_embd_head = hparams.n_embd_head_v();67 68 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());69 GGML_ASSERT(n_embd_head == n_rot);70 71 ggml_tensor * cur;72 ggml_tensor * inpL;73 74 inpL = build_inp_embd(model.tok_embd);75 76 // inp_pos - contains the positions77 ggml_tensor * inp_pos = build_inp_pos();78 79 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;80 inp_attn_type * inp_attn = nullptr;81 82 if constexpr (iswa) {83 inp_attn = build_attn_inp_kv_iswa();84 } else {85 inp_attn = build_attn_inp_kv();86 }87 ggml_tensor * inp_out_ids = build_inp_out_ids();88 89 for (int il = 0; il < n_layer; ++il) {90 ggml_tensor * inpSA = inpL;91 92 cur = inpL;93 94 // self_attention95 {96 // compute Q and K and RoPE them97 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);98 cb(Qcur, "Qcur", il);99 100 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);101 cb(Kcur, "Kcur", il);102 103 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);104 cb(Vcur, "Vcur", il);105 106 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,107 LLM_NORM_RMS, il);108 cb(Qcur, "Qcur_normed", il);109 110 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,111 LLM_NORM_RMS, il);112 cb(Kcur, "Kcur_normed", il);113 114 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);115 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);116 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);117 118 const bool is_swa = hparams.is_swa(il);119 120 if (is_swa) {121 // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.122 // This is achieved here by setting freq_scale and attn_factor to 1.123 // We also set ext_factor to 0 to avoid a few unnecessary computations.124 Qcur = ggml_rope_ext(125 ctx0, Qcur, inp_pos, nullptr,126 n_rot, rope_type, n_ctx_orig, freq_base, 1.0,127 0.0, 1.0, beta_fast, beta_slow128 );129 130 Kcur = ggml_rope_ext(131 ctx0, Kcur, inp_pos, nullptr,132 n_rot, rope_type, n_ctx_orig, freq_base, 1.0,133 0.0, 1.0, beta_fast, beta_slow134 );135 } else {136 Qcur = ggml_rope_ext(137 ctx0, Qcur, inp_pos, nullptr,138 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,139 ext_factor, attn_factor, beta_fast, beta_slow140 );141 142 Kcur = ggml_rope_ext(143 ctx0, Kcur, inp_pos, nullptr,144 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,145 ext_factor, attn_factor, beta_fast, beta_slow146 );147 }148 cb(Qcur, "Qcur", il);149 cb(Kcur, "Kcur", il);150 cb(Vcur, "Vcur", il);151 152 cur = build_attn(inp_attn,153 model.layers[il].wo, NULL, model.layers[il].wo_s,154 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);155 }156 if (il == n_layer - 1 && inp_out_ids) {157 cur = ggml_get_rows(ctx0, cur, inp_out_ids);158 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);159 }160 cur = build_norm(cur,161 model.layers[il].attn_post_norm, NULL,162 LLM_NORM_RMS, il);163 cb(cur, "attn_post_norm", il);164 165 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);166 cb(ffn_inp, "ffn_inp", il);167 168 // feed-forward network169 cur = build_ffn(ffn_inp,170 model.layers[il].ffn_up, NULL, NULL,171 model.layers[il].ffn_gate, NULL, NULL,172 model.layers[il].ffn_down, NULL, NULL,173 NULL,174 LLM_FFN_SILU, LLM_FFN_PAR, il);175 cb(cur, "ffn_out", il);176 177 cur = build_norm(cur,178 model.layers[il].ffn_post_norm, NULL,179 LLM_NORM_RMS, -1);180 cb(cur, "ffn_post_norm", -1);181 182 cur = ggml_add(ctx0, cur, ffn_inp);183 cb(cur, "ffn_out", il);184 185 cur = build_cvec(cur, il);186 cb(cur, "l_out", il);187 188 // input for next layer189 inpL = cur;190 }191 cur = inpL;192 193 cur = build_norm(cur,194 model.output_norm, NULL,195 LLM_NORM_RMS, -1);196 197 cb(cur, "result_norm", -1);198 res->t_embd = cur;199 200 // lm_head201 cur = build_lora_mm(model.output, cur, model.output_s);202 203 cb(cur, "result_output", -1);204 res->t_logits = cur;205 206 ggml_build_forward_expand(gf, cur);207}208 209// Explicit template instantiations210template struct llama_model_olmo2::graph<false>;211template struct llama_model_olmo2::graph<true>;212 