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_bailingmoe2::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_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);6 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);7 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);8 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);9 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);10 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);11 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func);12 ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);13 14 GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");15 16 switch (hparams.n_layer()) {17 case 20: type = LLM_TYPE_16B_A1B; break;18 case 32: type = LLM_TYPE_100B_A6B; break;19 default: type = LLM_TYPE_UNKNOWN;20 }21}22 23void llama_model_bailingmoe2::load_arch_tensors(llama_model_loader &) {24 LLAMA_LOAD_LOCALS;25 const int64_t n_expert_shared = hparams.n_expert_shared;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 GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for bailingmoe2");36 GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2");37 38 for (int i = 0; i < n_layer_all; ++i) {39 int flags = 0;40 if (i >= n_layer) {41 // skip all tensors in the NextN layers42 flags |= TENSOR_SKIP;43 }44 45 auto & layer = layers[i];46 47 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);48 49 layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags);50 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);51 52 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);53 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);54 55 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);56 57 if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers58 const int64_t n_ff_shexp = (hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp) * n_expert_shared;59 60 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);61 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);62 63 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags);64 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags);65 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags);66 67 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);68 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);69 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);70 } else { // Dense layers71 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags);72 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags);73 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags);74 }75 76 // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers77 if (i >= n_layer) {78 layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);79 layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);80 layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);81 layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);82 layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);83 layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED | flags);84 layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, flags);85 }86 }87}88 89std::unique_ptr<llm_graph_context> llama_model_bailingmoe2::build_arch_graph(const llm_graph_params & params) const {90 return std::make_unique<graph>(*this, params);91}92 93llama_model_bailingmoe2::graph::graph(const llama_model & model, const llm_graph_params & params) :94 llm_graph_context(params) {95 const int64_t n_embd_head = hparams.n_embd_head_v();96 97 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());98 99 ggml_tensor * cur;100 ggml_tensor * inpL;101 102 inpL = build_inp_embd(model.tok_embd);103 104 // inp_pos - contains the positions105 ggml_tensor * inp_pos = build_inp_pos();106 107 auto * inp_attn = build_attn_inp_kv();108 109 ggml_tensor * inp_out_ids = build_inp_out_ids();110 111 for (int il = 0; il < n_layer; ++il) {112 ggml_tensor * inpSA = inpL;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 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,121 n_embd_head, n_head, n_head_kv, il);122 123 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);124 cb(Qcur, "Qcur_normed", il);125 126 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,127 ext_factor, attn_factor, beta_fast, beta_slow);128 129 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);130 cb(Kcur, "Kcur_normed", il);131 132 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,133 ext_factor, attn_factor, beta_fast, beta_slow);134 135 cb(Qcur, "Qcur", il);136 cb(Kcur, "Kcur", il);137 cb(Vcur, "Vcur", il);138 139 cur = build_attn(inp_attn,140 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,141 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);142 }143 144 if (il == n_layer - 1 && inp_out_ids) {145 cur = ggml_get_rows(ctx0, cur, inp_out_ids);146 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);147 }148 149 ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA);150 cb(sa_out, "sa_out", il);151 152 // MoE branch153 cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);154 cb(cur, "ffn_norm", il);155 156 if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {157 cur = build_ffn(cur,158 model.layers[il].ffn_up, NULL, NULL,159 model.layers[il].ffn_gate, NULL, NULL,160 model.layers[il].ffn_down, NULL, NULL,161 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);162 cb(cur, "ffn_out", il);163 } else {164 ggml_tensor * moe_out = build_moe_ffn(cur,165 model.layers[il].ffn_gate_inp,166 model.layers[il].ffn_up_exps,167 model.layers[il].ffn_gate_exps,168 model.layers[il].ffn_down_exps,169 model.layers[il].ffn_exp_probs_b,170 n_expert, n_expert_used,171 LLM_FFN_SILU, hparams.expert_weights_norm,172 hparams.expert_weights_scale,173 (llama_expert_gating_func_type) hparams.expert_gating_func,174 il);175 cb(moe_out, "ffn_moe_out", il);176 177 {178 ggml_tensor * ffn_shexp =179 build_ffn(cur,180 model.layers[il].ffn_up_shexp, NULL, NULL,181 model.layers[il].ffn_gate_shexp, NULL, NULL,182 model.layers[il].ffn_down_shexp, NULL, NULL,183 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);184 cb(ffn_shexp, "ffn_shexp", il);185 186 cur = ggml_add(ctx0, moe_out, ffn_shexp);187 cb(cur, "ffn_out", il);188 }189 }190 191 cur = ggml_add(ctx0, cur, sa_out);192 193 cur = build_cvec(cur, il);194 cb(cur, "l_out", il);195 196 // input for next layer197 inpL = cur;198 }199 200 cur = inpL;201 202 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);203 204 cb(cur, "result_norm", -1);205 res->t_embd = cur;206 207 // lm_head208 cur = build_lora_mm(model.output, cur, model.output_s);209 210 cb(cur, "result_output", -1);211 res->t_logits = cur;212 213 ggml_build_forward_expand(gf, cur);214}215 