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_exaone_moe::load_arch_hparams(llama_model_loader & ml) {4 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;5 hparams.n_swa = 128;6 uint32_t swa_period = 4;7 ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);8 hparams.set_swa_pattern(swa_period);9 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;10 hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;11 12 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);13 ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);14 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);15 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared, false);16 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);17 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);18 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func);19 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);20 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);21 ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);22 23 ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);24 GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");25 26 switch (hparams.n_layer()) {27 case 32: type = LLM_TYPE_30B_A3B; break;28 case 48: type = LLM_TYPE_235B_A22B; break;29 default: type = LLM_TYPE_UNKNOWN;30 }31}32 33void llama_model_exaone_moe::load_arch_tensors(llama_model_loader &) {34 LLAMA_LOAD_LOCALS;35 36 const int64_t n_ff_exp = hparams.n_ff_exp;37 const int64_t n_ff_shexp = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : n_ff_exp;38 const int64_t head_dim = hparams.n_embd_head_k();39 const int64_t n_qo_dim = n_head * head_dim;40 const int64_t n_kv_dim = n_head_kv * head_dim;41 42 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);43 44 // output45 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);46 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);47 48 if (output == NULL) {49 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);50 }51 52 for (int i = 0; i < n_layer_all; ++i) {53 int flags = 0;54 if (i >= n_layer) {55 // skip all tensors in the NextN layers56 flags |= TENSOR_SKIP;57 }58 59 auto & layer = layers[i];60 create_tensor_qkv(layer, i, n_embd, n_qo_dim, n_kv_dim, n_kv_dim, flags);61 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, flags);62 63 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0) | flags);64 65 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);66 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);67 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);68 69 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);70 71 // dense layers for first n_layer_dense_lead layers or nextn_predict_layers layers at the end72 if (i < (int) hparams.n_layer_dense_lead || (i >= n_layer)) {73 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags);74 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, flags);75 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags);76 } else {77 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);78 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);79 80 if (n_expert == 0) {81 throw std::runtime_error("n_expert must be > 0");82 }83 if (n_expert_used == 0) {84 throw std::runtime_error("n_expert_used must be > 0");85 }86 87 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, flags);88 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags);89 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, flags);90 91 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);92 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);93 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);94 }95 96 // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers97 if (i >= n_layer) {98 layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, flags);99 layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), {n_embd}, flags);100 layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), {n_embd}, flags);101 102 layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, flags | TENSOR_NOT_REQUIRED);103 layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);104 layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);105 }106 }107}108 109std::unique_ptr<llm_graph_context> llama_model_exaone_moe::build_arch_graph(const llm_graph_params & params) const {110 return std::make_unique<graph>(*this, params);111}112 113llama_model_exaone_moe::graph::graph(const llama_model & model, const llm_graph_params & params) :114 llm_graph_context(params) {115 const int64_t n_embd_head = hparams.n_embd_head_k();116 117 GGML_ASSERT(n_embd_head == hparams.n_embd_head_v());118 GGML_ASSERT(n_embd_head == n_rot);119 120 ggml_tensor * cur;121 ggml_tensor * inpL;122 123 inpL = build_inp_embd(model.tok_embd);124 125 // inp_pos - contains the positions126 ggml_tensor * inp_pos = build_inp_pos();127 128 auto * inp_attn_iswa = build_attn_inp_kv_iswa();129 130 ggml_tensor * inp_out_ids = build_inp_out_ids();131 132 for (int il = 0; il < n_layer; ++il) {133 ggml_tensor * inpSA = inpL;134 135 // use RoPE for SWA layers136 const bool is_local_layer = hparams.is_swa(il);137 138 // norm139 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);140 cb(cur, "attn_norm", il);141 142 // self-attention143 {144 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);145 146 // compute Q and K and RoPE them147 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,148 n_embd_head, n_head, n_head_kv, il);149 150 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);151 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);152 cb(Qcur, "Qcur_normed", il);153 cb(Kcur, "Kcur_normed", il);154 155 if (is_local_layer) {156 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,157 freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);158 159 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,160 freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);161 }162 cb(Qcur, "Qcur", il);163 cb(Kcur, "Kcur", il);164 cb(Vcur, "Vcur", il);165 166 cur = build_attn(inp_attn_iswa,167 model.layers[il].wo, NULL, model.layers[il].wo_s,168 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);169 cb(cur, "attn_out", il);170 }171 if (il == n_layer - 1 && inp_out_ids) {172 cur = ggml_get_rows(ctx0, cur, inp_out_ids);173 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);174 }175 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);176 cb(ffn_inp, "ffn_inp", il);177 178 // norm179 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);180 cb(cur, "ffn_norm", il);181 182 // feed-forward network183 if (model.layers[il].ffn_gate_inp == nullptr) {184 // dense branch185 cur = build_ffn(cur,186 model.layers[il].ffn_up, NULL, NULL,187 model.layers[il].ffn_gate, NULL, NULL,188 model.layers[il].ffn_down, NULL, NULL, NULL,189 LLM_FFN_SILU, LLM_FFN_PAR, il);190 cb(cur, "ffn_out", il);191 } else {192 // MoE branch193 ggml_tensor * moe_out = 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 model.layers[il].ffn_exp_probs_b,199 n_expert, n_expert_used,200 LLM_FFN_SILU, hparams.expert_weights_norm,201 hparams.expert_weights_scale,202 (llama_expert_gating_func_type) hparams.expert_gating_func,203 il);204 cb(moe_out, "ffn_moe_out", il);205 206 // FFN shared expert207 {208 ggml_tensor * ffn_shexp =209 build_ffn(cur,210 model.layers[il].ffn_up_shexp, NULL, NULL,211 model.layers[il].ffn_gate_shexp, NULL, NULL,212 model.layers[il].ffn_down_shexp, NULL, NULL,213 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);214 cb(ffn_shexp, "ffn_shexp", il);215 216 cur = ggml_add(ctx0, moe_out, ffn_shexp);217 cb(cur, "ffn_out", il);218 }219 }220 221 cur = ggml_add(ctx0, cur, ffn_inp);222 223 cur = build_cvec(cur, il);224 cb(cur, "l_out", il);225 226 // input for next layer227 inpL = cur;228 }229 cur = inpL;230 231 // final norm232 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);233 234 cb(cur, "result_norm", -1);235 res->t_embd = cur;236 237 // lm_head238 cur = build_lora_mm(model.output, cur, model.output_s);239 240 cb(cur, "result_output", -1);241 res->t_logits = cur;242 243 ggml_build_forward_expand(gf, cur);244}245 