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