ggml-cpu.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #include "amx/amx.h"
  8. #include <cctype>
  9. #include <string>
  10. #include <vector>
  11. #ifdef GGML_USE_CPU_HBM
  12. #include "ggml-cpu-hbm.h"
  13. #endif
  14. #ifdef GGML_USE_CPU_KLEIDIAI
  15. #include "kleidiai/kleidiai.h"
  16. #endif
  17. #if defined(__APPLE__)
  18. #include <sys/types.h>
  19. #include <sys/sysctl.h>
  20. #endif
  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. // ggml-backend interface
  29. std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type() {
  30. static std::vector<ggml_backend_buffer_type_t> bufts = []() {
  31. std::vector<ggml_backend_buffer_type_t> bufts;
  32. #if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
  33. if (ggml_backend_amx_buffer_type()) {
  34. bufts.push_back(ggml_backend_amx_buffer_type());
  35. }
  36. #endif
  37. #ifdef GGML_USE_CPU_KLEIDIAI
  38. if (ggml_backend_cpu_kleidiai_buffer_type()) {
  39. bufts.push_back(ggml_backend_cpu_kleidiai_buffer_type());
  40. }
  41. #endif
  42. #ifdef GGML_USE_CPU_AARCH64
  43. if (ggml_backend_cpu_aarch64_buffer_type()) {
  44. bufts.push_back(ggml_backend_cpu_aarch64_buffer_type());
  45. }
  46. #endif
  47. bufts.push_back(NULL);
  48. return bufts;
  49. }();
  50. return bufts;
  51. }
  52. static ggml_backend_buffer_type_t * ggml_backend_cpu_device_get_extra_buffers_type(ggml_backend_dev_t device) {
  53. return ggml_backend_cpu_get_extra_buffers_type().data();
  54. GGML_UNUSED(device);
  55. }
  56. static bool ggml_backend_cpu_is_extra_buffer_type(ggml_backend_buffer_type_t buft) {
  57. for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
  58. if (extra && extra == buft) return true;
  59. }
  60. return false;
  61. }
  62. // CPU backend - backend (stream)
  63. struct ggml_backend_cpu_context {
  64. int n_threads;
  65. ggml_threadpool_t threadpool;
  66. uint8_t * work_data;
  67. size_t work_size;
  68. ggml_abort_callback abort_callback;
  69. void * abort_callback_data;
  70. };
  71. static const char * ggml_backend_cpu_get_name(ggml_backend_t backend) {
  72. return "CPU";
  73. GGML_UNUSED(backend);
  74. }
  75. static void ggml_backend_cpu_free(ggml_backend_t backend) {
  76. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  77. delete[] cpu_ctx->work_data;
  78. delete cpu_ctx;
  79. delete backend;
  80. }
  81. struct ggml_backend_plan_cpu {
  82. struct ggml_cplan cplan;
  83. struct ggml_cgraph cgraph;
  84. };
  85. static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(ggml_backend_t backend, const struct ggml_cgraph * cgraph) {
  86. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  87. struct ggml_backend_plan_cpu * cpu_plan = new ggml_backend_plan_cpu;
  88. cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
  89. cpu_plan->cgraph = *cgraph; // FIXME: deep copy
  90. if (cpu_plan->cplan.work_size > 0) {
  91. cpu_plan->cplan.work_data = new uint8_t[cpu_plan->cplan.work_size];
  92. if (cpu_plan->cplan.work_data == NULL) {
  93. delete cpu_plan;
  94. return NULL;
  95. }
  96. }
  97. cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
  98. cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  99. return cpu_plan;
  100. }
  101. static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  102. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  103. delete[] cpu_plan->cplan.work_data;
  104. delete cpu_plan;
  105. GGML_UNUSED(backend);
  106. }
  107. static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  108. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  109. return ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
  110. GGML_UNUSED(backend);
  111. }
  112. static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  113. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  114. struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
  115. if (cpu_ctx->work_size < cplan.work_size) {
  116. delete[] cpu_ctx->work_data;
  117. cpu_ctx->work_data = new uint8_t[cplan.work_size];
  118. if (cpu_ctx->work_data == NULL) {
  119. cpu_ctx->work_size = 0;
  120. return GGML_STATUS_ALLOC_FAILED;
  121. }
  122. cpu_ctx->work_size = cplan.work_size;
  123. }
  124. cplan.work_data = (uint8_t *)cpu_ctx->work_data;
  125. cplan.abort_callback = cpu_ctx->abort_callback;
  126. cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  127. return ggml_graph_compute(cgraph, &cplan);
  128. }
  129. static const struct ggml_backend_i ggml_backend_cpu_i = {
  130. /* .get_name = */ ggml_backend_cpu_get_name,
  131. /* .free = */ ggml_backend_cpu_free,
  132. /* .set_tensor_async = */ NULL,
  133. /* .get_tensor_async = */ NULL,
  134. /* .cpy_tensor_async = */ NULL,
  135. /* .synchronize = */ NULL,
  136. /* .graph_plan_create = */ ggml_backend_cpu_graph_plan_create,
  137. /* .graph_plan_free = */ ggml_backend_cpu_graph_plan_free,
  138. /* .graph_plan_update = */ NULL,
  139. /* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
  140. /* .graph_compute = */ ggml_backend_cpu_graph_compute,
  141. /* .event_record = */ NULL,
  142. /* .event_wait = */ NULL,
  143. };
  144. static ggml_guid_t ggml_backend_cpu_guid(void) {
  145. static ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
  146. return &guid;
  147. }
  148. ggml_backend_t ggml_backend_cpu_init(void) {
  149. // initialize CPU backend now to avoid slowing the first graph computation
  150. ggml_cpu_init();
  151. struct ggml_backend_cpu_context * ctx = new ggml_backend_cpu_context;
  152. if (ctx == NULL) {
  153. return NULL;
  154. }
  155. ctx->n_threads = GGML_DEFAULT_N_THREADS;
  156. ctx->threadpool = NULL;
  157. ctx->work_data = NULL;
  158. ctx->work_size = 0;
  159. ctx->abort_callback = NULL;
  160. ctx->abort_callback_data = NULL;
  161. ggml_backend_t cpu_backend = new ggml_backend {
  162. /* .guid = */ ggml_backend_cpu_guid(),
  163. /* .interface = */ ggml_backend_cpu_i,
  164. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0),
  165. /* .context = */ ctx,
  166. };
  167. if (cpu_backend == NULL) {
  168. delete ctx;
  169. return NULL;
  170. }
  171. return cpu_backend;
  172. }
  173. bool ggml_backend_is_cpu(ggml_backend_t backend) {
  174. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cpu_guid());
  175. }
  176. void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
  177. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  178. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  179. ctx->n_threads = n_threads;
  180. }
  181. void ggml_backend_cpu_set_threadpool(ggml_backend_t backend_cpu, ggml_threadpool_t threadpool) {
  182. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  183. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  184. if (ctx->threadpool && ctx->threadpool != threadpool) {
  185. // already had a different threadpool, pause/suspend it before switching
  186. ggml_threadpool_pause(ctx->threadpool);
  187. }
  188. ctx->threadpool = threadpool;
  189. }
  190. void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data) {
  191. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  192. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  193. ctx->abort_callback = abort_callback;
  194. ctx->abort_callback_data = abort_callback_data;
  195. }
  196. // CPU backend - device
  197. struct ggml_backend_cpu_device_context {
  198. std::string description = "CPU";
  199. ggml_backend_cpu_device_context() {
  200. #ifdef __APPLE__
  201. size_t len = 0;
  202. if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
  203. description.resize(len);
  204. sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
  205. }
  206. #elif defined(__linux__)
  207. FILE * f = fopen("/proc/cpuinfo", "r");
  208. if (f) {
  209. char buf[1024];
  210. while (fgets(buf, sizeof(buf), f)) {
  211. if (strncmp(buf, "model name", 10) == 0) {
  212. char * p = strchr(buf, ':');
  213. if (p) {
  214. p++;
  215. while (std::isspace(*p)) {
  216. p++;
  217. }
  218. while (std::isspace(p[strlen(p) - 1])) {
  219. p[strlen(p) - 1] = '\0';
  220. }
  221. description = p;
  222. break;
  223. }
  224. }
  225. }
  226. fclose(f);
  227. }
  228. #elif defined(_WIN32)
  229. HKEY hKey;
  230. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  231. TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
  232. 0,
  233. KEY_READ,
  234. &hKey) == ERROR_SUCCESS) {
  235. DWORD cpu_brand_size = 0;
  236. if (RegQueryValueExA(hKey,
  237. "ProcessorNameString",
  238. NULL,
  239. NULL,
  240. NULL,
  241. &cpu_brand_size) == ERROR_SUCCESS) {
  242. description.resize(cpu_brand_size);
  243. if (RegQueryValueExA(hKey,
  244. "ProcessorNameString",
  245. NULL,
  246. NULL,
  247. (LPBYTE)&description[0], // NOLINT
  248. &cpu_brand_size) == ERROR_SUCCESS) {
  249. if (description.find('\0') != std::string::npos) {
  250. description.resize(description.find('\0'));
  251. }
  252. }
  253. }
  254. RegCloseKey(hKey);
  255. }
  256. #endif
  257. }
  258. };
  259. static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) {
  260. return "CPU";
  261. GGML_UNUSED(dev);
  262. }
  263. static const char * ggml_backend_cpu_device_get_description(ggml_backend_dev_t dev) {
  264. struct ggml_backend_cpu_device_context * ctx = (struct ggml_backend_cpu_device_context *)dev->context;
  265. return ctx->description.c_str();
  266. }
  267. static void ggml_backend_cpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
  268. // TODO
  269. *free = 0;
  270. *total = 0;
  271. GGML_UNUSED(dev);
  272. }
  273. static enum ggml_backend_dev_type ggml_backend_cpu_device_get_type(ggml_backend_dev_t dev) {
  274. return GGML_BACKEND_DEVICE_TYPE_CPU;
  275. GGML_UNUSED(dev);
  276. }
  277. static void ggml_backend_cpu_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
  278. props->name = ggml_backend_cpu_device_get_name(dev);
  279. props->description = ggml_backend_cpu_device_get_description(dev);
  280. props->type = ggml_backend_cpu_device_get_type(dev);
  281. ggml_backend_cpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
  282. props->caps = {
  283. /* .async = */ false,
  284. /* .host_buffer = */ false,
  285. /* .buffer_from_host_ptr = */ true,
  286. /* .events = */ false,
  287. };
  288. }
  289. static ggml_backend_t ggml_backend_cpu_device_init_backend(ggml_backend_dev_t dev, const char * params) {
  290. return ggml_backend_cpu_init();
  291. GGML_UNUSED(dev);
  292. GGML_UNUSED(params);
  293. }
  294. static ggml_backend_buffer_type_t ggml_backend_cpu_device_get_buffer_type(ggml_backend_dev_t dev) {
  295. return ggml_backend_cpu_buffer_type();
  296. GGML_UNUSED(dev);
  297. }
  298. static ggml_backend_buffer_t ggml_backend_cpu_device_buffer_from_host_ptr(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size) {
  299. return ggml_backend_cpu_buffer_from_ptr(ptr, size);
  300. GGML_UNUSED(dev);
  301. GGML_UNUSED(max_tensor_size);
  302. }
  303. static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
  304. const struct ggml_tensor * src0 = op->src[0];
  305. const struct ggml_tensor * src1 = op->src[1];
  306. if (op->op == GGML_OP_NONE || op->op == GGML_OP_RESHAPE || op->op == GGML_OP_VIEW || op->op == GGML_OP_PERMUTE || op->op == GGML_OP_TRANSPOSE) {
  307. return true;
  308. }
  309. // extra_buffer_op?
  310. for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
  311. if (extra) {
  312. auto buf_extra = (ggml::cpu::extra_buffer_type*) extra->context;
  313. if (buf_extra && buf_extra->supports_op(dev, op)) {
  314. return true;
  315. }
  316. }
  317. }
  318. // the other case need host buffer.
  319. for (int i = 0; i < GGML_MAX_SRC; i++) {
  320. if (op->src[i] && op->src[i]->buffer && !ggml_backend_buft_is_host(op->src[i]->buffer->buft)) {
  321. return false;
  322. }
  323. }
  324. switch (op->op) {
  325. case GGML_OP_CPY:
  326. return
  327. op->type != GGML_TYPE_IQ3_XXS &&
  328. op->type != GGML_TYPE_IQ3_S &&
  329. op->type != GGML_TYPE_IQ2_XXS &&
  330. op->type != GGML_TYPE_IQ2_XS &&
  331. op->type != GGML_TYPE_IQ2_S &&
  332. op->type != GGML_TYPE_IQ1_S &&
  333. op->type != GGML_TYPE_IQ1_M; // missing type_traits.from_float
  334. case GGML_OP_MUL_MAT:
  335. return src1->type == GGML_TYPE_F32 || src1->type == ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
  336. case GGML_OP_SOFT_MAX_BACK: {
  337. if (op->src[0]->type != GGML_TYPE_F32 || op->src[1]->type != GGML_TYPE_F32) {
  338. return false;
  339. }
  340. float max_bias = 0.0f;
  341. memcpy(&max_bias, (const float *) op->op_params + 1, sizeof(float));
  342. return max_bias == 0.0f;
  343. }
  344. case GGML_OP_IM2COL_BACK:
  345. return src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32;
  346. case GGML_OP_OUT_PROD:
  347. return (src0->type == GGML_TYPE_F32 || (ggml_is_quantized(src0->type) && src0->ne[2] == src1->ne[2] && src0->ne[3] == src1->ne[3])) &&
  348. src1->type == GGML_TYPE_F32 && op->type == GGML_TYPE_F32;
  349. default:
  350. return true;
  351. }
  352. }
  353. static bool ggml_backend_cpu_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  354. return ggml_backend_buft_is_host(buft) || ggml_backend_cpu_is_extra_buffer_type(buft);
  355. GGML_UNUSED(dev);
  356. }
  357. static const struct ggml_backend_device_i ggml_backend_cpu_device_i = {
  358. /* .get_name = */ ggml_backend_cpu_device_get_name,
  359. /* .get_description = */ ggml_backend_cpu_device_get_description,
  360. /* .get_memory = */ ggml_backend_cpu_device_get_memory,
  361. /* .get_type = */ ggml_backend_cpu_device_get_type,
  362. /* .get_props = */ ggml_backend_cpu_device_get_props,
  363. /* .init_backend = */ ggml_backend_cpu_device_init_backend,
  364. /* .get_buffer_type = */ ggml_backend_cpu_device_get_buffer_type,
  365. /* .get_host_buffer_type = */ NULL,
  366. /* .buffer_from_host_ptr = */ ggml_backend_cpu_device_buffer_from_host_ptr,
  367. /* .supports_op = */ ggml_backend_cpu_device_supports_op,
  368. /* .supports_buft = */ ggml_backend_cpu_device_supports_buft,
  369. /* .offload_op = */ NULL,
  370. /* .event_new = */ NULL,
  371. /* .event_free = */ NULL,
  372. /* .event_synchronize = */ NULL,
  373. };
  374. // CPU backend - backend (reg)
  375. static const char * ggml_backend_cpu_reg_get_name(ggml_backend_reg_t reg) {
  376. return "CPU";
  377. GGML_UNUSED(reg);
  378. }
  379. static size_t ggml_backend_cpu_reg_get_device_count(ggml_backend_reg_t reg) {
  380. return 1;
  381. GGML_UNUSED(reg);
  382. }
  383. static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
  384. GGML_ASSERT(index == 0);
  385. static ggml_backend_cpu_device_context ctx;
  386. static ggml_backend_device ggml_backend_cpu_device = {
  387. /* .iface = */ ggml_backend_cpu_device_i,
  388. /* .reg = */ reg,
  389. /* .context = */ &ctx,
  390. };
  391. return &ggml_backend_cpu_device;
  392. }
  393. // This is intended to replace the the ggml_cpu_has_* functions when loading the CPU backend dynamically,
  394. // and additionally to allow other backends to expose their own list of features that applications can query using the same API
  395. static ggml_backend_feature * ggml_backend_cpu_get_features(ggml_backend_reg_t reg) {
  396. static std::vector<ggml_backend_feature> features = []() {
  397. ggml_cpu_init();
  398. std::vector<ggml_backend_feature> features;
  399. if (ggml_cpu_has_sse3()) {
  400. features.push_back({ "SSE3", "1" });
  401. }
  402. if (ggml_cpu_has_ssse3()) {
  403. features.push_back({ "SSSE3", "1" });
  404. }
  405. if (ggml_cpu_has_avx()) {
  406. features.push_back({ "AVX", "1" });
  407. }
  408. if (ggml_cpu_has_avx_vnni()) {
  409. features.push_back({ "AVX_VNNI", "1" });
  410. }
  411. if (ggml_cpu_has_avx2()) {
  412. features.push_back({ "AVX2", "1" });
  413. }
  414. if (ggml_cpu_has_f16c()) {
  415. features.push_back({ "F16C", "1" });
  416. }
  417. if (ggml_cpu_has_fma()) {
  418. features.push_back({ "FMA", "1" });
  419. }
  420. if (ggml_cpu_has_avx512()) {
  421. features.push_back({ "AVX512", "1" });
  422. }
  423. if (ggml_cpu_has_avx512_vbmi()) {
  424. features.push_back({ "AVX512_VBMI", "1" });
  425. }
  426. if (ggml_cpu_has_avx512_vnni()) {
  427. features.push_back({ "AVX512_VNNI", "1" });
  428. }
  429. if (ggml_cpu_has_avx512_bf16()) {
  430. features.push_back({ "AVX512_BF16", "1" });
  431. }
  432. if (ggml_cpu_has_amx_int8()) {
  433. features.push_back({ "AMX_INT8", "1" });
  434. }
  435. if (ggml_cpu_has_neon()) {
  436. features.push_back({ "NEON", "1" });
  437. }
  438. if (ggml_cpu_has_arm_fma()) {
  439. features.push_back({ "ARM_FMA", "1" });
  440. }
  441. if (ggml_cpu_has_fp16_va()) {
  442. features.push_back({ "FP16_VA", "1" });
  443. }
  444. if (ggml_cpu_has_matmul_int8()) {
  445. features.push_back({ "MATMUL_INT8", "1" });
  446. }
  447. if (ggml_cpu_has_sve()) {
  448. features.push_back({ "SVE", "1" });
  449. }
  450. if (ggml_cpu_has_dotprod()) {
  451. features.push_back({ "DOTPROD", "1" });
  452. }
  453. if (ggml_cpu_get_sve_cnt() > 0) {
  454. static std::string sve_cnt = std::to_string(ggml_cpu_get_sve_cnt());
  455. features.push_back({ "SVE_CNT", sve_cnt.c_str() });
  456. }
  457. if (ggml_cpu_has_sme()) {
  458. features.push_back({ "SME", "1" });
  459. }
  460. if (ggml_cpu_has_riscv_v()) {
  461. features.push_back({ "RISCV_V", "1" });
  462. }
  463. if (ggml_cpu_has_vsx()) {
  464. features.push_back({ "VSX", "1" });
  465. }
  466. if (ggml_cpu_has_wasm_simd()) {
  467. features.push_back({ "WASM_SIMD", "1" });
  468. }
  469. if (ggml_cpu_has_llamafile()) {
  470. features.push_back({ "LLAMAFILE", "1" });
  471. }
  472. #ifdef GGML_USE_ACCELERATE
  473. features.push_back({ "ACCELERATE", "1" });
  474. #endif
  475. #ifdef GGML_USE_CPU_HBM
  476. features.push_back({ "CPU_HBM", "1" });
  477. #endif
  478. #ifdef GGML_USE_OPENMP
  479. features.push_back({ "OPENMP", "1" });
  480. #endif
  481. #ifdef GGML_USE_CPU_KLEIDIAI
  482. features.push_back({ "KLEIDIAI", "1" });
  483. #endif
  484. #ifdef GGML_USE_CPU_AARCH64
  485. features.push_back({ "AARCH64_REPACK", "1" });
  486. #endif
  487. features.push_back({ nullptr, nullptr });
  488. return features;
  489. }();
  490. return features.data();
  491. GGML_UNUSED(reg);
  492. }
  493. static void * ggml_backend_cpu_get_proc_address(ggml_backend_reg_t reg, const char * name) {
  494. if (strcmp(name, "ggml_backend_set_n_threads") == 0) {
  495. ggml_backend_set_n_threads_t fct = ggml_backend_cpu_set_n_threads;
  496. return (void *)fct;
  497. }
  498. if (strcmp(name, "ggml_backend_dev_get_extra_bufts") == 0) {
  499. ggml_backend_dev_get_extra_bufts_t fct = ggml_backend_cpu_device_get_extra_buffers_type;
  500. return (void *)fct;
  501. }
  502. if (strcmp(name, "ggml_backend_get_features") == 0) {
  503. return (void *)ggml_backend_cpu_get_features;
  504. }
  505. if (strcmp(name, "ggml_backend_set_abort_callback") == 0) {
  506. return (void *)ggml_backend_cpu_set_abort_callback;
  507. }
  508. if (strcmp(name, "ggml_backend_cpu_numa_init") == 0) {
  509. return (void *)ggml_numa_init;
  510. }
  511. if (strcmp(name, "ggml_backend_cpu_is_numa") == 0) {
  512. return (void *)ggml_is_numa;
  513. }
  514. // threadpool - TODO: move to ggml-base
  515. if (strcmp(name, "ggml_threadpool_new") == 0) {
  516. return (void *)ggml_threadpool_new;
  517. }
  518. if (strcmp(name, "ggml_threadpool_free") == 0) {
  519. return (void *)ggml_threadpool_free;
  520. }
  521. if (strcmp(name, "ggml_backend_cpu_set_threadpool") == 0) {
  522. return (void *)ggml_backend_cpu_set_threadpool;
  523. }
  524. return NULL;
  525. GGML_UNUSED(reg);
  526. }
  527. static const struct ggml_backend_reg_i ggml_backend_cpu_reg_i = {
  528. /* .get_name = */ ggml_backend_cpu_reg_get_name,
  529. /* .get_device_count = */ ggml_backend_cpu_reg_get_device_count,
  530. /* .get_device = */ ggml_backend_cpu_reg_get_device,
  531. /* .get_proc_address = */ ggml_backend_cpu_get_proc_address,
  532. };
  533. ggml_backend_reg_t ggml_backend_cpu_reg(void) {
  534. // init CPU feature detection
  535. ggml_cpu_init();
  536. static struct ggml_backend_reg ggml_backend_cpu_reg = {
  537. /* .api_version = */ GGML_BACKEND_API_VERSION,
  538. /* .iface = */ ggml_backend_cpu_reg_i,
  539. /* .context = */ NULL,
  540. };
  541. return &ggml_backend_cpu_reg;
  542. }
  543. GGML_BACKEND_DL_IMPL(ggml_backend_cpu_reg)