ggml-cpu.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /**
  2. * llama.cpp - commit 081b29bd2a3d91e7772e3910ce223dd63b8d7d26 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "ggml-backend.h"
  27. #include "ggml-backend-impl.h"
  28. #include "ggml-cpu.h"
  29. #include "ggml-cpu-aarch64.h"
  30. #include "ggml-cpu-traits.h"
  31. #include "ggml-impl.h"
  32. #include "amx.h"
  33. #include <cctype>
  34. #include <string>
  35. #include <vector>
  36. #ifdef GGML_USE_CPU_HBM
  37. #include "ggml-cpu-hbm.h"
  38. #endif
  39. #if defined(__APPLE__)
  40. #include <sys/types.h>
  41. #include <sys/sysctl.h>
  42. #endif
  43. #if defined(_WIN32)
  44. #define WIN32_LEAN_AND_MEAN
  45. #ifndef NOMINMAX
  46. #define NOMINMAX
  47. #endif
  48. #include <windows.h>
  49. #endif
  50. // ggml-backend interface
  51. std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type() {
  52. static std::vector<ggml_backend_buffer_type_t> bufts = []() {
  53. std::vector<ggml_backend_buffer_type_t> bufts;
  54. #if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
  55. if (ggml_backend_amx_buffer_type()) {
  56. bufts.push_back(ggml_backend_amx_buffer_type());
  57. }
  58. #endif
  59. #ifdef GGML_USE_CPU_AARCH64
  60. if (ggml_backend_cpu_aarch64_buffer_type()) {
  61. bufts.push_back(ggml_backend_cpu_aarch64_buffer_type());
  62. }
  63. #endif
  64. bufts.push_back(NULL);
  65. return bufts;
  66. }();
  67. return bufts;
  68. }
  69. static ggml_backend_buffer_type_t * ggml_backend_cpu_device_get_extra_buffers_type(ggml_backend_dev_t device) {
  70. return ggml_backend_cpu_get_extra_buffers_type().data();
  71. GGML_UNUSED(device);
  72. }
  73. static bool ggml_backend_cpu_is_extra_buffer_type(ggml_backend_buffer_type_t buft) {
  74. for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
  75. if (extra && extra == buft) return true;
  76. }
  77. return false;
  78. }
  79. // CPU backend - backend (stream)
  80. struct ggml_backend_cpu_context {
  81. int n_threads;
  82. ggml_threadpool_t threadpool;
  83. uint8_t * work_data;
  84. size_t work_size;
  85. ggml_abort_callback abort_callback;
  86. void * abort_callback_data;
  87. };
  88. static const char * ggml_backend_cpu_get_name(ggml_backend_t backend) {
  89. return "CPU";
  90. GGML_UNUSED(backend);
  91. }
  92. static void ggml_backend_cpu_free(ggml_backend_t backend) {
  93. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  94. delete[] cpu_ctx->work_data;
  95. delete cpu_ctx;
  96. delete backend;
  97. }
  98. struct ggml_backend_plan_cpu {
  99. struct ggml_cplan cplan;
  100. struct ggml_cgraph cgraph;
  101. };
  102. static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(ggml_backend_t backend, const struct ggml_cgraph * cgraph) {
  103. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  104. struct ggml_backend_plan_cpu * cpu_plan = new ggml_backend_plan_cpu;
  105. cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
  106. cpu_plan->cgraph = *cgraph; // FIXME: deep copy
  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. cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
  115. cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  116. return cpu_plan;
  117. }
  118. static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  119. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  120. delete[] cpu_plan->cplan.work_data;
  121. delete cpu_plan;
  122. GGML_UNUSED(backend);
  123. }
  124. static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  125. struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
  126. return ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
  127. GGML_UNUSED(backend);
  128. }
  129. static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
  130. struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
  131. struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
  132. if (cpu_ctx->work_size < cplan.work_size) {
  133. delete[] cpu_ctx->work_data;
  134. cpu_ctx->work_data = new uint8_t[cplan.work_size];
  135. if (cpu_ctx->work_data == NULL) {
  136. cpu_ctx->work_size = 0;
  137. return GGML_STATUS_ALLOC_FAILED;
  138. }
  139. cpu_ctx->work_size = cplan.work_size;
  140. }
  141. cplan.work_data = (uint8_t *)cpu_ctx->work_data;
  142. cplan.abort_callback = cpu_ctx->abort_callback;
  143. cplan.abort_callback_data = cpu_ctx->abort_callback_data;
  144. return ggml_graph_compute(cgraph, &cplan);
  145. }
  146. static const struct ggml_backend_i ggml_backend_cpu_i = {
  147. /* .get_name = */ ggml_backend_cpu_get_name,
  148. /* .free = */ ggml_backend_cpu_free,
  149. /* .set_tensor_async = */ NULL,
  150. /* .get_tensor_async = */ NULL,
  151. /* .cpy_tensor_async = */ NULL,
  152. /* .synchronize = */ NULL,
  153. /* .graph_plan_create = */ ggml_backend_cpu_graph_plan_create,
  154. /* .graph_plan_free = */ ggml_backend_cpu_graph_plan_free,
  155. /* .graph_plan_update = */ NULL,
  156. /* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
  157. /* .graph_compute = */ ggml_backend_cpu_graph_compute,
  158. /* .event_record = */ NULL,
  159. /* .event_wait = */ NULL,
  160. };
  161. static ggml_guid_t ggml_backend_cpu_guid(void) {
  162. static ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
  163. return &guid;
  164. }
  165. ggml_backend_t ggml_backend_cpu_init(void) {
  166. // initialize CPU backend now to avoid slowing the first graph computation
  167. ggml_cpu_init();
  168. struct ggml_backend_cpu_context * ctx = new ggml_backend_cpu_context;
  169. if (ctx == NULL) {
  170. return NULL;
  171. }
  172. ctx->n_threads = GGML_DEFAULT_N_THREADS;
  173. ctx->threadpool = NULL;
  174. ctx->work_data = NULL;
  175. ctx->work_size = 0;
  176. ctx->abort_callback = NULL;
  177. ctx->abort_callback_data = NULL;
  178. ggml_backend_t cpu_backend = new ggml_backend {
  179. /* .guid = */ ggml_backend_cpu_guid(),
  180. /* .interface = */ ggml_backend_cpu_i,
  181. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0),
  182. /* .context = */ ctx,
  183. };
  184. if (cpu_backend == NULL) {
  185. delete ctx;
  186. return NULL;
  187. }
  188. return cpu_backend;
  189. }
  190. bool ggml_backend_is_cpu(ggml_backend_t backend) {
  191. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cpu_guid());
  192. }
  193. void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
  194. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  195. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  196. ctx->n_threads = n_threads;
  197. }
  198. void ggml_backend_cpu_set_threadpool(ggml_backend_t backend_cpu, ggml_threadpool_t threadpool) {
  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. if (ctx->threadpool && ctx->threadpool != threadpool) {
  202. // already had a different threadpool, pause/suspend it before switching
  203. ggml_threadpool_pause(ctx->threadpool);
  204. }
  205. ctx->threadpool = threadpool;
  206. }
  207. void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data) {
  208. GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
  209. struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
  210. ctx->abort_callback = abort_callback;
  211. ctx->abort_callback_data = abort_callback_data;
  212. }
  213. // CPU backend - device
  214. struct ggml_backend_cpu_device_context {
  215. std::string description = "CPU";
  216. ggml_backend_cpu_device_context() {
  217. #ifdef __APPLE__
  218. size_t len = 0;
  219. if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
  220. description.resize(len);
  221. sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
  222. }
  223. #elif defined(__linux__)
  224. FILE * f = fopen("/proc/cpuinfo", "r");
  225. if (f) {
  226. char buf[1024];
  227. while (fgets(buf, sizeof(buf), f)) {
  228. if (strncmp(buf, "model name", 10) == 0) {
  229. char * p = strchr(buf, ':');
  230. if (p) {
  231. p++;
  232. while (std::isspace(*p)) {
  233. p++;
  234. }
  235. while (std::isspace(p[strlen(p) - 1])) {
  236. p[strlen(p) - 1] = '\0';
  237. }
  238. description = p;
  239. break;
  240. }
  241. }
  242. }
  243. fclose(f);
  244. }
  245. #elif defined(_WIN32)
  246. HKEY hKey;
  247. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  248. TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
  249. 0,
  250. KEY_READ,
  251. &hKey) == ERROR_SUCCESS) {
  252. DWORD cpu_brand_size = 0;
  253. if (RegQueryValueExA(hKey,
  254. TEXT("ProcessorNameString"),
  255. NULL,
  256. NULL,
  257. NULL,
  258. &cpu_brand_size) == ERROR_SUCCESS) {
  259. description.resize(cpu_brand_size);
  260. if (RegQueryValueExA(hKey,
  261. TEXT("ProcessorNameString"),
  262. NULL,
  263. NULL,
  264. (LPBYTE)&description[0], // NOLINT
  265. &cpu_brand_size) == ERROR_SUCCESS) {
  266. if (description.find('\0') != std::string::npos) {
  267. description.resize(description.find('\0'));
  268. }
  269. }
  270. }
  271. RegCloseKey(hKey);
  272. }
  273. #endif
  274. }
  275. };
  276. static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) {
  277. return "CPU";
  278. GGML_UNUSED(dev);
  279. }
  280. static const char * ggml_backend_cpu_device_get_description(ggml_backend_dev_t dev) {
  281. struct ggml_backend_cpu_device_context * ctx = (struct ggml_backend_cpu_device_context *)dev->context;
  282. return ctx->description.c_str();
  283. }
  284. static void ggml_backend_cpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
  285. // TODO
  286. *free = 0;
  287. *total = 0;
  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. // extra_buffer_op?
  327. for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
  328. if (extra) {
  329. auto buf_extra = (ggml::cpu::extra_buffer_type*) extra->context;
  330. if (buf_extra && buf_extra->supports_op(dev, op)) {
  331. return true;
  332. }
  333. }
  334. }
  335. // the other case need host buffer.
  336. for (int i = 0; i < GGML_MAX_SRC; i++) {
  337. if (op->src[i] && op->src[i]->buffer && !ggml_backend_buft_is_host(op->src[i]->buffer->buft)) {
  338. return false;
  339. }
  340. }
  341. switch (op->op) {
  342. case GGML_OP_CPY:
  343. return
  344. op->type != GGML_TYPE_IQ3_XXS &&
  345. op->type != GGML_TYPE_IQ3_S &&
  346. op->type != GGML_TYPE_IQ2_XXS &&
  347. op->type != GGML_TYPE_IQ2_XS &&
  348. op->type != GGML_TYPE_IQ2_S &&
  349. op->type != GGML_TYPE_IQ1_S &&
  350. op->type != GGML_TYPE_IQ1_M; // missing type_traits.from_float
  351. case GGML_OP_MUL_MAT:
  352. return src1->type == GGML_TYPE_F32 || src1->type == ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
  353. case GGML_OP_ROPE_BACK:
  354. return op->src[2] == NULL && (op->op_params[2] & 4) == 0;
  355. case GGML_OP_IM2COL_BACK:
  356. return src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32;
  357. case GGML_OP_OUT_PROD:
  358. return (src0->type == GGML_TYPE_F32 || ggml_is_quantized(src0->type)) && src1->type == GGML_TYPE_F32;
  359. default:
  360. return true;
  361. }
  362. }
  363. static bool ggml_backend_cpu_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
  364. return ggml_backend_buft_is_host(buft) || ggml_backend_cpu_is_extra_buffer_type(buft);
  365. GGML_UNUSED(dev);
  366. }
  367. static const struct ggml_backend_device_i ggml_backend_cpu_device_i = {
  368. /* .get_name = */ ggml_backend_cpu_device_get_name,
  369. /* .get_description = */ ggml_backend_cpu_device_get_description,
  370. /* .get_memory = */ ggml_backend_cpu_device_get_memory,
  371. /* .get_type = */ ggml_backend_cpu_device_get_type,
  372. /* .get_props = */ ggml_backend_cpu_device_get_props,
  373. /* .init_backend = */ ggml_backend_cpu_device_init_backend,
  374. /* .get_buffer_type = */ ggml_backend_cpu_device_get_buffer_type,
  375. /* .get_host_buffer_type = */ NULL,
  376. /* .buffer_from_host_ptr = */ ggml_backend_cpu_device_buffer_from_host_ptr,
  377. /* .supports_op = */ ggml_backend_cpu_device_supports_op,
  378. /* .supports_buft = */ ggml_backend_cpu_device_supports_buft,
  379. /* .offload_op = */ NULL,
  380. /* .event_new = */ NULL,
  381. /* .event_free = */ NULL,
  382. /* .event_synchronize = */ NULL,
  383. };
  384. // CPU backend - backend (reg)
  385. static const char * ggml_backend_cpu_reg_get_name(ggml_backend_reg_t reg) {
  386. return "CPU";
  387. GGML_UNUSED(reg);
  388. }
  389. static size_t ggml_backend_cpu_reg_get_device_count(ggml_backend_reg_t reg) {
  390. return 1;
  391. GGML_UNUSED(reg);
  392. }
  393. static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
  394. GGML_ASSERT(index == 0);
  395. static ggml_backend_cpu_device_context ctx;
  396. static ggml_backend_device ggml_backend_cpu_device = {
  397. /* .iface = */ ggml_backend_cpu_device_i,
  398. /* .reg = */ reg,
  399. /* .context = */ &ctx,
  400. };
  401. return &ggml_backend_cpu_device;
  402. }
  403. // This is intended to replace the the ggml_cpu_has_* functions when loading the CPU backend dynamically,
  404. // and additionally to allow other backends to expose their own list of features that applications can query using the same API
  405. static ggml_backend_feature * ggml_backend_cpu_get_features(ggml_backend_reg_t reg) {
  406. static std::vector<ggml_backend_feature> features = []() {
  407. ggml_cpu_init();
  408. std::vector<ggml_backend_feature> features;
  409. if (ggml_cpu_has_sse3()) {
  410. features.push_back({ "SSE3", "1" });
  411. }
  412. if (ggml_cpu_has_ssse3()) {
  413. features.push_back({ "SSSE3", "1" });
  414. }
  415. if (ggml_cpu_has_avx()) {
  416. features.push_back({ "AVX", "1" });
  417. }
  418. if (ggml_cpu_has_avx_vnni()) {
  419. features.push_back({ "AVX_VNNI", "1" });
  420. }
  421. if (ggml_cpu_has_avx2()) {
  422. features.push_back({ "AVX2", "1" });
  423. }
  424. if (ggml_cpu_has_f16c()) {
  425. features.push_back({ "F16C", "1" });
  426. }
  427. if (ggml_cpu_has_fma()) {
  428. features.push_back({ "FMA", "1" });
  429. }
  430. if (ggml_cpu_has_avx512()) {
  431. features.push_back({ "AVX512", "1" });
  432. }
  433. if (ggml_cpu_has_avx512_vbmi()) {
  434. features.push_back({ "AVX512_VBMI", "1" });
  435. }
  436. if (ggml_cpu_has_avx512_vnni()) {
  437. features.push_back({ "AVX512_VNNI", "1" });
  438. }
  439. if (ggml_cpu_has_avx512_bf16()) {
  440. features.push_back({ "AVX512_BF16", "1" });
  441. }
  442. if (ggml_cpu_has_amx_int8()) {
  443. features.push_back({ "AMX_INT8", "1" });
  444. }
  445. if (ggml_cpu_has_neon()) {
  446. features.push_back({ "NEON", "1" });
  447. }
  448. if (ggml_cpu_has_arm_fma()) {
  449. features.push_back({ "ARM_FMA", "1" });
  450. }
  451. if (ggml_cpu_has_fp16_va()) {
  452. features.push_back({ "FP16_VA", "1" });
  453. }
  454. if (ggml_cpu_has_matmul_int8()) {
  455. features.push_back({ "MATMUL_INT8", "1" });
  456. }
  457. if (ggml_cpu_has_sve()) {
  458. features.push_back({ "SVE", "1" });
  459. }
  460. if (ggml_cpu_get_sve_cnt() > 0) {
  461. static std::string sve_cnt = std::to_string(ggml_cpu_get_sve_cnt());
  462. features.push_back({ "SVE_CNT", sve_cnt.c_str() });
  463. }
  464. if (ggml_cpu_has_riscv_v()) {
  465. features.push_back({ "RISCV_V", "1" });
  466. }
  467. if (ggml_cpu_has_vsx()) {
  468. features.push_back({ "VSX", "1" });
  469. }
  470. if (ggml_cpu_has_wasm_simd()) {
  471. features.push_back({ "WASM_SIMD", "1" });
  472. }
  473. if (ggml_cpu_has_llamafile()) {
  474. features.push_back({ "LLAMAFILE", "1" });
  475. }
  476. #ifdef GGML_USE_ACCELERATE
  477. features.push_back({ "ACCELERATE", "1" });
  478. #endif
  479. #ifdef GGML_USE_CPU_HBM
  480. features.push_back({ "CPU_HBM", "1" });
  481. #endif
  482. #ifdef GGML_USE_OPENMP
  483. features.push_back({ "OPENMP", "1" });
  484. #endif
  485. #ifdef GGML_USE_CPU_AARCH64
  486. features.push_back({ "AARCH64_REPACK", "1" });
  487. #endif
  488. features.push_back({ nullptr, nullptr });
  489. return features;
  490. }();
  491. return features.data();
  492. GGML_UNUSED(reg);
  493. }
  494. static void * ggml_backend_cpu_get_proc_address(ggml_backend_reg_t reg, const char * name) {
  495. if (strcmp(name, "ggml_backend_set_n_threads") == 0) {
  496. ggml_backend_set_n_threads_t fct = ggml_backend_cpu_set_n_threads;
  497. return (void *)fct;
  498. }
  499. if (strcmp(name, "ggml_backend_dev_get_extra_bufts") == 0) {
  500. ggml_backend_dev_get_extra_bufts_t fct = ggml_backend_cpu_device_get_extra_buffers_type;
  501. return (void *)fct;
  502. }
  503. if (strcmp(name, "ggml_backend_get_features") == 0) {
  504. return (void *)ggml_backend_cpu_get_features;
  505. }
  506. if (strcmp(name, "ggml_backend_set_abort_callback") == 0) {
  507. return (void *)ggml_backend_cpu_set_abort_callback;
  508. }
  509. if (strcmp(name, "ggml_backend_cpu_numa_init") == 0) {
  510. return (void *)ggml_numa_init;
  511. }
  512. if (strcmp(name, "ggml_backend_cpu_is_numa") == 0) {
  513. return (void *)ggml_is_numa;
  514. }
  515. // threadpool - TODO: move to ggml-base
  516. if (strcmp(name, "ggml_threadpool_new") == 0) {
  517. return (void *)ggml_threadpool_new;
  518. }
  519. if (strcmp(name, "ggml_threadpool_free") == 0) {
  520. return (void *)ggml_threadpool_free;
  521. }
  522. if (strcmp(name, "ggml_backend_cpu_set_threadpool") == 0) {
  523. return (void *)ggml_backend_cpu_set_threadpool;
  524. }
  525. return NULL;
  526. GGML_UNUSED(reg);
  527. }
  528. static const struct ggml_backend_reg_i ggml_backend_cpu_reg_i = {
  529. /* .get_name = */ ggml_backend_cpu_reg_get_name,
  530. /* .get_device_count = */ ggml_backend_cpu_reg_get_device_count,
  531. /* .get_device = */ ggml_backend_cpu_reg_get_device,
  532. /* .get_proc_address = */ ggml_backend_cpu_get_proc_address,
  533. };
  534. ggml_backend_reg_t ggml_backend_cpu_reg(void) {
  535. // init CPU feature detection
  536. ggml_cpu_init();
  537. static struct ggml_backend_reg ggml_backend_cpu_reg = {
  538. /* .api_version = */ GGML_BACKEND_API_VERSION,
  539. /* .iface = */ ggml_backend_cpu_reg_i,
  540. /* .context = */ NULL,
  541. };
  542. return &ggml_backend_cpu_reg;
  543. }
  544. GGML_BACKEND_DL_IMPL(ggml_backend_cpu_reg)