@fugood/llama.node 1.3.0 → 1.3.2

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 (143) hide show
  1. package/package.json +14 -14
  2. package/scripts/llama.cpp.patch +8 -8
  3. package/src/llama.cpp/common/CMakeLists.txt +2 -0
  4. package/src/llama.cpp/common/arg.cpp +44 -999
  5. package/src/llama.cpp/common/arg.h +2 -2
  6. package/src/llama.cpp/common/chat.cpp +17 -2
  7. package/src/llama.cpp/common/common.cpp +33 -0
  8. package/src/llama.cpp/common/common.h +15 -1
  9. package/src/llama.cpp/common/download.cpp +1054 -0
  10. package/src/llama.cpp/common/download.h +55 -0
  11. package/src/llama.cpp/ggml/CMakeLists.txt +1 -1
  12. package/src/llama.cpp/ggml/include/ggml.h +2 -0
  13. package/src/llama.cpp/ggml/src/CMakeLists.txt +6 -3
  14. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +29 -11
  15. package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +428 -26
  16. package/src/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +4 -5
  17. package/src/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +108 -49
  18. package/src/llama.cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
  19. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +3 -1
  20. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +21 -21
  21. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +172 -75
  22. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +0 -4
  23. package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +82 -21
  24. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +25 -25
  25. package/src/llama.cpp/include/llama.h +7 -3
  26. package/src/llama.cpp/src/CMakeLists.txt +95 -0
  27. package/src/llama.cpp/src/llama-arch.cpp +108 -0
  28. package/src/llama.cpp/src/llama-arch.h +11 -0
  29. package/src/llama.cpp/src/llama-batch.cpp +63 -31
  30. package/src/llama.cpp/src/llama-batch.h +12 -1
  31. package/src/llama.cpp/src/llama-chat.cpp +32 -0
  32. package/src/llama.cpp/src/llama-chat.h +1 -0
  33. package/src/llama.cpp/src/llama-context.cpp +36 -13
  34. package/src/llama.cpp/src/llama-context.h +5 -5
  35. package/src/llama.cpp/src/llama-cparams.h +1 -0
  36. package/src/llama.cpp/src/llama-graph.cpp +3 -3
  37. package/src/llama.cpp/src/llama-hparams.cpp +11 -1
  38. package/src/llama.cpp/src/llama-hparams.h +6 -0
  39. package/src/llama.cpp/src/llama-kv-cache-iswa.cpp +3 -1
  40. package/src/llama.cpp/src/llama-kv-cache.cpp +33 -1
  41. package/src/llama.cpp/src/llama-kv-cells.h +44 -2
  42. package/src/llama.cpp/src/llama-memory-recurrent.cpp +4 -3
  43. package/src/llama.cpp/src/llama-model.cpp +320 -13171
  44. package/src/llama.cpp/src/llama-model.h +8 -0
  45. package/src/llama.cpp/src/llama-quant.cpp +1 -1
  46. package/src/llama.cpp/src/llama-vocab.cpp +5 -0
  47. package/src/llama.cpp/src/llama-vocab.h +1 -0
  48. package/src/llama.cpp/src/models/apertus.cpp +125 -0
  49. package/src/llama.cpp/src/models/arcee.cpp +135 -0
  50. package/src/llama.cpp/src/models/arctic.cpp +138 -0
  51. package/src/llama.cpp/src/models/arwkv7.cpp +86 -0
  52. package/src/llama.cpp/src/models/baichuan.cpp +122 -0
  53. package/src/llama.cpp/src/models/bailingmoe.cpp +144 -0
  54. package/src/llama.cpp/src/models/bailingmoe2.cpp +135 -0
  55. package/src/llama.cpp/src/models/bert.cpp +176 -0
  56. package/src/llama.cpp/src/models/bitnet.cpp +160 -0
  57. package/src/llama.cpp/src/models/bloom.cpp +101 -0
  58. package/src/llama.cpp/src/models/chameleon.cpp +178 -0
  59. package/src/llama.cpp/src/models/chatglm.cpp +132 -0
  60. package/src/llama.cpp/src/models/codeshell.cpp +111 -0
  61. package/src/llama.cpp/src/models/cogvlm.cpp +100 -0
  62. package/src/llama.cpp/src/models/cohere2-iswa.cpp +131 -0
  63. package/src/llama.cpp/src/models/command-r.cpp +122 -0
  64. package/src/llama.cpp/src/models/dbrx.cpp +123 -0
  65. package/src/llama.cpp/src/models/deci.cpp +135 -0
  66. package/src/llama.cpp/src/models/deepseek.cpp +144 -0
  67. package/src/llama.cpp/src/models/deepseek2.cpp +236 -0
  68. package/src/llama.cpp/src/models/dots1.cpp +134 -0
  69. package/src/llama.cpp/src/models/dream.cpp +105 -0
  70. package/src/llama.cpp/src/models/ernie4-5-moe.cpp +150 -0
  71. package/src/llama.cpp/src/models/ernie4-5.cpp +110 -0
  72. package/src/llama.cpp/src/models/exaone.cpp +114 -0
  73. package/src/llama.cpp/src/models/exaone4.cpp +123 -0
  74. package/src/llama.cpp/src/models/falcon-h1.cpp +113 -0
  75. package/src/llama.cpp/src/models/falcon.cpp +120 -0
  76. package/src/llama.cpp/src/models/gemma-embedding.cpp +120 -0
  77. package/src/llama.cpp/src/models/gemma.cpp +112 -0
  78. package/src/llama.cpp/src/models/gemma2-iswa.cpp +125 -0
  79. package/src/llama.cpp/src/models/gemma3-iswa.cpp +131 -0
  80. package/src/llama.cpp/src/models/gemma3n-iswa.cpp +377 -0
  81. package/src/llama.cpp/src/models/glm4-moe.cpp +153 -0
  82. package/src/llama.cpp/src/models/glm4.cpp +127 -0
  83. package/src/llama.cpp/src/models/gpt2.cpp +105 -0
  84. package/src/llama.cpp/src/models/gptneox.cpp +144 -0
  85. package/src/llama.cpp/src/models/granite-hybrid.cpp +196 -0
  86. package/src/llama.cpp/src/models/granite.cpp +211 -0
  87. package/src/llama.cpp/src/models/graph-context-mamba.cpp +283 -0
  88. package/src/llama.cpp/src/models/grok.cpp +159 -0
  89. package/src/llama.cpp/src/models/grovemoe.cpp +141 -0
  90. package/src/llama.cpp/src/models/hunyuan-dense.cpp +132 -0
  91. package/src/llama.cpp/src/models/hunyuan-moe.cpp +154 -0
  92. package/src/llama.cpp/src/models/internlm2.cpp +120 -0
  93. package/src/llama.cpp/src/models/jais.cpp +86 -0
  94. package/src/llama.cpp/src/models/jamba.cpp +106 -0
  95. package/src/llama.cpp/src/models/lfm2.cpp +173 -0
  96. package/src/llama.cpp/src/models/llada-moe.cpp +122 -0
  97. package/src/llama.cpp/src/models/llada.cpp +99 -0
  98. package/src/llama.cpp/src/models/llama-iswa.cpp +174 -0
  99. package/src/llama.cpp/src/models/llama.cpp +155 -0
  100. package/src/llama.cpp/src/models/mamba.cpp +55 -0
  101. package/src/llama.cpp/src/models/minicpm3.cpp +199 -0
  102. package/src/llama.cpp/src/models/minimax-m2.cpp +124 -0
  103. package/src/llama.cpp/src/models/models.h +481 -0
  104. package/src/llama.cpp/src/models/mpt.cpp +126 -0
  105. package/src/llama.cpp/src/models/nemotron-h.cpp +121 -0
  106. package/src/llama.cpp/src/models/nemotron.cpp +122 -0
  107. package/src/llama.cpp/src/models/neo-bert.cpp +104 -0
  108. package/src/llama.cpp/src/models/olmo.cpp +121 -0
  109. package/src/llama.cpp/src/models/olmo2.cpp +150 -0
  110. package/src/llama.cpp/src/models/olmoe.cpp +124 -0
  111. package/src/llama.cpp/src/models/openai-moe-iswa.cpp +124 -0
  112. package/src/llama.cpp/src/models/openelm.cpp +124 -0
  113. package/src/llama.cpp/src/models/orion.cpp +123 -0
  114. package/src/llama.cpp/src/models/pangu-embedded.cpp +121 -0
  115. package/src/llama.cpp/src/models/phi2.cpp +121 -0
  116. package/src/llama.cpp/src/models/phi3.cpp +152 -0
  117. package/src/llama.cpp/src/models/plamo.cpp +110 -0
  118. package/src/llama.cpp/src/models/plamo2.cpp +316 -0
  119. package/src/llama.cpp/src/models/plm.cpp +168 -0
  120. package/src/llama.cpp/src/models/qwen.cpp +108 -0
  121. package/src/llama.cpp/src/models/qwen2.cpp +117 -0
  122. package/src/llama.cpp/src/models/qwen2moe.cpp +151 -0
  123. package/src/llama.cpp/src/models/qwen2vl.cpp +117 -0
  124. package/src/llama.cpp/src/models/qwen3.cpp +117 -0
  125. package/src/llama.cpp/src/models/qwen3moe.cpp +124 -0
  126. package/src/llama.cpp/src/models/qwen3vl-moe.cpp +149 -0
  127. package/src/llama.cpp/src/models/qwen3vl.cpp +141 -0
  128. package/src/llama.cpp/src/models/refact.cpp +94 -0
  129. package/src/llama.cpp/src/models/rwkv6-base.cpp +162 -0
  130. package/src/llama.cpp/src/models/rwkv6.cpp +94 -0
  131. package/src/llama.cpp/src/models/rwkv6qwen2.cpp +86 -0
  132. package/src/llama.cpp/src/models/rwkv7-base.cpp +135 -0
  133. package/src/llama.cpp/src/models/rwkv7.cpp +90 -0
  134. package/src/llama.cpp/src/models/seed-oss.cpp +124 -0
  135. package/src/llama.cpp/src/models/smallthinker.cpp +120 -0
  136. package/src/llama.cpp/src/models/smollm3.cpp +128 -0
  137. package/src/llama.cpp/src/models/stablelm.cpp +146 -0
  138. package/src/llama.cpp/src/models/starcoder.cpp +100 -0
  139. package/src/llama.cpp/src/models/starcoder2.cpp +121 -0
  140. package/src/llama.cpp/src/models/t5-dec.cpp +166 -0
  141. package/src/llama.cpp/src/models/t5-enc.cpp +96 -0
  142. package/src/llama.cpp/src/models/wavtokenizer-dec.cpp +149 -0
  143. package/src/llama.cpp/src/models/xverse.cpp +108 -0
@@ -0,0 +1,155 @@
1
+ #include "models.h"
2
+
3
+ llm_build_llama::llm_build_llama(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_kv();
18
+
19
+ const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
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,
28
+ model.layers[il].attn_norm, NULL,
29
+ LLM_NORM_RMS, il);
30
+ cb(cur, "attn_norm", il);
31
+
32
+ // self-attention
33
+ {
34
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
35
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
36
+
37
+ // compute Q and K and RoPE them
38
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
39
+ cb(Qcur, "Qcur", il);
40
+ if (model.layers[il].bq) {
41
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
42
+ cb(Qcur, "Qcur", il);
43
+ }
44
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
45
+ cb(Kcur, "Kcur", il);
46
+ if (model.layers[il].bk) {
47
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
48
+ cb(Kcur, "Kcur", il);
49
+ }
50
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
51
+ cb(Vcur, "Vcur", il);
52
+ if (model.layers[il].bv) {
53
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
54
+ cb(Vcur, "Vcur", il);
55
+ }
56
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
57
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
58
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
59
+
60
+ Qcur = ggml_rope_ext(
61
+ ctx0, Qcur, inp_pos, rope_factors,
62
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
63
+ ext_factor, attn_factor, beta_fast, beta_slow
64
+ );
65
+
66
+ Kcur = ggml_rope_ext(
67
+ ctx0, Kcur, inp_pos, rope_factors,
68
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
69
+ ext_factor, attn_factor, beta_fast, beta_slow
70
+ );
71
+
72
+ cb(Qcur, "Qcur", il);
73
+ cb(Kcur, "Kcur", il);
74
+ cb(Vcur, "Vcur", il);
75
+
76
+ if (hparams.use_kq_norm) {
77
+ // Llama4TextL2Norm
78
+ Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
79
+ Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
80
+ cb(Qcur, "Qcur_normed", il);
81
+ cb(Kcur, "Kcur_normed", il);
82
+ }
83
+ cur = build_attn(inp_attn,
84
+ model.layers[il].wo, model.layers[il].bo,
85
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
86
+ cb(cur, "attn_out", il);
87
+ }
88
+ if (il == n_layer - 1 && inp_out_ids) {
89
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
90
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
91
+ }
92
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
93
+ cb(ffn_inp, "ffn_inp", il);
94
+
95
+ // feed-forward network (non-MoE)
96
+ if (model.layers[il].ffn_gate_inp == nullptr) {
97
+
98
+ cur = build_norm(ffn_inp,
99
+ model.layers[il].ffn_norm, NULL,
100
+ LLM_NORM_RMS, il);
101
+ cb(cur, "ffn_norm", il);
102
+
103
+ cur = build_ffn(cur,
104
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
105
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
106
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
107
+ NULL,
108
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
109
+ cb(cur, "ffn_out", il);
110
+ } else {
111
+ // MoE branch
112
+ cur = build_norm(ffn_inp,
113
+ model.layers[il].ffn_norm, NULL,
114
+ LLM_NORM_RMS, il);
115
+ cb(cur, "ffn_norm", il);
116
+
117
+ cur = build_moe_ffn(cur,
118
+ model.layers[il].ffn_gate_inp,
119
+ model.layers[il].ffn_up_exps,
120
+ model.layers[il].ffn_gate_exps,
121
+ model.layers[il].ffn_down_exps,
122
+ nullptr,
123
+ n_expert, n_expert_used,
124
+ LLM_FFN_SILU, true,
125
+ false, 0.0,
126
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
127
+ il);
128
+ cb(cur, "ffn_moe_out", il);
129
+ }
130
+ cur = ggml_add(ctx0, cur, ffn_inp);
131
+ cb(cur, "ffn_out", il);
132
+
133
+ cur = build_cvec(cur, il);
134
+ cb(cur, "l_out", il);
135
+
136
+ // input for next layer
137
+ inpL = cur;
138
+ }
139
+ cur = inpL;
140
+
141
+ cur = build_norm(cur,
142
+ model.output_norm, NULL,
143
+ LLM_NORM_RMS, -1);
144
+
145
+ cb(cur, "result_norm", -1);
146
+ res->t_embd = cur;
147
+
148
+ // lm_head
149
+ cur = build_lora_mm(model.output, cur);
150
+
151
+ cb(cur, "result_output", -1);
152
+ res->t_logits = cur;
153
+
154
+ ggml_build_forward_expand(gf, cur);
155
+ }
@@ -0,0 +1,55 @@
1
+ #include "models.h"
2
+
3
+
4
+ llm_build_mamba::llm_build_mamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
5
+ ggml_tensor * cur;
6
+ ggml_tensor * inpL;
7
+
8
+ // {n_embd, n_tokens}
9
+ inpL = build_inp_embd(model.tok_embd);
10
+
11
+ auto * rs_inp = build_rs_inp();
12
+
13
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
14
+
15
+ for (int il = 0; il < n_layer; ++il) {
16
+ // norm
17
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
18
+ cb(cur, "attn_norm", il);
19
+
20
+ if (model.arch == LLM_ARCH_MAMBA2) {
21
+ cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);
22
+ } else {
23
+ cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);
24
+ }
25
+
26
+ if (il == n_layer - 1 && inp_out_ids) {
27
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
28
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
29
+ }
30
+
31
+ // residual
32
+ cur = ggml_add(ctx0, cur, inpL);
33
+
34
+ cur = build_cvec(cur, il);
35
+ cb(cur, "l_out", il);
36
+
37
+ // input for next layer
38
+ inpL = cur;
39
+ }
40
+
41
+ // final rmsnorm
42
+ cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
43
+
44
+ cb(cur, "result_norm", -1);
45
+ res->t_embd = cur;
46
+
47
+ // lm_head
48
+ cur = build_lora_mm(model.output, cur);
49
+
50
+ cb(cur, "result_output", -1);
51
+ res->t_logits = cur;
52
+
53
+ ggml_build_forward_expand(gf, cur);
54
+ }
55
+
@@ -0,0 +1,199 @@
1
+ #include "models.h"
2
+
3
+ llm_build_minicpm3::llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ //TODO: if the model varies, these parameters need to be read from the model
5
+ const int64_t n_embd_base = 256;
6
+ const float scale_embd = 12.0f;
7
+ const float scale_depth = 1.4f;
8
+ const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
9
+
10
+ const uint32_t n_embd_head_qk_rope = hparams.n_rot;
11
+ const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
12
+ const uint32_t kv_lora_rank = hparams.n_lora_kv;
13
+
14
+ ggml_tensor * cur;
15
+ ggml_tensor * inpL;
16
+
17
+ inpL = build_inp_embd(model.tok_embd);
18
+
19
+ // scale the input embeddings
20
+ inpL = ggml_scale(ctx0, inpL, scale_embd);
21
+ cb(inpL, "inp_scaled", -1);
22
+
23
+ // inp_pos - contains the positions
24
+ ggml_tensor * inp_pos = build_inp_pos();
25
+
26
+ auto * inp_attn = build_attn_inp_kv();
27
+
28
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
29
+
30
+ for (int il = 0; il < n_layer; ++il) {
31
+ ggml_tensor * inpSA = inpL;
32
+
33
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
34
+
35
+ // norm
36
+ cur = build_norm(inpL,
37
+ model.layers[il].attn_norm, NULL,
38
+ LLM_NORM_RMS, il);
39
+ cb(cur, "attn_norm", il);
40
+
41
+ // self_attention
42
+ {
43
+ ggml_tensor * q = NULL;
44
+ // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
45
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
46
+ cb(q, "q", il);
47
+
48
+ q = build_norm(q,
49
+ model.layers[il].attn_q_a_norm, NULL,
50
+ LLM_NORM_RMS, il);
51
+ cb(q, "q", il);
52
+
53
+ // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
54
+ q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
55
+ cb(q, "q", il);
56
+
57
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
58
+ ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
59
+ ggml_row_size(q->type, hparams.n_embd_head_k),
60
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
61
+ 0);
62
+ cb(q_nope, "q_nope", il);
63
+
64
+ // and {n_head * n_embd_head_qk_rope, n_tokens}
65
+ ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
66
+ ggml_row_size(q->type, hparams.n_embd_head_k),
67
+ ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
68
+ ggml_row_size(q->type, n_embd_head_qk_nope));
69
+ cb(q_pe, "q_pe", il);
70
+
71
+ // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
72
+ ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
73
+ cb(kv_pe_compresseed, "kv_pe_compresseed", il);
74
+
75
+ // split into {kv_lora_rank, n_tokens}
76
+ ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
77
+ kv_pe_compresseed->nb[1],
78
+ 0);
79
+ cb(kv_compressed, "kv_compressed", il);
80
+
81
+ // and {n_embd_head_qk_rope, n_tokens}
82
+ ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
83
+ kv_pe_compresseed->nb[1],
84
+ kv_pe_compresseed->nb[1],
85
+ ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
86
+ cb(k_pe, "k_pe", il);
87
+
88
+ kv_compressed = build_norm(kv_compressed,
89
+ model.layers[il].attn_kv_a_norm, NULL,
90
+ LLM_NORM_RMS, il);
91
+ cb(kv_compressed, "kv_compressed", il);
92
+
93
+ // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
94
+ ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
95
+ cb(kv, "kv", il);
96
+
97
+ // split into {n_head * n_embd_head_qk_nope, n_tokens}
98
+ ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
99
+ ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
100
+ ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
101
+ 0);
102
+ cb(k_nope, "k_nope", il);
103
+
104
+ // and {n_head * n_embd_head_v, n_tokens}
105
+ ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
106
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
107
+ ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
108
+ ggml_row_size(kv->type, (n_embd_head_qk_nope)));
109
+ cb(v_states, "v_states", il);
110
+
111
+ v_states = ggml_cont(ctx0, v_states);
112
+ cb(v_states, "v_states", il);
113
+
114
+ q_pe = ggml_rope_ext(
115
+ ctx0, q_pe, inp_pos, rope_factors,
116
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
117
+ ext_factor, attn_factor, beta_fast, beta_slow
118
+ );
119
+ cb(q_pe, "q_pe", il);
120
+
121
+ // shared RoPE key
122
+ k_pe = ggml_rope_ext(
123
+ ctx0, k_pe, inp_pos, rope_factors,
124
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
125
+ ext_factor, attn_factor, beta_fast, beta_slow
126
+ );
127
+ cb(k_pe, "k_pe", il);
128
+
129
+ ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
130
+ cb(q_states, "q_states", il);
131
+
132
+ ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
133
+ cb(k_states, "k_states", il);
134
+
135
+ cur = build_attn(inp_attn,
136
+ model.layers[il].wo, NULL,
137
+ q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
138
+ }
139
+ if (il == n_layer - 1 && inp_out_ids) {
140
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
141
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
142
+ }
143
+ // scale_res - scale the hidden states for residual connection
144
+ const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
145
+ cur = ggml_scale(ctx0, cur, scale_res);
146
+ cb(cur, "hidden_scaled", il);
147
+
148
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
149
+ cb(ffn_inp, "ffn_inp", il);
150
+
151
+ // feed-forward network
152
+ {
153
+ cur = build_norm(ffn_inp,
154
+ model.layers[il].ffn_norm, NULL,
155
+ LLM_NORM_RMS, il);
156
+ cb(cur, "ffn_norm", il);
157
+
158
+ cur = build_ffn(cur,
159
+ model.layers[il].ffn_up, NULL, NULL,
160
+ model.layers[il].ffn_gate, NULL, NULL,
161
+ model.layers[il].ffn_down, NULL, NULL,
162
+ NULL,
163
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
164
+ cb(cur, "ffn_out", il);
165
+ }
166
+ // scale the hidden states for residual connection
167
+ cur = ggml_scale(ctx0, cur, scale_res);
168
+ cb(cur, "hidden_scaled_ffn", il);
169
+
170
+ cur = ggml_add(ctx0, cur, ffn_inp);
171
+
172
+ cur = build_cvec(cur, il);
173
+ cb(cur, "l_out", il);
174
+
175
+ // input for next layer
176
+ inpL = cur;
177
+ }
178
+ cur = inpL;
179
+
180
+ cur = build_norm(cur,
181
+ model.output_norm, NULL,
182
+ LLM_NORM_RMS, -1);
183
+
184
+ cb(cur, "result_norm", -1);
185
+ res->t_embd = cur;
186
+
187
+ // lm_head scaling
188
+ const float scale_lmhead = float(n_embd_base)/float(n_embd);
189
+ cur = ggml_scale(ctx0, cur, scale_lmhead);
190
+ cb(cur, "lmhead_scaling", -1);
191
+
192
+ // lm_head
193
+ cur = build_lora_mm(model.output, cur);
194
+
195
+ cb(cur, "result_output", -1);
196
+ res->t_logits = cur;
197
+
198
+ ggml_build_forward_expand(gf, cur);
199
+ }
@@ -0,0 +1,124 @@
1
+
2
+ #include "models.h"
3
+
4
+ llm_build_minimax_m2::llm_build_minimax_m2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
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); this is wrong in case of minimax, head_dim = 128, n_rot = 64
9
+
10
+ ggml_tensor * cur;
11
+ ggml_tensor * inpL;
12
+
13
+ inpL = build_inp_embd(model.tok_embd);
14
+
15
+ ggml_tensor * inp_pos = build_inp_pos();
16
+ auto inp_attn = build_attn_inp_kv();
17
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
18
+
19
+ for (int il = 0; il < n_layer; ++il) {
20
+ ggml_tensor * inpSA = inpL;
21
+
22
+ cur = inpL;
23
+
24
+ // self_attention
25
+ {
26
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
27
+ cb(cur, "attn_norm", il);
28
+
29
+ // compute Q and K and RoPE them
30
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
31
+ cb(Qcur, "Qcur", il);
32
+
33
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
34
+ cb(Kcur, "Kcur", il);
35
+
36
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
37
+ cb(Vcur, "Vcur", il);
38
+
39
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
40
+ LLM_NORM_RMS, il);
41
+ cb(Qcur, "Qcur_normed", il);
42
+
43
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
44
+ LLM_NORM_RMS, il);
45
+ cb(Kcur, "Kcur_normed", il);
46
+
47
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
48
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
49
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
50
+
51
+ Qcur = ggml_rope_ext(
52
+ ctx0, Qcur, inp_pos, nullptr,
53
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
54
+ ext_factor, attn_factor, beta_fast, beta_slow
55
+ );
56
+
57
+ Kcur = ggml_rope_ext(
58
+ ctx0, Kcur, inp_pos, nullptr,
59
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
60
+ ext_factor, attn_factor, beta_fast, beta_slow
61
+ );
62
+
63
+ cb(Qcur, "Qcur", il);
64
+ cb(Kcur, "Kcur", il);
65
+ cb(Vcur, "Vcur", il);
66
+
67
+ cur = build_attn(inp_attn,
68
+ model.layers[il].wo, NULL,
69
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
70
+ }
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
+
77
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
78
+ cb(ffn_inp, "ffn_inp", il);
79
+
80
+ // MoE branch
81
+ cur = build_norm(ffn_inp,
82
+ model.layers[il].ffn_norm, NULL,
83
+ LLM_NORM_RMS, il);
84
+ cb(cur, "ffn_norm", il);
85
+
86
+ cur = build_moe_ffn(cur,
87
+ model.layers[il].ffn_gate_inp,
88
+ model.layers[il].ffn_up_exps,
89
+ model.layers[il].ffn_gate_exps,
90
+ model.layers[il].ffn_down_exps,
91
+ model.layers[il].ffn_exp_probs_b,
92
+ n_expert, n_expert_used,
93
+ LLM_FFN_SILU, true,
94
+ false, 0.0,
95
+ (llama_expert_gating_func_type) hparams.expert_gating_func,
96
+ il);
97
+ cb(cur, "ffn_moe_out", il);
98
+
99
+ cur = ggml_add(ctx0, cur, ffn_inp);
100
+
101
+ cur = build_cvec(cur, il);
102
+ cb(cur, "l_out", il);
103
+
104
+ // input for next layer
105
+ inpL = cur;
106
+ }
107
+
108
+ cur = inpL;
109
+
110
+ cur = build_norm(cur,
111
+ model.output_norm, NULL,
112
+ LLM_NORM_RMS, -1);
113
+
114
+ cb(cur, "result_norm", -1);
115
+ res->t_embd = cur;
116
+
117
+ // lm_head
118
+ cur = build_lora_mm(model.output, cur);
119
+
120
+ cb(cur, "result_output", -1);
121
+ res->t_logits = cur;
122
+
123
+ ggml_build_forward_expand(gf, cur);
124
+ }