ggml-cpu.cpp 22 KB

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