cui-llama.rn 1.7.3 → 1.7.6

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