@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,1116 @@
1
+ #include "llama-memory-recurrent.h"
2
+
3
+ #include "llama-impl.h"
4
+ #include "llama-io.h"
5
+ #include "llama-batch.h"
6
+ #include "llama-model.h"
7
+
8
+ #include <algorithm>
9
+ #include <cassert>
10
+ #include <limits>
11
+ #include <map>
12
+ #include <stdexcept>
13
+
14
+ //
15
+ // llama_memory_recurrent
16
+ //
17
+
18
+ llama_memory_recurrent::llama_memory_recurrent(
19
+ const llama_model & model,
20
+ layer_filter_cb && filter,
21
+ ggml_type type_r,
22
+ ggml_type type_s,
23
+ bool offload,
24
+ uint32_t mem_size,
25
+ uint32_t n_seq_max) : hparams(model.hparams), n_seq_max(n_seq_max) {
26
+ const int32_t n_layer = hparams.n_layer;
27
+
28
+ LLAMA_LOG_INFO("%s: mem_size = %u, n_seq_max = %u, type_r = '%s', type_s = '%s', n_layer = %d\n",
29
+ __func__, mem_size, n_seq_max, ggml_type_name(type_r), ggml_type_name(type_s), n_layer);
30
+
31
+ head = 0;
32
+ size = mem_size;
33
+ used = 0;
34
+
35
+ cells.clear();
36
+ cells.resize(mem_size);
37
+
38
+ // create a context for each buffer type
39
+ std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
40
+ auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
41
+ auto it = ctx_map.find(buft);
42
+ if (it == ctx_map.end()) {
43
+ ggml_init_params params = {
44
+ /*.mem_size =*/ size_t(2u*n_layer*ggml_tensor_overhead()),
45
+ /*.mem_buffer =*/ NULL,
46
+ /*.no_alloc =*/ true,
47
+ };
48
+
49
+ ggml_context * ctx = ggml_init(params);
50
+ if (!ctx) {
51
+ return nullptr;
52
+ }
53
+
54
+ ctx_map[buft] = ctx;
55
+ ctxs.emplace_back(ctx);
56
+
57
+ return ctx;
58
+ }
59
+
60
+ return it->second;
61
+ };
62
+
63
+ r_l.resize(n_layer);
64
+ s_l.resize(n_layer);
65
+
66
+ for (int i = 0; i < n_layer; i++) {
67
+ if (filter && !filter(i)) {
68
+ LLAMA_LOG_DEBUG("%s: layer %3d: skipped\n", __func__, i);
69
+ continue;
70
+ }
71
+
72
+ const char * dev_name = "CPU";
73
+
74
+ ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();
75
+
76
+ if (offload) {
77
+ auto * dev = model.dev_layer(i);
78
+ buft = ggml_backend_dev_buffer_type(dev);
79
+
80
+ dev_name = ggml_backend_dev_name(dev);
81
+ }
82
+
83
+ LLAMA_LOG_DEBUG("%s, layer %3d: dev = %s\n", __func__, i, dev_name);
84
+
85
+ ggml_context * ctx = ctx_for_buft(buft);
86
+ if (!ctx) {
87
+ throw std::runtime_error("failed to create ggml context for kv cache");
88
+ }
89
+
90
+ ggml_tensor * r = ggml_new_tensor_1d(ctx, type_r, hparams.n_embd_r()*mem_size);
91
+ ggml_tensor * s = ggml_new_tensor_1d(ctx, type_s, hparams.n_embd_s()*mem_size);
92
+ ggml_format_name(r, "cache_r_l%d", i);
93
+ ggml_format_name(s, "cache_s_l%d", i);
94
+ r_l[i] = r;
95
+ s_l[i] = s;
96
+ }
97
+
98
+ // allocate tensors and initialize the buffers to avoid NaNs in the padding
99
+ for (auto it : ctx_map) {
100
+ auto * buft = it.first;
101
+ auto * ctx = it.second;
102
+
103
+ ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
104
+ if (!buf) {
105
+ throw std::runtime_error("failed to allocate buffer for kv cache");
106
+ }
107
+ ggml_backend_buffer_clear(buf, 0);
108
+ LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
109
+ bufs.emplace_back(buf);
110
+ }
111
+
112
+ {
113
+ const size_t memory_size_r = size_r_bytes();
114
+ const size_t memory_size_s = size_s_bytes();
115
+
116
+ LLAMA_LOG_INFO("%s: KV self size = %7.2f MiB, R (%s): %7.2f MiB, S (%s): %7.2f MiB\n", __func__,
117
+ (float)(memory_size_r + memory_size_s) / (1024.0f * 1024.0f),
118
+ ggml_type_name(type_r), (float)memory_size_r / (1024.0f * 1024.0f),
119
+ ggml_type_name(type_s), (float)memory_size_s / (1024.0f * 1024.0f));
120
+ }
121
+ }
122
+
123
+ void llama_memory_recurrent::clear(bool data) {
124
+ for (int32_t i = 0; i < (int32_t) size; ++i) {
125
+ cells[i].pos = -1;
126
+ cells[i].seq_id.clear();
127
+ cells[i].src = -1;
128
+ cells[i].tail = -1;
129
+ }
130
+
131
+ head = 0;
132
+ used = 0;
133
+
134
+ if (data) {
135
+ for (auto & buf : bufs) {
136
+ ggml_backend_buffer_clear(buf.get(), 0);
137
+ }
138
+ }
139
+ }
140
+
141
+ bool llama_memory_recurrent::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
142
+ uint32_t new_head = size;
143
+
144
+ if (p0 < 0) {
145
+ p0 = 0;
146
+ }
147
+
148
+ if (p1 < 0) {
149
+ p1 = std::numeric_limits<llama_pos>::max();
150
+ }
151
+
152
+ // models like Mamba or RWKV can't have a state partially erased
153
+ if (seq_id >= (int64_t) size) {
154
+ // could be fatal
155
+ return false;
156
+ }
157
+ if (0 <= seq_id) {
158
+ int32_t & tail_id = cells[seq_id].tail;
159
+ if (tail_id >= 0) {
160
+ const auto & cell = cells[tail_id];
161
+ // partial intersection is invalid
162
+ if ((0 < p0 && p0 <= cell.pos) || (0 < p1 && p1 <= cell.pos)) {
163
+ return false;
164
+ }
165
+ // invalidate tails which will be cleared
166
+ if (p0 <= cell.pos && cell.pos < p1) {
167
+ tail_id = -1;
168
+ }
169
+ }
170
+ } else {
171
+ // seq_id is negative, then the range should include everything or nothing
172
+ if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits<llama_pos>::max())) {
173
+ return false;
174
+ }
175
+ }
176
+
177
+ for (uint32_t i = 0; i < size; ++i) {
178
+ if (cells[i].pos >= p0 && cells[i].pos < p1) {
179
+ if (seq_id < 0) {
180
+ cells[i].seq_id.clear();
181
+ } else if (cells[i].has_seq_id(seq_id)) {
182
+ cells[i].seq_id.erase(seq_id);
183
+ } else {
184
+ continue;
185
+ }
186
+ if (cells[i].is_empty()) {
187
+ // keep count of the number of used cells
188
+ if (cells[i].pos >= 0) {
189
+ used--;
190
+ }
191
+ cells[i].pos = -1;
192
+ cells[i].src = -1;
193
+ if (new_head == size) {
194
+ new_head = i;
195
+ }
196
+ }
197
+ }
198
+ }
199
+
200
+ // If we freed up a slot, set head to it so searching can start there.
201
+ if (new_head != size && new_head < head) {
202
+ head = new_head;
203
+ }
204
+
205
+ return true;
206
+ }
207
+
208
+ void llama_memory_recurrent::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
209
+ if (seq_id_src == seq_id_dst) {
210
+ return;
211
+ }
212
+
213
+ if (p0 < 0) {
214
+ p0 = 0;
215
+ }
216
+
217
+ if (p1 < 0) {
218
+ p1 = std::numeric_limits<llama_pos>::max();
219
+ }
220
+
221
+ if ((uint32_t) seq_id_dst < size && (uint32_t) seq_id_src < size) {
222
+ auto & tail_src = cells[seq_id_src];
223
+ auto & tail_dst = cells[seq_id_dst];
224
+ if (tail_dst.tail >= 0) {
225
+ // clear destination seq_id if it wasn't empty
226
+ auto & cell_dst = cells[tail_dst.tail];
227
+
228
+ cell_dst.seq_id.erase(seq_id_dst);
229
+ tail_dst.tail = -1;
230
+ if (cell_dst.seq_id.empty()) {
231
+ cell_dst.pos = -1;
232
+ cell_dst.src = -1;
233
+ used -= 1;
234
+ }
235
+ }
236
+ if (tail_src.tail >= 0) {
237
+ auto & cell_src = cells[tail_src.tail];
238
+
239
+ cell_src.seq_id.insert(seq_id_dst);
240
+ tail_dst.tail = tail_src.tail;
241
+ }
242
+ }
243
+ }
244
+
245
+ void llama_memory_recurrent::seq_keep(llama_seq_id seq_id) {
246
+ uint32_t new_head = size;
247
+
248
+ for (uint32_t i = 0; i < size; ++i) {
249
+ if ((llama_seq_id) i != seq_id) {
250
+ cells[i].tail = -1;
251
+ }
252
+
253
+ if (!cells[i].has_seq_id(seq_id)) {
254
+ if (cells[i].pos >= 0) {
255
+ used--;
256
+ }
257
+
258
+ cells[i].pos = -1;
259
+ cells[i].src = -1;
260
+ cells[i].seq_id.clear();
261
+
262
+ if (new_head == size){
263
+ new_head = i;
264
+ }
265
+ } else {
266
+ cells[i].seq_id.clear();
267
+ cells[i].seq_id.insert(seq_id);
268
+ }
269
+ }
270
+
271
+ // If we freed up a slot, set head to it so searching can start there.
272
+ if (new_head != size && new_head < head) {
273
+ head = new_head;
274
+ }
275
+ }
276
+
277
+ void llama_memory_recurrent::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
278
+ if (shift == 0) {
279
+ return;
280
+ }
281
+
282
+ if (p0 < 0) {
283
+ p0 = 0;
284
+ }
285
+
286
+ if (p1 < 0) {
287
+ p1 = std::numeric_limits<llama_pos>::max();
288
+ }
289
+
290
+ // If there is no range then return early to avoid looping over the
291
+ if (p0 == p1) {
292
+ return;
293
+ }
294
+
295
+ // for Mamba-like or RWKV models, only the pos needs to be shifted
296
+ if (0 <= seq_id && seq_id < (int64_t) size) {
297
+ const int32_t tail_id = cells[seq_id].tail;
298
+ if (tail_id >= 0) {
299
+ auto & cell = cells[tail_id];
300
+ if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
301
+ cell.pos += shift;
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ void llama_memory_recurrent::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
308
+ if (d == 1) {
309
+ return;
310
+ }
311
+
312
+ if (p0 < 0) {
313
+ p0 = 0;
314
+ }
315
+
316
+ if (p1 < 0) {
317
+ p1 = std::numeric_limits<llama_pos>::max();
318
+ }
319
+
320
+ // If there is no range then return early to avoid looping over the cache.
321
+ if (p0 == p1) {
322
+ return;
323
+ }
324
+
325
+ // for Mamba-like or RWKV models, only the pos needs to be changed
326
+ if (0 <= seq_id && seq_id < (int64_t) size) {
327
+ const int32_t tail_id = cells[seq_id].tail;
328
+ if (tail_id >= 0) {
329
+ auto & cell = cells[tail_id];
330
+ if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
331
+ cell.pos /= d;
332
+ }
333
+ }
334
+ }
335
+ }
336
+
337
+ llama_pos llama_memory_recurrent::seq_pos_min(llama_seq_id seq_id) const {
338
+ llama_pos result = std::numeric_limits<llama_pos>::max();
339
+
340
+ for (uint32_t i = 0; i < size; ++i) {
341
+ if (cells[i].has_seq_id(seq_id)) {
342
+ result = std::min(result, cells[i].pos);
343
+ }
344
+ }
345
+
346
+ if (result == std::numeric_limits<llama_pos>::max()) {
347
+ result = -1;
348
+ }
349
+
350
+ return result;
351
+ }
352
+
353
+ llama_pos llama_memory_recurrent::seq_pos_max(llama_seq_id seq_id) const {
354
+ llama_pos result = -1;
355
+
356
+ for (uint32_t i = 0; i < size; ++i) {
357
+ if (cells[i].has_seq_id(seq_id)) {
358
+ result = std::max(result, cells[i].pos);
359
+ }
360
+ }
361
+
362
+ return result;
363
+ }
364
+
365
+ llama_memory_state_ptr llama_memory_recurrent::init_batch(const llama_batch & batch, uint32_t n_ubatch, bool embd_all) {
366
+ auto sbatch = llama_sbatch(batch, hparams.n_embd, false);
367
+
368
+ std::vector<llama_ubatch> ubatches;
369
+
370
+ while (sbatch.n_tokens > 0) {
371
+ llama_ubatch ubatch;
372
+
373
+ if (embd_all) {
374
+ // if all tokens are output, split by sequence
375
+ ubatch = sbatch.split_seq(n_ubatch);
376
+ } else {
377
+ ubatch = sbatch.split_equal(n_ubatch);
378
+ }
379
+
380
+ ubatches.push_back(ubatch);
381
+ }
382
+
383
+ if (!prepare(ubatches)) {
384
+ return std::make_unique<llama_memory_recurrent_state>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
385
+ }
386
+
387
+ return std::make_unique<llama_memory_recurrent_state>(this, std::move(sbatch), std::move(ubatches));
388
+ }
389
+
390
+ llama_memory_state_ptr llama_memory_recurrent::init_full() {
391
+ return std::make_unique<llama_memory_recurrent_state>(this);
392
+ }
393
+
394
+ llama_memory_state_ptr llama_memory_recurrent::init_update(llama_context * lctx, bool optimize) {
395
+ GGML_UNUSED(lctx);
396
+ GGML_UNUSED(optimize);
397
+
398
+ return std::make_unique<llama_memory_recurrent_state>(LLAMA_MEMORY_STATUS_NO_UPDATE);
399
+ }
400
+
401
+ bool llama_memory_recurrent::prepare(const std::vector<llama_ubatch> & ubatches) {
402
+ // simply remember the full state because it is very small for this type of cache
403
+ // TODO: optimize
404
+ auto org_cells = cells;
405
+ auto org_used = used;
406
+ auto org_head = head;
407
+
408
+ bool success = true;
409
+
410
+ for (const auto & ubatch : ubatches) {
411
+ if (!find_slot(ubatch)) {
412
+ success = false;
413
+ break;
414
+ }
415
+ }
416
+
417
+ // restore the original state
418
+ cells = std::move(org_cells);
419
+ used = org_used;
420
+ head = org_head;
421
+
422
+ return success;
423
+ }
424
+
425
+ bool llama_memory_recurrent::find_slot(const llama_ubatch & ubatch) {
426
+ const uint32_t n_seqs = ubatch.n_seqs;
427
+
428
+ const uint32_t n_seq_tokens = ubatch.n_seq_tokens;
429
+
430
+ // if we have enough unused cells before the current head ->
431
+ // better to start searching from the beginning of the cache, hoping to fill it
432
+ if (head > used + 2*n_seqs) {
433
+ head = 0;
434
+ }
435
+
436
+ // For recurrent state architectures (like Mamba or RWKV),
437
+ // each cache cell can store the state for a whole sequence.
438
+ // A slot should be always be contiguous.
439
+
440
+ // can only process batches with an equal number of new tokens in each sequence
441
+ GGML_ASSERT(ubatch.equal_seqs);
442
+
443
+ int32_t min = size - 1;
444
+ int32_t max = 0;
445
+
446
+ // everything should fit if all seq_ids are smaller than the max
447
+ for (uint32_t s = 0; s < n_seqs; ++s) {
448
+ const uint32_t n_seq_id = ubatch.n_seq_id[s];
449
+ for (uint32_t j = 0; j < n_seq_id; ++j) {
450
+ const llama_seq_id seq_id = ubatch.seq_id[s][j];
451
+
452
+ if (seq_id < 0 || (uint32_t) seq_id >= size) {
453
+ // too big seq_id
454
+ // TODO: would it be possible to resize the cache instead?
455
+ LLAMA_LOG_ERROR("%s: seq_id=%d >= n_seq_max=%u Try using a bigger --parallel value\n", __func__, seq_id, n_seq_max);
456
+ return false;
457
+ }
458
+ if (j > 0) {
459
+ auto & seq = cells[seq_id];
460
+ if (seq.tail >= 0) {
461
+ auto & cell = cells[seq.tail];
462
+ // clear cells from seq_ids that become shared
463
+ // (should not normally happen, but let's handle it anyway)
464
+ cell.seq_id.erase(seq_id);
465
+ seq.tail = -1;
466
+ if (cell.seq_id.empty()) {
467
+ cell.pos = -1;
468
+ cell.src = -1;
469
+ used -= 1;
470
+ }
471
+ }
472
+ }
473
+ }
474
+ }
475
+
476
+ #ifndef NDEBUG
477
+ {
478
+ std::vector<int32_t> tails_verif;
479
+ tails_verif.assign(size, -1);
480
+ for (uint32_t i = 0; i < size; ++i) {
481
+ auto & cell = cells[i];
482
+ for (llama_seq_id seq_id : cell.seq_id) {
483
+ if (tails_verif[seq_id] != -1) {
484
+ LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tails_verif[seq_id]);
485
+ }
486
+ tails_verif[seq_id] = i;
487
+ }
488
+ }
489
+ for (uint32_t i = 0; i < size; ++i) {
490
+ if (tails_verif[i] != cells[i].tail) {
491
+ LLAMA_LOG_ERROR("%s: wrong tail for seq_id %d, (%d instead of %d)\n", __func__, i, cells[i].tail, tails_verif[i]);
492
+ }
493
+ }
494
+ }
495
+ #endif
496
+
497
+ // find next empty cell
498
+ uint32_t next_empty_cell = head;
499
+
500
+ for (uint32_t i = 0; i < size; ++i) {
501
+ if (next_empty_cell >= size) { next_empty_cell -= size; }
502
+ auto & cell = cells[next_empty_cell];
503
+ if (cell.is_empty()) { break; }
504
+ next_empty_cell += 1;
505
+ }
506
+
507
+ // find usable cell range
508
+ for (uint32_t s = 0; s < n_seqs; ++s) {
509
+ const llama_seq_id seq_id = ubatch.seq_id[s][0];
510
+ auto & seq_meta = cells[seq_id];
511
+ bool has_cell = false;
512
+ if (seq_meta.tail >= 0) {
513
+ auto & cell = cells[seq_meta.tail];
514
+ GGML_ASSERT(cell.has_seq_id(seq_id));
515
+ // does this seq_id "own" the cell?
516
+ if (cell.seq_id.size() == 1) { has_cell = true; }
517
+ }
518
+ if (!has_cell) {
519
+ auto & empty_cell = cells[next_empty_cell];
520
+ GGML_ASSERT(empty_cell.is_empty());
521
+ // copy old tail into the empty cell
522
+ if (seq_meta.tail >= 0) {
523
+ auto & orig_cell = cells[seq_meta.tail];
524
+ empty_cell.pos = orig_cell.pos;
525
+ empty_cell.src = orig_cell.src;
526
+ orig_cell.seq_id.erase(seq_id);
527
+ empty_cell.seq_id.insert(seq_id); // will be overwritten
528
+ GGML_ASSERT(!orig_cell.is_empty()); // has at least one remaining seq_id
529
+ }
530
+ seq_meta.tail = next_empty_cell;
531
+ // find next empty cell
532
+ if (s + 1 < n_seqs) {
533
+ for (uint32_t i = 0; i < size; ++i) {
534
+ next_empty_cell += 1;
535
+ if (next_empty_cell >= size) { next_empty_cell -= size; }
536
+ auto & cell = cells[next_empty_cell];
537
+ if (cell.is_empty()) { break; }
538
+ }
539
+ }
540
+ }
541
+ if (min > seq_meta.tail) { min = seq_meta.tail; }
542
+ if (max < seq_meta.tail) { max = seq_meta.tail; }
543
+ }
544
+
545
+ // gather and re-order
546
+ for (uint32_t s = 0; s < n_seqs; ++s) {
547
+ const int32_t dst_id = s + min;
548
+ const int32_t src_id = cells[ubatch.seq_id[s][0]].tail;
549
+ if (dst_id != src_id) {
550
+ auto & dst_cell = cells[dst_id];
551
+ auto & src_cell = cells[src_id];
552
+
553
+ std::swap(dst_cell.pos, src_cell.pos);
554
+ std::swap(dst_cell.src, src_cell.src);
555
+ std::swap(dst_cell.seq_id, src_cell.seq_id);
556
+
557
+ // swap tails
558
+ for (uint32_t i = 0; i < size; ++i) {
559
+ int32_t & tail = cells[i].tail;
560
+ if (tail == src_id) {
561
+ tail = dst_id;
562
+ } else if (tail == dst_id) {
563
+ tail = src_id;
564
+ }
565
+ }
566
+ }
567
+ }
568
+
569
+ // update the pos of the used seqs
570
+ for (uint32_t s = 0; s < n_seqs; ++s) {
571
+ const llama_pos last_pos = ubatch.pos[n_seq_tokens * s + n_seq_tokens - 1];
572
+ const int32_t cell_id = s + min;
573
+ auto & cell = cells[cell_id];
574
+
575
+ if (cell.pos >= 0 && last_pos != cell.pos + (llama_pos) n_seq_tokens) {
576
+ // What should happen when the pos backtracks or skips a value?
577
+ // Clearing the state mid-batch would require special-casing which isn't done.
578
+ LLAMA_LOG_WARN("%s: non-consecutive token position %d after %d for sequence %d with %u new tokens\n",
579
+ __func__, last_pos, cell.pos, ubatch.seq_id[s][0], n_seq_tokens);
580
+ }
581
+ cell.pos = last_pos;
582
+ cell.seq_id.clear();
583
+ for (int32_t j = 0; j < ubatch.n_seq_id[s]; ++j) {
584
+ const llama_seq_id seq_id = ubatch.seq_id[s][j];
585
+ cell.seq_id.insert(seq_id);
586
+ cells[seq_id].tail = cell_id;
587
+ }
588
+ }
589
+
590
+ // Find first cell without src refs, to use as the zero-ed state
591
+ {
592
+ // TODO: bake-in src refcounts in the cell metadata
593
+ std::vector<int32_t> refcounts(size, 0);
594
+ for (size_t i = 0; i < size; ++i) {
595
+ const int32_t src = cells[i].src;
596
+ if (src >= 0) {
597
+ refcounts[src] += 1;
598
+ }
599
+ }
600
+
601
+ rs_z = -1;
602
+ for (int i = min; i <= max; ++i) {
603
+ if (refcounts[i] == 0) {
604
+ rs_z = i;
605
+ break;
606
+ }
607
+ }
608
+
609
+ for (int i = min; i <= max; ++i) {
610
+ if (cells[i].src < 0) {
611
+ GGML_ASSERT(rs_z >= 0);
612
+ cells[i].src0 = rs_z;
613
+ } else {
614
+ // Stage the source ids for all used cells to allow correct seq_* behavior
615
+ // and still make these values available when setting the inputs
616
+ cells[i].src0 = cells[i].src;
617
+ }
618
+ cells[i].src = i; // avoid moving or clearing twice
619
+ }
620
+ }
621
+
622
+ // allow getting the range of used cells, from head to head + n
623
+ head = min;
624
+ n = max - min + 1;
625
+ used = std::count_if(cells.begin(), cells.end(),
626
+ [](const mem_cell & cell){ return !cell.is_empty(); });
627
+
628
+ // sanity check
629
+ return n >= n_seqs;
630
+ }
631
+
632
+ bool llama_memory_recurrent::get_can_shift() const {
633
+ // shifting the pos is trivial for recurrent models
634
+ return true;
635
+ }
636
+
637
+ size_t llama_memory_recurrent::total_size() const {
638
+ size_t size = 0;
639
+ for (const auto & buf : bufs) {
640
+ size += ggml_backend_buffer_get_size(buf.get());
641
+ }
642
+
643
+ return size;
644
+ }
645
+
646
+ size_t llama_memory_recurrent::size_r_bytes() const {
647
+ size_t size_r_bytes = 0;
648
+
649
+ for (const auto & r : r_l) {
650
+ if (r != nullptr) {
651
+ size_r_bytes += ggml_nbytes(r);
652
+ }
653
+ }
654
+
655
+ return size_r_bytes;
656
+ }
657
+
658
+ size_t llama_memory_recurrent::size_s_bytes() const {
659
+ size_t size_s_bytes = 0;
660
+
661
+ for (const auto & s : s_l) {
662
+ if (s != nullptr) {
663
+ size_s_bytes += ggml_nbytes(s);
664
+ }
665
+ }
666
+
667
+ return size_s_bytes;
668
+ }
669
+
670
+ void llama_memory_recurrent::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
671
+ std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive
672
+ uint32_t cell_count = 0;
673
+
674
+ // Count the number of cells with the specified seq_id
675
+ // Find all the ranges of cells with this seq id (or all, when -1)
676
+ uint32_t cell_range_begin = size;
677
+ for (uint32_t i = 0; i < size; ++i) {
678
+ const auto & cell = cells[i];
679
+ if ((seq_id == -1 && !cell.is_empty()) || cell.has_seq_id(seq_id)) {
680
+ ++cell_count;
681
+ if (cell_range_begin == size) {
682
+ cell_range_begin = i;
683
+ }
684
+ } else {
685
+ if (cell_range_begin != size) {
686
+ cell_ranges.emplace_back(cell_range_begin, i);
687
+ cell_range_begin = size;
688
+ }
689
+ }
690
+ }
691
+ if (cell_range_begin != size) {
692
+ cell_ranges.emplace_back(cell_range_begin, size);
693
+ }
694
+
695
+ // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
696
+ uint32_t cell_count_check = 0;
697
+ for (const auto & range : cell_ranges) {
698
+ cell_count_check += range.second - range.first;
699
+ }
700
+ GGML_ASSERT(cell_count == cell_count_check);
701
+
702
+ io.write(&cell_count, sizeof(cell_count));
703
+
704
+ state_write_meta(io, cell_ranges, seq_id);
705
+ state_write_data(io, cell_ranges);
706
+ }
707
+
708
+ void llama_memory_recurrent::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
709
+ uint32_t cell_count;
710
+ io.read_to(&cell_count, sizeof(cell_count));
711
+
712
+ bool res = true;
713
+
714
+ res = res && state_read_meta(io, cell_count, seq_id);
715
+ res = res && state_read_data(io, cell_count);
716
+
717
+ if (!res) {
718
+ if (seq_id == -1) {
719
+ clear(true);
720
+ } else {
721
+ seq_rm(seq_id, -1, -1);
722
+ }
723
+ throw std::runtime_error("failed to restore kv cache");
724
+ }
725
+ }
726
+
727
+ void llama_memory_recurrent::state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id) const {
728
+ for (const auto & range : cell_ranges) {
729
+ for (uint32_t i = range.first; i < range.second; ++i) {
730
+ const auto & cell = cells[i];
731
+ const llama_pos pos = cell.pos;
732
+ const uint32_t n_seq_id = seq_id == -1 ? cell.seq_id.size() : 0;
733
+
734
+ io.write(&pos, sizeof(pos));
735
+ io.write(&n_seq_id, sizeof(n_seq_id));
736
+
737
+ if (n_seq_id) {
738
+ for (auto seq_id : cell.seq_id) {
739
+ io.write(&seq_id, sizeof(seq_id));
740
+ }
741
+ }
742
+ }
743
+ }
744
+ }
745
+
746
+ void llama_memory_recurrent::state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const {
747
+ const uint32_t s_trans = 0;
748
+ const uint32_t n_layer = hparams.n_layer;
749
+
750
+ io.write(&s_trans, sizeof(s_trans));
751
+ io.write(&n_layer, sizeof(n_layer));
752
+
753
+ std::vector<uint8_t> tmp_buf;
754
+
755
+ // Iterate and write all the keys first, each row is a cell
756
+ // Get whole range at a time
757
+ for (uint32_t il = 0; il < n_layer; ++il) {
758
+
759
+ // Write key type
760
+ const int32_t r_type_i = (int32_t)r_l[il]->type;
761
+ io.write(&r_type_i, sizeof(r_type_i));
762
+
763
+ // Write row size of key
764
+ const uint64_t r_size_row = ggml_row_size(r_l[il]->type, hparams.n_embd_r());
765
+ io.write(&r_size_row, sizeof(r_size_row));
766
+
767
+ // Read each range of cells of k_size length each into tmp_buf and write out
768
+ for (const auto & range : cell_ranges) {
769
+ const size_t range_size = range.second - range.first;
770
+ const size_t buf_size = range_size * r_size_row;
771
+ io.write_tensor(r_l[il], range.first * r_size_row, buf_size);
772
+ }
773
+ }
774
+
775
+ if (!s_trans) {
776
+ for (uint32_t il = 0; il < n_layer; ++il) {
777
+
778
+ // Write value type
779
+ const int32_t s_type_i = (int32_t)s_l[il]->type;
780
+ io.write(&s_type_i, sizeof(s_type_i));
781
+
782
+ // Write row size of value
783
+ const uint64_t s_size_row = ggml_row_size(s_l[il]->type, hparams.n_embd_s());
784
+ io.write(&s_size_row, sizeof(s_size_row));
785
+
786
+ // Read each range of cells of s_size length each into tmp_buf and write out
787
+ for (const auto & range : cell_ranges) {
788
+ const size_t range_size = range.second - range.first;
789
+ const size_t buf_size = range_size * s_size_row;
790
+ io.write_tensor(s_l[il], range.first * s_size_row, buf_size);
791
+ }
792
+ }
793
+ } else {
794
+ // When v is transposed, we also need the element size and get the element ranges from each row
795
+ const uint32_t mem_size = size;
796
+ for (uint32_t il = 0; il < n_layer; ++il) {
797
+ const uint32_t n_embd_s = hparams.n_embd_s();
798
+
799
+ // Write value type
800
+ const int32_t s_type_i = (int32_t)s_l[il]->type;
801
+ io.write(&s_type_i, sizeof(s_type_i));
802
+
803
+ // Write element size
804
+ const uint32_t s_size_el = ggml_type_size(s_l[il]->type);
805
+ io.write(&s_size_el, sizeof(s_size_el));
806
+
807
+ // Write GQA embedding size
808
+ io.write(&n_embd_s, sizeof(n_embd_s));
809
+
810
+ // For each row, we get the element values of each cell
811
+ for (uint32_t j = 0; j < n_embd_s; ++j) {
812
+ // Read each range of cells of v_size_el length each into tmp_buf and write out
813
+ for (const auto & range : cell_ranges) {
814
+ const size_t range_size = range.second - range.first;
815
+ const size_t src_offset = (range.first + j * mem_size) * s_size_el;
816
+ const size_t buf_size = range_size * s_size_el;
817
+ io.write_tensor(s_l[il], src_offset, buf_size);
818
+ }
819
+ }
820
+ }
821
+ }
822
+ }
823
+
824
+ bool llama_memory_recurrent::state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id) {
825
+ if (dest_seq_id != -1) {
826
+ // single sequence
827
+
828
+ seq_rm(dest_seq_id, -1, -1);
829
+
830
+ llama_sbatch sbatch;
831
+ llama_ubatch batch = sbatch.reserve_ubatch(cell_count, /* has_embd */ false);
832
+
833
+ batch.n_tokens = cell_count;
834
+ batch.n_seq_tokens = cell_count;
835
+ batch.n_seqs = 1;
836
+
837
+ for (uint32_t i = 0; i < cell_count; ++i) {
838
+ llama_pos pos;
839
+ uint32_t n_seq_id;
840
+
841
+ io.read_to(&pos, sizeof(pos));
842
+ io.read_to(&n_seq_id, sizeof(n_seq_id));
843
+
844
+ if (n_seq_id != 0) {
845
+ LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
846
+ return false;
847
+ }
848
+
849
+ batch.pos[i] = pos;
850
+ }
851
+ batch.n_seq_id[0] = 1;
852
+ batch.seq_id[0] = &dest_seq_id;
853
+
854
+ if (!find_slot(batch)) {
855
+ LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
856
+ return false;
857
+ }
858
+
859
+ // DEBUG CHECK: kv.head should be our first cell, kv.head + cell_count - 1 should be our last cell (verify seq_id and pos values)
860
+ // Assume that this is one contiguous block of cells
861
+ GGML_ASSERT(head + cell_count <= size);
862
+ GGML_ASSERT(cells[head].pos == batch.pos[0]);
863
+ GGML_ASSERT(cells[head + cell_count - 1].pos == batch.pos[cell_count - 1]);
864
+ GGML_ASSERT(cells[head].has_seq_id(dest_seq_id));
865
+ GGML_ASSERT(cells[head + cell_count - 1].has_seq_id(dest_seq_id));
866
+ } else {
867
+ // whole KV cache restore
868
+
869
+ if (cell_count > size) {
870
+ LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
871
+ return false;
872
+ }
873
+
874
+ clear(true);
875
+
876
+ for (uint32_t i = 0; i < cell_count; ++i) {
877
+ auto & cell = cells[i];
878
+
879
+ llama_pos pos;
880
+ uint32_t n_seq_id;
881
+
882
+ io.read_to(&pos, sizeof(pos));
883
+ io.read_to(&n_seq_id, sizeof(n_seq_id));
884
+
885
+ cell.pos = pos;
886
+
887
+ for (uint32_t j = 0; j < n_seq_id; ++j) {
888
+ llama_seq_id seq_id;
889
+ io.read_to(&seq_id, sizeof(seq_id));
890
+
891
+ // TODO: llama_memory_recurrent should have a notion of max sequences
892
+ //if (seq_id < 0 || (uint32_t) seq_id >= llama_n_seq_max(ctx)) {
893
+ if (seq_id < 0) {
894
+ //LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, llama_n_seq_max(ctx));
895
+ LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, inf)\n", __func__, seq_id);
896
+ return false;
897
+ }
898
+
899
+ cell.seq_id.insert(seq_id);
900
+
901
+ int32_t & tail = cells[seq_id].tail;
902
+ if (tail != -1) {
903
+ LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tail);
904
+ return false;
905
+ }
906
+ tail = i;
907
+ }
908
+ }
909
+
910
+ head = 0;
911
+ used = cell_count;
912
+ }
913
+
914
+ for (uint32_t i = 0; i < cell_count; ++i) {
915
+ uint32_t cell_id = head + i;
916
+ // make sure the recurrent states will keep their restored state
917
+ cells[cell_id].src = cell_id;
918
+ }
919
+
920
+ return true;
921
+ }
922
+
923
+ bool llama_memory_recurrent::state_read_data(llama_io_read_i & io, uint32_t cell_count) {
924
+ uint32_t s_trans;
925
+ uint32_t n_layer;
926
+ io.read_to(&s_trans, sizeof(s_trans));
927
+ io.read_to(&n_layer, sizeof(n_layer));
928
+
929
+ if (n_layer != hparams.n_layer) {
930
+ LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer);
931
+ return false;
932
+ }
933
+ if (cell_count > size) {
934
+ LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size);
935
+ return false;
936
+ }
937
+ if (false != (bool) s_trans) {
938
+ LLAMA_LOG_ERROR("%s: incompatible s transposition\n", __func__);
939
+ return false;
940
+ }
941
+
942
+ // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
943
+ for (uint32_t il = 0; il < n_layer; ++il) {
944
+
945
+ // Read type of key
946
+ int32_t r_type_i_ref;
947
+ io.read_to(&r_type_i_ref, sizeof(r_type_i_ref));
948
+ const int32_t r_type_i = (int32_t) r_l[il]->type;
949
+ if (r_type_i != r_type_i_ref) {
950
+ LLAMA_LOG_ERROR("%s: mismatched r type (%d != %d, layer %d)\n", __func__, r_type_i, r_type_i_ref, il);
951
+ return false;
952
+ }
953
+
954
+ // Read row size of key
955
+ uint64_t r_size_row_ref;
956
+ io.read_to(&r_size_row_ref, sizeof(r_size_row_ref));
957
+ const size_t r_size_row = ggml_row_size(r_l[il]->type, hparams.n_embd_r());
958
+ if (r_size_row != r_size_row_ref) {
959
+ LLAMA_LOG_ERROR("%s: mismatched r row size (%zu != %zu, layer %d)\n", __func__, r_size_row, (size_t) r_size_row_ref, il);
960
+ return false;
961
+ }
962
+
963
+ if (cell_count) {
964
+ // Read and set the keys for the whole cell range
965
+ ggml_backend_tensor_set(r_l[il], io.read(cell_count * r_size_row), head * r_size_row, cell_count * r_size_row);
966
+ }
967
+ }
968
+
969
+ if (!s_trans) {
970
+ for (uint32_t il = 0; il < n_layer; ++il) {
971
+
972
+ // Read type of value
973
+ int32_t s_type_i_ref;
974
+ io.read_to(&s_type_i_ref, sizeof(s_type_i_ref));
975
+ const int32_t s_type_i = (int32_t)s_l[il]->type;
976
+ if (s_type_i != s_type_i_ref) {
977
+ LLAMA_LOG_ERROR("%s: mismatched s type (%d != %d, layer %d)\n", __func__, s_type_i, s_type_i_ref, il);
978
+ return false;
979
+ }
980
+
981
+ // Read row size of value
982
+ uint64_t s_size_row_ref;
983
+ io.read_to(&s_size_row_ref, sizeof(s_size_row_ref));
984
+ const size_t s_size_row = ggml_row_size(s_l[il]->type, hparams.n_embd_s());
985
+ if (s_size_row != s_size_row_ref) {
986
+ LLAMA_LOG_ERROR("%s: mismatched s row size (%zu != %zu, layer %d)\n", __func__, s_size_row, (size_t) s_size_row_ref, il);
987
+ return false;
988
+ }
989
+
990
+ if (cell_count) {
991
+ // Read and set the values for the whole cell range
992
+ ggml_backend_tensor_set(s_l[il], io.read(cell_count * s_size_row), head * s_size_row, cell_count * s_size_row);
993
+ }
994
+ }
995
+ } else {
996
+ // For each layer, read the values for each cell (transposed)
997
+ for (uint32_t il = 0; il < n_layer; ++il) {
998
+ const uint32_t n_embd_s = hparams.n_embd_s();
999
+
1000
+ // Read type of value
1001
+ int32_t s_type_i_ref;
1002
+ io.read_to(&s_type_i_ref, sizeof(s_type_i_ref));
1003
+ const int32_t s_type_i = (int32_t)s_l[il]->type;
1004
+ if (s_type_i != s_type_i_ref) {
1005
+ LLAMA_LOG_ERROR("%s: mismatched s type (%d != %d, layer %d)\n", __func__, s_type_i, s_type_i_ref, il);
1006
+ return false;
1007
+ }
1008
+
1009
+ // Read element size of value
1010
+ uint32_t s_size_el_ref;
1011
+ io.read_to(&s_size_el_ref, sizeof(s_size_el_ref));
1012
+ const size_t s_size_el = ggml_type_size(s_l[il]->type);
1013
+ if (s_size_el != s_size_el_ref) {
1014
+ LLAMA_LOG_ERROR("%s: mismatched s element size (%zu != %zu, layer %d)\n", __func__, s_size_el, (size_t) s_size_el_ref, il);
1015
+ return false;
1016
+ }
1017
+
1018
+ // Read state embedding size
1019
+ uint32_t n_embd_s_ref;
1020
+ io.read_to(&n_embd_s_ref, sizeof(n_embd_s_ref));
1021
+ if (n_embd_s != n_embd_s_ref) {
1022
+ LLAMA_LOG_ERROR("%s: mismatched s embedding size (%u != %u, layer %d)\n", __func__, n_embd_s, n_embd_s_ref, il);
1023
+ return false;
1024
+ }
1025
+
1026
+ if (cell_count) {
1027
+ // For each row in the transposed matrix, read the values for the whole cell range
1028
+ for (uint32_t j = 0; j < n_embd_s; ++j) {
1029
+ const size_t dst_offset = (head + j * size) * s_size_el;
1030
+ ggml_backend_tensor_set(s_l[il], io.read(cell_count * s_size_el), dst_offset, cell_count * s_size_el);
1031
+ }
1032
+ }
1033
+ }
1034
+ }
1035
+
1036
+ return true;
1037
+ }
1038
+
1039
+ //
1040
+ // llama_memory_recurrent_state
1041
+ //
1042
+
1043
+ llama_memory_recurrent_state::llama_memory_recurrent_state(llama_memory_status status) : status(status) {}
1044
+
1045
+ llama_memory_recurrent_state::llama_memory_recurrent_state(
1046
+ llama_memory_recurrent * mem) : status(LLAMA_MEMORY_STATUS_SUCCESS), mem(mem), is_full(true) {
1047
+ }
1048
+
1049
+ llama_memory_recurrent_state::llama_memory_recurrent_state(
1050
+ llama_memory_recurrent * mem,
1051
+ llama_sbatch sbatch,
1052
+ std::vector<llama_ubatch> ubatches) : status(LLAMA_MEMORY_STATUS_SUCCESS), mem(mem), sbatch(std::move(sbatch)), ubatches(std::move(ubatches)) {}
1053
+
1054
+ llama_memory_recurrent_state::~llama_memory_recurrent_state() = default;
1055
+
1056
+ bool llama_memory_recurrent_state::next() {
1057
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1058
+
1059
+ if (++i_next >= ubatches.size()) {
1060
+ return false;
1061
+ }
1062
+
1063
+ return true;
1064
+ }
1065
+
1066
+ bool llama_memory_recurrent_state::apply() {
1067
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1068
+
1069
+ mem->find_slot(ubatches[i_next]);
1070
+
1071
+ return true;
1072
+ }
1073
+
1074
+ std::vector<int64_t> & llama_memory_recurrent_state::out_ids() {
1075
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1076
+
1077
+ return sbatch.out_ids;
1078
+ }
1079
+
1080
+ llama_memory_status llama_memory_recurrent_state::get_status() const {
1081
+ return status;
1082
+ }
1083
+
1084
+ const llama_ubatch & llama_memory_recurrent_state::get_ubatch() const {
1085
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1086
+
1087
+ return ubatches[i_next];
1088
+ }
1089
+
1090
+ uint32_t llama_memory_recurrent_state::get_n_rs() const {
1091
+ return is_full ? mem->size : mem->n;
1092
+ }
1093
+
1094
+ uint32_t llama_memory_recurrent_state::get_head() const {
1095
+ return is_full ? 0 : mem->head;
1096
+ }
1097
+
1098
+ int32_t llama_memory_recurrent_state::get_rs_z() const {
1099
+ return is_full ? 0 : mem->rs_z;
1100
+ }
1101
+
1102
+ uint32_t llama_memory_recurrent_state::get_size() const {
1103
+ return mem->size;
1104
+ }
1105
+
1106
+ ggml_tensor * llama_memory_recurrent_state::get_r_l(int32_t il) const {
1107
+ return mem->r_l[il];
1108
+ }
1109
+
1110
+ ggml_tensor * llama_memory_recurrent_state::get_s_l(int32_t il) const {
1111
+ return mem->s_l[il];
1112
+ }
1113
+
1114
+ int32_t llama_memory_recurrent_state::s_copy(int i) const {
1115
+ return mem->cells[i + mem->head].src0;
1116
+ }