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