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.
03.1k
1#include "models.h"2 3void llama_model_smallthinker::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 6 if (found_swa && hparams.n_swa > 0) {7 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;8 hparams.n_swa = 4096;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, true);12 13 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;14 hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;15 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 hparams.n_no_rope_layer_step = hparams.n_layer();19 }20 21 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);22 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);23 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);24 25 switch (hparams.n_layer()) {26 case 32: type = LLM_TYPE_4B; break;27 case 52: type = LLM_TYPE_20B; break;28 default: type = LLM_TYPE_UNKNOWN;29 }30}31 32void llama_model_smallthinker::load_arch_tensors(llama_model_loader &) {33 LLAMA_LOAD_LOCALS;34 35 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);36 37 // output38 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);39 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);40 41 // if output is NULL, init from the input tok embed42 if (output == NULL) {43 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);44 }45 46 for (int i = 0; i < n_layer; ++i) {47 auto & layer = layers[i];48 49 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);50 51 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);52 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);53 54 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);55 56 GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER");57 GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER");58 59 // MoE branch60 const int64_t n_ff_exp = hparams.n_ff_exp;61 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);62 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);63 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);64 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);65 }66}67 68std::unique_ptr<llm_graph_context> llama_model_smallthinker::build_arch_graph(const llm_graph_params & params) const {69 if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {70 return std::make_unique<graph<true>> (*this, params);71 } else {72 return std::make_unique<graph<false>>(*this, params);73 }74}75 76template <bool iswa>77llama_model_smallthinker::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){78 const int64_t n_embd_head = hparams.n_embd_head_v();79 80 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());81 GGML_ASSERT(n_embd_head == n_rot);82 83 ggml_tensor * cur;84 ggml_tensor * inpL;85 86 inpL = build_inp_embd(model.tok_embd);87 88 // inp_pos - contains the positions89 ggml_tensor * inp_pos = build_inp_pos();90 91 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;92 inp_attn_type * inp_attn = nullptr;93 94 if constexpr (iswa) {95 inp_attn = build_attn_inp_kv_iswa();96 } else {97 inp_attn = build_attn_inp_kv();98 }99 ggml_tensor * inp_out_ids = build_inp_out_ids();100 101 for (int il = 0; il < n_layer; ++il) {102 const float freq_base_l = model.get_rope_freq_base (cparams, il);103 const float freq_scale_l = model.get_rope_freq_scale(cparams, il);104 105 ggml_tensor * inpSA = inpL;106 107 // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous108 const bool use_rope = hparams.n_no_rope_layer_step == n_layer ||109 il % hparams.n_no_rope_layer_step != 0;110 111 ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]112 cb(probs, "ffn_moe_logits", il);113 114 // norm115 cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);116 cb(cur, "attn_norm", il);117 118 // self_attention119 {120 // compute Q and K and RoPE them121 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,122 n_embd_head, n_head, n_head_kv, il);123 124 if (use_rope) {125 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,126 ext_factor, attn_factor, beta_fast, beta_slow);127 128 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,129 ext_factor, attn_factor, beta_fast, beta_slow);130 }131 cb(Qcur, "Qcur", il);132 cb(Kcur, "Kcur", il);133 134 cur = build_attn(inp_attn,135 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,136 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);137 }138 if (il == n_layer - 1 && inp_out_ids) {139 cur = ggml_get_rows(ctx0, cur, inp_out_ids);140 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);141 probs = ggml_get_rows(ctx0, probs, inp_out_ids);142 }143 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);144 cb(ffn_inp, "ffn_inp", il);145 146 // MoE branch147 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);148 cb(cur, "ffn_norm", il);149 150 ggml_tensor * ffn_out =151 build_moe_ffn(cur,152 nullptr,153 model.layers[il].ffn_up_exps,154 model.layers[il].ffn_gate_exps,155 model.layers[il].ffn_down_exps,156 nullptr,157 n_expert, n_expert_used,158 LLM_FFN_RELU, true,159 hparams.expert_weights_scale,160 static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),161 il, probs);162 163 cb(ffn_out, "ffn_out", il);164 cur = ffn_out;165 166 cur = ggml_add(ctx0, cur, ffn_inp);167 168 cur = build_cvec(cur, il);169 cb(cur, "l_out", il);170 171 // input for next layer172 inpL = cur;173 }174 cur = inpL;175 176 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);177 cb(cur, "result_norm", -1);178 res->t_embd = cur;179 180 // lm_head181 cur = build_lora_mm(model.output, cur, model.output_s);182 cb(cur, "result_output", -1);183 res->t_logits = cur;184 185 ggml_build_forward_expand(gf, cur);186}187 188// Explicit template instantiations189template struct llama_model_smallthinker::graph<false>;190template struct llama_model_smallthinker::graph<true>;191 