@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,2459 @@
1
+ #include "llama-context.h"
2
+
3
+ #include "llama-impl.h"
4
+ #include "llama-io.h"
5
+ #include "llama-mmap.h"
6
+ #include "llama-model.h"
7
+ #include "llama-kv-cache.h"
8
+
9
+ #include <cstring>
10
+ #include <stdexcept>
11
+ #include <cinttypes>
12
+
13
+ //
14
+ // llama_context
15
+ //
16
+
17
+ llama_context::llama_context(
18
+ const llama_model & model,
19
+ llama_context_params params) :
20
+ model(model) {
21
+ LLAMA_LOG_INFO("%s: constructing llama_context\n", __func__);
22
+
23
+ t_start_us = model.t_start_us;
24
+ t_load_us = model.t_load_us;
25
+
26
+ const auto & hparams = model.hparams;
27
+
28
+ cparams.n_seq_max = std::max(1u, params.n_seq_max);
29
+ cparams.n_threads = params.n_threads;
30
+ cparams.n_threads_batch = params.n_threads_batch;
31
+ cparams.yarn_ext_factor = params.yarn_ext_factor;
32
+ cparams.yarn_attn_factor = params.yarn_attn_factor;
33
+ cparams.yarn_beta_fast = params.yarn_beta_fast;
34
+ cparams.yarn_beta_slow = params.yarn_beta_slow;
35
+ cparams.defrag_thold = params.defrag_thold;
36
+ cparams.embeddings = params.embeddings;
37
+ cparams.offload_kqv = params.offload_kqv;
38
+ cparams.flash_attn = params.flash_attn;
39
+ cparams.no_perf = params.no_perf;
40
+ cparams.pooling_type = params.pooling_type;
41
+ cparams.warmup = false;
42
+
43
+ cparams.n_ctx = params.n_ctx == 0 ? hparams.n_ctx_train : params.n_ctx;
44
+ cparams.rope_freq_base = params.rope_freq_base == 0.0f ? hparams.rope_freq_base_train : params.rope_freq_base;
45
+ cparams.rope_freq_scale = params.rope_freq_scale == 0.0f ? hparams.rope_freq_scale_train : params.rope_freq_scale;
46
+
47
+ cparams.n_ctx_orig_yarn = params.yarn_orig_ctx != 0 ? params.yarn_orig_ctx :
48
+ hparams.n_ctx_orig_yarn != 0 ? hparams.n_ctx_orig_yarn :
49
+ hparams.n_ctx_train;
50
+
51
+ cparams.cb_eval = params.cb_eval;
52
+ cparams.cb_eval_user_data = params.cb_eval_user_data;
53
+
54
+ auto rope_scaling_type = params.rope_scaling_type;
55
+ if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED) {
56
+ rope_scaling_type = hparams.rope_scaling_type_train;
57
+ }
58
+
59
+ if (rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_NONE) {
60
+ cparams.rope_freq_scale = 1.0f; // never scale if scaling type is none
61
+ }
62
+
63
+ if (cparams.yarn_ext_factor < 0.0f) { // negative indicates 'not set'
64
+ cparams.yarn_ext_factor = rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_YARN ? 1.0f : 0.0f;
65
+ }
66
+
67
+ cparams.yarn_attn_factor *= hparams.rope_attn_factor;
68
+
69
+ if (cparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) {
70
+ if (hparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) {
71
+ cparams.pooling_type = LLAMA_POOLING_TYPE_NONE;
72
+ } else {
73
+ cparams.pooling_type = hparams.pooling_type;
74
+ }
75
+ }
76
+
77
+ if (params.attention_type == LLAMA_ATTENTION_TYPE_UNSPECIFIED) {
78
+ cparams.causal_attn = hparams.causal_attn;
79
+ } else {
80
+ cparams.causal_attn = params.attention_type == LLAMA_ATTENTION_TYPE_CAUSAL;
81
+ }
82
+
83
+ // with causal attention, the batch size is limited by the context size
84
+ cparams.n_batch = cparams.causal_attn ? std::min(cparams.n_ctx, params.n_batch) : params.n_batch;
85
+
86
+ // the batch has to be at least GGML_KQ_MASK_PAD because we will be padding the KQ_mask
87
+ // this is required by GPU kernels in order to avoid out-of-bounds accesses (e.g. ggml_flash_attn_ext)
88
+ // ref: https://github.com/ggerganov/llama.cpp/pull/5021
89
+ // TODO: this padding is not needed for the cache-less context so we should probably move it to llama_context_kv_self
90
+ if (cparams.n_batch < GGML_KQ_MASK_PAD) {
91
+ LLAMA_LOG_WARN("%s: n_batch is less than GGML_KQ_MASK_PAD - increasing to %d\n", __func__, GGML_KQ_MASK_PAD);
92
+ cparams.n_batch = GGML_KQ_MASK_PAD;
93
+ }
94
+
95
+ cparams.n_ubatch = std::min(cparams.n_batch, params.n_ubatch == 0 ? params.n_batch : params.n_ubatch);
96
+ cparams.op_offload = params.op_offload;
97
+
98
+ const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
99
+
100
+ LLAMA_LOG_INFO("%s: n_seq_max = %u\n", __func__, cparams.n_seq_max);
101
+ LLAMA_LOG_INFO("%s: n_ctx = %u\n", __func__, cparams.n_ctx);
102
+ LLAMA_LOG_INFO("%s: n_ctx_per_seq = %u\n", __func__, n_ctx_per_seq);
103
+ LLAMA_LOG_INFO("%s: n_batch = %u\n", __func__, cparams.n_batch);
104
+ LLAMA_LOG_INFO("%s: n_ubatch = %u\n", __func__, cparams.n_ubatch);
105
+ LLAMA_LOG_INFO("%s: causal_attn = %d\n", __func__, cparams.causal_attn);
106
+ LLAMA_LOG_INFO("%s: flash_attn = %d\n", __func__, cparams.flash_attn);
107
+ LLAMA_LOG_INFO("%s: freq_base = %.1f\n", __func__, cparams.rope_freq_base);
108
+ LLAMA_LOG_INFO("%s: freq_scale = %g\n", __func__, cparams.rope_freq_scale);
109
+
110
+ if (n_ctx_per_seq < hparams.n_ctx_train) {
111
+ LLAMA_LOG_WARN("%s: n_ctx_per_seq (%u) < n_ctx_train (%u) -- the full capacity of the model will not be utilized\n",
112
+ __func__, n_ctx_per_seq, hparams.n_ctx_train);
113
+ }
114
+
115
+ if (n_ctx_per_seq > hparams.n_ctx_train) {
116
+ LLAMA_LOG_WARN("%s: n_ctx_per_seq (%u) > n_ctx_train (%u) -- possible training context overflow\n",
117
+ __func__, n_ctx_per_seq, hparams.n_ctx_train);
118
+ }
119
+
120
+ if (!hparams.vocab_only) {
121
+ // GPU backends
122
+ for (auto * dev : model.devices) {
123
+ ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
124
+ if (backend == nullptr) {
125
+ throw std::runtime_error(format("failed to initialize %s backend", ggml_backend_dev_name(dev)));
126
+ }
127
+ backends.emplace_back(backend);
128
+ }
129
+
130
+ // add ACCEL backends (such as BLAS)
131
+ for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
132
+ ggml_backend_dev_t dev = ggml_backend_dev_get(i);
133
+ if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
134
+ ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
135
+ if (backend == nullptr) {
136
+ throw std::runtime_error(format("failed to initialize %s backend", ggml_backend_dev_name(dev)));
137
+ }
138
+ backends.emplace_back(backend);
139
+ }
140
+ }
141
+
142
+ // add CPU backend
143
+ backend_cpu = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
144
+ if (backend_cpu == nullptr) {
145
+ throw std::runtime_error("failed to initialize CPU backend");
146
+ }
147
+ backends.emplace_back(backend_cpu);
148
+
149
+ // create a list of the set_n_threads functions in the backends
150
+ for (auto & backend : backends) {
151
+ ggml_backend_dev_t dev = ggml_backend_get_device(backend.get());
152
+ ggml_backend_reg_t reg = dev ? ggml_backend_dev_backend_reg(dev) : nullptr;
153
+ if (reg) {
154
+ auto ggml_backend_set_n_threads_fn = (ggml_backend_set_n_threads_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_n_threads");
155
+ if (ggml_backend_set_n_threads_fn) {
156
+ set_n_threads_fns.emplace_back(backend.get(), ggml_backend_set_n_threads_fn);
157
+ }
158
+ }
159
+ }
160
+
161
+ llama_set_abort_callback(this, params.abort_callback, params.abort_callback_data);
162
+
163
+ // graph outputs buffer
164
+ {
165
+ // resized during inference when a batch uses more outputs
166
+ if ((uint32_t) output_reserve(params.n_seq_max) < params.n_seq_max) {
167
+ throw std::runtime_error("failed to reserve initial output buffer");
168
+ }
169
+
170
+ LLAMA_LOG_INFO("%s: %10s output buffer size = %8.2f MiB\n", __func__,
171
+ ggml_backend_buffer_name (buf_output.get()),
172
+ ggml_backend_buffer_get_size(buf_output.get()) / 1024.0 / 1024.0);
173
+ }
174
+ }
175
+
176
+ // init the memory module
177
+ if (!hparams.vocab_only) {
178
+ llama_memory_params params_mem = {
179
+ /*.type_k =*/ params.type_k,
180
+ /*.type_v =*/ params.type_v,
181
+ };
182
+
183
+ memory.reset(model.create_memory(params_mem, cparams));
184
+ }
185
+
186
+ // init backends
187
+ if (!hparams.vocab_only) {
188
+ LLAMA_LOG_DEBUG("%s: enumerating backends\n", __func__);
189
+
190
+ backend_buft.clear();
191
+ backend_ptrs.clear();
192
+
193
+ for (auto & backend : backends) {
194
+ auto * buft = ggml_backend_get_default_buffer_type(backend.get());
195
+ auto backend_type = ggml_backend_dev_type(ggml_backend_get_device(backend.get()));
196
+
197
+ if (backend_type == GGML_BACKEND_DEVICE_TYPE_CPU && !model.devices.empty()) {
198
+ // use the host buffer of the first device CPU for faster transfer of the intermediate state
199
+ auto * dev = model.devices[0];
200
+ auto * host_buft = ggml_backend_dev_host_buffer_type(dev);
201
+ if (host_buft) {
202
+ buft = host_buft;
203
+ }
204
+ }
205
+
206
+ backend_buft.push_back(buft);
207
+ backend_ptrs.push_back(backend.get());
208
+ }
209
+
210
+ LLAMA_LOG_DEBUG("%s: backend_ptrs.size() = %zu\n", __func__, backend_ptrs.size());
211
+
212
+ const size_t max_nodes = this->graph_max_nodes();
213
+
214
+ LLAMA_LOG_DEBUG("%s: max_nodes = %zu\n", __func__, max_nodes);
215
+
216
+ // buffer used to store the computation graph and the tensor meta data
217
+ buf_compute_meta.resize(ggml_tensor_overhead()*max_nodes + ggml_graph_overhead_custom(max_nodes, false));
218
+
219
+ // TODO: move these checks to ggml_backend_sched
220
+ // enabling pipeline parallelism in the scheduler increases memory usage, so it is only done when necessary
221
+ bool pipeline_parallel =
222
+ model.n_devices() > 1 &&
223
+ model.params.n_gpu_layers > (int) model.hparams.n_layer &&
224
+ model.params.split_mode == LLAMA_SPLIT_MODE_LAYER &&
225
+ cparams.offload_kqv &&
226
+ !model.has_tensor_overrides();
227
+
228
+ // pipeline parallelism requires support for async compute and events in all devices
229
+ if (pipeline_parallel) {
230
+ for (auto & backend : backends) {
231
+ auto dev_type = ggml_backend_dev_type(ggml_backend_get_device(backend.get()));
232
+ if (dev_type == GGML_BACKEND_DEVICE_TYPE_CPU) {
233
+ // ignore CPU backend
234
+ continue;
235
+ }
236
+ auto * dev = ggml_backend_get_device(backend.get());
237
+ ggml_backend_dev_props props;
238
+ ggml_backend_dev_get_props(dev, &props);
239
+ if (!props.caps.async || !props.caps.events) {
240
+ // device does not support async compute or events
241
+ pipeline_parallel = false;
242
+ break;
243
+ }
244
+ }
245
+ }
246
+
247
+ sched.reset(ggml_backend_sched_new(backend_ptrs.data(), backend_buft.data(), backend_ptrs.size(), max_nodes, pipeline_parallel, cparams.op_offload));
248
+
249
+ if (pipeline_parallel) {
250
+ LLAMA_LOG_INFO("%s: pipeline parallelism enabled (n_copies=%d)\n", __func__, ggml_backend_sched_get_n_copies(sched.get()));
251
+ }
252
+ }
253
+
254
+ // reserve worst-case graph
255
+ if (!hparams.vocab_only && memory) {
256
+ const uint32_t n_seqs = 1; // TODO: worst-case number of sequences
257
+ const uint32_t n_tokens = std::min(cparams.n_ctx, cparams.n_ubatch);
258
+
259
+ llama_token token = model.vocab.token_bos(); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
260
+
261
+ // restore later
262
+ // TODO: something cleaner
263
+ const auto n_outputs_save = n_outputs;
264
+
265
+ LLAMA_LOG_DEBUG("%s: worst-case: n_tokens = %d, n_seqs = %d, n_outputs = %d\n", __func__, n_tokens, n_seqs, n_outputs);
266
+
267
+ int n_splits_pp = -1;
268
+ int n_nodes_pp = -1;
269
+
270
+ int n_splits_tg = -1;
271
+ int n_nodes_tg = -1;
272
+
273
+ // simulate full KV cache
274
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
275
+
276
+ kv_self->set_full();
277
+
278
+ cross.v_embd.clear();
279
+
280
+ // reserve pp graph first so that buffers are only allocated once
281
+ {
282
+ llama_ubatch ubatch_pp = { true, n_tokens, n_tokens / n_seqs, n_seqs, &token, nullptr, nullptr, nullptr, nullptr, nullptr};
283
+
284
+ // max number of outputs
285
+ n_outputs = ubatch_pp.n_tokens;
286
+
287
+ LLAMA_LOG_DEBUG("%s: reserving graph for n_tokens = %d, n_seqs = %d\n", __func__, ubatch_pp.n_tokens, ubatch_pp.n_seqs);
288
+
289
+ auto * gf = graph_init();
290
+ graph_build(ctx_compute.get(), gf, ubatch_pp, LLM_GRAPH_TYPE_DEFAULT);
291
+
292
+ if (!ggml_backend_sched_reserve(sched.get(), gf)) {
293
+ throw std::runtime_error("failed to allocate compute pp buffers");
294
+ }
295
+
296
+ n_splits_pp = ggml_backend_sched_get_n_splits(sched.get());
297
+ n_nodes_pp = ggml_graph_n_nodes(gf);
298
+ }
299
+
300
+ // reserve with tg graph to get the number of splits and nodes
301
+ {
302
+ llama_ubatch ubatch_tg = { true, 1, 1, n_seqs, &token, nullptr, nullptr, nullptr, nullptr, nullptr};
303
+
304
+ n_outputs = ubatch_tg.n_tokens;
305
+
306
+ LLAMA_LOG_DEBUG("%s: reserving graph for n_tokens = %d, n_seqs = %d\n", __func__, ubatch_tg.n_tokens, ubatch_tg.n_seqs);
307
+
308
+ auto * gf = graph_init();
309
+ graph_build(ctx_compute.get(), gf, ubatch_tg, LLM_GRAPH_TYPE_DEFAULT);
310
+
311
+ if (!ggml_backend_sched_reserve(sched.get(), gf)) {
312
+ throw std::runtime_error("failed to allocate compute tg buffers");
313
+ }
314
+
315
+ n_splits_tg = ggml_backend_sched_get_n_splits(sched.get());
316
+ n_nodes_tg = ggml_graph_n_nodes(gf);
317
+ }
318
+
319
+ // reserve again with pp graph to avoid ggml-alloc reallocations during inference
320
+ {
321
+ llama_ubatch ubatch_pp = { true, n_tokens, n_tokens / n_seqs, n_seqs, &token, nullptr, nullptr, nullptr, nullptr, nullptr};
322
+
323
+ n_outputs = ubatch_pp.n_tokens;
324
+
325
+ LLAMA_LOG_DEBUG("%s: reserving graph for n_tokens = %d, n_seqs = %d\n", __func__, ubatch_pp.n_tokens, ubatch_pp.n_seqs);
326
+
327
+ auto * gf = graph_init();
328
+ graph_build(ctx_compute.get(), gf, ubatch_pp, LLM_GRAPH_TYPE_DEFAULT);
329
+
330
+ if (!ggml_backend_sched_reserve(sched.get(), gf)) {
331
+ throw std::runtime_error("failed to allocate compute pp buffers");
332
+ }
333
+ }
334
+
335
+ n_outputs = n_outputs_save;
336
+
337
+ for (size_t i = 0; i < backend_ptrs.size(); ++i) {
338
+ ggml_backend_t backend = backend_ptrs[i];
339
+ ggml_backend_buffer_type_t buft = backend_buft[i];
340
+ size_t size = ggml_backend_sched_get_buffer_size(sched.get(), backend);
341
+ if (size > 1) {
342
+ LLAMA_LOG_INFO("%s: %10s compute buffer size = %8.2f MiB\n", __func__,
343
+ ggml_backend_buft_name(buft),
344
+ size / 1024.0 / 1024.0);
345
+ }
346
+ }
347
+
348
+ if (n_nodes_pp == n_nodes_tg) {
349
+ LLAMA_LOG_INFO("%s: graph nodes = %d\n", __func__, n_nodes_pp);
350
+ } else {
351
+ LLAMA_LOG_INFO("%s: graph nodes = %d (with bs=%d), %d (with bs=1)\n", __func__, n_nodes_pp, n_tokens, n_nodes_tg);
352
+ }
353
+
354
+ if (n_splits_pp == n_splits_tg) {
355
+ LLAMA_LOG_INFO("%s: graph splits = %d\n", __func__, n_splits_pp);
356
+ } else {
357
+ LLAMA_LOG_INFO("%s: graph splits = %d (with bs=%d), %d (with bs=1)\n", __func__, n_splits_pp, n_tokens, n_splits_tg);
358
+ }
359
+ }
360
+ }
361
+
362
+ llama_context::~llama_context() = default;
363
+
364
+ void llama_context::synchronize() {
365
+ ggml_backend_sched_synchronize(sched.get());
366
+
367
+ // FIXME: if multiple single tokens are evaluated without a synchronization,
368
+ // the stats will be added to the prompt evaluation stats
369
+ // this should only happen when using batch size 1 to evaluate a batch
370
+
371
+ // add the evaluation to the stats
372
+ if (n_queued_tokens == 1) {
373
+ if (!cparams.no_perf) {
374
+ t_eval_us += ggml_time_us() - t_compute_start_us;
375
+ }
376
+ n_eval++;
377
+ } else if (n_queued_tokens > 1) {
378
+ if (!cparams.no_perf) {
379
+ t_p_eval_us += ggml_time_us() - t_compute_start_us;
380
+ }
381
+ n_p_eval += n_queued_tokens;
382
+ }
383
+
384
+ // get a more accurate load time, upon first eval
385
+ if (n_queued_tokens > 0 && !has_evaluated_once) {
386
+ t_load_us = ggml_time_us() - t_start_us;
387
+ has_evaluated_once = true;
388
+ }
389
+
390
+ n_queued_tokens = 0;
391
+ t_compute_start_us = 0;
392
+ }
393
+
394
+ const llama_model & llama_context::get_model() const {
395
+ return model;
396
+ }
397
+
398
+ const llama_cparams & llama_context::get_cparams() const {
399
+ return cparams;
400
+ }
401
+
402
+ ggml_backend_sched_t llama_context::get_sched() const {
403
+ return sched.get();
404
+ }
405
+
406
+ ggml_context * llama_context::get_ctx_compute() const {
407
+ return ctx_compute.get();
408
+ }
409
+
410
+ uint32_t llama_context::n_ctx() const {
411
+ return cparams.n_ctx;
412
+ }
413
+
414
+ uint32_t llama_context::n_ctx_per_seq() const {
415
+ return cparams.n_ctx / cparams.n_seq_max;
416
+ }
417
+
418
+ uint32_t llama_context::n_batch() const {
419
+ return cparams.n_batch;
420
+ }
421
+
422
+ uint32_t llama_context::n_ubatch() const {
423
+ return cparams.n_ubatch;
424
+ }
425
+
426
+ uint32_t llama_context::n_seq_max() const {
427
+ return cparams.n_seq_max;
428
+ }
429
+
430
+ uint32_t llama_context::n_threads() const {
431
+ return cparams.n_threads;
432
+ }
433
+
434
+ uint32_t llama_context::n_threads_batch() const {
435
+ return cparams.n_threads_batch;
436
+ }
437
+
438
+ llama_kv_cache * llama_context::get_kv_self() {
439
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
440
+ return kv_self;
441
+ }
442
+
443
+ const llama_kv_cache * llama_context::get_kv_self() const {
444
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
445
+ return kv_self;
446
+ }
447
+
448
+ void llama_context::kv_self_update() {
449
+ bool need_reserve = false;
450
+
451
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
452
+
453
+ need_reserve = kv_self->update(*this);
454
+
455
+ // reserve a worst case graph if needed
456
+ if (need_reserve) {
457
+ LLAMA_LOG_DEBUG("%s: reserving a worst case graph\n", __func__);
458
+
459
+ // build worst-case graph
460
+ uint32_t n_seqs = 1; // TODO: worst-case number of sequences
461
+ uint32_t n_tokens = std::min(cparams.n_ctx, cparams.n_ubatch);
462
+
463
+ // simulate full KV cache
464
+ kv_self->set_full();
465
+
466
+ llama_token token = model.vocab.token_bos(); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
467
+ llama_ubatch ubatch = { true, n_tokens, n_tokens / n_seqs, n_seqs, &token, nullptr, nullptr, nullptr, nullptr, nullptr};
468
+
469
+ auto * gf = graph_init();
470
+ graph_build(ctx_compute.get(), gf, ubatch, LLM_GRAPH_TYPE_DEFAULT);
471
+
472
+ // initialize scheduler with the worst-case graph
473
+ ggml_backend_sched_reset(sched.get());
474
+ if (!ggml_backend_sched_reserve(sched.get(), gf)) {
475
+ LLAMA_LOG_ERROR("%s: failed to allocate compute buffers\n", __func__);
476
+ }
477
+ }
478
+ }
479
+
480
+ enum llama_pooling_type llama_context::pooling_type() const {
481
+ return cparams.pooling_type;
482
+ }
483
+
484
+ float * llama_context::get_logits() {
485
+ return logits;
486
+ }
487
+
488
+ float * llama_context::get_logits_ith(int32_t i) {
489
+ int32_t j = -1;
490
+
491
+ try {
492
+ if (logits == nullptr) {
493
+ throw std::runtime_error("no logits");
494
+ }
495
+
496
+ if (i < 0) {
497
+ j = n_outputs + i;
498
+ if (j < 0) {
499
+ throw std::runtime_error(format("negative index out of range [0, %d)", n_outputs));
500
+ }
501
+ } else if ((size_t) i >= output_ids.size()) {
502
+ throw std::runtime_error(format("out of range [0, %zu)", output_ids.size()));
503
+ } else {
504
+ j = output_ids[i];
505
+ }
506
+
507
+ if (j < 0) {
508
+ throw std::runtime_error(format("batch.logits[%d] != true", i));
509
+ }
510
+ if (j >= n_outputs) {
511
+ // This should not happen
512
+ throw std::runtime_error(format("corrupt output buffer (j=%d, n_outputs=%d)", j, n_outputs));
513
+ }
514
+
515
+ return logits + j*model.vocab.n_tokens();
516
+ } catch (const std::exception & err) {
517
+ LLAMA_LOG_ERROR("%s: invalid logits id %d, reason: %s\n", __func__, i, err.what());
518
+ #ifndef NDEBUG
519
+ GGML_ABORT("fatal error");
520
+ #else
521
+ return nullptr;
522
+ #endif
523
+ }
524
+ }
525
+
526
+ float * llama_context::get_embeddings() {
527
+ return embd;
528
+ }
529
+
530
+ float * llama_context::get_embeddings_ith(int32_t i) {
531
+ int32_t j = -1;
532
+
533
+ try {
534
+ if (embd == nullptr) {
535
+ throw std::runtime_error("no embeddings");
536
+ }
537
+
538
+ if (i < 0) {
539
+ j = n_outputs + i;
540
+ if (j < 0) {
541
+ throw std::runtime_error(format("negative index out of range [0, %d)", n_outputs));
542
+ }
543
+ } else if ((size_t) i >= output_ids.size()) {
544
+ throw std::runtime_error(format("out of range [0, %zu)", output_ids.size()));
545
+ } else {
546
+ j = output_ids[i];
547
+ }
548
+
549
+ if (j < 0) {
550
+ throw std::runtime_error(format("batch.logits[%d] != true", i));
551
+ }
552
+ if (j >= n_outputs) {
553
+ // This should not happen
554
+ throw std::runtime_error(format("corrupt output buffer (j=%d, n_outputs=%d)", j, n_outputs));
555
+ }
556
+
557
+ return embd + j*model.hparams.n_embd;
558
+ } catch (const std::exception & err) {
559
+ LLAMA_LOG_ERROR("%s: invalid embeddings id %d, reason: %s\n", __func__, i, err.what());
560
+ #ifndef NDEBUG
561
+ GGML_ABORT("fatal error");
562
+ #else
563
+ return nullptr;
564
+ #endif
565
+ }
566
+ }
567
+
568
+ float * llama_context::get_embeddings_seq(llama_seq_id seq_id) {
569
+ auto it = embd_seq.find(seq_id);
570
+ if (it == embd_seq.end()) {
571
+ return nullptr;
572
+ }
573
+
574
+ return it->second.data();
575
+ }
576
+
577
+ void llama_context::attach_threadpool(
578
+ ggml_threadpool_t threadpool,
579
+ ggml_threadpool_t threadpool_batch) {
580
+ LLAMA_LOG_DEBUG("%s: call\n", __func__);
581
+
582
+ this->threadpool = threadpool;
583
+ this->threadpool_batch = threadpool_batch ? threadpool_batch : threadpool;
584
+ }
585
+
586
+ void llama_context::detach_threadpool() {
587
+ LLAMA_LOG_DEBUG("%s: call\n", __func__);
588
+
589
+ this->threadpool = nullptr;
590
+ this->threadpool_batch = nullptr;
591
+ }
592
+
593
+ void llama_context::set_n_threads(int32_t n_threads, int32_t n_threads_batch) {
594
+ LLAMA_LOG_DEBUG("%s: n_threads = %d, n_threads_batch = %d\n", __func__, n_threads, n_threads_batch);
595
+
596
+ cparams.n_threads = n_threads;
597
+ cparams.n_threads_batch = n_threads_batch;
598
+ }
599
+
600
+ void llama_context::set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data) {
601
+ LLAMA_LOG_DEBUG("%s: call\n", __func__);
602
+
603
+ this->abort_callback = abort_callback;
604
+ this->abort_callback_data = abort_callback_data;
605
+
606
+ for (auto & backend : backends) {
607
+ auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend.get()));
608
+ auto * set_abort_callback_fn = (ggml_backend_set_abort_callback_t) ggml_backend_reg_get_proc_address(reg, "ggml_backend_set_abort_callback");
609
+ if (set_abort_callback_fn) {
610
+ set_abort_callback_fn(backend.get(), this->abort_callback, this->abort_callback_data);
611
+ }
612
+ }
613
+ }
614
+
615
+ void llama_context::set_embeddings(bool value) {
616
+ LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
617
+
618
+ cparams.embeddings = value;
619
+ }
620
+
621
+ void llama_context::set_causal_attn(bool value) {
622
+ LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
623
+
624
+ cparams.causal_attn = value;
625
+ }
626
+
627
+ void llama_context::set_warmup(bool value) {
628
+ LLAMA_LOG_DEBUG("%s: value = %d\n", __func__, value);
629
+
630
+ cparams.warmup = value;
631
+ }
632
+
633
+ void llama_context::set_adapter_lora(
634
+ llama_adapter_lora * adapter,
635
+ float scale) {
636
+ LLAMA_LOG_DEBUG("%s: adapter = %p, scale = %f\n", __func__, (void *) adapter, scale);
637
+
638
+ loras[adapter] = scale;
639
+ }
640
+
641
+ bool llama_context::rm_adapter_lora(
642
+ llama_adapter_lora * adapter) {
643
+ LLAMA_LOG_DEBUG("%s: adapter = %p\n", __func__, (void *) adapter);
644
+
645
+ auto pos = loras.find(adapter);
646
+ if (pos != loras.end()) {
647
+ loras.erase(pos);
648
+ return true;
649
+ }
650
+
651
+ return false;
652
+ }
653
+
654
+ void llama_context::clear_adapter_lora() {
655
+ LLAMA_LOG_DEBUG("%s: call\n", __func__);
656
+
657
+ loras.clear();
658
+ }
659
+
660
+ bool llama_context::apply_adapter_cvec(
661
+ const float * data,
662
+ size_t len,
663
+ int32_t n_embd,
664
+ int32_t il_start,
665
+ int32_t il_end) {
666
+ LLAMA_LOG_DEBUG("%s: il_start = %d, il_end = %d\n", __func__, il_start, il_end);
667
+
668
+ return cvec.apply(model, data, len, n_embd, il_start, il_end);
669
+ }
670
+
671
+ int llama_context::encode(llama_batch & inp_batch) {
672
+ if (inp_batch.n_tokens == 0) {
673
+ LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__);
674
+ return -1;
675
+ }
676
+
677
+ // temporary allocate memory for the input batch if needed
678
+ // note: during encode, we always pass the full sequence starting from pos = 0
679
+ llama_batch_allocr batch_allocr(inp_batch, inp_batch.pos ? -1 : 0);
680
+
681
+ const llama_batch & batch = batch_allocr.batch;
682
+ const int32_t n_tokens = batch.n_tokens;
683
+
684
+ const auto & hparams = model.hparams;
685
+
686
+ GGML_ASSERT((!batch.token && batch.embd) || (batch.token && !batch.embd)); // NOLINT
687
+
688
+ if (batch.token) {
689
+ for (int32_t i = 0; i < n_tokens; ++i) {
690
+ if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= model.vocab.n_tokens()) {
691
+ LLAMA_LOG_ERROR("%s: invalid token[%d] = %d\n", __func__, i, batch.token[i]);
692
+ return -1;
693
+ }
694
+ }
695
+ }
696
+
697
+ // micro-batching is not possible for non-causal encoding, so we process the batch in a single shot
698
+ GGML_ASSERT(cparams.n_ubatch >= (uint32_t) n_tokens && "encoder requires n_ubatch >= n_tokens");
699
+
700
+ if (t_compute_start_us == 0) {
701
+ t_compute_start_us = ggml_time_us();
702
+ }
703
+
704
+ embd_seq.clear();
705
+
706
+ n_queued_tokens += n_tokens;
707
+
708
+ const int64_t n_embd = hparams.n_embd;
709
+
710
+ llama_sbatch sbatch = llama_sbatch(batch, n_embd, /* simple_split */ true, /* logits_all */ true);
711
+
712
+ const llama_ubatch ubatch = sbatch.split_simple(n_tokens);
713
+
714
+ // reserve output buffer
715
+ if (output_reserve(n_tokens) < n_tokens) {
716
+ LLAMA_LOG_ERROR("%s: could not reserve space for batch with %u outputs\n", __func__, n_tokens);
717
+ return -2;
718
+ };
719
+
720
+ for (int32_t i = 0; i < n_tokens; ++i) {
721
+ output_ids[i] = i;
722
+ }
723
+
724
+ n_outputs = n_tokens;
725
+
726
+ //batch_manager->prepare(ubatch);
727
+
728
+ ggml_backend_sched_reset(sched.get());
729
+ ggml_backend_sched_set_eval_callback(sched.get(), cparams.cb_eval, cparams.cb_eval_user_data);
730
+
731
+ const auto causal_attn_org = cparams.causal_attn;
732
+
733
+ // always use non-causal attention for encoder graphs
734
+ // TODO: this is a tmp solution until we have a proper way to support enc-dec models
735
+ // ref: https://github.com/ggml-org/llama.cpp/pull/12181#issuecomment-2730451223
736
+ cparams.causal_attn = false;
737
+
738
+ auto * gf = graph_init();
739
+ auto res = graph_build(ctx_compute.get(), gf, ubatch, LLM_GRAPH_TYPE_ENCODER);
740
+
741
+ ggml_backend_sched_alloc_graph(sched.get(), gf);
742
+
743
+ res->set_inputs(&ubatch);
744
+
745
+ cparams.causal_attn = causal_attn_org;
746
+
747
+ const auto compute_status = graph_compute(gf, n_tokens > 1);
748
+ switch (compute_status) {
749
+ case GGML_STATUS_SUCCESS:
750
+ break;
751
+ case GGML_STATUS_ABORTED:
752
+ return 2;
753
+ case GGML_STATUS_ALLOC_FAILED:
754
+ return -2;
755
+ case GGML_STATUS_FAILED:
756
+ default:
757
+ return -3;
758
+ }
759
+
760
+ auto * t_embd = res->get_embd_pooled() ? res->get_embd_pooled() : res->get_embd();
761
+
762
+ // extract embeddings
763
+ if (t_embd) {
764
+ ggml_backend_t backend_embd = ggml_backend_sched_get_tensor_backend(sched.get(), t_embd);
765
+ GGML_ASSERT(backend_embd != nullptr);
766
+
767
+ switch (cparams.pooling_type) {
768
+ case LLAMA_POOLING_TYPE_NONE:
769
+ {
770
+ // extract token embeddings
771
+ GGML_ASSERT(embd != nullptr);
772
+
773
+ GGML_ASSERT(n_tokens*n_embd <= (int64_t) embd_size);
774
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd, 0, n_tokens*n_embd*sizeof(float));
775
+ } break;
776
+ case LLAMA_POOLING_TYPE_MEAN:
777
+ case LLAMA_POOLING_TYPE_CLS:
778
+ case LLAMA_POOLING_TYPE_LAST:
779
+ {
780
+ // extract sequence embeddings
781
+ auto & embd_seq_out = embd_seq;
782
+ embd_seq_out.clear();
783
+
784
+ GGML_ASSERT(!ubatch.equal_seqs); // TODO: handle equal splits
785
+
786
+ for (int32_t i = 0; i < n_tokens; i++) {
787
+ const llama_seq_id seq_id = ubatch.seq_id[i][0];
788
+ if (embd_seq_out.find(seq_id) != embd_seq_out.end()) {
789
+ continue;
790
+ }
791
+ embd_seq_out[seq_id].resize(n_embd);
792
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_embd*seq_id)*sizeof(float), n_embd*sizeof(float));
793
+ }
794
+ } break;
795
+ case LLAMA_POOLING_TYPE_RANK:
796
+ {
797
+ // extract the rerank score - a single float per sequence
798
+ auto & embd_seq_out = embd_seq;
799
+
800
+ for (uint32_t s = 0; s < ubatch.n_seqs; ++s) {
801
+ const llama_seq_id seq_id = ubatch.seq_id[s][0];
802
+ if (embd_seq_out.find(seq_id) != embd_seq_out.end()) {
803
+ continue;
804
+ }
805
+ embd_seq_out[seq_id].resize(1);
806
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (seq_id)*sizeof(float), sizeof(float));
807
+ }
808
+ } break;
809
+ case LLAMA_POOLING_TYPE_UNSPECIFIED:
810
+ {
811
+ GGML_ABORT("unknown pooling type");
812
+ }
813
+ }
814
+ }
815
+
816
+ // Reset state for the next token before backend sync, to allow the CPU activities in the reset to
817
+ // overlap with device computation.
818
+ ggml_backend_sched_reset(sched.get());
819
+
820
+ // TODO: hacky solution
821
+ if (model.arch == LLM_ARCH_T5 && t_embd) {
822
+ //cross.t_embd = t_embd;
823
+
824
+ synchronize();
825
+
826
+ cross.n_embd = t_embd->ne[0];
827
+ cross.n_enc = t_embd->ne[1];
828
+ cross.v_embd.resize(cross.n_embd*cross.n_enc);
829
+ memcpy(cross.v_embd.data(), embd, ggml_nbytes(t_embd));
830
+
831
+ // remember the sequence ids used during the encoding - needed for cross attention later
832
+ cross.seq_ids_enc.resize(n_tokens);
833
+ for (int32_t i = 0; i < n_tokens; i++) {
834
+ cross.seq_ids_enc[i].clear();
835
+ for (int s = 0; s < ubatch.n_seq_id[i]; s++) {
836
+ llama_seq_id seq_id = ubatch.seq_id[i][s];
837
+ cross.seq_ids_enc[i].insert(seq_id);
838
+ }
839
+ }
840
+ }
841
+
842
+ return 0;
843
+ }
844
+
845
+ int llama_context::decode(llama_batch & inp_batch) {
846
+ if (!memory) {
847
+ LLAMA_LOG_WARN("%s: cannot decode batches with this context (use llama_encode() instead)\n", __func__);
848
+ return encode(inp_batch);
849
+ }
850
+
851
+ if (inp_batch.n_tokens == 0) {
852
+ LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__);
853
+ return -1;
854
+ }
855
+
856
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
857
+
858
+ // temporary allocate memory for the input batch if needed
859
+ // TODO: this is incorrect for multiple sequences because get_pos_max() is the maximum across all sequences
860
+ llama_batch_allocr batch_allocr(inp_batch, inp_batch.pos ? -1 : kv_self->get_pos_max() + 1);
861
+
862
+ const llama_batch & batch = batch_allocr.batch;
863
+
864
+ const auto & vocab = model.vocab;
865
+ const auto & hparams = model.hparams;
866
+
867
+ const int32_t n_vocab = vocab.n_tokens();
868
+
869
+ const int64_t n_tokens_all = batch.n_tokens;
870
+ const int64_t n_embd = hparams.n_embd;
871
+
872
+ llama_kv_cache_guard kv_guard(kv_self);
873
+
874
+ GGML_ASSERT((!batch.token && batch.embd) || (batch.token && !batch.embd)); // NOLINT
875
+
876
+ if (batch.token) {
877
+ for (int64_t i = 0; i < n_tokens_all; ++i) {
878
+ if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= model.vocab.n_tokens()) {
879
+ LLAMA_LOG_ERROR("%s: invalid token[%" PRId64 "] = %d\n", __func__, i, batch.token[i]);
880
+ throw std::runtime_error("invalid token");
881
+ }
882
+ }
883
+ }
884
+
885
+ GGML_ASSERT(n_tokens_all <= cparams.n_batch);
886
+
887
+ GGML_ASSERT((cparams.causal_attn || cparams.n_ubatch >= n_tokens_all) && "non-causal attention requires n_ubatch >= n_tokens");
888
+
889
+ if (t_compute_start_us == 0) {
890
+ t_compute_start_us = ggml_time_us();
891
+ }
892
+ n_queued_tokens += n_tokens_all;
893
+
894
+ // this indicates we are doing pooled embedding, so we ignore batch.logits and output all tokens
895
+ const bool embd_pooled = cparams.embeddings && cparams.pooling_type != LLAMA_POOLING_TYPE_NONE;
896
+
897
+ embd_seq.clear();
898
+
899
+ int64_t n_outputs_all = 0;
900
+
901
+ // count outputs
902
+ if (batch.logits && !embd_pooled) {
903
+ for (uint32_t i = 0; i < n_tokens_all; ++i) {
904
+ n_outputs_all += batch.logits[i] != 0;
905
+ }
906
+ } else if (embd_pooled) {
907
+ n_outputs_all = n_tokens_all;
908
+ } else {
909
+ // keep last output only
910
+ n_outputs_all = 1;
911
+ }
912
+
913
+ llama_sbatch sbatch = kv_self->sbatch_init(batch, /* logits_all */ n_outputs_all == n_tokens_all);
914
+
915
+ // reserve output buffer
916
+ if (output_reserve(n_outputs_all) < n_outputs_all) {
917
+ LLAMA_LOG_ERROR("%s: could not reserve space for batch with %" PRId64 " outputs\n", __func__, n_outputs_all);
918
+ return -2;
919
+ };
920
+
921
+ // handle any pending defrags/shifts
922
+ kv_self_update();
923
+
924
+ int64_t n_outputs_prev = 0;
925
+
926
+ while (sbatch.n_tokens > 0) {
927
+ llama_ubatch ubatch = kv_self->ubatch_next(sbatch, cparams.n_ubatch, embd_pooled);
928
+
929
+ // count the outputs in this u_batch
930
+ {
931
+ int32_t n_outputs_new = 0;
932
+
933
+ if (n_outputs_all == n_tokens_all) {
934
+ n_outputs_new = ubatch.n_tokens;
935
+ } else {
936
+ GGML_ASSERT(ubatch.output);
937
+ for (uint32_t i = 0; i < ubatch.n_tokens; i++) {
938
+ n_outputs_new += (int32_t) (ubatch.output[i] != 0);
939
+ }
940
+ }
941
+
942
+ // needs to happen before the graph is built
943
+ n_outputs = n_outputs_new;
944
+ }
945
+
946
+ // find KV slot
947
+ if (!kv_self->find_slot(ubatch)) {
948
+ LLAMA_LOG_WARN("%s: failed to find KV cache slot for ubatch of size %d\n", __func__, ubatch.n_tokens);
949
+
950
+ return 1;
951
+ }
952
+
953
+ ggml_backend_sched_reset(sched.get());
954
+ ggml_backend_sched_set_eval_callback(sched.get(), cparams.cb_eval, cparams.cb_eval_user_data);
955
+
956
+ auto * gf = graph_init();
957
+ auto res = graph_build(ctx_compute.get(), gf, ubatch, LLM_GRAPH_TYPE_DECODER);
958
+
959
+ // LLAMA_LOG_INFO("graph build time: %.3f ms (%d nodes, %d leafs)\n", (ggml_time_us() - t_start_us)/1000.0, gf->n_nodes, gf->n_leafs);
960
+
961
+ ggml_backend_sched_alloc_graph(sched.get(), gf);
962
+
963
+ res->set_inputs(&ubatch);
964
+
965
+ const auto compute_status = graph_compute(gf, ubatch.n_tokens > 1);
966
+ if (compute_status != GGML_STATUS_SUCCESS) {
967
+ switch (compute_status) {
968
+ case GGML_STATUS_ABORTED:
969
+ return 2;
970
+ case GGML_STATUS_ALLOC_FAILED:
971
+ return -2;
972
+ case GGML_STATUS_FAILED:
973
+ default:
974
+ return -3;
975
+ }
976
+ }
977
+
978
+ // plot the computation graph in dot format (for debugging purposes)
979
+ //if (n_past%100 == 0) {
980
+ // ggml_graph_dump_dot(gf, NULL, "llama.dot");
981
+ //}
982
+
983
+ auto * t_logits = cparams.embeddings ? nullptr : res->get_logits();
984
+ auto * t_embd = cparams.embeddings ? res->get_embd() : nullptr;
985
+
986
+ if (t_embd && res->get_embd_pooled()) {
987
+ t_embd = res->get_embd_pooled();
988
+ }
989
+
990
+ // extract logits
991
+ if (t_logits && n_outputs > 0) {
992
+ ggml_backend_t backend_res = ggml_backend_sched_get_tensor_backend(sched.get(), t_logits);
993
+ GGML_ASSERT(backend_res != nullptr);
994
+ GGML_ASSERT(logits != nullptr);
995
+
996
+ float * logits_out = logits + n_outputs_prev*n_vocab;
997
+
998
+ if (n_outputs) {
999
+ GGML_ASSERT( n_outputs_prev + n_outputs <= n_outputs_all);
1000
+ GGML_ASSERT((n_outputs_prev + n_outputs)*n_vocab <= (int64_t) logits_size);
1001
+ ggml_backend_tensor_get_async(backend_res, t_logits, logits_out, 0, n_outputs*n_vocab*sizeof(float));
1002
+ }
1003
+ }
1004
+
1005
+ // extract embeddings
1006
+ if (t_embd && n_outputs > 0) {
1007
+ ggml_backend_t backend_embd = ggml_backend_sched_get_tensor_backend(sched.get(), t_embd);
1008
+ GGML_ASSERT(backend_embd != nullptr);
1009
+
1010
+ switch (cparams.pooling_type) {
1011
+ case LLAMA_POOLING_TYPE_NONE:
1012
+ {
1013
+ // extract token embeddings
1014
+ GGML_ASSERT(embd != nullptr);
1015
+ float * embd_out = embd + n_outputs_prev*n_embd;
1016
+
1017
+ if (n_outputs) {
1018
+ GGML_ASSERT( n_outputs_prev + n_outputs <= n_outputs_all);
1019
+ GGML_ASSERT((n_outputs_prev + n_outputs)*n_embd <= (int64_t) embd_size);
1020
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd_out, 0, n_outputs*n_embd*sizeof(float));
1021
+ }
1022
+ } break;
1023
+ case LLAMA_POOLING_TYPE_MEAN:
1024
+ case LLAMA_POOLING_TYPE_CLS:
1025
+ case LLAMA_POOLING_TYPE_LAST:
1026
+ {
1027
+ // extract sequence embeddings (cleared before processing each batch)
1028
+ auto & embd_seq_out = embd_seq;
1029
+
1030
+ for (uint32_t s = 0; s < ubatch.n_seqs; ++s) {
1031
+ const llama_seq_id seq_id = ubatch.seq_id[s][0];
1032
+ if (embd_seq_out.find(seq_id) != embd_seq_out.end()) {
1033
+ continue;
1034
+ }
1035
+ embd_seq_out[seq_id].resize(n_embd);
1036
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (n_embd*seq_id)*sizeof(float), n_embd*sizeof(float));
1037
+ }
1038
+ } break;
1039
+ case LLAMA_POOLING_TYPE_RANK:
1040
+ {
1041
+ // extract the rerank score - a single float per sequence
1042
+ auto & embd_seq_out = embd_seq;
1043
+
1044
+ for (uint32_t s = 0; s < ubatch.n_seqs; ++s) {
1045
+ const llama_seq_id seq_id = ubatch.seq_id[s][0];
1046
+ if (embd_seq_out.find(seq_id) != embd_seq_out.end()) {
1047
+ continue;
1048
+ }
1049
+ embd_seq_out[seq_id].resize(1);
1050
+ ggml_backend_tensor_get_async(backend_embd, t_embd, embd_seq_out[seq_id].data(), (seq_id)*sizeof(float), sizeof(float));
1051
+ }
1052
+ } break;
1053
+ case LLAMA_POOLING_TYPE_UNSPECIFIED:
1054
+ {
1055
+ GGML_ABORT("unknown pooling type");
1056
+ }
1057
+ }
1058
+ }
1059
+
1060
+ n_outputs_prev += n_outputs;
1061
+ }
1062
+
1063
+ // finalize the batch processing
1064
+ kv_guard.commit();
1065
+
1066
+ // set to total number of outputs in the batch, for use in llama_get_logits_ith
1067
+ n_outputs = n_outputs_all;
1068
+
1069
+ // set output mappings
1070
+ {
1071
+ bool sorted_output = true;
1072
+
1073
+ auto & out_ids = sbatch.out_ids;
1074
+
1075
+ GGML_ASSERT(out_ids.size() == (size_t) n_outputs_all);
1076
+
1077
+ for (int64_t i = 0; i < n_outputs_all; ++i) {
1078
+ int64_t out_id = out_ids[i];
1079
+ output_ids[out_id] = i;
1080
+ if (out_id != i) {
1081
+ sorted_output = false;
1082
+ }
1083
+ }
1084
+
1085
+ // make the outputs have the same order they had in the user-provided batch
1086
+ // note: this is mostly relevant for recurrent models atm
1087
+ if (!sorted_output) {
1088
+ const uint32_t n_vocab = model.vocab.n_tokens();
1089
+ const uint32_t n_embd = model.hparams.n_embd;
1090
+
1091
+ GGML_ASSERT((size_t) n_outputs == out_ids.size());
1092
+
1093
+ // TODO: is there something more efficient which also minimizes swaps?
1094
+ // selection sort, to minimize swaps (from https://en.wikipedia.org/wiki/Selection_sort)
1095
+ for (int32_t i = 0; i < n_outputs - 1; ++i) {
1096
+ int32_t j_min = i;
1097
+ for (int32_t j = i + 1; j < n_outputs; ++j) {
1098
+ if (out_ids[j] < out_ids[j_min]) {
1099
+ j_min = j;
1100
+ }
1101
+ }
1102
+ if (j_min == i) { continue; }
1103
+ std::swap(out_ids[i], out_ids[j_min]);
1104
+ if (logits_size > 0) {
1105
+ for (uint32_t k = 0; k < n_vocab; k++) {
1106
+ std::swap(logits[i*n_vocab + k], logits[j_min*n_vocab + k]);
1107
+ }
1108
+ }
1109
+ if (embd_size > 0) {
1110
+ for (uint32_t k = 0; k < n_embd; k++) {
1111
+ std::swap(embd[i*n_embd + k], embd[j_min*n_embd + k]);
1112
+ }
1113
+ }
1114
+ }
1115
+ std::fill(output_ids.begin(), output_ids.end(), -1);
1116
+ for (int32_t i = 0; i < n_outputs; ++i) {
1117
+ output_ids[out_ids[i]] = i;
1118
+ }
1119
+ }
1120
+ }
1121
+
1122
+ // wait for the computation to finish (automatically done when obtaining the model output)
1123
+ //synchronize();
1124
+
1125
+ // decide if we need to defrag the kv cache
1126
+ if (cparams.defrag_thold > 0.0f) {
1127
+ kv_self->defrag_sched(cparams.defrag_thold);
1128
+ }
1129
+
1130
+ // Reset state for the next token before backend sync, to allow the CPU activities in the reset to
1131
+ // overlap with device computation.
1132
+ ggml_backend_sched_reset(sched.get());
1133
+
1134
+ return 0;
1135
+ }
1136
+
1137
+ //
1138
+ // output
1139
+ //
1140
+
1141
+ int32_t llama_context::output_reserve(int32_t n_outputs) {
1142
+ const auto & hparams = model.hparams;
1143
+ const auto & vocab = model.vocab;
1144
+
1145
+ const int64_t n_outputs_max = std::max<int64_t>(n_outputs, n_seq_max());
1146
+
1147
+ const auto n_batch = cparams.n_batch;
1148
+ const auto n_vocab = vocab.n_tokens();
1149
+ const auto n_embd = hparams.n_embd;
1150
+
1151
+ // TODO: use a per-batch flag for logits presence instead
1152
+ bool has_logits = !cparams.embeddings;
1153
+ bool has_embd = cparams.embeddings && (cparams.pooling_type == LLAMA_POOLING_TYPE_NONE);
1154
+
1155
+ // TODO: hacky enc-dec support
1156
+ if (model.arch == LLM_ARCH_T5) {
1157
+ has_logits = true;
1158
+ has_embd = true;
1159
+ }
1160
+
1161
+ logits_size = has_logits ? n_vocab*n_outputs_max : 0;
1162
+ embd_size = has_embd ? n_embd*n_outputs_max : 0;
1163
+
1164
+ if (output_ids.empty()) {
1165
+ // init, never resized afterwards
1166
+ output_ids.resize(n_batch);
1167
+ }
1168
+
1169
+ const size_t prev_size = buf_output ? ggml_backend_buffer_get_size(buf_output.get()) : 0;
1170
+ const size_t new_size = (logits_size + embd_size) * sizeof(float);
1171
+
1172
+ // alloc only when more than the current capacity is required
1173
+ // TODO: also consider shrinking the buffer
1174
+ if (!buf_output || prev_size < new_size) {
1175
+ if (buf_output) {
1176
+ #ifndef NDEBUG
1177
+ // This doesn't happen often, but may be annoying in some cases (like the HellaSwag benchmark)
1178
+ LLAMA_LOG_INFO("%s: reallocating output buffer from size %.02f MiB to %.02f MiB\n", __func__, prev_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
1179
+ #endif
1180
+ buf_output = nullptr;
1181
+ logits = nullptr;
1182
+ embd = nullptr;
1183
+ }
1184
+
1185
+ auto * buft = ggml_backend_cpu_buffer_type();
1186
+ // try to use the host buffer of the device where the output tensor is allocated for faster transfer to system memory
1187
+ auto * output_dev = model.dev_output();
1188
+ auto * output_dev_host_buft = output_dev ? ggml_backend_dev_host_buffer_type(output_dev) : nullptr;
1189
+ if (output_dev_host_buft) {
1190
+ buft = output_dev_host_buft;
1191
+ }
1192
+ buf_output.reset(ggml_backend_buft_alloc_buffer(buft, new_size));
1193
+ if (buf_output == nullptr) {
1194
+ LLAMA_LOG_ERROR("%s: failed to allocate output buffer of size %.2f MiB\n", __func__, new_size / (1024.0 * 1024.0));
1195
+ return 0;
1196
+ }
1197
+ }
1198
+
1199
+ float * output_base = (float *) ggml_backend_buffer_get_base(buf_output.get());
1200
+
1201
+ logits = has_logits ? output_base : nullptr;
1202
+ embd = has_embd ? output_base + logits_size : nullptr;
1203
+
1204
+ // set all ids as invalid (negative)
1205
+ std::fill(output_ids.begin(), output_ids.end(), -1);
1206
+
1207
+ this->n_outputs = 0;
1208
+ this->n_outputs_max = n_outputs_max;
1209
+
1210
+ return n_outputs_max;
1211
+ }
1212
+
1213
+ //
1214
+ // graph
1215
+ //
1216
+
1217
+ int32_t llama_context::graph_max_nodes() const {
1218
+ return std::max<int32_t>(65536, 5*model.n_tensors());
1219
+ }
1220
+
1221
+ ggml_cgraph * llama_context::graph_init() {
1222
+ ggml_init_params params = {
1223
+ /*.mem_size =*/ buf_compute_meta.size(),
1224
+ /*.mem_buffer =*/ buf_compute_meta.data(),
1225
+ /*.no_alloc =*/ true,
1226
+ };
1227
+
1228
+ ctx_compute.reset(ggml_init(params));
1229
+
1230
+ return ggml_new_graph_custom(ctx_compute.get(), graph_max_nodes(), false);
1231
+ }
1232
+
1233
+ llm_graph_result_ptr llama_context::graph_build(
1234
+ ggml_context * ctx,
1235
+ ggml_cgraph * gf,
1236
+ const llama_ubatch & ubatch,
1237
+ llm_graph_type gtype) {
1238
+ return model.build_graph(
1239
+ {
1240
+ /*.ctx =*/ ctx,
1241
+ /*.arch =*/ model.arch,
1242
+ /*.hparams =*/ model.hparams,
1243
+ /*.cparams =*/ cparams,
1244
+ /*.ubatch =*/ ubatch,
1245
+ /*.sched =*/ sched.get(),
1246
+ /*.backend_cpu =*/ backend_cpu,
1247
+ /*.cvec =*/ &cvec,
1248
+ /*.loras =*/ &loras,
1249
+ /*.memory =*/ memory.get(),
1250
+ /*.cross =*/ &cross,
1251
+ /*.n_outputs =*/ n_outputs,
1252
+ /*.cb =*/ graph_get_cb(),
1253
+ }, gf, gtype);
1254
+ }
1255
+
1256
+ ggml_status llama_context::graph_compute(
1257
+ ggml_cgraph * gf,
1258
+ bool batched) {
1259
+ int n_threads = batched ? cparams.n_threads_batch : cparams.n_threads;
1260
+ ggml_threadpool_t tp = batched ? threadpool_batch : threadpool;
1261
+
1262
+ if (backend_cpu != nullptr) {
1263
+ auto * reg = ggml_backend_dev_backend_reg(ggml_backend_get_device(backend_cpu));
1264
+ auto * set_threadpool_fn = (decltype(ggml_backend_cpu_set_threadpool) *) ggml_backend_reg_get_proc_address(reg, "ggml_backend_cpu_set_threadpool");
1265
+ set_threadpool_fn(backend_cpu, tp);
1266
+ }
1267
+
1268
+ // set the number of threads for all the backends
1269
+ for (const auto & set_n_threads_fn : set_n_threads_fns) {
1270
+ set_n_threads_fn.second(set_n_threads_fn.first, n_threads);
1271
+ }
1272
+
1273
+ auto status = ggml_backend_sched_graph_compute_async(sched.get(), gf);
1274
+ if (status != GGML_STATUS_SUCCESS) {
1275
+ LLAMA_LOG_ERROR("%s: ggml_backend_sched_graph_compute_async failed with error %d\n", __func__, status);
1276
+ }
1277
+
1278
+ // fprintf(stderr, "splits: %d\n", ggml_backend_sched_get_n_splits(sched));
1279
+
1280
+ return status;
1281
+ }
1282
+
1283
+ llm_graph_cb llama_context::graph_get_cb() const {
1284
+ return [&](const llama_ubatch & ubatch, ggml_tensor * cur, const char * name, int il) {
1285
+ if (il >= 0) {
1286
+ ggml_format_name(cur, "%s-%d", name, il);
1287
+ } else {
1288
+ ggml_set_name(cur, name);
1289
+ }
1290
+
1291
+ if (!cparams.offload_kqv) {
1292
+ if (strcmp(name, "kqv_merged_cont") == 0) {
1293
+ // all nodes between the KV store and the attention output are run on the CPU
1294
+ ggml_backend_sched_set_tensor_backend(sched.get(), cur, backend_cpu);
1295
+ }
1296
+ }
1297
+
1298
+ // norm may be automatically assigned to the backend of the previous layer, increasing data transfer between backends
1299
+ // FIXME: fix in ggml_backend_sched
1300
+ const bool full_offload = model.params.n_gpu_layers > (int) model.hparams.n_layer;
1301
+ if (ubatch.n_tokens < 32 || full_offload) {
1302
+ if (il != -1 && strcmp(name, "norm") == 0) {
1303
+ const auto & dev_layer = model.dev_layer(il);
1304
+ for (const auto & backend : backends) {
1305
+ if (ggml_backend_get_device(backend.get()) == dev_layer) {
1306
+ if (ggml_backend_supports_op(backend.get(), cur)) {
1307
+ ggml_backend_sched_set_tensor_backend(sched.get(), cur, backend.get());
1308
+ }
1309
+ }
1310
+ }
1311
+ }
1312
+ }
1313
+ };
1314
+ }
1315
+
1316
+ //
1317
+ // state save/load
1318
+ //
1319
+
1320
+ class llama_io_write_dummy : public llama_io_write_i {
1321
+ public:
1322
+ llama_io_write_dummy() = default;
1323
+
1324
+ void write(const void * /* src */, size_t size) override {
1325
+ size_written += size;
1326
+ }
1327
+
1328
+ void write_tensor(const ggml_tensor * /* tensor */, size_t /* offset */, size_t size) override {
1329
+ size_written += size;
1330
+ }
1331
+
1332
+ size_t n_bytes() override {
1333
+ return size_written;
1334
+ }
1335
+
1336
+ private:
1337
+ size_t size_written = 0;
1338
+ };
1339
+
1340
+ class llama_io_write_buffer : public llama_io_write_i {
1341
+ public:
1342
+ llama_io_write_buffer(
1343
+ uint8_t * p, size_t len) : ptr(p), buf_size(len) {}
1344
+
1345
+ void write(const void * src, size_t size) override {
1346
+ if (size > buf_size) {
1347
+ throw std::runtime_error("unexpectedly reached end of buffer");
1348
+ }
1349
+ memcpy(ptr, src, size);
1350
+ ptr += size;
1351
+ size_written += size;
1352
+ buf_size -= size;
1353
+ }
1354
+
1355
+ void write_tensor(const ggml_tensor * tensor, size_t offset, size_t size) override {
1356
+ if (size > buf_size) {
1357
+ throw std::runtime_error("unexpectedly reached end of buffer");
1358
+ }
1359
+ ggml_backend_tensor_get(tensor, ptr, offset, size);
1360
+ ptr += size;
1361
+ size_written += size;
1362
+ buf_size -= size;
1363
+ }
1364
+
1365
+ size_t n_bytes() override {
1366
+ return size_written;
1367
+ }
1368
+
1369
+ private:
1370
+ uint8_t * ptr;
1371
+ size_t buf_size = 0;
1372
+ size_t size_written = 0;
1373
+ };
1374
+
1375
+ class llama_io_read_buffer : public llama_io_read_i {
1376
+ public:
1377
+ llama_io_read_buffer(const uint8_t * p, size_t len) : ptr(p), buf_size(len) {}
1378
+
1379
+ const uint8_t * read(size_t size) override {
1380
+ const uint8_t * base_ptr = ptr;
1381
+ if (size > buf_size) {
1382
+ throw std::runtime_error("unexpectedly reached end of buffer");
1383
+ }
1384
+ ptr += size;
1385
+ size_read += size;
1386
+ buf_size -= size;
1387
+ return base_ptr;
1388
+ }
1389
+
1390
+ void read_to(void * dst, size_t size) override {
1391
+ memcpy(dst, read(size), size);
1392
+ }
1393
+
1394
+ size_t n_bytes() override {
1395
+ return size_read;
1396
+ }
1397
+
1398
+ private:
1399
+ const uint8_t * ptr;
1400
+ size_t buf_size = 0;
1401
+ size_t size_read = 0;
1402
+ };
1403
+
1404
+ class llama_io_write_file : public llama_io_write_i {
1405
+ public:
1406
+ llama_io_write_file(llama_file * f) : file(f) {}
1407
+
1408
+ void write(const void * src, size_t size) override {
1409
+ file->write_raw(src, size);
1410
+ size_written += size;
1411
+ }
1412
+
1413
+ void write_tensor(const ggml_tensor * tensor, size_t offset, size_t size) override {
1414
+ temp_buffer.resize(size);
1415
+ ggml_backend_tensor_get(tensor, temp_buffer.data(), offset, size);
1416
+ write(temp_buffer.data(), temp_buffer.size());
1417
+ }
1418
+
1419
+ size_t n_bytes() override {
1420
+ return size_written;
1421
+ }
1422
+
1423
+ private:
1424
+ llama_file * file;
1425
+ size_t size_written = 0;
1426
+ std::vector<uint8_t> temp_buffer;
1427
+ };
1428
+
1429
+ class llama_io_read_file : public llama_io_read_i {
1430
+ public:
1431
+ llama_io_read_file(llama_file * f) : file(f) {}
1432
+
1433
+ void read_to(void * dst, size_t size) override {
1434
+ file->read_raw(dst, size);
1435
+ size_read += size;
1436
+ }
1437
+
1438
+ const uint8_t * read(size_t size) override {
1439
+ temp_buffer.resize(size);
1440
+ read_to(temp_buffer.data(), size);
1441
+ return temp_buffer.data();
1442
+ }
1443
+
1444
+ size_t n_bytes() override {
1445
+ return size_read;
1446
+ }
1447
+
1448
+ private:
1449
+ llama_file * file;
1450
+ size_t size_read = 0;
1451
+ std::vector<uint8_t> temp_buffer;
1452
+ };
1453
+
1454
+ size_t llama_context::state_get_size() {
1455
+ llama_io_write_dummy io;
1456
+ try {
1457
+ return state_write_data(io);
1458
+ } catch (const std::exception & err) {
1459
+ LLAMA_LOG_ERROR("%s: error getting state size: %s\n", __func__, err.what());
1460
+ return 0;
1461
+ }
1462
+ }
1463
+
1464
+ size_t llama_context::state_get_data(uint8_t * dst, size_t size) {
1465
+ llama_io_write_buffer io(dst, size);
1466
+ try {
1467
+ return state_write_data(io);
1468
+ } catch (const std::exception & err) {
1469
+ LLAMA_LOG_ERROR("%s: error saving state: %s\n", __func__, err.what());
1470
+ return 0;
1471
+ }
1472
+ }
1473
+
1474
+ size_t llama_context::state_set_data(const uint8_t * src, size_t size) {
1475
+ llama_io_read_buffer io(src, size);
1476
+ try {
1477
+ return state_read_data(io);
1478
+ } catch (const std::exception & err) {
1479
+ LLAMA_LOG_ERROR("%s: error loading state: %s\n", __func__, err.what());
1480
+ return 0;
1481
+ }
1482
+ }
1483
+
1484
+ size_t llama_context::state_seq_get_size(llama_seq_id seq_id) {
1485
+ llama_io_write_dummy io;
1486
+ try {
1487
+ return state_seq_write_data(io, seq_id);
1488
+ } catch (const std::exception & err) {
1489
+ LLAMA_LOG_ERROR("%s: error getting state size: %s\n", __func__, err.what());
1490
+ return 0;
1491
+ }
1492
+ }
1493
+
1494
+ size_t llama_context::state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size) {
1495
+ llama_io_write_buffer io(dst, size);
1496
+ try {
1497
+ return state_seq_write_data(io, seq_id);
1498
+ } catch (const std::exception & err) {
1499
+ LLAMA_LOG_ERROR("%s: error saving state: %s\n", __func__, err.what());
1500
+ return 0;
1501
+ }
1502
+ }
1503
+
1504
+ size_t llama_context::state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size) {
1505
+ llama_io_read_buffer io(src, size);
1506
+ try {
1507
+ return state_seq_read_data(io, seq_id);
1508
+ } catch (const std::exception & err) {
1509
+ LLAMA_LOG_ERROR("%s: error loading state: %s\n", __func__, err.what());
1510
+ return 0;
1511
+ }
1512
+ }
1513
+
1514
+ bool llama_context::state_load_file(const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
1515
+ llama_file file(filepath, "rb");
1516
+
1517
+ // sanity checks
1518
+ {
1519
+ const uint32_t magic = file.read_u32();
1520
+ const uint32_t version = file.read_u32();
1521
+
1522
+ if (magic != LLAMA_SESSION_MAGIC || version != LLAMA_SESSION_VERSION) {
1523
+ LLAMA_LOG_ERROR("%s: unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version);
1524
+ return false;
1525
+ }
1526
+ }
1527
+
1528
+ // load the prompt
1529
+ {
1530
+ const uint32_t n_token_count = file.read_u32();
1531
+
1532
+ if (n_token_count > n_token_capacity) {
1533
+ LLAMA_LOG_ERROR("%s: token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
1534
+ return false;
1535
+ }
1536
+
1537
+ file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
1538
+ *n_token_count_out = n_token_count;
1539
+ }
1540
+
1541
+ // restore the context state
1542
+ {
1543
+ const size_t n_state_size_cur = file.size() - file.tell();
1544
+
1545
+ llama_io_read_file io( &file);
1546
+ const size_t n_read = state_read_data(io);
1547
+
1548
+ if (n_read != n_state_size_cur) {
1549
+ LLAMA_LOG_ERROR("%s: did not read all of the session file data! size %zu, got %zu\n", __func__, n_state_size_cur, n_read);
1550
+ return false;
1551
+ }
1552
+ }
1553
+
1554
+ return true;
1555
+ }
1556
+
1557
+ bool llama_context::state_save_file(const char * filepath, const llama_token * tokens, size_t n_token_count) {
1558
+ llama_file file(filepath, "wb");
1559
+
1560
+ file.write_u32(LLAMA_SESSION_MAGIC);
1561
+ file.write_u32(LLAMA_SESSION_VERSION);
1562
+
1563
+ // save the prompt
1564
+ file.write_u32((uint32_t) n_token_count);
1565
+ file.write_raw(tokens, sizeof(llama_token) * n_token_count);
1566
+
1567
+ // save the context state using stream saving
1568
+ llama_io_write_file io(&file);
1569
+ state_write_data(io);
1570
+
1571
+ return true;
1572
+ }
1573
+
1574
+ size_t llama_context::state_seq_load_file(llama_seq_id seq_id, const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
1575
+ llama_file file(filepath, "rb");
1576
+
1577
+ // version checks
1578
+ {
1579
+ const uint32_t magic = file.read_u32();
1580
+ const uint32_t version = file.read_u32();
1581
+
1582
+ if (magic != LLAMA_STATE_SEQ_MAGIC || version != LLAMA_STATE_SEQ_VERSION) {
1583
+ LLAMA_LOG_ERROR("%s: unknown (magic, version) for sequence state file: %08x, %08x\n", __func__, magic, version);
1584
+ return 0;
1585
+ }
1586
+ }
1587
+
1588
+ // load the prompt
1589
+ {
1590
+ const uint32_t n_token_count = file.read_u32();
1591
+
1592
+ if (n_token_count > n_token_capacity) {
1593
+ LLAMA_LOG_ERROR("%s: token count in sequence state file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity);
1594
+ return 0;
1595
+ }
1596
+
1597
+ file.read_raw(tokens_out, sizeof(llama_token) * n_token_count);
1598
+ *n_token_count_out = n_token_count;
1599
+ }
1600
+
1601
+ // restore the context state
1602
+ {
1603
+ const size_t state_size = file.size() - file.tell();
1604
+ llama_io_read_file io(&file);
1605
+ const size_t nread = state_seq_read_data(io, seq_id);
1606
+ if (!nread) {
1607
+ LLAMA_LOG_ERROR("%s: failed to restore sequence state\n", __func__);
1608
+ return 0;
1609
+ }
1610
+ GGML_ASSERT(nread <= state_size);
1611
+ GGML_ASSERT(nread + sizeof(uint32_t) * 3 + sizeof(llama_token) * *n_token_count_out == file.tell());
1612
+ }
1613
+
1614
+ return file.tell();
1615
+ }
1616
+
1617
+ size_t llama_context::state_seq_save_file(llama_seq_id seq_id, const char * filepath, const llama_token * tokens, size_t n_token_count) {
1618
+ llama_file file(filepath, "wb");
1619
+
1620
+ file.write_u32(LLAMA_STATE_SEQ_MAGIC);
1621
+ file.write_u32(LLAMA_STATE_SEQ_VERSION);
1622
+
1623
+ // save the prompt
1624
+ file.write_u32((uint32_t) n_token_count);
1625
+ file.write_raw(tokens, sizeof(llama_token) * n_token_count);
1626
+
1627
+ // save the context state using stream saving
1628
+ llama_io_write_file io(&file);
1629
+ state_seq_write_data(io, seq_id);
1630
+
1631
+ const size_t res = file.tell();
1632
+ GGML_ASSERT(res == sizeof(uint32_t) * 3 + sizeof(llama_token) * n_token_count + io.n_bytes());
1633
+
1634
+ return res;
1635
+ }
1636
+
1637
+ size_t llama_context::state_write_data(llama_io_write_i & io) {
1638
+ LLAMA_LOG_DEBUG("%s: writing state\n", __func__);
1639
+
1640
+ // write model info
1641
+ {
1642
+ LLAMA_LOG_DEBUG("%s: - writing model info\n", __func__);
1643
+
1644
+ const std::string arch_str = llm_arch_name(model.arch);
1645
+ io.write_string(arch_str);
1646
+ // TODO: add more model-specific info which should prevent loading the session file if not identical
1647
+ }
1648
+
1649
+ // write output ids
1650
+ {
1651
+ LLAMA_LOG_DEBUG("%s: - writing output ids\n", __func__);
1652
+
1653
+ const auto n_outputs = this->n_outputs;
1654
+ const auto & output_ids = this->output_ids;
1655
+
1656
+ std::vector<int32_t> w_output_pos;
1657
+
1658
+ GGML_ASSERT(n_outputs <= n_outputs_max);
1659
+
1660
+ w_output_pos.resize(n_outputs);
1661
+
1662
+ // build a more compact representation of the output ids
1663
+ for (size_t i = 0; i < n_batch(); ++i) {
1664
+ // map an output id to a position in the batch
1665
+ int32_t pos = output_ids[i];
1666
+ if (pos >= 0) {
1667
+ GGML_ASSERT(pos < n_outputs);
1668
+ w_output_pos[pos] = i;
1669
+ }
1670
+ }
1671
+
1672
+ io.write(&n_outputs, sizeof(n_outputs));
1673
+
1674
+ if (n_outputs) {
1675
+ io.write(w_output_pos.data(), n_outputs * sizeof(int32_t));
1676
+ }
1677
+ }
1678
+
1679
+ // write logits
1680
+ {
1681
+ LLAMA_LOG_DEBUG("%s: - writing logits\n", __func__);
1682
+
1683
+ const uint64_t logits_size = std::min((uint64_t) this->logits_size, (uint64_t) n_outputs * model.vocab.n_tokens());
1684
+
1685
+ io.write(&logits_size, sizeof(logits_size));
1686
+
1687
+ if (logits_size) {
1688
+ io.write(logits, logits_size * sizeof(float));
1689
+ }
1690
+ }
1691
+
1692
+ // write embeddings
1693
+ {
1694
+ LLAMA_LOG_DEBUG("%s: - writing embeddings\n", __func__);
1695
+
1696
+ const uint64_t embd_size = std::min((uint64_t) this->embd_size, (uint64_t) n_outputs * model.hparams.n_embd);
1697
+
1698
+ io.write(&embd_size, sizeof(embd_size));
1699
+
1700
+ if (embd_size) {
1701
+ io.write(embd, embd_size * sizeof(float));
1702
+ }
1703
+ }
1704
+
1705
+ LLAMA_LOG_DEBUG("%s: - writing KV self\n", __func__);
1706
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
1707
+
1708
+ kv_self->state_write(io);
1709
+
1710
+ return io.n_bytes();
1711
+ }
1712
+
1713
+ size_t llama_context::state_read_data(llama_io_read_i & io) {
1714
+ LLAMA_LOG_DEBUG("%s: reading state\n", __func__);
1715
+
1716
+ // read model info
1717
+ {
1718
+ LLAMA_LOG_DEBUG("%s: - reading model info\n", __func__);
1719
+
1720
+ const std::string cur_arch_str = llm_arch_name(model.arch);
1721
+
1722
+ std::string arch_str;
1723
+ io.read_string(arch_str);
1724
+ if (cur_arch_str != arch_str) {
1725
+ throw std::runtime_error(format("wrong model arch: '%s' instead of '%s'", arch_str.c_str(), cur_arch_str.c_str()));
1726
+ }
1727
+ // TODO: add more info which needs to be identical but which is not verified otherwise
1728
+ }
1729
+
1730
+ // read output ids
1731
+ {
1732
+ LLAMA_LOG_DEBUG("%s: - reading output ids\n", __func__);
1733
+
1734
+ auto n_outputs = this->n_outputs;
1735
+ io.read_to(&n_outputs, sizeof(n_outputs));
1736
+
1737
+ if (n_outputs > output_reserve(n_outputs)) {
1738
+ throw std::runtime_error("could not reserve outputs");
1739
+ }
1740
+
1741
+ std::vector<int32_t> output_pos;
1742
+
1743
+ if (n_outputs) {
1744
+ output_pos.resize(n_outputs);
1745
+ io.read_to(output_pos.data(), n_outputs * sizeof(int32_t));
1746
+
1747
+ for (int32_t i = 0; i < (int32_t) output_pos.size(); ++i) {
1748
+ int32_t id = output_pos[i];
1749
+ if ((uint32_t) id >= n_batch()) {
1750
+ throw std::runtime_error(format("invalid output id, %d does not fit in batch size of %u", id, n_batch()));
1751
+ }
1752
+ this->output_ids[id] = i;
1753
+ }
1754
+
1755
+ this->n_outputs = n_outputs;
1756
+ }
1757
+ }
1758
+
1759
+ // read logits
1760
+ {
1761
+ LLAMA_LOG_DEBUG("%s: - reading logits\n", __func__);
1762
+
1763
+ uint64_t logits_size;
1764
+ io.read_to(&logits_size, sizeof(logits_size));
1765
+
1766
+ if (this->logits_size < logits_size) {
1767
+ throw std::runtime_error("logits buffer too small");
1768
+ }
1769
+
1770
+ if (logits_size) {
1771
+ io.read_to(this->logits, logits_size * sizeof(float));
1772
+ }
1773
+ }
1774
+
1775
+ // read embeddings
1776
+ {
1777
+ LLAMA_LOG_DEBUG("%s: - reading embeddings\n", __func__);
1778
+
1779
+ uint64_t embd_size;
1780
+ io.read_to(&embd_size, sizeof(embd_size));
1781
+
1782
+ if (this->embd_size < embd_size) {
1783
+ throw std::runtime_error("embeddings buffer too small");
1784
+ }
1785
+
1786
+ if (embd_size) {
1787
+ io.read_to(this->embd, embd_size * sizeof(float));
1788
+ }
1789
+ }
1790
+
1791
+ LLAMA_LOG_DEBUG("%s: - reading KV self\n", __func__);
1792
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
1793
+
1794
+ kv_self->state_read(io);
1795
+
1796
+ return io.n_bytes();
1797
+ }
1798
+
1799
+ size_t llama_context::state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id) {
1800
+ GGML_UNUSED(seq_id);
1801
+
1802
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
1803
+
1804
+ kv_self->state_write(io, seq_id);
1805
+
1806
+ return io.n_bytes();
1807
+ }
1808
+
1809
+ size_t llama_context::state_seq_read_data(llama_io_read_i & io, llama_seq_id seq_id) {
1810
+ GGML_UNUSED(seq_id);
1811
+
1812
+ llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());
1813
+
1814
+ kv_self->state_read(io, seq_id);
1815
+
1816
+ return io.n_bytes();
1817
+ }
1818
+
1819
+ //
1820
+ // perf
1821
+ //
1822
+
1823
+ llama_perf_context_data llama_context::perf_get_data() const {
1824
+ llama_perf_context_data data = {};
1825
+
1826
+ data.t_start_ms = 1e-3 * t_start_us;
1827
+ data.t_load_ms = 1e-3 * t_load_us;
1828
+ data.t_p_eval_ms = 1e-3 * t_p_eval_us;
1829
+ data.t_eval_ms = 1e-3 * t_eval_us;
1830
+ data.n_p_eval = std::max(1, n_p_eval);
1831
+ data.n_eval = std::max(1, n_eval);
1832
+
1833
+ return data;
1834
+ }
1835
+
1836
+ void llama_context::perf_reset() {
1837
+ t_start_us = ggml_time_us();
1838
+ t_eval_us = n_eval = 0;
1839
+ t_p_eval_us = n_p_eval = 0;
1840
+ }
1841
+
1842
+ //
1843
+ // interface implementation
1844
+ //
1845
+
1846
+ llama_context_params llama_context_default_params() {
1847
+ llama_context_params result = {
1848
+ /*.n_ctx =*/ 512,
1849
+ /*.n_batch =*/ 2048,
1850
+ /*.n_ubatch =*/ 512,
1851
+ /*.n_seq_max =*/ 1,
1852
+ /*.n_threads =*/ GGML_DEFAULT_N_THREADS, // TODO: better default
1853
+ /*.n_threads_batch =*/ GGML_DEFAULT_N_THREADS,
1854
+ /*.rope_scaling_type =*/ LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED,
1855
+ /*.pooling_type =*/ LLAMA_POOLING_TYPE_UNSPECIFIED,
1856
+ /*.attention_type =*/ LLAMA_ATTENTION_TYPE_UNSPECIFIED,
1857
+ /*.rope_freq_base =*/ 0.0f,
1858
+ /*.rope_freq_scale =*/ 0.0f,
1859
+ /*.yarn_ext_factor =*/ -1.0f,
1860
+ /*.yarn_attn_factor =*/ 1.0f,
1861
+ /*.yarn_beta_fast =*/ 32.0f,
1862
+ /*.yarn_beta_slow =*/ 1.0f,
1863
+ /*.yarn_orig_ctx =*/ 0,
1864
+ /*.defrag_thold =*/ -1.0f,
1865
+ /*.cb_eval =*/ nullptr,
1866
+ /*.cb_eval_user_data =*/ nullptr,
1867
+ /*.type_k =*/ GGML_TYPE_F16,
1868
+ /*.type_v =*/ GGML_TYPE_F16,
1869
+ /*.abort_callback =*/ nullptr,
1870
+ /*.abort_callback_data =*/ nullptr,
1871
+ /*.embeddings =*/ false,
1872
+ /*.offload_kqv =*/ true,
1873
+ /*.flash_attn =*/ false,
1874
+ /*.no_perf =*/ true,
1875
+ /*.op_offload =*/ true,
1876
+ };
1877
+
1878
+ return result;
1879
+ }
1880
+
1881
+ llama_context * llama_init_from_model(
1882
+ llama_model * model,
1883
+ llama_context_params params) {
1884
+ if (!model) {
1885
+ LLAMA_LOG_ERROR("%s: model cannot be NULL\n", __func__);
1886
+ return nullptr;
1887
+ }
1888
+
1889
+ if (params.n_batch == 0 && params.n_ubatch == 0) {
1890
+ LLAMA_LOG_ERROR("%s: n_batch and n_ubatch cannot both be zero\n", __func__);
1891
+ return nullptr;
1892
+ }
1893
+
1894
+ if (params.n_ctx == 0 && model->hparams.n_ctx_train == 0) {
1895
+ LLAMA_LOG_ERROR("%s: n_ctx and model->hparams.n_ctx_train cannot both be zero\n", __func__);
1896
+ return nullptr;
1897
+ }
1898
+
1899
+ if (params.flash_attn && model->arch == LLM_ARCH_GROK) {
1900
+ LLAMA_LOG_WARN("%s: flash_attn is not compatible with Grok - forcing off\n", __func__);
1901
+ params.flash_attn = false;
1902
+ }
1903
+
1904
+ if (ggml_is_quantized(params.type_v) && !params.flash_attn) {
1905
+ LLAMA_LOG_ERROR("%s: V cache quantization requires flash_attn\n", __func__);
1906
+ return nullptr;
1907
+ }
1908
+
1909
+ try {
1910
+ auto * ctx = new llama_context(*model, params);
1911
+ return ctx;
1912
+ } catch (const std::exception & err) {
1913
+ LLAMA_LOG_ERROR("%s: failed to initialize the context: %s\n", __func__, err.what());
1914
+ }
1915
+
1916
+ return nullptr;
1917
+ }
1918
+
1919
+ // deprecated
1920
+ llama_context * llama_new_context_with_model(
1921
+ llama_model * model,
1922
+ llama_context_params params) {
1923
+ return llama_init_from_model(model, params);
1924
+ }
1925
+
1926
+ void llama_free(llama_context * ctx) {
1927
+ delete ctx;
1928
+ }
1929
+
1930
+ uint32_t llama_n_ctx(const llama_context * ctx) {
1931
+ return ctx->n_ctx();
1932
+ }
1933
+
1934
+ uint32_t llama_n_batch(const llama_context * ctx) {
1935
+ return ctx->n_batch();
1936
+ }
1937
+
1938
+ uint32_t llama_n_ubatch(const llama_context * ctx) {
1939
+ return ctx->n_ubatch();
1940
+ }
1941
+
1942
+ uint32_t llama_n_seq_max(const llama_context * ctx) {
1943
+ return ctx->n_seq_max();
1944
+ }
1945
+
1946
+ const llama_model * llama_get_model(const llama_context * ctx) {
1947
+ return &ctx->get_model();
1948
+ }
1949
+
1950
+ llama_kv_cache * llama_get_kv_self(llama_context * ctx) {
1951
+ return ctx->get_kv_self();
1952
+ }
1953
+
1954
+ void llama_kv_self_update(llama_context * ctx) {
1955
+ ctx->kv_self_update();
1956
+ }
1957
+
1958
+ enum llama_pooling_type llama_pooling_type(const llama_context * ctx) {
1959
+ return ctx->pooling_type();
1960
+ }
1961
+
1962
+ void llama_attach_threadpool(
1963
+ llama_context * ctx,
1964
+ ggml_threadpool_t threadpool,
1965
+ ggml_threadpool_t threadpool_batch) {
1966
+ ctx->attach_threadpool(threadpool, threadpool_batch);
1967
+ }
1968
+
1969
+ void llama_detach_threadpool(llama_context * ctx) {
1970
+ ctx->detach_threadpool();
1971
+ }
1972
+
1973
+ void llama_set_n_threads(llama_context * ctx, int32_t n_threads, int32_t n_threads_batch) {
1974
+ ctx->set_n_threads(n_threads, n_threads_batch);
1975
+ }
1976
+
1977
+ int32_t llama_n_threads(llama_context * ctx) {
1978
+ return ctx->n_threads();
1979
+ }
1980
+
1981
+ int32_t llama_n_threads_batch(llama_context * ctx) {
1982
+ return ctx->n_threads_batch();
1983
+ }
1984
+
1985
+ void llama_set_abort_callback(llama_context * ctx, bool (*abort_callback)(void * data), void * abort_callback_data) {
1986
+ ctx->set_abort_callback(abort_callback, abort_callback_data);
1987
+ }
1988
+
1989
+ void llama_set_embeddings(llama_context * ctx, bool embeddings) {
1990
+ ctx->set_embeddings(embeddings);
1991
+ }
1992
+
1993
+ void llama_set_causal_attn(llama_context * ctx, bool causal_attn) {
1994
+ ctx->set_causal_attn(causal_attn);
1995
+ }
1996
+
1997
+ void llama_set_warmup(llama_context * ctx, bool warmup) {
1998
+ ctx->set_warmup(warmup);
1999
+ }
2000
+
2001
+ void llama_synchronize(llama_context * ctx) {
2002
+ ctx->synchronize();
2003
+ }
2004
+
2005
+ float * llama_get_logits(llama_context * ctx) {
2006
+ ctx->synchronize();
2007
+
2008
+ return ctx->get_logits();
2009
+ }
2010
+
2011
+ float * llama_get_logits_ith(llama_context * ctx, int32_t i) {
2012
+ ctx->synchronize();
2013
+
2014
+ return ctx->get_logits_ith(i);
2015
+ }
2016
+
2017
+ float * llama_get_embeddings(llama_context * ctx) {
2018
+ ctx->synchronize();
2019
+
2020
+ return ctx->get_embeddings();
2021
+ }
2022
+
2023
+ float * llama_get_embeddings_ith(llama_context * ctx, int32_t i) {
2024
+ ctx->synchronize();
2025
+
2026
+ return ctx->get_embeddings_ith(i);
2027
+ }
2028
+
2029
+ float * llama_get_embeddings_seq(llama_context * ctx, llama_seq_id seq_id) {
2030
+ ctx->synchronize();
2031
+
2032
+ return ctx->get_embeddings_seq(seq_id);
2033
+ }
2034
+
2035
+ // llama adapter API
2036
+
2037
+ int32_t llama_set_adapter_lora(
2038
+ llama_context * ctx,
2039
+ llama_adapter_lora * adapter,
2040
+ float scale) {
2041
+ ctx->set_adapter_lora(adapter, scale);
2042
+
2043
+ return 0;
2044
+ }
2045
+
2046
+ int32_t llama_rm_adapter_lora(
2047
+ llama_context * ctx,
2048
+ llama_adapter_lora * adapter) {
2049
+ bool res = ctx->rm_adapter_lora(adapter);
2050
+
2051
+ return res ? 0 : -1;
2052
+ }
2053
+
2054
+ void llama_clear_adapter_lora(llama_context * ctx) {
2055
+ ctx->clear_adapter_lora();
2056
+ }
2057
+
2058
+ int32_t llama_apply_adapter_cvec(
2059
+ llama_context * ctx,
2060
+ const float * data,
2061
+ size_t len,
2062
+ int32_t n_embd,
2063
+ int32_t il_start,
2064
+ int32_t il_end) {
2065
+ bool res = ctx->apply_adapter_cvec(data, len, n_embd, il_start, il_end);
2066
+
2067
+ return res ? 0 : -1;
2068
+ }
2069
+
2070
+ //
2071
+ // kv cache view
2072
+ //
2073
+
2074
+ llama_kv_cache_view llama_kv_cache_view_init(const llama_context * ctx, int32_t n_seq_max) {
2075
+ const auto * kv = ctx->get_kv_self();
2076
+ if (kv == nullptr) {
2077
+ LLAMA_LOG_WARN("%s: the context does not have a KV cache\n", __func__);
2078
+ return {};
2079
+ }
2080
+
2081
+ return llama_kv_cache_view_init(*kv, n_seq_max);
2082
+ }
2083
+
2084
+ void llama_kv_cache_view_update(const llama_context * ctx, llama_kv_cache_view * view) {
2085
+ const auto * kv = ctx->get_kv_self();
2086
+ if (kv == nullptr) {
2087
+ LLAMA_LOG_WARN("%s: the context does not have a KV cache\n", __func__);
2088
+ return;
2089
+ }
2090
+
2091
+ llama_kv_cache_view_update(view, kv);
2092
+ }
2093
+
2094
+ //
2095
+ // kv cache
2096
+ //
2097
+
2098
+ // deprecated
2099
+ int32_t llama_get_kv_cache_token_count(const llama_context * ctx) {
2100
+ return llama_kv_self_n_tokens(ctx);
2101
+ }
2102
+
2103
+ int32_t llama_kv_self_n_tokens(const llama_context * ctx) {
2104
+ const auto * kv = ctx->get_kv_self();
2105
+ if (!kv) {
2106
+ return 0;
2107
+ }
2108
+
2109
+ return kv->get_n_tokens();
2110
+ }
2111
+
2112
+ // deprecated
2113
+ int32_t llama_get_kv_cache_used_cells(const llama_context * ctx) {
2114
+ return llama_kv_self_used_cells(ctx);
2115
+ }
2116
+
2117
+ int32_t llama_kv_self_used_cells(const llama_context * ctx) {
2118
+ const auto * kv = ctx->get_kv_self();
2119
+ if (!kv) {
2120
+ return 0;
2121
+ }
2122
+
2123
+ return kv->get_used_cells();
2124
+ }
2125
+
2126
+ // deprecated
2127
+ void llama_kv_cache_clear(llama_context * ctx) {
2128
+ llama_kv_self_clear(ctx);
2129
+ }
2130
+
2131
+ void llama_kv_self_clear(llama_context * ctx) {
2132
+ auto * kv = ctx->get_kv_self();
2133
+ if (!kv) {
2134
+ return;
2135
+ }
2136
+
2137
+ kv->clear();
2138
+ }
2139
+
2140
+ // deprecated
2141
+ bool llama_kv_cache_seq_rm(
2142
+ llama_context * ctx,
2143
+ llama_seq_id seq_id,
2144
+ llama_pos p0,
2145
+ llama_pos p1) {
2146
+ return llama_kv_self_seq_rm(ctx, seq_id, p0, p1);
2147
+ }
2148
+
2149
+ bool llama_kv_self_seq_rm(
2150
+ llama_context * ctx,
2151
+ llama_seq_id seq_id,
2152
+ llama_pos p0,
2153
+ llama_pos p1) {
2154
+ auto * kv = ctx->get_kv_self();
2155
+ if (!kv) {
2156
+ return true;
2157
+ }
2158
+
2159
+ return kv->seq_rm(seq_id, p0, p1);
2160
+ }
2161
+
2162
+ // deprecated
2163
+ void llama_kv_cache_seq_cp(
2164
+ llama_context * ctx,
2165
+ llama_seq_id seq_id_src,
2166
+ llama_seq_id seq_id_dst,
2167
+ llama_pos p0,
2168
+ llama_pos p1) {
2169
+ llama_kv_self_seq_cp(ctx, seq_id_src, seq_id_dst, p0, p1);
2170
+ }
2171
+
2172
+ void llama_kv_self_seq_cp(
2173
+ llama_context * ctx,
2174
+ llama_seq_id seq_id_src,
2175
+ llama_seq_id seq_id_dst,
2176
+ llama_pos p0,
2177
+ llama_pos p1) {
2178
+ auto * kv = ctx->get_kv_self();
2179
+ if (!kv) {
2180
+ return;
2181
+ }
2182
+
2183
+ kv->seq_cp(seq_id_src, seq_id_dst, p0, p1);
2184
+ }
2185
+
2186
+ // deprecated
2187
+ void llama_kv_cache_seq_keep(
2188
+ llama_context * ctx,
2189
+ llama_seq_id seq_id) {
2190
+ llama_kv_self_seq_keep(ctx, seq_id);
2191
+ }
2192
+
2193
+ void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
2194
+ auto * kv = ctx->get_kv_self();
2195
+ if (!kv) {
2196
+ return;
2197
+ }
2198
+
2199
+ kv->seq_keep(seq_id);
2200
+ }
2201
+
2202
+ // deprecated
2203
+ void llama_kv_cache_seq_add(
2204
+ llama_context * ctx,
2205
+ llama_seq_id seq_id,
2206
+ llama_pos p0,
2207
+ llama_pos p1,
2208
+ llama_pos delta) {
2209
+ llama_kv_self_seq_add(ctx, seq_id, p0, p1, delta);
2210
+ }
2211
+
2212
+ void llama_kv_self_seq_add(
2213
+ llama_context * ctx,
2214
+ llama_seq_id seq_id,
2215
+ llama_pos p0,
2216
+ llama_pos p1,
2217
+ llama_pos delta) {
2218
+ auto * kv = ctx->get_kv_self();
2219
+ if (!kv) {
2220
+ return;
2221
+ }
2222
+
2223
+ kv->seq_add(seq_id, p0, p1, delta);
2224
+ }
2225
+
2226
+ // deprecated
2227
+ void llama_kv_cache_seq_div(
2228
+ llama_context * ctx,
2229
+ llama_seq_id seq_id,
2230
+ llama_pos p0,
2231
+ llama_pos p1,
2232
+ int d) {
2233
+ llama_kv_self_seq_div(ctx, seq_id, p0, p1, d);
2234
+ }
2235
+
2236
+ void llama_kv_self_seq_div(
2237
+ llama_context * ctx,
2238
+ llama_seq_id seq_id,
2239
+ llama_pos p0,
2240
+ llama_pos p1,
2241
+ int d) {
2242
+ auto * kv = ctx->get_kv_self();
2243
+ if (!kv) {
2244
+ return;
2245
+ }
2246
+
2247
+ kv->seq_div(seq_id, p0, p1, d);
2248
+ }
2249
+
2250
+ // deprecated
2251
+ llama_pos llama_kv_cache_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
2252
+ return llama_kv_self_seq_pos_max(ctx, seq_id);
2253
+ }
2254
+
2255
+ llama_pos llama_kv_self_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
2256
+ const auto * kv = ctx->get_kv_self();
2257
+ if (!kv) {
2258
+ return 0;
2259
+ }
2260
+
2261
+ return kv->seq_pos_max(seq_id);
2262
+ }
2263
+
2264
+ // deprecated
2265
+ void llama_kv_cache_defrag(llama_context * ctx) {
2266
+ llama_kv_self_defrag(ctx);
2267
+ }
2268
+
2269
+ void llama_kv_self_defrag(llama_context * ctx) {
2270
+ auto * kv = ctx->get_kv_self();
2271
+ if (!kv) {
2272
+ return;
2273
+ }
2274
+
2275
+ // force defrag
2276
+ kv->defrag_sched(-1.0f);
2277
+ }
2278
+
2279
+ // deprecated
2280
+ bool llama_kv_cache_can_shift(const llama_context * ctx) {
2281
+ return llama_kv_self_can_shift(ctx);
2282
+ }
2283
+
2284
+ bool llama_kv_self_can_shift(const llama_context * ctx) {
2285
+ const auto * kv = ctx->get_kv_self();
2286
+ if (!kv) {
2287
+ return false;
2288
+ }
2289
+
2290
+ return kv->get_can_shift();
2291
+ }
2292
+
2293
+ // deprecated
2294
+ void llama_kv_cache_update(llama_context * ctx) {
2295
+ llama_kv_self_update(ctx);
2296
+ }
2297
+
2298
+ // llama state API
2299
+
2300
+ // deprecated
2301
+ size_t llama_get_state_size(llama_context * ctx) {
2302
+ return llama_state_get_size(ctx);
2303
+ }
2304
+
2305
+ // deprecated
2306
+ size_t llama_copy_state_data(llama_context * ctx, uint8_t * dst) {
2307
+ return llama_state_get_data(ctx, dst, -1);
2308
+ }
2309
+
2310
+ // deprecated
2311
+ size_t llama_set_state_data(llama_context * ctx, const uint8_t * src) {
2312
+ return llama_state_set_data(ctx, src, -1);
2313
+ }
2314
+
2315
+ // deprecated
2316
+ bool llama_load_session_file(llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2317
+ return llama_state_load_file(ctx, path_session, tokens_out, n_token_capacity, n_token_count_out);
2318
+ }
2319
+
2320
+ // deprecated
2321
+ bool llama_save_session_file(llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
2322
+ return llama_state_save_file(ctx, path_session, tokens, n_token_count);
2323
+ }
2324
+
2325
+ // Returns the *actual* size of the state.
2326
+ // Intended to be used when saving to state to a buffer.
2327
+ size_t llama_state_get_size(llama_context * ctx) {
2328
+ return ctx->state_get_size();
2329
+ }
2330
+
2331
+ size_t llama_state_get_data(llama_context * ctx, uint8_t * dst, size_t size) {
2332
+ ctx->synchronize();
2333
+
2334
+ return ctx->state_get_data(dst, size);
2335
+ }
2336
+
2337
+ // Sets the state reading from the specified source address
2338
+ size_t llama_state_set_data(llama_context * ctx, const uint8_t * src, size_t size) {
2339
+ ctx->synchronize();
2340
+
2341
+ return ctx->state_set_data(src, size);
2342
+ }
2343
+
2344
+ bool llama_state_load_file(llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2345
+ ctx->synchronize();
2346
+
2347
+ try {
2348
+ return ctx->state_load_file(path_session, tokens_out, n_token_capacity, n_token_count_out);
2349
+ } catch (const std::exception & err) {
2350
+ LLAMA_LOG_ERROR("%s: error loading session file: %s\n", __func__, err.what());
2351
+ return false;
2352
+ }
2353
+ }
2354
+
2355
+ bool llama_state_save_file(llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count) {
2356
+ ctx->synchronize();
2357
+
2358
+ try {
2359
+ return ctx->state_save_file(path_session, tokens, n_token_count);
2360
+ } catch (const std::exception & err) {
2361
+ LLAMA_LOG_ERROR("%s: error saving session file: %s\n", __func__, err.what());
2362
+ return false;
2363
+ }
2364
+ }
2365
+
2366
+ size_t llama_state_seq_get_size(llama_context * ctx, llama_seq_id seq_id) {
2367
+ return ctx->state_seq_get_size(seq_id);
2368
+ }
2369
+
2370
+ size_t llama_state_seq_get_data(llama_context * ctx, uint8_t * dst, size_t size, llama_seq_id seq_id) {
2371
+ ctx->synchronize();
2372
+
2373
+ return ctx->state_seq_get_data(seq_id, dst, size);
2374
+ }
2375
+
2376
+ size_t llama_state_seq_set_data(llama_context * ctx, const uint8_t * src, size_t size, llama_seq_id seq_id) {
2377
+ ctx->synchronize();
2378
+
2379
+ return ctx->state_seq_set_data(seq_id, src, size);
2380
+ }
2381
+
2382
+ size_t llama_state_seq_save_file(llama_context * ctx, const char * filepath, llama_seq_id seq_id, const llama_token * tokens, size_t n_token_count) {
2383
+ ctx->synchronize();
2384
+
2385
+ try {
2386
+ return ctx->state_seq_save_file(seq_id, filepath, tokens, n_token_count);
2387
+ } catch (const std::exception & err) {
2388
+ LLAMA_LOG_ERROR("%s: error saving sequence state file: %s\n", __func__, err.what());
2389
+ return 0;
2390
+ }
2391
+ }
2392
+
2393
+ size_t llama_state_seq_load_file(llama_context * ctx, const char * filepath, llama_seq_id dest_seq_id, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) {
2394
+ ctx->synchronize();
2395
+
2396
+ try {
2397
+ return ctx->state_seq_load_file(dest_seq_id, filepath, tokens_out, n_token_capacity, n_token_count_out);
2398
+ } catch (const std::exception & err) {
2399
+ LLAMA_LOG_ERROR("%s: error loading sequence state file: %s\n", __func__, err.what());
2400
+ return 0;
2401
+ }
2402
+ }
2403
+
2404
+ ///
2405
+
2406
+ int32_t llama_encode(
2407
+ llama_context * ctx,
2408
+ llama_batch batch) {
2409
+ const int ret = ctx->encode(batch);
2410
+ if (ret != 0) {
2411
+ LLAMA_LOG_ERROR("%s: failed to encode, ret = %d\n", __func__, ret);
2412
+ }
2413
+
2414
+ return ret;
2415
+ }
2416
+
2417
+ int32_t llama_decode(
2418
+ llama_context * ctx,
2419
+ llama_batch batch) {
2420
+ const int ret = ctx->decode(batch);
2421
+ if (ret != 0) {
2422
+ LLAMA_LOG_ERROR("%s: failed to decode, ret = %d\n", __func__, ret);
2423
+ }
2424
+
2425
+ return ret;
2426
+ }
2427
+
2428
+ //
2429
+ // perf
2430
+ //
2431
+
2432
+ llama_perf_context_data llama_perf_context(const llama_context * ctx) {
2433
+ llama_perf_context_data data = {};
2434
+
2435
+ if (ctx == nullptr) {
2436
+ return data;
2437
+ }
2438
+
2439
+ data = ctx->perf_get_data();
2440
+
2441
+ return data;
2442
+ }
2443
+
2444
+ void llama_perf_context_print(const llama_context * ctx) {
2445
+ const auto data = llama_perf_context(ctx);
2446
+
2447
+ const double t_end_ms = 1e-3 * ggml_time_us();
2448
+
2449
+ LLAMA_LOG_INFO("%s: load time = %10.2f ms\n", __func__, data.t_load_ms);
2450
+ LLAMA_LOG_INFO("%s: prompt eval time = %10.2f ms / %5d tokens (%8.2f ms per token, %8.2f tokens per second)\n",
2451
+ __func__, data.t_p_eval_ms, data.n_p_eval, data.t_p_eval_ms / data.n_p_eval, 1e3 / data.t_p_eval_ms * data.n_p_eval);
2452
+ LLAMA_LOG_INFO("%s: eval time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n",
2453
+ __func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval);
2454
+ LLAMA_LOG_INFO("%s: total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval));
2455
+ }
2456
+
2457
+ void llama_perf_context_reset(llama_context * ctx) {
2458
+ ctx->perf_reset();
2459
+ }