@novastera-oss/llamarn 0.0.1-alpha.4

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 (989) hide show
  1. package/INTERFACE.md +389 -0
  2. package/LICENSE +201 -0
  3. package/README.md +235 -0
  4. package/RNLlamaCpp.podspec +69 -0
  5. package/android/CMakeLists.txt +107 -0
  6. package/android/build.gradle +111 -0
  7. package/android/generated/java/com/novastera/llamarn/NativeRNLlamaCppSpec.java +47 -0
  8. package/android/generated/jni/CMakeLists.txt +36 -0
  9. package/android/generated/jni/RNLlamaCppSpec-generated.cpp +44 -0
  10. package/android/generated/jni/RNLlamaCppSpec.h +31 -0
  11. package/android/generated/jni/react/renderer/components/RNLlamaCppSpec/RNLlamaCppSpecJSI-generated.cpp +42 -0
  12. package/android/generated/jni/react/renderer/components/RNLlamaCppSpec/RNLlamaCppSpecJSI.h +336 -0
  13. package/android/gradle.properties +5 -0
  14. package/android/src/main/AndroidManifest.xml +3 -0
  15. package/android/src/main/AndroidManifestNew.xml +2 -0
  16. package/android/src/main/cpp/include/llama-cpp.h +30 -0
  17. package/android/src/main/cpp/include/llama.h +1440 -0
  18. package/android/src/main/java/com/novastera/llamarn/RNLlamaCppPackage.kt +21 -0
  19. package/android/src/main/jniLibs/arm64-v8a/libOpenCL.so +0 -0
  20. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  21. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  22. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  23. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  24. package/android/src/main/jniLibs/x86_64/libOpenCL.so +0 -0
  25. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  26. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  27. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  28. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  29. package/cpp/LlamaCppModel.cpp +984 -0
  30. package/cpp/LlamaCppModel.h +162 -0
  31. package/cpp/PureCppImpl.cpp +308 -0
  32. package/cpp/PureCppImpl.h +59 -0
  33. package/cpp/SystemUtils.cpp +180 -0
  34. package/cpp/SystemUtils.h +74 -0
  35. package/cpp/build-info.cpp +4 -0
  36. package/cpp/llama.cpp/AUTHORS +1106 -0
  37. package/cpp/llama.cpp/CMakeLists.txt +254 -0
  38. package/cpp/llama.cpp/CMakePresets.json +84 -0
  39. package/cpp/llama.cpp/CODEOWNERS +11 -0
  40. package/cpp/llama.cpp/CONTRIBUTING.md +127 -0
  41. package/cpp/llama.cpp/LICENSE +21 -0
  42. package/cpp/llama.cpp/Makefile +1608 -0
  43. package/cpp/llama.cpp/README.md +575 -0
  44. package/cpp/llama.cpp/SECURITY.md +68 -0
  45. package/cpp/llama.cpp/build-xcframework.sh +540 -0
  46. package/cpp/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
  47. package/cpp/llama.cpp/cmake/arm64-windows-llvm.cmake +16 -0
  48. package/cpp/llama.cpp/cmake/build-info.cmake +64 -0
  49. package/cpp/llama.cpp/cmake/common.cmake +35 -0
  50. package/cpp/llama.cpp/cmake/git-vars.cmake +22 -0
  51. package/cpp/llama.cpp/cmake/llama-config.cmake.in +30 -0
  52. package/cpp/llama.cpp/cmake/llama.pc.in +10 -0
  53. package/cpp/llama.cpp/cmake/x64-windows-llvm.cmake +5 -0
  54. package/cpp/llama.cpp/common/CMakeLists.txt +170 -0
  55. package/cpp/llama.cpp/common/arg.cpp +3337 -0
  56. package/cpp/llama.cpp/common/arg.h +89 -0
  57. package/cpp/llama.cpp/common/base64.hpp +392 -0
  58. package/cpp/llama.cpp/common/build-info.cpp.in +4 -0
  59. package/cpp/llama.cpp/common/chat.cpp +1781 -0
  60. package/cpp/llama.cpp/common/chat.h +135 -0
  61. package/cpp/llama.cpp/common/cmake/build-info-gen-cpp.cmake +24 -0
  62. package/cpp/llama.cpp/common/common.cpp +1567 -0
  63. package/cpp/llama.cpp/common/common.h +668 -0
  64. package/cpp/llama.cpp/common/console.cpp +504 -0
  65. package/cpp/llama.cpp/common/console.h +19 -0
  66. package/cpp/llama.cpp/common/json-schema-to-grammar.cpp +1027 -0
  67. package/cpp/llama.cpp/common/json-schema-to-grammar.h +21 -0
  68. package/cpp/llama.cpp/common/json.hpp +24766 -0
  69. package/cpp/llama.cpp/common/llguidance.cpp +254 -0
  70. package/cpp/llama.cpp/common/log.cpp +393 -0
  71. package/cpp/llama.cpp/common/log.h +103 -0
  72. package/cpp/llama.cpp/common/minja/chat-template.hpp +537 -0
  73. package/cpp/llama.cpp/common/minja/minja.hpp +2941 -0
  74. package/cpp/llama.cpp/common/ngram-cache.cpp +286 -0
  75. package/cpp/llama.cpp/common/ngram-cache.h +101 -0
  76. package/cpp/llama.cpp/common/sampling.cpp +580 -0
  77. package/cpp/llama.cpp/common/sampling.h +107 -0
  78. package/cpp/llama.cpp/common/speculative.cpp +278 -0
  79. package/cpp/llama.cpp/common/speculative.h +28 -0
  80. package/cpp/llama.cpp/common/stb_image.h +7988 -0
  81. package/cpp/llama.cpp/convert_hf_to_gguf.py +6195 -0
  82. package/cpp/llama.cpp/convert_hf_to_gguf_update.py +393 -0
  83. package/cpp/llama.cpp/convert_llama_ggml_to_gguf.py +450 -0
  84. package/cpp/llama.cpp/convert_lora_to_gguf.py +461 -0
  85. package/cpp/llama.cpp/flake.lock +58 -0
  86. package/cpp/llama.cpp/flake.nix +185 -0
  87. package/cpp/llama.cpp/ggml/CMakeLists.txt +388 -0
  88. package/cpp/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
  89. package/cpp/llama.cpp/ggml/cmake/common.cmake +26 -0
  90. package/cpp/llama.cpp/ggml/cmake/ggml-config.cmake.in +152 -0
  91. package/cpp/llama.cpp/ggml/include/ggml-alloc.h +76 -0
  92. package/cpp/llama.cpp/ggml/include/ggml-backend.h +354 -0
  93. package/cpp/llama.cpp/ggml/include/ggml-blas.h +25 -0
  94. package/cpp/llama.cpp/ggml/include/ggml-cann.h +123 -0
  95. package/cpp/llama.cpp/ggml/include/ggml-cpp.h +39 -0
  96. package/cpp/llama.cpp/ggml/include/ggml-cpu.h +143 -0
  97. package/cpp/llama.cpp/ggml/include/ggml-cuda.h +47 -0
  98. package/cpp/llama.cpp/ggml/include/ggml-kompute.h +50 -0
  99. package/cpp/llama.cpp/ggml/include/ggml-metal.h +66 -0
  100. package/cpp/llama.cpp/ggml/include/ggml-opencl.h +26 -0
  101. package/cpp/llama.cpp/ggml/include/ggml-opt.h +216 -0
  102. package/cpp/llama.cpp/ggml/include/ggml-rpc.h +33 -0
  103. package/cpp/llama.cpp/ggml/include/ggml-sycl.h +49 -0
  104. package/cpp/llama.cpp/ggml/include/ggml-vulkan.h +29 -0
  105. package/cpp/llama.cpp/ggml/include/ggml.h +2192 -0
  106. package/cpp/llama.cpp/ggml/include/gguf.h +202 -0
  107. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +345 -0
  108. package/cpp/llama.cpp/ggml/src/ggml-alloc.c +1042 -0
  109. package/cpp/llama.cpp/ggml/src/ggml-backend-impl.h +255 -0
  110. package/cpp/llama.cpp/ggml/src/ggml-backend-reg.cpp +586 -0
  111. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +2008 -0
  112. package/cpp/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +87 -0
  113. package/cpp/llama.cpp/ggml/src/ggml-blas/ggml-blas.cpp +517 -0
  114. package/cpp/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +74 -0
  115. package/cpp/llama.cpp/ggml/src/ggml-cann/Doxyfile +2579 -0
  116. package/cpp/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +179 -0
  117. package/cpp/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +258 -0
  118. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +2589 -0
  119. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +1083 -0
  120. package/cpp/llama.cpp/ggml/src/ggml-cann/common.h +420 -0
  121. package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +2554 -0
  122. package/cpp/llama.cpp/ggml/src/ggml-common.h +1857 -0
  123. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +495 -0
  124. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +221 -0
  125. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/amx.h +8 -0
  126. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/common.h +91 -0
  127. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +2511 -0
  128. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/mmq.h +10 -0
  129. package/cpp/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
  130. package/cpp/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
  131. package/cpp/llama.cpp/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
  132. package/cpp/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
  133. package/cpp/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +327 -0
  134. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +6431 -0
  135. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +8 -0
  136. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.cpp +55 -0
  137. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.h +8 -0
  138. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +512 -0
  139. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +13131 -0
  140. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  141. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.cpp +36 -0
  142. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.h +38 -0
  143. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +3492 -0
  144. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +671 -0
  145. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +254 -0
  146. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +60 -0
  147. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +287 -0
  148. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
  149. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +3544 -0
  150. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +14 -0
  151. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8796 -0
  152. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
  153. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
  154. package/cpp/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
  155. package/cpp/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
  156. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.cpp +252 -0
  157. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
  158. package/cpp/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +184 -0
  159. package/cpp/llama.cpp/ggml/src/ggml-cuda/acc.cu +47 -0
  160. package/cpp/llama.cpp/ggml/src/ggml-cuda/acc.cuh +5 -0
  161. package/cpp/llama.cpp/ggml/src/ggml-cuda/arange.cu +34 -0
  162. package/cpp/llama.cpp/ggml/src/ggml-cuda/arange.cuh +5 -0
  163. package/cpp/llama.cpp/ggml/src/ggml-cuda/argmax.cu +91 -0
  164. package/cpp/llama.cpp/ggml/src/ggml-cuda/argmax.cuh +3 -0
  165. package/cpp/llama.cpp/ggml/src/ggml-cuda/argsort.cu +104 -0
  166. package/cpp/llama.cpp/ggml/src/ggml-cuda/argsort.cuh +3 -0
  167. package/cpp/llama.cpp/ggml/src/ggml-cuda/binbcast.cu +363 -0
  168. package/cpp/llama.cpp/ggml/src/ggml-cuda/binbcast.cuh +9 -0
  169. package/cpp/llama.cpp/ggml/src/ggml-cuda/clamp.cu +45 -0
  170. package/cpp/llama.cpp/ggml/src/ggml-cuda/clamp.cuh +5 -0
  171. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +828 -0
  172. package/cpp/llama.cpp/ggml/src/ggml-cuda/concat.cu +221 -0
  173. package/cpp/llama.cpp/ggml/src/ggml-cuda/concat.cuh +5 -0
  174. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv-transpose-1d.cu +89 -0
  175. package/cpp/llama.cpp/ggml/src/ggml-cuda/conv-transpose-1d.cuh +5 -0
  176. package/cpp/llama.cpp/ggml/src/ggml-cuda/convert.cu +730 -0
  177. package/cpp/llama.cpp/ggml/src/ggml-cuda/convert.cuh +26 -0
  178. package/cpp/llama.cpp/ggml/src/ggml-cuda/count-equal.cu +64 -0
  179. package/cpp/llama.cpp/ggml/src/ggml-cuda/count-equal.cuh +5 -0
  180. package/cpp/llama.cpp/ggml/src/ggml-cuda/cp-async.cuh +57 -0
  181. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy.cu +695 -0
  182. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy.cuh +11 -0
  183. package/cpp/llama.cpp/ggml/src/ggml-cuda/cross-entropy-loss.cu +189 -0
  184. package/cpp/llama.cpp/ggml/src/ggml-cuda/cross-entropy-loss.cuh +7 -0
  185. package/cpp/llama.cpp/ggml/src/ggml-cuda/dequantize.cuh +103 -0
  186. package/cpp/llama.cpp/ggml/src/ggml-cuda/diagmask.cu +40 -0
  187. package/cpp/llama.cpp/ggml/src/ggml-cuda/diagmask.cuh +5 -0
  188. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-common.cuh +873 -0
  189. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +1269 -0
  190. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f16.cu +357 -0
  191. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f16.cuh +3 -0
  192. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f32.cu +365 -0
  193. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-tile-f32.cuh +3 -0
  194. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f16.cuh +437 -0
  195. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f32.cuh +428 -0
  196. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-wmma-f16.cu +634 -0
  197. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-wmma-f16.cuh +3 -0
  198. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn.cu +345 -0
  199. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn.cuh +3 -0
  200. package/cpp/llama.cpp/ggml/src/ggml-cuda/getrows.cu +275 -0
  201. package/cpp/llama.cpp/ggml/src/ggml-cuda/getrows.cuh +15 -0
  202. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +3501 -0
  203. package/cpp/llama.cpp/ggml/src/ggml-cuda/gla.cu +93 -0
  204. package/cpp/llama.cpp/ggml/src/ggml-cuda/gla.cuh +3 -0
  205. package/cpp/llama.cpp/ggml/src/ggml-cuda/im2col.cu +103 -0
  206. package/cpp/llama.cpp/ggml/src/ggml-cuda/im2col.cuh +5 -0
  207. package/cpp/llama.cpp/ggml/src/ggml-cuda/mma.cuh +396 -0
  208. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cu +322 -0
  209. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cuh +3217 -0
  210. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmv.cu +336 -0
  211. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmv.cuh +12 -0
  212. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmvq.cu +595 -0
  213. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmvq.cuh +12 -0
  214. package/cpp/llama.cpp/ggml/src/ggml-cuda/norm.cu +458 -0
  215. package/cpp/llama.cpp/ggml/src/ggml-cuda/norm.cuh +11 -0
  216. package/cpp/llama.cpp/ggml/src/ggml-cuda/opt-step-adamw.cu +78 -0
  217. package/cpp/llama.cpp/ggml/src/ggml-cuda/opt-step-adamw.cuh +5 -0
  218. package/cpp/llama.cpp/ggml/src/ggml-cuda/out-prod.cu +68 -0
  219. package/cpp/llama.cpp/ggml/src/ggml-cuda/out-prod.cuh +3 -0
  220. package/cpp/llama.cpp/ggml/src/ggml-cuda/pad.cu +49 -0
  221. package/cpp/llama.cpp/ggml/src/ggml-cuda/pad.cuh +5 -0
  222. package/cpp/llama.cpp/ggml/src/ggml-cuda/pool2d.cu +94 -0
  223. package/cpp/llama.cpp/ggml/src/ggml-cuda/pool2d.cuh +5 -0
  224. package/cpp/llama.cpp/ggml/src/ggml-cuda/quantize.cu +189 -0
  225. package/cpp/llama.cpp/ggml/src/ggml-cuda/quantize.cuh +27 -0
  226. package/cpp/llama.cpp/ggml/src/ggml-cuda/rope.cu +456 -0
  227. package/cpp/llama.cpp/ggml/src/ggml-cuda/rope.cuh +7 -0
  228. package/cpp/llama.cpp/ggml/src/ggml-cuda/scale.cu +31 -0
  229. package/cpp/llama.cpp/ggml/src/ggml-cuda/scale.cuh +5 -0
  230. package/cpp/llama.cpp/ggml/src/ggml-cuda/softmax.cu +283 -0
  231. package/cpp/llama.cpp/ggml/src/ggml-cuda/softmax.cuh +7 -0
  232. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-conv.cu +148 -0
  233. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-conv.cuh +3 -0
  234. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-scan.cu +153 -0
  235. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-scan.cuh +3 -0
  236. package/cpp/llama.cpp/ggml/src/ggml-cuda/sum.cu +45 -0
  237. package/cpp/llama.cpp/ggml/src/ggml-cuda/sum.cuh +5 -0
  238. package/cpp/llama.cpp/ggml/src/ggml-cuda/sumrows.cu +39 -0
  239. package/cpp/llama.cpp/ggml/src/ggml-cuda/sumrows.cuh +5 -0
  240. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +5 -0
  241. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +10 -0
  242. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu +10 -0
  243. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu +10 -0
  244. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +10 -0
  245. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +5 -0
  246. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +10 -0
  247. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +10 -0
  248. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_1.cu +10 -0
  249. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu +10 -0
  250. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +5 -0
  251. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu +10 -0
  252. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +10 -0
  253. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +10 -0
  254. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_64-ncols2_1.cu +10 -0
  255. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_1.cu +10 -0
  256. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu +10 -0
  257. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +10 -0
  258. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +10 -0
  259. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +5 -0
  260. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +5 -0
  261. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +5 -0
  262. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +5 -0
  263. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +5 -0
  264. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +5 -0
  265. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +5 -0
  266. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +5 -0
  267. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +5 -0
  268. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +5 -0
  269. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +5 -0
  270. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +5 -0
  271. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +5 -0
  272. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +5 -0
  273. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +5 -0
  274. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +5 -0
  275. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +5 -0
  276. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +5 -0
  277. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +5 -0
  278. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +5 -0
  279. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +5 -0
  280. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +5 -0
  281. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +5 -0
  282. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +5 -0
  283. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +5 -0
  284. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +5 -0
  285. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +5 -0
  286. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +5 -0
  287. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +5 -0
  288. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +5 -0
  289. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +5 -0
  290. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +5 -0
  291. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +5 -0
  292. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +5 -0
  293. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +5 -0
  294. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +5 -0
  295. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +5 -0
  296. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +5 -0
  297. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +5 -0
  298. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +5 -0
  299. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +5 -0
  300. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +5 -0
  301. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +5 -0
  302. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +5 -0
  303. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +5 -0
  304. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +5 -0
  305. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +5 -0
  306. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +5 -0
  307. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +5 -0
  308. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +5 -0
  309. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +5 -0
  310. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +5 -0
  311. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +5 -0
  312. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +5 -0
  313. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +5 -0
  314. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +5 -0
  315. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +5 -0
  316. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +5 -0
  317. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +5 -0
  318. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +5 -0
  319. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +5 -0
  320. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +5 -0
  321. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +5 -0
  322. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +5 -0
  323. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +5 -0
  324. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +5 -0
  325. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +5 -0
  326. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +5 -0
  327. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +5 -0
  328. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +5 -0
  329. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +5 -0
  330. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +5 -0
  331. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +5 -0
  332. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +5 -0
  333. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +5 -0
  334. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +5 -0
  335. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +5 -0
  336. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +5 -0
  337. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +5 -0
  338. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +5 -0
  339. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +5 -0
  340. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +5 -0
  341. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +5 -0
  342. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +5 -0
  343. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +5 -0
  344. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +5 -0
  345. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +78 -0
  346. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq1_s.cu +5 -0
  347. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_s.cu +5 -0
  348. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xs.cu +5 -0
  349. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xxs.cu +5 -0
  350. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_s.cu +5 -0
  351. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_xxs.cu +5 -0
  352. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_nl.cu +5 -0
  353. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_xs.cu +5 -0
  354. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q2_k.cu +5 -0
  355. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q3_k.cu +5 -0
  356. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_0.cu +5 -0
  357. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_1.cu +5 -0
  358. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_k.cu +5 -0
  359. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_0.cu +5 -0
  360. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_1.cu +5 -0
  361. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_k.cu +5 -0
  362. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q6_k.cu +5 -0
  363. package/cpp/llama.cpp/ggml/src/ggml-cuda/template-instances/mmq-instance-q8_0.cu +5 -0
  364. package/cpp/llama.cpp/ggml/src/ggml-cuda/tsembd.cu +47 -0
  365. package/cpp/llama.cpp/ggml/src/ggml-cuda/tsembd.cuh +5 -0
  366. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cu +279 -0
  367. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cuh +57 -0
  368. package/cpp/llama.cpp/ggml/src/ggml-cuda/upscale.cu +51 -0
  369. package/cpp/llama.cpp/ggml/src/ggml-cuda/upscale.cuh +5 -0
  370. package/cpp/llama.cpp/ggml/src/ggml-cuda/vecdotq.cuh +1135 -0
  371. package/cpp/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h +15 -0
  372. package/cpp/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +243 -0
  373. package/cpp/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +140 -0
  374. package/cpp/llama.cpp/ggml/src/ggml-cuda/wkv.cu +199 -0
  375. package/cpp/llama.cpp/ggml/src/ggml-cuda/wkv.cuh +7 -0
  376. package/cpp/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +131 -0
  377. package/cpp/llama.cpp/ggml/src/ggml-impl.h +601 -0
  378. package/cpp/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +166 -0
  379. package/cpp/llama.cpp/ggml/src/ggml-kompute/ggml-kompute.cpp +2251 -0
  380. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/common.comp +112 -0
  381. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +58 -0
  382. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +25 -0
  383. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +52 -0
  384. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +52 -0
  385. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +52 -0
  386. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +52 -0
  387. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +30 -0
  388. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +22 -0
  389. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +17 -0
  390. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +31 -0
  391. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +31 -0
  392. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +38 -0
  393. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +39 -0
  394. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +44 -0
  395. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +52 -0
  396. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +69 -0
  397. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +51 -0
  398. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +33 -0
  399. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +35 -0
  400. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +140 -0
  401. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +106 -0
  402. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +73 -0
  403. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +52 -0
  404. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +28 -0
  405. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +84 -0
  406. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +21 -0
  407. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +53 -0
  408. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +52 -0
  409. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +52 -0
  410. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +52 -0
  411. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +52 -0
  412. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +19 -0
  413. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +23 -0
  414. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +22 -0
  415. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +72 -0
  416. package/cpp/llama.cpp/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +71 -0
  417. package/cpp/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +120 -0
  418. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +618 -0
  419. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +5916 -0
  420. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +6891 -0
  421. package/cpp/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +107 -0
  422. package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +96 -0
  423. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +4966 -0
  424. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/add.cl +83 -0
  425. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/clamp.cl +20 -0
  426. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/cpy.cl +184 -0
  427. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/cvt.cl +118 -0
  428. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/diag_mask_inf.cl +58 -0
  429. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/embed_kernel.py +26 -0
  430. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/gelu.cl +62 -0
  431. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle.cl +268 -0
  432. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general.cl +274 -0
  433. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/get_rows.cl +163 -0
  434. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/im2col_f16.cl +57 -0
  435. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/im2col_f32.cl +57 -0
  436. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul.cl +79 -0
  437. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mat_Ab_Bi_8x4.cl +139 -0
  438. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f16.cl +118 -0
  439. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32.cl +118 -0
  440. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_1row.cl +94 -0
  441. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +84 -0
  442. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_f32_f32.cl +118 -0
  443. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32.cl +192 -0
  444. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_16x_flat.cl +307 -0
  445. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_8x_flat.cl +265 -0
  446. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_8x_flat.cl +272 -0
  447. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_v.cl +254 -0
  448. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_q6_k.cl +190 -0
  449. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/norm.cl +81 -0
  450. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/relu.cl +16 -0
  451. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/rms_norm.cl +96 -0
  452. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/rope.cl +721 -0
  453. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/scale.cl +16 -0
  454. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/silu.cl +30 -0
  455. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +87 -0
  456. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +87 -0
  457. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_f16.cl +86 -0
  458. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/softmax_f32.cl +86 -0
  459. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/transpose.cl +84 -0
  460. package/cpp/llama.cpp/ggml/src/ggml-opt.cpp +854 -0
  461. package/cpp/llama.cpp/ggml/src/ggml-quants.c +5232 -0
  462. package/cpp/llama.cpp/ggml/src/ggml-quants.h +100 -0
  463. package/cpp/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
  464. package/cpp/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +1813 -0
  465. package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +183 -0
  466. package/cpp/llama.cpp/ggml/src/ggml-sycl/backend.hpp +37 -0
  467. package/cpp/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +350 -0
  468. package/cpp/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
  469. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.cpp +83 -0
  470. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +493 -0
  471. package/cpp/llama.cpp/ggml/src/ggml-sycl/concat.cpp +197 -0
  472. package/cpp/llama.cpp/ggml/src/ggml-sycl/concat.hpp +20 -0
  473. package/cpp/llama.cpp/ggml/src/ggml-sycl/conv.cpp +100 -0
  474. package/cpp/llama.cpp/ggml/src/ggml-sycl/conv.hpp +20 -0
  475. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +596 -0
  476. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.hpp +34 -0
  477. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +701 -0
  478. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.hpp +11 -0
  479. package/cpp/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +753 -0
  480. package/cpp/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +1154 -0
  481. package/cpp/llama.cpp/ggml/src/ggml-sycl/dmmv.hpp +27 -0
  482. package/cpp/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +2957 -0
  483. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1559 -0
  484. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +75 -0
  485. package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +70 -0
  486. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +311 -0
  487. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +20 -0
  488. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +4302 -0
  489. package/cpp/llama.cpp/ggml/src/ggml-sycl/gla.cpp +105 -0
  490. package/cpp/llama.cpp/ggml/src/ggml-sycl/gla.hpp +8 -0
  491. package/cpp/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +136 -0
  492. package/cpp/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +21 -0
  493. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +3030 -0
  494. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmq.hpp +33 -0
  495. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +1081 -0
  496. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.hpp +27 -0
  497. package/cpp/llama.cpp/ggml/src/ggml-sycl/norm.cpp +474 -0
  498. package/cpp/llama.cpp/ggml/src/ggml-sycl/norm.hpp +26 -0
  499. package/cpp/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +46 -0
  500. package/cpp/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +10 -0
  501. package/cpp/llama.cpp/ggml/src/ggml-sycl/presets.hpp +74 -0
  502. package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +61 -0
  503. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +362 -0
  504. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.hpp +20 -0
  505. package/cpp/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +264 -0
  506. package/cpp/llama.cpp/ggml/src/ggml-sycl/softmax.hpp +20 -0
  507. package/cpp/llama.cpp/ggml/src/ggml-sycl/sycl_hw.cpp +13 -0
  508. package/cpp/llama.cpp/ggml/src/ggml-sycl/sycl_hw.hpp +23 -0
  509. package/cpp/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +73 -0
  510. package/cpp/llama.cpp/ggml/src/ggml-sycl/tsembd.hpp +20 -0
  511. package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +1189 -0
  512. package/cpp/llama.cpp/ggml/src/ggml-sycl/wkv.cpp +305 -0
  513. package/cpp/llama.cpp/ggml/src/ggml-sycl/wkv.hpp +10 -0
  514. package/cpp/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
  515. package/cpp/llama.cpp/ggml/src/ggml-threading.h +14 -0
  516. package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +202 -0
  517. package/cpp/llama.cpp/ggml/src/ggml-vulkan/cmake/host-toolchain.cmake.in +15 -0
  518. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +10502 -0
  519. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +22 -0
  520. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/acc.comp +29 -0
  521. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/add.comp +29 -0
  522. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp +51 -0
  523. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/argsort.comp +69 -0
  524. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/clamp.comp +17 -0
  525. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/concat.comp +41 -0
  526. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +49 -0
  527. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_dw.comp +105 -0
  528. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +23 -0
  529. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +51 -0
  530. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +242 -0
  531. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/cos.comp +17 -0
  532. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/count_equal.comp +31 -0
  533. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_f32.comp +20 -0
  534. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp +462 -0
  535. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.comp +699 -0
  536. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_head.comp +13 -0
  537. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_m.comp +42 -0
  538. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_s.comp +35 -0
  539. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp +44 -0
  540. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xs.comp +43 -0
  541. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp +48 -0
  542. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp +39 -0
  543. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp +49 -0
  544. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_nl.comp +32 -0
  545. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_xs.comp +34 -0
  546. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +34 -0
  547. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +42 -0
  548. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_0.comp +30 -0
  549. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_1.comp +32 -0
  550. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +68 -0
  551. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_0.comp +34 -0
  552. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_1.comp +35 -0
  553. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +70 -0
  554. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +33 -0
  555. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q8_0.comp +31 -0
  556. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/diag_mask_inf.comp +34 -0
  557. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/div.comp +27 -0
  558. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +483 -0
  559. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +383 -0
  560. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +59 -0
  561. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/gelu.comp +25 -0
  562. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/gelu_quick.comp +23 -0
  563. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +64 -0
  564. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/generic_head.comp +9 -0
  565. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/generic_unary_head.comp +76 -0
  566. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/get_rows.comp +33 -0
  567. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_quant.comp +41 -0
  568. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/group_norm.comp +66 -0
  569. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +100 -0
  570. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/l2_norm.comp +41 -0
  571. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/leaky_relu.comp +22 -0
  572. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul.comp +27 -0
  573. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_split_k_reduce.comp +48 -0
  574. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +169 -0
  575. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.comp +118 -0
  576. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_m.comp +82 -0
  577. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_s.comp +79 -0
  578. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_s.comp +90 -0
  579. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xs.comp +87 -0
  580. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xxs.comp +87 -0
  581. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_s.comp +90 -0
  582. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_xxs.comp +88 -0
  583. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_nc.comp +118 -0
  584. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_p021.comp +154 -0
  585. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +130 -0
  586. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q3_k.comp +132 -0
  587. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +136 -0
  588. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +167 -0
  589. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q6_k.comp +130 -0
  590. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +868 -0
  591. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +441 -0
  592. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +442 -0
  593. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.comp +99 -0
  594. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/norm.comp +44 -0
  595. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/opt_step_adamw.comp +42 -0
  596. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/pad.comp +28 -0
  597. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/pool2d.comp +74 -0
  598. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/quantize_q8_1.comp +77 -0
  599. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/relu.comp +21 -0
  600. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/repeat.comp +26 -0
  601. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/repeat_back.comp +37 -0
  602. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +52 -0
  603. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm_back.comp +55 -0
  604. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +58 -0
  605. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +60 -0
  606. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +43 -0
  607. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +43 -0
  608. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/rope_vision.comp +47 -0
  609. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +24 -0
  610. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/sigmoid.comp +20 -0
  611. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/silu.comp +22 -0
  612. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/silu_back.comp +26 -0
  613. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/sin.comp +17 -0
  614. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +173 -0
  615. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_back.comp +50 -0
  616. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/square.comp +17 -0
  617. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/sub.comp +29 -0
  618. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows.comp +37 -0
  619. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/tanh.comp +20 -0
  620. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/test_bfloat16_support.comp +7 -0
  621. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat2_support.comp +7 -0
  622. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat_support.comp +7 -0
  623. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/test_integer_dot_support.comp +7 -0
  624. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/timestep_embedding.comp +41 -0
  625. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +1373 -0
  626. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +36 -0
  627. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +740 -0
  628. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/wkv6.comp +87 -0
  629. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/wkv7.comp +91 -0
  630. package/cpp/llama.cpp/ggml/src/ggml.c +6499 -0
  631. package/cpp/llama.cpp/ggml/src/gguf.cpp +1330 -0
  632. package/cpp/llama.cpp/gguf-py/LICENSE +21 -0
  633. package/cpp/llama.cpp/gguf-py/README.md +99 -0
  634. package/cpp/llama.cpp/gguf-py/examples/reader.py +49 -0
  635. package/cpp/llama.cpp/gguf-py/examples/writer.py +39 -0
  636. package/cpp/llama.cpp/gguf-py/gguf/__init__.py +9 -0
  637. package/cpp/llama.cpp/gguf-py/gguf/constants.py +2296 -0
  638. package/cpp/llama.cpp/gguf-py/gguf/gguf.py +15 -0
  639. package/cpp/llama.cpp/gguf-py/gguf/gguf_reader.py +367 -0
  640. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +1041 -0
  641. package/cpp/llama.cpp/gguf-py/gguf/lazy.py +223 -0
  642. package/cpp/llama.cpp/gguf-py/gguf/metadata.py +642 -0
  643. package/cpp/llama.cpp/gguf-py/gguf/py.typed +0 -0
  644. package/cpp/llama.cpp/gguf-py/gguf/quants.py +1269 -0
  645. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_convert_endian.py +182 -0
  646. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_dump.py +454 -0
  647. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_editor_gui.py +1610 -0
  648. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_hash.py +102 -0
  649. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_new_metadata.py +207 -0
  650. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_set_metadata.py +95 -0
  651. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +1172 -0
  652. package/cpp/llama.cpp/gguf-py/gguf/utility.py +264 -0
  653. package/cpp/llama.cpp/gguf-py/gguf/vocab.py +492 -0
  654. package/cpp/llama.cpp/gguf-py/pyproject.toml +43 -0
  655. package/cpp/llama.cpp/gguf-py/tests/__init__.py +1 -0
  656. package/cpp/llama.cpp/gguf-py/tests/test_metadata.py +238 -0
  657. package/cpp/llama.cpp/gguf-py/tests/test_quants.py +238 -0
  658. package/cpp/llama.cpp/grammars/README.md +382 -0
  659. package/cpp/llama.cpp/grammars/arithmetic.gbnf +6 -0
  660. package/cpp/llama.cpp/grammars/c.gbnf +42 -0
  661. package/cpp/llama.cpp/grammars/chess.gbnf +13 -0
  662. package/cpp/llama.cpp/grammars/english.gbnf +6 -0
  663. package/cpp/llama.cpp/grammars/japanese.gbnf +7 -0
  664. package/cpp/llama.cpp/grammars/json.gbnf +25 -0
  665. package/cpp/llama.cpp/grammars/json_arr.gbnf +34 -0
  666. package/cpp/llama.cpp/grammars/list.gbnf +4 -0
  667. package/cpp/llama.cpp/include/llama-cpp.h +30 -0
  668. package/cpp/llama.cpp/include/llama.h +1440 -0
  669. package/cpp/llama.cpp/licenses/LICENSE-curl +9 -0
  670. package/cpp/llama.cpp/licenses/LICENSE-httplib +21 -0
  671. package/cpp/llama.cpp/licenses/LICENSE-jsonhpp +21 -0
  672. package/cpp/llama.cpp/licenses/LICENSE-linenoise +26 -0
  673. package/cpp/llama.cpp/media/llama0-banner.png +0 -0
  674. package/cpp/llama.cpp/media/llama0-logo.png +0 -0
  675. package/cpp/llama.cpp/media/llama1-banner.png +0 -0
  676. package/cpp/llama.cpp/media/llama1-logo.png +0 -0
  677. package/cpp/llama.cpp/media/llama1-logo.svg +34 -0
  678. package/cpp/llama.cpp/media/matmul.png +0 -0
  679. package/cpp/llama.cpp/media/matmul.svg +1238 -0
  680. package/cpp/llama.cpp/models/ggml-vocab-aquila.gguf +0 -0
  681. package/cpp/llama.cpp/models/ggml-vocab-baichuan.gguf +0 -0
  682. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf +0 -0
  683. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +112 -0
  684. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +46 -0
  685. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +112 -0
  686. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.out +46 -0
  687. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf +0 -0
  688. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.inp +112 -0
  689. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.out +46 -0
  690. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf +0 -0
  691. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +112 -0
  692. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +46 -0
  693. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf +0 -0
  694. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +112 -0
  695. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +46 -0
  696. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.inp +112 -0
  697. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.out +46 -0
  698. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf +0 -0
  699. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.inp +112 -0
  700. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.out +46 -0
  701. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf +0 -0
  702. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +112 -0
  703. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +46 -0
  704. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.inp +112 -0
  705. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.out +46 -0
  706. package/cpp/llama.cpp/models/ggml-vocab-gpt-neox.gguf +0 -0
  707. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf +0 -0
  708. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +112 -0
  709. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +46 -0
  710. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf +0 -0
  711. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +112 -0
  712. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +46 -0
  713. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
  714. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
  715. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf +0 -0
  716. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.inp +112 -0
  717. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.out +46 -0
  718. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf +0 -0
  719. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +112 -0
  720. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.out +46 -0
  721. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
  722. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
  723. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf +0 -0
  724. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +112 -0
  725. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.out +46 -0
  726. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf +0 -0
  727. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.inp +112 -0
  728. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.out +46 -0
  729. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +112 -0
  730. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +46 -0
  731. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf +0 -0
  732. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +112 -0
  733. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.out +46 -0
  734. package/cpp/llama.cpp/models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja +202 -0
  735. package/cpp/llama.cpp/models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja +156 -0
  736. package/cpp/llama.cpp/models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja +152 -0
  737. package/cpp/llama.cpp/models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja +152 -0
  738. package/cpp/llama.cpp/models/templates/Qwen-Qwen2.5-7B-Instruct.jinja +54 -0
  739. package/cpp/llama.cpp/models/templates/README.md +22 -0
  740. package/cpp/llama.cpp/models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja +1 -0
  741. package/cpp/llama.cpp/models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja +1 -0
  742. package/cpp/llama.cpp/models/templates/fireworks-ai-llama-3-firefunction-v2.jinja +57 -0
  743. package/cpp/llama.cpp/models/templates/google-gemma-2-2b-it.jinja +4 -0
  744. package/cpp/llama.cpp/models/templates/llama-cpp-deepseek-r1.jinja +76 -0
  745. package/cpp/llama.cpp/models/templates/meetkai-functionary-medium-v3.1.jinja +58 -0
  746. package/cpp/llama.cpp/models/templates/meetkai-functionary-medium-v3.2.jinja +287 -0
  747. package/cpp/llama.cpp/models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja +109 -0
  748. package/cpp/llama.cpp/models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja +93 -0
  749. package/cpp/llama.cpp/models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja +109 -0
  750. package/cpp/llama.cpp/models/templates/microsoft-Phi-3.5-mini-instruct.jinja +8 -0
  751. package/cpp/llama.cpp/models/templates/mistralai-Mistral-Nemo-Instruct-2407.jinja +87 -0
  752. package/cpp/llama.cpp/mypy.ini +7 -0
  753. package/cpp/llama.cpp/pocs/CMakeLists.txt +14 -0
  754. package/cpp/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  755. package/cpp/llama.cpp/pocs/vdot/q8dot.cpp +173 -0
  756. package/cpp/llama.cpp/pocs/vdot/vdot.cpp +311 -0
  757. package/cpp/llama.cpp/poetry.lock +1197 -0
  758. package/cpp/llama.cpp/prompts/LLM-questions.txt +49 -0
  759. package/cpp/llama.cpp/prompts/alpaca.txt +1 -0
  760. package/cpp/llama.cpp/prompts/assistant.txt +31 -0
  761. package/cpp/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  762. package/cpp/llama.cpp/prompts/chat-with-bob.txt +7 -0
  763. package/cpp/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  764. package/cpp/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  765. package/cpp/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  766. package/cpp/llama.cpp/prompts/chat.txt +28 -0
  767. package/cpp/llama.cpp/prompts/dan-modified.txt +1 -0
  768. package/cpp/llama.cpp/prompts/dan.txt +1 -0
  769. package/cpp/llama.cpp/prompts/mnemonics.txt +93 -0
  770. package/cpp/llama.cpp/prompts/parallel-questions.txt +43 -0
  771. package/cpp/llama.cpp/prompts/reason-act.txt +18 -0
  772. package/cpp/llama.cpp/pyproject.toml +45 -0
  773. package/cpp/llama.cpp/pyrightconfig.json +22 -0
  774. package/cpp/llama.cpp/requirements/requirements-all.txt +15 -0
  775. package/cpp/llama.cpp/requirements/requirements-compare-llama-bench.txt +2 -0
  776. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +3 -0
  777. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +3 -0
  778. package/cpp/llama.cpp/requirements/requirements-convert_legacy_llama.txt +5 -0
  779. package/cpp/llama.cpp/requirements/requirements-convert_llama_ggml_to_gguf.txt +1 -0
  780. package/cpp/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
  781. package/cpp/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
  782. package/cpp/llama.cpp/requirements/requirements-pydantic.txt +3 -0
  783. package/cpp/llama.cpp/requirements/requirements-test-tokenizer-random.txt +1 -0
  784. package/cpp/llama.cpp/requirements/requirements-tool_bench.txt +12 -0
  785. package/cpp/llama.cpp/requirements.txt +13 -0
  786. package/cpp/llama.cpp/src/CMakeLists.txt +45 -0
  787. package/cpp/llama.cpp/src/llama-adapter.cpp +388 -0
  788. package/cpp/llama.cpp/src/llama-adapter.h +76 -0
  789. package/cpp/llama.cpp/src/llama-arch.cpp +1743 -0
  790. package/cpp/llama.cpp/src/llama-arch.h +437 -0
  791. package/cpp/llama.cpp/src/llama-batch.cpp +372 -0
  792. package/cpp/llama.cpp/src/llama-batch.h +89 -0
  793. package/cpp/llama.cpp/src/llama-chat.cpp +663 -0
  794. package/cpp/llama.cpp/src/llama-chat.h +58 -0
  795. package/cpp/llama.cpp/src/llama-context.cpp +2459 -0
  796. package/cpp/llama.cpp/src/llama-context.h +246 -0
  797. package/cpp/llama.cpp/src/llama-cparams.cpp +1 -0
  798. package/cpp/llama.cpp/src/llama-cparams.h +39 -0
  799. package/cpp/llama.cpp/src/llama-grammar.cpp +1219 -0
  800. package/cpp/llama.cpp/src/llama-grammar.h +173 -0
  801. package/cpp/llama.cpp/src/llama-graph.cpp +1713 -0
  802. package/cpp/llama.cpp/src/llama-graph.h +595 -0
  803. package/cpp/llama.cpp/src/llama-hparams.cpp +79 -0
  804. package/cpp/llama.cpp/src/llama-hparams.h +161 -0
  805. package/cpp/llama.cpp/src/llama-impl.cpp +167 -0
  806. package/cpp/llama.cpp/src/llama-impl.h +61 -0
  807. package/cpp/llama.cpp/src/llama-io.cpp +15 -0
  808. package/cpp/llama.cpp/src/llama-io.h +35 -0
  809. package/cpp/llama.cpp/src/llama-kv-cache.cpp +2486 -0
  810. package/cpp/llama.cpp/src/llama-kv-cache.h +405 -0
  811. package/cpp/llama.cpp/src/llama-memory.cpp +1 -0
  812. package/cpp/llama.cpp/src/llama-memory.h +31 -0
  813. package/cpp/llama.cpp/src/llama-mmap.cpp +600 -0
  814. package/cpp/llama.cpp/src/llama-mmap.h +68 -0
  815. package/cpp/llama.cpp/src/llama-model-loader.cpp +1133 -0
  816. package/cpp/llama.cpp/src/llama-model-loader.h +169 -0
  817. package/cpp/llama.cpp/src/llama-model.cpp +13453 -0
  818. package/cpp/llama.cpp/src/llama-model.h +420 -0
  819. package/cpp/llama.cpp/src/llama-quant.cpp +964 -0
  820. package/cpp/llama.cpp/src/llama-quant.h +1 -0
  821. package/cpp/llama.cpp/src/llama-sampling.cpp +2575 -0
  822. package/cpp/llama.cpp/src/llama-sampling.h +32 -0
  823. package/cpp/llama.cpp/src/llama-vocab.cpp +3313 -0
  824. package/cpp/llama.cpp/src/llama-vocab.h +125 -0
  825. package/cpp/llama.cpp/src/llama.cpp +340 -0
  826. package/cpp/llama.cpp/src/unicode-data.cpp +7034 -0
  827. package/cpp/llama.cpp/src/unicode-data.h +20 -0
  828. package/cpp/llama.cpp/src/unicode.cpp +849 -0
  829. package/cpp/llama.cpp/src/unicode.h +66 -0
  830. package/cpp/rn-completion.cpp +431 -0
  831. package/cpp/rn-llama.hpp +60 -0
  832. package/cpp/rn-utils.hpp +331 -0
  833. package/ios/OnLoad.mm +22 -0
  834. package/ios/generated/RNLlamaCppSpec/RNLlamaCppSpec-generated.mm +64 -0
  835. package/ios/generated/RNLlamaCppSpec/RNLlamaCppSpec.h +251 -0
  836. package/ios/generated/RNLlamaCppSpecJSI-generated.cpp +42 -0
  837. package/ios/generated/RNLlamaCppSpecJSI.h +336 -0
  838. package/ios/include/chat.h +135 -0
  839. package/ios/include/common/base64.hpp +392 -0
  840. package/ios/include/common/json.hpp +24766 -0
  841. package/ios/include/common/minja/chat-template.hpp +537 -0
  842. package/ios/include/common/minja/minja.hpp +2941 -0
  843. package/ios/include/common.h +668 -0
  844. package/ios/include/json-schema-to-grammar.h +21 -0
  845. package/ios/include/llama-cpp.h +30 -0
  846. package/ios/include/llama.h +1440 -0
  847. package/ios/include/log.h +103 -0
  848. package/ios/include/ngram-cache.h +101 -0
  849. package/ios/include/sampling.h +107 -0
  850. package/ios/include/speculative.h +28 -0
  851. package/ios/libs/llama.xcframework/Info.plist +135 -0
  852. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  853. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  854. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4492 -0
  855. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-alloc.h +76 -0
  856. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-backend.h +354 -0
  857. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-blas.h +25 -0
  858. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-cpu.h +143 -0
  859. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-metal.h +66 -0
  860. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +2192 -0
  861. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/gguf.h +202 -0
  862. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +1440 -0
  863. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Info.plist +36 -0
  864. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Modules/module.modulemap +17 -0
  865. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  866. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  867. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  868. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4513 -0
  869. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3440 -0
  870. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-alloc.h +76 -0
  871. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +354 -0
  872. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-blas.h +25 -0
  873. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +143 -0
  874. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-metal.h +66 -0
  875. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +2192 -0
  876. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/gguf.h +202 -0
  877. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +1440 -0
  878. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Info.plist +36 -0
  879. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Modules/module.modulemap +17 -0
  880. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  881. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  882. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  883. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4513 -0
  884. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3442 -0
  885. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-alloc.h +76 -0
  886. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-backend.h +354 -0
  887. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-blas.h +25 -0
  888. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-cpu.h +143 -0
  889. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-metal.h +66 -0
  890. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +2192 -0
  891. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/gguf.h +202 -0
  892. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +1440 -0
  893. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Modules/module.modulemap +17 -0
  894. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Resources/Info.plist +32 -0
  895. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-alloc.h +76 -0
  896. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-backend.h +354 -0
  897. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-blas.h +25 -0
  898. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-cpu.h +143 -0
  899. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-metal.h +66 -0
  900. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +2192 -0
  901. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/gguf.h +202 -0
  902. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +1440 -0
  903. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Modules/module.modulemap +17 -0
  904. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Resources/Info.plist +32 -0
  905. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  906. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-alloc.h +76 -0
  907. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-backend.h +354 -0
  908. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-blas.h +25 -0
  909. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-cpu.h +143 -0
  910. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-metal.h +66 -0
  911. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +2192 -0
  912. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/gguf.h +202 -0
  913. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +1440 -0
  914. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Modules/module.modulemap +17 -0
  915. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Resources/Info.plist +32 -0
  916. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  917. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  918. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  919. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  920. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4492 -0
  921. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-alloc.h +76 -0
  922. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-backend.h +354 -0
  923. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-blas.h +25 -0
  924. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-cpu.h +143 -0
  925. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-metal.h +66 -0
  926. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +2192 -0
  927. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/gguf.h +202 -0
  928. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +1440 -0
  929. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Info.plist +35 -0
  930. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Modules/module.modulemap +17 -0
  931. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  932. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  933. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  934. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4513 -0
  935. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3440 -0
  936. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-alloc.h +76 -0
  937. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +354 -0
  938. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-blas.h +25 -0
  939. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +143 -0
  940. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-metal.h +66 -0
  941. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +2192 -0
  942. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/gguf.h +202 -0
  943. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +1440 -0
  944. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Info.plist +35 -0
  945. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Modules/module.modulemap +17 -0
  946. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  947. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  948. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  949. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4528 -0
  950. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-alloc.h +76 -0
  951. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-backend.h +354 -0
  952. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-blas.h +25 -0
  953. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-cpu.h +143 -0
  954. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-metal.h +66 -0
  955. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +2192 -0
  956. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/gguf.h +202 -0
  957. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +1440 -0
  958. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Info.plist +32 -0
  959. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Modules/module.modulemap +17 -0
  960. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  961. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Info.plist +20 -0
  962. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  963. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4549 -0
  964. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3470 -0
  965. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-alloc.h +76 -0
  966. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-backend.h +354 -0
  967. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-blas.h +25 -0
  968. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-cpu.h +143 -0
  969. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-metal.h +66 -0
  970. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +2192 -0
  971. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/gguf.h +202 -0
  972. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +1440 -0
  973. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Info.plist +32 -0
  974. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Modules/module.modulemap +17 -0
  975. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  976. package/lib/module/NativeRNLlamaCpp.js +35 -0
  977. package/lib/module/NativeRNLlamaCpp.js.map +1 -0
  978. package/lib/module/index.js +20 -0
  979. package/lib/module/index.js.map +1 -0
  980. package/lib/module/package.json +1 -0
  981. package/lib/typescript/package.json +1 -0
  982. package/lib/typescript/src/NativeRNLlamaCpp.d.ts +222 -0
  983. package/lib/typescript/src/NativeRNLlamaCpp.d.ts.map +1 -0
  984. package/lib/typescript/src/index.d.ts +5 -0
  985. package/lib/typescript/src/index.d.ts.map +1 -0
  986. package/package.json +161 -0
  987. package/react-native.config.js +15 -0
  988. package/src/NativeRNLlamaCpp.ts +282 -0
  989. package/src/index.tsx +54 -0
@@ -0,0 +1,3492 @@
1
+ #define _CRT_SECURE_NO_DEPRECATE // Disables "unsafe" warnings on Windows
2
+ #define _USE_MATH_DEFINES // For M_PI on MSVC
3
+
4
+ #include "ggml-backend-impl.h"
5
+ #include "ggml-backend.h"
6
+ #include "ggml-cpu-traits.h"
7
+ #include "ggml-cpu-impl.h"
8
+ #include "ggml-cpu.h"
9
+ #include "ggml-impl.h"
10
+ #include "ggml-cpu-quants.h"
11
+ #include "ggml-threading.h"
12
+ #include "unary-ops.h"
13
+ #include "binary-ops.h"
14
+ #include "vec.h"
15
+ #include "ops.h"
16
+ #include "ggml.h"
17
+
18
+ #if defined(_MSC_VER) || defined(__MINGW32__)
19
+ #include <malloc.h> // using malloc.h with MSC/MINGW
20
+ #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
21
+ #include <alloca.h>
22
+ #endif
23
+
24
+ #include <assert.h>
25
+ #include <errno.h>
26
+ #include <time.h>
27
+ #include <math.h>
28
+ #include <stdlib.h>
29
+ #include <string.h>
30
+ #include <stdint.h>
31
+ #include <inttypes.h>
32
+ #include <stdio.h>
33
+ #include <float.h>
34
+ #include <limits.h>
35
+ #include <stdarg.h>
36
+ #include <signal.h>
37
+ #if defined(__gnu_linux__)
38
+ #include <syscall.h>
39
+ #endif
40
+
41
+ #ifdef GGML_USE_OPENMP
42
+ #include <omp.h>
43
+ #endif
44
+
45
+ #if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_MATMUL_INT8)
46
+ #undef GGML_USE_LLAMAFILE
47
+ #endif
48
+
49
+ #ifdef GGML_USE_LLAMAFILE
50
+ #include "llamafile/sgemm.h"
51
+ #endif
52
+
53
+ // Note: once we move threading into a separate C++ file
54
+ // will use std::hardware_destructive_interference_size instead of hardcoding it here
55
+ // and we'll use C++ attribute syntax.
56
+ #define GGML_CACHE_LINE 64
57
+
58
+ #if defined(__clang__) || defined(__GNUC__)
59
+ #define GGML_CACHE_ALIGN __attribute__((aligned(GGML_CACHE_LINE)))
60
+ #endif
61
+
62
+ #if defined(__has_feature)
63
+ #if __has_feature(thread_sanitizer)
64
+ #define GGML_TSAN_ENABLED 1
65
+ #endif
66
+ #else // __has_feature
67
+ #if defined(__SANITIZE_THREAD__)
68
+ #define GGML_TSAN_ENABLED 1
69
+ #endif
70
+ #endif // __has_feature
71
+
72
+ #define UNUSED GGML_UNUSED
73
+ #define SWAP(x, y, T) do { T SWAP = x; (x) = y; (y) = SWAP; } while (0)
74
+
75
+ #if defined(__ARM_ARCH)
76
+ struct ggml_arm_arch_features_type {
77
+ int has_neon;
78
+ int has_dotprod;
79
+ int has_i8mm;
80
+ int has_sve;
81
+ int sve_cnt;
82
+ int has_sme;
83
+ } ggml_arm_arch_features = {-1, -1, -1, -1, 0, -1};
84
+ #endif
85
+
86
+
87
+ #if defined(_WIN32)
88
+
89
+ #define WIN32_LEAN_AND_MEAN
90
+ #ifndef NOMINMAX
91
+ #define NOMINMAX
92
+ #endif
93
+ #include <windows.h>
94
+
95
+ #if defined(_MSC_VER) && !defined(__clang__)
96
+ #define GGML_CACHE_ALIGN __declspec(align(GGML_CACHE_LINE))
97
+
98
+ typedef volatile LONG atomic_int;
99
+ typedef atomic_int atomic_bool;
100
+ typedef atomic_int atomic_flag;
101
+
102
+ #define ATOMIC_FLAG_INIT 0
103
+
104
+ typedef enum {
105
+ memory_order_relaxed,
106
+ memory_order_consume,
107
+ memory_order_acquire,
108
+ memory_order_release,
109
+ memory_order_acq_rel,
110
+ memory_order_seq_cst
111
+ } memory_order;
112
+
113
+ static void atomic_store(atomic_int * ptr, LONG val) {
114
+ InterlockedExchange(ptr, val);
115
+ }
116
+ static void atomic_store_explicit(atomic_int * ptr, LONG val, memory_order mo) {
117
+ // TODO: add support for explicit memory order
118
+ InterlockedExchange(ptr, val);
119
+ }
120
+ static LONG atomic_load(atomic_int * ptr) {
121
+ return InterlockedCompareExchange(ptr, 0, 0);
122
+ }
123
+ static LONG atomic_load_explicit(atomic_int * ptr, memory_order mo) {
124
+ // TODO: add support for explicit memory order
125
+ return InterlockedCompareExchange(ptr, 0, 0);
126
+ }
127
+ static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
128
+ return InterlockedExchangeAdd(ptr, inc);
129
+ }
130
+ static LONG atomic_fetch_add_explicit(atomic_int * ptr, LONG inc, memory_order mo) {
131
+ // TODO: add support for explicit memory order
132
+ return InterlockedExchangeAdd(ptr, inc);
133
+ }
134
+ static atomic_bool atomic_flag_test_and_set(atomic_flag * ptr) {
135
+ return InterlockedExchange(ptr, 1);
136
+ }
137
+ static void atomic_flag_clear(atomic_flag * ptr) {
138
+ InterlockedExchange(ptr, 0);
139
+ }
140
+ static void atomic_thread_fence(memory_order mo) {
141
+ MemoryBarrier();
142
+ }
143
+ #else // clang
144
+ #include <stdatomic.h>
145
+ #endif
146
+
147
+ typedef HANDLE pthread_t;
148
+
149
+ typedef DWORD thread_ret_t;
150
+ static int pthread_create(pthread_t * out, void * unused, thread_ret_t(*func)(void *), void * arg) {
151
+ (void) unused;
152
+ HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL);
153
+ if (handle == NULL)
154
+ {
155
+ return EAGAIN;
156
+ }
157
+
158
+ *out = handle;
159
+ return 0;
160
+ }
161
+
162
+ static int pthread_join(pthread_t thread, void * unused) {
163
+ (void) unused;
164
+ int ret = (int) WaitForSingleObject(thread, INFINITE);
165
+ CloseHandle(thread);
166
+ return ret;
167
+ }
168
+
169
+ static int sched_yield (void) {
170
+ Sleep (0);
171
+ return 0;
172
+ }
173
+ #else
174
+
175
+ #include <pthread.h>
176
+ #include <stdatomic.h>
177
+ #include <sched.h>
178
+ #if defined(__FreeBSD__)
179
+ #include <pthread_np.h>
180
+ #endif
181
+
182
+ typedef void * thread_ret_t;
183
+
184
+ #include <sys/types.h>
185
+ #include <sys/stat.h>
186
+ #include <unistd.h>
187
+
188
+ #endif
189
+
190
+ typedef pthread_t ggml_thread_t;
191
+
192
+ #if defined(__APPLE__)
193
+ #include <unistd.h>
194
+ #include <mach/mach.h>
195
+ #include <TargetConditionals.h>
196
+ #endif
197
+
198
+ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
199
+ [GGML_TYPE_F32] = {
200
+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
201
+ .vec_dot_type = GGML_TYPE_F32,
202
+ .nrows = 1,
203
+ },
204
+ [GGML_TYPE_F16] = {
205
+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_fp16,
206
+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16,
207
+ .vec_dot_type = GGML_TYPE_F16,
208
+ .nrows = 1,
209
+ },
210
+ [GGML_TYPE_Q4_0] = {
211
+ .from_float = quantize_row_q4_0,
212
+ .vec_dot = ggml_vec_dot_q4_0_q8_0,
213
+ .vec_dot_type = GGML_TYPE_Q8_0,
214
+ #if defined (__ARM_FEATURE_MATMUL_INT8)
215
+ .nrows = 2,
216
+ #else
217
+ .nrows = 1,
218
+ #endif
219
+ },
220
+ [GGML_TYPE_Q4_1] = {
221
+ .from_float = quantize_row_q4_1,
222
+ .vec_dot = ggml_vec_dot_q4_1_q8_1,
223
+ .vec_dot_type = GGML_TYPE_Q8_1,
224
+ #if defined (__ARM_FEATURE_MATMUL_INT8)
225
+ .nrows = 2,
226
+ #else
227
+ .nrows = 1,
228
+ #endif
229
+ },
230
+ [GGML_TYPE_Q5_0] = {
231
+ .from_float = quantize_row_q5_0,
232
+ .vec_dot = ggml_vec_dot_q5_0_q8_0,
233
+ .vec_dot_type = GGML_TYPE_Q8_0,
234
+ .nrows = 1,
235
+ },
236
+ [GGML_TYPE_Q5_1] = {
237
+ .from_float = quantize_row_q5_1,
238
+ .vec_dot = ggml_vec_dot_q5_1_q8_1,
239
+ .vec_dot_type = GGML_TYPE_Q8_1,
240
+ .nrows = 1,
241
+ },
242
+ [GGML_TYPE_Q8_0] = {
243
+ .from_float = quantize_row_q8_0,
244
+ .vec_dot = ggml_vec_dot_q8_0_q8_0,
245
+ .vec_dot_type = GGML_TYPE_Q8_0,
246
+ #if defined (__ARM_FEATURE_MATMUL_INT8)
247
+ .nrows = 2,
248
+ #else
249
+ .nrows = 1,
250
+ #endif
251
+ },
252
+ [GGML_TYPE_Q8_1] = {
253
+ .from_float = quantize_row_q8_1,
254
+ .vec_dot_type = GGML_TYPE_Q8_1,
255
+ .nrows = 1,
256
+ },
257
+ [GGML_TYPE_Q2_K] = {
258
+ .from_float = quantize_row_q2_K,
259
+ .vec_dot = ggml_vec_dot_q2_K_q8_K,
260
+ .vec_dot_type = GGML_TYPE_Q8_K,
261
+ .nrows = 1,
262
+ },
263
+ [GGML_TYPE_Q3_K] = {
264
+ .from_float = quantize_row_q3_K,
265
+ .vec_dot = ggml_vec_dot_q3_K_q8_K,
266
+ .vec_dot_type = GGML_TYPE_Q8_K,
267
+ .nrows = 1,
268
+ },
269
+ [GGML_TYPE_Q4_K] = {
270
+ .from_float = quantize_row_q4_K,
271
+ .vec_dot = ggml_vec_dot_q4_K_q8_K,
272
+ .vec_dot_type = GGML_TYPE_Q8_K,
273
+ .nrows = 1,
274
+ },
275
+ [GGML_TYPE_Q5_K] = {
276
+ .from_float = quantize_row_q5_K,
277
+ .vec_dot = ggml_vec_dot_q5_K_q8_K,
278
+ .vec_dot_type = GGML_TYPE_Q8_K,
279
+ .nrows = 1,
280
+ },
281
+ [GGML_TYPE_Q6_K] = {
282
+ .from_float = quantize_row_q6_K,
283
+ .vec_dot = ggml_vec_dot_q6_K_q8_K,
284
+ .vec_dot_type = GGML_TYPE_Q8_K,
285
+ .nrows = 1,
286
+ },
287
+ [GGML_TYPE_IQ2_XXS] = {
288
+ .from_float = NULL,
289
+ .vec_dot = ggml_vec_dot_iq2_xxs_q8_K,
290
+ .vec_dot_type = GGML_TYPE_Q8_K,
291
+ .nrows = 1,
292
+ },
293
+ [GGML_TYPE_IQ2_XS] = {
294
+ .from_float = NULL,
295
+ .vec_dot = ggml_vec_dot_iq2_xs_q8_K,
296
+ .vec_dot_type = GGML_TYPE_Q8_K,
297
+ .nrows = 1,
298
+ },
299
+ [GGML_TYPE_IQ3_XXS] = {
300
+ // NOTE: from_float for iq3 and iq2_s was removed because these quants require initialization in ggml_quantize_init
301
+ //.from_float = quantize_row_iq3_xxs,
302
+ .vec_dot = ggml_vec_dot_iq3_xxs_q8_K,
303
+ .vec_dot_type = GGML_TYPE_Q8_K,
304
+ .nrows = 1,
305
+ },
306
+ [GGML_TYPE_IQ3_S] = {
307
+ //.from_float = quantize_row_iq3_s,
308
+ .vec_dot = ggml_vec_dot_iq3_s_q8_K,
309
+ .vec_dot_type = GGML_TYPE_Q8_K,
310
+ .nrows = 1,
311
+ },
312
+ [GGML_TYPE_IQ2_S] = {
313
+ //.from_float = quantize_row_iq2_s,
314
+ .vec_dot = ggml_vec_dot_iq2_s_q8_K,
315
+ .vec_dot_type = GGML_TYPE_Q8_K,
316
+ .nrows = 1,
317
+ },
318
+ [GGML_TYPE_IQ1_S] = {
319
+ .from_float = NULL,
320
+ .vec_dot = ggml_vec_dot_iq1_s_q8_K,
321
+ .vec_dot_type = GGML_TYPE_Q8_K,
322
+ .nrows = 1,
323
+ },
324
+ [GGML_TYPE_IQ1_M] = {
325
+ .from_float = NULL,
326
+ .vec_dot = ggml_vec_dot_iq1_m_q8_K,
327
+ .vec_dot_type = GGML_TYPE_Q8_K,
328
+ .nrows = 1,
329
+ },
330
+ [GGML_TYPE_IQ4_NL] = {
331
+ .from_float = quantize_row_iq4_nl,
332
+ .vec_dot = ggml_vec_dot_iq4_nl_q8_0,
333
+ .vec_dot_type = GGML_TYPE_Q8_0,
334
+ .nrows = 1,
335
+ },
336
+ [GGML_TYPE_IQ4_XS] = {
337
+ .from_float = quantize_row_iq4_xs,
338
+ .vec_dot = ggml_vec_dot_iq4_xs_q8_K,
339
+ .vec_dot_type = GGML_TYPE_Q8_K,
340
+ .nrows = 1,
341
+ },
342
+ [GGML_TYPE_Q8_K] = {
343
+ .from_float = quantize_row_q8_K,
344
+ },
345
+ [GGML_TYPE_BF16] = {
346
+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_bf16,
347
+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,
348
+ .vec_dot_type = GGML_TYPE_BF16,
349
+ .nrows = 1,
350
+ },
351
+ [GGML_TYPE_TQ1_0] = {
352
+ .from_float = quantize_row_tq1_0,
353
+ .vec_dot = ggml_vec_dot_tq1_0_q8_K,
354
+ .vec_dot_type = GGML_TYPE_Q8_K,
355
+ .nrows = 1,
356
+ },
357
+ [GGML_TYPE_TQ2_0] = {
358
+ .from_float = quantize_row_tq2_0,
359
+ .vec_dot = ggml_vec_dot_tq2_0_q8_K,
360
+ .vec_dot_type = GGML_TYPE_Q8_K,
361
+ .nrows = 1,
362
+ },
363
+ };
364
+
365
+ const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) {
366
+ return &type_traits_cpu[type];
367
+ }
368
+
369
+ //
370
+ // Threading defs
371
+ //
372
+
373
+ typedef pthread_t ggml_thread_t;
374
+
375
+ #if defined(_WIN32)
376
+
377
+ typedef CONDITION_VARIABLE ggml_cond_t;
378
+ typedef SRWLOCK ggml_mutex_t;
379
+
380
+ #define ggml_mutex_init(m) InitializeSRWLock(m)
381
+ #define ggml_mutex_destroy(m)
382
+ #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m)
383
+ #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m)
384
+ #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m)
385
+ #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m)
386
+
387
+ #define ggml_cond_init(c) InitializeConditionVariable(c)
388
+ #define ggml_cond_destroy(c)
389
+ #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)
390
+ #define ggml_cond_broadcast(c) WakeAllConditionVariable(c)
391
+
392
+ #define ggml_thread_create pthread_create
393
+ #define ggml_thread_join pthread_join
394
+
395
+ #else
396
+
397
+ typedef pthread_cond_t ggml_cond_t;
398
+ typedef pthread_mutex_t ggml_mutex_t;
399
+
400
+ #define ggml_mutex_init(m) pthread_mutex_init(m, NULL)
401
+ #define ggml_mutex_destroy(m) pthread_mutex_destroy(m)
402
+ #define ggml_mutex_lock(m) pthread_mutex_lock(m)
403
+ #define ggml_mutex_unlock(m) pthread_mutex_unlock(m)
404
+ #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m)
405
+ #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m)
406
+
407
+ #define ggml_lock_init(x) UNUSED(x)
408
+ #define ggml_lock_destroy(x) UNUSED(x)
409
+ #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))
410
+ #define ggml_lock_lock(x) _mm_pause()
411
+ #else
412
+ #define ggml_lock_lock(x) UNUSED(x)
413
+ #endif
414
+ #define ggml_lock_unlock(x) UNUSED(x)
415
+
416
+ #define GGML_LOCK_INITIALIZER 0
417
+ #define ggml_cond_init(c) pthread_cond_init(c, NULL)
418
+ #define ggml_cond_destroy(c) pthread_cond_destroy(c)
419
+ #define ggml_cond_wait(c, m) pthread_cond_wait(c, m)
420
+ #define ggml_cond_broadcast(c) pthread_cond_broadcast(c)
421
+
422
+ #define ggml_thread_create pthread_create
423
+ #define ggml_thread_join pthread_join
424
+
425
+ #endif
426
+
427
+ // Threadpool def
428
+ struct ggml_threadpool {
429
+ ggml_mutex_t mutex; // mutex for cond.var
430
+ ggml_cond_t cond; // cond.var for waiting for new work
431
+
432
+ struct ggml_cgraph * cgraph;
433
+ struct ggml_cplan * cplan;
434
+
435
+ // synchronization primitives
436
+ atomic_int n_graph; // incremented when there is work to be done (i.e each graph)
437
+ atomic_int GGML_CACHE_ALIGN n_barrier;
438
+ atomic_int GGML_CACHE_ALIGN n_barrier_passed;
439
+ atomic_int GGML_CACHE_ALIGN current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads.
440
+
441
+ // these are atomic as an annotation for thread-sanitizer
442
+ atomic_bool stop; // Used for stopping the threadpool altogether
443
+ atomic_bool pause; // Used for pausing the threadpool or individual threads
444
+ atomic_int abort; // Used for aborting processing of a graph
445
+
446
+ struct ggml_compute_state * workers; // per thread state
447
+ int n_threads_max; // number of threads in the pool
448
+ atomic_int n_threads_cur; // number of threads used in the current graph
449
+
450
+ int32_t prio; // Scheduling priority
451
+ uint32_t poll; // Polling level (0 - no polling)
452
+
453
+ enum ggml_status ec;
454
+ };
455
+
456
+ // Per-thread state
457
+ struct ggml_compute_state {
458
+ #ifndef GGML_USE_OPENMP
459
+ ggml_thread_t thrd;
460
+ bool cpumask[GGML_MAX_N_THREADS];
461
+ int last_graph;
462
+ bool pending;
463
+ #endif
464
+ struct ggml_threadpool * threadpool;
465
+ int ith;
466
+ };
467
+
468
+ // Helpers for polling loops
469
+ #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) )
470
+ static inline void ggml_thread_cpu_relax(void) {
471
+ __asm__ volatile("yield" ::: "memory");
472
+ }
473
+ #elif defined(__x86_64__)
474
+ static inline void ggml_thread_cpu_relax(void) {
475
+ _mm_pause();
476
+ }
477
+ #else
478
+ static inline void ggml_thread_cpu_relax(void) {;}
479
+ #endif
480
+
481
+ //
482
+ // NUMA support
483
+ //
484
+
485
+ #define GGML_NUMA_MAX_NODES 8
486
+ #define GGML_NUMA_MAX_CPUS 512
487
+
488
+ struct ggml_numa_node {
489
+ uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node
490
+ uint32_t n_cpus;
491
+ };
492
+
493
+ struct ggml_numa_nodes {
494
+ enum ggml_numa_strategy numa_strategy;
495
+ struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES];
496
+ uint32_t n_nodes;
497
+ uint32_t total_cpus; // hardware threads on system
498
+ uint32_t current_node; // node on which main process is execting
499
+ #if defined(__gnu_linux__)
500
+ cpu_set_t cpuset; // cpuset from numactl
501
+ #else
502
+ uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype
503
+ #endif
504
+ };
505
+
506
+ //
507
+ // ggml state
508
+ //
509
+
510
+ struct ggml_state {
511
+ struct ggml_numa_nodes numa;
512
+ };
513
+
514
+ static struct ggml_state g_state = {0};
515
+
516
+ void ggml_barrier(struct ggml_threadpool * tp) {
517
+ int n_threads = atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed);
518
+ if (n_threads == 1) {
519
+ return;
520
+ }
521
+
522
+ #ifdef GGML_USE_OPENMP
523
+ #pragma omp barrier
524
+ #else
525
+ int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed);
526
+
527
+ // enter barrier (full seq-cst fence)
528
+ int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst);
529
+
530
+ if (n_barrier == (n_threads - 1)) {
531
+ // last thread
532
+ atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
533
+
534
+ // exit barrier (fill seq-cst fence)
535
+ atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
536
+ return;
537
+ }
538
+
539
+ // wait for other threads
540
+ while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
541
+ ggml_thread_cpu_relax();
542
+ }
543
+
544
+ // exit barrier (full seq-cst fence)
545
+ // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
546
+ #ifdef GGML_TSAN_ENABLED
547
+ atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
548
+ #else
549
+ atomic_thread_fence(memory_order_seq_cst);
550
+ #endif
551
+ #endif
552
+ }
553
+
554
+ #if defined(__gnu_linux__)
555
+ static cpu_set_t ggml_get_numa_affinity(void) {
556
+ cpu_set_t cpuset;
557
+ pthread_t thread;
558
+ thread = pthread_self();
559
+ CPU_ZERO(&cpuset);
560
+ pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
561
+ return cpuset;
562
+ }
563
+ #else
564
+ static uint32_t ggml_get_numa_affinity(void) {
565
+ return 0; // no NUMA support
566
+ }
567
+ #endif
568
+
569
+ void ggml_numa_init(enum ggml_numa_strategy numa_flag) {
570
+ if (g_state.numa.n_nodes > 0) {
571
+ fprintf(stderr, "ggml_numa_init: NUMA already initialized\n");
572
+
573
+ return;
574
+ }
575
+
576
+ #if defined(__gnu_linux__)
577
+ struct stat st;
578
+ char path[256];
579
+ int rv;
580
+
581
+ // set numa scheme
582
+ g_state.numa.numa_strategy = numa_flag;
583
+
584
+ GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy);
585
+
586
+ g_state.numa.cpuset = ggml_get_numa_affinity();
587
+
588
+ // enumerate nodes
589
+ while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) {
590
+ rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes);
591
+ GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
592
+ if (stat(path, &st) != 0) { break; }
593
+ ++g_state.numa.n_nodes;
594
+ }
595
+
596
+ // enumerate CPUs
597
+ while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) {
598
+ rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus);
599
+ GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
600
+ if (stat(path, &st) != 0) { break; }
601
+ ++g_state.numa.total_cpus;
602
+ }
603
+
604
+ GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus);
605
+
606
+ // figure out which node we're on
607
+ uint current_cpu;
608
+ int getcpu_ret = 0;
609
+ #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__)
610
+ getcpu_ret = getcpu(&current_cpu, &g_state.numa.current_node);
611
+ #else
612
+ // old glibc doesn't have a wrapper for this call. Fall back on direct syscall
613
+ # if !defined(SYS_getcpu) && defined(SYS_get_cpu)
614
+ # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name
615
+ # endif
616
+ getcpu_ret = syscall(SYS_getcpu, &current_cpu, &g_state.numa.current_node);
617
+ #endif
618
+
619
+ if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) {
620
+ g_state.numa.n_nodes = 0;
621
+ return;
622
+ }
623
+
624
+ GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu);
625
+
626
+ for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) {
627
+ struct ggml_numa_node * node = &g_state.numa.nodes[n];
628
+ GGML_PRINT_DEBUG("CPUs on node %u:", n);
629
+ node->n_cpus = 0;
630
+ for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) {
631
+ rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c);
632
+ GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
633
+ if (stat(path, &st) == 0) {
634
+ node->cpus[node->n_cpus++] = c;
635
+ GGML_PRINT_DEBUG(" %u", c);
636
+ }
637
+ }
638
+ GGML_PRINT_DEBUG("\n");
639
+ }
640
+
641
+ if (ggml_is_numa()) {
642
+ FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r");
643
+ if (fptr != NULL) {
644
+ char buf[42];
645
+ if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) {
646
+ GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n");
647
+ }
648
+ fclose(fptr);
649
+ }
650
+ }
651
+ #else
652
+ UNUSED(numa_flag);
653
+ // TODO
654
+ #endif
655
+ }
656
+
657
+ bool ggml_is_numa(void) {
658
+ return g_state.numa.n_nodes > 1;
659
+ }
660
+
661
+ #if defined(__ARM_ARCH)
662
+
663
+ #if defined(__linux__) && defined(__aarch64__)
664
+ #include <sys/auxv.h>
665
+ #elif defined(__APPLE__)
666
+ #include <sys/sysctl.h>
667
+ #endif
668
+
669
+ #if !defined(HWCAP2_I8MM)
670
+ #define HWCAP2_I8MM (1 << 13)
671
+ #endif
672
+
673
+ #if !defined(HWCAP2_SME)
674
+ #define HWCAP2_SME (1 << 23)
675
+ #endif
676
+
677
+ static void ggml_init_arm_arch_features(void) {
678
+ #if defined(__linux__) && defined(__aarch64__)
679
+ uint32_t hwcap = getauxval(AT_HWCAP);
680
+ uint32_t hwcap2 = getauxval(AT_HWCAP2);
681
+
682
+ ggml_arm_arch_features.has_neon = !!(hwcap & HWCAP_ASIMD);
683
+ ggml_arm_arch_features.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
684
+ ggml_arm_arch_features.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
685
+ ggml_arm_arch_features.has_sve = !!(hwcap & HWCAP_SVE);
686
+ ggml_arm_arch_features.has_sme = !!(hwcap2 & HWCAP2_SME);
687
+
688
+ #if defined(__ARM_FEATURE_SVE)
689
+ ggml_arm_arch_features.sve_cnt = PR_SVE_VL_LEN_MASK & prctl(PR_SVE_GET_VL);
690
+ #endif
691
+ #elif defined(__APPLE__)
692
+ int oldp = 0;
693
+ size_t size = sizeof(oldp);
694
+ if (sysctlbyname("hw.optional.AdvSIMD", &oldp, &size, NULL, 0) != 0) {
695
+ oldp = 0;
696
+ }
697
+ ggml_arm_arch_features.has_neon = oldp;
698
+
699
+ if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, NULL, 0) != 0) {
700
+ oldp = 0;
701
+ }
702
+ ggml_arm_arch_features.has_dotprod = oldp;
703
+
704
+ if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, NULL, 0) != 0) {
705
+ oldp = 0;
706
+ }
707
+ ggml_arm_arch_features.has_i8mm = oldp;
708
+
709
+ if (sysctlbyname("hw.optional.arm.FEAT_SME", &oldp, &size, NULL, 0) != 0) {
710
+ oldp = 0;
711
+ }
712
+ ggml_arm_arch_features.has_sme = oldp;
713
+
714
+ ggml_arm_arch_features.has_sve = 0;
715
+ ggml_arm_arch_features.sve_cnt = 0;
716
+ #else
717
+ // Run-time CPU feature detection not implemented for this platform, fallback to compile time
718
+ #if defined(__ARM_NEON)
719
+ ggml_arm_arch_features.has_neon = 1;
720
+ #else
721
+ ggml_arm_arch_features.has_neon = 0;
722
+ #endif
723
+
724
+ #if defined(__ARM_FEATURE_MATMUL_INT8)
725
+ ggml_arm_arch_features.has_i8mm = 1;
726
+ #else
727
+ ggml_arm_arch_features.has_i8mm = 0;
728
+ #endif
729
+
730
+ #if defined(__ARM_FEATURE_SVE)
731
+ ggml_arm_arch_features.has_sve = 1;
732
+ ggml_arm_arch_features.sve_cnt = 16;
733
+ #else
734
+ ggml_arm_arch_features.has_sve = 0;
735
+ ggml_arm_arch_features.sve_cnt = 0;
736
+ #endif
737
+
738
+ #if defined(__ARM_FEATURE_SME) || defined(__ARM_FEATURE_SME2)
739
+ ggml_arm_arch_features.has_sme = 1;
740
+ #else
741
+ ggml_arm_arch_features.has_sme = 0;
742
+ #endif
743
+ #endif
744
+ }
745
+ #endif
746
+
747
+ struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) {
748
+ GGML_ASSERT(!ggml_get_no_alloc(ctx));
749
+
750
+ struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
751
+
752
+ ggml_set_i32(result, value);
753
+
754
+ return result;
755
+ }
756
+
757
+ struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) {
758
+ GGML_ASSERT(!ggml_get_no_alloc(ctx));
759
+
760
+ struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
761
+
762
+ ggml_set_f32(result, value);
763
+
764
+ return result;
765
+ }
766
+
767
+ struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) {
768
+ const int n = ggml_nrows(tensor);
769
+ const int nc = tensor->ne[0];
770
+ const size_t n1 = tensor->nb[1];
771
+
772
+ char * const data = tensor->data;
773
+
774
+ switch (tensor->type) {
775
+ case GGML_TYPE_I8:
776
+ {
777
+ assert(tensor->nb[0] == sizeof(int8_t));
778
+ for (int i = 0; i < n; i++) {
779
+ ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
780
+ }
781
+ } break;
782
+ case GGML_TYPE_I16:
783
+ {
784
+ assert(tensor->nb[0] == sizeof(int16_t));
785
+ for (int i = 0; i < n; i++) {
786
+ ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
787
+ }
788
+ } break;
789
+ case GGML_TYPE_I32:
790
+ {
791
+ assert(tensor->nb[0] == sizeof(int32_t));
792
+ for (int i = 0; i < n; i++) {
793
+ ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
794
+ }
795
+ } break;
796
+ case GGML_TYPE_F16:
797
+ {
798
+ assert(tensor->nb[0] == sizeof(ggml_fp16_t));
799
+ for (int i = 0; i < n; i++) {
800
+ ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
801
+ }
802
+ } break;
803
+ case GGML_TYPE_BF16:
804
+ {
805
+ assert(tensor->nb[0] == sizeof(ggml_fp16_t));
806
+ for (int i = 0; i < n; i++) {
807
+ ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
808
+ }
809
+ } break;
810
+ case GGML_TYPE_F32:
811
+ {
812
+ assert(tensor->nb[0] == sizeof(float));
813
+ for (int i = 0; i < n; i++) {
814
+ ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
815
+ }
816
+ } break;
817
+ default:
818
+ {
819
+ GGML_ABORT("fatal error");
820
+ }
821
+ }
822
+
823
+ return tensor;
824
+ }
825
+
826
+ struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) {
827
+ const int n = ggml_nrows(tensor);
828
+ const int nc = tensor->ne[0];
829
+ const size_t n1 = tensor->nb[1];
830
+
831
+ char * const data = tensor->data;
832
+
833
+ switch (tensor->type) {
834
+ case GGML_TYPE_I8:
835
+ {
836
+ assert(tensor->nb[0] == sizeof(int8_t));
837
+ for (int i = 0; i < n; i++) {
838
+ ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
839
+ }
840
+ } break;
841
+ case GGML_TYPE_I16:
842
+ {
843
+ assert(tensor->nb[0] == sizeof(int16_t));
844
+ for (int i = 0; i < n; i++) {
845
+ ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
846
+ }
847
+ } break;
848
+ case GGML_TYPE_I32:
849
+ {
850
+ assert(tensor->nb[0] == sizeof(int32_t));
851
+ for (int i = 0; i < n; i++) {
852
+ ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
853
+ }
854
+ } break;
855
+ case GGML_TYPE_F16:
856
+ {
857
+ assert(tensor->nb[0] == sizeof(ggml_fp16_t));
858
+ for (int i = 0; i < n; i++) {
859
+ ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
860
+ }
861
+ } break;
862
+ case GGML_TYPE_BF16:
863
+ {
864
+ assert(tensor->nb[0] == sizeof(ggml_bf16_t));
865
+ for (int i = 0; i < n; i++) {
866
+ ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
867
+ }
868
+ } break;
869
+ case GGML_TYPE_F32:
870
+ {
871
+ assert(tensor->nb[0] == sizeof(float));
872
+ for (int i = 0; i < n; i++) {
873
+ ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
874
+ }
875
+ } break;
876
+ default:
877
+ {
878
+ GGML_ABORT("fatal error");
879
+ }
880
+ }
881
+
882
+ return tensor;
883
+ }
884
+
885
+ int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) {
886
+ if (!ggml_is_contiguous(tensor)) {
887
+ int64_t id[4] = { 0, 0, 0, 0 };
888
+ ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
889
+ return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]);
890
+ }
891
+ switch (tensor->type) {
892
+ case GGML_TYPE_I8:
893
+ {
894
+ GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
895
+ return ((int8_t *)(tensor->data))[i];
896
+ }
897
+ case GGML_TYPE_I16:
898
+ {
899
+ GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
900
+ return ((int16_t *)(tensor->data))[i];
901
+ }
902
+ case GGML_TYPE_I32:
903
+ {
904
+ GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
905
+ return ((int32_t *)(tensor->data))[i];
906
+ }
907
+ case GGML_TYPE_F16:
908
+ {
909
+ GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
910
+ return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
911
+ }
912
+ case GGML_TYPE_BF16:
913
+ {
914
+ GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
915
+ return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
916
+ }
917
+ case GGML_TYPE_F32:
918
+ {
919
+ GGML_ASSERT(tensor->nb[0] == sizeof(float));
920
+ return ((float *)(tensor->data))[i];
921
+ }
922
+ default:
923
+ {
924
+ GGML_ABORT("fatal error");
925
+ }
926
+ }
927
+ }
928
+
929
+ void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) {
930
+ if (!ggml_is_contiguous(tensor)) {
931
+ int64_t id[4] = { 0, 0, 0, 0 };
932
+ ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
933
+ ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value);
934
+ return;
935
+ }
936
+ switch (tensor->type) {
937
+ case GGML_TYPE_I8:
938
+ {
939
+ GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
940
+ ((int8_t *)(tensor->data))[i] = value;
941
+ } break;
942
+ case GGML_TYPE_I16:
943
+ {
944
+ GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
945
+ ((int16_t *)(tensor->data))[i] = value;
946
+ } break;
947
+ case GGML_TYPE_I32:
948
+ {
949
+ GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
950
+ ((int32_t *)(tensor->data))[i] = value;
951
+ } break;
952
+ case GGML_TYPE_F16:
953
+ {
954
+ GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
955
+ ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
956
+ } break;
957
+ case GGML_TYPE_BF16:
958
+ {
959
+ GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
960
+ ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
961
+ } break;
962
+ case GGML_TYPE_F32:
963
+ {
964
+ GGML_ASSERT(tensor->nb[0] == sizeof(float));
965
+ ((float *)(tensor->data))[i] = value;
966
+ } break;
967
+ default:
968
+ {
969
+ GGML_ABORT("fatal error");
970
+ }
971
+ }
972
+ }
973
+
974
+ int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
975
+ void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
976
+ switch (tensor->type) {
977
+ case GGML_TYPE_I8:
978
+ return ((int8_t *) data)[0];
979
+ case GGML_TYPE_I16:
980
+ return ((int16_t *) data)[0];
981
+ case GGML_TYPE_I32:
982
+ return ((int32_t *) data)[0];
983
+ case GGML_TYPE_F16:
984
+ return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
985
+ case GGML_TYPE_BF16:
986
+ return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
987
+ case GGML_TYPE_F32:
988
+ return ((float *) data)[0];
989
+ default:
990
+ GGML_ABORT("fatal error");
991
+ }
992
+ }
993
+
994
+ void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) {
995
+ void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
996
+ switch (tensor->type) {
997
+ case GGML_TYPE_I8:
998
+ {
999
+ ((int8_t *)(data))[0] = value;
1000
+ } break;
1001
+ case GGML_TYPE_I16:
1002
+ {
1003
+ ((int16_t *)(data))[0] = value;
1004
+ } break;
1005
+ case GGML_TYPE_I32:
1006
+ {
1007
+ ((int32_t *)(data))[0] = value;
1008
+ } break;
1009
+ case GGML_TYPE_F16:
1010
+ {
1011
+ ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
1012
+ } break;
1013
+ case GGML_TYPE_BF16:
1014
+ {
1015
+ ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
1016
+ } break;
1017
+ case GGML_TYPE_F32:
1018
+ {
1019
+ ((float *)(data))[0] = value;
1020
+ } break;
1021
+ default:
1022
+ {
1023
+ GGML_ABORT("fatal error");
1024
+ }
1025
+ }
1026
+ }
1027
+
1028
+ float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) {
1029
+ if (!ggml_is_contiguous(tensor)) {
1030
+ int64_t id[4] = { 0, 0, 0, 0 };
1031
+ ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
1032
+ return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]);
1033
+ }
1034
+ switch (tensor->type) {
1035
+ case GGML_TYPE_I8:
1036
+ {
1037
+ return ((int8_t *)(tensor->data))[i];
1038
+ }
1039
+ case GGML_TYPE_I16:
1040
+ {
1041
+ return ((int16_t *)(tensor->data))[i];
1042
+ }
1043
+ case GGML_TYPE_I32:
1044
+ {
1045
+ return ((int32_t *)(tensor->data))[i];
1046
+ }
1047
+ case GGML_TYPE_F16:
1048
+ {
1049
+ return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
1050
+ }
1051
+ case GGML_TYPE_BF16:
1052
+ {
1053
+ return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
1054
+ }
1055
+ case GGML_TYPE_F32:
1056
+ {
1057
+ return ((float *)(tensor->data))[i];
1058
+ }
1059
+ default:
1060
+ {
1061
+ GGML_ABORT("fatal error");
1062
+ }
1063
+ }
1064
+ }
1065
+
1066
+ void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) {
1067
+ if (!ggml_is_contiguous(tensor)) {
1068
+ int64_t id[4] = { 0, 0, 0, 0 };
1069
+ ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
1070
+ ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value);
1071
+ return;
1072
+ }
1073
+ switch (tensor->type) {
1074
+ case GGML_TYPE_I8:
1075
+ {
1076
+ ((int8_t *)(tensor->data))[i] = value;
1077
+ } break;
1078
+ case GGML_TYPE_I16:
1079
+ {
1080
+ ((int16_t *)(tensor->data))[i] = value;
1081
+ } break;
1082
+ case GGML_TYPE_I32:
1083
+ {
1084
+ ((int32_t *)(tensor->data))[i] = value;
1085
+ } break;
1086
+ case GGML_TYPE_F16:
1087
+ {
1088
+ ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
1089
+ } break;
1090
+ case GGML_TYPE_BF16:
1091
+ {
1092
+ ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
1093
+ } break;
1094
+ case GGML_TYPE_F32:
1095
+ {
1096
+ ((float *)(tensor->data))[i] = value;
1097
+ } break;
1098
+ default:
1099
+ {
1100
+ GGML_ABORT("fatal error");
1101
+ }
1102
+ }
1103
+ }
1104
+
1105
+ float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
1106
+ void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
1107
+ switch (tensor->type) {
1108
+ case GGML_TYPE_I8:
1109
+ return ((int8_t *) data)[0];
1110
+ case GGML_TYPE_I16:
1111
+ return ((int16_t *) data)[0];
1112
+ case GGML_TYPE_I32:
1113
+ return ((int32_t *) data)[0];
1114
+ case GGML_TYPE_F16:
1115
+ return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
1116
+ case GGML_TYPE_BF16:
1117
+ return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
1118
+ case GGML_TYPE_F32:
1119
+ return ((float *) data)[0];
1120
+ default:
1121
+ GGML_ABORT("fatal error");
1122
+ }
1123
+ }
1124
+
1125
+ void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) {
1126
+ void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
1127
+ switch (tensor->type) {
1128
+ case GGML_TYPE_I8:
1129
+ {
1130
+ ((int8_t *)(data))[0] = value;
1131
+ } break;
1132
+ case GGML_TYPE_I16:
1133
+ {
1134
+ ((int16_t *)(data))[0] = value;
1135
+ } break;
1136
+ case GGML_TYPE_I32:
1137
+ {
1138
+ ((int32_t *)(data))[0] = value;
1139
+ } break;
1140
+ case GGML_TYPE_F16:
1141
+ {
1142
+ ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
1143
+ } break;
1144
+ case GGML_TYPE_BF16:
1145
+ {
1146
+ ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
1147
+ } break;
1148
+ case GGML_TYPE_F32:
1149
+ {
1150
+ ((float *)(data))[0] = value;
1151
+ } break;
1152
+ default:
1153
+ {
1154
+ GGML_ABORT("fatal error");
1155
+ }
1156
+ }
1157
+ }
1158
+
1159
+ ////////////////////////////////////////////////////////////////////////////////
1160
+
1161
+ // ggml_compute_forward_mul_mat
1162
+
1163
+ static void ggml_compute_forward_mul_mat_one_chunk(
1164
+ const struct ggml_compute_params * params,
1165
+ struct ggml_tensor * dst,
1166
+ const enum ggml_type type,
1167
+ const int64_t num_rows_per_vec_dot,
1168
+ const int64_t ir0_start,
1169
+ const int64_t ir0_end,
1170
+ const int64_t ir1_start,
1171
+ const int64_t ir1_end) {
1172
+
1173
+ const struct ggml_tensor * src0 = dst->src[0];
1174
+ const struct ggml_tensor * src1 = dst->src[1];
1175
+
1176
+ GGML_TENSOR_BINARY_OP_LOCALS
1177
+
1178
+ const bool src1_cont = ggml_is_contiguous(src1);
1179
+
1180
+ ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
1181
+ enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
1182
+
1183
+ // broadcast factors
1184
+ const int64_t r2 = ne12 / ne02;
1185
+ const int64_t r3 = ne13 / ne03;
1186
+
1187
+ //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end);
1188
+
1189
+ // threads with no work simply yield (not sure if it helps)
1190
+ if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
1191
+ return;
1192
+ }
1193
+
1194
+ const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
1195
+ const size_t row_size = ggml_row_size(vec_dot_type, ne10);
1196
+
1197
+ assert(ne12 % ne02 == 0);
1198
+ assert(ne13 % ne03 == 0);
1199
+
1200
+ // block-tiling attempt
1201
+ const int64_t blck_0 = 16;
1202
+ const int64_t blck_1 = 16;
1203
+
1204
+ const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11;
1205
+
1206
+ // attempt to reduce false-sharing (does not seem to make a difference)
1207
+ // 16 * 2, accounting for mmla kernels
1208
+ float tmp[32];
1209
+
1210
+ for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
1211
+ for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
1212
+ for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) {
1213
+ const int64_t i13 = (ir1 / (ne12 * ne1));
1214
+ const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
1215
+ const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
1216
+
1217
+ // broadcast src0 into src1
1218
+ const int64_t i03 = i13 / r3;
1219
+ const int64_t i02 = i12 / r2;
1220
+
1221
+ const int64_t i1 = i11;
1222
+ const int64_t i2 = i12;
1223
+ const int64_t i3 = i13;
1224
+
1225
+ const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
1226
+
1227
+ // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
1228
+ // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
1229
+ // the original src1 data pointer, so we should index using the indices directly
1230
+ // TODO: this is a bit of a hack, we should probably have a better way to handle this
1231
+ const char * src1_col = (const char*)wdata +
1232
+ (src1_cont || src1->type != vec_dot_type
1233
+ ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size
1234
+ : (i11 * nb11 + i12 * nb12 + i13 * nb13));
1235
+ float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
1236
+
1237
+ //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
1238
+ // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
1239
+ //}
1240
+
1241
+ for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
1242
+ vec_dot(ne00, &tmp[ir0 - iir0], (num_rows_per_vec_dot > 1 ? 16 : 0), src0_row + ir0 * nb01, (num_rows_per_vec_dot > 1 ? nb01 : 0), src1_col, (num_rows_per_vec_dot > 1 ? src1_col_stride : 0), num_rows_per_vec_dot);
1243
+ }
1244
+
1245
+ for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
1246
+ memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float));
1247
+ }
1248
+ }
1249
+ }
1250
+ }
1251
+ }
1252
+
1253
+ static void ggml_compute_forward_mul_mat(
1254
+ const struct ggml_compute_params * params,
1255
+ struct ggml_tensor * dst) {
1256
+
1257
+ const struct ggml_tensor * src0 = dst->src[0];
1258
+ const struct ggml_tensor * src1 = dst->src[1];
1259
+
1260
+ GGML_TENSOR_BINARY_OP_LOCALS
1261
+
1262
+ const int ith = params->ith;
1263
+ const int nth = params->nth;
1264
+
1265
+ enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
1266
+ ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
1267
+ int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
1268
+
1269
+ GGML_ASSERT(ne0 == ne01);
1270
+ GGML_ASSERT(ne1 == ne11);
1271
+ GGML_ASSERT(ne2 == ne12);
1272
+ GGML_ASSERT(ne3 == ne13);
1273
+
1274
+ // we don't support permuted src0 or src1
1275
+ GGML_ASSERT(nb00 == ggml_type_size(src0->type));
1276
+ GGML_ASSERT(nb10 == ggml_type_size(src1->type));
1277
+
1278
+ // dst cannot be transposed or permuted
1279
+ GGML_ASSERT(nb0 == sizeof(float));
1280
+ GGML_ASSERT(nb0 <= nb1);
1281
+ GGML_ASSERT(nb1 <= nb2);
1282
+ GGML_ASSERT(nb2 <= nb3);
1283
+
1284
+ // nb01 >= nb00 - src0 is not transposed
1285
+ // compute by src0 rows
1286
+
1287
+ // TODO: extract to "extra_op"
1288
+ #if GGML_USE_LLAMAFILE
1289
+ // broadcast factors
1290
+ const int64_t r2 = ne12 / ne02;
1291
+ const int64_t r3 = ne13 / ne03;
1292
+
1293
+ const bool src1_cont = ggml_is_contiguous(src1);
1294
+
1295
+ if (src1_cont) {
1296
+ for (int64_t i13 = 0; i13 < ne13; i13++)
1297
+ for (int64_t i12 = 0; i12 < ne12; i12++)
1298
+ if (!llamafile_sgemm(params,
1299
+ ne01, ne11, ne00/ggml_blck_size(src0->type),
1300
+ (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
1301
+ nb01/ggml_type_size(src0->type),
1302
+ (const char *)src1->data + i12*nb12 + i13*nb13,
1303
+ nb11/ggml_type_size(src1->type),
1304
+ (char *)dst->data + i12*nb2 + i13*nb3,
1305
+ nb1/ggml_type_size(dst->type),
1306
+ src0->type,
1307
+ src1->type,
1308
+ dst->type))
1309
+ goto UseGgmlGemm1;
1310
+ return;
1311
+ }
1312
+ UseGgmlGemm1:;
1313
+ #endif
1314
+
1315
+ if (src1->type != vec_dot_type) {
1316
+ char * wdata = params->wdata;
1317
+
1318
+ const size_t nbw0 = ggml_type_size(vec_dot_type);
1319
+ const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
1320
+ const size_t nbw2 = nbw1*ne11;
1321
+ const size_t nbw3 = nbw2*ne12;
1322
+
1323
+ assert(params->wsize >= ne13*nbw3);
1324
+ GGML_ASSERT(src1->type == GGML_TYPE_F32);
1325
+
1326
+ #if 0
1327
+ for (int64_t i13 = 0; i13 < ne13; ++i13) {
1328
+ for (int64_t i12 = 0; i12 < ne12; ++i12) {
1329
+ for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
1330
+ from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
1331
+ (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
1332
+ ne10);
1333
+ }
1334
+ }
1335
+ }
1336
+ #else
1337
+ for (int64_t i13 = 0; i13 < ne13; ++i13) {
1338
+ for (int64_t i12 = 0; i12 < ne12; ++i12) {
1339
+ for (int64_t i11 = 0; i11 < ne11; ++i11) {
1340
+ size_t bs = ggml_blck_size(vec_dot_type);
1341
+ int64_t ne10_block_start = (ith * ne10/bs) / nth;
1342
+ int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth;
1343
+ from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10),
1344
+ (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0),
1345
+ (ne10_block_end - ne10_block_start) * bs);
1346
+ }
1347
+ }
1348
+ }
1349
+ #endif
1350
+ }
1351
+
1352
+ if (ith == 0) {
1353
+ // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
1354
+ atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
1355
+ }
1356
+
1357
+ ggml_barrier(params->threadpool);
1358
+
1359
+ #if GGML_USE_LLAMAFILE
1360
+ if (src1->type != vec_dot_type) {
1361
+ const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
1362
+ const size_t row_size = ggml_row_size(vec_dot_type, ne10);
1363
+
1364
+ for (int64_t i13 = 0; i13 < ne13; i13++)
1365
+ for (int64_t i12 = 0; i12 < ne12; i12++)
1366
+ if (!llamafile_sgemm(params,
1367
+ ne01, ne11, ne00/ggml_blck_size(src0->type),
1368
+ (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
1369
+ nb01/ggml_type_size(src0->type),
1370
+ (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size,
1371
+ row_size/ggml_type_size(vec_dot_type),
1372
+ (char *)dst->data + i12*nb2 + i13*nb3,
1373
+ nb1/ggml_type_size(dst->type),
1374
+ src0->type,
1375
+ vec_dot_type,
1376
+ dst->type))
1377
+ goto UseGgmlGemm2;
1378
+ return;
1379
+ }
1380
+ UseGgmlGemm2:;
1381
+ #endif
1382
+
1383
+ // This is the size of the first dimension of the result, so we can iterate that way. (see the ASSERT above, these are the same numbers)
1384
+ const int64_t nr0 = ne0;
1385
+
1386
+ // This is the size of the rest of the dimensions of the result
1387
+ const int64_t nr1 = ne1 * ne2 * ne3;
1388
+
1389
+ // Now select a reasonable chunk size.
1390
+ int chunk_size = 16;
1391
+
1392
+ // We need to step up the size if it's small
1393
+ if (nr0 == 1 || nr1 == 1) {
1394
+ chunk_size = 64;
1395
+ }
1396
+
1397
+ // distribute the work across the inner or outer loop based on which one is larger
1398
+ // The number of chunks in the 0/1 dim.
1399
+ // CEIL(nr0/chunk_size)
1400
+ int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
1401
+ int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
1402
+
1403
+ // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
1404
+ // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggml-org/llama.cpp/pull/6915
1405
+ // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
1406
+ if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
1407
+ // distribute the thread work across the inner or outer loop based on which one is larger
1408
+ nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
1409
+ nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
1410
+ }
1411
+
1412
+ // The number of elements in each chunk
1413
+ const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
1414
+ const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
1415
+
1416
+ // The first chunk comes from our thread_id, the rest will get auto-assigned.
1417
+ int current_chunk = ith;
1418
+
1419
+ while (current_chunk < nchunk0 * nchunk1) {
1420
+ const int64_t ith0 = current_chunk % nchunk0;
1421
+ const int64_t ith1 = current_chunk / nchunk0;
1422
+
1423
+ const int64_t ir0_start = dr0 * ith0;
1424
+ const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
1425
+
1426
+ const int64_t ir1_start = dr1 * ith1;
1427
+ const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
1428
+
1429
+ // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
1430
+ int64_t num_rows_per_vec_dot = vec_dot_num_rows;
1431
+
1432
+ // these checks are needed to avoid crossing dim1 boundaries
1433
+ // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
1434
+ if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
1435
+ num_rows_per_vec_dot = 1;
1436
+ }
1437
+ ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
1438
+
1439
+ if (nth >= nchunk0 * nchunk1) {
1440
+ break;
1441
+ }
1442
+
1443
+ current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
1444
+ }
1445
+ }
1446
+
1447
+ // ggml_compute_forward_mul_mat_id
1448
+
1449
+ #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ids->ne[0]*ids->ne[1] + (i1)]
1450
+
1451
+ struct mmid_row_mapping {
1452
+ int32_t i1;
1453
+ int32_t i2;
1454
+ };
1455
+
1456
+ static void ggml_compute_forward_mul_mat_id_one_chunk(
1457
+ struct ggml_tensor * dst,
1458
+ const struct ggml_tensor * src0,
1459
+ const struct ggml_tensor * src1,
1460
+ const struct ggml_tensor * ids,
1461
+ const int64_t cur_a,
1462
+ const int64_t ir0_start,
1463
+ const int64_t ir0_end,
1464
+ const int64_t ir1_start,
1465
+ const int64_t ir1_end,
1466
+ const char * src0_cur,
1467
+ const struct mmid_row_mapping * matrix_rows,
1468
+ const size_t row_size,
1469
+ const bool src1_cont,
1470
+ const void * wdata) {
1471
+
1472
+ GGML_TENSOR_BINARY_OP_LOCALS
1473
+
1474
+ const enum ggml_type type = src0->type;
1475
+
1476
+ ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
1477
+ enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
1478
+
1479
+ const int64_t blck_0 = 16;
1480
+ const int64_t blck_1 = 16;
1481
+
1482
+ float tmp[16];
1483
+
1484
+ for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
1485
+ for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
1486
+ for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ++ir1) {
1487
+ const int64_t _i12 = ir1; // logical row index for this expert
1488
+
1489
+ struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12);
1490
+ const int id = row_mapping.i1; // selected expert index
1491
+
1492
+ const int64_t i11 = id % ne11;
1493
+ const int64_t i12 = row_mapping.i2; // row index in src1
1494
+
1495
+ const int64_t i1 = id; // selected expert index
1496
+ const int64_t i2 = i12; // row
1497
+
1498
+ // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
1499
+ // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
1500
+ // the original src1 data pointer, so we should index using the indices directly
1501
+ // TODO: this is a bit of a hack, we should probably have a better way to handle this
1502
+ const char * src1_col = (const char *) wdata +
1503
+ (src1_cont || src1->type != vec_dot_type
1504
+ ? (i11 + i12*ne11)*row_size
1505
+ : (i11*nb11 + i12*nb12));
1506
+
1507
+ float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2));
1508
+
1509
+ for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
1510
+ vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1);
1511
+ }
1512
+
1513
+ memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir0_end) - iir0)*sizeof(float));
1514
+ }
1515
+ }
1516
+ }
1517
+ }
1518
+
1519
+ static void * incr_ptr_aligned(void ** p, size_t size, size_t align) {
1520
+
1521
+ void * ptr = *p;
1522
+ ptr = (void *) GGML_PAD((uintptr_t) ptr, align);
1523
+ *p = (void *) ((char *) ptr + size);
1524
+ return ptr;
1525
+ }
1526
+
1527
+ static void ggml_compute_forward_mul_mat_id(
1528
+ const struct ggml_compute_params * params,
1529
+ struct ggml_tensor * dst) {
1530
+
1531
+ const struct ggml_tensor * src0 = dst->src[0];
1532
+ const struct ggml_tensor * src1 = dst->src[1];
1533
+ const struct ggml_tensor * ids = dst->src[2];
1534
+
1535
+ GGML_TENSOR_BINARY_OP_LOCALS
1536
+
1537
+ const int ith = params->ith;
1538
+ const int nth = params->nth;
1539
+
1540
+ const enum ggml_type type = src0->type;
1541
+
1542
+ const bool src1_cont = ggml_is_contiguous(src1);
1543
+
1544
+ enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
1545
+ ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
1546
+
1547
+ // we don't support permuted src0 or src1
1548
+ GGML_ASSERT(nb00 == ggml_type_size(type));
1549
+ GGML_ASSERT(nb10 == ggml_type_size(src1->type));
1550
+
1551
+ // dst cannot be transposed or permuted
1552
+ GGML_ASSERT(nb0 == sizeof(float));
1553
+ GGML_ASSERT(nb0 <= nb1);
1554
+ GGML_ASSERT(nb1 <= nb2);
1555
+ GGML_ASSERT(nb2 <= nb3);
1556
+
1557
+ // row groups
1558
+ const int n_ids = ids->ne[0]; // n_expert_used
1559
+ const int n_as = ne02; // n_expert
1560
+
1561
+ void * wdata_cur = params->wdata;
1562
+
1563
+ if (src1->type != vec_dot_type) {
1564
+ incr_ptr_aligned(&wdata_cur, ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
1565
+ }
1566
+
1567
+ int64_t * matrix_row_counts = // [n_as]
1568
+ incr_ptr_aligned(&wdata_cur, n_as*sizeof(int64_t), sizeof(int64_t));
1569
+
1570
+ struct mmid_row_mapping * matrix_rows = // [n_as][ids->ne[0]*ids->ne[1]]
1571
+ incr_ptr_aligned(&wdata_cur, n_as*ids->ne[0]*ids->ne[1]*sizeof(struct mmid_row_mapping), sizeof(int64_t));
1572
+
1573
+ char (*atomic_current_chunk)[CACHE_LINE_SIZE] = // [n_as]
1574
+ incr_ptr_aligned(&wdata_cur, CACHE_LINE_SIZE * n_as, CACHE_LINE_SIZE);
1575
+
1576
+ GGML_ASSERT(params->wsize >= (size_t)((char *) wdata_cur - (char *) params->wdata));
1577
+
1578
+ if (src1->type != vec_dot_type) {
1579
+ char * wdata = params->wdata;
1580
+
1581
+ const size_t nbw0 = ggml_type_size(vec_dot_type);
1582
+ const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
1583
+ const size_t nbw2 = nbw1*ne11;
1584
+ const size_t nbw3 = nbw2*ne12;
1585
+
1586
+ assert(params->wsize >= ne13*nbw3);
1587
+ GGML_ASSERT(src1->type == GGML_TYPE_F32);
1588
+
1589
+ #if 0
1590
+ for (int64_t i13 = 0; i13 < ne13; ++i13) {
1591
+ for (int64_t i12 = ith; i12 < ne12; i12 += nth) {
1592
+ for (int64_t i11 = 0; i11 < ne11; ++i11) {
1593
+ from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
1594
+ (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
1595
+ ne10);
1596
+ }
1597
+ }
1598
+ }
1599
+ #else
1600
+ for (int64_t i13 = 0; i13 < ne13; ++i13) {
1601
+ for (int64_t i12 = 0; i12 < ne12; ++i12) {
1602
+ for (int64_t i11 = 0; i11 < ne11; ++i11) {
1603
+ size_t bs = ggml_blck_size(vec_dot_type);
1604
+ int64_t ne10_block_start = (ith * ne10/bs) / nth;
1605
+ int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth;
1606
+ from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10),
1607
+ (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0),
1608
+ (ne10_block_end - ne10_block_start) * bs);
1609
+ }
1610
+ }
1611
+ }
1612
+ #endif
1613
+ }
1614
+
1615
+ if (ith == 0) {
1616
+ // initialize matrix_row_counts
1617
+ memset(matrix_row_counts, 0, n_as*sizeof(int64_t));
1618
+
1619
+ // group rows by src0 matrix
1620
+ for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) {
1621
+ for (int id = 0; id < n_ids; ++id) {
1622
+ const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);
1623
+
1624
+ assert(i02 >= 0 && i02 < n_as);
1625
+
1626
+ MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
1627
+ matrix_row_counts[i02] += 1;
1628
+ }
1629
+ }
1630
+ }
1631
+
1632
+ // reset current_chunk
1633
+ for (int cur_a = ith; cur_a < n_as; cur_a += nth) {
1634
+ atomic_int * current_chunk_ctr = (atomic_int *)(atomic_current_chunk + cur_a);
1635
+ *current_chunk_ctr = nth;
1636
+ }
1637
+
1638
+ ggml_barrier(params->threadpool);
1639
+
1640
+ for (int cur_a = 0; cur_a < n_as; ++cur_a) {
1641
+ const int64_t cne1 = matrix_row_counts[cur_a];
1642
+
1643
+ if (cne1 == 0) {
1644
+ continue;
1645
+ }
1646
+
1647
+ const char * src0_cur = (const char *) src0->data + cur_a * nb02;
1648
+ const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
1649
+ const size_t row_size = ggml_row_size(vec_dot_type, ne10);
1650
+
1651
+ const int64_t nr0 = ne01;
1652
+ const int64_t nr1 = cne1;
1653
+
1654
+ int chunk_size = 16;
1655
+ if (nr0 == 1 || nr1 == 1) {
1656
+ chunk_size = 64;
1657
+ }
1658
+
1659
+ #if defined(__aarch64__)
1660
+ // disable for ARM
1661
+ const bool disable_chunking = true;
1662
+ #else
1663
+ // disable for NUMA
1664
+ const bool disable_chunking = ggml_is_numa();
1665
+ #endif // defined(__aarch64__)
1666
+
1667
+ int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
1668
+ int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
1669
+
1670
+ if (nchunk0 * nchunk1 < nth * 4 || disable_chunking) {
1671
+ nchunk0 = nr0 > nr1 ? nth : 1;
1672
+ nchunk1 = nr0 > nr1 ? 1 : nth;
1673
+ }
1674
+
1675
+ const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
1676
+ const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
1677
+
1678
+ int current_chunk = ith;
1679
+
1680
+ atomic_int * current_chunk_ctr = (atomic_int *)(atomic_current_chunk + cur_a);
1681
+
1682
+ while (current_chunk < nchunk0 * nchunk1) {
1683
+ const int64_t ith0 = current_chunk % nchunk0;
1684
+ const int64_t ith1 = current_chunk / nchunk0;
1685
+
1686
+ const int64_t ir0_start = dr0 * ith0;
1687
+ const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
1688
+
1689
+ const int64_t ir1_start = dr1 * ith1;
1690
+ const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
1691
+
1692
+ ggml_compute_forward_mul_mat_id_one_chunk(
1693
+ dst, src0, src1, ids, cur_a,
1694
+ ir0_start, ir0_end, ir1_start, ir1_end,
1695
+ src0_cur, matrix_rows, row_size, src1_cont, wdata
1696
+ );
1697
+
1698
+ if (nth >= nchunk0 * nchunk1) {
1699
+ break;
1700
+ }
1701
+
1702
+ current_chunk = atomic_fetch_add_explicit(current_chunk_ctr, 1, memory_order_relaxed);
1703
+ }
1704
+ }
1705
+ }
1706
+
1707
+ /////////////////////////////////
1708
+
1709
+ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
1710
+ GGML_ASSERT(params);
1711
+
1712
+ if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
1713
+ return;
1714
+ }
1715
+
1716
+ // extra_buffer op?
1717
+ if (ggml_cpu_extra_compute_forward(params, tensor)) {
1718
+ return;
1719
+ }
1720
+
1721
+ switch (tensor->op) {
1722
+ case GGML_OP_DUP:
1723
+ {
1724
+ ggml_compute_forward_dup(params, tensor);
1725
+ } break;
1726
+ case GGML_OP_ADD:
1727
+ {
1728
+ ggml_compute_forward_add(params, tensor);
1729
+ } break;
1730
+ case GGML_OP_ADD1:
1731
+ {
1732
+ ggml_compute_forward_add1(params, tensor);
1733
+ } break;
1734
+ case GGML_OP_ACC:
1735
+ {
1736
+ ggml_compute_forward_acc(params, tensor);
1737
+ } break;
1738
+ case GGML_OP_SUB:
1739
+ {
1740
+ ggml_compute_forward_sub(params, tensor);
1741
+ } break;
1742
+ case GGML_OP_MUL:
1743
+ {
1744
+ ggml_compute_forward_mul(params, tensor);
1745
+ } break;
1746
+ case GGML_OP_DIV:
1747
+ {
1748
+ ggml_compute_forward_div(params, tensor);
1749
+ } break;
1750
+ case GGML_OP_SQR:
1751
+ {
1752
+ ggml_compute_forward_sqr(params, tensor);
1753
+ } break;
1754
+ case GGML_OP_SQRT:
1755
+ {
1756
+ ggml_compute_forward_sqrt(params, tensor);
1757
+ } break;
1758
+ case GGML_OP_LOG:
1759
+ {
1760
+ ggml_compute_forward_log(params, tensor);
1761
+ } break;
1762
+ case GGML_OP_SIN:
1763
+ {
1764
+ ggml_compute_forward_sin(params, tensor);
1765
+ } break;
1766
+ case GGML_OP_COS:
1767
+ {
1768
+ ggml_compute_forward_cos(params, tensor);
1769
+ } break;
1770
+ case GGML_OP_SUM:
1771
+ {
1772
+ ggml_compute_forward_sum(params, tensor);
1773
+ } break;
1774
+ case GGML_OP_SUM_ROWS:
1775
+ {
1776
+ ggml_compute_forward_sum_rows(params, tensor);
1777
+ } break;
1778
+ case GGML_OP_MEAN:
1779
+ {
1780
+ ggml_compute_forward_mean(params, tensor);
1781
+ } break;
1782
+ case GGML_OP_ARGMAX:
1783
+ {
1784
+ ggml_compute_forward_argmax(params, tensor);
1785
+ } break;
1786
+ case GGML_OP_COUNT_EQUAL:
1787
+ {
1788
+ ggml_compute_forward_count_equal(params, tensor);
1789
+ } break;
1790
+ case GGML_OP_REPEAT:
1791
+ {
1792
+ ggml_compute_forward_repeat(params, tensor);
1793
+ } break;
1794
+ case GGML_OP_REPEAT_BACK:
1795
+ {
1796
+ ggml_compute_forward_repeat_back(params, tensor);
1797
+ } break;
1798
+ case GGML_OP_CONCAT:
1799
+ {
1800
+ ggml_compute_forward_concat(params, tensor);
1801
+ } break;
1802
+ case GGML_OP_SILU_BACK:
1803
+ {
1804
+ ggml_compute_forward_silu_back(params, tensor);
1805
+ } break;
1806
+ case GGML_OP_NORM:
1807
+ {
1808
+ ggml_compute_forward_norm(params, tensor);
1809
+ } break;
1810
+ case GGML_OP_RMS_NORM:
1811
+ {
1812
+ ggml_compute_forward_rms_norm(params, tensor);
1813
+ } break;
1814
+ case GGML_OP_RMS_NORM_BACK:
1815
+ {
1816
+ ggml_compute_forward_rms_norm_back(params, tensor);
1817
+ } break;
1818
+ case GGML_OP_GROUP_NORM:
1819
+ {
1820
+ ggml_compute_forward_group_norm(params, tensor);
1821
+ } break;
1822
+ case GGML_OP_L2_NORM:
1823
+ {
1824
+ ggml_compute_forward_l2_norm(params, tensor);
1825
+ } break;
1826
+ case GGML_OP_MUL_MAT:
1827
+ {
1828
+ ggml_compute_forward_mul_mat(params, tensor);
1829
+ } break;
1830
+ case GGML_OP_MUL_MAT_ID:
1831
+ {
1832
+ ggml_compute_forward_mul_mat_id(params, tensor);
1833
+ } break;
1834
+ case GGML_OP_OUT_PROD:
1835
+ {
1836
+ ggml_compute_forward_out_prod(params, tensor);
1837
+ } break;
1838
+ case GGML_OP_SCALE:
1839
+ {
1840
+ ggml_compute_forward_scale(params, tensor);
1841
+ } break;
1842
+ case GGML_OP_SET:
1843
+ {
1844
+ ggml_compute_forward_set(params, tensor);
1845
+ } break;
1846
+ case GGML_OP_CPY:
1847
+ {
1848
+ ggml_compute_forward_cpy(params, tensor);
1849
+ } break;
1850
+ case GGML_OP_CONT:
1851
+ {
1852
+ ggml_compute_forward_cont(params, tensor);
1853
+ } break;
1854
+ case GGML_OP_RESHAPE:
1855
+ {
1856
+ ggml_compute_forward_reshape(params, tensor);
1857
+ } break;
1858
+ case GGML_OP_VIEW:
1859
+ {
1860
+ ggml_compute_forward_view(params, tensor);
1861
+ } break;
1862
+ case GGML_OP_PERMUTE:
1863
+ {
1864
+ ggml_compute_forward_permute(params, tensor);
1865
+ } break;
1866
+ case GGML_OP_TRANSPOSE:
1867
+ {
1868
+ ggml_compute_forward_transpose(params, tensor);
1869
+ } break;
1870
+ case GGML_OP_GET_ROWS:
1871
+ {
1872
+ ggml_compute_forward_get_rows(params, tensor);
1873
+ } break;
1874
+ case GGML_OP_GET_ROWS_BACK:
1875
+ {
1876
+ ggml_compute_forward_get_rows_back(params, tensor);
1877
+ } break;
1878
+ case GGML_OP_DIAG:
1879
+ {
1880
+ ggml_compute_forward_diag(params, tensor);
1881
+ } break;
1882
+ case GGML_OP_DIAG_MASK_INF:
1883
+ {
1884
+ ggml_compute_forward_diag_mask_inf(params, tensor);
1885
+ } break;
1886
+ case GGML_OP_DIAG_MASK_ZERO:
1887
+ {
1888
+ ggml_compute_forward_diag_mask_zero(params, tensor);
1889
+ } break;
1890
+ case GGML_OP_SOFT_MAX:
1891
+ {
1892
+ ggml_compute_forward_soft_max(params, tensor);
1893
+ } break;
1894
+ case GGML_OP_SOFT_MAX_BACK:
1895
+ {
1896
+ ggml_compute_forward_soft_max_ext_back(params, tensor);
1897
+ } break;
1898
+ case GGML_OP_ROPE:
1899
+ {
1900
+ ggml_compute_forward_rope(params, tensor);
1901
+ } break;
1902
+ case GGML_OP_ROPE_BACK:
1903
+ {
1904
+ ggml_compute_forward_rope_back(params, tensor);
1905
+ } break;
1906
+ case GGML_OP_CLAMP:
1907
+ {
1908
+ ggml_compute_forward_clamp(params, tensor);
1909
+ } break;
1910
+ case GGML_OP_CONV_TRANSPOSE_1D:
1911
+ {
1912
+ ggml_compute_forward_conv_transpose_1d(params, tensor);
1913
+ } break;
1914
+ case GGML_OP_IM2COL:
1915
+ {
1916
+ ggml_compute_forward_im2col(params, tensor);
1917
+ } break;
1918
+ case GGML_OP_IM2COL_BACK:
1919
+ {
1920
+ ggml_compute_forward_im2col_back_f32(params, tensor);
1921
+ } break;
1922
+ case GGML_OP_CONV_2D_DW:
1923
+ {
1924
+ ggml_compute_forward_conv_2d_dw(params, tensor);
1925
+ } break;
1926
+ case GGML_OP_CONV_TRANSPOSE_2D:
1927
+ {
1928
+ ggml_compute_forward_conv_transpose_2d(params, tensor);
1929
+ } break;
1930
+ case GGML_OP_POOL_1D:
1931
+ {
1932
+ ggml_compute_forward_pool_1d(params, tensor);
1933
+ } break;
1934
+ case GGML_OP_POOL_2D:
1935
+ {
1936
+ ggml_compute_forward_pool_2d(params, tensor);
1937
+ } break;
1938
+ case GGML_OP_POOL_2D_BACK:
1939
+ {
1940
+ ggml_compute_forward_pool_2d_back(params, tensor);
1941
+ } break;
1942
+ case GGML_OP_UPSCALE:
1943
+ {
1944
+ ggml_compute_forward_upscale(params, tensor);
1945
+ } break;
1946
+ case GGML_OP_PAD:
1947
+ {
1948
+ ggml_compute_forward_pad(params, tensor);
1949
+ } break;
1950
+ case GGML_OP_PAD_REFLECT_1D:
1951
+ {
1952
+ ggml_compute_forward_pad_reflect_1d(params, tensor);
1953
+ } break;
1954
+ case GGML_OP_ARANGE:
1955
+ {
1956
+ ggml_compute_forward_arange(params, tensor);
1957
+ } break;
1958
+ case GGML_OP_TIMESTEP_EMBEDDING:
1959
+ {
1960
+ ggml_compute_forward_timestep_embedding(params, tensor);
1961
+ } break;
1962
+ case GGML_OP_ARGSORT:
1963
+ {
1964
+ ggml_compute_forward_argsort(params, tensor);
1965
+ } break;
1966
+ case GGML_OP_LEAKY_RELU:
1967
+ {
1968
+ ggml_compute_forward_leaky_relu(params, tensor);
1969
+ } break;
1970
+ case GGML_OP_FLASH_ATTN_EXT:
1971
+ {
1972
+ ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
1973
+ } break;
1974
+ case GGML_OP_FLASH_ATTN_BACK:
1975
+ {
1976
+ int32_t t = ggml_get_op_params_i32(tensor, 0);
1977
+ GGML_ASSERT(t == 0 || t == 1);
1978
+ bool masked = t != 0;
1979
+ ggml_compute_forward_flash_attn_back(params, masked, tensor);
1980
+ } break;
1981
+ case GGML_OP_SSM_CONV:
1982
+ {
1983
+ ggml_compute_forward_ssm_conv(params, tensor);
1984
+ } break;
1985
+ case GGML_OP_SSM_SCAN:
1986
+ {
1987
+ ggml_compute_forward_ssm_scan(params, tensor);
1988
+ } break;
1989
+ case GGML_OP_WIN_PART:
1990
+ {
1991
+ ggml_compute_forward_win_part(params, tensor);
1992
+ } break;
1993
+ case GGML_OP_WIN_UNPART:
1994
+ {
1995
+ ggml_compute_forward_win_unpart(params, tensor);
1996
+ } break;
1997
+ case GGML_OP_UNARY:
1998
+ {
1999
+ ggml_compute_forward_unary(params, tensor);
2000
+ } break;
2001
+ case GGML_OP_GET_REL_POS:
2002
+ {
2003
+ ggml_compute_forward_get_rel_pos(params, tensor);
2004
+ } break;
2005
+ case GGML_OP_ADD_REL_POS:
2006
+ {
2007
+ ggml_compute_forward_add_rel_pos(params, tensor);
2008
+ } break;
2009
+ case GGML_OP_RWKV_WKV6:
2010
+ {
2011
+ ggml_compute_forward_rwkv_wkv6(params, tensor);
2012
+ } break;
2013
+ case GGML_OP_GATED_LINEAR_ATTN:
2014
+ {
2015
+ ggml_compute_forward_gla(params, tensor);
2016
+ } break;
2017
+ case GGML_OP_RWKV_WKV7:
2018
+ {
2019
+ ggml_compute_forward_rwkv_wkv7(params, tensor);
2020
+ } break;
2021
+ case GGML_OP_MAP_CUSTOM1:
2022
+ {
2023
+ ggml_compute_forward_map_custom1(params, tensor);
2024
+ }
2025
+ break;
2026
+ case GGML_OP_MAP_CUSTOM2:
2027
+ {
2028
+ ggml_compute_forward_map_custom2(params, tensor);
2029
+ }
2030
+ break;
2031
+ case GGML_OP_MAP_CUSTOM3:
2032
+ {
2033
+ ggml_compute_forward_map_custom3(params, tensor);
2034
+ }
2035
+ break;
2036
+ case GGML_OP_CUSTOM:
2037
+ {
2038
+ ggml_compute_forward_custom(params, tensor);
2039
+ }
2040
+ break;
2041
+ case GGML_OP_CROSS_ENTROPY_LOSS:
2042
+ {
2043
+ ggml_compute_forward_cross_entropy_loss(params, tensor);
2044
+ }
2045
+ break;
2046
+ case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
2047
+ {
2048
+ ggml_compute_forward_cross_entropy_loss_back(params, tensor);
2049
+ }
2050
+ break;
2051
+ case GGML_OP_OPT_STEP_ADAMW:
2052
+ {
2053
+ ggml_compute_forward_opt_step_adamw(params, tensor);
2054
+ }
2055
+ break;
2056
+ case GGML_OP_NONE:
2057
+ {
2058
+ // nop
2059
+ } break;
2060
+ case GGML_OP_COUNT:
2061
+ {
2062
+ GGML_ABORT("fatal error");
2063
+ }
2064
+ }
2065
+ }
2066
+
2067
+ // Android's libc implementation "bionic" does not support setting affinity
2068
+ #if defined(__gnu_linux__)
2069
+ static void set_numa_thread_affinity(int thread_n) {
2070
+ if (!ggml_is_numa()) {
2071
+ return;
2072
+ }
2073
+
2074
+ int node_num;
2075
+ int rv;
2076
+ size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
2077
+
2078
+ switch(g_state.numa.numa_strategy) {
2079
+ case GGML_NUMA_STRATEGY_DISTRIBUTE:
2080
+ // run thread on node_num thread_n / (threads per node)
2081
+ node_num = thread_n % g_state.numa.n_nodes;
2082
+ break;
2083
+ case GGML_NUMA_STRATEGY_ISOLATE:
2084
+ // run thread on current_node
2085
+ node_num = g_state.numa.current_node;
2086
+ break;
2087
+ case GGML_NUMA_STRATEGY_NUMACTL:
2088
+ // use the cpuset that numactl gave us
2089
+ rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset);
2090
+ if (rv) {
2091
+ fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv));
2092
+ }
2093
+ return;
2094
+ default:
2095
+ return;
2096
+ }
2097
+
2098
+ struct ggml_numa_node * node = &g_state.numa.nodes[node_num];
2099
+
2100
+ cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
2101
+ CPU_ZERO_S(setsize, cpus);
2102
+ for (size_t i = 0; i < node->n_cpus; ++i) {
2103
+ CPU_SET_S(node->cpus[i], setsize, cpus);
2104
+ }
2105
+
2106
+ rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
2107
+ if (rv) {
2108
+ fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
2109
+ }
2110
+
2111
+ CPU_FREE(cpus);
2112
+ }
2113
+
2114
+ static void clear_numa_thread_affinity(void) {
2115
+ if (!ggml_is_numa()) {
2116
+ return;
2117
+ }
2118
+
2119
+ size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
2120
+
2121
+ cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
2122
+ CPU_ZERO_S(setsize, cpus);
2123
+ for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) {
2124
+ CPU_SET_S(i, setsize, cpus);
2125
+ }
2126
+
2127
+ int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
2128
+ if (rv) {
2129
+ fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
2130
+ }
2131
+
2132
+ CPU_FREE(cpus);
2133
+ }
2134
+ #else
2135
+ // TODO: Windows etc.
2136
+ // (the linux implementation may also work on BSD, someone should test)
2137
+ static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); }
2138
+ static void clear_numa_thread_affinity(void) {}
2139
+ #endif
2140
+
2141
+ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
2142
+ int n_tasks = 0;
2143
+
2144
+ if (ggml_is_empty(node)) {
2145
+ // no need to multi-thread a no-op
2146
+ n_tasks = 1;
2147
+ return n_tasks;
2148
+ }
2149
+
2150
+ switch (node->op) {
2151
+ case GGML_OP_CPY:
2152
+ case GGML_OP_DUP:
2153
+ case GGML_OP_CONT:
2154
+ case GGML_OP_ADD:
2155
+ case GGML_OP_ADD1:
2156
+ case GGML_OP_ACC:
2157
+ {
2158
+ n_tasks = n_threads;
2159
+ } break;
2160
+ case GGML_OP_SUB:
2161
+ case GGML_OP_SQR:
2162
+ case GGML_OP_SQRT:
2163
+ case GGML_OP_LOG:
2164
+ case GGML_OP_SIN:
2165
+ case GGML_OP_COS:
2166
+ case GGML_OP_SUM:
2167
+ case GGML_OP_SUM_ROWS:
2168
+ case GGML_OP_MEAN:
2169
+ case GGML_OP_ARGMAX:
2170
+ {
2171
+ n_tasks = 1;
2172
+ } break;
2173
+ case GGML_OP_COUNT_EQUAL:
2174
+ {
2175
+ n_tasks = n_threads;
2176
+ } break;
2177
+ case GGML_OP_REPEAT:
2178
+ case GGML_OP_REPEAT_BACK:
2179
+ case GGML_OP_LEAKY_RELU:
2180
+ {
2181
+ n_tasks = 1;
2182
+ } break;
2183
+ case GGML_OP_UNARY:
2184
+ switch (ggml_get_unary_op(node)) {
2185
+ case GGML_UNARY_OP_ABS:
2186
+ case GGML_UNARY_OP_SGN:
2187
+ case GGML_UNARY_OP_NEG:
2188
+ case GGML_UNARY_OP_STEP:
2189
+ case GGML_UNARY_OP_TANH:
2190
+ case GGML_UNARY_OP_ELU:
2191
+ case GGML_UNARY_OP_RELU:
2192
+ case GGML_UNARY_OP_SIGMOID:
2193
+ case GGML_UNARY_OP_HARDSWISH:
2194
+ case GGML_UNARY_OP_HARDSIGMOID:
2195
+ case GGML_UNARY_OP_EXP:
2196
+ {
2197
+ n_tasks = 1;
2198
+ } break;
2199
+
2200
+ case GGML_UNARY_OP_GELU:
2201
+ case GGML_UNARY_OP_GELU_QUICK:
2202
+ case GGML_UNARY_OP_SILU:
2203
+ {
2204
+ n_tasks = n_threads;
2205
+ } break;
2206
+ default:
2207
+ GGML_ABORT("fatal error");
2208
+ }
2209
+ break;
2210
+ case GGML_OP_SILU_BACK:
2211
+ case GGML_OP_MUL:
2212
+ case GGML_OP_DIV:
2213
+ case GGML_OP_NORM:
2214
+ case GGML_OP_RMS_NORM:
2215
+ case GGML_OP_RMS_NORM_BACK:
2216
+ case GGML_OP_L2_NORM:
2217
+ case GGML_OP_GROUP_NORM:
2218
+ case GGML_OP_CONCAT:
2219
+ case GGML_OP_MUL_MAT:
2220
+ case GGML_OP_MUL_MAT_ID:
2221
+ case GGML_OP_OUT_PROD:
2222
+ {
2223
+ n_tasks = n_threads;
2224
+ } break;
2225
+ case GGML_OP_GET_ROWS:
2226
+ {
2227
+ // FIXME: get_rows can use additional threads, but the cost of launching additional threads
2228
+ // decreases performance with GPU offloading
2229
+ //n_tasks = n_threads;
2230
+ n_tasks = 1;
2231
+ } break;
2232
+ case GGML_OP_SCALE:
2233
+ case GGML_OP_SET:
2234
+ case GGML_OP_RESHAPE:
2235
+ case GGML_OP_VIEW:
2236
+ case GGML_OP_PERMUTE:
2237
+ case GGML_OP_TRANSPOSE:
2238
+ case GGML_OP_GET_ROWS_BACK:
2239
+ case GGML_OP_DIAG:
2240
+ {
2241
+ n_tasks = 1;
2242
+ } break;
2243
+ case GGML_OP_DIAG_MASK_ZERO:
2244
+ case GGML_OP_DIAG_MASK_INF:
2245
+ case GGML_OP_SOFT_MAX_BACK:
2246
+ case GGML_OP_ROPE:
2247
+ case GGML_OP_ROPE_BACK:
2248
+ case GGML_OP_ADD_REL_POS:
2249
+ {
2250
+ n_tasks = n_threads;
2251
+ } break;
2252
+ case GGML_OP_CLAMP:
2253
+ {
2254
+ n_tasks = 1; //TODO
2255
+ } break;
2256
+ case GGML_OP_SOFT_MAX:
2257
+ {
2258
+ n_tasks = MIN(n_threads, ggml_nrows(node->src[0]));
2259
+ } break;
2260
+ case GGML_OP_IM2COL:
2261
+ case GGML_OP_IM2COL_BACK:
2262
+ case GGML_OP_CONV_2D_DW:
2263
+ case GGML_OP_CONV_TRANSPOSE_1D:
2264
+ case GGML_OP_CONV_TRANSPOSE_2D:
2265
+ {
2266
+ n_tasks = n_threads;
2267
+ } break;
2268
+ case GGML_OP_POOL_1D:
2269
+ case GGML_OP_POOL_2D:
2270
+ case GGML_OP_POOL_2D_BACK:
2271
+ {
2272
+ n_tasks = 1;
2273
+ } break;
2274
+ case GGML_OP_UPSCALE:
2275
+ case GGML_OP_PAD:
2276
+ case GGML_OP_PAD_REFLECT_1D:
2277
+ case GGML_OP_ARANGE:
2278
+ case GGML_OP_TIMESTEP_EMBEDDING:
2279
+ case GGML_OP_ARGSORT:
2280
+ case GGML_OP_FLASH_ATTN_EXT:
2281
+ case GGML_OP_FLASH_ATTN_BACK:
2282
+ case GGML_OP_SSM_CONV:
2283
+ case GGML_OP_SSM_SCAN:
2284
+ case GGML_OP_RWKV_WKV6:
2285
+ case GGML_OP_GATED_LINEAR_ATTN:
2286
+ case GGML_OP_RWKV_WKV7:
2287
+ {
2288
+ n_tasks = n_threads;
2289
+ } break;
2290
+ case GGML_OP_WIN_PART:
2291
+ case GGML_OP_WIN_UNPART:
2292
+ case GGML_OP_GET_REL_POS:
2293
+ {
2294
+ n_tasks = 1;
2295
+ } break;
2296
+ case GGML_OP_MAP_CUSTOM1:
2297
+ {
2298
+ struct ggml_map_custom1_op_params p;
2299
+ memcpy(&p, node->op_params, sizeof(p));
2300
+ if (p.n_tasks == GGML_N_TASKS_MAX) {
2301
+ n_tasks = n_threads;
2302
+ } else {
2303
+ n_tasks = MIN(p.n_tasks, n_threads);
2304
+ }
2305
+ } break;
2306
+ case GGML_OP_MAP_CUSTOM2:
2307
+ {
2308
+ struct ggml_map_custom2_op_params p;
2309
+ memcpy(&p, node->op_params, sizeof(p));
2310
+ if (p.n_tasks == GGML_N_TASKS_MAX) {
2311
+ n_tasks = n_threads;
2312
+ } else {
2313
+ n_tasks = MIN(p.n_tasks, n_threads);
2314
+ }
2315
+ } break;
2316
+ case GGML_OP_MAP_CUSTOM3:
2317
+ {
2318
+ struct ggml_map_custom3_op_params p;
2319
+ memcpy(&p, node->op_params, sizeof(p));
2320
+ if (p.n_tasks == GGML_N_TASKS_MAX) {
2321
+ n_tasks = n_threads;
2322
+ } else {
2323
+ n_tasks = MIN(p.n_tasks, n_threads);
2324
+ }
2325
+ } break;
2326
+ case GGML_OP_CUSTOM:
2327
+ {
2328
+ struct ggml_custom_op_params p;
2329
+ memcpy(&p, node->op_params, sizeof(p));
2330
+ if (p.n_tasks == GGML_N_TASKS_MAX) {
2331
+ n_tasks = n_threads;
2332
+ } else {
2333
+ n_tasks = MIN(p.n_tasks, n_threads);
2334
+ }
2335
+ } break;
2336
+ case GGML_OP_CROSS_ENTROPY_LOSS:
2337
+ case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
2338
+ case GGML_OP_OPT_STEP_ADAMW:
2339
+ {
2340
+ n_tasks = n_threads;
2341
+ } break;
2342
+ case GGML_OP_NONE:
2343
+ {
2344
+ n_tasks = 1;
2345
+ } break;
2346
+ case GGML_OP_COUNT:
2347
+ {
2348
+ GGML_ABORT("fatal error");
2349
+ }
2350
+ default:
2351
+ {
2352
+ fprintf(stderr, "%s: op not implemented: ", __func__);
2353
+ if (node->op < GGML_OP_COUNT) {
2354
+ fprintf(stderr, "%s\n", ggml_op_name(node->op));
2355
+ } else {
2356
+ fprintf(stderr, "%d\n", node->op);
2357
+ }
2358
+ GGML_ABORT("fatal error");
2359
+ }
2360
+ }
2361
+
2362
+ assert(n_tasks > 0);
2363
+
2364
+ return n_tasks;
2365
+ }
2366
+
2367
+ static thread_ret_t ggml_graph_compute_secondary_thread(void* data);
2368
+
2369
+ #if defined(_WIN32)
2370
+ #include "windows.h"
2371
+
2372
+ // TODO: support > 64 CPUs
2373
+ static bool ggml_thread_apply_affinity(bool * mask) {
2374
+ HANDLE h = GetCurrentThread();
2375
+ uint64_t bitmask = 0ULL;
2376
+
2377
+ assert(GGML_MAX_N_THREADS >= 64);
2378
+
2379
+ for (int32_t i = 0; i < 8; i++) {
2380
+ int32_t idx = i * 8;
2381
+ uint8_t val = 0;
2382
+ val |= mask[idx + 0] << 0;
2383
+ val |= mask[idx + 1] << 1;
2384
+ val |= mask[idx + 2] << 2;
2385
+ val |= mask[idx + 3] << 3;
2386
+ val |= mask[idx + 4] << 4;
2387
+ val |= mask[idx + 5] << 5;
2388
+ val |= mask[idx + 6] << 6;
2389
+ val |= mask[idx + 7] << 7;
2390
+ bitmask |= (uint64_t)val << idx;
2391
+ }
2392
+
2393
+ for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) {
2394
+ if (mask[i]) {
2395
+ fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n");
2396
+ break;
2397
+ }
2398
+ }
2399
+
2400
+ DWORD_PTR m = (DWORD_PTR)bitmask;
2401
+
2402
+ m = SetThreadAffinityMask(h, m);
2403
+
2404
+ return m != 0;
2405
+ }
2406
+
2407
+ static bool ggml_thread_apply_priority(int32_t prio) {
2408
+ // Note that on Windows the Process Priority Class must be updated in order to set Thread priority.
2409
+ // This is up to the applications.
2410
+ DWORD p = THREAD_PRIORITY_NORMAL;
2411
+ switch (prio) {
2412
+ case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
2413
+ case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
2414
+ case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break;
2415
+ case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
2416
+ }
2417
+
2418
+ if (prio == GGML_SCHED_PRIO_NORMAL) {
2419
+ // Keep inherited policy/priority
2420
+ return true;
2421
+ }
2422
+
2423
+ if (!SetThreadPriority(GetCurrentThread(), p)) {
2424
+ fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
2425
+ return false;
2426
+ }
2427
+
2428
+ return true;
2429
+ }
2430
+
2431
+ #elif defined(__APPLE__)
2432
+ #include <sys/types.h>
2433
+ #include <sys/resource.h>
2434
+
2435
+ static bool ggml_thread_apply_affinity(const bool * mask) {
2436
+ // Not supported on Apple platforms
2437
+ UNUSED(mask);
2438
+ return true;
2439
+ }
2440
+
2441
+ static bool ggml_thread_apply_priority(int32_t prio) {
2442
+ struct sched_param p;
2443
+ int32_t policy = SCHED_OTHER;
2444
+ switch (prio) {
2445
+ case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
2446
+ case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
2447
+ case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
2448
+ case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
2449
+ }
2450
+
2451
+ if (prio == GGML_SCHED_PRIO_NORMAL) {
2452
+ // Keep inherited policy/priority
2453
+ return true;
2454
+ }
2455
+
2456
+ int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
2457
+ if (err != 0) {
2458
+ fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
2459
+ return false;
2460
+ }
2461
+
2462
+ return true;
2463
+ }
2464
+
2465
+ #elif defined(__gnu_linux__)
2466
+ // TODO: this may not work on BSD, to be verified
2467
+
2468
+ static bool ggml_thread_apply_affinity(const bool * mask) {
2469
+ cpu_set_t cpuset;
2470
+ int err;
2471
+
2472
+ CPU_ZERO(&cpuset);
2473
+
2474
+ for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
2475
+ if (mask[i]) {
2476
+ GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i);
2477
+ CPU_SET(i, &cpuset);
2478
+ }
2479
+ }
2480
+
2481
+ #ifdef __ANDROID__
2482
+ err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
2483
+ if (err < 0) {
2484
+ err = errno;
2485
+ }
2486
+ #else
2487
+ err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
2488
+ #endif
2489
+ if (err != 0) {
2490
+ fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
2491
+ return false;
2492
+ }
2493
+
2494
+ return true;
2495
+ }
2496
+
2497
+ static bool ggml_thread_apply_priority(int32_t prio) {
2498
+ struct sched_param p;
2499
+ int32_t policy = SCHED_OTHER;
2500
+ switch (prio) {
2501
+ case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
2502
+ case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
2503
+ case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
2504
+ case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
2505
+ }
2506
+
2507
+ if (prio == GGML_SCHED_PRIO_NORMAL) {
2508
+ // Keep inherited policy/priority
2509
+ return true;
2510
+ }
2511
+
2512
+ int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
2513
+ if (err != 0) {
2514
+ fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
2515
+ return false;
2516
+ }
2517
+
2518
+ return true;
2519
+ }
2520
+
2521
+ #else // unsupported platforms
2522
+
2523
+ static bool ggml_thread_apply_affinity(const bool * mask) {
2524
+ UNUSED(mask);
2525
+ return true;
2526
+ }
2527
+
2528
+ static bool ggml_thread_apply_priority(int32_t prio) {
2529
+ UNUSED(prio);
2530
+ return true;
2531
+ }
2532
+
2533
+ #endif
2534
+
2535
+ static bool ggml_thread_cpumask_is_valid(const bool * mask) {
2536
+ for (int i = 0; i < GGML_MAX_N_THREADS; i++) {
2537
+ if (mask[i]) { return true; }
2538
+ }
2539
+ return false;
2540
+ }
2541
+
2542
+ static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) {
2543
+ if (!strict) {
2544
+ memcpy(local_mask, global_mask, GGML_MAX_N_THREADS);
2545
+ return;
2546
+ } else {
2547
+ memset(local_mask, 0, GGML_MAX_N_THREADS);
2548
+ int32_t base_idx = *iter;
2549
+ for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
2550
+ int32_t idx = base_idx + i;
2551
+ if (idx >= GGML_MAX_N_THREADS) {
2552
+ // Just a cheaper modulo
2553
+ idx -= GGML_MAX_N_THREADS;
2554
+ }
2555
+ if (global_mask[idx]) {
2556
+ local_mask[idx] = 1;
2557
+ *iter = idx + 1;
2558
+ return;
2559
+ }
2560
+ }
2561
+ }
2562
+ }
2563
+
2564
+ void ggml_threadpool_free(struct ggml_threadpool* threadpool) {
2565
+ if (!threadpool) return;
2566
+
2567
+ const int n_threads = threadpool->n_threads_max;
2568
+
2569
+ #ifndef GGML_USE_OPENMP
2570
+ struct ggml_compute_state* workers = threadpool->workers;
2571
+
2572
+ ggml_mutex_lock(&threadpool->mutex);
2573
+
2574
+ threadpool->stop = true;
2575
+ threadpool->pause = false;
2576
+
2577
+ ggml_cond_broadcast(&threadpool->cond);
2578
+ ggml_mutex_unlock(&threadpool->mutex);
2579
+
2580
+ for (int j = 1; j < n_threads; j++) {
2581
+ int32_t rc = ggml_thread_join(workers[j].thrd, NULL);
2582
+ GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED);
2583
+ UNUSED(rc);
2584
+ }
2585
+
2586
+ ggml_mutex_destroy(&threadpool->mutex);
2587
+ ggml_cond_destroy(&threadpool->cond);
2588
+ #endif // GGML_USE_OPENMP
2589
+
2590
+ const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads;
2591
+ ggml_aligned_free(threadpool->workers, workers_size);
2592
+ ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool));
2593
+ }
2594
+
2595
+ #ifndef GGML_USE_OPENMP
2596
+ // pause/resume must be called under mutex
2597
+ static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) {
2598
+ GGML_PRINT_DEBUG("Pausing threadpool\n");
2599
+ threadpool->pause = true;
2600
+ ggml_cond_broadcast(&threadpool->cond);
2601
+ }
2602
+
2603
+ static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) {
2604
+ GGML_PRINT_DEBUG("Resuming threadpool\n");
2605
+ threadpool->pause = false;
2606
+ ggml_cond_broadcast(&threadpool->cond);
2607
+ }
2608
+ #endif
2609
+
2610
+ void ggml_threadpool_pause(struct ggml_threadpool * threadpool) {
2611
+ #ifndef GGML_USE_OPENMP
2612
+ ggml_mutex_lock(&threadpool->mutex);
2613
+ if (!threadpool->pause) {
2614
+ ggml_threadpool_pause_locked(threadpool);
2615
+ }
2616
+ ggml_mutex_unlock(&threadpool->mutex);
2617
+ #else
2618
+ UNUSED(threadpool);
2619
+ #endif
2620
+ }
2621
+
2622
+ void ggml_threadpool_resume(struct ggml_threadpool * threadpool) {
2623
+ #ifndef GGML_USE_OPENMP
2624
+ ggml_mutex_lock(&threadpool->mutex);
2625
+ if (threadpool->pause) {
2626
+ ggml_threadpool_resume_locked(threadpool);
2627
+ }
2628
+ ggml_mutex_unlock(&threadpool->mutex);
2629
+ #else
2630
+ UNUSED(threadpool);
2631
+ #endif
2632
+ }
2633
+
2634
+ struct ggml_cplan ggml_graph_plan(
2635
+ const struct ggml_cgraph * cgraph,
2636
+ int n_threads,
2637
+ struct ggml_threadpool * threadpool) {
2638
+
2639
+ if (threadpool == NULL) {
2640
+ //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
2641
+ }
2642
+ if (n_threads <= 0) {
2643
+ n_threads = threadpool ? threadpool->n_threads_max : GGML_DEFAULT_N_THREADS;
2644
+ }
2645
+
2646
+ size_t work_size = 0;
2647
+
2648
+ struct ggml_cplan cplan;
2649
+ memset(&cplan, 0, sizeof(struct ggml_cplan));
2650
+
2651
+ int max_tasks = 1;
2652
+
2653
+ // thread scheduling for the different operations + work buffer size estimation
2654
+ for (int i = 0; i < cgraph->n_nodes; i++) {
2655
+ struct ggml_tensor * node = cgraph->nodes[i];
2656
+
2657
+ const int n_tasks = ggml_get_n_tasks(node, n_threads);
2658
+
2659
+ max_tasks = MAX(max_tasks, n_tasks);
2660
+
2661
+ size_t cur = 0;
2662
+
2663
+ if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) {
2664
+ switch (node->op) {
2665
+ case GGML_OP_CPY:
2666
+ case GGML_OP_DUP:
2667
+ {
2668
+ if (ggml_is_quantized(node->type) ||
2669
+ // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
2670
+ (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
2671
+ (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
2672
+ cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
2673
+ }
2674
+ } break;
2675
+ case GGML_OP_ADD:
2676
+ case GGML_OP_ADD1:
2677
+ {
2678
+ if (ggml_is_quantized(node->src[0]->type)) {
2679
+ cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
2680
+ }
2681
+ } break;
2682
+ case GGML_OP_ACC:
2683
+ {
2684
+ if (ggml_is_quantized(node->src[0]->type)) {
2685
+ cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
2686
+ }
2687
+ } break;
2688
+ case GGML_OP_COUNT_EQUAL:
2689
+ {
2690
+ cur = ggml_type_size(node->type)*n_tasks;
2691
+ } break;
2692
+ case GGML_OP_MUL_MAT:
2693
+ {
2694
+ const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type;
2695
+
2696
+ if (node->src[1]->type != vec_dot_type) {
2697
+ cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
2698
+ }
2699
+ } break;
2700
+ case GGML_OP_MUL_MAT_ID:
2701
+ {
2702
+ cur = 0;
2703
+ const struct ggml_tensor * src0 = node->src[0];
2704
+ const struct ggml_tensor * src1 = node->src[1];
2705
+ const struct ggml_tensor * ids = node->src[2];
2706
+ const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
2707
+ const int n_as = src0->ne[2];
2708
+ // src1
2709
+ if (src1->type != vec_dot_type) {
2710
+ cur += ggml_row_size(vec_dot_type, ggml_nelements(src1)) + sizeof(int64_t);
2711
+ }
2712
+ // matrix_row_counts
2713
+ cur += n_as * sizeof(int64_t) + sizeof(int64_t);
2714
+ // matrix_rows
2715
+ cur += n_as*ids->ne[0]*ids->ne[1]*sizeof(struct mmid_row_mapping) + sizeof(int64_t);
2716
+ // atomic_current_chunk
2717
+ cur += CACHE_LINE_SIZE*n_as + CACHE_LINE_SIZE;
2718
+ } break;
2719
+ case GGML_OP_OUT_PROD:
2720
+ {
2721
+ if (ggml_is_quantized(node->src[0]->type)) {
2722
+ cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
2723
+ }
2724
+ } break;
2725
+ case GGML_OP_SOFT_MAX:
2726
+ case GGML_OP_ROPE:
2727
+ case GGML_OP_ROPE_BACK:
2728
+ {
2729
+ cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
2730
+ } break;
2731
+ case GGML_OP_CONV_TRANSPOSE_1D:
2732
+ {
2733
+ GGML_ASSERT(node->src[0]->ne[3] == 1);
2734
+ GGML_ASSERT(node->src[1]->ne[2] == 1);
2735
+ GGML_ASSERT(node->src[1]->ne[3] == 1);
2736
+
2737
+ const int64_t ne00 = node->src[0]->ne[0]; // K
2738
+ const int64_t ne01 = node->src[0]->ne[1]; // Cout
2739
+ const int64_t ne02 = node->src[0]->ne[2]; // Cin
2740
+ const int64_t ne10 = node->src[1]->ne[0]; // L
2741
+ const int64_t ne11 = node->src[1]->ne[1]; // Cin
2742
+
2743
+ if ((node->src[0]->type == GGML_TYPE_F16 ||
2744
+ node->src[0]->type == GGML_TYPE_BF16) &&
2745
+ node->src[1]->type == GGML_TYPE_F32) {
2746
+ cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02;
2747
+ cur += sizeof(ggml_fp16_t)*ne10*ne11;
2748
+ } else if (node->src[0]->type == GGML_TYPE_F32 &&
2749
+ node->src[1]->type == GGML_TYPE_F32) {
2750
+ cur += sizeof(float)*ne00*ne01*ne02;
2751
+ cur += sizeof(float)*ne10*ne11;
2752
+ } else {
2753
+ GGML_ABORT("fatal error");
2754
+ }
2755
+ } break;
2756
+ case GGML_OP_CONV_TRANSPOSE_2D:
2757
+ {
2758
+ const int64_t ne00 = node->src[0]->ne[0]; // W
2759
+ const int64_t ne01 = node->src[0]->ne[1]; // H
2760
+ const int64_t ne02 = node->src[0]->ne[2]; // Channels Out
2761
+ const int64_t ne03 = node->src[0]->ne[3]; // Channels In
2762
+
2763
+ const int64_t ne10 = node->src[1]->ne[0]; // W
2764
+ const int64_t ne11 = node->src[1]->ne[1]; // H
2765
+ const int64_t ne12 = node->src[1]->ne[2]; // Channels In
2766
+
2767
+ cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02*ne03;
2768
+ cur += sizeof(ggml_fp16_t)*ne10*ne11*ne12;
2769
+ } break;
2770
+ case GGML_OP_FLASH_ATTN_EXT:
2771
+ {
2772
+ const int64_t ne10 = node->src[1]->ne[0]; // DK
2773
+ const int64_t ne20 = node->src[2]->ne[0]; // DV
2774
+
2775
+ cur = sizeof(float)*(1*ne10 + 2*ne20)*n_tasks; // 1x head size K + 2x head size V (per thread)
2776
+ } break;
2777
+ case GGML_OP_FLASH_ATTN_BACK:
2778
+ {
2779
+ const int64_t D = node->src[0]->ne[0];
2780
+ const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL);
2781
+ const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back
2782
+ if (node->src[1]->type == GGML_TYPE_F32) {
2783
+ cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
2784
+ cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
2785
+ } else if (node->src[1]->type == GGML_TYPE_F16) {
2786
+ cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
2787
+ cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
2788
+ } else if (node->src[1]->type == GGML_TYPE_BF16) {
2789
+ cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
2790
+ cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
2791
+ }
2792
+ } break;
2793
+
2794
+ case GGML_OP_CROSS_ENTROPY_LOSS:
2795
+ {
2796
+ cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks);
2797
+ } break;
2798
+ case GGML_OP_COUNT:
2799
+ {
2800
+ GGML_ABORT("fatal error");
2801
+ }
2802
+ default:
2803
+ break;
2804
+ }
2805
+ }
2806
+
2807
+ work_size = MAX(work_size, cur);
2808
+ }
2809
+
2810
+ if (work_size > 0) {
2811
+ work_size += CACHE_LINE_SIZE*(n_threads);
2812
+ }
2813
+
2814
+ cplan.threadpool = threadpool;
2815
+ cplan.n_threads = MIN(max_tasks, n_threads);
2816
+ cplan.work_size = work_size;
2817
+ cplan.work_data = NULL;
2818
+
2819
+ return cplan;
2820
+ }
2821
+
2822
+ static thread_ret_t ggml_graph_compute_thread(void * data) {
2823
+ struct ggml_compute_state * state = (struct ggml_compute_state *) data;
2824
+ struct ggml_threadpool * tp = state->threadpool;
2825
+
2826
+ const struct ggml_cgraph * cgraph = tp->cgraph;
2827
+ const struct ggml_cplan * cplan = tp->cplan;
2828
+
2829
+ set_numa_thread_affinity(state->ith);
2830
+
2831
+ struct ggml_compute_params params = {
2832
+ /*.ith =*/ state->ith,
2833
+ /*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
2834
+ /*.wsize =*/ cplan->work_size,
2835
+ /*.wdata =*/ cplan->work_data,
2836
+ /*.threadpool=*/ tp,
2837
+ };
2838
+
2839
+ for (int node_n = 0; node_n < cgraph->n_nodes && atomic_load_explicit(&tp->abort, memory_order_relaxed) != node_n; node_n++) {
2840
+ struct ggml_tensor * node = cgraph->nodes[node_n];
2841
+
2842
+ ggml_compute_forward(&params, node);
2843
+
2844
+ if (state->ith == 0 && cplan->abort_callback &&
2845
+ cplan->abort_callback(cplan->abort_callback_data)) {
2846
+ atomic_store_explicit(&tp->abort, node_n + 1, memory_order_relaxed);
2847
+ tp->ec = GGML_STATUS_ABORTED;
2848
+ }
2849
+
2850
+ if (node_n + 1 < cgraph->n_nodes) {
2851
+ ggml_barrier(state->threadpool);
2852
+ }
2853
+ }
2854
+
2855
+ ggml_barrier(state->threadpool);
2856
+
2857
+ return 0;
2858
+ }
2859
+
2860
+ #ifndef GGML_USE_OPENMP
2861
+
2862
+ // check if thread is active
2863
+ static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
2864
+ struct ggml_threadpool * threadpool = state->threadpool;
2865
+ int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
2866
+ return (state->ith < n_threads);
2867
+ }
2868
+
2869
+ // check if thread is ready to proceed (exit from polling or sleeping)
2870
+ static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
2871
+ struct ggml_threadpool * threadpool = state->threadpool;
2872
+
2873
+ if (state->pending || threadpool->stop || threadpool->pause) { return true; }
2874
+
2875
+ // check for new graph/work
2876
+ int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
2877
+ if (new_graph != state->last_graph) {
2878
+ state->pending = ggml_graph_compute_thread_active(state);
2879
+ state->last_graph = new_graph;
2880
+ }
2881
+
2882
+ return state->pending;
2883
+ }
2884
+
2885
+ // sync thread state after polling
2886
+ static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
2887
+ // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
2888
+ #ifdef GGML_TSAN_ENABLED
2889
+ atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
2890
+ #else
2891
+ atomic_thread_fence(memory_order_seq_cst);
2892
+ #endif
2893
+ UNUSED(state);
2894
+ }
2895
+
2896
+ static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
2897
+ struct ggml_threadpool * threadpool = state->threadpool;
2898
+
2899
+ // Skip polling for unused threads
2900
+ if (!ggml_graph_compute_thread_active(state)) {
2901
+ return state->pending;
2902
+ }
2903
+
2904
+ // This seems to make 0 ... 100 a decent range for polling level across modern processors.
2905
+ // Perhaps, we can adjust it dynamically based on load and things.
2906
+ const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
2907
+
2908
+ for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
2909
+ // No new work. Keep polling.
2910
+ ggml_thread_cpu_relax();
2911
+ }
2912
+
2913
+ return state->pending;
2914
+ }
2915
+
2916
+ static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
2917
+ struct ggml_threadpool * threadpool = state->threadpool;
2918
+
2919
+ if (ggml_graph_compute_poll_for_work(state)) {
2920
+ ggml_graph_compute_thread_sync(state);
2921
+ return state->pending;
2922
+ }
2923
+
2924
+ ggml_mutex_lock_shared(&threadpool->mutex);
2925
+ while (!ggml_graph_compute_thread_ready(state)) {
2926
+ // No new work. Wait for the signal.
2927
+ GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
2928
+ ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
2929
+ }
2930
+ ggml_mutex_unlock_shared(&threadpool->mutex);
2931
+
2932
+ return state->pending;
2933
+ }
2934
+
2935
+ static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
2936
+ struct ggml_compute_state * state = (struct ggml_compute_state *) data;
2937
+ struct ggml_threadpool * threadpool = state->threadpool;
2938
+
2939
+ ggml_thread_apply_priority(threadpool->prio);
2940
+ if (ggml_thread_cpumask_is_valid(state->cpumask)) {
2941
+ ggml_thread_apply_affinity(state->cpumask);
2942
+ }
2943
+
2944
+ while (true) {
2945
+ // Check if we need to sleep
2946
+ while (threadpool->pause) {
2947
+ GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith);
2948
+ ggml_mutex_lock_shared(&threadpool->mutex);
2949
+ if (threadpool->pause) {
2950
+ ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
2951
+ }
2952
+ GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith);
2953
+ ggml_mutex_unlock_shared(&threadpool->mutex);
2954
+ }
2955
+
2956
+ // This needs to be checked for after the cond_wait
2957
+ if (threadpool->stop) break;
2958
+
2959
+ // Check if there is new work
2960
+ // The main thread is the only one that can dispatch new work
2961
+
2962
+ ggml_graph_compute_check_for_work(state);
2963
+ if (state->pending) {
2964
+ state->pending = false;
2965
+
2966
+ ggml_graph_compute_thread(state);
2967
+ }
2968
+ }
2969
+
2970
+ return (thread_ret_t) 0;
2971
+ }
2972
+
2973
+ // Start processing new graph
2974
+ static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
2975
+ {
2976
+ // Always take the mutex here because the worker threads are doing hybrid poll/wait
2977
+
2978
+ ggml_mutex_lock(&threadpool->mutex);
2979
+
2980
+ GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
2981
+
2982
+ // Update the number of active threads
2983
+ atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
2984
+
2985
+ // Indicate the graph is ready to be processed
2986
+ // We need the full seq-cst fence here because of the polling threads (used in thread_sync)
2987
+ atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_seq_cst);
2988
+
2989
+ if (threadpool->pause) {
2990
+ // Update main thread prio and affinity to match the threadpool settings
2991
+ ggml_thread_apply_priority(threadpool->prio);
2992
+ if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
2993
+ ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
2994
+ }
2995
+
2996
+ // resume does cond broadcast
2997
+ ggml_threadpool_resume_locked(threadpool);
2998
+ } else {
2999
+ ggml_cond_broadcast(&threadpool->cond);
3000
+ }
3001
+
3002
+ ggml_mutex_unlock(&threadpool->mutex);
3003
+ }
3004
+
3005
+ #endif // GGML_USE_OPENMP
3006
+
3007
+ static struct ggml_threadpool * ggml_threadpool_new_impl(
3008
+ struct ggml_threadpool_params * tpp,
3009
+ struct ggml_cgraph * cgraph,
3010
+ struct ggml_cplan * cplan) {
3011
+
3012
+ struct ggml_threadpool * threadpool =
3013
+ ggml_aligned_malloc(sizeof(struct ggml_threadpool));
3014
+ {
3015
+ threadpool->cgraph = cgraph;
3016
+ threadpool->cplan = cplan;
3017
+ threadpool->n_graph = 0;
3018
+ threadpool->n_barrier = 0;
3019
+ threadpool->n_barrier_passed = 0;
3020
+ threadpool->current_chunk = 0;
3021
+ threadpool->stop = false;
3022
+ threadpool->pause = tpp->paused;
3023
+ threadpool->abort = -1;
3024
+ threadpool->workers = NULL;
3025
+ threadpool->n_threads_max = tpp->n_threads;
3026
+ threadpool->n_threads_cur = tpp->n_threads;
3027
+ threadpool->poll = tpp->poll;
3028
+ threadpool->prio = tpp->prio;
3029
+ threadpool->ec = GGML_STATUS_SUCCESS;
3030
+ }
3031
+
3032
+ // Allocate and init workers state
3033
+ const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads;
3034
+ struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size);
3035
+
3036
+ memset(workers, 0, workers_size);
3037
+ for (int j = 0; j < tpp->n_threads; j++) {
3038
+ workers[j].threadpool = threadpool;
3039
+ workers[j].ith = j;
3040
+ }
3041
+
3042
+ threadpool->workers = workers;
3043
+
3044
+ #ifndef GGML_USE_OPENMP
3045
+ ggml_mutex_init(&threadpool->mutex);
3046
+ ggml_cond_init(&threadpool->cond);
3047
+
3048
+ // Spin the threads for all workers, and update CPU placements.
3049
+ // Place the main thread last (towards the higher numbered CPU cores).
3050
+
3051
+ int32_t cpumask_iter = 0;
3052
+
3053
+ for (int j = 1; j < tpp->n_threads; j++) {
3054
+ ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);
3055
+
3056
+ int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
3057
+ GGML_ASSERT(rc == 0);
3058
+ }
3059
+
3060
+ ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
3061
+
3062
+ if (!threadpool->pause) {
3063
+ // Update main thread prio and affinity at the start, otherwise we'll do it in resume
3064
+ ggml_thread_apply_priority(threadpool->prio);
3065
+ if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
3066
+ ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
3067
+ }
3068
+ }
3069
+ #endif // GGML_USE_OPENMP
3070
+
3071
+ return threadpool;
3072
+ }
3073
+
3074
+ struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) {
3075
+ return ggml_threadpool_new_impl(tpp, NULL, NULL);
3076
+ }
3077
+
3078
+ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) {
3079
+ ggml_cpu_init();
3080
+
3081
+ GGML_ASSERT(cplan);
3082
+ GGML_ASSERT(cplan->n_threads > 0);
3083
+ GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL);
3084
+
3085
+ int n_threads = cplan->n_threads;
3086
+ struct ggml_threadpool * threadpool = cplan->threadpool;
3087
+
3088
+ bool disposable_threadpool = false;
3089
+
3090
+ if (threadpool == NULL) {
3091
+ //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
3092
+ disposable_threadpool = true;
3093
+
3094
+ struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
3095
+ threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
3096
+ } else {
3097
+ // Reset some of the parameters that need resetting
3098
+ // No worker threads should be accessing the parameters below at this stage
3099
+ threadpool->cgraph = cgraph;
3100
+ threadpool->cplan = cplan;
3101
+ threadpool->current_chunk = 0;
3102
+ threadpool->abort = -1;
3103
+ threadpool->ec = GGML_STATUS_SUCCESS;
3104
+ }
3105
+
3106
+ #ifdef GGML_USE_OPENMP
3107
+ if (n_threads > 1) {
3108
+ #pragma omp parallel num_threads(n_threads)
3109
+ {
3110
+ #pragma omp single
3111
+ {
3112
+ // update the number of threads from the actual number of threads that we got from OpenMP
3113
+ n_threads = omp_get_num_threads();
3114
+ atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
3115
+ }
3116
+
3117
+ ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
3118
+ }
3119
+ } else {
3120
+ atomic_store_explicit(&threadpool->n_threads_cur, 1, memory_order_relaxed);
3121
+ ggml_graph_compute_thread(&threadpool->workers[0]);
3122
+ }
3123
+ #else
3124
+ if (n_threads > threadpool->n_threads_max) {
3125
+ GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
3126
+ n_threads = threadpool->n_threads_max;
3127
+ }
3128
+
3129
+ // Kick all threads to start the new graph
3130
+ ggml_graph_compute_kickoff(threadpool, n_threads);
3131
+
3132
+ // This is a work thread too
3133
+ ggml_graph_compute_thread(&threadpool->workers[0]);
3134
+ #endif
3135
+
3136
+ // don't leave affinity set on the main thread
3137
+ clear_numa_thread_affinity();
3138
+
3139
+ enum ggml_status ret = threadpool->ec;
3140
+
3141
+ if (disposable_threadpool) {
3142
+ ggml_threadpool_free(threadpool);
3143
+ }
3144
+
3145
+ return ret;
3146
+ }
3147
+
3148
+ enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) {
3149
+ struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL);
3150
+
3151
+ cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size);
3152
+
3153
+ return ggml_graph_compute(cgraph, &cplan);
3154
+ }
3155
+
3156
+ void ggml_cpu_fp32_to_fp16(const float * x, ggml_fp16_t * y, int64_t n) {
3157
+ int64_t i = 0;
3158
+ #if defined(__F16C__)
3159
+ #if defined(__AVX512F__)
3160
+ for (; i + 15 < n; i += 16) {
3161
+ __m512 x_vec = _mm512_loadu_ps(x + i);
3162
+ __m256i y_vec = _mm512_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT);
3163
+ _mm256_storeu_si256((__m256i *)(y + i), y_vec);
3164
+ }
3165
+ #endif
3166
+ for (; i + 7 < n; i += 8) {
3167
+ __m256 x_vec = _mm256_loadu_ps(x + i);
3168
+ __m128i y_vec = _mm256_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT);
3169
+ _mm_storeu_si128((__m128i *)(y + i), y_vec);
3170
+ }
3171
+ for (; i + 3 < n; i += 4) {
3172
+ __m128 x_vec = _mm_loadu_ps(x + i);
3173
+ __m128i y_vec = _mm_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT);
3174
+ _mm_storel_epi64((__m128i *)(y + i), y_vec);
3175
+ }
3176
+ #endif
3177
+ for (; i < n; ++i) {
3178
+ y[i] = GGML_FP32_TO_FP16(x[i]);
3179
+ }
3180
+ }
3181
+
3182
+ void ggml_cpu_fp16_to_fp32(const ggml_fp16_t * x, float * y, int64_t n) {
3183
+ int64_t i = 0;
3184
+ #if defined(__F16C__)
3185
+ #if defined(__AVX512F__)
3186
+ for (; i + 15 < n; i += 16) {
3187
+ __m256i x_vec = _mm256_loadu_si256((const __m256i *)(x + i));
3188
+ __m512 y_vec = _mm512_cvtph_ps(x_vec);
3189
+ _mm512_storeu_ps(y + i, y_vec);
3190
+ }
3191
+ #endif
3192
+ for (; i + 7 < n; i += 8) {
3193
+ __m128i x_vec = _mm_loadu_si128((const __m128i *)(x + i));
3194
+ __m256 y_vec = _mm256_cvtph_ps(x_vec);
3195
+ _mm256_storeu_ps(y + i, y_vec);
3196
+ }
3197
+ for (; i + 3 < n; i += 4) {
3198
+ __m128i x_vec = _mm_loadl_epi64((const __m128i *)(x + i));
3199
+ __m128 y_vec = _mm_cvtph_ps(x_vec);
3200
+ _mm_storeu_ps(y + i, y_vec);
3201
+ }
3202
+ #endif
3203
+ for (; i < n; ++i) {
3204
+ y[i] = GGML_FP16_TO_FP32(x[i]);
3205
+ }
3206
+ }
3207
+
3208
+ void ggml_cpu_fp32_to_bf16(const float * x, ggml_bf16_t * y, int64_t n) {
3209
+ int64_t i = 0;
3210
+ for (; i < n; ++i) {
3211
+ y[i] = GGML_FP32_TO_BF16(x[i]);
3212
+ }
3213
+ }
3214
+
3215
+ void ggml_cpu_bf16_to_fp32(const ggml_bf16_t * x, float * y, int64_t n) {
3216
+ int64_t i = 0;
3217
+ #if defined(__AVX2__)
3218
+ #if defined(__AVX512F__)
3219
+ for (; i + 15 < n; i += 16) {
3220
+ _mm512_storeu_ps(y + i,
3221
+ _mm512_castsi512_ps(
3222
+ _mm512_slli_epi32(
3223
+ _mm512_cvtepu16_epi32(
3224
+ _mm256_loadu_si256(
3225
+ (const __m256i *)(x + i))),
3226
+ 16)));
3227
+ }
3228
+ #endif
3229
+ for (; i + 7 < n; i += 8) {
3230
+ _mm256_storeu_ps(y + i,
3231
+ _mm256_castsi256_ps(
3232
+ _mm256_slli_epi32(
3233
+ _mm256_cvtepu16_epi32(
3234
+ _mm_loadu_si128(
3235
+ (const __m128i *)(x + i))),
3236
+ 16)));
3237
+ }
3238
+ #endif
3239
+ for (; i < n; i++) {
3240
+ y[i] = GGML_BF16_TO_FP32(x[i]);
3241
+ }
3242
+ }
3243
+
3244
+ int ggml_cpu_has_avx(void) {
3245
+ #if defined(__AVX__)
3246
+ return 1;
3247
+ #else
3248
+ return 0;
3249
+ #endif
3250
+ }
3251
+
3252
+ int ggml_cpu_has_avx_vnni(void) {
3253
+ #if defined(__AVXVNNI__)
3254
+ return 1;
3255
+ #else
3256
+ return 0;
3257
+ #endif
3258
+ }
3259
+
3260
+ int ggml_cpu_has_avx2(void) {
3261
+ #if defined(__AVX2__)
3262
+ return 1;
3263
+ #else
3264
+ return 0;
3265
+ #endif
3266
+ }
3267
+
3268
+ int ggml_cpu_has_avx512(void) {
3269
+ #if defined(__AVX512F__)
3270
+ return 1;
3271
+ #else
3272
+ return 0;
3273
+ #endif
3274
+ }
3275
+
3276
+ int ggml_cpu_has_avx512_vbmi(void) {
3277
+ #if defined(__AVX512VBMI__)
3278
+ return 1;
3279
+ #else
3280
+ return 0;
3281
+ #endif
3282
+ }
3283
+
3284
+ int ggml_cpu_has_avx512_vnni(void) {
3285
+ #if defined(__AVX512VNNI__)
3286
+ return 1;
3287
+ #else
3288
+ return 0;
3289
+ #endif
3290
+ }
3291
+
3292
+ int ggml_cpu_has_avx512_bf16(void) {
3293
+ #if defined(__AVX512BF16__)
3294
+ return 1;
3295
+ #else
3296
+ return 0;
3297
+ #endif
3298
+ }
3299
+
3300
+ int ggml_cpu_has_amx_int8(void) {
3301
+ #if defined(__AMX_INT8__)
3302
+ return 1;
3303
+ #else
3304
+ return 0;
3305
+ #endif
3306
+ }
3307
+
3308
+ int ggml_cpu_has_bmi2(void) {
3309
+ #if defined(__BMI2__)
3310
+ return 1;
3311
+ #else
3312
+ return 0;
3313
+ #endif
3314
+ }
3315
+
3316
+ int ggml_cpu_has_fma(void) {
3317
+ #if defined(__FMA__)
3318
+ return 1;
3319
+ #else
3320
+ return 0;
3321
+ #endif
3322
+ }
3323
+
3324
+ int ggml_cpu_has_arm_fma(void) {
3325
+ #if defined(__ARM_FEATURE_FMA)
3326
+ return 1;
3327
+ #else
3328
+ return 0;
3329
+ #endif
3330
+ }
3331
+
3332
+ int ggml_cpu_has_riscv_v(void) {
3333
+ #if defined(__riscv_v_intrinsic)
3334
+ return 1;
3335
+ #else
3336
+ return 0;
3337
+ #endif
3338
+ }
3339
+
3340
+ int ggml_cpu_has_f16c(void) {
3341
+ #if defined(__F16C__)
3342
+ return 1;
3343
+ #else
3344
+ return 0;
3345
+ #endif
3346
+ }
3347
+
3348
+ int ggml_cpu_has_fp16_va(void) {
3349
+ #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
3350
+ return 1;
3351
+ #else
3352
+ return 0;
3353
+ #endif
3354
+ }
3355
+
3356
+ int ggml_cpu_has_wasm_simd(void) {
3357
+ #if defined(__wasm_simd128__)
3358
+ return 1;
3359
+ #else
3360
+ return 0;
3361
+ #endif
3362
+ }
3363
+
3364
+ int ggml_cpu_has_llamafile(void) {
3365
+ #if defined(GGML_USE_LLAMAFILE)
3366
+ return 1;
3367
+ #else
3368
+ return 0;
3369
+ #endif
3370
+ }
3371
+
3372
+ int ggml_cpu_has_sse3(void) {
3373
+ #if defined(__SSE3__)
3374
+ return 1;
3375
+ #else
3376
+ return 0;
3377
+ #endif
3378
+ }
3379
+
3380
+ int ggml_cpu_has_ssse3(void) {
3381
+ #if defined(__SSSE3__)
3382
+ return 1;
3383
+ #else
3384
+ return 0;
3385
+ #endif
3386
+ }
3387
+
3388
+ int ggml_cpu_has_vsx(void) {
3389
+ #if defined(__POWER9_VECTOR__)
3390
+ return 1;
3391
+ #else
3392
+ return 0;
3393
+ #endif
3394
+ }
3395
+
3396
+ int ggml_cpu_has_vxe(void) {
3397
+ #if defined(__VXE__) || defined(__VXE2__)
3398
+ return 1;
3399
+ #else
3400
+ return 0;
3401
+ #endif
3402
+ }
3403
+
3404
+ int ggml_cpu_has_neon(void) {
3405
+ #if defined(__ARM_ARCH) && defined(__ARM_NEON)
3406
+ return ggml_arm_arch_features.has_neon;
3407
+ #else
3408
+ return 0;
3409
+ #endif
3410
+ }
3411
+
3412
+ int ggml_cpu_has_dotprod(void) {
3413
+ #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD)
3414
+ return ggml_arm_arch_features.has_dotprod;
3415
+ #else
3416
+ return 0;
3417
+ #endif
3418
+ }
3419
+
3420
+ int ggml_cpu_has_sve(void) {
3421
+ #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
3422
+ return ggml_arm_arch_features.has_sve;
3423
+ #else
3424
+ return 0;
3425
+ #endif
3426
+ }
3427
+
3428
+ int ggml_cpu_has_matmul_int8(void) {
3429
+ #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8)
3430
+ return ggml_arm_arch_features.has_i8mm;
3431
+ #else
3432
+ return 0;
3433
+ #endif
3434
+ }
3435
+
3436
+ int ggml_cpu_get_sve_cnt(void) {
3437
+ #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
3438
+ return ggml_arm_arch_features.sve_cnt;
3439
+ #else
3440
+ return 0;
3441
+ #endif
3442
+ }
3443
+
3444
+ int ggml_cpu_has_sme(void) {
3445
+ #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SME)
3446
+ return ggml_arm_arch_features.has_sme;
3447
+ #else
3448
+ return 0;
3449
+ #endif
3450
+ }
3451
+
3452
+ void ggml_cpu_init(void) {
3453
+ // needed to initialize f16 tables
3454
+ {
3455
+ struct ggml_init_params params = { 0, NULL, false };
3456
+ struct ggml_context * ctx = ggml_init(params);
3457
+ ggml_free(ctx);
3458
+ }
3459
+
3460
+ ggml_critical_section_start();
3461
+
3462
+ static bool is_first_call = true;
3463
+
3464
+ if (is_first_call) {
3465
+ // initialize GELU, Quick GELU, SILU and EXP F32 tables
3466
+ {
3467
+ const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
3468
+
3469
+ for (int i = 0; i < (1 << 16); ++i) {
3470
+ union {
3471
+ uint16_t u16;
3472
+ ggml_fp16_t fp16;
3473
+ } u = {i};
3474
+ float f = GGML_FP16_TO_FP32(u.fp16);
3475
+ ggml_table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
3476
+ ggml_table_gelu_quick_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_quick_f32(f));
3477
+ }
3478
+
3479
+ const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
3480
+
3481
+ GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0);
3482
+ }
3483
+
3484
+ #if defined(__ARM_ARCH)
3485
+ ggml_init_arm_arch_features();
3486
+ #endif
3487
+
3488
+ is_first_call = false;
3489
+ }
3490
+
3491
+ ggml_critical_section_end();
3492
+ }