ggml-cpu.cpp 25 KB

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