@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,132 @@
1
+ #include "models.h"
2
+
3
+ llm_build_hunyuan_dense::llm_build_hunyuan_dense(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 = 1.0f / sqrtf(float(n_embd_head));
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
+ // self-attention
32
+ {
33
+ // rope freq factors for llama3; may return nullptr for llama2 and other models
34
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
35
+
36
+ // compute Q and K and RoPE them
37
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
38
+ cb(Qcur, "Qcur", il);
39
+ if (model.layers[il].bq) {
40
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
41
+ cb(Qcur, "Qcur", il);
42
+ }
43
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
44
+ cb(Kcur, "Kcur", il);
45
+ if (model.layers[il].bk) {
46
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
47
+ cb(Kcur, "Kcur", il);
48
+ }
49
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
50
+ cb(Vcur, "Vcur", il);
51
+ if (model.layers[il].bv) {
52
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
53
+ cb(Vcur, "Vcur", il);
54
+ }
55
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
56
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
57
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
58
+
59
+ Qcur = ggml_rope_ext(
60
+ ctx0, Qcur, inp_pos, rope_factors,
61
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
62
+ ext_factor, attn_factor, beta_fast, beta_slow
63
+ );
64
+
65
+ cb(Qcur, "Qcur", il);
66
+ cb(Kcur, "Kcur", il);
67
+ cb(Vcur, "Vcur", il);
68
+
69
+ Kcur = ggml_rope_ext(
70
+ ctx0, Kcur, inp_pos, rope_factors,
71
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
72
+ ext_factor, attn_factor, beta_fast, beta_slow
73
+ );
74
+
75
+ Kcur = build_norm(Kcur,
76
+ model.layers[il].attn_k_norm, nullptr,
77
+ LLM_NORM_RMS, il);
78
+ cb(Kcur, "Kcur_norm", il);
79
+
80
+ Qcur = build_norm(Qcur,
81
+ model.layers[il].attn_q_norm, nullptr,
82
+ LLM_NORM_RMS, il);
83
+ cb(Qcur, "Qcur_norm", il);
84
+
85
+ cur = build_attn(inp_attn,
86
+ model.layers[il].wo, model.layers[il].bo,
87
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
88
+ cb(cur, "attn_out", il);
89
+ }
90
+ if (il == n_layer - 1 && inp_out_ids) {
91
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
92
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
93
+ }
94
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
95
+ cb(ffn_inp, "ffn_inp", il);
96
+
97
+ cur = build_norm(ffn_inp,
98
+ model.layers[il].ffn_norm, NULL,
99
+ LLM_NORM_RMS, il);
100
+ cb(cur, "ffn_norm", il);
101
+ // feed-forward network (non-MoE)
102
+ ggml_tensor * cur_mlp = build_ffn(cur,
103
+ model.layers[il].ffn_up, NULL, NULL,
104
+ model.layers[il].ffn_gate, NULL, NULL,
105
+ model.layers[il].ffn_down, NULL, NULL,
106
+ NULL,
107
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
108
+ cb(cur_mlp, "ffn_out", il);
109
+
110
+ cur = ggml_add(ctx0, cur_mlp, ffn_inp);
111
+
112
+ cur = build_cvec(cur, il);
113
+ cb(cur, "l_out", il);
114
+
115
+ // input for next layer
116
+ inpL = cur;
117
+ }
118
+ cur = inpL;
119
+
120
+ cur = build_norm(cur,
121
+ model.output_norm, NULL,
122
+ LLM_NORM_RMS, -1);
123
+
124
+ cb(cur, "result_norm", -1);
125
+ res->t_embd = cur;
126
+ // lm_head
127
+ cur = build_lora_mm(model.output, cur);
128
+ cb(cur, "result_output", -1);
129
+ res->t_logits = cur;
130
+
131
+ ggml_build_forward_expand(gf, cur);
132
+ }
@@ -0,0 +1,154 @@
1
+ #include "models.h"
2
+
3
+ llm_build_hunyuan_moe::llm_build_hunyuan_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_kv();
18
+
19
+ const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
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
+ cb(Qcur, "Qcur", il);
67
+ cb(Kcur, "Kcur", il);
68
+ cb(Vcur, "Vcur", il);
69
+
70
+ Kcur = ggml_rope_ext(
71
+ ctx0, Kcur, inp_pos, rope_factors,
72
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
73
+ ext_factor, attn_factor, beta_fast, beta_slow
74
+ );
75
+
76
+ Kcur = build_norm(Kcur,
77
+ model.layers[il].attn_k_norm, nullptr,
78
+ LLM_NORM_RMS, il);
79
+ cb(Kcur, "Kcur_norm", il);
80
+
81
+ Qcur = build_norm(Qcur,
82
+ model.layers[il].attn_q_norm, nullptr,
83
+ LLM_NORM_RMS, il);
84
+ cb(Qcur, "Qcur_norm", il);
85
+
86
+ cur = build_attn(inp_attn,
87
+ model.layers[il].wo, model.layers[il].bo,
88
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
89
+ cb(cur, "attn_out", il);
90
+ }
91
+ if (il == n_layer - 1 && inp_out_ids) {
92
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
93
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
94
+ }
95
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
96
+ cb(ffn_inp, "ffn_inp", il);
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
+ // feed-forward network (non-MoE)
104
+ ggml_tensor * cur_mlp = build_ffn(cur,
105
+ model.layers[il].ffn_up_shexp, NULL, NULL,
106
+ model.layers[il].ffn_gate_shexp, NULL, NULL,
107
+ model.layers[il].ffn_down_shexp, NULL, NULL,
108
+ NULL,
109
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
110
+ cb(cur_mlp, "ffn_mlp", il);
111
+
112
+ // MoE branch
113
+ ggml_tensor * cur_moe = build_moe_ffn(cur,
114
+ model.layers[il].ffn_gate_inp,
115
+ model.layers[il].ffn_up_exps,
116
+ model.layers[il].ffn_gate_exps,
117
+ model.layers[il].ffn_down_exps,
118
+ nullptr,
119
+ n_expert, n_expert_used,
120
+ LLM_FFN_SILU,
121
+ true, // norm_topk_prob
122
+ false,
123
+ 0.0,
124
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
125
+ il);
126
+ cb(cur_moe, "ffn_moe_out", il);
127
+
128
+ ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
129
+ cb(ffn_out, "ffn_out", il);
130
+
131
+ cur = ggml_add(ctx0, ffn_out, ffn_inp);
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
+ cb(cur, "result_output", -1);
151
+ res->t_logits = cur;
152
+
153
+ ggml_build_forward_expand(gf, cur);
154
+ }
@@ -0,0 +1,120 @@
1
+ #include "models.h"
2
+
3
+ llm_build_internlm2::llm_build_internlm2(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
+ 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
+ if (model.layers[il].bq) {
36
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
37
+ cb(Qcur, "Qcur", il);
38
+ }
39
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
40
+ cb(Kcur, "Kcur", il);
41
+ if (model.layers[il].bk) {
42
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
43
+ cb(Kcur, "Kcur", il);
44
+ }
45
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
46
+ cb(Vcur, "Vcur", il);
47
+ if (model.layers[il].bv) {
48
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
49
+ cb(Vcur, "Vcur", il);
50
+ }
51
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
52
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
53
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
54
+
55
+ Qcur = ggml_rope_ext(
56
+ ctx0, Qcur, inp_pos, nullptr,
57
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
58
+ ext_factor, attn_factor, beta_fast, beta_slow
59
+ );
60
+
61
+ Kcur = ggml_rope_ext(
62
+ ctx0, Kcur, inp_pos, nullptr,
63
+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
64
+ ext_factor, attn_factor, beta_fast, beta_slow
65
+ );
66
+
67
+ cb(Qcur, "Qcur", il);
68
+ cb(Kcur, "Kcur", il);
69
+ cb(Vcur, "Vcur", il);
70
+
71
+ cur = build_attn(inp_attn,
72
+ model.layers[il].wo, model.layers[il].bo,
73
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
74
+ }
75
+ if (il == n_layer - 1 && inp_out_ids) {
76
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
77
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
78
+ }
79
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
80
+ cb(ffn_inp, "ffn_inp", il);
81
+
82
+ // feed-forward network
83
+ cur = build_norm(ffn_inp,
84
+ model.layers[il].ffn_norm, NULL,
85
+ LLM_NORM_RMS, il);
86
+ cb(cur, "ffn_norm", il);
87
+
88
+ cur = build_ffn(cur,
89
+ model.layers[il].ffn_up, NULL, NULL,
90
+ model.layers[il].ffn_gate, NULL, NULL,
91
+ model.layers[il].ffn_down, NULL, NULL,
92
+ NULL,
93
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
94
+ cb(cur, "ffn_out", il);
95
+
96
+ cur = ggml_add(ctx0, cur, ffn_inp);
97
+
98
+ cur = build_cvec(cur, il);
99
+ cb(cur, "l_out", il);
100
+
101
+ // input for next layer
102
+ inpL = cur;
103
+ }
104
+ cur = inpL;
105
+
106
+ cur = build_norm(cur,
107
+ model.output_norm, NULL,
108
+ LLM_NORM_RMS, -1);
109
+
110
+ cb(cur, "result_norm", -1);
111
+ res->t_embd = cur;
112
+
113
+ // lm_head
114
+ cur = build_lora_mm(model.output, cur);
115
+
116
+ cb(cur, "result_output", -1);
117
+ res->t_logits = cur;
118
+
119
+ ggml_build_forward_expand(gf, cur);
120
+ }
@@ -0,0 +1,86 @@
1
+ #include "models.h"
2
+
3
+ llm_build_jais::llm_build_jais(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
+ const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
6
+
7
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
+
9
+ ggml_tensor * cur;
10
+ ggml_tensor * inpL;
11
+
12
+ inpL = build_inp_embd(model.tok_embd);
13
+
14
+ auto * inp_attn = build_attn_inp_kv();
15
+
16
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
17
+
18
+ for (int il = 0; il < n_layer; ++il) {
19
+ cur = build_norm(inpL,
20
+ model.layers[il].attn_norm,
21
+ model.layers[il].attn_norm_b,
22
+ LLM_NORM, il);
23
+ cb(cur, "attn_norm", il);
24
+
25
+ // self-attention
26
+ {
27
+ cur = build_lora_mm(model.layers[il].wqkv, cur);
28
+ cb(cur, "wqkv", il);
29
+
30
+ cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
31
+ cb(cur, "bqkv", il);
32
+
33
+ ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*cur->nb[0]*(n_embd));
34
+ ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*cur->nb[0]*(n_embd));
35
+ ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa));
36
+
37
+ cb(Qcur, "Qcur", il);
38
+ cb(Kcur, "Kcur", il);
39
+ cb(Vcur, "Vcur", il);
40
+
41
+ cur = build_attn(inp_attn,
42
+ model.layers[il].wo, model.layers[il].bo,
43
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il);
44
+ }
45
+ if (il == n_layer - 1 && inp_out_ids) {
46
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
47
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
48
+ }
49
+ // add the input
50
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
51
+ cb(ffn_inp, "ffn_inp", il);
52
+
53
+ // FF
54
+ {
55
+ cur = build_norm(ffn_inp,
56
+ model.layers[il].ffn_norm,
57
+ model.layers[il].ffn_norm_b,
58
+ LLM_NORM, il);
59
+ cb(cur, "ffn_norm", il);
60
+
61
+ cur = build_ffn(cur,
62
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
63
+ model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
64
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
65
+ NULL,
66
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
67
+ cb(cur, "ffn_out", il);
68
+ }
69
+ inpL = ggml_add(ctx0, cur, ffn_inp);
70
+ cb(inpL, "l_out", il);
71
+ }
72
+ cur = build_norm(inpL,
73
+ model.output_norm,
74
+ model.output_norm_b,
75
+ LLM_NORM, -1);
76
+
77
+ cb(cur, "result_norm", -1);
78
+ res->t_embd = cur;
79
+
80
+ cur = build_lora_mm(model.output, cur);
81
+
82
+ cb(cur, "result_output", -1);
83
+ res->t_logits = cur;
84
+
85
+ ggml_build_forward_expand(gf, cur);
86
+ }
@@ -0,0 +1,106 @@
1
+ #include "models.h"
2
+
3
+ llm_build_jamba::llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
4
+ const int64_t n_embd_head = hparams.n_embd_head_v;
5
+
6
+ ggml_tensor * cur;
7
+ ggml_tensor * inpL;
8
+
9
+ // {n_embd, n_tokens}
10
+ inpL = build_inp_embd(model.tok_embd);
11
+
12
+ auto * inp_hybrid = build_inp_mem_hybrid();
13
+
14
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
15
+
16
+ for (int il = 0; il < n_layer; ++il) {
17
+ const int64_t n_head_kv = hparams.n_head_kv(il);
18
+
19
+ cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
20
+ cb(cur, "attn_norm", il);
21
+
22
+ if (n_head_kv == 0) {
23
+ cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
24
+ } else {
25
+ // Attention
26
+
27
+ struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
28
+ struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
29
+ struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
30
+
31
+ cb(Qcur, "Qcur", il);
32
+ cb(Kcur, "Kcur", il);
33
+ cb(Vcur, "Vcur", il);
34
+
35
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
36
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
37
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
38
+
39
+ cb(Qcur, "Qcur", il);
40
+ cb(Kcur, "Kcur", il);
41
+ cb(Vcur, "Vcur", il);
42
+
43
+ // No RoPE :)
44
+ cur = build_attn(inp_hybrid->get_attn(),
45
+ model.layers[il].wo, NULL,
46
+ Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
47
+ }
48
+ if (il == n_layer - 1 && inp_out_ids) {
49
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
50
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
51
+ }
52
+ // residual
53
+ struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
54
+ cb(cur, "ffn_inp", il);
55
+
56
+ cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
57
+ cb(cur, "ffn_norm", il);
58
+
59
+ // feed-forward network
60
+ if (model.layers[il].ffn_gate_inp == nullptr) {
61
+ // FFN
62
+ cur = build_ffn(cur,
63
+ model.layers[il].ffn_up, NULL, NULL,
64
+ model.layers[il].ffn_gate, NULL, NULL,
65
+ model.layers[il].ffn_down, NULL, NULL,
66
+ NULL,
67
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
68
+ cb(cur, "ffn_out", il);
69
+ } else {
70
+ // MoE branch
71
+ cur = build_moe_ffn(cur,
72
+ model.layers[il].ffn_gate_inp,
73
+ model.layers[il].ffn_up_exps,
74
+ model.layers[il].ffn_gate_exps,
75
+ model.layers[il].ffn_down_exps,
76
+ nullptr,
77
+ n_expert, n_expert_used,
78
+ LLM_FFN_SILU, false,
79
+ false, 0.0,
80
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
81
+ il);
82
+ cb(cur, "ffn_moe_out", il);
83
+ }
84
+ // residual
85
+ cur = ggml_add(ctx0, ffn_inp, cur);
86
+
87
+ cur = build_cvec(cur, il);
88
+ cb(cur, "l_out", il);
89
+
90
+ // input for next layer
91
+ inpL = cur;
92
+ }
93
+ // final rmsnorm
94
+ cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
95
+
96
+ cb(cur, "result_norm", -1);
97
+ res->t_embd = cur;
98
+
99
+ // lm_head
100
+ cur = build_lora_mm(model.output, cur);
101
+
102
+ cb(cur, "result_output", -1);
103
+ res->t_logits = cur;
104
+
105
+ ggml_build_forward_expand(gf, cur);
106
+ }