@fugood/llama.node 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/CMakeLists.txt +2 -0
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  7. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  8. package/bin/win32/arm64/llama-node.node +0 -0
  9. package/bin/win32/arm64/node.lib +0 -0
  10. package/bin/win32/x64/llama-node.node +0 -0
  11. package/bin/win32/x64/node.lib +0 -0
  12. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  13. package/bin/win32-vulkan/arm64/node.lib +0 -0
  14. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/x64/node.lib +0 -0
  16. package/package.json +1 -1
  17. package/src/DetokenizeWorker.cpp +1 -1
  18. package/src/EmbeddingWorker.cpp +2 -2
  19. package/src/LlamaCompletionWorker.cpp +8 -8
  20. package/src/LlamaCompletionWorker.h +2 -2
  21. package/src/LlamaContext.cpp +8 -9
  22. package/src/TokenizeWorker.cpp +1 -1
  23. package/src/common.hpp +4 -4
  24. package/src/llama.cpp/.github/workflows/build.yml +43 -9
  25. package/src/llama.cpp/.github/workflows/docker.yml +3 -0
  26. package/src/llama.cpp/CMakeLists.txt +7 -4
  27. package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
  28. package/src/llama.cpp/common/CMakeLists.txt +0 -2
  29. package/src/llama.cpp/common/arg.cpp +642 -607
  30. package/src/llama.cpp/common/arg.h +22 -22
  31. package/src/llama.cpp/common/common.cpp +79 -281
  32. package/src/llama.cpp/common/common.h +130 -100
  33. package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
  34. package/src/llama.cpp/common/log.cpp +50 -50
  35. package/src/llama.cpp/common/log.h +18 -18
  36. package/src/llama.cpp/common/ngram-cache.cpp +36 -36
  37. package/src/llama.cpp/common/ngram-cache.h +19 -19
  38. package/src/llama.cpp/common/sampling.cpp +116 -108
  39. package/src/llama.cpp/common/sampling.h +20 -20
  40. package/src/llama.cpp/docs/build.md +37 -17
  41. package/src/llama.cpp/examples/CMakeLists.txt +1 -1
  42. package/src/llama.cpp/examples/batched/batched.cpp +14 -14
  43. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +10 -11
  44. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +1 -1
  45. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +9 -9
  46. package/src/llama.cpp/examples/embedding/embedding.cpp +12 -12
  47. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +8 -8
  48. package/src/llama.cpp/examples/export-lora/export-lora.cpp +5 -5
  49. package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +7 -7
  50. package/src/llama.cpp/examples/gritlm/gritlm.cpp +18 -18
  51. package/src/llama.cpp/examples/imatrix/imatrix.cpp +20 -11
  52. package/src/llama.cpp/examples/infill/infill.cpp +40 -86
  53. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +42 -151
  54. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  55. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +11 -14
  56. package/src/llama.cpp/examples/llava/clip.cpp +1 -0
  57. package/src/llama.cpp/examples/llava/llava-cli.cpp +23 -23
  58. package/src/llama.cpp/examples/llava/llava.cpp +37 -3
  59. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +21 -21
  60. package/src/llama.cpp/examples/lookahead/lookahead.cpp +26 -26
  61. package/src/llama.cpp/examples/lookup/lookup-create.cpp +7 -7
  62. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  63. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +14 -14
  64. package/src/llama.cpp/examples/lookup/lookup.cpp +29 -29
  65. package/src/llama.cpp/examples/main/main.cpp +64 -109
  66. package/src/llama.cpp/examples/parallel/parallel.cpp +18 -19
  67. package/src/llama.cpp/examples/passkey/passkey.cpp +14 -14
  68. package/src/llama.cpp/examples/perplexity/perplexity.cpp +99 -120
  69. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +10 -9
  70. package/src/llama.cpp/examples/retrieval/retrieval.cpp +13 -13
  71. package/src/llama.cpp/examples/rpc/rpc-server.cpp +3 -1
  72. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +34 -17
  73. package/src/llama.cpp/examples/server/CMakeLists.txt +4 -13
  74. package/src/llama.cpp/examples/server/server.cpp +553 -691
  75. package/src/llama.cpp/examples/server/utils.hpp +312 -25
  76. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  77. package/src/llama.cpp/examples/simple/simple.cpp +128 -96
  78. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
  79. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +197 -0
  80. package/src/llama.cpp/examples/speculative/speculative.cpp +54 -51
  81. package/src/llama.cpp/examples/tokenize/tokenize.cpp +2 -2
  82. package/src/llama.cpp/ggml/CMakeLists.txt +15 -9
  83. package/src/llama.cpp/ggml/include/ggml-amx.h +25 -0
  84. package/src/llama.cpp/ggml/include/ggml-backend.h +46 -33
  85. package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
  86. package/src/llama.cpp/ggml/include/ggml-cann.h +9 -7
  87. package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
  88. package/src/llama.cpp/ggml/include/ggml-cpu.h +177 -0
  89. package/src/llama.cpp/ggml/include/ggml-cuda.h +12 -12
  90. package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
  91. package/src/llama.cpp/ggml/include/ggml-metal.h +11 -7
  92. package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
  93. package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
  94. package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
  95. package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
  96. package/src/llama.cpp/ggml/include/ggml.h +53 -393
  97. package/src/llama.cpp/ggml/src/CMakeLists.txt +66 -1149
  98. package/src/llama.cpp/ggml/src/ggml-aarch64.c +46 -3126
  99. package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -20
  100. package/src/llama.cpp/ggml/src/ggml-alloc.c +23 -27
  101. package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +107 -0
  102. package/src/llama.cpp/ggml/src/ggml-amx/common.h +94 -0
  103. package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
  104. package/src/llama.cpp/ggml/src/ggml-amx/mmq.cpp +2510 -0
  105. package/src/llama.cpp/ggml/src/ggml-amx/mmq.h +17 -0
  106. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +6 -25
  107. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +195 -0
  108. package/src/llama.cpp/ggml/src/ggml-backend.cpp +303 -864
  109. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +91 -0
  110. package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +213 -65
  111. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +46 -0
  112. package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +255 -149
  113. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +261 -0
  114. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.c +3560 -0
  115. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +30 -0
  116. package/src/llama.cpp/ggml/src/{ggml-cpu-impl.h → ggml-cpu/ggml-cpu-impl.h} +0 -243
  117. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10822 -0
  118. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  119. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +13970 -0
  120. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +663 -0
  121. package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.cpp +667 -1
  122. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +155 -0
  123. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +106 -0
  124. package/src/llama.cpp/ggml/src/ggml-impl.h +366 -16
  125. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +162 -0
  126. package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +238 -72
  127. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +108 -0
  128. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +249 -0
  129. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +100 -0
  130. package/src/llama.cpp/ggml/src/ggml-opt.cpp +867 -0
  131. package/src/llama.cpp/ggml/src/ggml-quants.c +187 -10692
  132. package/src/llama.cpp/ggml/src/ggml-quants.h +78 -125
  133. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +11 -0
  134. package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +475 -300
  135. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +81 -0
  136. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +3 -0
  137. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +40 -0
  138. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +258 -0
  139. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +1 -0
  140. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +2 -22
  141. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1011 -0
  142. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
  143. package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3584 -4142
  144. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +69 -67
  145. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +3 -3
  146. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
  147. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
  148. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +6 -0
  149. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
  150. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +138 -0
  151. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
  152. package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
  153. package/src/llama.cpp/ggml/src/ggml-threading.h +12 -0
  154. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +78 -0
  155. package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +555 -623
  156. package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/vulkan-shaders-gen.cpp +125 -206
  157. package/src/llama.cpp/ggml/src/ggml.c +4032 -19890
  158. package/src/llama.cpp/include/llama.h +67 -33
  159. package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
  160. package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
  161. package/src/llama.cpp/src/CMakeLists.txt +2 -1
  162. package/src/llama.cpp/src/llama-sampling.cpp +745 -105
  163. package/src/llama.cpp/src/llama-sampling.h +21 -2
  164. package/src/llama.cpp/src/llama-vocab.cpp +49 -9
  165. package/src/llama.cpp/src/llama-vocab.h +35 -11
  166. package/src/llama.cpp/src/llama.cpp +2636 -2406
  167. package/src/llama.cpp/src/unicode-data.cpp +2 -2
  168. package/src/llama.cpp/tests/CMakeLists.txt +1 -2
  169. package/src/llama.cpp/tests/test-arg-parser.cpp +14 -14
  170. package/src/llama.cpp/tests/test-backend-ops.cpp +185 -60
  171. package/src/llama.cpp/tests/test-barrier.cpp +1 -0
  172. package/src/llama.cpp/tests/test-chat-template.cpp +9 -5
  173. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -4
  174. package/src/llama.cpp/tests/test-log.cpp +2 -2
  175. package/src/llama.cpp/tests/test-opt.cpp +853 -142
  176. package/src/llama.cpp/tests/test-quantize-fns.cpp +22 -19
  177. package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
  178. package/src/llama.cpp/tests/test-rope.cpp +1 -0
  179. package/src/llama.cpp/tests/test-sampling.cpp +162 -137
  180. package/src/llama.cpp/tests/test-tokenizer-0.cpp +7 -7
  181. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
  182. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
  183. package/src/llama.cpp/common/train.cpp +0 -1515
  184. package/src/llama.cpp/common/train.h +0 -233
  185. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +0 -5
  186. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1639
  187. package/src/llama.cpp/tests/test-grad0.cpp +0 -1683
  188. /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
  189. /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
  190. /package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +0 -0
@@ -0,0 +1,663 @@
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-impl.h"
6
+ #include <cctype>
7
+ #include <string>
8
+ #include <vector>
9
+
10
+ #if defined(__APPLE__)
11
+ #include <sys/types.h>
12
+ #include <sys/sysctl.h>
13
+ #endif
14
+
15
+ #if defined(_WIN32)
16
+ #define WIN32_LEAN_AND_MEAN
17
+ #ifndef NOMINMAX
18
+ #define NOMINMAX
19
+ #endif
20
+ #include <windows.h>
21
+ #endif
22
+
23
+ // ggml-backend interface
24
+
25
+ #ifdef GGML_USE_CPU_HBM
26
+
27
+ // buffer type HBM
28
+
29
+ #include <hbwmalloc.h>
30
+
31
+ static const char * ggml_backend_cpu_hbm_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
32
+ return "CPU_HBM";
33
+
34
+ GGML_UNUSED(buft);
35
+ }
36
+
37
+ static void ggml_backend_cpu_hbm_buffer_free_buffer(ggml_backend_buffer_t buffer) {
38
+ hbw_free(buffer->context);
39
+ }
40
+
41
+ static ggml_backend_buffer_t ggml_backend_cpu_hbm_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
42
+ void * ptr;
43
+ int result = hbw_posix_memalign(&ptr, ggml_backend_cpu_buffer_type_get_alignment(buft), size);
44
+ if (result != 0) {
45
+ GGML_LOG_ERROR("failed to allocate HBM buffer of size %zu\n", size);
46
+ return NULL;
47
+ }
48
+
49
+ ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
50
+ buffer->buft = buft;
51
+ buffer->iface.free_buffer = ggml_backend_cpu_hbm_buffer_free_buffer;
52
+
53
+ return buffer;
54
+ }
55
+
56
+ ggml_backend_buffer_type_t ggml_backend_cpu_hbm_buffer_type(void) {
57
+ static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type_hbm = {
58
+ /* .iface = */ {
59
+ /* .get_name = */ ggml_backend_cpu_hbm_buffer_type_get_name,
60
+ /* .alloc_buffer = */ ggml_backend_cpu_hbm_buffer_type_alloc_buffer,
61
+ /* .get_alignment = */ ggml_backend_cpu_buffer_type_get_alignment,
62
+ /* .get_max_size = */ NULL, // defaults to SIZE_MAX
63
+ /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
64
+ /* .is_host = */ ggml_backend_cpu_buffer_type_is_host,
65
+ },
66
+ /* .context = */ NULL,
67
+ };
68
+
69
+ return &ggml_backend_cpu_buffer_type_hbm;
70
+ }
71
+ #endif
72
+
73
+ // buffer type AARCH64
74
+
75
+ static void ggml_backend_cpu_aarch64_buffer_init_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) {
76
+ tensor->extra = (void *)ggml_aarch64_get_optimal_repack_type(tensor); // NOLINT
77
+
78
+ GGML_UNUSED(buffer);
79
+ }
80
+
81
+ 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) {
82
+ GGML_ASSERT(offset == 0);
83
+ GGML_ASSERT(size == ggml_nbytes(tensor));
84
+
85
+ enum ggml_type repack_type = (enum ggml_type)(intptr_t)tensor->extra;
86
+
87
+ ggml_aarch64_repack_tensor(tensor, repack_type, data, size);
88
+
89
+ GGML_UNUSED(buffer);
90
+ }
91
+
92
+ static const char * ggml_backend_cpu_aarch64_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
93
+ return "CPU_AARCH64";
94
+
95
+ GGML_UNUSED(buft);
96
+ }
97
+
98
+ static ggml_backend_buffer_t ggml_backend_cpu_aarch64_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
99
+ auto * buffer = ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
100
+
101
+ if (buffer == NULL) {
102
+ return NULL;
103
+ }
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
+
109
+ return buffer;
110
+ }
111
+
112
+ ggml_backend_buffer_type_t ggml_backend_cpu_aarch64_buffer_type(void) {
113
+ static struct ggml_backend_buffer_type ggml_backend_cpu_buffer_type_aarch64 = {
114
+ /* .iface = */ {
115
+ /* .get_name = */ ggml_backend_cpu_aarch64_buffer_type_get_name,
116
+ /* .alloc_buffer = */ ggml_backend_cpu_aarch64_buffer_type_alloc_buffer,
117
+ /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
118
+ /* .get_max_size = */ NULL, // defaults to SIZE_MAX
119
+ /* .get_alloc_size = */ NULL, // defaults to ggml_nbytes
120
+ /* .is_host = */ NULL,
121
+ },
122
+ /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0),
123
+ /* .context = */ NULL,
124
+ };
125
+
126
+ return &ggml_backend_cpu_buffer_type_aarch64;
127
+ }
128
+
129
+ bool ggml_backend_cpu_buft_is_aarch64(ggml_backend_buffer_type_t buft) {
130
+ return buft == ggml_backend_cpu_aarch64_buffer_type();
131
+ }
132
+
133
+ static ggml_backend_buffer_type_t * ggml_backend_cpu_get_extra_bufts(ggml_backend_dev_t device) {
134
+ static std::vector<ggml_backend_buffer_type_t> bufts = []() {
135
+ std::vector<ggml_backend_buffer_type_t> bufts;
136
+
137
+ #ifdef GGML_USE_CPU_HBM
138
+ bufts.push_back(ggml_backend_cpu_hbm_buffer_type());
139
+ #endif
140
+
141
+ #ifdef GGML_USE_CPU_AARCH64
142
+ bufts.push_back(ggml_backend_cpu_aarch64_buffer_type());
143
+ #endif
144
+
145
+ bufts.push_back(NULL);
146
+
147
+ return bufts;
148
+ }();
149
+
150
+ return bufts.data();
151
+
152
+ GGML_UNUSED(device);
153
+ }
154
+
155
+ // CPU backend - backend (stream)
156
+
157
+ struct ggml_backend_cpu_context {
158
+ int n_threads;
159
+ ggml_threadpool_t threadpool;
160
+
161
+ uint8_t * work_data;
162
+ size_t work_size;
163
+
164
+ ggml_abort_callback abort_callback;
165
+ void * abort_callback_data;
166
+ };
167
+
168
+ static const char * ggml_backend_cpu_get_name(ggml_backend_t backend) {
169
+ return "CPU";
170
+
171
+ GGML_UNUSED(backend);
172
+ }
173
+
174
+ static void ggml_backend_cpu_free(ggml_backend_t backend) {
175
+ struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
176
+ delete[] cpu_ctx->work_data;
177
+ delete cpu_ctx;
178
+ delete backend;
179
+ }
180
+
181
+ struct ggml_backend_plan_cpu {
182
+ struct ggml_cplan cplan;
183
+ struct ggml_cgraph cgraph;
184
+ };
185
+
186
+ static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(ggml_backend_t backend, const struct ggml_cgraph * cgraph) {
187
+ struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
188
+
189
+ struct ggml_backend_plan_cpu * cpu_plan = new ggml_backend_plan_cpu;
190
+
191
+ cpu_plan->cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
192
+ cpu_plan->cgraph = *cgraph; // FIXME: deep copy
193
+
194
+ if (cpu_plan->cplan.work_size > 0) {
195
+ cpu_plan->cplan.work_data = new uint8_t[cpu_plan->cplan.work_size];
196
+ if (cpu_plan->cplan.work_data == NULL) {
197
+ delete cpu_plan;
198
+ return NULL;
199
+ }
200
+ }
201
+
202
+ cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
203
+ cpu_plan->cplan.abort_callback_data = cpu_ctx->abort_callback_data;
204
+
205
+ return cpu_plan;
206
+ }
207
+
208
+ static void ggml_backend_cpu_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
209
+ struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
210
+
211
+ delete[] cpu_plan->cplan.work_data;
212
+ delete cpu_plan;
213
+
214
+ GGML_UNUSED(backend);
215
+ }
216
+
217
+ static enum ggml_status ggml_backend_cpu_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
218
+ struct ggml_backend_plan_cpu * cpu_plan = (struct ggml_backend_plan_cpu *)plan;
219
+
220
+ return ggml_graph_compute(&cpu_plan->cgraph, &cpu_plan->cplan);
221
+
222
+ GGML_UNUSED(backend);
223
+ }
224
+
225
+ static enum ggml_status ggml_backend_cpu_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
226
+ struct ggml_backend_cpu_context * cpu_ctx = (struct ggml_backend_cpu_context *)backend->context;
227
+
228
+ struct ggml_cplan cplan = ggml_graph_plan(cgraph, cpu_ctx->n_threads, cpu_ctx->threadpool);
229
+
230
+ if (cpu_ctx->work_size < cplan.work_size) {
231
+ delete[] cpu_ctx->work_data;
232
+ cpu_ctx->work_data = new uint8_t[cplan.work_size];
233
+ if (cpu_ctx->work_data == NULL) {
234
+ cpu_ctx->work_size = 0;
235
+ return GGML_STATUS_ALLOC_FAILED;
236
+ }
237
+ cpu_ctx->work_size = cplan.work_size;
238
+ }
239
+ cplan.work_data = (uint8_t *)cpu_ctx->work_data;
240
+
241
+ cplan.abort_callback = cpu_ctx->abort_callback;
242
+ cplan.abort_callback_data = cpu_ctx->abort_callback_data;
243
+
244
+ return ggml_graph_compute(cgraph, &cplan);
245
+ }
246
+
247
+ static const struct ggml_backend_i ggml_backend_cpu_i = {
248
+ /* .get_name = */ ggml_backend_cpu_get_name,
249
+ /* .free = */ ggml_backend_cpu_free,
250
+ /* .set_tensor_async = */ NULL,
251
+ /* .get_tensor_async = */ NULL,
252
+ /* .cpy_tensor_async = */ NULL,
253
+ /* .synchronize = */ NULL,
254
+ /* .graph_plan_create = */ ggml_backend_cpu_graph_plan_create,
255
+ /* .graph_plan_free = */ ggml_backend_cpu_graph_plan_free,
256
+ /* .graph_plan_update = */ NULL,
257
+ /* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
258
+ /* .graph_compute = */ ggml_backend_cpu_graph_compute,
259
+ /* .event_record = */ NULL,
260
+ /* .event_wait = */ NULL,
261
+ };
262
+
263
+ static ggml_guid_t ggml_backend_cpu_guid(void) {
264
+ static ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
265
+ return &guid;
266
+ }
267
+
268
+ ggml_backend_t ggml_backend_cpu_init(void) {
269
+ // initialize CPU backend now to avoid slowing the first graph computation
270
+ ggml_cpu_init();
271
+
272
+ struct ggml_backend_cpu_context * ctx = new ggml_backend_cpu_context;
273
+ if (ctx == NULL) {
274
+ return NULL;
275
+ }
276
+
277
+ ctx->n_threads = GGML_DEFAULT_N_THREADS;
278
+ ctx->threadpool = NULL;
279
+ ctx->work_data = NULL;
280
+ ctx->work_size = 0;
281
+ ctx->abort_callback = NULL;
282
+ ctx->abort_callback_data = NULL;
283
+
284
+ ggml_backend_t cpu_backend = new ggml_backend {
285
+ /* .guid = */ ggml_backend_cpu_guid(),
286
+ /* .interface = */ ggml_backend_cpu_i,
287
+ /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0),
288
+ /* .context = */ ctx,
289
+ };
290
+
291
+ if (cpu_backend == NULL) {
292
+ delete ctx;
293
+ return NULL;
294
+ }
295
+
296
+ return cpu_backend;
297
+ }
298
+
299
+ bool ggml_backend_is_cpu(ggml_backend_t backend) {
300
+ return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cpu_guid());
301
+ }
302
+
303
+ void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
304
+ GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
305
+
306
+ struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
307
+ ctx->n_threads = n_threads;
308
+ }
309
+
310
+ void ggml_backend_cpu_set_threadpool(ggml_backend_t backend_cpu, ggml_threadpool_t threadpool) {
311
+ GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
312
+
313
+ struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
314
+
315
+ if (ctx->threadpool && ctx->threadpool != threadpool) {
316
+ // already had a different threadpool, pause/suspend it before switching
317
+ ggml_threadpool_pause(ctx->threadpool);
318
+ }
319
+ ctx->threadpool = threadpool;
320
+ }
321
+
322
+ void ggml_backend_cpu_set_abort_callback(ggml_backend_t backend_cpu, ggml_abort_callback abort_callback, void * abort_callback_data) {
323
+ GGML_ASSERT(ggml_backend_is_cpu(backend_cpu));
324
+
325
+ struct ggml_backend_cpu_context * ctx = (struct ggml_backend_cpu_context *)backend_cpu->context;
326
+ ctx->abort_callback = abort_callback;
327
+ ctx->abort_callback_data = abort_callback_data;
328
+ }
329
+
330
+ // CPU backend - device
331
+
332
+ struct ggml_backend_cpu_device_context {
333
+ std::string description = "CPU";
334
+
335
+ ggml_backend_cpu_device_context() {
336
+ #ifdef __APPLE__
337
+ size_t len = 0;
338
+ if (!sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0)) {
339
+ description.resize(len);
340
+ sysctlbyname("machdep.cpu.brand_string", &description[0], &len, NULL, 0); // NOLINT
341
+ }
342
+ #elif defined(__linux__)
343
+ FILE * f = fopen("/proc/cpuinfo", "r");
344
+ if (f) {
345
+ char buf[1024];
346
+ while (fgets(buf, sizeof(buf), f)) {
347
+ if (strncmp(buf, "model name", 10) == 0) {
348
+ char * p = strchr(buf, ':');
349
+ if (p) {
350
+ p++;
351
+ while (std::isspace(*p)) {
352
+ p++;
353
+ }
354
+ while (std::isspace(p[strlen(p) - 1])) {
355
+ p[strlen(p) - 1] = '\0';
356
+ }
357
+ description = p;
358
+ break;
359
+ }
360
+ }
361
+ }
362
+ fclose(f);
363
+ }
364
+ #elif defined(_WIN32)
365
+ HKEY hKey;
366
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
367
+ TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
368
+ 0,
369
+ KEY_READ,
370
+ &hKey) == ERROR_SUCCESS) {
371
+ DWORD cpu_brand_size = 0;
372
+ if (RegQueryValueExA(hKey,
373
+ TEXT("ProcessorNameString"),
374
+ NULL,
375
+ NULL,
376
+ NULL,
377
+ &cpu_brand_size) == ERROR_SUCCESS) {
378
+ description.resize(cpu_brand_size);
379
+ if (RegQueryValueExA(hKey,
380
+ TEXT("ProcessorNameString"),
381
+ NULL,
382
+ NULL,
383
+ (LPBYTE)&description[0], // NOLINT
384
+ &cpu_brand_size) == ERROR_SUCCESS) {
385
+ if (description.find('\0') != std::string::npos) {
386
+ description.resize(description.find('\0'));
387
+ }
388
+ }
389
+ }
390
+ RegCloseKey(hKey);
391
+ }
392
+ #endif
393
+ }
394
+ };
395
+
396
+ static const char * ggml_backend_cpu_device_get_name(ggml_backend_dev_t dev) {
397
+ return "CPU";
398
+
399
+ GGML_UNUSED(dev);
400
+ }
401
+
402
+ static const char * ggml_backend_cpu_device_get_description(ggml_backend_dev_t dev) {
403
+ struct ggml_backend_cpu_device_context * ctx = (struct ggml_backend_cpu_device_context *)dev->context;
404
+
405
+ return ctx->description.c_str();
406
+ }
407
+
408
+ static void ggml_backend_cpu_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
409
+ // TODO
410
+ *free = 0;
411
+ *total = 0;
412
+
413
+ GGML_UNUSED(dev);
414
+ }
415
+
416
+ static enum ggml_backend_dev_type ggml_backend_cpu_device_get_type(ggml_backend_dev_t dev) {
417
+ return GGML_BACKEND_DEVICE_TYPE_CPU;
418
+
419
+ GGML_UNUSED(dev);
420
+ }
421
+
422
+ static void ggml_backend_cpu_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
423
+ props->name = ggml_backend_cpu_device_get_name(dev);
424
+ props->description = ggml_backend_cpu_device_get_description(dev);
425
+ props->type = ggml_backend_cpu_device_get_type(dev);
426
+ ggml_backend_cpu_device_get_memory(dev, &props->memory_free, &props->memory_total);
427
+ props->caps = {
428
+ /* .async = */ false,
429
+ /* .host_buffer = */ false,
430
+ /* .buffer_from_host_ptr = */ true,
431
+ /* .events = */ false,
432
+ };
433
+ }
434
+
435
+ static ggml_backend_t ggml_backend_cpu_device_init_backend(ggml_backend_dev_t dev, const char * params) {
436
+ return ggml_backend_cpu_init();
437
+
438
+ GGML_UNUSED(dev);
439
+ GGML_UNUSED(params);
440
+ }
441
+
442
+ static ggml_backend_buffer_type_t ggml_backend_cpu_device_get_buffer_type(ggml_backend_dev_t dev) {
443
+ return ggml_backend_cpu_buffer_type();
444
+
445
+ GGML_UNUSED(dev);
446
+ }
447
+
448
+ 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) {
449
+ return ggml_backend_cpu_buffer_from_ptr(ptr, size);
450
+
451
+ GGML_UNUSED(dev);
452
+ GGML_UNUSED(max_tensor_size);
453
+ }
454
+
455
+ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
456
+ const struct ggml_tensor * src0 = op->src[0];
457
+ const struct ggml_tensor * src1 = op->src[1];
458
+
459
+ if (src0 && src0->buffer && ggml_backend_cpu_buft_is_aarch64(src0->buffer->buft)) {
460
+ if (op->op != GGML_OP_MUL_MAT || src0->type != GGML_TYPE_Q4_0 || ggml_aarch64_get_optimal_repack_type(src0) == GGML_TYPE_Q4_0) {
461
+ return false;
462
+ }
463
+ }
464
+
465
+ for (int i = 1; i < GGML_MAX_SRC; i++) {
466
+ if (op->src[i] && op->src[i]->buffer && ggml_backend_cpu_buft_is_aarch64(op->src[i]->buffer->buft)) {
467
+ return false;
468
+ }
469
+ }
470
+
471
+ switch (op->op) {
472
+ case GGML_OP_CPY:
473
+ return
474
+ op->type != GGML_TYPE_IQ2_XXS &&
475
+ op->type != GGML_TYPE_IQ2_XS &&
476
+ op->type != GGML_TYPE_IQ1_S &&
477
+ op->type != GGML_TYPE_IQ1_M; // missing type_traits.from_float
478
+ case GGML_OP_MUL_MAT:
479
+ return src1->type == GGML_TYPE_F32 || src1->type == ggml_get_type_traits_cpu(src0->type)->vec_dot_type;
480
+ case GGML_OP_ROPE_BACK:
481
+ return op->src[2] == NULL && (op->op_params[2] & 4) == 0;
482
+ case GGML_OP_IM2COL_BACK:
483
+ return src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32;
484
+ case GGML_OP_OUT_PROD:
485
+ return (src0->type == GGML_TYPE_F32 || ggml_is_quantized(src0->type)) && src1->type == GGML_TYPE_F32;
486
+ default:
487
+ return true;
488
+ }
489
+
490
+ GGML_UNUSED(dev);
491
+ }
492
+
493
+ static bool ggml_backend_cpu_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
494
+ return ggml_backend_buft_is_host(buft) || ggml_backend_cpu_buft_is_aarch64(buft);
495
+
496
+ GGML_UNUSED(dev);
497
+ }
498
+
499
+ static const struct ggml_backend_device_i ggml_backend_cpu_device_i = {
500
+ /* .get_name = */ ggml_backend_cpu_device_get_name,
501
+ /* .get_description = */ ggml_backend_cpu_device_get_description,
502
+ /* .get_memory = */ ggml_backend_cpu_device_get_memory,
503
+ /* .get_type = */ ggml_backend_cpu_device_get_type,
504
+ /* .get_props = */ ggml_backend_cpu_device_get_props,
505
+ /* .init_backend = */ ggml_backend_cpu_device_init_backend,
506
+ /* .get_buffer_type = */ ggml_backend_cpu_device_get_buffer_type,
507
+ /* .get_host_buffer_type = */ NULL,
508
+ /* .buffer_from_host_ptr = */ ggml_backend_cpu_device_buffer_from_host_ptr,
509
+ /* .supports_op = */ ggml_backend_cpu_device_supports_op,
510
+ /* .supports_buft = */ ggml_backend_cpu_device_supports_buft,
511
+ /* .offload_op = */ NULL,
512
+ /* .event_new = */ NULL,
513
+ /* .event_free = */ NULL,
514
+ /* .event_synchronize = */ NULL,
515
+ };
516
+
517
+ // CPU backend - backend (reg)
518
+
519
+ static const char * ggml_backend_cpu_reg_get_name(ggml_backend_reg_t reg) {
520
+ return "CPU";
521
+
522
+ GGML_UNUSED(reg);
523
+ }
524
+
525
+ static size_t ggml_backend_cpu_reg_get_device_count(ggml_backend_reg_t reg) {
526
+ return 1;
527
+
528
+ GGML_UNUSED(reg);
529
+ }
530
+
531
+ static ggml_backend_dev_t ggml_backend_cpu_reg_get_device(ggml_backend_reg_t reg, size_t index) {
532
+ GGML_ASSERT(index == 0);
533
+
534
+ static ggml_backend_cpu_device_context ctx;
535
+ static ggml_backend_device ggml_backend_cpu_device = {
536
+ /* .iface = */ ggml_backend_cpu_device_i,
537
+ /* .reg = */ reg,
538
+ /* .context = */ &ctx,
539
+ };
540
+
541
+ return &ggml_backend_cpu_device;
542
+ }
543
+
544
+ struct ggml_backend_feature {
545
+ const char * name;
546
+ const char * value;
547
+ };
548
+
549
+ // Not used yet
550
+ // This is intended to replace the the ggml_cpu_has_* functions when loading the CPU backend dynamically,
551
+ // and additionally to allow other backends to expose their own list of features that applications can query using the same API.
552
+ static ggml_backend_feature * ggml_backend_cpu_get_features(ggml_backend_reg_t reg) {
553
+ static std::vector<ggml_backend_feature> features = []() {
554
+ std::vector<ggml_backend_feature> features;
555
+ if (ggml_cpu_has_sse3()) {
556
+ features.push_back({ "SSE3", "1" });
557
+ }
558
+ if (ggml_cpu_has_ssse3()) {
559
+ features.push_back({ "SSSE3", "1" });
560
+ }
561
+ if (ggml_cpu_has_avx()) {
562
+ features.push_back({ "AVX", "1" });
563
+ }
564
+ if (ggml_cpu_has_avx2()) {
565
+ features.push_back({ "AVX2", "1" });
566
+ }
567
+ if (ggml_cpu_has_f16c()) {
568
+ features.push_back({ "F16C", "1" });
569
+ }
570
+ if (ggml_cpu_has_fma()) {
571
+ features.push_back({ "FMA", "1" });
572
+ }
573
+ if (ggml_cpu_has_avx_vnni()) {
574
+ features.push_back({ "AVX_VNNI", "1" });
575
+ }
576
+ if (ggml_cpu_has_avx512()) {
577
+ features.push_back({ "AVX512", "1" });
578
+ }
579
+ if (ggml_cpu_has_avx512_vbmi()) {
580
+ features.push_back({ "AVX512_VBMI", "1" });
581
+ }
582
+ if (ggml_cpu_has_avx512_vnni()) {
583
+ features.push_back({ "AVX512_VNNI", "1" });
584
+ }
585
+ if (ggml_cpu_has_avx512_bf16()) {
586
+ features.push_back({ "AVX512_BF16", "1" });
587
+ }
588
+ if (ggml_cpu_has_amx_int8()) {
589
+ features.push_back({ "AMX_INT8", "1" });
590
+ }
591
+ if (ggml_cpu_has_neon()) {
592
+ features.push_back({ "NEON", "1" });
593
+ }
594
+ if (ggml_cpu_has_arm_fma()) {
595
+ features.push_back({ "ARM_FMA", "1" });
596
+ }
597
+ if (ggml_cpu_has_fp16_va()) {
598
+ features.push_back({ "FP16_VA", "1" });
599
+ }
600
+ if (ggml_cpu_has_matmul_int8()) {
601
+ features.push_back({ "MATMUL_INT8", "1" });
602
+ }
603
+ if (ggml_cpu_has_sve()) {
604
+ features.push_back({ "SVE", "1" });
605
+ }
606
+ if (ggml_cpu_get_sve_cnt() > 0) {
607
+ static std::string sve_cnt = std::to_string(ggml_cpu_get_sve_cnt());
608
+ features.push_back({ "SVE_CNT", sve_cnt.c_str() });
609
+ }
610
+ if (ggml_cpu_has_riscv_v()) {
611
+ features.push_back({ "RISCV_V", "1" });
612
+ }
613
+ if (ggml_cpu_has_vsx()) {
614
+ features.push_back({ "VSX", "1" });
615
+ }
616
+ if (ggml_cpu_has_wasm_simd()) {
617
+ features.push_back({ "WASM_SIMD", "1" });
618
+ }
619
+ if (ggml_cpu_has_llamafile()) {
620
+ features.push_back({ "LLAMAFILE", "1" });
621
+ }
622
+
623
+ features.push_back({ nullptr, nullptr });
624
+
625
+ return features;
626
+ }();
627
+
628
+ return features.data();
629
+
630
+ GGML_UNUSED(reg);
631
+ }
632
+
633
+ static void * ggml_backend_cpu_get_proc_address(ggml_backend_reg_t reg, const char * name) {
634
+ if (strcmp(name, "ggml_backend_set_n_threads") == 0) {
635
+ return (void *)ggml_backend_cpu_set_n_threads;
636
+ }
637
+ if (strcmp(name, "ggml_backend_dev_get_extra_bufts") == 0) {
638
+ return (void *)ggml_backend_cpu_get_extra_bufts;
639
+ }
640
+
641
+ return NULL;
642
+
643
+ GGML_UNUSED(reg);
644
+ }
645
+
646
+ static const struct ggml_backend_reg_i ggml_backend_cpu_reg_i = {
647
+ /* .get_name = */ ggml_backend_cpu_reg_get_name,
648
+ /* .get_device_count = */ ggml_backend_cpu_reg_get_device_count,
649
+ /* .get_device = */ ggml_backend_cpu_reg_get_device,
650
+ /* .get_proc_address = */ ggml_backend_cpu_get_proc_address,
651
+ };
652
+
653
+ ggml_backend_reg_t ggml_backend_cpu_reg(void) {
654
+ // init CPU feature detection
655
+ ggml_cpu_init();
656
+
657
+ static struct ggml_backend_reg ggml_backend_cpu_reg = {
658
+ /* .iface = */ ggml_backend_cpu_reg_i,
659
+ /* .context = */ NULL,
660
+ };
661
+
662
+ return &ggml_backend_cpu_reg;
663
+ }