1#include "models.h"
2
3
4
5llm_build_glm4::llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
6 const int64_t n_embd_head = hparams.n_embd_head_v;
7 const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
8
9 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
10
11 ggml_tensor * cur;
12 ggml_tensor * inpL;
13
14 inpL = build_inp_embd(tok_embd: model.tok_embd);
15
16 // inp_pos - contains the positions
17 ggml_tensor * inp_pos = build_inp_pos();
18
19 auto * inp_attn = build_attn_inp_kv();
20
21 ggml_tensor * inp_out_ids = build_inp_out_ids();
22
23 for (int il = 0; il < n_layer; ++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 = nullptr;
33 ggml_tensor * Kcur = nullptr;
34 ggml_tensor * Vcur = nullptr;
35
36 if (model.layers[il].wqkv == nullptr) {
37 Qcur = build_lora_mm(w: model.layers[il].wq, cur);
38 if (model.layers[il].bq) {
39 Qcur = ggml_add(ctx: ctx0, a: Qcur, b: model.layers[il].bq);
40 }
41 Kcur = build_lora_mm(w: model.layers[il].wk, cur);
42 if (model.layers[il].bk) {
43 Kcur = ggml_add(ctx: ctx0, a: Kcur, b: model.layers[il].bk);
44 }
45 Vcur = build_lora_mm(w: model.layers[il].wv, cur);
46 if (model.layers[il].bv) {
47 Vcur = ggml_add(ctx: ctx0, a: Vcur, b: model.layers[il].bv);
48 }
49 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
50 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
51 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
52 } else {
53 cur = build_lora_mm(w: model.layers[il].wqkv, cur);
54 cb(cur, name: "wqkv", il);
55 if (model.layers[il].bqkv) {
56 cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv);
57 cb(cur, name: "bqkv", il);
58 }
59 Qcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens, nb1: n_embd_head * sizeof(float), nb2: cur->nb[1],
60 offset: 0 * sizeof(float) * (n_embd));
61 Kcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float),
62 nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd));
63 Vcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float),
64 nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd + n_embd_gqa));
65 }
66 Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
67 ext_factor, attn_factor, beta_fast, beta_slow);
68
69 Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
70 ext_factor, attn_factor, beta_fast, beta_slow);
71
72 cb(cur: Qcur, name: "Qcur", il);
73 cb(cur: Kcur, name: "Kcur", il);
74 cb(cur: Vcur, name: "Vcur", il);
75
76 cur = build_attn(inp: inp_attn,
77 wo: model.layers[il].wo, NULL,
78 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);
79 }
80 if (il == n_layer - 1 && inp_out_ids) {
81 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
82 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
83 }
84 // Post-attention norm (new!)
85 cur = build_norm(cur, mw: model.layers[il].attn_post_norm, NULL, type: LLM_NORM_RMS, il);
86 cb(cur, name: "post_attn_norm", il);
87
88 // Add the input (residual connection after post-attention norm)
89 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
90 cb(cur: ffn_inp, name: "ffn_inp", il);
91
92 // FF
93 {
94 // Pre-MLP norm
95 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
96 cb(cur, name: "ffn_norm", il);
97
98 // MLP
99 cur = build_ffn(cur,
100 up: model.layers[il].ffn_up, NULL, NULL,
101 NULL, NULL, NULL,
102 down: model.layers[il].ffn_down, NULL, NULL,
103 NULL, type_op: LLM_FFN_SWIGLU, type_gate: LLM_FFN_SEQ, il);
104 cb(cur, name: "ffn_out", il);
105
106 // Post-MLP norm
107 cur = build_norm(cur, mw: model.layers[il].ffn_post_norm, NULL, type: LLM_NORM_RMS, il);
108 cb(cur, name: "post_mlp_norm", il);
109 }
110 // Add residual connection after post-MLP norm
111 inpL = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
112 cb(cur: inpL, name: "l_out", il);
113 }
114 // Final norm
115 cur = build_norm(cur: inpL, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
116
117 cb(cur, name: "result_norm", il: -1);
118 res->t_embd = cur;
119
120 // Output projection
121 cur = build_lora_mm(w: model.output, cur);
122
123 cb(cur, name: "result_output", il: -1);
124 res->t_logits = cur;
125
126 ggml_build_forward_expand(cgraph: gf, tensor: cur);
127}
128