@fugood/llama.node 0.0.1-alpha.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 (204) hide show
  1. package/CMakeLists.txt +85 -0
  2. package/README.md +56 -0
  3. package/bin/darwin/arm64/llama-node.node +0 -0
  4. package/bin/darwin/x64/llama-node.node +0 -0
  5. package/bin/linux/arm64/llama-node.node +0 -0
  6. package/bin/linux/x64/llama-node.node +0 -0
  7. package/bin/win32/arm64/llama-node.node +0 -0
  8. package/bin/win32/arm64/node.lib +0 -0
  9. package/bin/win32/x64/llama-node.node +0 -0
  10. package/bin/win32/x64/node.lib +0 -0
  11. package/lib/binding.js +13 -0
  12. package/lib/binding.ts +57 -0
  13. package/lib/index.js +24 -0
  14. package/lib/index.ts +13 -0
  15. package/package.json +65 -0
  16. package/src/addons.cpp +506 -0
  17. package/src/llama.cpp/CMakeLists.txt +1320 -0
  18. package/src/llama.cpp/build.zig +172 -0
  19. package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
  20. package/src/llama.cpp/common/CMakeLists.txt +87 -0
  21. package/src/llama.cpp/common/base64.hpp +392 -0
  22. package/src/llama.cpp/common/common.cpp +2949 -0
  23. package/src/llama.cpp/common/common.h +324 -0
  24. package/src/llama.cpp/common/console.cpp +501 -0
  25. package/src/llama.cpp/common/console.h +19 -0
  26. package/src/llama.cpp/common/grammar-parser.cpp +440 -0
  27. package/src/llama.cpp/common/grammar-parser.h +29 -0
  28. package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
  29. package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
  30. package/src/llama.cpp/common/json.hpp +24766 -0
  31. package/src/llama.cpp/common/log.h +724 -0
  32. package/src/llama.cpp/common/ngram-cache.cpp +282 -0
  33. package/src/llama.cpp/common/ngram-cache.h +94 -0
  34. package/src/llama.cpp/common/sampling.cpp +353 -0
  35. package/src/llama.cpp/common/sampling.h +147 -0
  36. package/src/llama.cpp/common/stb_image.h +8396 -0
  37. package/src/llama.cpp/common/train.cpp +1513 -0
  38. package/src/llama.cpp/common/train.h +233 -0
  39. package/src/llama.cpp/examples/CMakeLists.txt +52 -0
  40. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
  41. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
  42. package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
  43. package/src/llama.cpp/examples/batched/batched.cpp +262 -0
  44. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
  45. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
  46. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
  47. package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
  48. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
  49. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
  50. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
  52. package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
  53. package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
  54. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
  55. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
  56. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
  58. package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
  59. package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
  60. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
  61. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
  62. package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
  63. package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
  64. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
  65. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
  66. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
  67. package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
  68. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
  69. package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
  70. package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
  71. package/src/llama.cpp/examples/infill/infill.cpp +767 -0
  72. package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
  73. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
  74. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
  75. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
  76. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
  77. package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
  78. package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
  79. package/src/llama.cpp/examples/llava/clip.h +85 -0
  80. package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
  81. package/src/llama.cpp/examples/llava/llava.cpp +426 -0
  82. package/src/llama.cpp/examples/llava/llava.h +50 -0
  83. package/src/llama.cpp/examples/llava/requirements.txt +3 -0
  84. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
  85. package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
  86. package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
  87. package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
  88. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
  89. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
  90. package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
  91. package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
  92. package/src/llama.cpp/examples/main/main.cpp +957 -0
  93. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
  94. package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
  95. package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
  96. package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
  97. package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
  98. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
  99. package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
  100. package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
  101. package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
  102. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
  103. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
  104. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
  106. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
  107. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
  108. package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
  109. package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
  110. package/src/llama.cpp/examples/server/httplib.h +9465 -0
  111. package/src/llama.cpp/examples/server/server.cpp +3826 -0
  112. package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
  113. package/src/llama.cpp/examples/server/utils.hpp +653 -0
  114. package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
  115. package/src/llama.cpp/examples/simple/simple.cpp +183 -0
  116. package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
  117. package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
  118. package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
  119. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
  120. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
  121. package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
  122. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
  123. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
  124. package/src/llama.cpp/ggml-alloc.c +985 -0
  125. package/src/llama.cpp/ggml-alloc.h +76 -0
  126. package/src/llama.cpp/ggml-backend-impl.h +141 -0
  127. package/src/llama.cpp/ggml-backend.c +2099 -0
  128. package/src/llama.cpp/ggml-backend.h +233 -0
  129. package/src/llama.cpp/ggml-common.h +1853 -0
  130. package/src/llama.cpp/ggml-cuda.h +43 -0
  131. package/src/llama.cpp/ggml-impl.h +265 -0
  132. package/src/llama.cpp/ggml-kompute.cpp +2006 -0
  133. package/src/llama.cpp/ggml-kompute.h +46 -0
  134. package/src/llama.cpp/ggml-metal.h +66 -0
  135. package/src/llama.cpp/ggml-mpi.c +216 -0
  136. package/src/llama.cpp/ggml-mpi.h +39 -0
  137. package/src/llama.cpp/ggml-opencl.cpp +2301 -0
  138. package/src/llama.cpp/ggml-opencl.h +36 -0
  139. package/src/llama.cpp/ggml-quants.c +12678 -0
  140. package/src/llama.cpp/ggml-quants.h +133 -0
  141. package/src/llama.cpp/ggml-sycl.cpp +17882 -0
  142. package/src/llama.cpp/ggml-sycl.h +49 -0
  143. package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
  144. package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
  145. package/src/llama.cpp/ggml-vulkan.h +29 -0
  146. package/src/llama.cpp/ggml.c +21819 -0
  147. package/src/llama.cpp/ggml.h +2403 -0
  148. package/src/llama.cpp/llama.cpp +17468 -0
  149. package/src/llama.cpp/llama.h +1117 -0
  150. package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
  151. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  152. package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
  153. package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
  154. package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
  155. package/src/llama.cpp/prompts/alpaca.txt +1 -0
  156. package/src/llama.cpp/prompts/assistant.txt +31 -0
  157. package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  158. package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
  159. package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  160. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  161. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  162. package/src/llama.cpp/prompts/chat.txt +28 -0
  163. package/src/llama.cpp/prompts/dan-modified.txt +1 -0
  164. package/src/llama.cpp/prompts/dan.txt +1 -0
  165. package/src/llama.cpp/prompts/mnemonics.txt +93 -0
  166. package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
  167. package/src/llama.cpp/prompts/reason-act.txt +18 -0
  168. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
  169. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
  170. package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
  171. package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
  172. package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
  173. package/src/llama.cpp/requirements.txt +12 -0
  174. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
  175. package/src/llama.cpp/scripts/xxd.cmake +16 -0
  176. package/src/llama.cpp/sgemm.cpp +999 -0
  177. package/src/llama.cpp/sgemm.h +12 -0
  178. package/src/llama.cpp/tests/CMakeLists.txt +78 -0
  179. package/src/llama.cpp/tests/get-model.cpp +21 -0
  180. package/src/llama.cpp/tests/get-model.h +2 -0
  181. package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
  182. package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
  183. package/src/llama.cpp/tests/test-c.c +7 -0
  184. package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
  185. package/src/llama.cpp/tests/test-double-float.cpp +57 -0
  186. package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
  187. package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
  188. package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
  189. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
  190. package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
  191. package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
  192. package/src/llama.cpp/tests/test-opt.cpp +181 -0
  193. package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
  194. package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
  195. package/src/llama.cpp/tests/test-rope.cpp +221 -0
  196. package/src/llama.cpp/tests/test-sampling.cpp +301 -0
  197. package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
  198. package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
  199. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
  200. package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
  201. package/src/llama.cpp/unicode-data.cpp +1651 -0
  202. package/src/llama.cpp/unicode-data.h +16 -0
  203. package/src/llama.cpp/unicode.cpp +277 -0
  204. package/src/llama.cpp/unicode.h +28 -0
@@ -0,0 +1,1640 @@
1
+ #include "ggml.h"
2
+ #include "train.h"
3
+
4
+ #include <vector>
5
+ #include <cassert>
6
+ #include <cstdlib>
7
+ #include <cstring>
8
+ #include <random>
9
+ #include <vector>
10
+
11
+ #if defined(_MSC_VER)
12
+ #pragma warning(disable: 4244 4267) // possible loss of data
13
+ #endif
14
+
15
+ #ifdef LLAMA_DEFAULT_RMS_EPS
16
+ constexpr float rms_norm_eps = LLAMA_DEFAULT_RMS_EPS;
17
+ #else
18
+ constexpr float rms_norm_eps = 5e-6f;
19
+ #endif
20
+
21
+ static void ggml_graph_compute_helper(std::vector<uint8_t> & buf, ggml_cgraph * graph, int n_threads) {
22
+ struct ggml_cplan plan = ggml_graph_plan(graph, n_threads);
23
+
24
+ if (plan.work_size > 0) {
25
+ buf.resize(plan.work_size);
26
+ plan.work_data = buf.data();
27
+ }
28
+
29
+ ggml_graph_compute(graph, &plan);
30
+ }
31
+
32
+ static struct ggml_tensor * randomize_tensor(
33
+ struct ggml_tensor * tensor, int ndims, const int64_t ne[], float fmin, float fmax
34
+ ) {
35
+ switch (ndims) {
36
+ case 1:
37
+ for (int i0 = 0; i0 < ne[0]; i0++) {
38
+ ((float *)tensor->data)[i0] = frand()*(fmax - fmin) + fmin;
39
+ }
40
+ break;
41
+ case 2:
42
+ for (int i1 = 0; i1 < ne[1]; i1++) {
43
+ for (int i0 = 0; i0 < ne[0]; i0++) {
44
+ ((float *)tensor->data)[i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
45
+ }
46
+ }
47
+ break;
48
+ case 3:
49
+ for (int i2 = 0; i2 < ne[2]; i2++) {
50
+ for (int i1 = 0; i1 < ne[1]; i1++) {
51
+ for (int i0 = 0; i0 < ne[0]; i0++) {
52
+ ((float *)tensor->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
53
+ }
54
+ }
55
+ }
56
+ break;
57
+ case 4:
58
+ for (int i3 = 0; i3 < ne[3]; i3++) {
59
+ for (int i2 = 0; i2 < ne[2]; i2++) {
60
+ for (int i1 = 0; i1 < ne[1]; i1++) {
61
+ for (int i0 = 0; i0 < ne[0]; i0++) {
62
+ ((float *)tensor->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
63
+ }
64
+ }
65
+ }
66
+ }
67
+ break;
68
+ default:
69
+ assert(false);
70
+ }
71
+
72
+ return tensor;
73
+ }
74
+
75
+ struct llama_hparams {
76
+ uint32_t n_vocab = 32000;
77
+ uint32_t n_ctx = 512; // this is provided as user input?
78
+ uint32_t n_embd = 4096;
79
+ uint32_t n_mult = 4;
80
+ uint32_t n_head = 32;
81
+ uint32_t n_layer = 32;
82
+ uint32_t n_rot = 64;
83
+
84
+ bool operator!=(const llama_hparams & other) const {
85
+ return memcmp(this, &other, sizeof(llama_hparams));
86
+ }
87
+ };
88
+
89
+ static uint32_t get_n_ff(const struct llama_hparams* hparams) {
90
+ const uint32_t n_ff = ((2*(4*hparams->n_embd)/3 + hparams->n_mult - 1)/hparams->n_mult)*hparams->n_mult;
91
+ return n_ff;
92
+ }
93
+
94
+ struct llama_hparams_lora {
95
+ uint32_t n_vocab = 32000;
96
+ uint32_t n_ctx = 512; // this is provided as user input?
97
+ uint32_t n_embd = 4096;
98
+ uint32_t n_mult = 4;
99
+ uint32_t n_head = 32;
100
+ uint32_t n_layer = 32;
101
+ uint32_t n_rot = 64;
102
+ uint32_t n_lora = 64;
103
+
104
+ bool operator!=(const llama_hparams_lora & other) const {
105
+ return memcmp(this, &other, sizeof(llama_hparams_lora)) != 0;
106
+ }
107
+ };
108
+
109
+ struct llama_layer {
110
+ // normalization
111
+ struct ggml_tensor * attention_norm;
112
+
113
+ // attention
114
+ struct ggml_tensor * wq;
115
+ struct ggml_tensor * wk;
116
+ struct ggml_tensor * wv;
117
+ struct ggml_tensor * wo;
118
+
119
+ // normalization
120
+ struct ggml_tensor * ffn_norm;
121
+
122
+ // ff
123
+ struct ggml_tensor * w1;
124
+ struct ggml_tensor * w2;
125
+ struct ggml_tensor * w3;
126
+ };
127
+
128
+ struct llama_layer_lora {
129
+ // normalization
130
+ struct ggml_tensor * attention_norm;
131
+
132
+ // attention
133
+ struct ggml_tensor * wqa;
134
+ struct ggml_tensor * wqb;
135
+ struct ggml_tensor * wka;
136
+ struct ggml_tensor * wkb;
137
+ struct ggml_tensor * wva;
138
+ struct ggml_tensor * wvb;
139
+ struct ggml_tensor * woa;
140
+ struct ggml_tensor * wob;
141
+
142
+ // normalization
143
+ struct ggml_tensor * ffn_norm;
144
+
145
+ // ff
146
+ struct ggml_tensor * w1;
147
+ struct ggml_tensor * w2;
148
+ struct ggml_tensor * w3;
149
+ };
150
+
151
+
152
+ struct llama_kv_cache {
153
+ struct ggml_context * ctx = NULL;
154
+
155
+ struct ggml_tensor * k;
156
+ struct ggml_tensor * v;
157
+
158
+ // llama_ctx_buffer buf;
159
+
160
+ int n; // number of tokens currently in the cache
161
+ };
162
+
163
+ struct llama_model {
164
+ struct ggml_context * ctx = NULL;
165
+
166
+ llama_hparams hparams;
167
+
168
+ struct ggml_tensor * tok_embeddings;
169
+
170
+ struct ggml_tensor * norm;
171
+ struct ggml_tensor * output;
172
+
173
+ std::vector<llama_layer> layers;
174
+ };
175
+
176
+ struct llama_model_lora {
177
+ struct ggml_context * ctx = NULL;
178
+
179
+ llama_hparams_lora hparams;
180
+
181
+ struct ggml_tensor * tok_embeddings;
182
+
183
+ struct ggml_tensor * norm;
184
+ struct ggml_tensor * outputa;
185
+ struct ggml_tensor * outputb;
186
+
187
+ std::vector<llama_layer_lora> layers;
188
+ };
189
+
190
+ static void init_model(struct llama_model * model) {
191
+ const auto & hparams = model->hparams;
192
+
193
+ const uint32_t n_embd = hparams.n_embd;
194
+ const uint32_t n_layer = hparams.n_layer;
195
+ const uint32_t n_vocab = hparams.n_vocab;
196
+
197
+ const uint32_t n_ff = get_n_ff(&hparams);
198
+
199
+ struct ggml_context * ctx = model->ctx;
200
+
201
+ model->tok_embeddings = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_vocab); // ("tok_embeddings.weight", {n_embd, n_vocab});
202
+ model->norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // ("norm.weight", {n_embd});
203
+ model->output = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_vocab); // ("output.weight", {n_embd, n_vocab});
204
+
205
+ model->layers.resize(n_layer);
206
+ for (uint32_t i = 0; i < n_layer; ++i) {
207
+ auto & layer = model->layers[i];
208
+
209
+ // std::string layers_i = "layers." + std::to_string(i);
210
+
211
+ layer.attention_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // (layers_i + ".attention_norm.weight", {n_embd});
212
+
213
+ layer.wq = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_embd); // (layers_i + ".attention.wq.weight", {n_embd, n_embd});
214
+ layer.wk = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_embd); // (layers_i + ".attention.wk.weight", {n_embd, n_embd});
215
+ layer.wv = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_embd); // (layers_i + ".attention.wv.weight", {n_embd, n_embd});
216
+ layer.wo = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_embd); // (layers_i + ".attention.wo.weight", {n_embd, n_embd});
217
+
218
+ layer.ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // (layers_i + ".ffn_norm.weight", {n_embd});
219
+
220
+ layer.w1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff); // (layers_i + ".feed_forward.w1.weight", {n_embd, n_ff});
221
+ layer.w2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd); // (layers_i + ".feed_forward.w2.weight", { n_ff, n_embd});
222
+ layer.w3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff); // (layers_i + ".feed_forward.w3.weight", {n_embd, n_ff});
223
+ }
224
+ }
225
+
226
+
227
+ static void init_model_lora(struct llama_model_lora * model) {
228
+ const auto & hparams = model->hparams;
229
+
230
+ const uint32_t n_embd = hparams.n_embd;
231
+ const uint32_t n_mult = hparams.n_mult;
232
+ const uint32_t n_layer = hparams.n_layer;
233
+ const uint32_t n_vocab = hparams.n_vocab;
234
+ const uint32_t n_lora = hparams.n_lora;
235
+
236
+ const uint32_t n_ff = ((2*(4*n_embd)/3 + n_mult - 1)/n_mult)*n_mult;
237
+
238
+ struct ggml_context * ctx = model->ctx;
239
+
240
+ model->tok_embeddings = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_vocab); // ("tok_embeddings.weight", {n_embd, n_vocab});
241
+ model->norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // ("norm.weight", {n_embd});
242
+ model->outputa = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lora, n_vocab); // ("output.weight", {n_embd, n_vocab});
243
+ model->outputb = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_lora); // ("output.weight", {n_embd, n_vocab});
244
+
245
+ model->layers.resize(n_layer);
246
+ for (uint32_t i = 0; i < n_layer; ++i) {
247
+ auto & layer = model->layers[i];
248
+
249
+ // std::string layers_i = "layers." + std::to_string(i);
250
+
251
+ layer.attention_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // (layers_i + ".attention_norm.weight", {n_embd});
252
+
253
+ layer.wqa = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lora, n_embd); // (layers_i + ".attention.wq.weight", {n_embd, n_embd});
254
+ layer.wqb = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_lora); // (layers_i + ".attention.wq.weight", {n_embd, n_embd});
255
+ layer.wka = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lora, n_embd); // (layers_i + ".attention.wk.weight", {n_embd, n_embd});
256
+ layer.wkb = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_lora); // (layers_i + ".attention.wk.weight", {n_embd, n_embd});
257
+ layer.wva = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lora, n_embd); // (layers_i + ".attention.wv.weight", {n_embd, n_embd});
258
+ layer.wvb = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_lora); // (layers_i + ".attention.wv.weight", {n_embd, n_embd});
259
+ layer.woa = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lora, n_embd); // (layers_i + ".attention.wo.weight", {n_embd, n_embd});
260
+ layer.wob = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_lora); // (layers_i + ".attention.wo.weight", {n_embd, n_embd});
261
+
262
+ layer.ffn_norm = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_embd); // (layers_i + ".ffn_norm.weight", {n_embd});
263
+
264
+ layer.w1 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff); // (layers_i + ".feed_forward.w1.weight", {n_embd, n_ff});
265
+ layer.w2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_ff, n_embd); // (layers_i + ".feed_forward.w2.weight", { n_ff, n_embd});
266
+ layer.w3 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_ff); // (layers_i + ".feed_forward.w3.weight", {n_embd, n_ff});
267
+ }
268
+ }
269
+
270
+ static void set_param_model(struct llama_model * model) {
271
+ const auto& hparams = model->hparams;
272
+
273
+ const uint32_t n_layer = hparams.n_layer;
274
+
275
+ struct ggml_context* ctx = model->ctx;
276
+
277
+ ggml_set_param(ctx, model->tok_embeddings);
278
+ ggml_set_param(ctx, model->norm);
279
+ ggml_set_param(ctx, model->output);
280
+
281
+ for (uint32_t i = 0; i < n_layer; ++i) {
282
+ auto & layer = model->layers[i];
283
+
284
+ ggml_set_param(ctx, layer.attention_norm);
285
+ ggml_set_param(ctx, layer.wq);
286
+ ggml_set_param(ctx, layer.wk);
287
+ ggml_set_param(ctx, layer.wv);
288
+ ggml_set_param(ctx, layer.wo);
289
+ ggml_set_param(ctx, layer.ffn_norm);
290
+ ggml_set_param(ctx, layer.w1);
291
+ ggml_set_param(ctx, layer.w2);
292
+ ggml_set_param(ctx, layer.w3);
293
+ }
294
+ }
295
+
296
+ static void set_param_model_lora(struct llama_model_lora * model) {
297
+ const auto& hparams = model->hparams;
298
+
299
+ const uint32_t n_layer = hparams.n_layer;
300
+
301
+ struct ggml_context* ctx = model->ctx;
302
+
303
+ ggml_set_param(ctx, model->tok_embeddings);
304
+ ggml_set_param(ctx, model->norm);
305
+ ggml_set_param(ctx, model->outputa);
306
+ ggml_set_param(ctx, model->outputb);
307
+
308
+ for (uint32_t i = 0; i < n_layer; ++i) {
309
+ auto & layer = model->layers[i];
310
+
311
+ ggml_set_param(ctx, layer.attention_norm);
312
+ ggml_set_param(ctx, layer.wqa);
313
+ ggml_set_param(ctx, layer.wqb);
314
+ ggml_set_param(ctx, layer.wka);
315
+ ggml_set_param(ctx, layer.wkb);
316
+ ggml_set_param(ctx, layer.wva);
317
+ ggml_set_param(ctx, layer.wvb);
318
+ ggml_set_param(ctx, layer.woa);
319
+ ggml_set_param(ctx, layer.wob);
320
+ ggml_set_param(ctx, layer.ffn_norm);
321
+ ggml_set_param(ctx, layer.w1);
322
+ ggml_set_param(ctx, layer.w2);
323
+ ggml_set_param(ctx, layer.w3);
324
+ }
325
+ }
326
+
327
+ static void randomize_model(struct llama_model * model, int seed, float mean, float std, float min, float max) {
328
+ const auto & hparams = model->hparams;
329
+
330
+ const uint32_t n_layer = hparams.n_layer;
331
+
332
+ struct random_normal_distribution * rnd = init_random_normal_distribution(seed, mean, std, min, max);
333
+
334
+ randomize_tensor_normal(model->tok_embeddings , rnd);
335
+ randomize_tensor_normal(model->norm , rnd);
336
+ randomize_tensor_normal(model->output , rnd);
337
+
338
+ for (uint32_t i = 0; i < n_layer; ++i) {
339
+ auto & layer = model->layers[i];
340
+ randomize_tensor_normal(layer.attention_norm, rnd);
341
+
342
+ randomize_tensor_normal(layer.wq, rnd);
343
+ randomize_tensor_normal(layer.wk, rnd);
344
+ randomize_tensor_normal(layer.wv, rnd);
345
+ randomize_tensor_normal(layer.wo, rnd);
346
+
347
+ randomize_tensor_normal(layer.ffn_norm, rnd);
348
+
349
+ randomize_tensor_normal(layer.w1, rnd);
350
+ randomize_tensor_normal(layer.w2, rnd);
351
+ randomize_tensor_normal(layer.w3, rnd);
352
+ }
353
+
354
+ free_random_normal_distribution(rnd);
355
+ }
356
+
357
+
358
+ static void randomize_model_lora(
359
+ struct llama_model_lora * model, int seed, float mean, float std, float min, float max
360
+ ) {
361
+ const auto & hparams = model->hparams;
362
+
363
+ const uint32_t n_layer = hparams.n_layer;
364
+
365
+ struct random_normal_distribution * rnd = init_random_normal_distribution(seed, mean, std, min, max);
366
+
367
+ randomize_tensor_normal(model->tok_embeddings, rnd);
368
+ randomize_tensor_normal(model->norm , rnd);
369
+ randomize_tensor_normal(model->outputa , rnd);
370
+ randomize_tensor_normal(model->outputb , rnd);
371
+
372
+ for (uint32_t i = 0; i < n_layer; ++i) {
373
+ auto & layer = model->layers[i];
374
+ randomize_tensor_normal(layer.attention_norm, rnd);
375
+
376
+ randomize_tensor_normal(layer.wqa, rnd);
377
+ randomize_tensor_normal(layer.wqb, rnd);
378
+ randomize_tensor_normal(layer.wka, rnd);
379
+ randomize_tensor_normal(layer.wkb, rnd);
380
+ randomize_tensor_normal(layer.wva, rnd);
381
+ randomize_tensor_normal(layer.wvb, rnd);
382
+ randomize_tensor_normal(layer.woa, rnd);
383
+ randomize_tensor_normal(layer.wob, rnd);
384
+
385
+ randomize_tensor_normal(layer.ffn_norm, rnd);
386
+
387
+ randomize_tensor_normal(layer.w1, rnd);
388
+ randomize_tensor_normal(layer.w2, rnd);
389
+ randomize_tensor_normal(layer.w3, rnd);
390
+ }
391
+
392
+ free_random_normal_distribution(rnd);
393
+ }
394
+
395
+ static void init_kv_cache(struct llama_kv_cache* cache, struct llama_model * model, int n_batch) {
396
+ const auto & hparams = model->hparams;
397
+
398
+ const uint32_t n_ctx = hparams.n_ctx;
399
+ const uint32_t n_embd = hparams.n_embd;
400
+ const uint32_t n_layer = hparams.n_layer;
401
+
402
+ const int64_t n_mem = n_layer*n_ctx*n_batch;
403
+ const int64_t n_elements = n_embd*n_mem;
404
+
405
+ // cache.buf.resize(2u*n_elements*ggml_type_size(wtype) + 2u*MB);
406
+
407
+ // struct ggml_init_params params;
408
+ // params.mem_size = cache.buf.size;
409
+ // params.mem_buffer = cache.buf.addr;
410
+ // params.no_alloc = false;
411
+ if (!cache->ctx) {
412
+ struct ggml_init_params params;
413
+ params.mem_size = 2u*n_elements*ggml_type_size(GGML_TYPE_F32) + 2u*1024*1024;
414
+ params.mem_buffer = NULL;
415
+ params.no_alloc = false;
416
+
417
+ cache->ctx = ggml_init(params);
418
+
419
+ if (!cache->ctx) {
420
+ fprintf(stderr, "%s: failed to allocate memory for kv cache\n", __func__);
421
+ exit(1);
422
+ }
423
+ }
424
+
425
+ cache->k = ggml_new_tensor_1d(cache->ctx, GGML_TYPE_F32, n_elements);
426
+ cache->v = ggml_new_tensor_1d(cache->ctx, GGML_TYPE_F32, n_elements);
427
+ }
428
+
429
+ static bool init_kv_cache_lora(struct llama_kv_cache* cache, struct llama_model_lora * model, int n_batch) {
430
+ const auto & hparams = model->hparams;
431
+
432
+ const uint32_t n_ctx = hparams.n_ctx;
433
+ const uint32_t n_embd = hparams.n_embd;
434
+ const uint32_t n_layer = hparams.n_layer;
435
+
436
+ const int64_t n_mem = n_layer*n_ctx*n_batch;
437
+ const int64_t n_elements = n_embd*n_mem;
438
+
439
+ // cache.buf.resize(2u*n_elements*ggml_type_size(wtype) + 2u*MB);
440
+
441
+ // struct ggml_init_params params;
442
+ // params.mem_size = cache.buf.size;
443
+ // params.mem_buffer = cache.buf.addr;
444
+ // params.no_alloc = false;
445
+ if (!cache->ctx) {
446
+ struct ggml_init_params params;
447
+ params.mem_size = 2u*n_elements*ggml_type_size(GGML_TYPE_F32) + 2u*1024*1024;
448
+ params.mem_buffer = NULL;
449
+ params.no_alloc = false;
450
+
451
+ cache->ctx = ggml_init(params);
452
+
453
+ if (!cache->ctx) {
454
+ fprintf(stderr, "%s: failed to allocate memory for kv cache\n", __func__);
455
+ return false;
456
+ }
457
+ }
458
+
459
+ cache->k = ggml_new_tensor_1d(cache->ctx, GGML_TYPE_F32, n_elements);
460
+ cache->v = ggml_new_tensor_1d(cache->ctx, GGML_TYPE_F32, n_elements);
461
+
462
+ return true;
463
+ }
464
+
465
+ static struct ggml_tensor * forward(
466
+ struct llama_model * model,
467
+ struct llama_kv_cache * cache,
468
+ struct ggml_context * ctx0,
469
+ struct ggml_cgraph * gf,
470
+ struct ggml_tensor * tokens_input,
471
+ const int n_tokens,
472
+ const int n_past
473
+ ) {
474
+ const int N = n_tokens;
475
+
476
+ struct llama_kv_cache& kv_self = *cache;
477
+ const auto & hparams = model->hparams;
478
+ const int n_ctx = hparams.n_ctx;
479
+ const int n_embd = hparams.n_embd;
480
+ const int n_layer = hparams.n_layer;
481
+ const int n_head = hparams.n_head;
482
+ const int n_rot = hparams.n_rot;
483
+
484
+ struct ggml_tensor * tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
485
+ memcpy(tokens->data, tokens_input->data, N*ggml_element_size(tokens));
486
+
487
+ struct ggml_tensor * kc = kv_self.k;
488
+ struct ggml_tensor * vc = kv_self.v;
489
+
490
+ struct ggml_tensor * KQ_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
491
+ {
492
+ int * data = (int *) KQ_pos->data;
493
+ for (int i = 0; i < N; ++i) {
494
+ data[i] = n_past + i;
495
+ }
496
+ }
497
+
498
+ // inpL shape [n_embd,N,1,1]
499
+ struct ggml_tensor * inpL = ggml_get_rows(ctx0, model->tok_embeddings, tokens);
500
+ for (int il = 0; il < n_layer; ++il) {
501
+ struct ggml_tensor * inpSA = inpL;
502
+
503
+ struct ggml_tensor * cur;
504
+
505
+ // lctx.use_buf(ctx0, 0);
506
+
507
+ // norm
508
+ {
509
+ // cur shape [n_embd,N,1,1]
510
+ cur = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
511
+
512
+ // cur = attention_norm*cur
513
+ cur = ggml_mul(ctx0,
514
+ ggml_repeat(ctx0, model->layers[il].attention_norm, cur),
515
+ cur);
516
+ }
517
+
518
+ // self-attention
519
+ {
520
+ // compute Q and K and RoPE them
521
+ // wq shape [n_embd, n_embd, 1, 1]
522
+ // wk shape [n_embd, n_embd, 1, 1]
523
+ // Qcur shape [n_embd/n_head, n_head, N, 1]
524
+ // Kcur shape [n_embd/n_head, n_head, N, 1]
525
+ struct ggml_tensor * Qcur = ggml_rope(ctx0, ggml_reshape_3d(ctx0, ggml_mul_mat(ctx0, model->layers[il].wq, cur), n_embd/n_head, n_head, N), KQ_pos, n_rot, 0, 0);
526
+ struct ggml_tensor * Kcur = ggml_rope(ctx0, ggml_reshape_3d(ctx0, ggml_mul_mat(ctx0, model->layers[il].wk, cur), n_embd/n_head, n_head, N), KQ_pos, n_rot, 0, 0);
527
+
528
+ // store key and value to memory
529
+ {
530
+ // compute the transposed [N, n_embd] V matrix
531
+ // wv shape [n_embd, n_embd, 1, 1]
532
+ // Vcur shape [n_embd, N, 1, 1]
533
+ struct ggml_tensor * Vcur = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, ggml_mul_mat(ctx0, model->layers[il].wv, cur), n_embd, N)));
534
+
535
+ // kv_self.k shape [n_embd * n_ctx * n_layer, 1]
536
+ // kv_self.v shape [n_embd * n_ctx * n_layer, 1]
537
+ // k shape [n_embd * N, 1] == kv_self.k[:,n_past:n_past+N,il,0]
538
+ // v shape [N, n_embd, 1, 1] == kv_self.v[:,n_past:n_past+N,il,0]
539
+
540
+ /* {
541
+ struct ggml_tensor * k = ggml_view_1d(ctx0, kv_self.k, N*n_embd, (ggml_element_size(kv_self.k)*n_embd)*(il*n_ctx + n_past));
542
+ struct ggml_tensor * v = ggml_view_2d(ctx0, kv_self.v, N, n_embd,
543
+ ( n_ctx)*ggml_element_size(kv_self.v),
544
+ (il*n_ctx)*ggml_element_size(kv_self.v)*n_embd + n_past*ggml_element_size(kv_self.v));
545
+
546
+ // important: storing RoPE-ed version of K in the KV cache!
547
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, k));
548
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, v));
549
+ } //*/
550
+
551
+ kc = ggml_set_1d(ctx0, kc, ggml_reshape_1d(ctx0, Kcur, n_embd*N), (ggml_element_size(kv_self.k)*n_embd)*(il*n_ctx + n_past));
552
+ vc = ggml_set_2d(ctx0, vc, Vcur, ( n_ctx)*ggml_element_size(kv_self.v),
553
+ (il*n_ctx)*ggml_element_size(kv_self.v)*n_embd + n_past*ggml_element_size(kv_self.v));
554
+ }
555
+
556
+ // Qcur shape [n_embd/n_head, n_head, N, 1]
557
+ // Q shape [n_embd/n_head, N, n_head, 1]
558
+ struct ggml_tensor * Q =
559
+ ggml_permute(ctx0,
560
+ Qcur,
561
+ 0, 2, 1, 3);
562
+
563
+ // kv_self.k shape [n_embd * n_ctx * n_layer, 1]
564
+ // K shape [n_embd/n_head, n_past + N, n_head, 1]
565
+ struct ggml_tensor * K =
566
+ ggml_permute(ctx0,
567
+ ggml_reshape_3d(ctx0,
568
+ ggml_view_1d(ctx0, kc, (n_past + N)*n_embd, il*n_ctx*ggml_element_size(kc)*n_embd),
569
+ n_embd/n_head, n_head, n_past + N),
570
+ 0, 2, 1, 3);
571
+
572
+ // K * Q
573
+ // KQ shape [n_past + N, N, n_head, 1]
574
+ struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
575
+
576
+ // KQ_scaled = KQ / sqrt(n_embd/n_head)
577
+ // KQ_scaled shape [n_past + N, N, n_head, 1]
578
+ struct ggml_tensor * KQ_scaled = ggml_scale(ctx0, KQ, 1.0f/sqrtf(float(n_embd)/n_head));
579
+
580
+ // KQ_masked = mask_past(KQ_scaled)
581
+ // KQ_masked shape [n_past + N, N, n_head, 1]
582
+ struct ggml_tensor * KQ_masked = ggml_diag_mask_inf(ctx0, KQ_scaled, n_past);
583
+
584
+ // KQ = soft_max(KQ_masked)
585
+ // KQ_soft_max shape [n_past + N, N, n_head, 1]
586
+ struct ggml_tensor * KQ_soft_max = ggml_soft_max(ctx0, KQ_masked);
587
+
588
+ // split cached V into n_head heads
589
+ //// V shape [n_past + N, n_embd/n_head, n_head, 1]
590
+ // V shape [n_past + N, n_embd/n_head, n_head, 1] == kv_self.v[:,:(n_past+N),il,1]
591
+ struct ggml_tensor * V =
592
+ ggml_view_3d(ctx0, vc,
593
+ n_past + N, n_embd/n_head, n_head,
594
+ n_ctx*ggml_element_size(vc),
595
+ n_ctx*ggml_element_size(vc)*n_embd/n_head,
596
+ il*n_ctx*ggml_element_size(vc)*n_embd);
597
+
598
+ // KQV shape [n_embd/n_head, N, n_head, 1]
599
+ struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V, KQ_soft_max);
600
+
601
+ // KQV_merged = KQV.permute(0, 2, 1, 3)
602
+ // KQV_merged shape [n_embd/n_head, n_head, N, 1]
603
+ struct ggml_tensor * KQV_merged = ggml_permute(ctx0, KQV, 0, 2, 1, 3);
604
+ // KQV_merged shape
605
+
606
+ // cur = KQV_merged.contiguous().view(n_embd, N)
607
+ // cur shape [n_embd,N,1,1]
608
+ cur = ggml_reshape_2d(ctx0, ggml_cont(ctx0, KQV_merged), n_embd, N);
609
+ // cur = ggml_cpy(ctx0,
610
+ // KQV_merged,
611
+ // ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, N));
612
+
613
+ // projection (no bias)
614
+ // cur shape [n_embd,N,1,1]
615
+ cur = ggml_mul_mat(ctx0,
616
+ model->layers[il].wo,
617
+ cur);
618
+ }
619
+
620
+ // lctx.use_buf(ctx0, 1);
621
+
622
+ // inpFF shape [n_embd,N,1,1]
623
+ struct ggml_tensor * inpFF = ggml_add(ctx0, cur, inpSA);
624
+
625
+ // feed-forward network
626
+ {
627
+ // norm
628
+ {
629
+ // cur shape [n_embd,N,1,1]
630
+ cur = ggml_rms_norm(ctx0, inpFF, rms_norm_eps);
631
+
632
+ // cur = ffn_norm*cur
633
+ // cur shape [n_embd,N,1,1]
634
+ cur = ggml_mul(ctx0,
635
+ ggml_repeat(ctx0, model->layers[il].ffn_norm, cur),
636
+ cur);
637
+ }
638
+
639
+ // tmp shape [n_ff,N,1,1]
640
+ struct ggml_tensor * tmp = ggml_mul_mat(ctx0,
641
+ model->layers[il].w3,
642
+ cur);
643
+
644
+ // cur shape [n_ff,N,1,1]
645
+ cur = ggml_mul_mat(ctx0,
646
+ model->layers[il].w1,
647
+ cur);
648
+
649
+ // SILU activation
650
+ // cur shape [n_ff,N,1,1]
651
+ cur = ggml_silu(ctx0, cur);
652
+
653
+ // cur shape [n_ff,N,1,1]
654
+ cur = ggml_mul(ctx0, cur, tmp);
655
+
656
+ // cur shape [n_embd,N,1,1]
657
+ cur = ggml_mul_mat(ctx0,
658
+ model->layers[il].w2,
659
+ cur);
660
+ }
661
+
662
+ // cur shape [n_embd,N,1,1]
663
+ cur = ggml_add(ctx0, cur, inpFF);
664
+
665
+ // input for next layer
666
+ // inpL shape [n_embd,N,1,1]
667
+ inpL = cur;
668
+ }
669
+
670
+ // norm
671
+ {
672
+
673
+ // inpL shape [n_embd,N,1,1]
674
+ inpL = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
675
+
676
+ // inpL = norm*inpL
677
+ // inpL shape [n_embd,N,1,1]
678
+ inpL = ggml_mul(ctx0,
679
+ ggml_repeat(ctx0, model->norm, inpL),
680
+ inpL);
681
+
682
+ //embeddings = inpL;
683
+ }
684
+
685
+ // lm_head
686
+ // inpL shape [n_vocab,N,1,1]
687
+ inpL = ggml_mul_mat(ctx0, model->output, inpL);
688
+
689
+ // run the computation
690
+ ggml_build_forward_expand(gf, inpL);
691
+
692
+ return inpL;
693
+ }
694
+
695
+ static struct ggml_tensor * forward_batch(
696
+ struct llama_model * model,
697
+ struct llama_kv_cache * cache,
698
+ struct ggml_context * ctx0,
699
+ struct ggml_cgraph * gf,
700
+ struct ggml_tensor * tokens_input,
701
+ const int n_tokens,
702
+ const int n_past,
703
+ const int n_batch
704
+ ) {
705
+ const int N = n_tokens;
706
+
707
+ struct llama_kv_cache& kv_self = *cache;
708
+ const auto & hparams = model->hparams;
709
+ const int n_ctx = hparams.n_ctx;
710
+ const int n_vocab = hparams.n_vocab;
711
+ const int n_embd = hparams.n_embd;
712
+ const int n_layer = hparams.n_layer;
713
+ const int n_head = hparams.n_head;
714
+ const int n_rot = hparams.n_rot;
715
+ const int n_ff = get_n_ff(&hparams);
716
+
717
+ struct ggml_tensor * tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N*n_batch);
718
+ memcpy(tokens->data, tokens_input->data, ggml_element_size(tokens)*N*n_batch);
719
+
720
+ struct ggml_tensor * kc = kv_self.k;
721
+ struct ggml_tensor * vc = kv_self.v;
722
+
723
+ struct ggml_tensor * KQ_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
724
+ {
725
+ int * data = (int *) KQ_pos->data;
726
+ for (int i = 0; i < N; ++i) {
727
+ data[i] = n_past + i;
728
+ }
729
+ }
730
+
731
+ // inpL shape [n_embd,N*n_batch,1]
732
+ struct ggml_tensor * inpL = ggml_get_rows(ctx0, model->tok_embeddings, tokens);
733
+ assert_shape_2d(inpL, n_embd, N*n_batch);
734
+
735
+ for (int il = 0; il < n_layer; ++il) {
736
+ struct ggml_tensor * inpSA = inpL;
737
+
738
+ struct ggml_tensor * cur;
739
+
740
+ // lctx.use_buf(ctx0, 0);
741
+
742
+ // norm
743
+ {
744
+ // cur shape [n_embd,N*n_batch,1,1]
745
+ cur = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
746
+ assert_shape_2d(cur, n_embd, N*n_batch);
747
+
748
+ // cur = attention_norm*cur
749
+ cur = ggml_mul(ctx0,
750
+ ggml_repeat(ctx0, model->layers[il].attention_norm, cur),
751
+ cur);
752
+ assert_shape_2d(cur, n_embd, N*n_batch);
753
+ }
754
+
755
+ // self-attention
756
+ {
757
+ // compute Q and K and RoPE them
758
+ // wq shape [n_embd, n_embd, 1, 1]
759
+ // wk shape [n_embd, n_embd, 1, 1]
760
+ // Qcur shape [n_embd/n_head, n_head, N, n_batch]
761
+ // Kcur shape [n_embd/n_head, n_head, N, n_batch]
762
+ struct ggml_tensor * Qcur = ggml_rope(ctx0, ggml_reshape_4d(ctx0, ggml_mul_mat(ctx0, model->layers[il].wq, cur), n_embd/n_head, n_head, N, n_batch), KQ_pos, n_rot, 0, 0);
763
+ struct ggml_tensor * Kcur = ggml_rope(ctx0, ggml_reshape_4d(ctx0, ggml_mul_mat(ctx0, model->layers[il].wk, cur), n_embd/n_head, n_head, N, n_batch), KQ_pos, n_rot, 0, 0);
764
+ assert_shape_4d(Qcur, n_embd/n_head, n_head, N, n_batch);
765
+ assert_shape_4d(Kcur, n_embd/n_head, n_head, N, n_batch);
766
+
767
+ // store key and value to memory
768
+ {
769
+ // compute the transposed [N, n_embd] V matrix
770
+ // wv shape [n_embd, n_embd, 1, 1]
771
+ // Vcur shape [N, n_embd, n_batch, 1]
772
+ struct ggml_tensor * Vcur = ggml_cont(ctx0,
773
+ ggml_permute(ctx0,
774
+ ggml_reshape_3d(ctx0,
775
+ ggml_mul_mat(ctx0,
776
+ model->layers[il].wv,
777
+ cur),
778
+ n_embd, N, n_batch),
779
+ 1, 0, 2, 3));
780
+
781
+ assert_shape_3d(Vcur, N, n_embd, n_batch);
782
+
783
+ // kv_self.k shape [n_embd * n_ctx * n_batch * n_layer]
784
+ // kv_self.v shape [n_ctx * n_embd * n_batch * n_layer]
785
+ // k shape [n_embd * N, n_batch] == kv_self.k[:,n_past:n_past+N,:,il]
786
+ // v shape [N, n_embd, n_batch, 1] == kv_self.v[:,n_past:n_past+N,:,il]
787
+
788
+ /* {
789
+ struct ggml_tensor * k = ggml_view_1d(ctx0, kv_self.k, N*n_embd, (ggml_element_size(kv_self.k)*n_embd)*(il*n_ctx + n_past));
790
+ struct ggml_tensor * v = ggml_view_2d(ctx0, kv_self.v, N, n_embd,
791
+ ( n_ctx)*ggml_element_size(kv_self.v),
792
+ (il*n_ctx)*ggml_element_size(kv_self.v)*n_embd + n_past*ggml_element_size(kv_self.v));
793
+
794
+ // important: storing RoPE-ed version of K in the KV cache!
795
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, k));
796
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, v));
797
+ } //*/
798
+
799
+ kc = ggml_set_2d(ctx0, kc,
800
+ ggml_reshape_2d(ctx0, Kcur, n_embd*N, n_batch),
801
+ ggml_element_size(kc)*n_embd*n_ctx,
802
+ (ggml_element_size(kc)*n_embd)*(il*n_batch*n_ctx + n_past));
803
+ vc = ggml_set_2d(ctx0, vc,
804
+ ggml_reshape_2d(ctx0, Vcur, N*n_embd, n_batch),
805
+ ggml_element_size(vc)*n_ctx*n_embd,
806
+ ggml_element_size(vc)*(n_past + il*n_embd*n_batch*n_ctx));
807
+
808
+ assert_shape_1d(kc, n_embd * n_ctx * n_batch * n_layer);
809
+ assert_shape_1d(vc, n_embd * n_ctx * n_batch * n_layer);
810
+ }
811
+
812
+ // Qcur shape [n_embd/n_head, n_head, N, n_batch]
813
+ // Q shape [n_embd/n_head, N, n_head, n_batch]
814
+ struct ggml_tensor * Q =
815
+ ggml_permute(ctx0,
816
+ Qcur,
817
+ 0, 2, 1, 3);
818
+ assert_shape_4d(Q, n_embd/n_head, N, n_head, n_batch);
819
+
820
+ // kv_self.k shape [n_embd * n_ctx * n_batch * n_layer]
821
+ // K shape [n_embd/n_head, n_past + N, n_head, n_batch]
822
+ struct ggml_tensor * K =
823
+ ggml_permute(ctx0,
824
+ ggml_reshape_4d(ctx0,
825
+ ggml_view_3d(ctx0,
826
+ kc,
827
+ n_embd,
828
+ (n_past + N),
829
+ n_batch,
830
+ n_embd*ggml_element_size(kc),
831
+ n_ctx*n_embd*ggml_element_size(kc),
832
+ il*n_batch*n_ctx*n_embd*ggml_element_size(kc)),
833
+ n_embd/n_head, n_head, n_past + N, n_batch),
834
+ 0, 2, 1, 3);
835
+ assert_shape_4d(K, n_embd/n_head, n_past + N, n_head, n_batch);
836
+
837
+ // K * Q
838
+ // KQ shape [n_past + N, N, n_head, n_batch]
839
+ struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
840
+ assert_shape_4d(KQ, n_past + N, N, n_head, n_batch);
841
+
842
+ // KQ_scaled = KQ / sqrt(n_embd/n_head)
843
+ // KQ_scaled shape [n_past + N, N, n_head, n_batch]
844
+ struct ggml_tensor * KQ_scaled = ggml_scale(ctx0, KQ, 1.0f/sqrtf(float(n_embd)/n_head));
845
+ assert_shape_4d(KQ_scaled, n_past + N, N, n_head, n_batch);
846
+
847
+ // KQ_masked = mask_past(KQ_scaled)
848
+ // KQ_masked shape [n_past + N, N, n_head, n_batch]
849
+ struct ggml_tensor * KQ_masked = ggml_diag_mask_inf(ctx0, KQ_scaled, n_past);
850
+ assert_shape_4d(KQ_masked, n_past + N, N, n_head, n_batch);
851
+
852
+ // KQ = soft_max(KQ_masked)
853
+ // KQ_soft_max shape [n_past + N, N, n_head, n_batch]
854
+ struct ggml_tensor * KQ_soft_max = ggml_soft_max(ctx0, KQ_masked);
855
+ assert_shape_4d(KQ_soft_max, n_past + N, N, n_head, n_batch);
856
+
857
+ // split cached V into n_head heads
858
+ // kv_self.v shape [n_ctx * n_embd * n_batch * n_layer]
859
+ // V shape [n_past + N, n_embd/n_head, n_head, n_batch] == kv_self.v[:(n_past+N),:,:,il]
860
+ struct ggml_tensor * V =
861
+ ggml_view_4d(ctx0, vc,
862
+ n_past + N, n_embd/n_head, n_head, n_batch,
863
+ ggml_element_size(vc)*n_ctx,
864
+ ggml_element_size(vc)*n_ctx*n_embd/n_head,
865
+ ggml_element_size(vc)*n_ctx*n_embd,
866
+ il*n_batch*n_ctx*n_embd*ggml_element_size(vc));
867
+ assert_shape_4d(V, n_past + N, n_embd/n_head, n_head, n_batch);
868
+
869
+ // KQV shape [n_embd/n_head, N, n_head, n_batch]
870
+ struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V, KQ_soft_max);
871
+ assert_shape_4d(KQV, n_embd/n_head, N, n_head, n_batch);
872
+
873
+ // KQV_merged = KQV.permute(0, 2, 1, 3)
874
+ // KQV_merged shape [n_embd/n_head, n_head, N, n_batch]
875
+ struct ggml_tensor * KQV_merged = ggml_permute(ctx0, KQV, 0, 2, 1, 3);
876
+ assert_shape_4d(KQV_merged, n_embd/n_head, n_head, N, n_batch);
877
+ // KQV_merged shape
878
+
879
+ // cur = KQV_merged.contiguous().view(n_embd, N)
880
+ // cur shape [n_embd,N*n_batch,1,1]
881
+ cur = ggml_reshape_2d(ctx0, ggml_cont(ctx0, KQV_merged), n_embd, N*n_batch);
882
+ assert_shape_2d(cur, n_embd, N*n_batch);
883
+ // cur = ggml_cpy(ctx0,
884
+ // KQV_merged,
885
+ // ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, N));
886
+
887
+ // projection (no bias)
888
+ // cur shape [n_embd,N*n_batch,1,1]
889
+ cur = ggml_mul_mat(ctx0,
890
+ model->layers[il].wo,
891
+ cur);
892
+ assert_shape_2d(cur, n_embd, N*n_batch);
893
+ }
894
+
895
+ // lctx.use_buf(ctx0, 1);
896
+
897
+ // inpFF shape [n_embd,N*n_batch,1,1]
898
+ struct ggml_tensor * inpFF = ggml_add(ctx0, cur, inpSA);
899
+ assert_shape_2d(inpFF, n_embd, N*n_batch);
900
+
901
+ // feed-forward network
902
+ {
903
+ // norm
904
+ {
905
+ // cur shape [n_embd,N*n_batch,1,1]
906
+ cur = ggml_rms_norm(ctx0, inpFF, rms_norm_eps);
907
+ assert_shape_2d(cur, n_embd, N*n_batch);
908
+
909
+ // cur = ffn_norm*cur
910
+ // cur shape [n_embd,N*n_batch,1,1]
911
+ cur = ggml_mul(ctx0,
912
+ ggml_repeat(ctx0, model->layers[il].ffn_norm, cur),
913
+ cur);
914
+ assert_shape_2d(cur, n_embd, N*n_batch);
915
+ }
916
+
917
+ // tmp shape [n_ff,N*n_batch,1,1]
918
+ struct ggml_tensor * tmp = ggml_mul_mat(ctx0,
919
+ model->layers[il].w3,
920
+ cur);
921
+ assert_shape_2d(tmp, n_ff, N*n_batch);
922
+
923
+ // cur shape [n_ff,N*n_batch,1,1]
924
+ cur = ggml_mul_mat(ctx0,
925
+ model->layers[il].w1,
926
+ cur);
927
+ assert_shape_2d(cur, n_ff, N*n_batch);
928
+
929
+ // SILU activation
930
+ // cur shape [n_ff,N*n_batch,1,1]
931
+ cur = ggml_silu(ctx0, cur);
932
+ assert_shape_2d(cur, n_ff, N*n_batch);
933
+
934
+ // cur shape [n_ff,N*n_batch,1,1]
935
+ cur = ggml_mul(ctx0, cur, tmp);
936
+ assert_shape_2d(cur, n_ff, N*n_batch);
937
+
938
+ // cur shape [n_embd,N*n_batch,1,1]
939
+ cur = ggml_mul_mat(ctx0,
940
+ model->layers[il].w2,
941
+ cur);
942
+ assert_shape_2d(cur, n_embd, N*n_batch);
943
+ }
944
+
945
+ // cur shape [n_embd,N*n_batch,1,1]
946
+ cur = ggml_add(ctx0, cur, inpFF);
947
+ assert_shape_2d(cur, n_embd, N*n_batch);
948
+
949
+ // input for next layer
950
+ // inpL shape [n_embd,N*n_batch,1,1]
951
+ inpL = cur;
952
+ assert_shape_2d(inpL, n_embd, N*n_batch);
953
+ }
954
+
955
+ // norm
956
+ {
957
+
958
+ // inpL shape [n_embd,N*n_batch,1,1]
959
+ inpL = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
960
+ assert_shape_2d(inpL, n_embd, N*n_batch);
961
+
962
+ // inpL = norm*inpL
963
+ // inpL shape [n_embd,N*n_batch,1,1]
964
+ inpL = ggml_mul(ctx0,
965
+ ggml_repeat(ctx0, model->norm, inpL),
966
+ inpL);
967
+
968
+ assert_shape_2d(inpL, n_embd, N*n_batch);
969
+
970
+ //embeddings = inpL;
971
+ }
972
+
973
+ // lm_head
974
+ // inpL shape [n_vocab,N*n_batch,1,1]
975
+ inpL = ggml_mul_mat(ctx0, model->output, inpL);
976
+ assert_shape_2d(inpL, n_vocab, N*n_batch);
977
+
978
+ {
979
+ // inpL shape [n_vocab,N,n_batch,1]
980
+ inpL = ggml_reshape_3d(ctx0,
981
+ inpL,
982
+ n_vocab, N, n_batch);
983
+ assert_shape_3d(inpL, n_vocab, N, n_batch);
984
+ }
985
+
986
+ // run the computation
987
+ ggml_build_forward_expand(gf, inpL);
988
+
989
+ return inpL;
990
+ }
991
+
992
+ static struct ggml_tensor * forward_lora(
993
+ struct llama_model_lora * model,
994
+ struct llama_kv_cache * cache,
995
+ struct ggml_context * ctx0,
996
+ struct ggml_cgraph * gf,
997
+ struct ggml_tensor * tokens_input,
998
+ const int n_tokens,
999
+ const int n_past
1000
+ ) {
1001
+ const int N = n_tokens;
1002
+
1003
+ struct llama_kv_cache& kv_self = *cache;
1004
+ const auto & hparams = model->hparams;
1005
+
1006
+ const int n_ctx = hparams.n_ctx;
1007
+ const int n_embd = hparams.n_embd;
1008
+ const int n_layer = hparams.n_layer;
1009
+ const int n_head = hparams.n_head;
1010
+ const int n_rot = hparams.n_rot;
1011
+
1012
+ struct ggml_tensor * tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
1013
+ memcpy(tokens->data, tokens_input->data, N*ggml_element_size(tokens));
1014
+
1015
+ struct ggml_tensor * kc = kv_self.k;
1016
+ struct ggml_tensor * vc = kv_self.v;
1017
+
1018
+ struct ggml_tensor * KQ_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
1019
+ {
1020
+ int * data = (int *) KQ_pos->data;
1021
+ for (int i = 0; i < N; ++i) {
1022
+ data[i] = n_past + i;
1023
+ }
1024
+ }
1025
+
1026
+ // inpL shape [n_embd,N,1,1]
1027
+ struct ggml_tensor * inpL = ggml_get_rows(ctx0, model->tok_embeddings, tokens);
1028
+ for (int il = 0; il < n_layer; ++il) {
1029
+ struct ggml_tensor * inpSA = inpL;
1030
+
1031
+ struct ggml_tensor * cur;
1032
+
1033
+ // norm
1034
+ {
1035
+ // cur shape [n_embd,N,1,1]
1036
+ cur = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
1037
+
1038
+ // cur = attention_norm*cur
1039
+ cur = ggml_mul(ctx0,
1040
+ ggml_repeat(ctx0, model->layers[il].attention_norm, cur),
1041
+ cur);
1042
+ }
1043
+
1044
+ // self-attention
1045
+ {
1046
+ // compute Q and K and RoPE them
1047
+ // wq shape [n_embd, n_embd, 1, 1]
1048
+ // wk shape [n_embd, n_embd, 1, 1]
1049
+ // Qcur shape [n_embd/n_head, n_head, N, 1]
1050
+ // Kcur shape [n_embd/n_head, n_head, N, 1]
1051
+ struct ggml_tensor * Qcur = ggml_rope(ctx0,
1052
+ ggml_reshape_3d(ctx0,
1053
+ ggml_mul_mat(ctx0,
1054
+ model->layers[il].wqa,
1055
+ ggml_mul_mat(ctx0,
1056
+ model->layers[il].wqb,
1057
+ cur)),
1058
+ n_embd/n_head, n_head, N),
1059
+ KQ_pos, n_rot, 0, 0);
1060
+ struct ggml_tensor * Kcur = ggml_rope(ctx0,
1061
+ ggml_reshape_3d(ctx0,
1062
+ ggml_mul_mat(ctx0,
1063
+ model->layers[il].wka,
1064
+ ggml_mul_mat(ctx0,
1065
+ model->layers[il].wkb,
1066
+ cur)),
1067
+ n_embd/n_head, n_head, N),
1068
+ KQ_pos, n_rot, 0, 0);
1069
+
1070
+ // store key and value to memory
1071
+ {
1072
+ // compute the transposed [N, n_embd] V matrix
1073
+ // wv shape [n_embd, n_embd, 1, 1]
1074
+ // Vcur shape [n_embd, N, 1, 1]
1075
+ struct ggml_tensor * Vcur = ggml_cont(ctx0,
1076
+ ggml_transpose(ctx0,
1077
+ ggml_reshape_2d(ctx0,
1078
+ ggml_mul_mat(ctx0,
1079
+ model->layers[il].wva,
1080
+ ggml_mul_mat(ctx0,
1081
+ model->layers[il].wvb,
1082
+ cur)),
1083
+ n_embd, N)));
1084
+
1085
+ // kv_self.k shape [n_embd * n_ctx * n_layer, 1]
1086
+ // kv_self.v shape [n_embd * n_ctx * n_layer, 1]
1087
+ // k shape [n_embd * N, 1] == kv_self.k[:,n_past:n_past+N,il,0]
1088
+ // v shape [N, n_embd, 1, 1] == kv_self.v[:,n_past:n_past+N,il,0]
1089
+
1090
+ /* {
1091
+ struct ggml_tensor * k = ggml_view_1d(ctx0, kv_self.k, N*n_embd, (ggml_element_size(kv_self.k)*n_embd)*(il*n_ctx + n_past));
1092
+ struct ggml_tensor * v = ggml_view_2d(ctx0, kv_self.v, N, n_embd,
1093
+ ( n_ctx)*ggml_element_size(kv_self.v),
1094
+ (il*n_ctx)*ggml_element_size(kv_self.v)*n_embd + n_past*ggml_element_size(kv_self.v));
1095
+
1096
+ // important: storing RoPE-ed version of K in the KV cache!
1097
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Kcur, k));
1098
+ ggml_build_forward_expand(gf, ggml_cpy(ctx0, Vcur, v));
1099
+ } //*/
1100
+
1101
+ kc = ggml_set_1d(ctx0, kc, ggml_reshape_1d(ctx0, Kcur, n_embd*N), (ggml_element_size(kv_self.k)*n_embd)*(il*n_ctx + n_past));
1102
+ vc = ggml_set_2d(ctx0, vc, Vcur, ( n_ctx)*ggml_element_size(kv_self.v),
1103
+ (il*n_ctx)*ggml_element_size(kv_self.v)*n_embd + n_past*ggml_element_size(kv_self.v));
1104
+ }
1105
+
1106
+ // Qcur shape [n_embd/n_head, n_head, N, 1]
1107
+ // Q shape [n_embd/n_head, N, n_head, 1]
1108
+ struct ggml_tensor * Q =
1109
+ ggml_permute(ctx0,
1110
+ Qcur,
1111
+ 0, 2, 1, 3);
1112
+
1113
+ // kv_self.k shape [n_embd * n_ctx * n_layer, 1]
1114
+ // K shape [n_embd/n_head, n_past + N, n_head, 1]
1115
+ struct ggml_tensor * K =
1116
+ ggml_permute(ctx0,
1117
+ ggml_reshape_3d(ctx0,
1118
+ ggml_view_1d(ctx0, kc, (n_past + N)*n_embd, il*n_ctx*ggml_element_size(kc)*n_embd),
1119
+ n_embd/n_head, n_head, n_past + N),
1120
+ 0, 2, 1, 3);
1121
+
1122
+ // K * Q
1123
+ // KQ shape [n_past + N, N, n_head, 1]
1124
+ struct ggml_tensor * KQ = ggml_mul_mat(ctx0, K, Q);
1125
+
1126
+ // KQ_scaled = KQ / sqrt(n_embd/n_head)
1127
+ // KQ_scaled shape [n_past + N, N, n_head, 1]
1128
+ struct ggml_tensor * KQ_scaled = ggml_scale(ctx0, KQ, 1.0f/sqrtf(float(n_embd)/n_head));
1129
+
1130
+ // KQ_masked = mask_past(KQ_scaled)
1131
+ // KQ_masked shape [n_past + N, N, n_head, 1]
1132
+ struct ggml_tensor * KQ_masked = ggml_diag_mask_inf(ctx0, KQ_scaled, n_past);
1133
+
1134
+ // KQ = soft_max(KQ_masked)
1135
+ // KQ_soft_max shape [n_past + N, N, n_head, 1]
1136
+ struct ggml_tensor * KQ_soft_max = ggml_soft_max(ctx0, KQ_masked);
1137
+
1138
+ // split cached V into n_head heads
1139
+ //// V shape [n_past + N, n_embd/n_head, n_head, 1]
1140
+ // V shape [n_past + N, n_embd/n_head, n_head, 1] == kv_self.v[:,:(n_past+N),il,1]
1141
+ struct ggml_tensor * V =
1142
+ ggml_view_3d(ctx0, vc,
1143
+ n_past + N, n_embd/n_head, n_head,
1144
+ n_ctx*ggml_element_size(vc),
1145
+ n_ctx*ggml_element_size(vc)*n_embd/n_head,
1146
+ il*n_ctx*ggml_element_size(vc)*n_embd);
1147
+
1148
+ // KQV shape [n_embd/n_head, N, n_head, 1]
1149
+ struct ggml_tensor * KQV = ggml_mul_mat(ctx0, V, KQ_soft_max);
1150
+
1151
+ // KQV_merged = KQV.permute(0, 2, 1, 3)
1152
+ // KQV_merged shape [n_embd/n_head, n_head, N, 1]
1153
+ struct ggml_tensor * KQV_merged = ggml_permute(ctx0, KQV, 0, 2, 1, 3);
1154
+ // KQV_merged shape
1155
+
1156
+ // cur = KQV_merged.contiguous().view(n_embd, N)
1157
+ // cur shape [n_embd,N,1,1]
1158
+ cur = ggml_reshape_2d(ctx0, ggml_cont(ctx0, KQV_merged), n_embd, N);
1159
+ // cur = ggml_cpy(ctx0,
1160
+ // KQV_merged,
1161
+ // ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, N));
1162
+
1163
+ // projection (no bias)
1164
+ // cur shape [n_embd,N,1,1]
1165
+ cur = ggml_mul_mat(ctx0,
1166
+ model->layers[il].woa,
1167
+ ggml_mul_mat(ctx0,
1168
+ model->layers[il].wob,
1169
+ cur));
1170
+ }
1171
+
1172
+ // inpFF shape [n_embd,N,1,1]
1173
+ struct ggml_tensor * inpFF = ggml_add(ctx0, cur, inpSA);
1174
+
1175
+ // feed-forward network
1176
+ {
1177
+ // norm
1178
+ {
1179
+ // cur shape [n_embd,N,1,1]
1180
+ cur = ggml_rms_norm(ctx0, inpFF, rms_norm_eps);
1181
+
1182
+ // cur = ffn_norm*cur
1183
+ // cur shape [n_embd,N,1,1]
1184
+ cur = ggml_mul(ctx0,
1185
+ ggml_repeat(ctx0, model->layers[il].ffn_norm, cur),
1186
+ cur);
1187
+ }
1188
+
1189
+ // tmp shape [n_ff,N,1,1]
1190
+ struct ggml_tensor * tmp = ggml_mul_mat(ctx0,
1191
+ model->layers[il].w3,
1192
+ cur);
1193
+
1194
+ // cur shape [n_ff,N,1,1]
1195
+ cur = ggml_mul_mat(ctx0,
1196
+ model->layers[il].w1,
1197
+ cur);
1198
+
1199
+ // SILU activation
1200
+ // cur shape [n_ff,N,1,1]
1201
+ cur = ggml_silu(ctx0, cur);
1202
+
1203
+ // cur shape [n_ff,N,1,1]
1204
+ cur = ggml_mul(ctx0, cur, tmp);
1205
+
1206
+ // cur shape [n_embd,N,1,1]
1207
+ cur = ggml_mul_mat(ctx0,
1208
+ model->layers[il].w2,
1209
+ cur);
1210
+ }
1211
+
1212
+ // cur shape [n_embd,N,1,1]
1213
+ cur = ggml_add(ctx0, cur, inpFF);
1214
+
1215
+ // input for next layer
1216
+ // inpL shape [n_embd,N,1,1]
1217
+ inpL = cur;
1218
+ }
1219
+
1220
+ // norm
1221
+ {
1222
+
1223
+ // inpL shape [n_embd,N,1,1]
1224
+ inpL = ggml_rms_norm(ctx0, inpL, rms_norm_eps);
1225
+
1226
+ // inpL = norm*inpL
1227
+ // inpL shape [n_embd,N,1,1]
1228
+ inpL = ggml_mul(ctx0,
1229
+ ggml_repeat(ctx0, model->norm, inpL),
1230
+ inpL);
1231
+
1232
+ //embeddings = inpL;
1233
+ }
1234
+
1235
+
1236
+ // lm_head
1237
+ // inpL shape [n_vocab,N,1,1]
1238
+ inpL = ggml_mul_mat(ctx0,
1239
+ model->outputa,
1240
+ ggml_mul_mat(ctx0,
1241
+ model->outputb,
1242
+ inpL));
1243
+
1244
+ // ggml_set_scratch(ctx0, { 0, 0, nullptr, });
1245
+ // run the computation
1246
+ ggml_build_forward_expand(gf, inpL);
1247
+
1248
+ return inpL;
1249
+ }
1250
+
1251
+ static void sample_softmax(struct ggml_tensor * logits, struct ggml_tensor * probs, struct ggml_tensor * best_samples) {
1252
+ assert(ggml_is_matrix(logits));
1253
+ assert(ggml_is_matrix(probs));
1254
+ assert(ggml_is_vector(best_samples));
1255
+ assert(logits->ne[1] == best_samples->ne[0]);
1256
+ assert(logits->ne[0] == probs->ne[0]);
1257
+ assert(logits->ne[1] == probs->ne[1]);
1258
+ for (int i = 0; i < logits->ne[1]; ++i) {
1259
+ float max_logit = ggml_get_f32_1d(logits, i * logits->ne[0]);
1260
+ ggml_set_i32_1d(best_samples, i, 0);
1261
+ for (int k = 0; k < logits->ne[0]; ++k) {
1262
+ float logit = ggml_get_f32_1d(logits, i * logits->ne[0] + k);
1263
+ if (logit > max_logit) {
1264
+ max_logit = logit;
1265
+ ggml_set_i32_1d(best_samples, i, k);
1266
+ }
1267
+ }
1268
+ float psum = 0;
1269
+ for (int k = 0; k < logits->ne[0]; ++k) {
1270
+ float logit = ggml_get_f32_1d(logits, i * logits->ne[0] + k);
1271
+ float p = (logit == -INFINITY) ? 0 : expf(logit - max_logit);
1272
+ psum += p;
1273
+ ggml_set_f32_1d(probs, i * probs->ne[0] + k, p);
1274
+ }
1275
+ for (int k = 0; k < logits->ne[0]; ++k) {
1276
+ float p = ggml_get_f32_1d(probs, i*probs->ne[0] + k);
1277
+ ggml_set_f32_1d(probs, i * probs->ne[0] + k, p / psum);
1278
+ }
1279
+ }
1280
+ }
1281
+
1282
+ static void sample_softmax_batch(
1283
+ struct ggml_context * ctx, struct ggml_tensor * logits, struct ggml_tensor * probs,
1284
+ struct ggml_tensor * best_samples
1285
+ ) {
1286
+ GGML_ASSERT(ggml_is_matrix(best_samples));
1287
+ GGML_ASSERT(ggml_is_3d(logits));
1288
+ GGML_ASSERT(ggml_is_3d(probs));
1289
+ int n_tokens = best_samples->ne[0];
1290
+ int n_batch = best_samples->ne[1];
1291
+ int n_vocab = logits->ne[0];
1292
+ GGML_ASSERT(n_tokens == logits->ne[1]);
1293
+ GGML_ASSERT(n_batch == logits->ne[2]);
1294
+ GGML_ASSERT(n_vocab == probs->ne[0]);
1295
+ GGML_ASSERT(n_tokens == probs->ne[1]);
1296
+ GGML_ASSERT(n_batch == probs->ne[2]);
1297
+
1298
+ for (int k = 0; k < n_batch; ++k) {
1299
+ struct ggml_tensor * best_samples_k = ggml_view_1d(ctx,
1300
+ best_samples,
1301
+ best_samples->ne[0],
1302
+ k*best_samples->nb[1]);
1303
+ struct ggml_tensor * logits_k = ggml_view_2d(ctx,
1304
+ logits,
1305
+ logits->ne[0],
1306
+ logits->ne[1],
1307
+ logits->nb[1],
1308
+ k*logits->nb[2]);
1309
+ struct ggml_tensor * probs_k = ggml_view_2d(ctx,
1310
+ probs,
1311
+ probs->ne[0],
1312
+ probs->ne[1],
1313
+ probs->nb[1],
1314
+ k*probs->nb[2]);
1315
+ sample_softmax(logits_k, probs_k, best_samples_k);
1316
+ }
1317
+ }
1318
+
1319
+ static void print_row(struct ggml_tensor * probs, int i) {
1320
+ for (int k = 0; k < probs->ne[0]; ++k) {
1321
+ float p = ggml_get_f32_1d(probs, i*probs->ne[0] + k);
1322
+ printf(" %.2f", p);
1323
+ }
1324
+ printf("\n");
1325
+ }
1326
+
1327
+ static void print_matrix(struct ggml_tensor * probs) {
1328
+ assert(ggml_is_matrix(probs));
1329
+ for (int i = 0; i < probs->ne[1]; ++i) {
1330
+ for (int k = 0; k < probs->ne[0]; ++k) {
1331
+ float p = ggml_get_f32_1d(probs, i*probs->ne[0] + k);
1332
+ printf(" %.2f", p);
1333
+ }
1334
+ printf("\n");
1335
+ }
1336
+ }
1337
+
1338
+ static void print_token(int token, int n_vocab) {
1339
+ for (int k = 0; k < token; ++k) {
1340
+ printf(" ");
1341
+ }
1342
+ printf("X");
1343
+ for (int k = token+1; k < n_vocab; ++k) {
1344
+ printf(" ");
1345
+ }
1346
+ printf("\n");
1347
+ }
1348
+
1349
+ static void print_tokens(struct ggml_tensor * tokens, int n_vocab) {
1350
+ for (int i=0; i<tokens->ne[0]; ++i) {
1351
+ int token = ggml_get_i32_1d(tokens, i);
1352
+ print_token(token, n_vocab);
1353
+ }
1354
+ }
1355
+
1356
+ static void get_example_targets(int example_id, struct ggml_tensor * tokens_input, struct ggml_tensor * targets) {
1357
+ int n_tokens = tokens_input->ne[0];
1358
+ int n_vocab = targets->ne[0];
1359
+ float randomness = 0.0f;
1360
+ // ggml_set_zero(targets);
1361
+ ggml_set_f32(targets, -1.0f);
1362
+ ggml_set_i32_1d(tokens_input, 0, 0);
1363
+ for (int i=1; i<n_tokens+1; ++i) {
1364
+ float x = example_id + i * 3.14159f * 2.0f * 1.0f * 0.5f / n_tokens;
1365
+ float y = sinf(x);//*cosf(x*1.1f+1.0f);
1366
+ float z = (y+1.0f)*0.5f; // scale to [0..1]
1367
+ z += (frand()-0.5f)*(randomness/n_vocab);
1368
+ z = (z < 0.0f) ? 0.0f : (z > 1.0f) ? 1.0f : z; // clamp to [0..1]
1369
+ int token = std::max(1,std::min(1+(int)(z*(float)(n_vocab-1)), n_vocab-1));
1370
+ ggml_set_f32_1d(targets, (i-1)*n_vocab + token, +1.0f);
1371
+ if (i<n_tokens) {
1372
+ ggml_set_i32_1d(tokens_input, i, token);
1373
+ }
1374
+ }
1375
+ }
1376
+
1377
+ static void get_example_targets_batch(
1378
+ struct ggml_context * ctx, int example_id, struct ggml_tensor * tokens_input, struct ggml_tensor * targets
1379
+ ) {
1380
+ GGML_ASSERT(ggml_is_matrix(tokens_input));
1381
+ GGML_ASSERT(ggml_is_3d(targets));
1382
+ int n_tokens = tokens_input->ne[0];
1383
+ int n_batch = tokens_input->ne[1];
1384
+ GGML_ASSERT(n_tokens == targets->ne[1]);
1385
+ GGML_ASSERT(n_batch == targets->ne[2]);
1386
+
1387
+ for (int k=0; k<n_batch; ++k) {
1388
+ struct ggml_tensor * tokens_input_k = ggml_view_1d(ctx,
1389
+ tokens_input,
1390
+ tokens_input->ne[0],
1391
+ k*tokens_input->nb[1]);
1392
+ struct ggml_tensor * targets_k = ggml_view_2d(ctx,
1393
+ targets,
1394
+ targets->ne[0],
1395
+ targets->ne[1],
1396
+ targets->nb[1],
1397
+ k*targets->nb[2]);
1398
+ get_example_targets(example_id*n_batch + k, tokens_input_k, targets_k);
1399
+ }
1400
+ }
1401
+
1402
+ static void lshift_examples(struct ggml_tensor * tokens_input, struct ggml_tensor * targets, int n_shift) {
1403
+ int n_tokens = tokens_input->ne[0];
1404
+ int n_vocab = targets->ne[0];
1405
+ for (int i=0; i<n_tokens-n_shift; ++i) {
1406
+ ggml_set_i32_1d(tokens_input, i, ggml_get_i32_1d(tokens_input, i + n_shift));
1407
+ for (int k=0; k<n_vocab; ++k) {
1408
+ ggml_set_f32_1d(targets, i*n_vocab + k, ggml_get_f32_1d(targets, (i + n_shift)*n_vocab + k));
1409
+ }
1410
+ }
1411
+ }
1412
+
1413
+ static struct ggml_tensor * square_error_loss(
1414
+ struct ggml_context * ctx, struct ggml_tensor * a, struct ggml_tensor * b
1415
+ ) {
1416
+ // todo: instead of a-b: a[1:]-b[:-1]
1417
+ return ggml_sum(ctx, ggml_sqr(ctx, ggml_sub(ctx, a, b)));
1418
+ }
1419
+
1420
+ static struct ggml_tensor * cross_entropy_loss(
1421
+ struct ggml_context * ctx, struct ggml_tensor * a, struct ggml_tensor * b
1422
+ ) {
1423
+ const float eps = 1e-3f;
1424
+ return
1425
+ ggml_sum(ctx,
1426
+ ggml_neg(ctx,
1427
+ ggml_sum_rows(ctx,
1428
+ ggml_mul(ctx,
1429
+ ggml_soft_max(ctx, a),
1430
+ ggml_log(ctx,
1431
+ ggml_add1(ctx,
1432
+ ggml_soft_max(ctx, b),
1433
+ ggml_new_f32(ctx, eps)))))));
1434
+ }
1435
+
1436
+ int main(int argc, char ** argv) {
1437
+ if (argc < 1) {
1438
+ fprintf(stderr, "usage: %s\n", argv[0]);
1439
+
1440
+ return 1;
1441
+ }
1442
+
1443
+ struct ggml_init_params lcparams;
1444
+ lcparams.mem_size = 1024ll*1024ll*1024ll;
1445
+ lcparams.mem_buffer = NULL;
1446
+ lcparams.no_alloc = false;
1447
+
1448
+ struct llama_model model;
1449
+ model.hparams.n_vocab = 8;
1450
+ model.hparams.n_ctx = 8;
1451
+ model.hparams.n_embd = 32;
1452
+ model.hparams.n_mult = 2;
1453
+ model.hparams.n_head = 8;
1454
+ model.hparams.n_layer = 1;
1455
+ model.hparams.n_rot = std::min(16u, model.hparams.n_embd / model.hparams.n_head);
1456
+
1457
+ // model.hparams.n_embd = 32;
1458
+ // model.hparams.n_mult = 2;
1459
+ // model.hparams.n_head = 4;
1460
+ // model.hparams.n_layer = 8;
1461
+ // model.hparams.n_rot = 8;
1462
+
1463
+ model.ctx = ggml_init(lcparams);
1464
+ printf("init model\n");
1465
+ init_model(&model);
1466
+ set_param_model(&model);
1467
+
1468
+ randomize_model(&model, 1337, 0.0f, 1.0f, -1.0f, +1.0f);
1469
+
1470
+ /*
1471
+ struct llama_model_lora model_lora;
1472
+ // model.hparams.n_vocab = 6;
1473
+ // model.hparams.n_ctx = 64;
1474
+ // model.hparams.n_embd = 128;
1475
+ // model.hparams.n_mult = 2;
1476
+ // model.hparams.n_head = 8;
1477
+ // model.hparams.n_layer = 6;
1478
+ // model.hparams.n_rot = model.hparams.n_embd / model.hparams.n_head;
1479
+
1480
+ model_lora.hparams.n_vocab = 16;
1481
+ model_lora.hparams.n_ctx = 32;
1482
+ model_lora.hparams.n_embd = 256;
1483
+ model_lora.hparams.n_mult = 2;
1484
+ model_lora.hparams.n_head = 16;
1485
+ model_lora.hparams.n_layer = 1;
1486
+ model_lora.hparams.n_lora = 64;
1487
+ model_lora.hparams.n_rot = MIN(16, model_lora.hparams.n_embd / model_lora.hparams.n_head);
1488
+ // model.hparams.n_rot = (model.hparams.n_embd / model.hparams.n_head) / 2;
1489
+
1490
+ // model.hparams.n_embd = 32;
1491
+ // model.hparams.n_mult = 2;
1492
+ // model.hparams.n_head = 4;
1493
+ // model.hparams.n_layer = 8;
1494
+ // model.hparams.n_rot = 8;
1495
+
1496
+ model_lora.ctx = ggml_init(lcparams);
1497
+ printf("init model_lora\n");
1498
+ init_model_lora(&model_lora);
1499
+ set_param_model_lora(&model_lora);
1500
+
1501
+ randomize_model_lora(&model_lora, 1337, 0.0f, 1.0f, -1.0f, +1.0f);
1502
+ */
1503
+ int n_batch = 8;
1504
+ // key + value cache for the self attention
1505
+ struct llama_kv_cache kv_self;
1506
+ printf("init_kv_cache\n");
1507
+ kv_self.ctx = model.ctx;
1508
+ init_kv_cache(&kv_self, &model, n_batch);
1509
+ //init_kv_cache_lora(&kv_self, &model_lora);
1510
+
1511
+ size_t compute_size = 1024ll*1024ll*1024ll;
1512
+ uint8_t * compute_addr = new uint8_t[compute_size];
1513
+
1514
+ int n_examples = 256;
1515
+ int n_tokens = model.hparams.n_ctx;
1516
+ int n_vocab = model.hparams.n_vocab;
1517
+
1518
+ std::vector<uint8_t> work_buffer;
1519
+
1520
+ for (int ex=0; ex<n_examples; ++ex) {
1521
+ struct ggml_init_params params = {
1522
+ /*.mem_size =*/ compute_size,
1523
+ /*.mem_buffer =*/ compute_addr,
1524
+ /*.no_alloc =*/ false,
1525
+ };
1526
+
1527
+ struct ggml_context * ctx0 = ggml_init(params);
1528
+
1529
+ struct ggml_tensor * after_opt_best_samples = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, n_tokens, n_batch);
1530
+ struct ggml_tensor * after_opt_probs = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens, n_batch);
1531
+ struct ggml_tensor * tokens_input = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, n_tokens, n_batch);
1532
+ struct ggml_tensor * targets = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens, n_batch);
1533
+
1534
+ int n_past = 0;
1535
+
1536
+ struct ggml_cgraph * gf = NULL;
1537
+ gf = ggml_new_graph_custom(ctx0, LLAMA_TRAIN_MAX_NODES, true);
1538
+
1539
+ get_example_targets_batch(ctx0, 64*ex+0, tokens_input, targets);
1540
+
1541
+ struct ggml_tensor * logits = forward_batch(&model, &kv_self, ctx0, gf, tokens_input, n_tokens, n_past, n_batch);
1542
+ // struct ggml_tensor * e = cross_entropy_loss(ctx0, targets, logits);
1543
+ struct ggml_tensor * e = square_error_loss(ctx0, targets, logits);
1544
+
1545
+ ggml_build_forward_expand(gf, e);
1546
+ ggml_graph_compute_helper(work_buffer, gf, /*n_threads*/ 1);
1547
+
1548
+ float error_before_opt = ggml_get_f32_1d(e, 0);
1549
+
1550
+ struct ggml_opt_params opt_params_lbfgs = ggml_opt_default_params(GGML_OPT_TYPE_LBFGS);
1551
+ opt_params_lbfgs.print_forward_graph = false;
1552
+ opt_params_lbfgs.print_backward_graph = false;
1553
+ opt_params_lbfgs.lbfgs.n_iter = 16;
1554
+ ggml_opt(ctx0, opt_params_lbfgs, e);
1555
+ //
1556
+ ggml_build_forward_expand(gf, e);
1557
+ ggml_graph_compute_helper(work_buffer, gf, /*n_threads*/ 1);
1558
+
1559
+ float error_after_opt = ggml_get_f32_1d(e, 0);
1560
+
1561
+ if (ex % 8 == 0) {
1562
+ printf("Example %d\n", (ex+1));
1563
+ printf("error_before_opt: %.2f\n", error_before_opt);
1564
+ printf("error_after_opt: %.2f\n", error_after_opt);
1565
+ }
1566
+
1567
+ if (ex % 64 == 0) {
1568
+ sample_softmax_batch(ctx0, logits, after_opt_probs, after_opt_best_samples);
1569
+ // printf("probabilities after optimization:\n");
1570
+ // print_matrix(after_opt_probs);
1571
+ printf("best samples after optimization:\n");
1572
+ print_tokens(after_opt_best_samples, n_vocab);
1573
+ }
1574
+
1575
+ ggml_free(ctx0);
1576
+ }
1577
+
1578
+ {
1579
+ int n_gen = 128;
1580
+ int sample_ctx = n_tokens-n_tokens/8;
1581
+
1582
+ printf("Generating %d tokens.\n", n_gen);
1583
+
1584
+ struct ggml_tensor * tokens_input = ggml_new_tensor_1d(model.ctx, GGML_TYPE_I32, n_tokens);
1585
+ struct ggml_tensor * targets = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, n_vocab, n_tokens);
1586
+
1587
+ get_example_targets(137, tokens_input, targets);
1588
+ for (int i=sample_ctx; i<n_tokens; ++i) {
1589
+ ggml_set_i32_1d(tokens_input, i, n_vocab/2);
1590
+ }
1591
+
1592
+ for (int i=0; i<sample_ctx-1; ++i) {
1593
+ print_token(ggml_get_i32_1d(tokens_input, i), n_vocab);
1594
+ }
1595
+ printf("---\n");
1596
+ for (int i=0; i<n_gen; ++i) {
1597
+ struct ggml_init_params params = {
1598
+ /*.mem_size =*/ compute_size,
1599
+ /*.mem_buffer =*/ compute_addr,
1600
+ /*.no_alloc =*/ false,
1601
+ };
1602
+ struct ggml_context * ctx0 = ggml_init(params);
1603
+
1604
+ struct ggml_cgraph * gf = NULL;
1605
+ gf = ggml_new_graph_custom(ctx0, LLAMA_TRAIN_MAX_NODES, true);
1606
+
1607
+ int n_past = 0;
1608
+ struct ggml_tensor * logits = forward(&model, &kv_self, ctx0, gf, tokens_input, sample_ctx, n_past);
1609
+
1610
+ ggml_build_forward_expand(gf, logits);
1611
+ ggml_graph_compute_helper(work_buffer, gf, /*n_threads*/ 1);
1612
+
1613
+ struct ggml_tensor * best_samples = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, sample_ctx);
1614
+ struct ggml_tensor * probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, sample_ctx);
1615
+
1616
+ sample_softmax(logits, probs, best_samples);
1617
+
1618
+ // int sample_at = n_tokens-1;
1619
+ int token = ggml_get_i32_1d(best_samples, sample_ctx-1);
1620
+
1621
+ // print_row(probs, sample_at);
1622
+ print_token(token, n_vocab);
1623
+
1624
+ lshift_examples(tokens_input, targets, 1);
1625
+ ggml_set_i32_1d(tokens_input, 0, 0);
1626
+ ggml_set_i32_1d(tokens_input, sample_ctx-1, token);
1627
+
1628
+ ggml_free(ctx0);
1629
+ }
1630
+ }
1631
+
1632
+ print_matrix(model.tok_embeddings);
1633
+ printf("done\n");
1634
+
1635
+ // ggml_free(kv_self.ctx);
1636
+ // ggml_free(model_lora.ctx);
1637
+ ggml_free(model.ctx);
1638
+
1639
+ return 0;
1640
+ }