Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_glm4::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_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);6 7 switch (hparams.n_layer()) {8 case 17: type = LLM_TYPE_1B; break; // GLM-OCR9 case 40: type = LLM_TYPE_9B; break;10 case 61: type = LLM_TYPE_32B; break;11 default: type = LLM_TYPE_UNKNOWN;12 }13}14 15void llama_model_glm4::load_arch_tensors(llama_model_loader &) {16 LLAMA_LOAD_LOCALS;17 18 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);19 20 // output21 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);22 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);23 // if output is NULL, init from the input tok embed24 if (output == NULL) {25 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);26 }27 28 for (int i = 0; i < n_layer_all; ++i) {29 int flags = 0;30 if (i >= n_layer) {31 // skip all tensors in the NextN layers32 flags |= TENSOR_SKIP;33 }34 35 auto & layer = layers[i];36 37 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);38 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, flags);39 40 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);41 42 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, flags);43 44 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);45 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags);46 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, flags);47 48 layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, flags);49 50 // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers51 if (i >= n_layer) {52 layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);53 layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);54 layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);55 56 // Optional tensors57 layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);58 layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);59 layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED);60 }61 }62}63 64std::unique_ptr<llm_graph_context> llama_model_glm4::build_arch_graph(const llm_graph_params & params) const {65 return std::make_unique<graph>(*this, params);66}67 68llama_model_glm4::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {69 const int64_t n_embd_head = hparams.n_embd_head_v();70 71 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());72 73 int sections[4];74 std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);75 76 ggml_tensor * cur;77 ggml_tensor * inpL;78 79 inpL = build_inp_embd(model.tok_embd);80 81 bool use_mrope = hparams.use_mrope();82 if (ubatch.embd && !use_mrope) {83 // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results84 GGML_ABORT("This GGUF does not support multimodal. Please reconvert it.");85 }86 87 // inp_pos - contains the positions88 ggml_tensor * inp_pos = build_inp_pos();89 90 auto * inp_attn = build_attn_inp_kv();91 92 ggml_tensor * inp_out_ids = build_inp_out_ids();93 94 // Only process up to last layer (skip final NextN layer)95 // Final layer tensors are loaded but not processed in forward pass96 for (int il = 0; il < n_layer; ++il) {97 ggml_tensor * inpSA = inpL;98 99 // Pre-attention norm100 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);101 cb(cur, "attn_norm", il);102 103 // self-attention104 {105 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,106 n_embd_head, n_head, n_head_kv, il);107 108 if (use_mrope) {109 Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr,110 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,111 ext_factor, attn_factor, beta_fast, beta_slow);112 113 Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr,114 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,115 ext_factor, attn_factor, beta_fast, beta_slow);116 } else {117 // Normal RoPE118 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot,119 rope_type, n_ctx_orig, freq_base, freq_scale,120 ext_factor, attn_factor, beta_fast, beta_slow);121 122 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot,123 rope_type, n_ctx_orig, freq_base, freq_scale,124 ext_factor, attn_factor, beta_fast, beta_slow);125 }126 127 cb(Qcur, "Qcur", il);128 cb(Kcur, "Kcur", il);129 cb(Vcur, "Vcur", il);130 131 cur = build_attn(inp_attn,132 model.layers[il].wo, NULL, model.layers[il].wo_s,133 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);134 }135 if (il == n_layer - 1 && inp_out_ids) {136 cur = ggml_get_rows(ctx0, cur, inp_out_ids);137 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);138 }139 // Post-attention norm (new!)140 cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);141 cb(cur, "post_attn_norm", il);142 143 // Add the input (residual connection after post-attention norm)144 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);145 cb(ffn_inp, "ffn_inp", il);146 147 // FF148 {149 // Pre-MLP norm150 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);151 cb(cur, "ffn_norm", il);152 153 // MLP154 cur = build_ffn(cur,155 model.layers[il].ffn_up, NULL, NULL,156 NULL, NULL, NULL,157 model.layers[il].ffn_down, NULL, NULL,158 NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);159 cb(cur, "ffn_out", il);160 161 // Post-MLP norm162 cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);163 cb(cur, "post_mlp_norm", il);164 }165 cur = ggml_add(ctx0, cur, ffn_inp);166 167 cur = build_cvec(cur, il);168 cb(cur, "l_out", il);169 170 // input for next layer171 inpL = cur;172 }173 // Final norm174 cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);175 176 cb(cur, "result_norm", -1);177 res->t_embd = cur;178 179 // Output projection180 cur = build_lora_mm(model.output, cur, model.output_s);181 182 cb(cur, "result_output", -1);183 res->t_logits = cur;184 185 ggml_build_forward_expand(gf, cur);186}187 