@novastera-oss/llamarn 0.2.5 → 0.2.7

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 (225) hide show
  1. package/RNLlamaCpp.podspec +3 -2
  2. package/android/CMakeLists.txt +6 -3
  3. package/android/src/main/cpp/include/llama.h +140 -38
  4. package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
  8. package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
  11. package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
  12. package/cpp/LlamaCppModel.cpp +48 -67
  13. package/cpp/LlamaCppModel.h +8 -3
  14. package/cpp/PureCppImpl.cpp +1 -1
  15. package/cpp/PureCppImpl.h +2 -2
  16. package/cpp/build-info.cpp +2 -2
  17. package/cpp/llama.cpp/CMakeLists.txt +15 -4
  18. package/cpp/llama.cpp/Makefile +2 -2
  19. package/cpp/llama.cpp/README.md +33 -13
  20. package/cpp/llama.cpp/common/CMakeLists.txt +15 -28
  21. package/cpp/llama.cpp/common/arg.cpp +38 -12
  22. package/cpp/llama.cpp/common/build-info.cpp.in +2 -2
  23. package/cpp/llama.cpp/common/chat-parser.cpp +9 -3
  24. package/cpp/llama.cpp/common/chat-parser.h +4 -1
  25. package/cpp/llama.cpp/common/chat.cpp +16 -13
  26. package/cpp/llama.cpp/common/chat.h +1 -1
  27. package/cpp/llama.cpp/common/common.cpp +52 -40
  28. package/cpp/llama.cpp/common/common.h +5 -2
  29. package/cpp/llama.cpp/common/json-partial.cpp +5 -4
  30. package/cpp/llama.cpp/common/json-partial.h +2 -1
  31. package/cpp/llama.cpp/common/json-schema-to-grammar.cpp +2 -1
  32. package/cpp/llama.cpp/common/json-schema-to-grammar.h +4 -4
  33. package/cpp/llama.cpp/common/speculative.cpp +6 -4
  34. package/cpp/llama.cpp/convert_hf_to_gguf.py +128 -84
  35. package/cpp/llama.cpp/ggml/CMakeLists.txt +47 -2
  36. package/cpp/llama.cpp/ggml/cmake/common.cmake +1 -2
  37. package/cpp/llama.cpp/ggml/include/ggml.h +1 -3
  38. package/cpp/llama.cpp/ggml/src/CMakeLists.txt +49 -13
  39. package/cpp/llama.cpp/ggml/src/ggml-backend-reg.cpp +5 -0
  40. package/cpp/llama.cpp/ggml/src/ggml-backend.cpp +10 -5
  41. package/cpp/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +3 -3
  42. package/cpp/llama.cpp/ggml/src/ggml-cann/common.h +6 -1
  43. package/cpp/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +33 -9
  44. package/cpp/llama.cpp/ggml/src/ggml-common.h +4 -0
  45. package/cpp/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +93 -24
  46. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +1 -1
  47. package/cpp/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +1 -1
  48. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  49. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/quants.c +4113 -0
  50. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +2174 -0
  51. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +2638 -0
  52. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +2731 -0
  53. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +2068 -0
  54. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +396 -0
  55. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/s390/quants.c +1299 -0
  56. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +1480 -0
  57. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch/x86/quants.c +4310 -0
  58. package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-aarch64.cpp → arch/x86/repack.cpp} +59 -3206
  59. package/cpp/llama.cpp/ggml/src/ggml-cpu/arch-fallback.h +184 -0
  60. package/cpp/llama.cpp/ggml/src/ggml-cpu/common.h +1 -1
  61. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +7 -4
  62. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +33 -2
  63. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +8 -8
  64. package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.cpp → hbm.cpp} +1 -1
  65. package/cpp/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +1 -1
  66. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +56 -7
  67. package/cpp/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +5 -0
  68. package/cpp/llama.cpp/ggml/src/ggml-cpu/ops.cpp +2 -2
  69. package/cpp/llama.cpp/ggml/src/ggml-cpu/quants.c +1157 -0
  70. package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-quants.h → quants.h} +26 -0
  71. package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.cpp +1555 -0
  72. package/cpp/llama.cpp/ggml/src/ggml-cpu/repack.h +98 -0
  73. package/cpp/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +2 -4
  74. package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.cpp → traits.cpp} +1 -1
  75. package/cpp/llama.cpp/ggml/src/ggml-cuda/common.cuh +6 -8
  76. package/cpp/llama.cpp/ggml/src/ggml-cuda/fattn-mma-f16.cuh +5 -2
  77. package/cpp/llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu +25 -16
  78. package/cpp/llama.cpp/ggml/src/ggml-cuda/ssm-scan.cu +6 -4
  79. package/cpp/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +4 -0
  80. package/cpp/llama.cpp/ggml/src/ggml-impl.h +2 -0
  81. package/cpp/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +11 -10
  82. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.m +33 -8
  83. package/cpp/llama.cpp/ggml/src/ggml-metal/ggml-metal.metal +135 -100
  84. package/cpp/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +7 -0
  85. package/cpp/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +908 -3
  86. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/concat.cl +109 -0
  87. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
  88. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/pad.cl +30 -0
  89. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/repeat.cl +39 -0
  90. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/tanh.cl +63 -0
  91. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
  92. package/cpp/llama.cpp/ggml/src/ggml-opencl/kernels/upscale.cl +121 -0
  93. package/cpp/llama.cpp/ggml/src/ggml-quants.c +0 -2
  94. package/cpp/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +18 -15
  95. package/cpp/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +3 -3
  96. package/cpp/llama.cpp/ggml/src/ggml-sycl/common.hpp +19 -24
  97. package/cpp/llama.cpp/ggml/src/ggml-sycl/convert.cpp +21 -2
  98. package/cpp/llama.cpp/ggml/src/ggml-sycl/cpy.cpp +121 -4
  99. package/cpp/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +32 -0
  100. package/cpp/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +3 -0
  101. package/cpp/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +2 -96
  102. package/cpp/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +164 -46
  103. package/cpp/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +32 -8
  104. package/cpp/llama.cpp/ggml/src/ggml-sycl/quants.hpp +38 -10
  105. package/cpp/llama.cpp/ggml/src/ggml-sycl/rope.cpp +118 -11
  106. package/cpp/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +108 -16
  107. package/cpp/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +26 -29
  108. package/cpp/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +432 -248
  109. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +0 -12
  110. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/conv_transpose_1d.comp +98 -0
  111. package/cpp/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +2 -0
  112. package/cpp/llama.cpp/ggml/src/ggml.c +9 -8
  113. package/cpp/llama.cpp/ggml/src/ggml.cpp +26 -0
  114. package/cpp/llama.cpp/ggml/src/gguf.cpp +19 -2
  115. package/cpp/llama.cpp/gguf-py/gguf/constants.py +57 -0
  116. package/cpp/llama.cpp/gguf-py/gguf/gguf_writer.py +4 -1
  117. package/cpp/llama.cpp/gguf-py/gguf/tensor_mapping.py +14 -3
  118. package/cpp/llama.cpp/include/llama.h +140 -38
  119. package/cpp/llama.cpp/requirements/requirements-compare-llama-bench.txt +1 -0
  120. package/cpp/llama.cpp/src/CMakeLists.txt +4 -1
  121. package/cpp/llama.cpp/src/llama-arch.cpp +95 -3
  122. package/cpp/llama.cpp/src/llama-arch.h +7 -1
  123. package/cpp/llama.cpp/src/llama-batch.cpp +289 -31
  124. package/cpp/llama.cpp/src/llama-batch.h +47 -17
  125. package/cpp/llama.cpp/src/llama-chat.cpp +19 -2
  126. package/cpp/llama.cpp/src/llama-chat.h +1 -0
  127. package/cpp/llama.cpp/src/llama-context.cpp +488 -313
  128. package/cpp/llama.cpp/src/llama-context.h +38 -17
  129. package/cpp/llama.cpp/src/llama-cparams.cpp +1 -1
  130. package/cpp/llama.cpp/src/llama-cparams.h +1 -1
  131. package/cpp/llama.cpp/src/llama-graph.cpp +275 -152
  132. package/cpp/llama.cpp/src/llama-graph.h +109 -52
  133. package/cpp/llama.cpp/src/llama-hparams.cpp +6 -2
  134. package/cpp/llama.cpp/src/llama-hparams.h +8 -2
  135. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.cpp +281 -0
  136. package/cpp/llama.cpp/src/llama-kv-cache-unified-iswa.h +133 -0
  137. package/cpp/llama.cpp/src/llama-kv-cache-unified.cpp +1835 -0
  138. package/cpp/llama.cpp/src/llama-kv-cache-unified.h +308 -0
  139. package/cpp/llama.cpp/src/llama-kv-cells.h +53 -17
  140. package/cpp/llama.cpp/src/llama-memory-hybrid.cpp +247 -0
  141. package/cpp/llama.cpp/src/llama-memory-hybrid.h +143 -0
  142. package/cpp/llama.cpp/src/llama-memory-recurrent.cpp +1116 -0
  143. package/cpp/llama.cpp/src/llama-memory-recurrent.h +188 -0
  144. package/cpp/llama.cpp/src/llama-memory.cpp +41 -0
  145. package/cpp/llama.cpp/src/llama-memory.h +89 -4
  146. package/cpp/llama.cpp/src/llama-mmap.cpp +1 -1
  147. package/cpp/llama.cpp/src/llama-model-loader.cpp +42 -17
  148. package/cpp/llama.cpp/src/llama-model.cpp +735 -143
  149. package/cpp/llama.cpp/src/llama-model.h +4 -0
  150. package/cpp/llama.cpp/src/llama-quant.cpp +2 -1
  151. package/cpp/llama.cpp/src/llama-vocab.cpp +39 -25
  152. package/cpp/llama.cpp/src/llama.cpp +11 -7
  153. package/cpp/llama.cpp/src/unicode.cpp +5 -0
  154. package/cpp/llama.cpp/vendor/cpp-httplib/httplib.h +10518 -0
  155. package/cpp/llama.cpp/vendor/miniaudio/miniaudio.h +93468 -0
  156. package/cpp/llama.cpp/{common → vendor}/minja/chat-template.hpp +1 -1
  157. package/cpp/llama.cpp/{common → vendor}/minja/minja.hpp +1 -1
  158. package/cpp/llama.cpp/{common → vendor/nlohmann}/json.hpp +3027 -2267
  159. package/cpp/llama.cpp/vendor/nlohmann/json_fwd.hpp +187 -0
  160. package/cpp/llama.cpp/vendor/stb/stb_image.h +7988 -0
  161. package/cpp/rn-completion.cpp +65 -10
  162. package/cpp/{rn-llama.hpp → rn-llama.h} +1 -1
  163. package/cpp/{rn-utils.hpp → rn-utils.h} +8 -1
  164. package/ios/include/chat.h +1 -1
  165. package/ios/include/common/minja/chat-template.hpp +1 -1
  166. package/ios/include/common/minja/minja.hpp +1 -1
  167. package/ios/include/common.h +5 -2
  168. package/ios/include/json-schema-to-grammar.h +4 -4
  169. package/ios/include/llama.h +140 -38
  170. package/ios/include/{common → nlohmann}/json.hpp +3027 -2267
  171. package/ios/libs/llama.xcframework/Info.plist +20 -20
  172. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  173. package/ios/libs/llama.xcframework/ios-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4863 -4617
  174. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/ggml.h +1 -3
  175. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/Headers/llama.h +140 -38
  176. package/ios/libs/llama.xcframework/ios-arm64/llama.framework/llama +0 -0
  177. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  178. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4638
  179. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3742 -3557
  180. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  181. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
  182. package/ios/libs/llama.xcframework/ios-arm64_x86_64-simulator/llama.framework/llama +0 -0
  183. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  184. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4638
  185. package/ios/libs/llama.xcframework/macos-arm64_x86_64/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3744 -3559
  186. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/ggml.h +1 -3
  187. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Headers/llama.h +140 -38
  188. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/ggml.h +1 -3
  189. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/Headers/llama.h +140 -38
  190. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/A/llama +0 -0
  191. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/ggml.h +1 -3
  192. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/Headers/llama.h +140 -38
  193. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/Versions/Current/llama +0 -0
  194. package/ios/libs/llama.xcframework/macos-arm64_x86_64/llama.framework/llama +0 -0
  195. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  196. package/ios/libs/llama.xcframework/tvos-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4863 -4616
  197. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/ggml.h +1 -3
  198. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/Headers/llama.h +140 -38
  199. package/ios/libs/llama.xcframework/tvos-arm64/llama.framework/llama +0 -0
  200. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  201. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4834 -4637
  202. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3742 -3556
  203. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  204. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
  205. package/ios/libs/llama.xcframework/tvos-arm64_x86_64-simulator/llama.framework/llama +0 -0
  206. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  207. package/ios/libs/llama.xcframework/xros-arm64/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4900 -4653
  208. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/ggml.h +1 -3
  209. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/Headers/llama.h +140 -38
  210. package/ios/libs/llama.xcframework/xros-arm64/llama.framework/llama +0 -0
  211. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/DWARF/llama +0 -0
  212. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/aarch64/llama.yml +4871 -4674
  213. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/dSYMs/llama.dSYM/Contents/Resources/Relocations/x86_64/llama.yml +3773 -3587
  214. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/ggml.h +1 -3
  215. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/Headers/llama.h +140 -38
  216. package/ios/libs/llama.xcframework/xros-arm64_x86_64-simulator/llama.framework/llama +0 -0
  217. package/package.json +1 -2
  218. package/cpp/llama.cpp/common/cmake/build-info-gen-cpp.cmake +0 -24
  219. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +0 -8
  220. package/cpp/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +0 -13891
  221. package/cpp/llama.cpp/src/llama-kv-cache.cpp +0 -2747
  222. package/cpp/llama.cpp/src/llama-kv-cache.h +0 -502
  223. /package/cpp/llama.cpp/ggml/src/ggml-cpu/{cpu-feats-x86.cpp → arch/x86/cpu-feats.cpp} +0 -0
  224. /package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-hbm.h → hbm.h} +0 -0
  225. /package/cpp/llama.cpp/ggml/src/ggml-cpu/{ggml-cpu-traits.h → traits.h} +0 -0
@@ -0,0 +1,188 @@
1
+ #pragma once
2
+
3
+ #include "llama-batch.h"
4
+ #include "llama-graph.h"
5
+ #include "llama-memory.h"
6
+
7
+ #include <set>
8
+ #include <vector>
9
+
10
+ //
11
+ // llama_memory_recurrent
12
+ //
13
+
14
+ // TODO: extract the cache state used for graph computation into llama_memory_recurrent_state_i
15
+ // see the implementation of llama_kv_cache_unified_state_i for an example how to do it
16
+ class llama_memory_recurrent : public llama_memory_i {
17
+ public:
18
+
19
+ // this callback is used to filter out layers that should not be included in the cache
20
+ using layer_filter_cb = std::function<bool(int32_t il)>;
21
+
22
+ llama_memory_recurrent(
23
+ const llama_model & model,
24
+ layer_filter_cb && filter,
25
+ ggml_type type_r,
26
+ ggml_type type_s,
27
+ bool offload,
28
+ uint32_t mem_size,
29
+ uint32_t n_seq_max);
30
+
31
+ ~llama_memory_recurrent() = default;
32
+
33
+ //
34
+ // llama_memory_i
35
+ //
36
+
37
+ llama_memory_state_ptr init_batch(
38
+ const llama_batch & batch,
39
+ uint32_t n_ubatch,
40
+ bool embd_all) override;
41
+
42
+ llama_memory_state_ptr init_full() override;
43
+
44
+ llama_memory_state_ptr init_update(llama_context * lctx, bool optimize) override;
45
+
46
+ void clear(bool data) override;
47
+
48
+ bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
49
+ void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
50
+ void seq_keep(llama_seq_id seq_id) override;
51
+ void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override;
52
+ void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
53
+
54
+ llama_pos seq_pos_min(llama_seq_id seq_id) const override;
55
+ llama_pos seq_pos_max(llama_seq_id seq_id) const override;
56
+
57
+ bool prepare(const std::vector<llama_ubatch> & ubatches);
58
+
59
+ // find a contiguous slot of memory cells and emplace the ubatch there
60
+ bool find_slot(const llama_ubatch & ubatch);
61
+
62
+ bool get_can_shift() const override;
63
+
64
+ // state write/load
65
+
66
+ void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
67
+ void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
68
+
69
+ uint32_t head = 0; // the location where the batch will be placed in the cache (see find_slot())
70
+ uint32_t size = 0; // total number of cells, shared across all sequences
71
+ uint32_t used = 0; // used cells (i.e. at least one seq_id)
72
+
73
+ // computed before each graph build
74
+ uint32_t n = 0;
75
+
76
+ // first zero-ed state
77
+ int32_t rs_z = -1;
78
+
79
+ // TODO: optimize for recurrent state needs
80
+ struct mem_cell {
81
+ llama_pos pos = -1;
82
+ int32_t src = -1; // used to know where states should be copied from
83
+ int32_t src0 = -1; // like src, but only used when setting the inputs (allowing to copy once)
84
+ int32_t tail = -1;
85
+
86
+ std::set<llama_seq_id> seq_id;
87
+
88
+ bool has_seq_id(const llama_seq_id & id) const {
89
+ return seq_id.find(id) != seq_id.end();
90
+ }
91
+
92
+ bool is_empty() const {
93
+ return seq_id.empty();
94
+ }
95
+
96
+ bool is_same_seq(const mem_cell & other) const {
97
+ return seq_id == other.seq_id;
98
+ }
99
+ };
100
+
101
+ std::vector<mem_cell> cells;
102
+
103
+ // per layer
104
+ std::vector<ggml_tensor *> r_l;
105
+ std::vector<ggml_tensor *> s_l;
106
+
107
+ private:
108
+ //const llama_model & model;
109
+ const llama_hparams & hparams;
110
+
111
+ const uint32_t n_seq_max = 1;
112
+
113
+ std::vector<ggml_context_ptr> ctxs;
114
+ std::vector<ggml_backend_buffer_ptr> bufs;
115
+
116
+ size_t total_size() const;
117
+
118
+ size_t size_r_bytes() const;
119
+ size_t size_s_bytes() const;
120
+
121
+ void state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) const;
122
+ void state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const;
123
+
124
+ bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
125
+ bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
126
+ };
127
+
128
+ class llama_memory_recurrent_state : public llama_memory_state_i {
129
+ public:
130
+ // used for errors
131
+ llama_memory_recurrent_state(llama_memory_status status);
132
+
133
+ // used to create a full-cache state
134
+ llama_memory_recurrent_state(
135
+ llama_memory_recurrent * mem);
136
+
137
+ // used to create a state from a batch
138
+ llama_memory_recurrent_state(
139
+ llama_memory_recurrent * mem,
140
+ llama_sbatch sbatch,
141
+ std::vector<llama_ubatch> ubatches);
142
+
143
+ virtual ~llama_memory_recurrent_state();
144
+
145
+ //
146
+ // llama_memory_state_i
147
+ //
148
+
149
+ bool next() override;
150
+ bool apply() override;
151
+
152
+ std::vector<int64_t> & out_ids() override;
153
+
154
+ llama_memory_status get_status() const override;
155
+ const llama_ubatch & get_ubatch() const override;
156
+
157
+ //
158
+ // llama_memory_recurrent_state specific API
159
+ //
160
+
161
+ uint32_t get_n_rs() const;
162
+ uint32_t get_head() const;
163
+ int32_t get_rs_z() const;
164
+ uint32_t get_size() const;
165
+
166
+ ggml_tensor * get_r_l(int32_t il) const;
167
+ ggml_tensor * get_s_l(int32_t il) const;
168
+
169
+ int32_t s_copy(int i) const;
170
+
171
+ private:
172
+ const llama_memory_status status;
173
+
174
+ llama_memory_recurrent * mem;
175
+
176
+ llama_sbatch sbatch;
177
+
178
+ size_t i_next = 0;
179
+
180
+ std::vector<llama_ubatch> ubatches;
181
+
182
+ //
183
+ // data needed for building the compute graph for the current ubatch:
184
+ // TODO: extract all the state like `head` and `n` here
185
+ //
186
+
187
+ const bool is_full = false;
188
+ };
@@ -1 +1,42 @@
1
1
  #include "llama-memory.h"
2
+
3
+ llama_memory_status llama_memory_status_combine(llama_memory_status s0, llama_memory_status s1) {
4
+ bool has_update = false;
5
+
6
+ switch (s0) {
7
+ case LLAMA_MEMORY_STATUS_SUCCESS:
8
+ {
9
+ has_update = true;
10
+ break;
11
+ }
12
+ case LLAMA_MEMORY_STATUS_NO_UPDATE:
13
+ {
14
+ break;
15
+ }
16
+ case LLAMA_MEMORY_STATUS_FAILED_PREPARE:
17
+ case LLAMA_MEMORY_STATUS_FAILED_COMPUTE:
18
+ {
19
+ return s0;
20
+ }
21
+ }
22
+
23
+ switch (s1) {
24
+ case LLAMA_MEMORY_STATUS_SUCCESS:
25
+ {
26
+ has_update = true;
27
+ break;
28
+ }
29
+ case LLAMA_MEMORY_STATUS_NO_UPDATE:
30
+ {
31
+ break;
32
+ }
33
+ case LLAMA_MEMORY_STATUS_FAILED_PREPARE:
34
+ case LLAMA_MEMORY_STATUS_FAILED_COMPUTE:
35
+ {
36
+ return s1;
37
+ }
38
+ }
39
+
40
+ // if either status has an update, then the combined status has an update
41
+ return has_update ? LLAMA_MEMORY_STATUS_SUCCESS : LLAMA_MEMORY_STATUS_NO_UPDATE;
42
+ }
@@ -2,6 +2,14 @@
2
2
 
3
3
  #include "llama.h"
4
4
 
5
+ #include <memory>
6
+ #include <vector>
7
+
8
+ struct llama_ubatch;
9
+
10
+ class llama_io_write_i;
11
+ class llama_io_read_i;
12
+
5
13
  struct llama_memory_params {
6
14
  // kv cache
7
15
  ggml_type type_k;
@@ -11,13 +19,78 @@ struct llama_memory_params {
11
19
  bool swa_full;
12
20
  };
13
21
 
22
+ enum llama_memory_status {
23
+ LLAMA_MEMORY_STATUS_SUCCESS = 0,
24
+ LLAMA_MEMORY_STATUS_NO_UPDATE,
25
+ LLAMA_MEMORY_STATUS_FAILED_PREPARE,
26
+ LLAMA_MEMORY_STATUS_FAILED_COMPUTE,
27
+ };
28
+
29
+ // helper function for combining the status of two memory states
30
+ // useful for implementing hybrid memory types (e.g. iSWA)
31
+ llama_memory_status llama_memory_status_combine(llama_memory_status s0, llama_memory_status s1);
32
+
33
+ // the interface for managing the memory state during batch processing
34
+ // this interface is implemented per memory type. see:
35
+ // - llama_kv_cache_unified_state
36
+ // - llama_kv_cache_unified_iswa_state
37
+ // ...
38
+ //
39
+ // the only method that can mutate the memory and the memory state is llama_memory_i::apply()
40
+ //
41
+ // TODO: rename to llama_memory_context_i ?
42
+ struct llama_memory_state_i {
43
+ virtual ~llama_memory_state_i() = default;
44
+
45
+ // consume the current ubatch from the state and proceed to the next one
46
+ // return false if we are done
47
+ virtual bool next() = 0;
48
+
49
+ // apply the memory state for the current ubatch to the memory object
50
+ // return false on failure
51
+ virtual bool apply() = 0;
52
+
53
+ // TODO: this might get reworked in the future when refactoring llama_batch
54
+ virtual std::vector<int64_t> & out_ids() = 0;
55
+
56
+ // get the current ubatch
57
+ virtual const llama_ubatch & get_ubatch() const = 0;
58
+
59
+ // get the status of the memory state - used for error handling and checking if any updates would be applied
60
+ virtual llama_memory_status get_status() const = 0;
61
+ };
62
+
63
+ using llama_memory_state_ptr = std::unique_ptr<llama_memory_state_i>;
64
+
14
65
  // general concept of LLM memory
15
66
  // the KV cache is a type of LLM memory, but there can be other types
16
- class llama_memory_i {
17
- public:
67
+ struct llama_memory_i {
18
68
  virtual ~llama_memory_i() = default;
19
69
 
20
- virtual void clear() = 0;
70
+ // split the input batch into a set of ubatches and verify that they can fit into the cache
71
+ // return a state object containing the ubatches and KV cache state required to process them
72
+ // check the llama_memory_state_i::get_status() for the result
73
+ virtual llama_memory_state_ptr init_batch(
74
+ const llama_batch & batch,
75
+ uint32_t n_ubatch,
76
+ bool embd_all) = 0;
77
+
78
+ // simulate full cache, used for allocating worst-case compute buffers
79
+ virtual llama_memory_state_ptr init_full() = 0;
80
+
81
+ // prepare for any pending memory updates, such as shifts, defrags, etc.
82
+ // status == LLAMA_MEMORY_STATUS_NO_UPDATE if there is nothing to update
83
+ virtual llama_memory_state_ptr init_update(llama_context * lctx, bool optimize) = 0;
84
+
85
+ // getters
86
+ virtual bool get_can_shift() const = 0;
87
+
88
+ //
89
+ // ops
90
+ //
91
+
92
+ // if data == true, the data buffers will also be cleared together with the metadata
93
+ virtual void clear(bool data) = 0;
21
94
 
22
95
  virtual bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) = 0;
23
96
  virtual void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) = 0;
@@ -28,5 +101,17 @@ public:
28
101
  virtual llama_pos seq_pos_min(llama_seq_id seq_id) const = 0;
29
102
  virtual llama_pos seq_pos_max(llama_seq_id seq_id) const = 0;
30
103
 
31
- virtual bool get_can_edit() const = 0;
104
+ //
105
+ // state write/read
106
+ //
107
+
108
+ virtual void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const = 0;
109
+ virtual void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) = 0;
110
+ };
111
+
112
+ using llama_memory_ptr = std::unique_ptr<llama_memory_i>;
113
+
114
+ // TODO: temporary until the llama_kv_cache is removed from the public API
115
+ struct llama_kv_cache : public llama_memory_i {
116
+ virtual ~llama_kv_cache() = default;
32
117
  };
@@ -401,7 +401,7 @@ struct llama_mmap::impl {
401
401
  }
402
402
  }
403
403
  #else
404
- throw std::runtime_error("PrefetchVirtualMemory unavailable");
404
+ LLAMA_LOG_DEBUG("skipping PrefetchVirtualMemory because _WIN32_WINNT < 0x602\n");
405
405
  #endif
406
406
  }
407
407
  }
@@ -288,9 +288,10 @@ namespace GGUFMeta {
288
288
 
289
289
  template<typename T>
290
290
  bool llama_model_loader::get_arr(const std::string & key, std::vector<T> & result, bool required) {
291
- const int kid = gguf_find_key(meta.get(), key.c_str());
291
+ const gguf_context * ctx = meta.get();
292
+ const int kid = gguf_find_key(ctx, key.c_str());
292
293
 
293
- if (kid < 0 || gguf_get_kv_type(meta.get(), kid) != GGUF_TYPE_ARRAY) {
294
+ if (kid < 0 || gguf_get_kv_type(ctx, kid) != GGUF_TYPE_ARRAY) {
294
295
  if (required) {
295
296
  throw std::runtime_error(format("array key not found in model: %s", key.c_str()));
296
297
  }
@@ -298,28 +299,40 @@ namespace GGUFMeta {
298
299
  }
299
300
 
300
301
  struct GGUFMeta::ArrayInfo arr_info =
301
- GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
302
+ GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(ctx, kid);
302
303
 
303
304
  switch (arr_info.gt) {
304
305
  case GGUF_TYPE_UINT32:
305
- case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same<T, int32_t>::value) ||
306
- (std::is_same<T, uint32_t>::value)); break;
307
- case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
306
+ case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same<T, int32_t>::value) ||
307
+ (std::is_same<T, uint32_t>::value)); break;
308
+ case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
309
+ case GGUF_TYPE_STRING: GGML_ASSERT((std::is_same<T, std::string>::value)); break;
308
310
  default:
309
- throw std::runtime_error(format("%s is not a float32/uint32/int32 array", key.c_str()));
311
+ throw std::runtime_error(format("%s is not a string/float32/uint32/int32 array", key.c_str()));
310
312
  }
311
313
 
312
- result.resize(arr_info.length);
313
- result.assign((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length);
314
+ if constexpr (std::is_same<T, std::string>::value) {
315
+ const size_t n_items = gguf_get_arr_n(ctx, kid);
316
+ result.clear();
317
+
318
+ for (size_t i = 0; i < n_items; i++) {
319
+ const T value = gguf_get_arr_str(ctx, kid, i);
320
+ result.emplace_back(value);
321
+ }
322
+ } else {
323
+ result.resize(arr_info.length);
324
+ result.assign((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length);
325
+ }
314
326
 
315
327
  return true;
316
328
  }
317
329
 
318
330
  template<typename T, size_t N_MAX>
319
331
  bool llama_model_loader::get_arr(const std::string & key, std::array<T, N_MAX> & result, bool required) {
320
- const int kid = gguf_find_key(meta.get(), key.c_str());
332
+ const gguf_context * ctx = meta.get();
333
+ const int kid = gguf_find_key(ctx, key.c_str());
321
334
 
322
- if (kid < 0 || gguf_get_kv_type(meta.get(), kid) != GGUF_TYPE_ARRAY) {
335
+ if (kid < 0 || gguf_get_kv_type(ctx, kid) != GGUF_TYPE_ARRAY) {
323
336
  if (required) {
324
337
  throw std::runtime_error(format("array key not found in model: %s", key.c_str()));
325
338
  }
@@ -327,22 +340,32 @@ namespace GGUFMeta {
327
340
  }
328
341
 
329
342
  struct GGUFMeta::ArrayInfo arr_info =
330
- GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
343
+ GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(ctx, kid);
331
344
 
332
345
  switch (arr_info.gt) {
333
346
  case GGUF_TYPE_UINT32:
334
- case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same<T, int32_t>::value) ||
335
- (std::is_same<T, uint32_t>::value)); break;
336
- case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
347
+ case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same<T, int32_t>::value) ||
348
+ (std::is_same<T, uint32_t>::value)); break;
349
+ case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
350
+ case GGUF_TYPE_STRING: GGML_ASSERT((std::is_same<T, std::string>::value)); break;
337
351
  default:
338
- throw std::runtime_error(format("%s is not a float32/uint32/int32 array", key.c_str()));
352
+ throw std::runtime_error(format("%s is not a string/float32/uint32/int32 array", key.c_str()));
339
353
  }
340
354
 
341
355
  if (arr_info.length > N_MAX) {
342
356
  throw std::runtime_error(format("array length %u for key %s exceeds max %u", (uint32_t) arr_info.length, key.c_str(), (uint32_t) N_MAX));
343
357
  }
344
358
 
345
- std::copy((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length, result.begin());
359
+ if constexpr (std::is_same<T, std::string>::value) {
360
+ const size_t n_items = gguf_get_arr_n(ctx, kid);
361
+
362
+ for (size_t i = 0; i < n_items; i++) {
363
+ const T value = gguf_get_arr_str(ctx, kid, i);
364
+ result[i] = value;
365
+ }
366
+ } else {
367
+ std::copy((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length, result.begin());
368
+ }
346
369
 
347
370
  return true;
348
371
  }
@@ -352,6 +375,8 @@ namespace GGUFMeta {
352
375
  return get_arr(llm_kv(kid), result, required);
353
376
  }
354
377
 
378
+ template bool llama_model_loader::get_arr<std::vector<std::string>>(enum llm_kv kid, std::vector<std::string> & result, bool required);
379
+
355
380
  template<typename T>
356
381
  bool llama_model_loader::get_key(const std::string & key, T & result, bool required) {
357
382
  auto it = kv_overrides.find(key);