@fugood/llama.node 0.3.1 → 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 (252) hide show
  1. package/CMakeLists.txt +1 -8
  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 +4 -2
  17. package/src/DetokenizeWorker.cpp +1 -1
  18. package/src/EmbeddingWorker.cpp +2 -2
  19. package/src/LlamaCompletionWorker.cpp +10 -10
  20. package/src/LlamaCompletionWorker.h +2 -2
  21. package/src/LlamaContext.cpp +14 -17
  22. package/src/TokenizeWorker.cpp +1 -1
  23. package/src/common.hpp +5 -4
  24. package/src/llama.cpp/.github/workflows/build.yml +137 -29
  25. package/src/llama.cpp/.github/workflows/close-issue.yml +5 -0
  26. package/src/llama.cpp/.github/workflows/docker.yml +46 -34
  27. package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +7 -0
  28. package/src/llama.cpp/.github/workflows/nix-ci.yml +7 -0
  29. package/src/llama.cpp/.github/workflows/python-check-requirements.yml +2 -4
  30. package/src/llama.cpp/.github/workflows/python-type-check.yml +3 -1
  31. package/src/llama.cpp/.github/workflows/server.yml +7 -0
  32. package/src/llama.cpp/CMakeLists.txt +26 -11
  33. package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
  34. package/src/llama.cpp/common/CMakeLists.txt +10 -10
  35. package/src/llama.cpp/common/arg.cpp +2041 -0
  36. package/src/llama.cpp/common/arg.h +77 -0
  37. package/src/llama.cpp/common/common.cpp +523 -1861
  38. package/src/llama.cpp/common/common.h +234 -106
  39. package/src/llama.cpp/common/console.cpp +3 -0
  40. package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
  41. package/src/llama.cpp/common/log.cpp +401 -0
  42. package/src/llama.cpp/common/log.h +66 -698
  43. package/src/llama.cpp/common/ngram-cache.cpp +39 -36
  44. package/src/llama.cpp/common/ngram-cache.h +19 -19
  45. package/src/llama.cpp/common/sampling.cpp +356 -350
  46. package/src/llama.cpp/common/sampling.h +62 -139
  47. package/src/llama.cpp/common/stb_image.h +5990 -6398
  48. package/src/llama.cpp/docs/build.md +72 -17
  49. package/src/llama.cpp/examples/CMakeLists.txt +1 -2
  50. package/src/llama.cpp/examples/batched/batched.cpp +49 -65
  51. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +42 -53
  52. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +55 -52
  53. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +22 -22
  54. package/src/llama.cpp/examples/cvector-generator/pca.hpp +3 -13
  55. package/src/llama.cpp/examples/embedding/embedding.cpp +147 -91
  56. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +37 -37
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +39 -38
  58. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +14 -39
  59. package/src/llama.cpp/examples/{baby-llama → gen-docs}/CMakeLists.txt +2 -2
  60. package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +83 -0
  61. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +58 -39
  62. package/src/llama.cpp/examples/gritlm/gritlm.cpp +46 -39
  63. package/src/llama.cpp/examples/imatrix/imatrix.cpp +75 -69
  64. package/src/llama.cpp/examples/infill/infill.cpp +131 -192
  65. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +276 -178
  66. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  67. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +40 -36
  68. package/src/llama.cpp/examples/llava/CMakeLists.txt +7 -0
  69. package/src/llama.cpp/examples/llava/clip.cpp +686 -150
  70. package/src/llama.cpp/examples/llava/clip.h +11 -2
  71. package/src/llama.cpp/examples/llava/llava-cli.cpp +60 -71
  72. package/src/llama.cpp/examples/llava/llava.cpp +146 -26
  73. package/src/llama.cpp/examples/llava/llava.h +2 -3
  74. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +323 -0
  75. package/src/llama.cpp/examples/llava/requirements.txt +1 -0
  76. package/src/llama.cpp/examples/lookahead/lookahead.cpp +55 -56
  77. package/src/llama.cpp/examples/lookup/lookup-create.cpp +15 -13
  78. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  79. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +34 -33
  80. package/src/llama.cpp/examples/lookup/lookup.cpp +60 -63
  81. package/src/llama.cpp/examples/main/main.cpp +216 -313
  82. package/src/llama.cpp/examples/parallel/parallel.cpp +58 -59
  83. package/src/llama.cpp/examples/passkey/passkey.cpp +53 -61
  84. package/src/llama.cpp/examples/perplexity/perplexity.cpp +277 -311
  85. package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
  86. package/src/llama.cpp/examples/quantize/quantize.cpp +27 -9
  87. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +12 -12
  88. package/src/llama.cpp/examples/retrieval/retrieval.cpp +57 -52
  89. package/src/llama.cpp/examples/rpc/rpc-server.cpp +27 -2
  90. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +60 -46
  91. package/src/llama.cpp/examples/server/CMakeLists.txt +7 -18
  92. package/src/llama.cpp/examples/server/server.cpp +1347 -1531
  93. package/src/llama.cpp/examples/server/tests/requirements.txt +2 -1
  94. package/src/llama.cpp/examples/server/utils.hpp +396 -107
  95. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  96. package/src/llama.cpp/examples/simple/simple.cpp +132 -106
  97. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
  98. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +197 -0
  99. package/src/llama.cpp/examples/speculative/speculative.cpp +153 -124
  100. package/src/llama.cpp/examples/sycl/run-llama2.sh +10 -19
  101. package/src/llama.cpp/examples/sycl/win-run-llama2.bat +1 -1
  102. package/src/llama.cpp/examples/tokenize/tokenize.cpp +27 -29
  103. package/src/llama.cpp/ggml/CMakeLists.txt +29 -12
  104. package/src/llama.cpp/ggml/include/ggml-alloc.h +3 -3
  105. package/src/llama.cpp/ggml/include/ggml-amx.h +25 -0
  106. package/src/llama.cpp/ggml/include/ggml-backend.h +166 -68
  107. package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
  108. package/src/llama.cpp/ggml/include/ggml-cann.h +17 -19
  109. package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
  110. package/src/llama.cpp/ggml/include/ggml-cpu.h +177 -0
  111. package/src/llama.cpp/ggml/include/ggml-cuda.h +17 -17
  112. package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
  113. package/src/llama.cpp/ggml/include/ggml-metal.h +13 -12
  114. package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
  115. package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
  116. package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
  117. package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
  118. package/src/llama.cpp/ggml/include/ggml.h +272 -505
  119. package/src/llama.cpp/ggml/src/CMakeLists.txt +69 -1110
  120. package/src/llama.cpp/ggml/src/ggml-aarch64.c +52 -2116
  121. package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -20
  122. package/src/llama.cpp/ggml/src/ggml-alloc.c +29 -27
  123. package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +107 -0
  124. package/src/llama.cpp/ggml/src/ggml-amx/common.h +94 -0
  125. package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
  126. package/src/llama.cpp/ggml/src/ggml-amx/mmq.cpp +2510 -0
  127. package/src/llama.cpp/ggml/src/ggml-amx/mmq.h +17 -0
  128. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +144 -81
  129. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +195 -0
  130. package/src/llama.cpp/ggml/src/{ggml-backend.c → ggml-backend.cpp} +394 -635
  131. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +91 -0
  132. package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +217 -70
  133. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +46 -0
  134. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +4 -27
  135. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +32 -4
  136. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +179 -41
  137. package/src/llama.cpp/ggml/src/ggml-cann/common.h +1 -0
  138. package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +458 -353
  139. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +2 -1
  140. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +2 -0
  141. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +278 -0
  142. package/src/llama.cpp/ggml/src/ggml-common.h +20 -0
  143. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +261 -0
  144. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.c +3560 -0
  145. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +30 -0
  146. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +371 -0
  147. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10822 -0
  148. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  149. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +13970 -0
  150. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +663 -0
  151. package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +1885 -0
  152. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +155 -0
  153. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h +14 -0
  154. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +178 -0
  155. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +134 -0
  156. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +106 -0
  157. package/src/llama.cpp/ggml/src/ggml-impl.h +380 -584
  158. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +162 -0
  159. package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +233 -87
  160. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +108 -0
  161. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +249 -0
  162. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +100 -0
  163. package/src/llama.cpp/ggml/src/ggml-opt.cpp +867 -0
  164. package/src/llama.cpp/ggml/src/ggml-quants.c +369 -9994
  165. package/src/llama.cpp/ggml/src/ggml-quants.h +78 -110
  166. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +11 -0
  167. package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +560 -335
  168. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +81 -0
  169. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +6 -0
  170. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +51 -0
  171. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +310 -0
  172. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +1 -0
  173. package/src/llama.cpp/ggml/src/ggml-sycl/conv.cpp +99 -0
  174. package/src/llama.cpp/ggml/src/ggml-sycl/conv.hpp +21 -0
  175. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +57 -57
  176. package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +1 -1
  177. package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +106 -106
  178. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +4 -4
  179. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +18 -25
  180. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1011 -0
  181. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
  182. package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +101 -0
  183. package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3350 -3980
  184. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +125 -0
  185. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +23 -0
  186. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +70 -68
  187. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +9 -6
  188. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
  189. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
  190. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +8 -0
  191. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +1 -1
  192. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +71 -0
  193. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.hpp +21 -0
  194. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
  195. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +138 -0
  196. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
  197. package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
  198. package/src/llama.cpp/ggml/src/ggml-threading.h +12 -0
  199. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +78 -0
  200. package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +2034 -1718
  201. package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +2 -0
  202. package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/vulkan-shaders-gen.cpp +152 -185
  203. package/src/llama.cpp/ggml/src/ggml.c +2075 -16579
  204. package/src/llama.cpp/include/llama.h +296 -285
  205. package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +112 -0
  206. package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.out +46 -0
  207. package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
  208. package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
  209. package/src/llama.cpp/requirements/requirements-convert_legacy_llama.txt +1 -1
  210. package/src/llama.cpp/src/CMakeLists.txt +2 -1
  211. package/src/llama.cpp/src/llama-grammar.cpp +721 -122
  212. package/src/llama.cpp/src/llama-grammar.h +120 -15
  213. package/src/llama.cpp/src/llama-impl.h +156 -1
  214. package/src/llama.cpp/src/llama-sampling.cpp +2058 -346
  215. package/src/llama.cpp/src/llama-sampling.h +39 -47
  216. package/src/llama.cpp/src/llama-vocab.cpp +390 -127
  217. package/src/llama.cpp/src/llama-vocab.h +60 -20
  218. package/src/llama.cpp/src/llama.cpp +6215 -3263
  219. package/src/llama.cpp/src/unicode-data.cpp +6 -4
  220. package/src/llama.cpp/src/unicode-data.h +4 -4
  221. package/src/llama.cpp/src/unicode.cpp +15 -7
  222. package/src/llama.cpp/tests/CMakeLists.txt +4 -2
  223. package/src/llama.cpp/tests/test-arg-parser.cpp +131 -0
  224. package/src/llama.cpp/tests/test-backend-ops.cpp +1725 -297
  225. package/src/llama.cpp/tests/test-barrier.cpp +94 -0
  226. package/src/llama.cpp/tests/test-chat-template.cpp +9 -5
  227. package/src/llama.cpp/tests/test-grammar-integration.cpp +23 -38
  228. package/src/llama.cpp/tests/test-grammar-parser.cpp +6 -4
  229. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +23 -8
  230. package/src/llama.cpp/tests/test-llama-grammar.cpp +9 -8
  231. package/src/llama.cpp/tests/test-log.cpp +39 -0
  232. package/src/llama.cpp/tests/test-opt.cpp +853 -142
  233. package/src/llama.cpp/tests/test-quantize-fns.cpp +28 -19
  234. package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
  235. package/src/llama.cpp/tests/test-rope.cpp +2 -1
  236. package/src/llama.cpp/tests/test-sampling.cpp +226 -142
  237. package/src/llama.cpp/tests/test-tokenizer-0.cpp +56 -36
  238. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
  239. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
  240. package/patches/llama.patch +0 -22
  241. package/src/llama.cpp/.github/workflows/bench.yml +0 -310
  242. package/src/llama.cpp/common/grammar-parser.cpp +0 -536
  243. package/src/llama.cpp/common/grammar-parser.h +0 -29
  244. package/src/llama.cpp/common/train.cpp +0 -1513
  245. package/src/llama.cpp/common/train.h +0 -233
  246. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1640
  247. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +0 -6
  248. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +0 -275
  249. package/src/llama.cpp/ggml/src/llamafile/sgemm.cpp +0 -1027
  250. package/src/llama.cpp/tests/test-grad0.cpp +0 -1566
  251. /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
  252. /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
@@ -0,0 +1,30 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ // GGML internal header
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ // Quantization
12
+ void quantize_mat_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nrows, int64_t n_per_row, int64_t blck_size_interleave);
13
+
14
+ // GEMV
15
+ void ggml_gemv_q4_0_4x4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
16
+ void ggml_gemv_q4_0_4x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
17
+ void ggml_gemv_q4_0_8x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
18
+
19
+ // GEMM
20
+ void ggml_gemm_q4_0_4x4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
21
+ void ggml_gemm_q4_0_4x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
22
+ void ggml_gemm_q4_0_8x8_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc);
23
+
24
+ void ggml_aarch64_repack_tensor(struct ggml_tensor * cur, enum ggml_type repack_type, const void * data, size_t data_size);
25
+ enum ggml_type ggml_aarch64_get_optimal_repack_type(const struct ggml_tensor * cur);
26
+
27
+ #ifdef __cplusplus
28
+ }
29
+ #endif
30
+
@@ -0,0 +1,371 @@
1
+ #pragma once
2
+
3
+ // GGML CPU internal header
4
+
5
+ #include "ggml.h"
6
+ #include "ggml-impl.h"
7
+ #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
8
+ //#include <stddef.h>
9
+ #include <stdbool.h>
10
+ #include <string.h> // memcpy
11
+ #include <math.h> // fabsf
12
+
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ #if defined(_MSC_VER)
19
+
20
+ #define m512bh(p) p
21
+ #define m512i(p) p
22
+
23
+ #else
24
+
25
+ #define m512bh(p) (__m512bh)(p)
26
+ #define m512i(p) (__m512i)(p)
27
+
28
+ #endif
29
+
30
+ // __FMA__ and __F16C__ are not defined in MSVC, however they are implied with AVX2/AVX512
31
+ #if defined(_MSC_VER) && (defined(__AVX2__) || defined(__AVX512F__))
32
+ #ifndef __FMA__
33
+ #define __FMA__
34
+ #endif
35
+ #ifndef __F16C__
36
+ #define __F16C__
37
+ #endif
38
+ #endif
39
+
40
+ // __SSE3__ and __SSSE3__ are not defined in MSVC, but SSE3/SSSE3 are present when AVX/AVX2/AVX512 are available
41
+ #if defined(_MSC_VER) && (defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__))
42
+ #ifndef __SSE3__
43
+ #define __SSE3__
44
+ #endif
45
+ #ifndef __SSSE3__
46
+ #define __SSSE3__
47
+ #endif
48
+ #endif
49
+
50
+ #if defined(__ARM_FEATURE_SVE)
51
+ #include <arm_sve.h>
52
+ #include <sys/prctl.h>
53
+ #endif
54
+
55
+ // 16-bit float
56
+ // on Arm, we use __fp16
57
+ // on x86, we use uint16_t
58
+ #if defined(__ARM_NEON)
59
+
60
+ // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
61
+ //
62
+ // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
63
+ //
64
+ #include <arm_neon.h>
65
+
66
+ #ifdef _MSC_VER
67
+
68
+ typedef uint16_t ggml_fp16_internal_t;
69
+
70
+ #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
71
+
72
+ #else
73
+
74
+ typedef __fp16 ggml_fp16_internal_t;
75
+
76
+ #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
77
+
78
+ #endif // _MSC_VER
79
+
80
+ #if !defined(__aarch64__)
81
+
82
+ // 32-bit ARM compatibility
83
+
84
+ // vaddlvq_s16
85
+ // vpaddq_s16
86
+ // vpaddq_s32
87
+ // vaddvq_s32
88
+ // vaddvq_f32
89
+ // vmaxvq_f32
90
+ // vcvtnq_s32_f32
91
+ // vzip1_u8
92
+ // vzip2_u8
93
+
94
+ inline static int32_t vaddlvq_s16(int16x8_t v) {
95
+ int32x4_t v0 = vreinterpretq_s32_s64(vpaddlq_s32(vpaddlq_s16(v)));
96
+ return vgetq_lane_s32(v0, 0) + vgetq_lane_s32(v0, 2);
97
+ }
98
+
99
+ inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
100
+ int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
101
+ int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
102
+ return vcombine_s16(a0, b0);
103
+ }
104
+
105
+ inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
106
+ int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
107
+ int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
108
+ return vcombine_s32(a0, b0);
109
+ }
110
+
111
+ inline static int32_t vaddvq_s32(int32x4_t v) {
112
+ return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
113
+ }
114
+
115
+ inline static float vaddvq_f32(float32x4_t v) {
116
+ return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
117
+ }
118
+
119
+ inline static float vmaxvq_f32(float32x4_t v) {
120
+ return
121
+ MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
122
+ MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
123
+ }
124
+
125
+ inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
126
+ int32x4_t res;
127
+
128
+ res[0] = roundf(vgetq_lane_f32(v, 0));
129
+ res[1] = roundf(vgetq_lane_f32(v, 1));
130
+ res[2] = roundf(vgetq_lane_f32(v, 2));
131
+ res[3] = roundf(vgetq_lane_f32(v, 3));
132
+
133
+ return res;
134
+ }
135
+
136
+ inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
137
+ uint8x8_t res;
138
+
139
+ res[0] = a[0]; res[1] = b[0];
140
+ res[2] = a[1]; res[3] = b[1];
141
+ res[4] = a[2]; res[5] = b[2];
142
+ res[6] = a[3]; res[7] = b[3];
143
+
144
+ return res;
145
+ }
146
+
147
+ inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
148
+ uint8x8_t res;
149
+
150
+ res[0] = a[4]; res[1] = b[4];
151
+ res[2] = a[5]; res[3] = b[5];
152
+ res[4] = a[6]; res[5] = b[6];
153
+ res[6] = a[7]; res[7] = b[7];
154
+
155
+ return res;
156
+ }
157
+
158
+ // vld1q_s16_x2
159
+ // vld1q_u8_x2
160
+ // vld1q_u8_x4
161
+ // vld1q_s8_x2
162
+ // vld1q_s8_x4
163
+ // TODO: double-check these work correctly
164
+
165
+ typedef struct ggml_int16x8x2_t {
166
+ int16x8_t val[2];
167
+ } ggml_int16x8x2_t;
168
+
169
+ inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
170
+ ggml_int16x8x2_t res;
171
+
172
+ res.val[0] = vld1q_s16(ptr + 0);
173
+ res.val[1] = vld1q_s16(ptr + 8);
174
+
175
+ return res;
176
+ }
177
+
178
+ typedef struct ggml_uint8x16x2_t {
179
+ uint8x16_t val[2];
180
+ } ggml_uint8x16x2_t;
181
+
182
+ inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
183
+ ggml_uint8x16x2_t res;
184
+
185
+ res.val[0] = vld1q_u8(ptr + 0);
186
+ res.val[1] = vld1q_u8(ptr + 16);
187
+
188
+ return res;
189
+ }
190
+
191
+ typedef struct ggml_uint8x16x4_t {
192
+ uint8x16_t val[4];
193
+ } ggml_uint8x16x4_t;
194
+
195
+ inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
196
+ ggml_uint8x16x4_t res;
197
+
198
+ res.val[0] = vld1q_u8(ptr + 0);
199
+ res.val[1] = vld1q_u8(ptr + 16);
200
+ res.val[2] = vld1q_u8(ptr + 32);
201
+ res.val[3] = vld1q_u8(ptr + 48);
202
+
203
+ return res;
204
+ }
205
+
206
+ typedef struct ggml_int8x16x2_t {
207
+ int8x16_t val[2];
208
+ } ggml_int8x16x2_t;
209
+
210
+ inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
211
+ ggml_int8x16x2_t res;
212
+
213
+ res.val[0] = vld1q_s8(ptr + 0);
214
+ res.val[1] = vld1q_s8(ptr + 16);
215
+
216
+ return res;
217
+ }
218
+
219
+ typedef struct ggml_int8x16x4_t {
220
+ int8x16_t val[4];
221
+ } ggml_int8x16x4_t;
222
+
223
+ inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
224
+ ggml_int8x16x4_t res;
225
+
226
+ res.val[0] = vld1q_s8(ptr + 0);
227
+ res.val[1] = vld1q_s8(ptr + 16);
228
+ res.val[2] = vld1q_s8(ptr + 32);
229
+ res.val[3] = vld1q_s8(ptr + 48);
230
+
231
+ return res;
232
+ }
233
+
234
+ // NOTE: not tested
235
+ inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
236
+ int8x16_t res;
237
+
238
+ res[ 0] = a[b[ 0]];
239
+ res[ 1] = a[b[ 1]];
240
+ res[ 2] = a[b[ 2]];
241
+ res[ 3] = a[b[ 3]];
242
+ res[ 4] = a[b[ 4]];
243
+ res[ 5] = a[b[ 5]];
244
+ res[ 6] = a[b[ 6]];
245
+ res[ 7] = a[b[ 7]];
246
+ res[ 8] = a[b[ 8]];
247
+ res[ 9] = a[b[ 9]];
248
+ res[10] = a[b[10]];
249
+ res[11] = a[b[11]];
250
+ res[12] = a[b[12]];
251
+ res[13] = a[b[13]];
252
+ res[14] = a[b[14]];
253
+ res[15] = a[b[15]];
254
+
255
+ return res;
256
+ }
257
+
258
+ // NOTE: not tested
259
+ inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
260
+ uint8x16_t res;
261
+
262
+ res[ 0] = a[b[ 0]];
263
+ res[ 1] = a[b[ 1]];
264
+ res[ 2] = a[b[ 2]];
265
+ res[ 3] = a[b[ 3]];
266
+ res[ 4] = a[b[ 4]];
267
+ res[ 5] = a[b[ 5]];
268
+ res[ 6] = a[b[ 6]];
269
+ res[ 7] = a[b[ 7]];
270
+ res[ 8] = a[b[ 8]];
271
+ res[ 9] = a[b[ 9]];
272
+ res[10] = a[b[10]];
273
+ res[11] = a[b[11]];
274
+ res[12] = a[b[12]];
275
+ res[13] = a[b[13]];
276
+ res[14] = a[b[14]];
277
+ res[15] = a[b[15]];
278
+
279
+ return res;
280
+ }
281
+
282
+ #else
283
+
284
+ #define ggml_int16x8x2_t int16x8x2_t
285
+ #define ggml_uint8x16x2_t uint8x16x2_t
286
+ #define ggml_uint8x16x4_t uint8x16x4_t
287
+ #define ggml_int8x16x2_t int8x16x2_t
288
+ #define ggml_int8x16x4_t int8x16x4_t
289
+
290
+ #define ggml_vld1q_s16_x2 vld1q_s16_x2
291
+ #define ggml_vld1q_u8_x2 vld1q_u8_x2
292
+ #define ggml_vld1q_u8_x4 vld1q_u8_x4
293
+ #define ggml_vld1q_s8_x2 vld1q_s8_x2
294
+ #define ggml_vld1q_s8_x4 vld1q_s8_x4
295
+ #define ggml_vqtbl1q_s8 vqtbl1q_s8
296
+ #define ggml_vqtbl1q_u8 vqtbl1q_u8
297
+
298
+ #endif // !defined(__aarch64__)
299
+
300
+ #if !defined(__ARM_FEATURE_DOTPROD)
301
+
302
+ inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
303
+ const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
304
+ const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
305
+
306
+ return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
307
+ }
308
+
309
+ #else
310
+
311
+ #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
312
+
313
+ #endif // !defined(__ARM_FEATURE_DOTPROD)
314
+
315
+ #endif // defined(__ARM_NEON)
316
+
317
+ #ifdef __wasm_simd128__
318
+ #include <wasm_simd128.h>
319
+ #else
320
+ #ifdef __POWER9_VECTOR__
321
+ #include <altivec.h>
322
+ #undef bool
323
+ #define bool _Bool
324
+ #else
325
+ #if defined(_MSC_VER) || defined(__MINGW32__)
326
+ #include <intrin.h>
327
+ #else
328
+ #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__) || defined(__SSE__)
329
+ #if !defined(__riscv)
330
+ #include <immintrin.h>
331
+ #endif
332
+ #endif
333
+ #endif
334
+ #endif
335
+ #endif
336
+
337
+ #ifdef __riscv_v_intrinsic
338
+ #include <riscv_vector.h>
339
+ #endif
340
+
341
+ #if defined(__loongarch64)
342
+ #if defined(__loongarch_asx)
343
+ #include <lasxintrin.h>
344
+ #endif
345
+ #if defined(__loongarch_sx)
346
+ #include <lsxintrin.h>
347
+ #endif
348
+ #endif
349
+
350
+ #if defined(__loongarch_asx)
351
+
352
+ typedef union {
353
+ int32_t i;
354
+ float f;
355
+ } ft_union;
356
+
357
+ /* float type data load instructions */
358
+ static __m128 __lsx_vreplfr2vr_s(float val) {
359
+ ft_union fi_tmpval = {.f = val};
360
+ return (__m128)__lsx_vreplgr2vr_w(fi_tmpval.i);
361
+ }
362
+
363
+ static __m256 __lasx_xvreplfr2vr_s(float val) {
364
+ ft_union fi_tmpval = {.f = val};
365
+ return (__m256)__lasx_xvreplgr2vr_w(fi_tmpval.i);
366
+ }
367
+ #endif
368
+
369
+ #ifdef __cplusplus
370
+ }
371
+ #endif