@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,149 @@
1
+ #include "models.h"
2
+
3
+ llm_build_qwen3vlmoe::llm_build_qwen3vlmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ const size_t n_deepstack_layers = hparams.n_deepstack_layers;
5
+ const int64_t n_embd = hparams.n_embd;
6
+ const int64_t n_embd_head = hparams.n_embd_head_v;
7
+
8
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
10
+
11
+ ggml_tensor * cur;
12
+ ggml_tensor * inpL;
13
+
14
+ inpL = build_inp_embd(model.tok_embd);
15
+
16
+ int sections[4];
17
+ std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
18
+
19
+ std::vector<ggml_tensor *> deepstack_features(n_deepstack_layers, nullptr);
20
+
21
+ if (ubatch.embd) {
22
+ // Image input: split main embd and deepstack embds
23
+ ggml_tensor * inpL_main = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], 0);
24
+ for (size_t i = 0; i < n_deepstack_layers; i++) {
25
+ deepstack_features[i] = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], (i + 1) * n_embd * sizeof(float));
26
+ }
27
+ inpL = inpL_main;
28
+ }
29
+
30
+ // inp_pos - contains the positions
31
+ ggml_tensor * inp_pos = build_inp_pos();
32
+
33
+ auto * inp_attn = build_attn_inp_kv();
34
+
35
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
36
+
37
+ for (int il = 0; il < n_layer; ++il) {
38
+ ggml_tensor * inpSA = inpL;
39
+
40
+ // norm
41
+ cur = build_norm(inpL,
42
+ model.layers[il].attn_norm, NULL,
43
+ LLM_NORM_RMS, il);
44
+ cb(cur, "attn_norm", il);
45
+
46
+ // self_attention
47
+ {
48
+ // compute Q and K and RoPE them
49
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
50
+ cb(Qcur, "Qcur", il);
51
+
52
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
53
+ cb(Kcur, "Kcur", il);
54
+
55
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
56
+ cb(Vcur, "Vcur", il);
57
+
58
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
59
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
60
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
61
+
62
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
63
+ cb(Qcur, "Qcur_normed", il);
64
+
65
+ Qcur = ggml_rope_multi(
66
+ ctx0, Qcur, inp_pos, nullptr,
67
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
68
+ ext_factor, attn_factor, beta_fast, beta_slow
69
+ );
70
+
71
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
72
+ cb(Kcur, "Kcur_normed", il);
73
+
74
+ Kcur = ggml_rope_multi(
75
+ ctx0, Kcur, inp_pos, nullptr,
76
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
77
+ ext_factor, attn_factor, beta_fast, beta_slow
78
+ );
79
+
80
+ cb(Qcur, "Qcur", il);
81
+ cb(Kcur, "Kcur", il);
82
+ cb(Vcur, "Vcur", il);
83
+
84
+ cur = build_attn(inp_attn,
85
+ model.layers[il].wo, model.layers[il].bo,
86
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
87
+ }
88
+
89
+ if (il == n_layer - 1 && inp_out_ids) {
90
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
91
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
92
+ }
93
+
94
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
95
+ cb(ffn_inp, "ffn_inp", il);
96
+
97
+ // MoE branch
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
+ ggml_tensor * moe_out =
104
+ build_moe_ffn(cur,
105
+ model.layers[il].ffn_gate_inp,
106
+ model.layers[il].ffn_up_exps,
107
+ model.layers[il].ffn_gate_exps,
108
+ model.layers[il].ffn_down_exps,
109
+ nullptr,
110
+ n_expert, n_expert_used,
111
+ LLM_FFN_SILU, true,
112
+ false, 0.0,
113
+ LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
114
+ il);
115
+ cb(moe_out, "ffn_moe_out", il);
116
+ cur = moe_out;
117
+
118
+ cur = ggml_add(ctx0, cur, ffn_inp);
119
+
120
+ cur = build_cvec(cur, il);
121
+ cb(cur, "l_out", il);
122
+
123
+ if (ubatch.embd && (size_t)il < n_deepstack_layers) {
124
+ cur = ggml_add(ctx0, cur, deepstack_features[il]);
125
+ cb(cur, "deepstack_out", il);
126
+ }
127
+
128
+ // input for next layer
129
+ inpL = cur;
130
+ }
131
+
132
+ cur = inpL;
133
+
134
+ cur = build_norm(cur,
135
+ model.output_norm, NULL,
136
+ LLM_NORM_RMS, -1);
137
+
138
+ cb(cur, "result_norm", -1);
139
+ res->t_embd = cur;
140
+
141
+ // lm_head
142
+ cur = build_lora_mm(model.output, cur);
143
+
144
+ cb(cur, "result_output", -1);
145
+ res->t_logits = cur;
146
+
147
+ ggml_build_forward_expand(gf, cur);
148
+ }
149
+
@@ -0,0 +1,141 @@
1
+ #include "models.h"
2
+
3
+ llm_build_qwen3vl::llm_build_qwen3vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
+ const size_t n_deepstack_layers = hparams.n_deepstack_layers;
5
+ const int64_t n_embd = hparams.n_embd;
6
+ const int64_t n_embd_head = hparams.n_embd_head_v;
7
+
8
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
10
+
11
+ ggml_tensor * cur;
12
+ ggml_tensor * inpL;
13
+
14
+ inpL = build_inp_embd(model.tok_embd);
15
+
16
+ int sections[4];
17
+ std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
18
+
19
+ std::vector<ggml_tensor *> deepstack_features(n_deepstack_layers, nullptr);
20
+
21
+ if (ubatch.embd) {
22
+ // Image input: split main embd and deepstack embds
23
+ ggml_tensor * inpL_main = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], 0);
24
+ for (size_t i = 0; i < n_deepstack_layers; i++) {
25
+ deepstack_features[i] = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], (i + 1) * n_embd * sizeof(float));
26
+ }
27
+ inpL = inpL_main;
28
+ }
29
+
30
+ // inp_pos - contains the positions
31
+ ggml_tensor * inp_pos = build_inp_pos();
32
+
33
+ auto * inp_attn = build_attn_inp_kv();
34
+
35
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
36
+
37
+ for (int il = 0; il < n_layer; ++il) {
38
+ ggml_tensor * inpSA = inpL;
39
+
40
+ // norm
41
+ cur = build_norm(inpL,
42
+ model.layers[il].attn_norm, NULL,
43
+ LLM_NORM_RMS, il);
44
+ cb(cur, "attn_norm", il);
45
+
46
+ // self-attention
47
+ {
48
+ // compute Q and K and RoPE them
49
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
50
+ cb(Qcur, "Qcur", il);
51
+
52
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
53
+ cb(Kcur, "Kcur", il);
54
+
55
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
56
+ cb(Vcur, "Vcur", il);
57
+
58
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
59
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
60
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
61
+
62
+ Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
63
+ cb(Qcur, "Qcur_normed", il);
64
+
65
+ Qcur = ggml_rope_multi(
66
+ ctx0, Qcur, inp_pos, nullptr,
67
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
68
+ ext_factor, attn_factor, beta_fast, beta_slow
69
+ );
70
+
71
+ Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
72
+ cb(Kcur, "Kcur_normed", il);
73
+
74
+ Kcur = ggml_rope_multi(
75
+ ctx0, Kcur, inp_pos, nullptr,
76
+ n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
77
+ ext_factor, attn_factor, beta_fast, beta_slow
78
+ );
79
+
80
+ cb(Qcur, "Qcur", il);
81
+ cb(Kcur, "Kcur", il);
82
+ cb(Vcur, "Vcur", il);
83
+
84
+ cur = build_attn(inp_attn,
85
+ model.layers[il].wo, model.layers[il].bo,
86
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
87
+ }
88
+
89
+ if (il == n_layer - 1 && inp_out_ids) {
90
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
91
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
92
+ }
93
+
94
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
95
+ cb(ffn_inp, "ffn_inp", il);
96
+
97
+ // feed-forward network
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, NULL, NULL,
105
+ model.layers[il].ffn_gate, NULL, NULL,
106
+ model.layers[il].ffn_down, NULL, NULL,
107
+ NULL,
108
+ LLM_FFN_SILU, LLM_FFN_PAR, il);
109
+ cb(cur, "ffn_out", il);
110
+
111
+ cur = ggml_add(ctx0, cur, ffn_inp);
112
+
113
+ cur = build_cvec(cur, il);
114
+ cb(cur, "l_out", il);
115
+
116
+ if (ubatch.embd && (size_t)il < n_deepstack_layers) {
117
+ cur = ggml_add(ctx0, cur, deepstack_features[il]);
118
+ cb(cur, "deepstack_out", il);
119
+ }
120
+
121
+ // input for next layer
122
+ inpL = cur;
123
+ }
124
+
125
+ cur = inpL;
126
+
127
+ cur = build_norm(cur,
128
+ model.output_norm, NULL,
129
+ LLM_NORM_RMS, -1);
130
+
131
+ cb(cur, "result_norm", -1);
132
+ res->t_embd = cur;
133
+
134
+ // lm_head
135
+ cur = build_lora_mm(model.output, cur);
136
+
137
+ cb(cur, "result_output", -1);
138
+ res->t_logits = cur;
139
+
140
+ ggml_build_forward_expand(gf, cur);
141
+ }
@@ -0,0 +1,94 @@
1
+ #include "models.h"
2
+
3
+ llm_build_refact::llm_build_refact(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
+ auto * inp_attn = build_attn_inp_kv();
14
+
15
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
16
+
17
+ for (int il = 0; il < n_layer; ++il) {
18
+ ggml_tensor * inpSA = inpL;
19
+
20
+ cur = build_norm(inpL,
21
+ model.layers[il].attn_norm, NULL,
22
+ LLM_NORM_RMS, il);
23
+ cb(cur, "attn_norm", il);
24
+
25
+ // self-attention
26
+ {
27
+ ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
28
+ cb(Qcur, "Qcur", il);
29
+
30
+ ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
31
+ cb(Kcur, "Kcur", il);
32
+
33
+ ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
34
+ cb(Vcur, "Vcur", il);
35
+
36
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
37
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
38
+ Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
39
+
40
+ cb(Qcur, "Qcur", il);
41
+ cb(Kcur, "Kcur", il);
42
+ cb(Vcur, "Vcur", il);
43
+
44
+ cur = build_attn(inp_attn,
45
+ model.layers[il].wo, NULL,
46
+ Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 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
+ inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
51
+ }
52
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
53
+ cb(ffn_inp, "ffn_inp", il);
54
+
55
+ // feed-forward network
56
+ {
57
+ cur = build_norm(ffn_inp,
58
+ model.layers[il].ffn_norm, NULL,
59
+ LLM_NORM_RMS, il);
60
+ cb(cur, "ffn_norm", il);
61
+
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
+ }
70
+ cur = ggml_add(ctx0, cur, ffn_inp);
71
+
72
+ cur = build_cvec(cur, il);
73
+ cb(cur, "l_out", il);
74
+
75
+ // input for next layer
76
+ inpL = cur;
77
+ }
78
+ cur = inpL;
79
+
80
+ cur = build_norm(cur,
81
+ model.output_norm, NULL,
82
+ LLM_NORM_RMS, -1);
83
+
84
+ cb(cur, "result_norm", -1);
85
+ res->t_embd = cur;
86
+
87
+ // lm_head
88
+ cur = build_lora_mm(model.output, cur);
89
+
90
+ cb(cur, "result_output", -1);
91
+ res->t_logits = cur;
92
+
93
+ ggml_build_forward_expand(gf, cur);
94
+ }
@@ -0,0 +1,162 @@
1
+ #include "models.h"
2
+
3
+ llm_build_rwkv6_base::llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) :
4
+ llm_graph_context(params),
5
+ model(model) {}
6
+
7
+ ggml_tensor * llm_build_rwkv6_base::build_rwkv6_channel_mix(const llama_layer * layer,
8
+ ggml_tensor * cur,
9
+ ggml_tensor * x_prev,
10
+ llm_arch arch) const {
11
+ ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
12
+ switch (arch) {
13
+ case LLM_ARCH_RWKV6:
14
+ {
15
+ ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
16
+ ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
17
+
18
+ ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
19
+ ggml_tensor * k = ggml_sqr(ctx0, ggml_relu(ctx0, build_lora_mm(layer->channel_mix_key, xk)));
20
+ cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
21
+ }
22
+ break;
23
+ default:
24
+ GGML_ABORT("fatal error");
25
+ }
26
+ return cur;
27
+ }
28
+
29
+ ggml_tensor * llm_build_rwkv6_base::build_rwkv6_time_mix(llm_graph_input_rs * inp,
30
+ ggml_tensor * cur,
31
+ ggml_tensor * x_prev,
32
+ const llama_ubatch & ubatch,
33
+ int il) const {
34
+ const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
35
+
36
+ const auto n_tokens = ubatch.n_tokens;
37
+ const auto n_seqs = ubatch.n_seqs;
38
+ const auto n_seq_tokens = ubatch.n_seq_tokens;
39
+ const auto n_embd = hparams.n_embd;
40
+ const auto head_size = hparams.wkv_head_size;
41
+ const auto n_head = n_embd / head_size;
42
+ const auto n_head_kv = hparams.n_head_kv(il);
43
+
44
+ const auto kv_head = mctx_cur->get_head();
45
+
46
+ const auto & layer = model.layers[il];
47
+
48
+ bool is_qrwkv = layer.time_mix_first == nullptr;
49
+
50
+ ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
51
+
52
+ sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
53
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
54
+
55
+ ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
56
+
57
+ xxx = ggml_reshape_4d(ctx0, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)),
58
+ layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens);
59
+
60
+ xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
61
+
62
+ xxx = ggml_mul_mat(
63
+ ctx0, ggml_reshape_4d(ctx0, layer.time_mix_w2, layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5), xxx);
64
+
65
+ ggml_tensor *xw, *xk, *xv, *xr, *xg;
66
+ if (layer.time_mix_lerp_fused) {
67
+ // fusing these weights makes some performance improvement
68
+ sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
69
+ cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
70
+ xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
71
+ xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
72
+ xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
73
+ xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
74
+ xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
75
+ xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
76
+ } else {
77
+ // for backward compatibility
78
+ xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
79
+ xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
80
+ xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
81
+ xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
82
+ xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
83
+
84
+ xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
85
+ xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
86
+ xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
87
+ xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
88
+ xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
89
+ }
90
+ ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
91
+ ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
92
+ ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
93
+ if (layer.time_mix_receptance_b) {
94
+ r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
95
+ }
96
+ if (layer.time_mix_key_b) {
97
+ k = ggml_add(ctx0, k, layer.time_mix_key_b);
98
+ }
99
+ if (layer.time_mix_value_b) {
100
+ v = ggml_add(ctx0, v, layer.time_mix_value_b);
101
+ }
102
+ ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
103
+ if (is_qrwkv) {
104
+ g = ggml_sigmoid(ctx0, g);
105
+ } else {
106
+ g = ggml_silu(ctx0, g);
107
+ }
108
+ if (n_head_kv != 0 && n_head_kv != n_head) {
109
+ GGML_ASSERT(n_head % n_head_kv == 0);
110
+ k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
111
+ v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
112
+ ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
113
+ k = ggml_repeat(ctx0, k, tmp);
114
+ v = ggml_repeat(ctx0, v, tmp);
115
+ }
116
+ k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
117
+ v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
118
+ r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
119
+
120
+ ggml_tensor * w =
121
+ ggml_mul_mat(ctx0, layer.time_mix_decay_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)));
122
+
123
+ w = ggml_add(ctx0, w, layer.time_mix_decay);
124
+ w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
125
+ w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
126
+
127
+ if (is_qrwkv) {
128
+ // k = k * (1 - w)
129
+ k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
130
+ }
131
+ ggml_tensor * wkv_state = build_rs(inp, mctx_cur->get_s_l(il), hparams.n_embd_s(), n_seqs);
132
+
133
+ ggml_tensor * wkv_output;
134
+ if (is_qrwkv) {
135
+ wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
136
+ } else {
137
+ wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
138
+ }
139
+ cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
140
+ wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
141
+
142
+ ggml_build_forward_expand(
143
+ gf, ggml_cpy(ctx0, wkv_state,
144
+ ggml_view_1d(ctx0, mctx_cur->get_s_l(il), hparams.n_embd_s() * n_seqs,
145
+ hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il)))));
146
+
147
+ if (!is_qrwkv) {
148
+ // group norm with head_count groups
149
+ cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
150
+ cur = ggml_norm(ctx0, cur, 64e-5f);
151
+
152
+ // Convert back to regular vectors.
153
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
154
+ cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
155
+ } else {
156
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
157
+ }
158
+ cur = ggml_mul(ctx0, cur, g);
159
+ cur = build_lora_mm(layer.time_mix_output, cur);
160
+
161
+ return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
162
+ }
@@ -0,0 +1,94 @@
1
+ #include "models.h"
2
+
3
+ llm_build_rwkv6::llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) :
4
+ llm_build_rwkv6_base(model, params) {
5
+ GGML_ASSERT(hparams.token_shift_count == 2);
6
+
7
+ ggml_tensor * cur;
8
+ ggml_tensor * inpL;
9
+
10
+ inpL = build_inp_embd(model.tok_embd);
11
+ inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
12
+
13
+ auto * rs_inp = build_rs_inp();
14
+
15
+ const auto n_embd = hparams.n_embd;
16
+ const auto n_seq_tokens = ubatch.n_seq_tokens;
17
+ const auto n_seqs = ubatch.n_seqs;
18
+
19
+ ggml_tensor * inp_out_ids = build_inp_out_ids();
20
+
21
+ for (int il = 0; il < n_layer; ++il) {
22
+ const llama_layer * layer = &model.layers[il];
23
+ inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
24
+
25
+ ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
26
+
27
+ ggml_tensor * att_shift =
28
+ ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
29
+ ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1],
30
+ token_shift->nb[2], n_embd * ggml_element_size(token_shift));
31
+
32
+ ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
33
+ cb(att_norm, "attn_norm", il);
34
+
35
+ ggml_tensor * x_prev = ggml_concat(
36
+ ctx0, att_shift,
37
+ ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0), 1);
38
+
39
+ cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
40
+
41
+ ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
42
+ cb(ffn_inp, "ffn_inp", il);
43
+
44
+ ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
45
+ cb(ffn_norm, "ffn_norm", il);
46
+
47
+ x_prev = ggml_concat(
48
+ ctx0, ffn_shift,
49
+ ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0), 1);
50
+
51
+ token_shift = ggml_concat(ctx0,
52
+ ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2],
53
+ (n_seq_tokens - 1) * n_embd * ggml_element_size(att_norm)),
54
+ ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2],
55
+ (n_seq_tokens - 1) * n_embd * ggml_element_size(ffn_norm)),
56
+ 1);
57
+ ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
58
+
59
+ ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
60
+ ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
61
+ x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
62
+ cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
63
+
64
+ if (il == n_layer - 1 && inp_out_ids) {
65
+ ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
66
+ ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
67
+ x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
68
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
69
+ }
70
+ cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
71
+ cur = ggml_add(ctx0, cur, ffn_inp);
72
+
73
+ if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
74
+ cur = ggml_scale(ctx0, cur, 0.5F);
75
+ }
76
+ cur = build_cvec(cur, il);
77
+ cb(cur, "l_out", il);
78
+
79
+ // input for next layer
80
+ inpL = cur;
81
+ }
82
+ cur = inpL;
83
+ cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
84
+
85
+ cb(cur, "result_norm", -1);
86
+ res->t_embd = cur;
87
+
88
+ cur = build_lora_mm(model.output, cur);
89
+
90
+ cb(cur, "result_output", -1);
91
+ res->t_logits = cur;
92
+
93
+ ggml_build_forward_expand(gf, cur);
94
+ }