| 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 | ggml_tensor * cur; |
| 9 | ggml_tensor * inpL; |
| 10 | |
| 11 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 12 | |
| 13 | // inp_pos - contains the positions |
| 14 | ggml_tensor * inp_pos = build_inp_pos(); |
| 15 | |
| 16 | auto * inp_attn = build_attn_inp_kv(); |
| 17 | |
| 18 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 19 | |
| 20 | // Only process up to last layer (skip final NextN layer) |
| 21 | // Final layer tensors are loaded but not processed in forward pass |
| 22 | const int n_transformer_layers = n_layer - hparams.nextn_predict_layers; |
| 23 | for (int il = 0; il < n_transformer_layers; ++il) { |
| 24 | ggml_tensor * inpSA = inpL; |
| 25 | |
| 26 | // Pre-attention norm |
| 27 | cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il); |
| 28 | cb(cur, name: "attn_norm" , il); |
| 29 | |
| 30 | // self-attention |
| 31 | { |
| 32 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 33 | if (model.layers[il].bq) { |
| 34 | Qcur = ggml_add(ctx: ctx0, a: Qcur, b: model.layers[il].bq); |
| 35 | } |
| 36 | cb(cur: Qcur, name: "Qcur" , il); |
| 37 | |
| 38 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 39 | if (model.layers[il].bk) { |
| 40 | Kcur = ggml_add(ctx: ctx0, a: Kcur, b: model.layers[il].bk); |
| 41 | } |
| 42 | cb(cur: Kcur, name: "Kcur" , il); |
| 43 | |
| 44 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 45 | if (model.layers[il].bv) { |
| 46 | Vcur = ggml_add(ctx: ctx0, a: Vcur, b: model.layers[il].bv); |
| 47 | } |
| 48 | cb(cur: Vcur, name: "Vcur" , il); |
| 49 | |
| 50 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 51 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 52 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 53 | |
| 54 | // Apply Q/K norm if available (GLM-4.5 355B variant) |
| 55 | if (model.layers[il].attn_q_norm) { |
| 56 | Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il); |
| 57 | cb(cur: Qcur, name: "Qcur_normed" , il); |
| 58 | } |
| 59 | if (model.layers[il].attn_k_norm) { |
| 60 | Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il); |
| 61 | cb(cur: Kcur, name: "Kcur_normed" , il); |
| 62 | } |
| 63 | Qcur = ggml_rope_ext( |
| 64 | ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, |
| 65 | n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 66 | ext_factor, attn_factor, beta_fast, beta_slow |
| 67 | ); |
| 68 | |
| 69 | Kcur = ggml_rope_ext( |
| 70 | ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, |
| 71 | n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 72 | ext_factor, attn_factor, beta_fast, beta_slow |
| 73 | ); |
| 74 | |
| 75 | cb(cur: Qcur, name: "Qcur" , il); |
| 76 | cb(cur: Kcur, name: "Kcur" , il); |
| 77 | cb(cur: Vcur, name: "Vcur" , il); |
| 78 | |
| 79 | cur = build_attn(inp: inp_attn, |
| 80 | wo: model.layers[il].wo, NULL, |
| 81 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f/sqrtf(x: float(n_embd_head)), il); |
| 82 | } |
| 83 | if (il == n_transformer_layers - 1 && inp_out_ids) { |
| 84 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 85 | inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids); |
| 86 | } |
| 87 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA); |
| 88 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 89 | |
| 90 | // Post-attention norm |
| 91 | cur = build_norm(cur: ffn_inp, mw: model.layers[il].attn_post_norm, NULL, type: LLM_NORM_RMS, il); |
| 92 | cb(cur, name: "post_attn_norm" , il); |
| 93 | |
| 94 | // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense) |
| 95 | if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) { |
| 96 | // Dense FFN layer |
| 97 | cur = build_ffn(cur, |
| 98 | up: model.layers[il].ffn_up, NULL, NULL, |
| 99 | gate: model.layers[il].ffn_gate, NULL, NULL, |
| 100 | down: model.layers[il].ffn_down, NULL, NULL, |
| 101 | NULL, |
| 102 | type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il); |
| 103 | cb(cur, name: "ffn_out" , il); |
| 104 | } else { |
| 105 | // Process routed experts using existing MoE infrastructure |
| 106 | ggml_tensor * routed_out = build_moe_ffn(cur, |
| 107 | gate_inp: model.layers[il].ffn_gate_inp, |
| 108 | up_exps: model.layers[il].ffn_up_exps, |
| 109 | gate_exps: model.layers[il].ffn_gate_exps, |
| 110 | down_exps: model.layers[il].ffn_down_exps, |
| 111 | exp_probs_b: model.layers[il].ffn_exp_probs_b, |
| 112 | n_expert, n_expert_used, |
| 113 | type_op: LLM_FFN_SILU, norm_w: hparams.expert_weights_norm, |
| 114 | scale_w: true, w_scale: hparams.expert_weights_scale, |
| 115 | gating_op: (llama_expert_gating_func_type) hparams.expert_gating_func, |
| 116 | il); |
| 117 | cb(cur: routed_out, name: "ffn_moe_out" , il); |
| 118 | |
| 119 | // Process shared expert on original input |
| 120 | ggml_tensor * shared_out = build_ffn(cur, |
| 121 | up: model.layers[il].ffn_up_shexp, NULL, NULL, |
| 122 | gate: model.layers[il].ffn_gate_shexp, NULL, NULL, |
| 123 | down: model.layers[il].ffn_down_shexp, NULL, NULL, |
| 124 | NULL, |
| 125 | type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il); |
| 126 | cb(cur: shared_out, name: "ffn_shexp_out" , il); |
| 127 | |
| 128 | // Final output: routed_output + shared_output |
| 129 | cur = ggml_add(ctx: ctx0, a: routed_out, b: shared_out); |
| 130 | cb(cur, name: "ffn_out" , il); |
| 131 | } |
| 132 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 133 | |
| 134 | cur = build_cvec(cur, il); |
| 135 | cb(cur, name: "l_out" , il); |
| 136 | |
| 137 | // input for next layer |
| 138 | inpL = cur; |
| 139 | } |
| 140 | cur = inpL; |
| 141 | cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 142 | |
| 143 | cb(cur, name: "result_norm" , il: -1); |
| 144 | res->t_embd = cur; |
| 145 | |
| 146 | // lm_head |
| 147 | cur = build_lora_mm(w: model.output, cur); |
| 148 | |
| 149 | cb(cur, name: "result_output" , il: -1); |
| 150 | res->t_logits = cur; |
| 151 | |
| 152 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 153 | } |
| 154 | |