@novastera-oss/llamarn 0.2.1 → 0.2.2

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 (266) hide show
  1. package/README.md +80 -14
  2. package/RNLlamaCpp.podspec +10 -3
  3. package/android/CMakeLists.txt +8 -0
  4. package/android/src/main/cpp/include/llama.h +62 -125
  5. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  11. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  12. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  13. package/cpp/build-info.cpp +2 -2
  14. package/cpp/llama.cpp/README.md +11 -3
  15. package/cpp/llama.cpp/build-xcframework.sh +1 -0
  16. package/cpp/llama.cpp/common/CMakeLists.txt +8 -2
  17. package/cpp/llama.cpp/common/arg.cpp +153 -113
  18. package/cpp/llama.cpp/common/chat-parser.cpp +379 -0
  19. package/cpp/llama.cpp/common/chat-parser.h +117 -0
  20. package/cpp/llama.cpp/common/chat.cpp +847 -699
  21. package/cpp/llama.cpp/common/chat.h +73 -6
  22. package/cpp/llama.cpp/common/common.cpp +50 -82
  23. package/cpp/llama.cpp/common/common.h +21 -17
  24. package/cpp/llama.cpp/common/json-partial.cpp +255 -0
  25. package/cpp/llama.cpp/common/json-partial.h +37 -0
  26. package/cpp/llama.cpp/common/minja/chat-template.hpp +9 -5
  27. package/cpp/llama.cpp/common/minja/minja.hpp +69 -36
  28. package/cpp/llama.cpp/common/regex-partial.cpp +204 -0
  29. package/cpp/llama.cpp/common/regex-partial.h +56 -0
  30. package/cpp/llama.cpp/common/sampling.cpp +7 -8
  31. package/cpp/llama.cpp/convert_hf_to_gguf.py +453 -118
  32. package/cpp/llama.cpp/convert_hf_to_gguf_update.py +120 -68
  33. package/cpp/llama.cpp/ggml/CMakeLists.txt +2 -1
  34. package/cpp/llama.cpp/ggml/cmake/common.cmake +25 -0
  35. package/cpp/llama.cpp/ggml/include/ggml-opt.h +49 -28
  36. package/cpp/llama.cpp/ggml/include/ggml.h +26 -7
  37. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +16 -10
  38. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +4 -1
  39. package/cpp/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +1 -0
  40. package/cpp/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +2 -0
  41. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +604 -0
  42. package/cpp/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +42 -0
  43. package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +54 -2
  44. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +50 -51
  45. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +2 -2
  46. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +5 -9
  47. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +779 -19
  48. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +22 -0
  49. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +88 -5
  50. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +47 -12
  51. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +264 -69
  52. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +322 -100
  53. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +117 -1
  54. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.cpp +85 -16
  55. package/cpp/llama.cpp/ggml/src/ggml-cpu/vec.h +220 -49
  56. package/cpp/llama.cpp/ggml/src/ggml-cuda/acc.cu +40 -26
  57. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +1 -1
  58. package/cpp/llama.cpp/ggml/src/ggml-cuda/cpy.cu +11 -1
  59. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-common.cuh +15 -7
  60. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +266 -64
  61. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f16.cuh +49 -4
  62. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-vec-f32.cuh +48 -4
  63. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn.cu +2 -1
  64. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +5 -1
  65. package/cpp/llama.cpp/ggml/src/ggml-cuda/mmq.cu +2 -0
  66. package/cpp/llama.cpp/ggml/src/ggml-cuda/quantize.cu +7 -6
  67. package/cpp/llama.cpp/ggml/src/ggml-cuda/sum.cu +1 -1
  68. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cu +10 -0
  69. package/cpp/llama.cpp/ggml/src/ggml-cuda/unary.cuh +2 -0
  70. package/cpp/llama.cpp/ggml/src/ggml-impl.h +1 -1
  71. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +4 -0
  72. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +99 -17
  73. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +200 -2
  74. package/cpp/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +8 -2
  75. package/cpp/llama.cpp/ggml/src/ggml-musa/mudnn.cu +112 -0
  76. package/cpp/llama.cpp/ggml/src/ggml-musa/mudnn.cuh +12 -0
  77. package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +6 -0
  78. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +972 -178
  79. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
  80. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/div.cl +72 -0
  81. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/group_norm.cl +72 -0
  82. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
  83. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sub.cl +72 -0
  84. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/sum_rows.cl +39 -0
  85. package/cpp/llama.cpp/ggml/src/ggml-opt.cpp +373 -190
  86. package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +29 -23
  87. package/cpp/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +5 -10
  88. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +101 -5
  89. package/cpp/llama.cpp/ggml/src/ggml-sycl/concat.cpp +31 -33
  90. package/cpp/llama.cpp/ggml/src/ggml-sycl/conv.cpp +1 -0
  91. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +29 -2
  92. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +4 -5
  93. package/cpp/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +59 -21
  94. package/cpp/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +9 -1
  95. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +84 -72
  96. package/cpp/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +2 -0
  97. package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +37 -8
  98. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +1 -3
  99. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +324 -129
  100. package/cpp/llama.cpp/ggml/src/ggml-sycl/gla.cpp +1 -0
  101. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +31 -2
  102. package/cpp/llama.cpp/ggml/src/ggml-sycl/norm.cpp +95 -68
  103. package/cpp/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +1 -0
  104. package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +22 -0
  105. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +1 -2
  106. package/cpp/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +1 -4
  107. package/cpp/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +2 -3
  108. package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +69 -43
  109. package/cpp/llama.cpp/ggml/src/ggml-sycl/wkv.cpp +2 -14
  110. package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +81 -91
  111. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +432 -181
  112. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +17 -0
  113. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_m.comp +1 -1
  114. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +6 -152
  115. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +162 -0
  116. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +360 -0
  117. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +2 -118
  118. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +1 -1
  119. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +12 -1
  120. package/cpp/llama.cpp/ggml/src/ggml.c +107 -36
  121. package/cpp/llama.cpp/ggml/src/gguf.cpp +33 -33
  122. package/cpp/llama.cpp/gguf-py/gguf/constants.py +100 -15
  123. package/cpp/llama.cpp/gguf-py/gguf/gguf_reader.py +1 -1
  124. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +44 -12
  125. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_editor_gui.py +21 -10
  126. package/cpp/llama.cpp/gguf-py/gguf/scripts/gguf_new_metadata.py +5 -2
  127. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +128 -31
  128. package/cpp/llama.cpp/gguf-py/gguf/utility.py +1 -1
  129. package/cpp/llama.cpp/gguf-py/pyproject.toml +1 -1
  130. package/cpp/llama.cpp/include/llama.h +62 -125
  131. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +1 -1
  132. package/cpp/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +1 -1
  133. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.inp +1 -1
  134. package/cpp/llama.cpp/models/ggml-vocab-command-r.gguf.out +1 -1
  135. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +1 -1
  136. package/cpp/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +1 -1
  137. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +1 -1
  138. package/cpp/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +1 -1
  139. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.inp +1 -1
  140. package/cpp/llama.cpp/models/ggml-vocab-falcon.gguf.out +1 -1
  141. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +1 -1
  142. package/cpp/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +1 -1
  143. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +1 -1
  144. package/cpp/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +1 -1
  145. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +1 -1
  146. package/cpp/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +1 -1
  147. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.inp +1 -1
  148. package/cpp/llama.cpp/models/ggml-vocab-mpt.gguf.out +1 -1
  149. package/cpp/llama.cpp/models/ggml-vocab-nomic-bert-moe.gguf +0 -0
  150. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +1 -1
  151. package/cpp/llama.cpp/models/ggml-vocab-phi-3.gguf.out +1 -1
  152. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +1 -1
  153. package/cpp/llama.cpp/models/ggml-vocab-qwen2.gguf.out +1 -1
  154. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.inp +1 -1
  155. package/cpp/llama.cpp/models/ggml-vocab-refact.gguf.out +1 -1
  156. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +1 -1
  157. package/cpp/llama.cpp/models/ggml-vocab-starcoder.gguf.out +1 -1
  158. package/cpp/llama.cpp/models/templates/Qwen-QwQ-32B.jinja +62 -0
  159. package/cpp/llama.cpp/models/templates/Qwen-Qwen3-0.6B.jinja +85 -0
  160. package/cpp/llama.cpp/models/templates/README.md +2 -0
  161. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +5 -1
  162. package/cpp/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +5 -1
  163. package/cpp/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
  164. package/cpp/llama.cpp/requirements/requirements-gguf_editor_gui.txt +1 -1
  165. package/cpp/llama.cpp/src/CMakeLists.txt +2 -0
  166. package/cpp/llama.cpp/src/llama-arch.cpp +6 -0
  167. package/cpp/llama.cpp/src/llama-arch.h +2 -0
  168. package/cpp/llama.cpp/src/llama-batch.cpp +3 -1
  169. package/cpp/llama.cpp/src/llama-context.cpp +340 -123
  170. package/cpp/llama.cpp/src/llama-context.h +30 -0
  171. package/cpp/llama.cpp/src/llama-cparams.cpp +4 -0
  172. package/cpp/llama.cpp/src/llama-cparams.h +2 -0
  173. package/cpp/llama.cpp/src/llama-grammar.cpp +12 -2
  174. package/cpp/llama.cpp/src/llama-graph.cpp +157 -247
  175. package/cpp/llama.cpp/src/llama-graph.h +52 -7
  176. package/cpp/llama.cpp/src/llama-hparams.cpp +17 -1
  177. package/cpp/llama.cpp/src/llama-hparams.h +37 -5
  178. package/cpp/llama.cpp/src/llama-kv-cache.cpp +742 -481
  179. package/cpp/llama.cpp/src/llama-kv-cache.h +196 -99
  180. package/cpp/llama.cpp/src/llama-kv-cells.h +379 -0
  181. package/cpp/llama.cpp/src/llama-memory.h +4 -3
  182. package/cpp/llama.cpp/src/llama-model-loader.cpp +22 -17
  183. package/cpp/llama.cpp/src/llama-model-saver.cpp +281 -0
  184. package/cpp/llama.cpp/src/llama-model-saver.h +37 -0
  185. package/cpp/llama.cpp/src/llama-model.cpp +529 -172
  186. package/cpp/llama.cpp/src/llama-model.h +6 -1
  187. package/cpp/llama.cpp/src/llama-quant.cpp +15 -13
  188. package/cpp/llama.cpp/src/llama-sampling.cpp +2 -2
  189. package/cpp/llama.cpp/src/llama-vocab.cpp +35 -8
  190. package/cpp/llama.cpp/src/llama-vocab.h +6 -0
  191. package/cpp/llama.cpp/src/llama.cpp +14 -0
  192. package/cpp/rn-completion.cpp +4 -2
  193. package/ios/include/chat.h +73 -6
  194. package/ios/include/common/minja/chat-template.hpp +9 -5
  195. package/ios/include/common/minja/minja.hpp +69 -36
  196. package/ios/include/common.h +21 -17
  197. package/ios/include/llama.h +62 -125
  198. package/ios/libs/llama.xcframework/Info.plist +19 -19
  199. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  200. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4617 -4487
  201. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  202. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +26 -7
  203. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +62 -125
  204. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  205. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  206. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4638 -4508
  207. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3557 -3435
  208. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  209. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  210. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  211. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  212. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  213. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4638 -4508
  214. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3559 -3437
  215. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml-opt.h +237 -0
  216. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +26 -7
  217. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +62 -125
  218. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml-opt.h +237 -0
  219. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +26 -7
  220. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +62 -125
  221. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  222. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml-opt.h +237 -0
  223. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +26 -7
  224. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +62 -125
  225. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  226. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  227. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  228. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4616 -4487
  229. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  230. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +26 -7
  231. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +62 -125
  232. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  233. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  234. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4637 -4508
  235. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3556 -3435
  236. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  237. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  238. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  239. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  240. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  241. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4653 -4523
  242. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml-opt.h +237 -0
  243. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +26 -7
  244. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +62 -125
  245. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  246. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  247. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4674 -4544
  248. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3587 -3465
  249. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml-opt.h +237 -0
  250. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +26 -7
  251. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +62 -125
  252. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  253. package/package.json +1 -1
  254. package/cpp/llama.cpp/common/stb_image.h +0 -7988
  255. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +0 -112
  256. package/cpp/llama.cpp/models/ggml-vocab-chameleon.gguf.out +0 -46
  257. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.inp +0 -112
  258. package/cpp/llama.cpp/models/ggml-vocab-deepseek-r1-qwen.gguf.out +0 -46
  259. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.inp +0 -112
  260. package/cpp/llama.cpp/models/ggml-vocab-gpt-4o.gguf.out +0 -46
  261. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.inp +0 -112
  262. package/cpp/llama.cpp/models/ggml-vocab-llama4.gguf.out +0 -46
  263. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +0 -112
  264. package/cpp/llama.cpp/models/ggml-vocab-pixtral.gguf.out +0 -46
  265. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +0 -112
  266. package/cpp/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +0 -46
@@ -0,0 +1,237 @@
1
+ // This file contains functionality for training models using GGML.
2
+ // It is not strictly needed vs. just vanilla GGML but it provides a more high-level interface for common needs such as datasets.
3
+ // At the bottom of this file especially there are relatively high-level functions that are suitable use or adaptation in user code.
4
+ //
5
+ // Module maintainer: Johannes Gäßler (@JohannesGaessler, johannesg@5d6.de)
6
+
7
+ #pragma once
8
+
9
+ #include "ggml.h"
10
+ #include "ggml-backend.h"
11
+
12
+ #include <stdint.h>
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ struct ggml_opt_dataset;
19
+ struct ggml_opt_context;
20
+ struct ggml_opt_result;
21
+
22
+ typedef struct ggml_opt_dataset * ggml_opt_dataset_t;
23
+ typedef struct ggml_opt_context * ggml_opt_context_t;
24
+ typedef struct ggml_opt_result * ggml_opt_result_t;
25
+
26
+ // ====== Loss ======
27
+
28
+ // built-in loss types, i.e. the built-in quantities minimized by the optimizer
29
+ // custom loss types can be defined via mean or sum which simply reduce the outputs for all datapoints to a single value
30
+ enum ggml_opt_loss_type {
31
+ GGML_OPT_LOSS_TYPE_MEAN,
32
+ GGML_OPT_LOSS_TYPE_SUM,
33
+ GGML_OPT_LOSS_TYPE_CROSS_ENTROPY,
34
+ GGML_OPT_LOSS_TYPE_MEAN_SQUARED_ERROR,
35
+ };
36
+
37
+ // ====== Dataset ======
38
+
39
+ GGML_API ggml_opt_dataset_t ggml_opt_dataset_init(
40
+ enum ggml_type type_data, // the type for the internal data tensor
41
+ enum ggml_type type_label, // the type for the internal labels tensor
42
+ int64_t ne_datapoint, // number of elements per datapoint
43
+ int64_t ne_label, // number of elements per label
44
+ int64_t ndata, // total number of datapoints/labels
45
+ int64_t ndata_shard); // number of datapoints/labels per shard (unit at which the dataset is shuffled/copied)
46
+ GGML_API void ggml_opt_dataset_free(ggml_opt_dataset_t dataset);
47
+
48
+ // get underlying tensors that store the data
49
+ GGML_API int64_t ggml_opt_dataset_ndata (ggml_opt_dataset_t dataset);
50
+ GGML_API struct ggml_tensor * ggml_opt_dataset_data (ggml_opt_dataset_t dataset); // shape = [ne_datapoint, ndata]
51
+ GGML_API struct ggml_tensor * ggml_opt_dataset_labels(ggml_opt_dataset_t dataset); // shape = [nd_label, ndata]
52
+
53
+ // shuffle idata first datapoints from dataset with RNG from opt_ctx, shuffle all datapoints if idata is negative
54
+ GGML_API void ggml_opt_dataset_shuffle(ggml_opt_context_t opt_ctx, ggml_opt_dataset_t dataset, int64_t idata);
55
+
56
+ // get batch at position ibatch from dataset and copy the data to data_batch and labels_batch
57
+ GGML_API void ggml_opt_dataset_get_batch(
58
+ ggml_opt_dataset_t dataset,
59
+ struct ggml_tensor * data_batch, // shape = [ne_datapoint, ndata_batch]
60
+ struct ggml_tensor * labels_batch, // shape = [ne_label, ndata_batch]
61
+ int64_t ibatch);
62
+ GGML_API void ggml_opt_dataset_get_batch_host(
63
+ ggml_opt_dataset_t dataset,
64
+ void * data_batch,
65
+ size_t nb_data_batch,
66
+ void * labels_batch,
67
+ int64_t ibatch);
68
+
69
+ // ====== Model / Context ======
70
+
71
+ enum ggml_opt_build_type {
72
+ GGML_OPT_BUILD_TYPE_FORWARD = 10,
73
+ GGML_OPT_BUILD_TYPE_GRAD = 20,
74
+ GGML_OPT_BUILD_TYPE_OPT = 30,
75
+ };
76
+
77
+ // parameters that control which optimizer is used and how said optimizer tries to find the minimal loss
78
+ struct ggml_opt_optimizer_params {
79
+ // AdamW optimizer parameters
80
+ struct {
81
+ float alpha; // learning rate
82
+ float beta1;
83
+ float beta2;
84
+ float eps; // epsilon for numerical stability
85
+ float wd; // weight decay for AdamW, use 0.0f to disable
86
+ } adamw;
87
+ };
88
+
89
+ // callback to calculate optimizer parameters prior to a backward pass
90
+ // userdata can be used to pass arbitrary data
91
+ typedef struct ggml_opt_optimizer_params (*ggml_opt_get_optimizer_params)(void * userdata);
92
+
93
+ // returns the default optimizer params (constant, hard-coded values)
94
+ // userdata is not used
95
+ GGML_API struct ggml_opt_optimizer_params ggml_opt_get_default_optimizer_params(void * userdata);
96
+
97
+ // casts userdata to ggml_opt_optimizer_params and returns it
98
+ GGML_API struct ggml_opt_optimizer_params ggml_opt_get_constant_optimizer_params(void * userdata);
99
+
100
+ // parameters for initializing a new optimization context
101
+ struct ggml_opt_params {
102
+ ggml_backend_sched_t backend_sched; // defines which backends are used to construct the compute graphs
103
+
104
+ // by default the forward graph needs to be reconstructed for each eval
105
+ // if ctx_compute, inputs, and outputs are set the graphs are instead allocated statically
106
+ struct ggml_context * ctx_compute;
107
+ struct ggml_tensor * inputs;
108
+ struct ggml_tensor * outputs;
109
+
110
+ enum ggml_opt_loss_type loss_type;
111
+ enum ggml_opt_build_type build_type;
112
+
113
+ int32_t opt_period; // after how many gradient accumulation steps an optimizer step should be done
114
+
115
+ ggml_opt_get_optimizer_params get_opt_pars; // callback for calculating optimizer parameters
116
+ void * get_opt_pars_ud; // userdata for calculating optimizer parameters
117
+ };
118
+
119
+ // get parameters for an optimization context with defaults set where possible
120
+ // parameters for which no sensible defaults exist are supplied as arguments to this function
121
+ GGML_API struct ggml_opt_params ggml_opt_default_params(
122
+ ggml_backend_sched_t backend_sched,
123
+ enum ggml_opt_loss_type loss_type);
124
+
125
+ GGML_API ggml_opt_context_t ggml_opt_init(struct ggml_opt_params params);
126
+ GGML_API void ggml_opt_free(ggml_opt_context_t opt_ctx);
127
+
128
+ // set gradients to zero, initilize loss, and optionally reset the optimizer
129
+ GGML_API void ggml_opt_reset(ggml_opt_context_t opt_ctx, bool optimizer);
130
+
131
+ GGML_API bool ggml_opt_static_graphs(ggml_opt_context_t opt_ctx); // whether the graphs are allocated_statically
132
+
133
+ // get underlying tensors that store data
134
+ // if not using static graphs these pointers become invalid with the next call to ggml_opt_alloc
135
+ GGML_API struct ggml_tensor * ggml_opt_inputs( ggml_opt_context_t opt_ctx); // forward graph input tensor
136
+ GGML_API struct ggml_tensor * ggml_opt_outputs( ggml_opt_context_t opt_ctx); // forward graph output tensor
137
+ GGML_API struct ggml_tensor * ggml_opt_labels( ggml_opt_context_t opt_ctx); // labels to compare outputs against
138
+ GGML_API struct ggml_tensor * ggml_opt_loss( ggml_opt_context_t opt_ctx); // scalar tensor that contains the loss
139
+ GGML_API struct ggml_tensor * ggml_opt_pred( ggml_opt_context_t opt_ctx); // predictions made by outputs
140
+ GGML_API struct ggml_tensor * ggml_opt_ncorrect(ggml_opt_context_t opt_ctx); // number of matching predictions between outputs and labels
141
+
142
+ // get the gradient accumulator for a node from the forward graph
143
+ GGML_API struct ggml_tensor * ggml_opt_grad_acc(ggml_opt_context_t opt_ctx, struct ggml_tensor * node);
144
+
145
+ // ====== Optimization Result ======
146
+
147
+ GGML_API ggml_opt_result_t ggml_opt_result_init(void);
148
+ GGML_API void ggml_opt_result_free(ggml_opt_result_t result);
149
+ GGML_API void ggml_opt_result_reset(ggml_opt_result_t result);
150
+
151
+ // get data from result, uncertainties are optional and can be ignored by passing NULL
152
+ GGML_API void ggml_opt_result_ndata( ggml_opt_result_t result, int64_t * ndata); // writes 1 value, number of datapoints
153
+ GGML_API void ggml_opt_result_loss( ggml_opt_result_t result, double * loss, double * unc); // writes 1 value
154
+ GGML_API void ggml_opt_result_pred( ggml_opt_result_t result, int32_t * pred); // writes ndata values
155
+ GGML_API void ggml_opt_result_accuracy(ggml_opt_result_t result, double * accuracy, double * unc); // writes 1 value
156
+
157
+ // ====== Computation ======
158
+
159
+ // if not using static graphs, this function must be called prior to ggml_opt_alloc
160
+ GGML_API void ggml_opt_prepare_alloc(
161
+ ggml_opt_context_t opt_ctx,
162
+ struct ggml_context * ctx_compute,
163
+ struct ggml_cgraph * gf,
164
+ struct ggml_tensor * inputs,
165
+ struct ggml_tensor * outputs);
166
+
167
+ // allocate the next graph for evaluation, either forward or forward + backward
168
+ // must be called exactly once prior to calling ggml_opt_eval
169
+ GGML_API void ggml_opt_alloc(ggml_opt_context_t opt_ctx, bool backward);
170
+
171
+ // do forward pass, increment result if not NULL, do backward pass if allocated
172
+ GGML_API void ggml_opt_eval(ggml_opt_context_t opt_ctx, ggml_opt_result_t result);
173
+
174
+ // ############################################################################
175
+ // ## The high-level functions start here. They do not depend on any private ##
176
+ // ## functions or structs and can be copied to and adapted for user code. ##
177
+ // ############################################################################
178
+
179
+ // ====== Intended Usage ======
180
+ //
181
+ // 1. Select the appropriate loss for your problem.
182
+ // 2. Create a dataset and set the data for the "data" tensor. Also set the "labels" tensor if your loss needs them.
183
+ // Setting the shard size to 1 will be fine, it's the granularity with which data is shuffled/loaded (bigger values are faster).
184
+ // 3. Create a GGML graph for your model with no_alloc == true. Use two separate contexts for the tensors.
185
+ // The first context should contain the model parameters and inputs and be allocated statically in user code.
186
+ // The second context should contain all other tensors and will be (re)allocated automatically.
187
+ // Due to this automated allocation the data of the second context is not defined when accessed in user code.
188
+ // Note that the second dimension of the inputs/outputs are interpreted as the number of datapoints in those tensors.
189
+ // 4. Call ggml_opt_fit. If you need more control you can use ggml_opt_epoch instead.
190
+
191
+ // signature for a callback while evaluating opt_ctx on dataset, called after an evaluation
192
+ typedef void (*ggml_opt_epoch_callback)(
193
+ bool train, // true after training evaluation, false after validation evaluation
194
+ ggml_opt_context_t opt_ctx,
195
+ ggml_opt_dataset_t dataset,
196
+ ggml_opt_result_t result, // result associated with the dataset subsection
197
+ int64_t ibatch, // number of batches that have been evaluated so far
198
+ int64_t ibatch_max, // total number of batches in this dataset subsection
199
+ int64_t t_start_us); // time at which the evaluation on the dataset subsection was started
200
+
201
+ // do training on front of dataset, do evaluation only on back of dataset
202
+ GGML_API void ggml_opt_epoch(
203
+ ggml_opt_context_t opt_ctx,
204
+ ggml_opt_dataset_t dataset,
205
+ ggml_opt_result_t result_train, // result to increment during training, ignored if NULL
206
+ ggml_opt_result_t result_eval, // result to increment during evaluation, ignored if NULL
207
+ int64_t idata_split, // data index at which to split training and evaluation
208
+ ggml_opt_epoch_callback callback_train,
209
+ ggml_opt_epoch_callback callback_eval);
210
+
211
+ // callback that prints a progress bar on stderr
212
+ GGML_API void ggml_opt_epoch_callback_progress_bar(
213
+ bool train,
214
+ ggml_opt_context_t opt_ctx,
215
+ ggml_opt_dataset_t dataset,
216
+ ggml_opt_result_t result,
217
+ int64_t ibatch,
218
+ int64_t ibatch_max,
219
+ int64_t t_start_us);
220
+
221
+ // fit model defined by inputs and outputs to dataset
222
+ GGML_API void ggml_opt_fit(
223
+ ggml_backend_sched_t backend_sched, // backend scheduler for constructing the compute graphs
224
+ struct ggml_context * ctx_compute, // context with temporarily allocated tensors to calculate the outputs
225
+ struct ggml_tensor * inputs, // input tensor with shape [ne_datapoint, ndata_batch]
226
+ struct ggml_tensor * outputs, // output tensor, must have shape [ne_label, ndata_batch] if labels are used
227
+ ggml_opt_dataset_t dataset, // dataset with data and optionally also labels
228
+ enum ggml_opt_loss_type loss_type, // loss to minimize
229
+ ggml_opt_get_optimizer_params get_opt_pars, // callback to get optimizer params, userdata is pointer to epoch (of type int64_t)
230
+ int64_t nepoch, // how many times the dataset should be iterated over
231
+ int64_t nbatch_logical, // datapoints optimizer step, must be a multiple of ndata_batch in inputs/outputs
232
+ float val_split, // fraction of the dataset to use for validation, must be in [0.0f, 1.0f)
233
+ bool silent); // whether or not info prints to stderr should be suppressed
234
+
235
+ #ifdef __cplusplus
236
+ }
237
+ #endif
@@ -536,6 +536,7 @@ extern "C" {
536
536
  GGML_UNARY_OP_HARDSWISH,
537
537
  GGML_UNARY_OP_HARDSIGMOID,
538
538
  GGML_UNARY_OP_EXP,
539
+ GGML_UNARY_OP_GELU_ERF,
539
540
 
540
541
  GGML_UNARY_OP_COUNT,
541
542
  };
@@ -768,7 +769,7 @@ extern "C" {
768
769
  // Tensor flags
769
770
  GGML_API void ggml_set_input(struct ggml_tensor * tensor);
770
771
  GGML_API void ggml_set_output(struct ggml_tensor * tensor);
771
- GGML_API void ggml_set_param(struct ggml_context * ctx, struct ggml_tensor * tensor);
772
+ GGML_API void ggml_set_param(struct ggml_tensor * tensor);
772
773
  GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
773
774
 
774
775
  //
@@ -934,11 +935,20 @@ extern "C" {
934
935
  struct ggml_tensor * a,
935
936
  struct ggml_tensor * b);
936
937
 
938
+ // repeat a to the specified shape
939
+ GGML_API struct ggml_tensor * ggml_repeat_4d(
940
+ struct ggml_context * ctx,
941
+ struct ggml_tensor * a,
942
+ int64_t ne0,
943
+ int64_t ne1,
944
+ int64_t ne2,
945
+ int64_t ne3);
946
+
937
947
  // sums repetitions in a into shape of b
938
948
  GGML_API struct ggml_tensor * ggml_repeat_back(
939
949
  struct ggml_context * ctx,
940
950
  struct ggml_tensor * a,
941
- struct ggml_tensor * b);
951
+ struct ggml_tensor * b); // sum up values that are adjacent in dims > 0 instead of repeated with same stride
942
952
 
943
953
  // concat a and b along dim
944
954
  // used in stable-diffusion
@@ -1024,6 +1034,16 @@ extern "C" {
1024
1034
  struct ggml_context * ctx,
1025
1035
  struct ggml_tensor * a);
1026
1036
 
1037
+ // GELU using erf (error function) when possible
1038
+ // some backends may fallback to approximation based on Abramowitz and Stegun formula
1039
+ GGML_API struct ggml_tensor * ggml_gelu_erf(
1040
+ struct ggml_context * ctx,
1041
+ struct ggml_tensor * a);
1042
+
1043
+ GGML_API struct ggml_tensor * ggml_gelu_erf_inplace(
1044
+ struct ggml_context * ctx,
1045
+ struct ggml_tensor * a);
1046
+
1027
1047
  GGML_API struct ggml_tensor * ggml_gelu_quick(
1028
1048
  struct ggml_context * ctx,
1029
1049
  struct ggml_tensor * a);
@@ -2049,15 +2069,14 @@ extern "C" {
2049
2069
 
2050
2070
  GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2051
2071
  GGML_API void ggml_build_backward_expand(
2052
- struct ggml_context * ctx_static, // context for static gradients (loss + gradient accumulation)
2053
- struct ggml_context * ctx_compute, // context for gradient computation
2054
- struct ggml_cgraph * cgraph,
2055
- bool accumulate); // whether or not gradients should be accumulated, requires static allocation of tensors in ctx_static
2072
+ struct ggml_context * ctx, // context for gradient computation
2073
+ struct ggml_cgraph * cgraph,
2074
+ struct ggml_tensor ** grad_accs);
2056
2075
 
2057
2076
  // graph allocation in a context
2058
2077
  GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
2059
2078
  GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
2060
- GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
2079
+ GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool force_grads);
2061
2080
  GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
2062
2081
  GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2063
2082
  GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
@@ -4,6 +4,7 @@
4
4
  #include "ggml.h"
5
5
  #include "ggml-cpu.h"
6
6
  #include "ggml-backend.h"
7
+ #include "ggml-opt.h"
7
8
 
8
9
  #include <stddef.h>
9
10
  #include <stdint.h>
@@ -344,7 +345,7 @@ extern "C" {
344
345
  float yarn_beta_fast; // YaRN low correction dim
345
346
  float yarn_beta_slow; // YaRN high correction dim
346
347
  uint32_t yarn_orig_ctx; // YaRN original context size
347
- float defrag_thold; // defragment the KV cache if holes/size > thold, < 0 disabled (default)
348
+ float defrag_thold; // defragment the KV cache if holes/size > thold, <= 0 disabled (default)
348
349
 
349
350
  ggml_backend_sched_eval_callback cb_eval;
350
351
  void * cb_eval_user_data;
@@ -360,10 +361,11 @@ extern "C" {
360
361
 
361
362
  // Keep the booleans together and at the end of the struct to avoid misalignment during copy-by-value.
362
363
  bool embeddings; // if true, extract embeddings (together with logits)
363
- bool offload_kqv; // whether to offload the KQV ops (including the KV cache) to GPU
364
- bool flash_attn; // whether to use flash attention [EXPERIMENTAL]
365
- bool no_perf; // whether to measure performance timings
366
- bool op_offload; // whether to offload host tensor operations to device
364
+ bool offload_kqv; // offload the KQV ops (including the KV cache) to GPU
365
+ bool flash_attn; // use flash attention [EXPERIMENTAL]
366
+ bool no_perf; // measure performance timings
367
+ bool op_offload; // offload host tensor operations to device
368
+ bool swa_full; // use full-size SWA cache (https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055)
367
369
  };
368
370
 
369
371
  // model quantization parameters
@@ -445,6 +447,10 @@ extern "C" {
445
447
  size_t n_paths,
446
448
  struct llama_model_params params);
447
449
 
450
+ LLAMA_API void llama_model_save_to_file(
451
+ const struct llama_model * model,
452
+ const char * path_model);
453
+
448
454
  DEPRECATED(LLAMA_API void llama_free_model(struct llama_model * model),
449
455
  "use llama_model_free instead");
450
456
 
@@ -465,6 +471,7 @@ extern "C" {
465
471
  LLAMA_API int64_t llama_time_us(void);
466
472
 
467
473
  LLAMA_API size_t llama_max_devices(void);
474
+ LLAMA_API size_t llama_max_parallel_sequences(void);
468
475
 
469
476
  LLAMA_API bool llama_supports_mmap (void);
470
477
  LLAMA_API bool llama_supports_mlock (void);
@@ -602,71 +609,14 @@ extern "C" {
602
609
  // KV cache
603
610
  //
604
611
 
605
- // TODO: start using struct llama_kv_cache
606
-
607
- // Information associated with an individual cell in the KV cache view.
608
- struct llama_kv_cache_view_cell {
609
- // The position for this cell. Takes KV cache shifts into account.
610
- // May be negative if the cell is not populated.
611
- llama_pos pos;
612
- };
613
-
614
- // An updateable view of the KV cache.
615
- struct llama_kv_cache_view {
616
- // Number of KV cache cells. This will be the same as the context size.
617
- int32_t n_cells;
618
-
619
- // Maximum number of sequences that can exist in a cell. It's not an error
620
- // if there are more sequences in a cell than this value, however they will
621
- // not be visible in the view cells_sequences.
622
- int32_t n_seq_max;
623
-
624
- // Number of tokens in the cache. For example, if there are two populated
625
- // cells, the first with 1 sequence id in it and the second with 2 sequence
626
- // ids then you'll have 3 tokens.
627
- int32_t token_count;
628
-
629
- // Number of populated cache cells.
630
- int32_t used_cells;
631
-
632
- // Maximum contiguous empty slots in the cache.
633
- int32_t max_contiguous;
634
-
635
- // Index to the start of the max_contiguous slot range. Can be negative
636
- // when cache is full.
637
- int32_t max_contiguous_idx;
638
-
639
- // Information for an individual cell.
640
- struct llama_kv_cache_view_cell * cells;
641
-
642
- // The sequences for each cell. There will be n_seq_max items per cell.
643
- llama_seq_id * cells_sequences;
644
- };
645
-
646
- // Create an empty KV cache view. (use only for debugging purposes)
647
- LLAMA_API struct llama_kv_cache_view llama_kv_cache_view_init(const struct llama_context * ctx, int32_t n_seq_max);
648
-
649
- // Free a KV cache view. (use only for debugging purposes)
650
- LLAMA_API void llama_kv_cache_view_free(struct llama_kv_cache_view * view);
651
-
652
- // Update the KV cache view structure with the current state of the KV cache. (use only for debugging purposes)
653
- // TODO: change signature to llama_kv_cache_view_update(struct llama_kv_cache_view * view, const struct llama_context * ctx)
654
- LLAMA_API void llama_kv_cache_view_update(const struct llama_context * ctx, struct llama_kv_cache_view * view);
655
-
656
- ///
657
-
658
612
  // Returns the number of tokens in the KV cache (slow, use only for debug)
659
613
  // If a KV cell has multiple sequences assigned to it, it will be counted multiple times
660
- LLAMA_API int32_t llama_kv_self_n_tokens(const struct llama_context * ctx);
661
-
662
- DEPRECATED(LLAMA_API int32_t llama_get_kv_cache_token_count(const struct llama_context * ctx),
663
- "use llama_kv_self_n_tokens instead");
614
+ DEPRECATED(LLAMA_API int32_t llama_kv_self_n_tokens(const struct llama_context * ctx),
615
+ "Use llama_kv_self_seq_pos_max() and llama_kv_self_seq_pos_min() instead (https://github.com/ggml-org/llama.cpp/issues/13793)");
664
616
 
665
617
  // Returns the number of used KV cells (i.e. have at least one sequence assigned to them)
666
- LLAMA_API int32_t llama_kv_self_used_cells(const struct llama_context * ctx);
667
-
668
- DEPRECATED(LLAMA_API int32_t llama_get_kv_cache_used_cells(const struct llama_context * ctx),
669
- "use llama_kv_self_used_cells instead");
618
+ DEPRECATED(LLAMA_API int32_t llama_kv_self_used_cells(const struct llama_context * ctx),
619
+ "Use llama_kv_self_seq_pos_max() and llama_kv_self_seq_pos_min() instead (https://github.com/ggml-org/llama.cpp/issues/13793)");
670
620
 
671
621
  // Clear the KV cache - both cell info is erased and KV data is zeroed
672
622
  LLAMA_API void llama_kv_self_clear(
@@ -725,10 +675,18 @@ extern "C" {
725
675
  llama_pos p1,
726
676
  int d);
727
677
 
678
+ // Returns the smallest position present in the KV cache for the specified sequence
679
+ // This is typically non-zero only for SWA caches
680
+ // Return -1 if the sequence is empty
681
+ LLAMA_API llama_pos llama_kv_self_seq_pos_min(
682
+ struct llama_context * ctx,
683
+ llama_seq_id seq_id);
684
+
728
685
  // Returns the largest position present in the KV cache for the specified sequence
686
+ // Return -1 if the sequence is empty
729
687
  LLAMA_API llama_pos llama_kv_self_seq_pos_max(
730
688
  struct llama_context * ctx,
731
- llama_seq_id seq_id);
689
+ llama_seq_id seq_id);
732
690
 
733
691
  // Defragment the KV cache
734
692
  // This will be applied:
@@ -742,61 +700,6 @@ extern "C" {
742
700
  // Apply the KV cache updates (such as K-shifts, defragmentation, etc.)
743
701
  LLAMA_API void llama_kv_self_update(struct llama_context * ctx);
744
702
 
745
- DEPRECATED(LLAMA_API void llama_kv_cache_clear(
746
- struct llama_context * ctx),
747
- "use llama_kv_self_clear instead");
748
-
749
- DEPRECATED(LLAMA_API bool llama_kv_cache_seq_rm(
750
- struct llama_context * ctx,
751
- llama_seq_id seq_id,
752
- llama_pos p0,
753
- llama_pos p1),
754
- "use llama_kv_self_seq_rm instead");
755
-
756
- DEPRECATED(LLAMA_API void llama_kv_cache_seq_cp(
757
- struct llama_context * ctx,
758
- llama_seq_id seq_id_src,
759
- llama_seq_id seq_id_dst,
760
- llama_pos p0,
761
- llama_pos p1),
762
- "use llama_kv_self_seq_cp instead");
763
-
764
- DEPRECATED(LLAMA_API void llama_kv_cache_seq_keep(
765
- struct llama_context * ctx,
766
- llama_seq_id seq_id),
767
- "use llama_kv_self_seq_keep instead");
768
-
769
- DEPRECATED(LLAMA_API void llama_kv_cache_seq_add(
770
- struct llama_context * ctx,
771
- llama_seq_id seq_id,
772
- llama_pos p0,
773
- llama_pos p1,
774
- llama_pos delta),
775
- "use llama_kv_self_seq_add instead");
776
-
777
- DEPRECATED(LLAMA_API void llama_kv_cache_seq_div(
778
- struct llama_context * ctx,
779
- llama_seq_id seq_id,
780
- llama_pos p0,
781
- llama_pos p1,
782
- int d),
783
- "use llama_kv_self_seq_div instead");
784
-
785
- DEPRECATED(LLAMA_API llama_pos llama_kv_cache_seq_pos_max(
786
- struct llama_context * ctx,
787
- llama_seq_id seq_id),
788
- "use llama_kv_self_seq_pos_max instead");
789
-
790
- DEPRECATED(LLAMA_API void llama_kv_cache_defrag(struct llama_context * ctx),
791
- "use llama_kv_self_defrag instead");
792
-
793
- DEPRECATED(LLAMA_API bool llama_kv_cache_can_shift(const struct llama_context * ctx),
794
- "use llama_kv_self_can_shift instead");
795
-
796
- DEPRECATED(LLAMA_API void llama_kv_cache_update(struct llama_context * ctx),
797
- "use llama_kv_self_update instead");
798
-
799
-
800
703
  //
801
704
  // State / sessions
802
705
  //
@@ -938,9 +841,12 @@ extern "C" {
938
841
  // Requires KV cache.
939
842
  // For encode-decoder contexts, processes the batch using the decoder.
940
843
  // Positive return values does not mean a fatal error, but rather a warning.
941
- // 0 - success
942
- // 1 - could not find a KV slot for the batch (try reducing the size of the batch or increase the context)
943
- // < 0 - error. the KV cache state is restored to the state before this call
844
+ // Upon non-zero return values, the KV cache state is restored to the state before this call
845
+ // 0 - success
846
+ // 1 - could not find a KV slot for the batch (try reducing the size of the batch or increase the context)
847
+ // 2 - aborted
848
+ // -1 - invalid input batch
849
+ // < -1 - error
944
850
  LLAMA_API int32_t llama_decode(
945
851
  struct llama_context * ctx,
946
852
  struct llama_batch batch);
@@ -1433,6 +1339,37 @@ extern "C" {
1433
1339
  LLAMA_API void llama_perf_sampler_print(const struct llama_sampler * chain);
1434
1340
  LLAMA_API void llama_perf_sampler_reset( struct llama_sampler * chain);
1435
1341
 
1342
+ //
1343
+ // training
1344
+ //
1345
+
1346
+ // function that returns whether or not a given tensor contains trainable parameters
1347
+ typedef bool (*llama_opt_param_filter)(const struct ggml_tensor * tensor, void * userdata);
1348
+
1349
+ // always returns true
1350
+ LLAMA_API bool llama_opt_param_filter_all(const struct ggml_tensor * tensor, void * userdata);
1351
+
1352
+ struct llama_opt_params {
1353
+ uint32_t n_ctx_train; // assumed context size post training, use context size specified in llama_context if 0
1354
+
1355
+ llama_opt_param_filter param_filter; // callback for determining which tensors contain trainable parameters
1356
+ void * param_filter_ud; // userdata for determining which tensors contain trainable parameters
1357
+
1358
+ ggml_opt_get_optimizer_params get_opt_pars; // callback for calculating optimizer parameters
1359
+ void * get_opt_pars_ud; // userdata for calculating optimizer parameters
1360
+ };
1361
+
1362
+ LLAMA_API void llama_opt_init(struct llama_context * lctx, struct llama_model * model, struct llama_opt_params lopt_params);
1363
+
1364
+ LLAMA_API void llama_opt_epoch(
1365
+ struct llama_context * lctx,
1366
+ ggml_opt_dataset_t dataset,
1367
+ ggml_opt_result_t result_train,
1368
+ ggml_opt_result_t result_eval,
1369
+ int64_t idata_split,
1370
+ ggml_opt_epoch_callback callback_train,
1371
+ ggml_opt_epoch_callback callback_eval);
1372
+
1436
1373
  #ifdef __cplusplus
1437
1374
  }
1438
1375
  #endif