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_openai_moe::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_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);6 ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);7 8 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;9 uint32_t swa_period = 2;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 = hparams.rope_freq_scale_train;15 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);16 17 switch (hparams.n_layer()) {18 case 24: type = LLM_TYPE_20B; break;19 case 36: type = LLM_TYPE_120B; break;20 default: type = LLM_TYPE_UNKNOWN;21 }22}23 24void llama_model_openai_moe::load_arch_tensors(llama_model_loader &) {25 LLAMA_LOAD_LOCALS;26 27 const int64_t n_ff_exp = hparams.n_ff_exp;28 29 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);30 31 // output32 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);33 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);34 35 for (int i = 0; i < n_layer; ++i) {36 auto & layer = layers[i];37 38 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);39 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);40 41 create_tensor_qkv(layer, i, n_embd, n_head * n_rot, n_head_kv * n_rot, n_head_kv * n_rot, 0);42 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);43 44 layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);45 46 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);47 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);48 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);49 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);50 51 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);52 53 layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);54 layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);55 layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);56 layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);57 }58}59 60std::unique_ptr<llm_graph_context> llama_model_openai_moe::build_arch_graph(const llm_graph_params & params) const {61 return std::make_unique<graph>(*this, params);62}63 64llama_model_openai_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {65 ggml_tensor * cur;66 ggml_tensor * inpL;67 68 inpL = build_inp_embd(model.tok_embd);69 70 // inp_pos - contains the positions71 ggml_tensor * inp_pos = build_inp_pos();72 73 auto * inp_attn = build_attn_inp_kv_iswa();74 75 ggml_tensor * inp_out_ids = build_inp_out_ids();76 77 for (int il = 0; il < n_layer; ++il) {78 res->t_layer_inp[il] = inpL;79 80 const float freq_base_l = model.get_rope_freq_base (cparams, il);81 const float freq_scale_l = model.get_rope_freq_scale(cparams, il);82 83 ggml_tensor * inpSA = inpL;84 85 // norm86 cur = build_norm(inpL,87 model.layers[il].attn_norm, nullptr,88 LLM_NORM_RMS, il);89 cb(cur, "attn_norm", il);90 91 // self-attention92 {93 // compute Q and K and RoPE them94 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,95 n_rot, n_head, n_head_kv, il);96 97 Qcur = ggml_rope_ext(98 ctx0, Qcur, inp_pos, nullptr,99 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,100 ext_factor, attn_factor, beta_fast, beta_slow101 );102 103 Kcur = ggml_rope_ext(104 ctx0, Kcur, inp_pos, nullptr,105 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,106 ext_factor, attn_factor, beta_fast, beta_slow107 );108 109 cb(Qcur, "Qcur", il);110 cb(Kcur, "Kcur", il);111 cb(Vcur, "Vcur", il);112 113 cur = build_attn(inp_attn,114 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,115 Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);116 117 cb(cur, "attn_out", il);118 }119 if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) {120 // skip computing output for unused tokens121 cur = ggml_get_rows(ctx0, cur, inp_out_ids);122 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);123 }124 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);125 cb(ffn_inp, "ffn_inp", il);126 127 cur = ffn_inp;128 cur = build_norm(cur,129 model.layers[il].attn_post_norm, nullptr,130 LLM_NORM_RMS, il);131 cb(cur, "attn_post_norm", il);132 133 // MoE branch134 cur = build_moe_ffn(cur,135 model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,136 model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,137 model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,138 model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,139 nullptr,140 n_expert, n_expert_used,141 LLM_FFN_SWIGLU_OAI_MOE, false,142 hparams.expert_weights_scale,143 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,144 il);145 cb(cur, "ffn_moe_out", il);146 147 cur = ggml_add(ctx0, cur, ffn_inp);148 149 cur = build_cvec(cur, il);150 cb(cur, "l_out", il);151 152 // input for next layer153 inpL = cur;154 }155 cur = inpL;156 157 res->t_h_nextn = cur;158 159 if (!cparams.embeddings_nextn_masked && inp_out_ids) {160 cur = ggml_get_rows(ctx0, cur, inp_out_ids);161 }162 163 cur = build_norm(cur,164 model.output_norm, NULL,165 LLM_NORM_RMS, -1);166 167 cb(cur, "result_norm", -1);168 res->t_embd = cur;169 170 // lm_head171 cur = build_lora_mm(model.output, cur, model.output_s);172 173 cb(cur, "result_output", -1);174 res->t_logits = cur;175 176 ggml_build_forward_expand(gf, cur);177}178 