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_glm4_moe::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);5 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);6 ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);7 8 // MoE parameters9 ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert);10 ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used);11 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);12 ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);13 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);14 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);15 16 // Expert gating function (GLM-4.5 uses sigmoid)17 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);18 if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {19 hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;20 }21 22 // NextN/MTP parameters23 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 46: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air28 case 48: type = LLM_TYPE_102B_A12B; break; // Solar Open29 case 92: type = LLM_TYPE_355B_A32B; break; // GLM-4.530 default: type = LLM_TYPE_UNKNOWN;31 }32}33 34void llama_model_glm4_moe::load_arch_tensors(llama_model_loader &) {35 LLAMA_LOAD_LOCALS;36 const int64_t n_expert_shared = hparams.n_expert_shared;37 38 39 GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers");40 GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers");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 }, TENSOR_NOT_REQUIRED);47 // if output is NULL, init from the input tok embed48 if (output == NULL) {49 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);50 }51 52 // Load ALL tensors including NextN layer to satisfy total tensor count53 // but only PROCESS up to last layer (skipping final NextN layer) in forward pass54 for (int i = 0; i < n_layer_all; ++i) {55 int flags = 0;56 if (i >= n_layer) {57 // skip all tensors in the NextN layers58 flags |= TENSOR_SKIP;59 }60 61 auto & layer = layers[i];62 63 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);64 65 // GLM-style attention with bias terms66 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, flags);67 68 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);69 70 // K/Q norm tensors (optional for GLM-4.5 355B variant)71 layer.attn_q_norm = create_tensor(72 tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);73 layer.attn_k_norm = create_tensor(74 tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);75 76 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags);77 78 // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead79 // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE80 const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead);81 82 if (use_moe) {83 // MoE layers84 layer.ffn_gate_inp =85 create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags);86 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags);87 88 // MoE branch89 const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;90 91 layer.ffn_gate_exps = create_tensor(92 tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);93 layer.ffn_down_exps = create_tensor(94 tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);95 layer.ffn_up_exps = create_tensor(96 tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);97 98 // Shared expert99 if (n_expert_shared > 0) {100 const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;101 layer.ffn_gate_shexp = create_tensor(102 tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);103 layer.ffn_down_shexp = create_tensor(104 tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);105 layer.ffn_up_shexp = create_tensor(106 tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);107 }108 } else {109 // Dense layers (first k layers) - GLM uses separate gate/up projections110 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);111 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);112 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags);113 }114 115 // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers116 if (i >= n_layer) {117 layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);118 layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);119 layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);120 121 // Optional tensors122 layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);123 layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);124 layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED);125 }126 }127}128 129std::unique_ptr<llm_graph_context> llama_model_glm4_moe::build_arch_graph(const llm_graph_params & params) const {130 return std::make_unique<graph>(*this, params);131}132 133llama_model_glm4_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {134 const int64_t n_embd_head = hparams.n_embd_head_v();135 136 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());137 138 int sections[4];139 std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);140 141 ggml_tensor * cur;142 ggml_tensor * inpL;143 144 inpL = build_inp_embd(model.tok_embd);145 146 bool use_mrope = hparams.use_mrope();147 if (ubatch.embd && !use_mrope) {148 // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results149 GGML_ABORT("This GGUF does not support multimodal. Please reconvert it.");150 }151 152 // inp_pos - contains the positions153 ggml_tensor * inp_pos = build_inp_pos();154 155 auto * inp_attn = build_attn_inp_kv();156 157 ggml_tensor * inp_out_ids = build_inp_out_ids();158 159 // Only process up to last layer (skip final NextN layer)160 // Final layer tensors are loaded but not processed in forward pass161 for (int il = 0; il < n_layer; ++il) {162 ggml_tensor * inpSA = inpL;163 164 // Pre-attention norm165 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);166 cb(cur, "attn_norm", il);167 168 // self-attention169 {170 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,171 n_embd_head, n_head, n_head_kv, il);172 173 // Apply Q/K norm if available (GLM-4.5 355B variant)174 if (model.layers[il].attn_q_norm) {175 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);176 cb(Qcur, "Qcur_normed", il);177 }178 if (model.layers[il].attn_k_norm) {179 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);180 cb(Kcur, "Kcur_normed", il);181 }182 183 if (use_mrope) {184 Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr,185 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,186 ext_factor, attn_factor, beta_fast, beta_slow);187 188 Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr,189 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,190 ext_factor, attn_factor, beta_fast, beta_slow);191 } else {192 // Normal RoPE193 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot,194 rope_type, n_ctx_orig, freq_base, freq_scale,195 ext_factor, attn_factor, beta_fast, beta_slow);196 197 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot,198 rope_type, n_ctx_orig, freq_base, freq_scale,199 ext_factor, attn_factor, beta_fast, beta_slow);200 }201 202 cb(Qcur, "Qcur", il);203 cb(Kcur, "Kcur", il);204 cb(Vcur, "Vcur", il);205 206 cur = build_attn(inp_attn,207 model.layers[il].wo, NULL, model.layers[il].wo_s,208 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);209 }210 if (il == n_layer - 1 && inp_out_ids) {211 cur = ggml_get_rows(ctx0, cur, inp_out_ids);212 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);213 }214 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);215 cb(ffn_inp, "ffn_inp", il);216 217 // Post-attention norm218 cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);219 cb(cur, "post_attn_norm", il);220 221 // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense)222 if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {223 // Dense FFN layer224 cur = build_ffn(cur,225 model.layers[il].ffn_up, NULL, NULL,226 model.layers[il].ffn_gate, NULL, NULL,227 model.layers[il].ffn_down, NULL, NULL,228 NULL,229 LLM_FFN_SILU, LLM_FFN_PAR, il);230 cb(cur, "ffn_out", il);231 } else {232 // Process routed experts using existing MoE infrastructure233 ggml_tensor * routed_out = build_moe_ffn(cur,234 model.layers[il].ffn_gate_inp,235 model.layers[il].ffn_up_exps,236 model.layers[il].ffn_gate_exps,237 model.layers[il].ffn_down_exps,238 model.layers[il].ffn_exp_probs_b,239 n_expert, n_expert_used,240 LLM_FFN_SILU, hparams.expert_weights_norm,241 hparams.expert_weights_scale,242 (llama_expert_gating_func_type) hparams.expert_gating_func,243 il);244 cb(routed_out, "ffn_moe_out", il);245 246 // Process shared expert on original input247 ggml_tensor * shared_out = build_ffn(cur,248 model.layers[il].ffn_up_shexp, NULL, NULL,249 model.layers[il].ffn_gate_shexp, NULL, NULL,250 model.layers[il].ffn_down_shexp, NULL, NULL,251 NULL,252 LLM_FFN_SILU, LLM_FFN_PAR, il);253 cb(shared_out, "ffn_shexp_out", il);254 255 // Final output: routed_output + shared_output256 cur = ggml_add(ctx0, routed_out, shared_out);257 cb(cur, "ffn_out", il);258 }259 cur = ggml_add(ctx0, cur, ffn_inp);260 261 cur = build_cvec(cur, il);262 cb(cur, "l_out", il);263 264 // input for next layer265 inpL = cur;266 }267 cur = inpL;268 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);269 270 cb(cur, "result_norm", -1);271 res->t_embd = cur;272 273 // lm_head274 cur = build_lora_mm(model.output, cur, model.output_s);275 276 cb(cur, "result_output", -1);277 res->t_logits = cur;278 279 ggml_build_forward_expand(gf, cur);280}281 