@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,173 @@
1
+ #include "models.h"
2
+
3
+ #include "../llama-memory-hybrid.h"
4
+
5
+
6
+ llm_build_lfm2::llm_build_lfm2(const llama_model & model, const llm_graph_params & params) :
7
+ llm_graph_context(params),
8
+ model(model) {
9
+ ggml_tensor * cur = build_inp_embd(model.tok_embd);
10
+ cb(cur, "model.embed_tokens", -1);
11
+
12
+ ggml_tensor * inp_pos = build_inp_pos();
13
+ auto * inp_hybrid = build_inp_mem_hybrid();
14
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
15
+
16
+ for (int il = 0; il < n_layer; ++il) {
17
+ const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
18
+
19
+ auto * prev_cur = cur;
20
+ cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
21
+ cb(cur, "model.layers.{}.operator_norm", il);
22
+
23
+ cur = hparams.is_recurrent(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
24
+ build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il);
25
+
26
+ if (il == n_layer - 1 && inp_out_ids) {
27
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
28
+ prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
29
+ }
30
+
31
+ cur = ggml_add(ctx0, prev_cur, cur);
32
+
33
+ auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
34
+ cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);
35
+
36
+ ggml_tensor * ffn_out =
37
+ is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il);
38
+ cb(ffn_norm_out, "model.layers.{}.ffn_out", il);
39
+
40
+ cur = ggml_add(ctx0, cur, ffn_out);
41
+ }
42
+
43
+ cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
44
+ cb(cur, "model.embedding_norm", -1);
45
+ res->t_embd = cur;
46
+
47
+ cur = build_lora_mm(model.output, cur);
48
+ cb(cur, "lm_head", -1);
49
+
50
+ res->t_logits = cur;
51
+
52
+ ggml_build_forward_expand(gf, cur);
53
+ }
54
+
55
+ ggml_tensor * llm_build_lfm2::build_moe_feed_forward(ggml_tensor * cur, int il) const {
56
+ return build_moe_ffn(cur,
57
+ model.layers[il].ffn_gate_inp, model.layers[il].ffn_up_exps,
58
+ model.layers[il].ffn_gate_exps, model.layers[il].ffn_down_exps,
59
+ model.layers[il].ffn_exp_probs_b, n_expert, n_expert_used, LLM_FFN_SILU, true, false, 0.0,
60
+ static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func), il);
61
+ }
62
+
63
+ ggml_tensor * llm_build_lfm2::build_dense_feed_forward(ggml_tensor * cur, int il) const {
64
+ GGML_ASSERT(!model.layers[il].ffn_up_b);
65
+ GGML_ASSERT(!model.layers[il].ffn_gate_b);
66
+ GGML_ASSERT(!model.layers[il].ffn_down_b);
67
+ return build_ffn(cur,
68
+ model.layers[il].ffn_up, NULL, NULL,
69
+ model.layers[il].ffn_gate, NULL, NULL,
70
+ model.layers[il].ffn_down, NULL, NULL,
71
+ NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
72
+ }
73
+
74
+ ggml_tensor * llm_build_lfm2::build_attn_block(ggml_tensor * cur,
75
+ ggml_tensor * inp_pos,
76
+ llm_graph_input_attn_kv * inp_attn,
77
+ int il) const {
78
+ GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
79
+ const auto n_embd_head = hparams.n_embd_head_v;
80
+ const auto n_head_kv = hparams.n_head_kv(il);
81
+
82
+ auto * q = build_lora_mm(model.layers[il].wq, cur);
83
+ cb(q, "model.layers.{}.self_attn.q_proj", il);
84
+ auto * k = build_lora_mm(model.layers[il].wk, cur);
85
+ cb(k, "model.layers.{}.self_attn.k_proj", il);
86
+ auto * v = build_lora_mm(model.layers[il].wv, cur);
87
+ cb(v, "model.layers.{}.self_attn.v_proj", il);
88
+
89
+ q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
90
+ k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
91
+ v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
92
+
93
+ // qk norm
94
+ q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
95
+ cb(q, "model.layers.{}.self_attn.q_layernorm", il);
96
+ k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
97
+ cb(k, "model.layers.{}.self_attn.k_layernorm", il);
98
+
99
+ // RoPE
100
+ q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
101
+ attn_factor, beta_fast, beta_slow);
102
+ k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
103
+ attn_factor, beta_fast, beta_slow);
104
+
105
+ cur = build_attn(inp_attn,
106
+ model.layers[il].wo, NULL,
107
+ q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
108
+
109
+ cb(cur, "model.layers.{}.self_attn.out_proj", il);
110
+
111
+ return cur;
112
+ }
113
+
114
+ ggml_tensor * llm_build_lfm2::build_shortconv_block(ggml_tensor * cur, llm_graph_input_rs * inp_recr, int il) {
115
+ const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr();
116
+ const uint32_t kv_head = mctx_cur->get_head();
117
+ const int64_t n_seq_tokens = ubatch.n_seq_tokens;
118
+ const int64_t n_seqs = ubatch.n_seqs;
119
+ GGML_ASSERT(n_seqs != 0);
120
+ GGML_ASSERT(ubatch.equal_seqs());
121
+ GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
122
+
123
+ GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
124
+ const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
125
+
126
+ // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
127
+ cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
128
+
129
+ auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
130
+ cb(bcx, "model.layers.{}.conv.in_proj", il);
131
+
132
+ constexpr auto n_chunks = 3;
133
+ GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
134
+ const auto chunk_size = bcx->ne[0] / n_chunks;
135
+ auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
136
+ 0 * chunk_size * ggml_element_size(bcx));
137
+ auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
138
+ 1 * chunk_size * ggml_element_size(bcx));
139
+ auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
140
+ 2 * chunk_size * ggml_element_size(bcx));
141
+
142
+ auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
143
+
144
+ // read conv state
145
+ auto * conv_state = mctx_cur->get_r_l(il);
146
+ auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
147
+ auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
148
+
149
+ bx = ggml_concat(ctx0, conv, bx, 0);
150
+ GGML_ASSERT(bx->ne[0] > conv->ne[0]);
151
+
152
+ // last d_conv columns is a new conv state
153
+ auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2],
154
+ (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx));
155
+ GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
156
+
157
+ // write new conv conv state
158
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv,
159
+ ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv),
160
+ kv_head * d_conv * n_embd * ggml_element_size(new_conv))));
161
+
162
+ auto * conv_kernel = model.layers[il].shortconv.conv;
163
+ auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);
164
+ cb(conv_out, "model.layers.{}.conv.conv", il);
165
+
166
+ auto * y = ggml_mul(ctx0, c, conv_out);
167
+ y = build_lora_mm(model.layers[il].shortconv.out_proj, y);
168
+ cb(y, "model.layers.{}.conv.out_proj", il);
169
+ // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
170
+ y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
171
+
172
+ return y;
173
+ }
@@ -0,0 +1,122 @@
1
+ #include "models.h"
2
+
3
+ llm_build_llada_moe::llm_build_llada_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
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ // inp_pos - contains the positions
15
+ ggml_tensor * inp_pos = build_inp_pos();
16
+
17
+ auto * inp_attn = build_attn_inp_no_cache();
18
+
19
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
20
+
21
+ for (int il = 0; il < n_layer; ++il) {
22
+ ggml_tensor * inpSA = inpL;
23
+
24
+ // norm
25
+ cur = build_norm(inpL,
26
+ model.layers[il].attn_norm, NULL,
27
+ LLM_NORM_RMS, il);
28
+ cb(cur, "attn_norm", il);
29
+
30
+ // self_attention
31
+ {
32
+ // compute Q and K and RoPE them
33
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
34
+ cb(Qcur, "Qcur", il);
35
+
36
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
37
+ cb(Kcur, "Kcur", il);
38
+
39
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
40
+ cb(Vcur, "Vcur", il);
41
+
42
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
43
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
44
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
45
+
46
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
47
+ cb(Qcur, "Qcur_normed", il);
48
+
49
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
50
+ cb(Kcur, "Kcur_normed", il);
51
+
52
+ Qcur = ggml_rope_ext(
53
+ ctx0, Qcur, inp_pos, nullptr,
54
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
55
+ ext_factor, attn_factor, beta_fast, beta_slow
56
+ );
57
+
58
+ Kcur = ggml_rope_ext(
59
+ ctx0, Kcur, inp_pos, nullptr,
60
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
61
+ ext_factor, attn_factor, beta_fast, beta_slow
62
+ );
63
+
64
+ cb(Qcur, "Qcur", il);
65
+ cb(Kcur, "Kcur", il);
66
+ cb(Vcur, "Vcur", il);
67
+
68
+ cur = build_attn(inp_attn,
69
+ model.layers[il].wo, NULL,
70
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
71
+ }
72
+ if (il == n_layer - 1 && inp_out_ids) {
73
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
74
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
75
+ }
76
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
77
+ cb(ffn_inp, "ffn_inp", il);
78
+
79
+ // MoE branch
80
+ cur = build_norm(ffn_inp,
81
+ model.layers[il].ffn_norm, NULL,
82
+ LLM_NORM_RMS, il);
83
+ cb(cur, "ffn_norm", il);
84
+
85
+ cur = build_moe_ffn(cur,
86
+ model.layers[il].ffn_gate_inp,
87
+ model.layers[il].ffn_up_exps,
88
+ model.layers[il].ffn_gate_exps,
89
+ model.layers[il].ffn_down_exps,
90
+ nullptr,
91
+ n_expert, n_expert_used,
92
+ LLM_FFN_SILU, false,
93
+ false, 0.0,
94
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
95
+ il);
96
+ cb(cur, "ffn_moe_out", il);
97
+
98
+ cur = ggml_add(ctx0, cur, ffn_inp);
99
+
100
+ cur = build_cvec(cur, il);
101
+ cb(cur, "l_out", il);
102
+
103
+ // input for next layer
104
+ inpL = cur;
105
+ }
106
+ cur = inpL;
107
+
108
+ cur = build_norm(cur,
109
+ model.output_norm, NULL,
110
+ LLM_NORM_RMS, -1);
111
+
112
+ cb(cur, "result_norm", -1);
113
+ res->t_embd = cur;
114
+
115
+ // lm_head
116
+ cur = build_lora_mm(model.output, cur);
117
+
118
+ cb(cur, "result_output", -1);
119
+ res->t_logits = cur;
120
+
121
+ ggml_build_forward_expand(gf, cur);
122
+ }
@@ -0,0 +1,99 @@
1
+ #include "models.h"
2
+
3
+ llm_build_llada::llm_build_llada(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
5
+ const int64_t n_embd_head = hparams.n_embd_head_v;
6
+
7
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
9
+
10
+ ggml_tensor * cur;
11
+ ggml_tensor * inpL;
12
+
13
+ inpL = build_inp_embd(model.tok_embd);
14
+
15
+ // inp_pos - contains the positions
16
+ ggml_tensor * inp_pos = build_inp_pos();
17
+
18
+ // Non-causal attention for diffusion
19
+ auto * inp_attn = build_attn_inp_no_cache();
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
+ // 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
+ // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
33
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
34
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
35
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
36
+
37
+ cb(Qcur, "Qcur", il);
38
+ cb(Kcur, "Kcur", il);
39
+ cb(Vcur, "Vcur", il);
40
+
41
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
42
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
43
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
44
+
45
+ Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
46
+ ext_factor, attn_factor, beta_fast, beta_slow);
47
+
48
+ Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
49
+ ext_factor, attn_factor, beta_fast, beta_slow);
50
+
51
+ cb(Qcur, "Qcur", il);
52
+ cb(Kcur, "Kcur", il);
53
+ cb(Vcur, "Vcur", il);
54
+
55
+ cur = build_attn(inp_attn,
56
+ model.layers[il].wo, NULL,
57
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
58
+ }
59
+ if (il == n_layer - 1 && inp_out_ids) {
60
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
61
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
62
+ }
63
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
64
+ cb(ffn_inp, "ffn_inp", il);
65
+
66
+ // feed-forward network
67
+ cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
68
+ cb(cur, "ffn_norm", il);
69
+
70
+ cur = build_ffn(cur,
71
+ model.layers[il].ffn_up, NULL, NULL,
72
+ model.layers[il].ffn_gate, NULL, NULL,
73
+ model.layers[il].ffn_down, NULL, NULL,
74
+ NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
75
+ cb(cur, "ffn_out", il);
76
+
77
+ cur = ggml_add(ctx0, cur, ffn_inp);
78
+
79
+ cur = build_cvec(cur, il);
80
+ cb(cur, "l_out", il);
81
+
82
+ // input for next layer
83
+ inpL = cur;
84
+ }
85
+ cur = inpL;
86
+
87
+ cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
88
+
89
+ cb(cur, "result_norm", -1);
90
+ res->t_embd = cur;
91
+
92
+ // lm_head
93
+ cur = build_lora_mm(model.output, cur);
94
+
95
+ cb(cur, "result_output", -1);
96
+ res->t_logits = cur;
97
+
98
+ ggml_build_forward_expand(gf, cur);
99
+ }
@@ -0,0 +1,174 @@
1
+ #include "models.h"
2
+
3
+ llm_build_llama_iswa::llm_build_llama_iswa(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
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ // inp_pos - contains the positions
15
+ ggml_tensor * inp_pos = build_inp_pos();
16
+
17
+ // temperature tuning
18
+ ggml_tensor * inp_attn_scale = nullptr;
19
+ inp_attn_scale = build_inp_attn_scale();
20
+
21
+ auto * inp_attn = build_attn_inp_kv_iswa();
22
+
23
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
24
+
25
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
26
+
27
+ for (int il = 0; il < n_layer; ++il) {
28
+ ggml_tensor * inpSA = inpL;
29
+
30
+ const bool use_rope = hparams.n_no_rope_layer_step > 0 &&
31
+ (il + 1) % hparams.n_no_rope_layer_step != 0;
32
+
33
+ // norm
34
+ cur = build_norm(inpL,
35
+ model.layers[il].attn_norm, NULL,
36
+ LLM_NORM_RMS, il);
37
+ cb(cur, "attn_norm", il);
38
+
39
+ // self-attention
40
+ {
41
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
42
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
43
+
44
+ // compute Q and K and RoPE them
45
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
46
+ cb(Qcur, "Qcur", il);
47
+ if (model.layers[il].bq) {
48
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
49
+ cb(Qcur, "Qcur", il);
50
+ }
51
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
52
+ cb(Kcur, "Kcur", il);
53
+ if (model.layers[il].bk) {
54
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
55
+ cb(Kcur, "Kcur", il);
56
+ }
57
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
58
+ cb(Vcur, "Vcur", il);
59
+ if (model.layers[il].bv) {
60
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
61
+ cb(Vcur, "Vcur", il);
62
+ }
63
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
64
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
65
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
66
+
67
+ if (use_rope) {
68
+ Qcur = ggml_rope_ext(
69
+ ctx0, Qcur, inp_pos, rope_factors,
70
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
71
+ ext_factor, attn_factor, beta_fast, beta_slow
72
+ );
73
+
74
+ Kcur = ggml_rope_ext(
75
+ ctx0, Kcur, inp_pos, rope_factors,
76
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
77
+ ext_factor, attn_factor, beta_fast, beta_slow
78
+ );
79
+ } else if (inp_attn_scale) {
80
+ Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
81
+ }
82
+ cb(Qcur, "Qcur", il);
83
+ cb(Kcur, "Kcur", il);
84
+ cb(Vcur, "Vcur", il);
85
+
86
+ if (use_rope && hparams.use_kq_norm) {
87
+ // Llama4TextL2Norm
88
+ Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
89
+ Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
90
+ cb(Qcur, "Qcur_normed", il);
91
+ cb(Kcur, "Kcur_normed", il);
92
+ }
93
+ cur = build_attn(inp_attn,
94
+ model.layers[il].wo, model.layers[il].bo,
95
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
96
+ cb(cur, "attn_out", il);
97
+ }
98
+ if (il == n_layer - 1 && inp_out_ids) {
99
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
100
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
101
+ }
102
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
103
+ cb(ffn_inp, "ffn_inp", il);
104
+
105
+ // feed-forward network (non-MoE)
106
+ if (model.layers[il].ffn_gate_inp == nullptr) {
107
+ cur = build_norm(ffn_inp,
108
+ model.layers[il].ffn_norm, NULL,
109
+ LLM_NORM_RMS, il);
110
+ cb(cur, "ffn_norm", il);
111
+
112
+ cur = build_ffn(cur,
113
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
114
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
115
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
116
+ NULL,
117
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
118
+ cb(cur, "ffn_out", il);
119
+ } else {
120
+ ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
121
+ model.layers[il].ffn_norm, NULL,
122
+ LLM_NORM_RMS, il);
123
+ cb(cur, "ffn_norm", il);
124
+
125
+ ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
126
+ model.layers[il].ffn_gate_inp,
127
+ model.layers[il].ffn_up_exps,
128
+ model.layers[il].ffn_gate_exps,
129
+ model.layers[il].ffn_down_exps,
130
+ nullptr,
131
+ n_expert, n_expert_used,
132
+ LLM_FFN_SILU, false,
133
+ false, 0.0,
134
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
135
+ il);
136
+
137
+ // Shared experts
138
+ ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
139
+ model.layers[il].ffn_up_shexp, NULL, NULL,
140
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
141
+ model.layers[il].ffn_down_shexp, NULL, NULL,
142
+ NULL,
143
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
144
+ cb(shexp_out, "ffn_moe_shexp", il);
145
+
146
+ cur = ggml_add(ctx0, moe_out, shexp_out);
147
+ cb(cur, "ffn_moe_out_merged", il);
148
+ }
149
+ cur = ggml_add(ctx0, cur, ffn_inp);
150
+ cb(cur, "ffn_out", il);
151
+
152
+ cur = build_cvec(cur, il);
153
+ cb(cur, "l_out", il);
154
+
155
+ // input for next layer
156
+ inpL = cur;
157
+ }
158
+ cur = inpL;
159
+
160
+ cur = build_norm(cur,
161
+ model.output_norm, NULL,
162
+ LLM_NORM_RMS, -1);
163
+
164
+ cb(cur, "result_norm", -1);
165
+ res->t_embd = cur;
166
+
167
+ // lm_head
168
+ cur = build_lora_mm(model.output, cur);
169
+
170
+ cb(cur, "result_output", -1);
171
+ res->t_logits = cur;
172
+
173
+ ggml_build_forward_expand(gf, cur);
174
+ }