cui-llama.rn 1.3.6 → 1.4.0
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.
- package/README.md +22 -1
- package/android/src/main/CMakeLists.txt +25 -26
- package/android/src/main/java/com/rnllama/LlamaContext.java +31 -9
- package/android/src/main/java/com/rnllama/RNLlama.java +98 -0
- package/android/src/main/jni-utils.h +94 -0
- package/android/src/main/jni.cpp +132 -62
- package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +15 -0
- package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +15 -0
- package/cpp/common.cpp +1982 -1982
- package/cpp/common.h +665 -664
- package/cpp/ggml-cpu.c +14122 -14122
- package/cpp/ggml-cpu.cpp +627 -627
- package/cpp/ggml-metal-impl.h +288 -0
- package/cpp/ggml-opt.cpp +854 -0
- package/cpp/ggml-opt.h +216 -0
- package/cpp/llama-mmap.cpp +589 -589
- package/cpp/llama.cpp +12547 -12544
- package/cpp/rn-llama.hpp +117 -116
- package/cpp/sgemm.h +14 -14
- package/ios/RNLlama.mm +47 -0
- package/ios/RNLlamaContext.h +3 -1
- package/ios/RNLlamaContext.mm +71 -14
- package/jest/mock.js +15 -3
- package/lib/commonjs/NativeRNLlama.js.map +1 -1
- package/lib/commonjs/index.js +33 -37
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/NativeRNLlama.js.map +1 -1
- package/lib/module/index.js +31 -35
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/NativeRNLlama.d.ts +26 -6
- package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +21 -36
- package/lib/typescript/index.d.ts.map +1 -1
- package/llama-rn.podspec +4 -18
- package/package.json +2 -3
- package/src/NativeRNLlama.ts +32 -13
- package/src/index.ts +52 -47
- package/cpp/llama.cpp.rej +0 -23
package/cpp/ggml-cpu.cpp
CHANGED
@@ -1,627 +1,627 @@
|
|
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_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)
|