| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_glm4_moe::llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 4 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 5 | |
| 6 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 7 | |
| 8 | int sections[4]; |
| 9 | std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
| 10 | |
| 11 | ggml_tensor * cur; |
| 12 | ggml_tensor * inpL; |
| 13 | |
| 14 | inpL = build_inp_embd(model.tok_embd); |
| 15 | |
| 16 | bool use_mrope = hparams.use_mrope(); |
| 17 | if (ubatch.embd && !use_mrope) { |
| 18 | // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results |
| 19 | GGML_ABORT("This GGUF does not support multimodal. Please reconvert it."); |
| 20 | } |
| 21 | |
| 22 | // inp_pos - contains the positions |
| 23 | ggml_tensor * inp_pos = build_inp_pos(); |
| 24 | |
| 25 | auto * inp_attn = build_attn_inp_kv(); |
| 26 | |
| 27 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 28 | |
| 29 | // Only process up to last layer (skip final NextN layer) |
| 30 | // Final layer tensors are loaded but not processed in forward pass |
| 31 | const int n_transformer_layers = n_layer - hparams.nextn_predict_layers; |
| 32 | for (int il = 0; il < n_transformer_layers; ++il) { |
| 33 | ggml_tensor * inpSA = inpL; |
| 34 | |
| 35 | // Pre-attention norm |
| 36 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 37 | cb(cur, "attn_norm", il); |
| 38 | |
| 39 | // self-attention |
| 40 | { |
| 41 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
| 42 | if (model.layers[il].bq) { |
| 43 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
| 44 | } |
| 45 | cb(Qcur, "Qcur", il); |
| 46 | |
| 47 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
| 48 | if (model.layers[il].bk) { |
| 49 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
| 50 | } |
| 51 | cb(Kcur, "Kcur", il); |
| 52 | |
| 53 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
| 54 | if (model.layers[il].bv) { |
| 55 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
| 56 | } |
| 57 | cb(Vcur, "Vcur", il); |
| 58 | |
| 59 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 60 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
nothing calls this directly
no test coverage detected