@fugood/llama.node 1.3.0-rc.6 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/CMakeLists.txt +12 -2
  2. package/package.json +14 -14
  3. package/scripts/llama.cpp.patch +8 -9
  4. package/src/llama.cpp/common/CMakeLists.txt +2 -0
  5. package/src/llama.cpp/common/arg.cpp +39 -1001
  6. package/src/llama.cpp/common/arg.h +2 -2
  7. package/src/llama.cpp/common/chat.cpp +216 -2
  8. package/src/llama.cpp/common/chat.h +1 -0
  9. package/src/llama.cpp/common/common.cpp +33 -0
  10. package/src/llama.cpp/common/common.h +13 -0
  11. package/src/llama.cpp/common/download.cpp +1054 -0
  12. package/src/llama.cpp/common/download.h +55 -0
  13. package/src/llama.cpp/common/json-schema-to-grammar.cpp +19 -3
  14. package/src/llama.cpp/ggml/CMakeLists.txt +3 -1
  15. package/src/llama.cpp/ggml/include/ggml-hexagon.h +19 -0
  16. package/src/llama.cpp/ggml/include/ggml.h +2 -0
  17. package/src/llama.cpp/ggml/src/CMakeLists.txt +7 -3
  18. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +10 -3
  19. package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +4 -5
  20. package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +108 -49
  21. package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
  22. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +3 -1
  23. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +0 -5
  24. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -35
  25. package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +82 -21
  26. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +25 -25
  27. package/src/llama.cpp/include/llama.h +7 -3
  28. package/src/llama.cpp/src/CMakeLists.txt +95 -0
  29. package/src/llama.cpp/src/llama-arch.cpp +108 -0
  30. package/src/llama.cpp/src/llama-arch.h +11 -0
  31. package/src/llama.cpp/src/llama-batch.cpp +63 -31
  32. package/src/llama.cpp/src/llama-batch.h +12 -1
  33. package/src/llama.cpp/src/llama-chat.cpp +32 -0
  34. package/src/llama.cpp/src/llama-chat.h +1 -0
  35. package/src/llama.cpp/src/llama-context.cpp +44 -16
  36. package/src/llama.cpp/src/llama-context.h +5 -5
  37. package/src/llama.cpp/src/llama-cparams.h +1 -0
  38. package/src/llama.cpp/src/llama-graph.cpp +12 -7
  39. package/src/llama.cpp/src/llama-hparams.cpp +11 -1
  40. package/src/llama.cpp/src/llama-hparams.h +6 -0
  41. package/src/llama.cpp/src/llama-kv-cache-iswa.cpp +3 -1
  42. package/src/llama.cpp/src/llama-kv-cache.cpp +56 -21
  43. package/src/llama.cpp/src/llama-kv-cache.h +2 -4
  44. package/src/llama.cpp/src/llama-kv-cells.h +44 -2
  45. package/src/llama.cpp/src/llama-memory-recurrent.cpp +18 -14
  46. package/src/llama.cpp/src/llama-memory-recurrent.h +2 -2
  47. package/src/llama.cpp/src/llama-model.cpp +350 -13194
  48. package/src/llama.cpp/src/llama-model.h +9 -2
  49. package/src/llama.cpp/src/llama-quant.cpp +1 -1
  50. package/src/llama.cpp/src/llama-vocab.cpp +5 -0
  51. package/src/llama.cpp/src/llama-vocab.h +1 -0
  52. package/src/llama.cpp/src/models/apertus.cpp +125 -0
  53. package/src/llama.cpp/src/models/arcee.cpp +135 -0
  54. package/src/llama.cpp/src/models/arctic.cpp +138 -0
  55. package/src/llama.cpp/src/models/arwkv7.cpp +86 -0
  56. package/src/llama.cpp/src/models/baichuan.cpp +122 -0
  57. package/src/llama.cpp/src/models/bailingmoe.cpp +144 -0
  58. package/src/llama.cpp/src/models/bailingmoe2.cpp +135 -0
  59. package/src/llama.cpp/src/models/bert.cpp +176 -0
  60. package/src/llama.cpp/src/models/bitnet.cpp +160 -0
  61. package/src/llama.cpp/src/models/bloom.cpp +101 -0
  62. package/src/llama.cpp/src/models/chameleon.cpp +178 -0
  63. package/src/llama.cpp/src/models/chatglm.cpp +132 -0
  64. package/src/llama.cpp/src/models/codeshell.cpp +111 -0
  65. package/src/llama.cpp/src/models/cogvlm.cpp +100 -0
  66. package/src/llama.cpp/src/models/cohere2-iswa.cpp +131 -0
  67. package/src/llama.cpp/src/models/command-r.cpp +122 -0
  68. package/src/llama.cpp/src/models/dbrx.cpp +123 -0
  69. package/src/llama.cpp/src/models/deci.cpp +135 -0
  70. package/src/llama.cpp/src/models/deepseek.cpp +144 -0
  71. package/src/llama.cpp/src/models/deepseek2.cpp +236 -0
  72. package/src/llama.cpp/src/models/dots1.cpp +134 -0
  73. package/src/llama.cpp/src/models/dream.cpp +105 -0
  74. package/src/llama.cpp/src/models/ernie4-5-moe.cpp +150 -0
  75. package/src/llama.cpp/src/models/ernie4-5.cpp +111 -0
  76. package/src/llama.cpp/src/models/exaone.cpp +114 -0
  77. package/src/llama.cpp/src/models/exaone4.cpp +123 -0
  78. package/src/llama.cpp/src/models/falcon-h1.cpp +113 -0
  79. package/src/llama.cpp/src/models/falcon.cpp +120 -0
  80. package/src/llama.cpp/src/models/gemma-embedding.cpp +120 -0
  81. package/src/llama.cpp/src/models/gemma.cpp +112 -0
  82. package/src/llama.cpp/src/models/gemma2-iswa.cpp +125 -0
  83. package/src/llama.cpp/src/models/gemma3-iswa.cpp +131 -0
  84. package/src/llama.cpp/src/models/gemma3n-iswa.cpp +377 -0
  85. package/src/llama.cpp/src/models/glm4-moe.cpp +153 -0
  86. package/src/llama.cpp/src/models/glm4.cpp +127 -0
  87. package/src/llama.cpp/src/models/gpt2.cpp +105 -0
  88. package/src/llama.cpp/src/models/gptneox.cpp +144 -0
  89. package/src/llama.cpp/src/models/granite-hybrid.cpp +196 -0
  90. package/src/llama.cpp/src/models/granite.cpp +211 -0
  91. package/src/llama.cpp/src/models/graph-context-mamba.cpp +283 -0
  92. package/src/llama.cpp/src/models/grok.cpp +159 -0
  93. package/src/llama.cpp/src/models/grovemoe.cpp +141 -0
  94. package/src/llama.cpp/src/models/hunyuan-dense.cpp +132 -0
  95. package/src/llama.cpp/src/models/hunyuan-moe.cpp +154 -0
  96. package/src/llama.cpp/src/models/internlm2.cpp +120 -0
  97. package/src/llama.cpp/src/models/jais.cpp +86 -0
  98. package/src/llama.cpp/src/models/jamba.cpp +106 -0
  99. package/src/llama.cpp/src/models/lfm2.cpp +173 -0
  100. package/src/llama.cpp/src/models/llada-moe.cpp +122 -0
  101. package/src/llama.cpp/src/models/llada.cpp +99 -0
  102. package/src/llama.cpp/src/models/llama-iswa.cpp +174 -0
  103. package/src/llama.cpp/src/models/llama.cpp +155 -0
  104. package/src/llama.cpp/src/models/mamba.cpp +55 -0
  105. package/src/llama.cpp/src/models/minicpm3.cpp +199 -0
  106. package/src/llama.cpp/src/models/minimax-m2.cpp +124 -0
  107. package/src/llama.cpp/src/models/models.h +481 -0
  108. package/src/llama.cpp/src/models/mpt.cpp +126 -0
  109. package/src/llama.cpp/src/models/nemotron-h.cpp +121 -0
  110. package/src/llama.cpp/src/models/nemotron.cpp +122 -0
  111. package/src/llama.cpp/src/models/neo-bert.cpp +104 -0
  112. package/src/llama.cpp/src/models/olmo.cpp +121 -0
  113. package/src/llama.cpp/src/models/olmo2.cpp +150 -0
  114. package/src/llama.cpp/src/models/olmoe.cpp +124 -0
  115. package/src/llama.cpp/src/models/openai-moe-iswa.cpp +123 -0
  116. package/src/llama.cpp/src/models/openelm.cpp +124 -0
  117. package/src/llama.cpp/src/models/orion.cpp +123 -0
  118. package/src/llama.cpp/src/models/pangu-embedded.cpp +121 -0
  119. package/src/llama.cpp/src/models/phi2.cpp +121 -0
  120. package/src/llama.cpp/src/models/phi3.cpp +152 -0
  121. package/src/llama.cpp/src/models/plamo.cpp +110 -0
  122. package/src/llama.cpp/src/models/plamo2.cpp +316 -0
  123. package/src/llama.cpp/src/models/plm.cpp +168 -0
  124. package/src/llama.cpp/src/models/qwen.cpp +108 -0
  125. package/src/llama.cpp/src/models/qwen2.cpp +117 -0
  126. package/src/llama.cpp/src/models/qwen2moe.cpp +151 -0
  127. package/src/llama.cpp/src/models/qwen2vl.cpp +117 -0
  128. package/src/llama.cpp/src/models/qwen3.cpp +117 -0
  129. package/src/llama.cpp/src/models/qwen3moe.cpp +124 -0
  130. package/src/llama.cpp/src/models/qwen3vl-moe.cpp +149 -0
  131. package/src/llama.cpp/src/models/qwen3vl.cpp +141 -0
  132. package/src/llama.cpp/src/models/refact.cpp +94 -0
  133. package/src/llama.cpp/src/models/rwkv6-base.cpp +162 -0
  134. package/src/llama.cpp/src/models/rwkv6.cpp +94 -0
  135. package/src/llama.cpp/src/models/rwkv6qwen2.cpp +86 -0
  136. package/src/llama.cpp/src/models/rwkv7-base.cpp +135 -0
  137. package/src/llama.cpp/src/models/rwkv7.cpp +90 -0
  138. package/src/llama.cpp/src/models/seed-oss.cpp +124 -0
  139. package/src/llama.cpp/src/models/smallthinker.cpp +120 -0
  140. package/src/llama.cpp/src/models/smollm3.cpp +128 -0
  141. package/src/llama.cpp/src/models/stablelm.cpp +146 -0
  142. package/src/llama.cpp/src/models/starcoder.cpp +100 -0
  143. package/src/llama.cpp/src/models/starcoder2.cpp +121 -0
  144. package/src/llama.cpp/src/models/t5-dec.cpp +166 -0
  145. package/src/llama.cpp/src/models/t5-enc.cpp +96 -0
  146. package/src/llama.cpp/src/models/wavtokenizer-dec.cpp +149 -0
  147. package/src/llama.cpp/src/models/xverse.cpp +108 -0
@@ -0,0 +1,377 @@
1
+ #include "models.h"
2
+
3
+
4
+
5
+ llm_build_gemma3n_iswa::llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params) :
6
+ llm_graph_context(params),
7
+ model(model),
8
+ n_embd_head(model.hparams.n_embd_head_k),
9
+ n_embd_altup(model.hparams.n_embd_altup),
10
+ n_altup(model.hparams.n_altup),
11
+ i_altup_act(model.hparams.i_altup_act) {
12
+ ggml_tensor * cur;
13
+ ggml_tensor * inpL;
14
+
15
+ inpL = build_inp_embd(model.tok_embd);
16
+
17
+ // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
18
+ if (ubatch.token) {
19
+ inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
20
+ cb(inpL, "inp_scaled", -1);
21
+ }
22
+ // inp_pos - contains the positions
23
+ ggml_tensor * inp_pos = build_inp_pos();
24
+
25
+ // TODO: is causal == true correct? might need some changes
26
+ auto * inp_attn = build_attn_inp_kv_iswa();
27
+
28
+ // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
29
+ ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
30
+
31
+ // inpL now has only 1 altup, project it to the rest of the altups
32
+ // these "added" altups will be concat to the last dim of inpL
33
+ {
34
+ ggml_tensor * target_magnitude = calc_magnitude(inpL);
35
+ ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
36
+ ggml_tensor * altup_added =
37
+ ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
38
+ ggml_tensor * new_magnitude = calc_magnitude(altup_added);
39
+ altup_added = ggml_div(ctx0, ggml_mul(ctx0, altup_added, target_magnitude), new_magnitude);
40
+ inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
41
+ cb(inpL, "inp_stacked", -1);
42
+ }
43
+ // inpL now has shape: [n_embd, n_tokens, n_altup]
44
+ // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
45
+
46
+ for (int il = 0; il < n_layer; ++il) {
47
+ // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
48
+ const float freq_base_l = model.get_rope_freq_base(cparams, il);
49
+ const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
50
+
51
+ ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
52
+ ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
53
+
54
+ // predicted value will go through self-attention and laurel
55
+ ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
56
+ cur = active_prediction;
57
+ cb(cur, "active_prediction", il);
58
+
59
+ // norm
60
+ cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
61
+ cb(cur, "attn_norm", il);
62
+
63
+ // laurel
64
+ ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
65
+
66
+ // self-attention
67
+ if (hparams.has_kv(il)) {
68
+ // compute Q and K and RoPE them
69
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
70
+ cb(Qcur, "Qcur", il);
71
+
72
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
73
+ cb(Kcur, "Kcur", il);
74
+
75
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
76
+ cb(Vcur, "Vcur", il);
77
+
78
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
79
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
80
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
81
+
82
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
83
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
84
+ Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
85
+
86
+ cb(Qcur, "Qcur_normed", il);
87
+ cb(Kcur, "Kcur_normed", il);
88
+ cb(Vcur, "Vcur_normed", il);
89
+
90
+ Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
91
+ ext_factor, attn_factor, beta_fast, beta_slow);
92
+
93
+ Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
94
+ ext_factor, attn_factor, beta_fast, beta_slow);
95
+
96
+ cb(Qcur, "Qcur_pos", il);
97
+ cb(Kcur, "Kcur_pos", il);
98
+
99
+ cur = build_attn(inp_attn, model.layers[il].wo,
100
+ NULL, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,
101
+ hparams.f_attention_scale, il);
102
+ } else {
103
+ // reuse KV cache of earlier layers
104
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
105
+ cb(Qcur, "Qcur", il);
106
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
107
+
108
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
109
+ cb(Qcur, "Qcur_normed", il);
110
+
111
+ Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
112
+ ext_factor, attn_factor, beta_fast, beta_slow);
113
+ cb(Qcur, "Qcur_pos", il);
114
+
115
+ cur = build_attn(inp_attn,
116
+ model.layers[il].wo, NULL,
117
+ Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
118
+ }
119
+ cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
120
+ cb(cur, "attn_post_norm", il);
121
+
122
+ cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
123
+ cb(cur, "attn_gated", il);
124
+
125
+ ggml_tensor * attn_laurel = ggml_scale(ctx0, ggml_add(ctx0, cur, laurel_out),
126
+ 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
127
+ cb(attn_laurel, "attn_laurel", il);
128
+
129
+ cur = build_norm(attn_laurel, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
130
+ cb(cur, "ffn_norm", il);
131
+
132
+ // feed-forward network
133
+ {
134
+ ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
135
+ ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
136
+
137
+ if (il < n_layer_sparsity) {
138
+ // apply activation sparsity
139
+ gate_proj = gaussian_topk(gate_proj);
140
+ }
141
+ gate_proj = ggml_gelu(ctx0, gate_proj);
142
+
143
+ cur = ggml_mul(ctx0, up_proj, gate_proj);
144
+ cur = build_lora_mm(model.layers[il].ffn_down, cur);
145
+ cb(cur, "ffn_out", il);
146
+ }
147
+ cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, -1);
148
+ cb(cur, "ffn_post_norm", il);
149
+
150
+ ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
151
+ cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
152
+
153
+ ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
154
+
155
+ ggml_tensor * first_prediction; // [n_embd, n_tokens]
156
+ {
157
+ first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
158
+ first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
159
+ first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
160
+ first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
161
+ cb(first_prediction, "first_prediction_gated", il);
162
+ ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
163
+ first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
164
+ cb(first_prediction, "first_prediction_scaled", il);
165
+
166
+ first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
167
+ first_prediction =
168
+ build_norm(first_prediction, model.layers[il].per_layer_post_norm, NULL, LLM_NORM_RMS, il);
169
+ cb(first_prediction, "first_prediction_out", il);
170
+ }
171
+ // equivalent to python code: corrected_predictions[1:] += first_prediction
172
+ {
173
+ ggml_tensor * slice_first = view_2d_slice(corrected, 0);
174
+ ggml_tensor * slice_rest = ggml_view_3d(
175
+ ctx0, corrected, n_embd, n_tokens, n_altup - 1, ggml_row_size(corrected->type, n_embd),
176
+ ggml_row_size(corrected->type, n_embd * n_tokens), n_embd * n_tokens * ggml_element_size(corrected));
177
+ ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
178
+ corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
179
+ }
180
+ cur = corrected; // [n_embd, n_tokens, n_altup]
181
+ cur = build_cvec(cur, il);
182
+ cb(cur, "l_out", il);
183
+
184
+ // input for next layer
185
+ inpL = cur;
186
+ }
187
+ cur = inpL; // [n_embd, n_tokens, n_altup]
188
+
189
+ // cur now has multiple altup(s), we want to merge them back to 1 altup
190
+ {
191
+ ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
192
+ // do a view to skip the first slice (active altup)
193
+ ggml_tensor * alt_slice =
194
+ ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1, ggml_row_size(cur->type, n_embd),
195
+ ggml_row_size(cur->type, n_embd * n_tokens), n_embd * n_tokens * ggml_element_size(cur));
196
+ ggml_tensor * altup_unembd =
197
+ ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
198
+ ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
199
+ altup_unembd = ggml_div(ctx0, ggml_mul(ctx0, altup_unembd, target_magnitude), new_magnitude);
200
+ cb(altup_unembd, "altup_unembd", -1);
201
+
202
+ // equivalent to torch.mean(hidden_states, dim=0)
203
+ cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
204
+ for (int i = 0; i < n_altup - 1; ++i) {
205
+ cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
206
+ }
207
+ cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
208
+ cb(cur, "unembd_merged", -1);
209
+ }
210
+ // cur now has shape: [n_embd, n_tokens]
211
+
212
+ // TODO: move this to right after the last KV layer
213
+ {
214
+ // skip computing output for unused tokens
215
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
216
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
217
+ }
218
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
219
+
220
+ cb(cur, "result_norm", -1);
221
+ res->t_embd = cur;
222
+
223
+ cur = build_lora_mm(model.output, cur);
224
+
225
+ {
226
+ // final logit soft-capping
227
+ cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
228
+ cur = ggml_tanh(ctx0, cur);
229
+ cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
230
+ }
231
+ cb(cur, "result_output", -1);
232
+ res->t_logits = cur;
233
+
234
+ ggml_build_forward_expand(gf, cur);
235
+ }
236
+
237
+ ggml_tensor * llm_build_gemma3n_iswa::calc_magnitude(ggml_tensor * x) {
238
+ return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
239
+ }
240
+
241
+ // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
242
+ ggml_tensor * llm_build_gemma3n_iswa::view_2d_slice(ggml_tensor * x, int idx) {
243
+ GGML_ASSERT(idx < (int) x->ne[2]);
244
+ return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1], ggml_row_size(x->type, x->ne[0]),
245
+ idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
246
+ }
247
+
248
+ // equivalent to get_per_layer_inputs() in python code
249
+ // output shape: [n_embd_altup, n_layer, n_tokens]
250
+ ggml_tensor * llm_build_gemma3n_iswa::get_per_layer_inputs() {
251
+ auto inp = std::make_unique<llm_graph_input_embd>();
252
+ ggml_tensor * inp_per_layer;
253
+ if (ubatch.token) {
254
+ inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
255
+ ggml_set_input(inp->tokens);
256
+ res->t_tokens = inp->tokens;
257
+ inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
258
+ inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
259
+ inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float) n_embd_altup));
260
+ cb(inp_per_layer, "inp_per_layer_selected", -1);
261
+ } else {
262
+ GGML_ABORT("TODO: support embd input");
263
+ }
264
+ res->add_input(std::move(inp));
265
+ return inp_per_layer;
266
+ }
267
+
268
+ // equivalent to project_per_layer_inputs() in python code
269
+ // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
270
+ // output shape: [n_embd_altup, n_tokens, n_layer]
271
+ ggml_tensor * llm_build_gemma3n_iswa::project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
272
+ const float per_layer_projection_scale = 1.0f / sqrtf((float) n_embd);
273
+ const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
274
+
275
+ ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
276
+ per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
277
+ per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
278
+ per_layer_proj = build_norm(per_layer_proj, model.per_layer_proj_norm, NULL, LLM_NORM_RMS,
279
+ -1); // [n_embd_altup, n_layer, n_tokens]
280
+ cb(per_layer_proj, "per_layer_proj", -1);
281
+
282
+ inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
283
+ inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
284
+ cb(inp_per_layer, "inp_per_layer", -1);
285
+
286
+ // permute to shape: [n_embd_altup, n_tokens, n_layer]
287
+ inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
288
+ return inp_per_layer;
289
+ }
290
+
291
+ // input cur shape: [n_altup, n_tokens]
292
+ // output shape: [n_altup, n_tokens]
293
+ ggml_tensor * llm_build_gemma3n_iswa::laurel(ggml_tensor * cur, int il) {
294
+ ggml_tensor * tmp = cur;
295
+ tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
296
+ tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
297
+ tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
298
+ tmp = ggml_add(ctx0, tmp, cur);
299
+ cb(tmp, "laurel_out", il);
300
+ return tmp;
301
+ }
302
+
303
+ // input x shape: [n_embd, n_tokens]
304
+ // output shape: [n_embd, n_tokens]
305
+ ggml_tensor * llm_build_gemma3n_iswa::gaussian_topk(ggml_tensor * x) {
306
+ ggml_tensor * mean = ggml_mean(ctx0, x);
307
+ ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
308
+ 1.0f / (float) (x->ne[0] - 1)));
309
+ ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
310
+ return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
311
+ }
312
+
313
+ //
314
+ // altup functions
315
+ //
316
+
317
+ // equivalent to compute_router_modalities() in python code
318
+ // input x shape: [n_embd, n_tokens]
319
+ // output shape: [n_altup, n_tokens]
320
+ ggml_tensor * llm_build_gemma3n_iswa::altup_compute_router_modalities(ggml_tensor * x, int il) {
321
+ ggml_tensor * router_inputs = build_norm(x, model.layers[il].altup_router_norm, NULL, LLM_NORM_RMS, il);
322
+
323
+ // router_input_scale
324
+ router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float) n_embd);
325
+
326
+ ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
327
+ return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
328
+ }
329
+
330
+ // input cur shape: [n_embd, n_tokens, n_altup]
331
+ // output shape: [n_embd, n_tokens, n_altup]
332
+ ggml_tensor * llm_build_gemma3n_iswa::altup_predict(ggml_tensor * cur, int il) {
333
+ ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
334
+ ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
335
+ cb(modalities, "modalities", il);
336
+
337
+ ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
338
+ cb(all_coefs, "all_coefs", il);
339
+ // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
340
+ all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
341
+
342
+ // permute to [n_altup, n_embd, n_tokens]
343
+ ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
344
+ ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
345
+
346
+ // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
347
+ predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
348
+ predictions = ggml_add(ctx0, predictions, cur);
349
+ cb(predictions, "predictions", il);
350
+
351
+ return predictions;
352
+ }
353
+
354
+ // input predictions shape: [n_embd, n_tokens, n_altup]
355
+ // input activated shape: [n_embd, n_tokens]
356
+ // output shape: [n_embd, n_tokens, n_altup]
357
+ ggml_tensor * llm_build_gemma3n_iswa::altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
358
+ ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
359
+ cb(modalities, "modalities", il);
360
+
361
+ ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
362
+ ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
363
+ cb(innovation, "innovation", il);
364
+
365
+ ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
366
+ all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
367
+ cb(all_coefs, "all_coefs", il);
368
+ all_coefs = ggml_transpose(ctx0, all_coefs); // [n_tokens, n_altup]
369
+ all_coefs = ggml_cont_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
370
+
371
+ innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
372
+ ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
373
+ corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
374
+ cb(corrected, "corrected", il);
375
+
376
+ return corrected;
377
+ }
@@ -0,0 +1,153 @@
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(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(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
28
+ cb(cur, "attn_norm", il);
29
+
30
+ // self-attention
31
+ {
32
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
33
+ if (model.layers[il].bq) {
34
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
35
+ }
36
+ cb(Qcur, "Qcur", il);
37
+
38
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
39
+ if (model.layers[il].bk) {
40
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
41
+ }
42
+ cb(Kcur, "Kcur", il);
43
+
44
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
45
+ if (model.layers[il].bv) {
46
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
47
+ }
48
+ cb(Vcur, "Vcur", il);
49
+
50
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
51
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
52
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, 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(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
57
+ cb(Qcur, "Qcur_normed", il);
58
+ }
59
+ if (model.layers[il].attn_k_norm) {
60
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
61
+ cb(Kcur, "Kcur_normed", il);
62
+ }
63
+ Qcur = ggml_rope_ext(
64
+ ctx0, Qcur, inp_pos, nullptr,
65
+ n_rot, 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
+ ctx0, Kcur, inp_pos, nullptr,
71
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
72
+ ext_factor, attn_factor, beta_fast, beta_slow
73
+ );
74
+
75
+ cb(Qcur, "Qcur", il);
76
+ cb(Kcur, "Kcur", il);
77
+ cb(Vcur, "Vcur", il);
78
+
79
+ cur = build_attn(inp_attn,
80
+ model.layers[il].wo, NULL,
81
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
82
+ }
83
+ if (il == n_transformer_layers - 1 && inp_out_ids) {
84
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
85
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
86
+ }
87
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
88
+ cb(ffn_inp, "ffn_inp", il);
89
+
90
+ // Post-attention norm
91
+ cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
92
+ cb(cur, "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
+ model.layers[il].ffn_up, NULL, NULL,
99
+ model.layers[il].ffn_gate, NULL, NULL,
100
+ model.layers[il].ffn_down, NULL, NULL,
101
+ NULL,
102
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
103
+ cb(cur, "ffn_out", il);
104
+ } else {
105
+ // Process routed experts using existing MoE infrastructure
106
+ ggml_tensor * routed_out = build_moe_ffn(cur,
107
+ model.layers[il].ffn_gate_inp,
108
+ model.layers[il].ffn_up_exps,
109
+ model.layers[il].ffn_gate_exps,
110
+ model.layers[il].ffn_down_exps,
111
+ model.layers[il].ffn_exp_probs_b,
112
+ n_expert, n_expert_used,
113
+ LLM_FFN_SILU, hparams.expert_weights_norm,
114
+ true, hparams.expert_weights_scale,
115
+ (llama_expert_gating_func_type) hparams.expert_gating_func,
116
+ il);
117
+ cb(routed_out, "ffn_moe_out", il);
118
+
119
+ // Process shared expert on original input
120
+ ggml_tensor * shared_out = build_ffn(cur,
121
+ model.layers[il].ffn_up_shexp, NULL, NULL,
122
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
123
+ model.layers[il].ffn_down_shexp, NULL, NULL,
124
+ NULL,
125
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
126
+ cb(shared_out, "ffn_shexp_out", il);
127
+
128
+ // Final output: routed_output + shared_output
129
+ cur = ggml_add(ctx0, routed_out, shared_out);
130
+ cb(cur, "ffn_out", il);
131
+ }
132
+ cur = ggml_add(ctx0, cur, ffn_inp);
133
+
134
+ cur = build_cvec(cur, il);
135
+ cb(cur, "l_out", il);
136
+
137
+ // input for next layer
138
+ inpL = cur;
139
+ }
140
+ cur = inpL;
141
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
142
+
143
+ cb(cur, "result_norm", -1);
144
+ res->t_embd = cur;
145
+
146
+ // lm_head
147
+ cur = build_lora_mm(model.output, cur);
148
+
149
+ cb(cur, "result_output", -1);
150
+ res->t_logits = cur;
151
+
152
+ ggml_build_forward_expand(gf, cur);
153
+ }
@@ -0,0 +1,127 @@
1
+ #include "models.h"
2
+
3
+
4
+
5
+ llm_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(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(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
28
+ cb(cur, "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(model.layers[il].wq, cur);
38
+ if (model.layers[il].bq) {
39
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
40
+ }
41
+ Kcur = build_lora_mm(model.layers[il].wk, cur);
42
+ if (model.layers[il].bk) {
43
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
44
+ }
45
+ Vcur = build_lora_mm(model.layers[il].wv, cur);
46
+ if (model.layers[il].bv) {
47
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
48
+ }
49
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
50
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
51
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
52
+ } else {
53
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
54
+ cb(cur, "wqkv", il);
55
+ if (model.layers[il].bqkv) {
56
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
57
+ cb(cur, "bqkv", il);
58
+ }
59
+ Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), cur->nb[1],
60
+ 0 * sizeof(float) * (n_embd));
61
+ Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
62
+ cur->nb[1], 1 * sizeof(float) * (n_embd));
63
+ Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
64
+ cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa));
65
+ }
66
+ Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
67
+ ext_factor, attn_factor, beta_fast, beta_slow);
68
+
69
+ Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
70
+ ext_factor, attn_factor, beta_fast, beta_slow);
71
+
72
+ cb(Qcur, "Qcur", il);
73
+ cb(Kcur, "Kcur", il);
74
+ cb(Vcur, "Vcur", il);
75
+
76
+ cur = build_attn(inp_attn,
77
+ model.layers[il].wo, NULL,
78
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
79
+ }
80
+ if (il == n_layer - 1 && inp_out_ids) {
81
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
82
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
83
+ }
84
+ // Post-attention norm (new!)
85
+ cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
86
+ cb(cur, "post_attn_norm", il);
87
+
88
+ // Add the input (residual connection after post-attention norm)
89
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
90
+ cb(ffn_inp, "ffn_inp", il);
91
+
92
+ // FF
93
+ {
94
+ // Pre-MLP norm
95
+ cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
96
+ cb(cur, "ffn_norm", il);
97
+
98
+ // MLP
99
+ cur = build_ffn(cur,
100
+ model.layers[il].ffn_up, NULL, NULL,
101
+ NULL, NULL, NULL,
102
+ model.layers[il].ffn_down, NULL, NULL,
103
+ NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
104
+ cb(cur, "ffn_out", il);
105
+
106
+ // Post-MLP norm
107
+ cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
108
+ cb(cur, "post_mlp_norm", il);
109
+ }
110
+ // Add residual connection after post-MLP norm
111
+ inpL = ggml_add(ctx0, cur, ffn_inp);
112
+ cb(inpL, "l_out", il);
113
+ }
114
+ // Final norm
115
+ cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
116
+
117
+ cb(cur, "result_norm", -1);
118
+ res->t_embd = cur;
119
+
120
+ // Output projection
121
+ cur = build_lora_mm(model.output, cur);
122
+
123
+ cb(cur, "result_output", -1);
124
+ res->t_logits = cur;
125
+
126
+ ggml_build_forward_expand(gf, cur);
127
+ }