llama-cpp-capacitor 0.0.5 → 0.0.7

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 (149) hide show
  1. package/cpp/LICENSE +21 -0
  2. package/cpp/README.md +4 -0
  3. package/cpp/anyascii.c +22223 -0
  4. package/cpp/anyascii.h +42 -0
  5. package/cpp/chat-parser.cpp +393 -0
  6. package/cpp/chat-parser.h +120 -0
  7. package/cpp/chat.cpp +2315 -0
  8. package/cpp/chat.h +221 -0
  9. package/cpp/common.cpp +1619 -0
  10. package/cpp/common.h +744 -0
  11. package/cpp/ggml-alloc.c +1028 -0
  12. package/cpp/ggml-alloc.h +76 -0
  13. package/cpp/ggml-backend-impl.h +255 -0
  14. package/cpp/ggml-backend-reg.cpp +600 -0
  15. package/cpp/ggml-backend.cpp +2118 -0
  16. package/cpp/ggml-backend.h +354 -0
  17. package/cpp/ggml-common.h +1878 -0
  18. package/cpp/ggml-cpp.h +39 -0
  19. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  20. package/cpp/ggml-cpu/amx/amx.h +8 -0
  21. package/cpp/ggml-cpu/amx/common.h +91 -0
  22. package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
  23. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  24. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  25. package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
  26. package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
  27. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  28. package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
  29. package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
  30. package/cpp/ggml-cpu/arch-fallback.h +215 -0
  31. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  32. package/cpp/ggml-cpu/binary-ops.h +16 -0
  33. package/cpp/ggml-cpu/common.h +73 -0
  34. package/cpp/ggml-cpu/ggml-cpu-impl.h +525 -0
  35. package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
  36. package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
  37. package/cpp/ggml-cpu/ops.cpp +10587 -0
  38. package/cpp/ggml-cpu/ops.h +114 -0
  39. package/cpp/ggml-cpu/quants.c +1193 -0
  40. package/cpp/ggml-cpu/quants.h +97 -0
  41. package/cpp/ggml-cpu/repack.cpp +1982 -0
  42. package/cpp/ggml-cpu/repack.h +120 -0
  43. package/cpp/ggml-cpu/simd-mappings.h +1184 -0
  44. package/cpp/ggml-cpu/traits.cpp +36 -0
  45. package/cpp/ggml-cpu/traits.h +38 -0
  46. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  47. package/cpp/ggml-cpu/unary-ops.h +28 -0
  48. package/cpp/ggml-cpu/vec.cpp +348 -0
  49. package/cpp/ggml-cpu/vec.h +1121 -0
  50. package/cpp/ggml-cpu.h +145 -0
  51. package/cpp/ggml-impl.h +622 -0
  52. package/cpp/ggml-metal-impl.h +688 -0
  53. package/cpp/ggml-metal.h +66 -0
  54. package/cpp/ggml-metal.m +6833 -0
  55. package/cpp/ggml-opt.cpp +1093 -0
  56. package/cpp/ggml-opt.h +256 -0
  57. package/cpp/ggml-quants.c +5324 -0
  58. package/cpp/ggml-quants.h +106 -0
  59. package/cpp/ggml-threading.cpp +12 -0
  60. package/cpp/ggml-threading.h +14 -0
  61. package/cpp/ggml.c +7108 -0
  62. package/cpp/ggml.h +2492 -0
  63. package/cpp/gguf.cpp +1358 -0
  64. package/cpp/gguf.h +202 -0
  65. package/cpp/json-partial.cpp +256 -0
  66. package/cpp/json-partial.h +38 -0
  67. package/cpp/json-schema-to-grammar.cpp +985 -0
  68. package/cpp/json-schema-to-grammar.h +21 -0
  69. package/cpp/llama-adapter.cpp +388 -0
  70. package/cpp/llama-adapter.h +76 -0
  71. package/cpp/llama-arch.cpp +2355 -0
  72. package/cpp/llama-arch.h +499 -0
  73. package/cpp/llama-batch.cpp +875 -0
  74. package/cpp/llama-batch.h +160 -0
  75. package/cpp/llama-chat.cpp +783 -0
  76. package/cpp/llama-chat.h +65 -0
  77. package/cpp/llama-context.cpp +2748 -0
  78. package/cpp/llama-context.h +306 -0
  79. package/cpp/llama-cparams.cpp +5 -0
  80. package/cpp/llama-cparams.h +41 -0
  81. package/cpp/llama-cpp.h +30 -0
  82. package/cpp/llama-grammar.cpp +1229 -0
  83. package/cpp/llama-grammar.h +173 -0
  84. package/cpp/llama-graph.cpp +1891 -0
  85. package/cpp/llama-graph.h +810 -0
  86. package/cpp/llama-hparams.cpp +180 -0
  87. package/cpp/llama-hparams.h +233 -0
  88. package/cpp/llama-impl.cpp +167 -0
  89. package/cpp/llama-impl.h +61 -0
  90. package/cpp/llama-io.cpp +15 -0
  91. package/cpp/llama-io.h +35 -0
  92. package/cpp/llama-kv-cache-iswa.cpp +318 -0
  93. package/cpp/llama-kv-cache-iswa.h +135 -0
  94. package/cpp/llama-kv-cache.cpp +2059 -0
  95. package/cpp/llama-kv-cache.h +374 -0
  96. package/cpp/llama-kv-cells.h +491 -0
  97. package/cpp/llama-memory-hybrid.cpp +258 -0
  98. package/cpp/llama-memory-hybrid.h +137 -0
  99. package/cpp/llama-memory-recurrent.cpp +1146 -0
  100. package/cpp/llama-memory-recurrent.h +179 -0
  101. package/cpp/llama-memory.cpp +59 -0
  102. package/cpp/llama-memory.h +119 -0
  103. package/cpp/llama-mmap.cpp +600 -0
  104. package/cpp/llama-mmap.h +68 -0
  105. package/cpp/llama-model-loader.cpp +1164 -0
  106. package/cpp/llama-model-loader.h +170 -0
  107. package/cpp/llama-model-saver.cpp +282 -0
  108. package/cpp/llama-model-saver.h +37 -0
  109. package/cpp/llama-model.cpp +19042 -0
  110. package/cpp/llama-model.h +491 -0
  111. package/cpp/llama-sampling.cpp +2575 -0
  112. package/cpp/llama-sampling.h +32 -0
  113. package/cpp/llama-vocab.cpp +3792 -0
  114. package/cpp/llama-vocab.h +176 -0
  115. package/cpp/llama.cpp +358 -0
  116. package/cpp/llama.h +1373 -0
  117. package/cpp/log.cpp +427 -0
  118. package/cpp/log.h +103 -0
  119. package/cpp/minja/chat-template.hpp +550 -0
  120. package/cpp/minja/minja.hpp +3009 -0
  121. package/cpp/nlohmann/json.hpp +25526 -0
  122. package/cpp/nlohmann/json_fwd.hpp +187 -0
  123. package/cpp/regex-partial.cpp +204 -0
  124. package/cpp/regex-partial.h +56 -0
  125. package/cpp/rn-completion.cpp +681 -0
  126. package/cpp/rn-completion.h +116 -0
  127. package/cpp/rn-llama.cpp +345 -0
  128. package/cpp/rn-llama.h +149 -0
  129. package/cpp/rn-mtmd.hpp +602 -0
  130. package/cpp/rn-tts.cpp +591 -0
  131. package/cpp/rn-tts.h +59 -0
  132. package/cpp/sampling.cpp +579 -0
  133. package/cpp/sampling.h +107 -0
  134. package/cpp/tools/mtmd/clip-impl.h +473 -0
  135. package/cpp/tools/mtmd/clip.cpp +4322 -0
  136. package/cpp/tools/mtmd/clip.h +106 -0
  137. package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
  138. package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
  139. package/cpp/tools/mtmd/mtmd-audio.h +47 -0
  140. package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
  141. package/cpp/tools/mtmd/mtmd-helper.h +91 -0
  142. package/cpp/tools/mtmd/mtmd.cpp +1066 -0
  143. package/cpp/tools/mtmd/mtmd.h +298 -0
  144. package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
  145. package/cpp/unicode-data.cpp +7034 -0
  146. package/cpp/unicode-data.h +20 -0
  147. package/cpp/unicode.cpp +1061 -0
  148. package/cpp/unicode.h +68 -0
  149. package/package.json +2 -1
@@ -0,0 +1,672 @@
1
+ #include "ggml-backend.h"
2
+ #include "ggml-backend-impl.h"
3
+ #include "ggml-cpu.h"
4
+ #include "repack.h"
5
+ #include "traits.h"
6
+ #include "ggml-impl.h"
7
+ #include "amx/amx.h"
8
+
9
+ #include <cctype>
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ #ifdef LM_GGML_USE_CPU_HBM
14
+ # include "hbm.h"
15
+ #endif
16
+
17
+ #ifdef LM_GGML_USE_CPU_KLEIDIAI
18
+ # include "kleidiai/kleidiai.h"
19
+ #endif
20
+
21
+ #if defined(_WIN32)
22
+ # define WIN32_LEAN_AND_MEAN
23
+ # ifndef NOMINMAX
24
+ # define NOMINMAX
25
+ # endif
26
+ # include <windows.h>
27
+ #else
28
+ # include <unistd.h>
29
+ #endif
30
+
31
+ #if defined(__APPLE__)
32
+ # include <sys/sysctl.h>
33
+ # include <sys/types.h>
34
+ #endif
35
+
36
+ // ggml-backend interface
37
+
38
+ std::vector<lm_ggml_backend_buffer_type_t> & lm_ggml_backend_cpu_get_extra_buffer_types() {
39
+ static std::vector<lm_ggml_backend_buffer_type_t> bufts = []() {
40
+ std::vector<lm_ggml_backend_buffer_type_t> bufts;
41
+
42
+ #if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
43
+ if (lm_ggml_backend_amx_buffer_type()) {
44
+ bufts.push_back(lm_ggml_backend_amx_buffer_type());
45
+ }
46
+ #endif
47
+
48
+ #ifdef LM_GGML_USE_CPU_KLEIDIAI
49
+ if (lm_ggml_backend_cpu_kleidiai_buffer_type()) {
50
+ bufts.push_back(lm_ggml_backend_cpu_kleidiai_buffer_type());
51
+ }
52
+ #endif
53
+
54
+ #ifdef LM_GGML_USE_CPU_REPACK
55
+ if (lm_ggml_backend_cpu_repack_buffer_type()) {
56
+ bufts.push_back(lm_ggml_backend_cpu_repack_buffer_type());
57
+ }
58
+ #endif
59
+
60
+ return bufts;
61
+ }();
62
+
63
+ return bufts;
64
+ }
65
+
66
+ static lm_ggml_backend_buffer_type_t * lm_ggml_backend_cpu_device_get_extra_buffers_type(lm_ggml_backend_dev_t device) {
67
+ static std::vector<lm_ggml_backend_buffer_type_t> extra_bufts = [] {
68
+ std::vector<lm_ggml_backend_buffer_type_t> bufts = lm_ggml_backend_cpu_get_extra_buffer_types();
69
+ bufts.push_back(nullptr);
70
+ return bufts;
71
+ }();
72
+
73
+ return extra_bufts.data();
74
+
75
+ LM_GGML_UNUSED(device);
76
+ }
77
+
78
+ static bool lm_ggml_backend_cpu_is_extra_buffer_type(lm_ggml_backend_buffer_type_t buft) {
79
+ for (auto * extra : lm_ggml_backend_cpu_get_extra_buffer_types()) {
80
+ if (extra == buft) {
81
+ return true;
82
+ }
83
+ }
84
+ return false;
85
+ }
86
+
87
+ // CPU backend - backend (stream)
88
+
89
+ struct lm_ggml_backend_cpu_context {
90
+ int n_threads;
91
+ lm_ggml_threadpool_t threadpool;
92
+
93
+ uint8_t * work_data;
94
+ size_t work_size;
95
+
96
+ lm_ggml_abort_callback abort_callback;
97
+ void * abort_callback_data;
98
+ };
99
+
100
+ static const char * lm_ggml_backend_cpu_get_name(lm_ggml_backend_t backend) {
101
+ return "CPU";
102
+
103
+ LM_GGML_UNUSED(backend);
104
+ }
105
+
106
+ static void lm_ggml_backend_cpu_free(lm_ggml_backend_t backend) {
107
+ struct lm_ggml_backend_cpu_context * cpu_ctx = (struct lm_ggml_backend_cpu_context *)backend->context;
108
+ delete[] cpu_ctx->work_data;
109
+ delete cpu_ctx;
110
+ delete backend;
111
+ }
112
+
113
+ struct lm_ggml_backend_plan_cpu {
114
+ struct lm_ggml_cplan cplan;
115
+ struct lm_ggml_cgraph cgraph;
116
+ };
117
+
118
+ static lm_ggml_backend_graph_plan_t lm_ggml_backend_cpu_graph_plan_create(lm_ggml_backend_t backend, const struct lm_ggml_cgraph * cgraph) {
119
+ struct lm_ggml_backend_cpu_context * cpu_ctx = (struct lm_ggml_backend_cpu_context *)backend->context;
120
+
121
+ struct lm_ggml_backend_plan_cpu * cpu_plan = new lm_ggml_backend_plan_cpu;
122
+
123
+ cpu_plan->cplan = lm_ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
124
+ cpu_plan->cgraph = *cgraph; // FIXME: deep copy
125
+
126
+ if (cpu_plan->cplan.work_size > 0) {
127
+ cpu_plan->cplan.work_data = new uint8_t[cpu_plan->cplan.work_size];
128
+ if (cpu_plan->cplan.work_data == NULL) {
129
+ delete cpu_plan;
130
+ return NULL;
131
+ }
132
+ }
133
+
134
+ cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
135
+ cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
136
+
137
+ return cpu_plan;
138
+ }
139
+
140
+ static void lm_ggml_backend_cpu_graph_plan_free(lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan) {
141
+ struct lm_ggml_backend_plan_cpu * cpu_plan = (struct lm_ggml_backend_plan_cpu *)plan;
142
+
143
+ delete[] cpu_plan->cplan.work_data;
144
+ delete cpu_plan;
145
+
146
+ LM_GGML_UNUSED(backend);
147
+ }
148
+
149
+ static enum lm_ggml_status lm_ggml_backend_cpu_graph_plan_compute(lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan) {
150
+ struct lm_ggml_backend_plan_cpu * cpu_plan = (struct lm_ggml_backend_plan_cpu *)plan;
151
+
152
+ return lm_ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
153
+
154
+ LM_GGML_UNUSED(backend);
155
+ }
156
+
157
+ static enum lm_ggml_status lm_ggml_backend_cpu_graph_compute(lm_ggml_backend_t backend, struct lm_ggml_cgraph * cgraph) {
158
+ struct lm_ggml_backend_cpu_context * cpu_ctx = (struct lm_ggml_backend_cpu_context *)backend->context;
159
+
160
+ struct lm_ggml_cplan cplan = lm_ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
161
+
162
+ if (cpu_ctx->work_size < cplan.work_size) {
163
+ delete[] cpu_ctx->work_data;
164
+ cpu_ctx->work_data = new uint8_t[cplan.work_size];
165
+ if (cpu_ctx->work_data == NULL) {
166
+ cpu_ctx->work_size = 0;
167
+ return LM_GGML_STATUS_ALLOC_FAILED;
168
+ }
169
+ cpu_ctx->work_size = cplan.work_size;
170
+ }
171
+ cplan.work_data = (uint8_t *)cpu_ctx->work_data;
172
+
173
+ cplan.abort_callback = cpu_ctx->abort_callback;
174
+ cplan.abort_callback_data = cpu_ctx->abort_callback_data;
175
+
176
+ return lm_ggml_graph_compute(cgraph, &cplan);
177
+ }
178
+
179
+ static const struct lm_ggml_backend_i lm_ggml_backend_cpu_i = {
180
+ /* .get_name = */ lm_ggml_backend_cpu_get_name,
181
+ /* .free = */ lm_ggml_backend_cpu_free,
182
+ /* .set_tensor_async = */ NULL,
183
+ /* .get_tensor_async = */ NULL,
184
+ /* .cpy_tensor_async = */ NULL,
185
+ /* .synchronize = */ NULL,
186
+ /* .graph_plan_create = */ lm_ggml_backend_cpu_graph_plan_create,
187
+ /* .graph_plan_free = */ lm_ggml_backend_cpu_graph_plan_free,
188
+ /* .graph_plan_update = */ NULL,
189
+ /* .graph_plan_compute = */ lm_ggml_backend_cpu_graph_plan_compute,
190
+ /* .graph_compute = */ lm_ggml_backend_cpu_graph_compute,
191
+ /* .event_record = */ NULL,
192
+ /* .event_wait = */ NULL,
193
+ };
194
+
195
+ static lm_ggml_guid_t lm_ggml_backend_cpu_guid(void) {
196
+ static lm_ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
197
+ return &guid;
198
+ }
199
+
200
+ lm_ggml_backend_t lm_ggml_backend_cpu_init(void) {
201
+ // initialize CPU backend now to avoid slowing the first graph computation
202
+ lm_ggml_cpu_init();
203
+
204
+ struct lm_ggml_backend_cpu_context * ctx = new lm_ggml_backend_cpu_context;
205
+ if (ctx == NULL) {
206
+ return NULL;
207
+ }
208
+
209
+ ctx->n_threads = LM_GGML_DEFAULT_N_THREADS;
210
+ ctx->threadpool = NULL;
211
+ ctx->work_data = NULL;
212
+ ctx->work_size = 0;
213
+ ctx->abort_callback = NULL;
214
+ ctx->abort_callback_data = NULL;
215
+
216
+ lm_ggml_backend_t cpu_backend = new lm_ggml_backend {
217
+ /* .guid = */ lm_ggml_backend_cpu_guid(),
218
+ /* .iface = */ lm_ggml_backend_cpu_i,
219
+ /* .device = */ lm_ggml_backend_reg_dev_get(lm_ggml_backend_cpu_reg(), 0),
220
+ /* .context = */ ctx,
221
+ };
222
+
223
+ if (cpu_backend == NULL) {
224
+ delete ctx;
225
+ return NULL;
226
+ }
227
+
228
+ return cpu_backend;
229
+ }
230
+
231
+ bool lm_ggml_backend_is_cpu(lm_ggml_backend_t backend) {
232
+ return backend != NULL && lm_ggml_guid_matches(backend->guid, lm_ggml_backend_cpu_guid());
233
+ }
234
+
235
+ void lm_ggml_backend_cpu_set_n_threads(lm_ggml_backend_t backend_cpu, int n_threads) {
236
+ LM_GGML_ASSERT(lm_ggml_backend_is_cpu(backend_cpu));
237
+
238
+ struct lm_ggml_backend_cpu_context * ctx = (struct lm_ggml_backend_cpu_context *)backend_cpu->context;
239
+ ctx->n_threads = n_threads;
240
+ }
241
+
242
+ void lm_ggml_backend_cpu_set_threadpool(lm_ggml_backend_t backend_cpu, lm_ggml_threadpool_t threadpool) {
243
+ LM_GGML_ASSERT(lm_ggml_backend_is_cpu(backend_cpu));
244
+
245
+ struct lm_ggml_backend_cpu_context * ctx = (struct lm_ggml_backend_cpu_context *)backend_cpu->context;
246
+
247
+ if (ctx->threadpool && ctx->threadpool != threadpool) {
248
+ // already had a different threadpool, pause/suspend it before switching
249
+ lm_ggml_threadpool_pause(ctx->threadpool);
250
+ }
251
+ ctx->threadpool = threadpool;
252
+ }
253
+
254
+ void lm_ggml_backend_cpu_set_abort_callback(lm_ggml_backend_t backend_cpu, lm_ggml_abort_callback abort_callback, void * abort_callback_data) {
255
+ LM_GGML_ASSERT(lm_ggml_backend_is_cpu(backend_cpu));
256
+
257
+ struct lm_ggml_backend_cpu_context * ctx = (struct lm_ggml_backend_cpu_context *)backend_cpu->context;
258
+ ctx->abort_callback = abort_callback;
259
+ ctx->abort_callback_data = abort_callback_data;
260
+ }
261
+
262
+ // CPU backend - device
263
+
264
+ struct lm_ggml_backend_cpu_device_context {
265
+ std::string description = "CPU";
266
+
267
+ lm_ggml_backend_cpu_device_context() {
268
+ #ifdef __APPLE__
269
+ size_t len = 0;
270
+ if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
271
+ description.resize(len);
272
+ sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
273
+ }
274
+ #elif defined(__linux__)
275
+ FILE * f = fopen("/proc/cpuinfo", "r");
276
+ if (f) {
277
+ char buf[1024];
278
+ while (fgets(buf, sizeof(buf), f)) {
279
+ if (strncmp(buf, "model name", 10) == 0) {
280
+ char * p = strchr(buf, ':');
281
+ if (p) {
282
+ p++;
283
+ while (std::isspace(*p)) {
284
+ p++;
285
+ }
286
+ while (std::isspace(p[strlen(p) - 1])) {
287
+ p[strlen(p) - 1] = '\0';
288
+ }
289
+ description = p;
290
+ break;
291
+ }
292
+ }
293
+ }
294
+ fclose(f);
295
+ }
296
+ #elif defined(_WIN32)
297
+ HKEY hKey;
298
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
299
+ TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
300
+ 0,
301
+ KEY_READ,
302
+ &hKey) == ERROR_SUCCESS) {
303
+ DWORD cpu_brand_size = 0;
304
+ if (RegQueryValueExA(hKey,
305
+ "ProcessorNameString",
306
+ NULL,
307
+ NULL,
308
+ NULL,
309
+ &cpu_brand_size) == ERROR_SUCCESS) {
310
+ description.resize(cpu_brand_size);
311
+ if (RegQueryValueExA(hKey,
312
+ "ProcessorNameString",
313
+ NULL,
314
+ NULL,
315
+ (LPBYTE)&description[0], // NOLINT
316
+ &cpu_brand_size) == ERROR_SUCCESS) {
317
+ if (description.find('\0') != std::string::npos) {
318
+ description.resize(description.find('\0'));
319
+ }
320
+ }
321
+ }
322
+ RegCloseKey(hKey);
323
+ }
324
+ #endif
325
+ }
326
+ };
327
+
328
+ static const char * lm_ggml_backend_cpu_device_get_name(lm_ggml_backend_dev_t dev) {
329
+ return "CPU";
330
+
331
+ LM_GGML_UNUSED(dev);
332
+ }
333
+
334
+ static const char * lm_ggml_backend_cpu_device_get_description(lm_ggml_backend_dev_t dev) {
335
+ struct lm_ggml_backend_cpu_device_context * ctx = (struct lm_ggml_backend_cpu_device_context *)dev->context;
336
+
337
+ return ctx->description.c_str();
338
+ }
339
+
340
+ static void lm_ggml_backend_cpu_device_get_memory(lm_ggml_backend_dev_t dev, size_t * free, size_t * total) {
341
+ #ifdef _WIN32
342
+ MEMORYSTATUSEX status;
343
+ status.dwLength = sizeof(status);
344
+ GlobalMemoryStatusEx(&status);
345
+ *total = status.ullTotalPhys;
346
+ *free = status.ullAvailPhys;
347
+ #else
348
+ long pages = sysconf(_SC_PHYS_PAGES);
349
+ long page_size = sysconf(_SC_PAGE_SIZE);
350
+ *total = pages * page_size;
351
+ *free = *total;
352
+ #endif
353
+
354
+ LM_GGML_UNUSED(dev);
355
+ }
356
+
357
+ static enum lm_ggml_backend_dev_type lm_ggml_backend_cpu_device_get_type(lm_ggml_backend_dev_t dev) {
358
+ return LM_GGML_BACKEND_DEVICE_TYPE_CPU;
359
+
360
+ LM_GGML_UNUSED(dev);
361
+ }
362
+
363
+ static void lm_ggml_backend_cpu_device_get_props(lm_ggml_backend_dev_t dev, struct lm_ggml_backend_dev_props * props) {
364
+ props->name = lm_ggml_backend_cpu_device_get_name(dev);
365
+ props->description = lm_ggml_backend_cpu_device_get_description(dev);
366
+ props->type = lm_ggml_backend_cpu_device_get_type(dev);
367
+ lm_ggml_backend_cpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
368
+ props->caps = {
369
+ /* .async = */ false,
370
+ /* .host_buffer = */ false,
371
+ /* .buffer_from_host_ptr = */ true,
372
+ /* .events = */ false,
373
+ };
374
+ }
375
+
376
+ static lm_ggml_backend_t lm_ggml_backend_cpu_device_init_backend(lm_ggml_backend_dev_t dev, const char * params) {
377
+ return lm_ggml_backend_cpu_init();
378
+
379
+ LM_GGML_UNUSED(dev);
380
+ LM_GGML_UNUSED(params);
381
+ }
382
+
383
+ static lm_ggml_backend_buffer_type_t lm_ggml_backend_cpu_device_get_buffer_type(lm_ggml_backend_dev_t dev) {
384
+ return lm_ggml_backend_cpu_buffer_type();
385
+
386
+ LM_GGML_UNUSED(dev);
387
+ }
388
+
389
+ static lm_ggml_backend_buffer_t lm_ggml_backend_cpu_device_buffer_from_host_ptr(lm_ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
390
+ return lm_ggml_backend_cpu_buffer_from_ptr(ptr, size);
391
+
392
+ LM_GGML_UNUSED(dev);
393
+ LM_GGML_UNUSED(max_tensor_size);
394
+ }
395
+
396
+ static bool lm_ggml_backend_cpu_device_supports_op(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op) {
397
+ const struct lm_ggml_tensor * src0 = op->src[0];
398
+ const struct lm_ggml_tensor * src1 = op->src[1];
399
+
400
+ if (op->op == LM_GGML_OP_NONE || op->op == LM_GGML_OP_RESHAPE || op->op == LM_GGML_OP_VIEW || op->op == LM_GGML_OP_PERMUTE || op->op == LM_GGML_OP_TRANSPOSE) {
401
+ return true;
402
+ }
403
+
404
+ // check extra buffer types
405
+ // note: only the first sources are checked for extra buffer types to reduce overhead, increase if necessary
406
+ for (int i = 0; i < 4; i++) {
407
+ if (op->src[i] && op->src[i]->buffer &&
408
+ lm_ggml_backend_cpu_is_extra_buffer_type(op->src[i]->buffer->buft)) {
409
+ auto * buf_extra = (ggml::cpu::extra_buffer_type *) op->src[i]->buffer->buft->context;
410
+ return buf_extra->supports_op(dev, op);
411
+ }
412
+ }
413
+
414
+ switch (op->op) {
415
+ case LM_GGML_OP_CPY:
416
+ case LM_GGML_OP_SET_ROWS:
417
+ return
418
+ op->type != LM_GGML_TYPE_IQ3_XXS &&
419
+ op->type != LM_GGML_TYPE_IQ3_S &&
420
+ op->type != LM_GGML_TYPE_IQ2_XXS &&
421
+ op->type != LM_GGML_TYPE_IQ2_XS &&
422
+ op->type != LM_GGML_TYPE_IQ2_S &&
423
+ op->type != LM_GGML_TYPE_IQ1_S &&
424
+ op->type != LM_GGML_TYPE_IQ1_M; // missing type_traits.from_float
425
+ case LM_GGML_OP_MUL_MAT:
426
+ return src1->type == LM_GGML_TYPE_F32 || src1->type == lm_ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
427
+ case LM_GGML_OP_SOFT_MAX_BACK: {
428
+ if (op->src[0]->type != LM_GGML_TYPE_F32 || op->src[1]->type != LM_GGML_TYPE_F32) {
429
+ return false;
430
+ }
431
+ float max_bias = 0.0f;
432
+
433
+ memcpy(&max_bias, (const float *) op->op_params + 1, sizeof(float));
434
+
435
+ return max_bias == 0.0f;
436
+ }
437
+ case LM_GGML_OP_IM2COL_BACK:
438
+ return src0->type == LM_GGML_TYPE_F32 && src1->type == LM_GGML_TYPE_F32;
439
+ case LM_GGML_OP_GET_ROWS_BACK:
440
+ return src0->type == LM_GGML_TYPE_F32 || src0->type == LM_GGML_TYPE_F16;
441
+ case LM_GGML_OP_OUT_PROD:
442
+ return (src0->type == LM_GGML_TYPE_F32 || (lm_ggml_is_quantized(src0->type) && src0->ne[2] == src1->ne[2] && src0->ne[3] == src1->ne[3])) &&
443
+ src1->type == LM_GGML_TYPE_F32 && op->type == LM_GGML_TYPE_F32;
444
+ default:
445
+ return true;
446
+ }
447
+ }
448
+
449
+ static bool lm_ggml_backend_cpu_device_supports_buft(lm_ggml_backend_dev_t dev, lm_ggml_backend_buffer_type_t buft) {
450
+ return lm_ggml_backend_buft_is_host(buft) || lm_ggml_backend_cpu_is_extra_buffer_type(buft);
451
+ LM_GGML_UNUSED(dev);
452
+ }
453
+
454
+ static const struct lm_ggml_backend_device_i lm_ggml_backend_cpu_device_i = {
455
+ /* .get_name = */ lm_ggml_backend_cpu_device_get_name,
456
+ /* .get_description = */ lm_ggml_backend_cpu_device_get_description,
457
+ /* .get_memory = */ lm_ggml_backend_cpu_device_get_memory,
458
+ /* .get_type = */ lm_ggml_backend_cpu_device_get_type,
459
+ /* .get_props = */ lm_ggml_backend_cpu_device_get_props,
460
+ /* .init_backend = */ lm_ggml_backend_cpu_device_init_backend,
461
+ /* .get_buffer_type = */ lm_ggml_backend_cpu_device_get_buffer_type,
462
+ /* .get_host_buffer_type = */ NULL,
463
+ /* .buffer_from_host_ptr = */ lm_ggml_backend_cpu_device_buffer_from_host_ptr,
464
+ /* .supports_op = */ lm_ggml_backend_cpu_device_supports_op,
465
+ /* .supports_buft = */ lm_ggml_backend_cpu_device_supports_buft,
466
+ /* .offload_op = */ NULL,
467
+ /* .event_new = */ NULL,
468
+ /* .event_free = */ NULL,
469
+ /* .event_synchronize = */ NULL,
470
+ };
471
+
472
+ // CPU backend - backend (reg)
473
+
474
+ static const char * lm_ggml_backend_cpu_reg_get_name(lm_ggml_backend_reg_t reg) {
475
+ return "CPU";
476
+
477
+ LM_GGML_UNUSED(reg);
478
+ }
479
+
480
+ static size_t lm_ggml_backend_cpu_reg_get_device_count(lm_ggml_backend_reg_t reg) {
481
+ return 1;
482
+
483
+ LM_GGML_UNUSED(reg);
484
+ }
485
+
486
+ static lm_ggml_backend_dev_t lm_ggml_backend_cpu_reg_get_device(lm_ggml_backend_reg_t reg, size_t index) {
487
+ LM_GGML_ASSERT(index == 0);
488
+
489
+ static lm_ggml_backend_cpu_device_context ctx;
490
+ static lm_ggml_backend_device lm_ggml_backend_cpu_device = {
491
+ /* .iface = */ lm_ggml_backend_cpu_device_i,
492
+ /* .reg = */ reg,
493
+ /* .context = */ &ctx,
494
+ };
495
+
496
+ return &lm_ggml_backend_cpu_device;
497
+ }
498
+
499
+ // This is intended to replace the the lm_ggml_cpu_has_* functions when loading the CPU backend dynamically,
500
+ // and additionally to allow other backends to expose their own list of features that applications can query using the same API
501
+ static lm_ggml_backend_feature * lm_ggml_backend_cpu_get_features(lm_ggml_backend_reg_t reg) {
502
+ static std::vector<lm_ggml_backend_feature> features = []() {
503
+ lm_ggml_cpu_init();
504
+
505
+ std::vector<lm_ggml_backend_feature> features;
506
+ if (lm_ggml_cpu_has_sse3()) {
507
+ features.push_back({ "SSE3", "1" });
508
+ }
509
+ if (lm_ggml_cpu_has_ssse3()) {
510
+ features.push_back({ "SSSE3", "1" });
511
+ }
512
+ if (lm_ggml_cpu_has_avx()) {
513
+ features.push_back({ "AVX", "1" });
514
+ }
515
+ if (lm_ggml_cpu_has_avx_vnni()) {
516
+ features.push_back({ "AVX_VNNI", "1" });
517
+ }
518
+ if (lm_ggml_cpu_has_avx2()) {
519
+ features.push_back({ "AVX2", "1" });
520
+ }
521
+ if (lm_ggml_cpu_has_f16c()) {
522
+ features.push_back({ "F16C", "1" });
523
+ }
524
+ if (lm_ggml_cpu_has_fma()) {
525
+ features.push_back({ "FMA", "1" });
526
+ }
527
+ if (lm_ggml_cpu_has_bmi2()) {
528
+ features.push_back({ "BMI2", "1" });
529
+ }
530
+ if (lm_ggml_cpu_has_avx512()) {
531
+ features.push_back({ "AVX512", "1" });
532
+ }
533
+ if (lm_ggml_cpu_has_avx512_vbmi()) {
534
+ features.push_back({ "AVX512_VBMI", "1" });
535
+ }
536
+ if (lm_ggml_cpu_has_avx512_vnni()) {
537
+ features.push_back({ "AVX512_VNNI", "1" });
538
+ }
539
+ if (lm_ggml_cpu_has_avx512_bf16()) {
540
+ features.push_back({ "AVX512_BF16", "1" });
541
+ }
542
+ if (lm_ggml_cpu_has_amx_int8()) {
543
+ features.push_back({ "AMX_INT8", "1" });
544
+ }
545
+ if (lm_ggml_cpu_has_neon()) {
546
+ features.push_back({ "NEON", "1" });
547
+ }
548
+ if (lm_ggml_cpu_has_arm_fma()) {
549
+ features.push_back({ "ARM_FMA", "1" });
550
+ }
551
+ if (lm_ggml_cpu_has_fp16_va()) {
552
+ features.push_back({ "FP16_VA", "1" });
553
+ }
554
+ if (lm_ggml_cpu_has_matmul_int8()) {
555
+ features.push_back({ "MATMUL_INT8", "1" });
556
+ }
557
+ if (lm_ggml_cpu_has_sve()) {
558
+ features.push_back({ "SVE", "1" });
559
+ }
560
+ if (lm_ggml_cpu_has_dotprod()) {
561
+ features.push_back({ "DOTPROD", "1" });
562
+ }
563
+ if (lm_ggml_cpu_get_sve_cnt() > 0) {
564
+ static std::string sve_cnt = std::to_string(lm_ggml_cpu_get_sve_cnt());
565
+ features.push_back({ "SVE_CNT", sve_cnt.c_str() });
566
+ }
567
+ if (lm_ggml_cpu_has_sme()) {
568
+ features.push_back({ "SME", "1" });
569
+ }
570
+ if (lm_ggml_cpu_has_riscv_v()) {
571
+ features.push_back({ "RISCV_V", "1" });
572
+ }
573
+ if (lm_ggml_cpu_has_vsx()) {
574
+ features.push_back({ "VSX", "1" });
575
+ }
576
+ if (lm_ggml_cpu_has_vxe()) {
577
+ features.push_back({ "VXE", "1" });
578
+ }
579
+ if (lm_ggml_cpu_has_nnpa()) {
580
+ features.push_back({ "NNPA", "1" });
581
+ }
582
+ if (lm_ggml_cpu_has_wasm_simd()) {
583
+ features.push_back({ "WASM_SIMD", "1" });
584
+ }
585
+ if (lm_ggml_cpu_has_llamafile()) {
586
+ features.push_back({ "LLAMAFILE", "1" });
587
+ }
588
+ #ifdef LM_GGML_USE_ACCELERATE
589
+ features.push_back({ "ACCELERATE", "1" });
590
+ #endif
591
+ #ifdef LM_GGML_USE_CPU_HBM
592
+ features.push_back({ "CPU_HBM", "1" });
593
+ #endif
594
+ #ifdef LM_GGML_USE_OPENMP
595
+ features.push_back({ "OPENMP", "1" });
596
+ #endif
597
+ #ifdef LM_GGML_USE_CPU_KLEIDIAI
598
+ features.push_back({ "KLEIDIAI", "1" });
599
+ #endif
600
+ #ifdef LM_GGML_USE_CPU_REPACK
601
+ features.push_back({ "REPACK", "1" });
602
+ #endif
603
+
604
+ features.push_back({ nullptr, nullptr });
605
+
606
+ return features;
607
+ }();
608
+
609
+ return features.data();
610
+
611
+ LM_GGML_UNUSED(reg);
612
+ }
613
+
614
+ static void * lm_ggml_backend_cpu_get_proc_address(lm_ggml_backend_reg_t reg, const char * name) {
615
+ if (strcmp(name, "lm_ggml_backend_set_n_threads") == 0) {
616
+ lm_ggml_backend_set_n_threads_t fct = lm_ggml_backend_cpu_set_n_threads;
617
+ return (void *)fct;
618
+ }
619
+ if (strcmp(name, "lm_ggml_backend_dev_get_extra_bufts") == 0) {
620
+ lm_ggml_backend_dev_get_extra_bufts_t fct = lm_ggml_backend_cpu_device_get_extra_buffers_type;
621
+ return (void *)fct;
622
+ }
623
+ if (strcmp(name, "lm_ggml_backend_get_features") == 0) {
624
+ return (void *)lm_ggml_backend_cpu_get_features;
625
+ }
626
+ if (strcmp(name, "lm_ggml_backend_set_abort_callback") == 0) {
627
+ return (void *)lm_ggml_backend_cpu_set_abort_callback;
628
+ }
629
+ if (strcmp(name, "lm_ggml_backend_cpu_numa_init") == 0) {
630
+ return (void *)lm_ggml_numa_init;
631
+ }
632
+ if (strcmp(name, "lm_ggml_backend_cpu_is_numa") == 0) {
633
+ return (void *)lm_ggml_is_numa;
634
+ }
635
+
636
+ // threadpool - TODO: move to ggml-base
637
+ if (strcmp(name, "lm_ggml_threadpool_new") == 0) {
638
+ return (void *)lm_ggml_threadpool_new;
639
+ }
640
+ if (strcmp(name, "lm_ggml_threadpool_free") == 0) {
641
+ return (void *)lm_ggml_threadpool_free;
642
+ }
643
+ if (strcmp(name, "lm_ggml_backend_cpu_set_threadpool") == 0) {
644
+ return (void *)lm_ggml_backend_cpu_set_threadpool;
645
+ }
646
+
647
+ return NULL;
648
+
649
+ LM_GGML_UNUSED(reg);
650
+ }
651
+
652
+ static const struct lm_ggml_backend_reg_i lm_ggml_backend_cpu_reg_i = {
653
+ /* .get_name = */ lm_ggml_backend_cpu_reg_get_name,
654
+ /* .get_device_count = */ lm_ggml_backend_cpu_reg_get_device_count,
655
+ /* .get_device = */ lm_ggml_backend_cpu_reg_get_device,
656
+ /* .get_proc_address = */ lm_ggml_backend_cpu_get_proc_address,
657
+ };
658
+
659
+ lm_ggml_backend_reg_t lm_ggml_backend_cpu_reg(void) {
660
+ // init CPU feature detection
661
+ lm_ggml_cpu_init();
662
+
663
+ static struct lm_ggml_backend_reg lm_ggml_backend_cpu_reg = {
664
+ /* .api_version = */ LM_GGML_BACKEND_API_VERSION,
665
+ /* .iface = */ lm_ggml_backend_cpu_reg_i,
666
+ /* .context = */ NULL,
667
+ };
668
+
669
+ return &lm_ggml_backend_cpu_reg;
670
+ }
671
+
672
+ LM_GGML_BACKEND_DL_IMPL(lm_ggml_backend_cpu_reg)