cui-llama.rn 1.5.0 → 1.6.1

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 (324) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +345 -319
  3. package/android/build.gradle +116 -116
  4. package/android/gradle.properties +5 -5
  5. package/android/src/main/AndroidManifest.xml +4 -4
  6. package/android/src/main/CMakeLists.txt +129 -124
  7. package/android/src/main/java/com/rnllama/LlamaContext.java +648 -645
  8. package/android/src/main/java/com/rnllama/RNLlama.java +695 -695
  9. package/android/src/main/java/com/rnllama/RNLlamaPackage.java +48 -48
  10. package/android/src/main/jni-utils.h +100 -100
  11. package/android/src/main/jni.cpp +1279 -1263
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  13. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  14. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  15. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  16. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  17. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  18. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  19. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  20. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +135 -135
  21. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +136 -136
  22. package/cpp/LICENSE +21 -0
  23. package/cpp/README.md +4 -4
  24. package/cpp/chat.cpp +1 -1
  25. package/cpp/common.cpp +17 -2
  26. package/cpp/common.h +7 -3
  27. package/cpp/ggml-alloc.c +4 -1
  28. package/cpp/ggml-cpp.h +1 -1
  29. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  30. package/cpp/ggml-cpu/amx/amx.h +8 -0
  31. package/cpp/ggml-cpu/amx/common.h +91 -0
  32. package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
  33. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  34. package/cpp/{binary-ops.h → ggml-cpu/binary-ops.h} +1 -1
  35. package/cpp/ggml-cpu/common.h +72 -0
  36. package/cpp/{ggml-cpu-aarch64.cpp → ggml-cpu/ggml-cpu-aarch64.cpp} +809 -101
  37. package/cpp/{ggml-cpu.c → ggml-cpu/ggml-cpu.c} +109 -42
  38. package/cpp/{ggml-cpu.cpp → ggml-cpu/ggml-cpu.cpp} +3 -0
  39. package/cpp/{ops.cpp → ggml-cpu/ops.cpp} +246 -160
  40. package/cpp/{ops.h → ggml-cpu/ops.h} +2 -20
  41. package/cpp/{sgemm.cpp → ggml-cpu/sgemm.cpp} +501 -0
  42. package/cpp/{simd-mappings.h → ggml-cpu/simd-mappings.h} +7 -3
  43. package/cpp/{unary-ops.h → ggml-cpu/unary-ops.h} +1 -1
  44. package/cpp/ggml-cpu.h +5 -0
  45. package/cpp/ggml-impl.h +16 -9
  46. package/cpp/ggml-llama-sim.metallib +0 -0
  47. package/cpp/ggml-llama.metallib +0 -0
  48. package/cpp/ggml-metal-impl.h +597 -597
  49. package/cpp/ggml-metal.m +496 -47
  50. package/cpp/ggml.c +134 -244
  51. package/cpp/ggml.h +62 -95
  52. package/cpp/json-schema-to-grammar.cpp +3 -0
  53. package/cpp/llama-arch.cpp +46 -17
  54. package/cpp/llama-arch.h +9 -0
  55. package/cpp/llama-batch.cpp +5 -1
  56. package/cpp/llama-batch.h +2 -1
  57. package/cpp/llama-chat.cpp +31 -10
  58. package/cpp/llama-chat.h +3 -2
  59. package/cpp/llama-context.cpp +104 -489
  60. package/cpp/llama-context.h +14 -30
  61. package/cpp/llama-graph.cpp +69 -62
  62. package/cpp/llama-graph.h +21 -18
  63. package/cpp/llama-hparams.h +5 -0
  64. package/cpp/llama-kv-cache.cpp +1497 -391
  65. package/cpp/llama-kv-cache.h +272 -80
  66. package/cpp/llama-memory.h +11 -1
  67. package/cpp/llama-model.cpp +502 -176
  68. package/cpp/llama-model.h +13 -3
  69. package/cpp/llama-sampling.cpp +2 -1
  70. package/cpp/llama-vocab.cpp +8 -1
  71. package/cpp/llama.h +14 -11
  72. package/cpp/rn-llama.cpp +721 -873
  73. package/cpp/rn-llama.h +134 -138
  74. package/cpp/sampling.h +107 -107
  75. package/cpp/unicode-data.cpp +7034 -7034
  76. package/cpp/unicode-data.h +20 -20
  77. package/cpp/unicode.cpp +849 -849
  78. package/cpp/unicode.h +66 -66
  79. package/ios/CMakeLists.txt +119 -108
  80. package/ios/RNLlama.h +13 -7
  81. package/ios/RNLlama.mm +423 -405
  82. package/ios/RNLlamaContext.h +57 -57
  83. package/ios/RNLlamaContext.mm +833 -835
  84. package/ios/rnllama.xcframework/Info.plist +74 -74
  85. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/chat.h +143 -0
  86. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/common.h +681 -0
  87. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/cpu-common.h +72 -0
  88. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-alloc.h +76 -0
  89. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  90. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-backend.h +354 -0
  91. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-common.h +1857 -0
  92. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpp.h +39 -0
  93. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu.h +143 -0
  94. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-impl.h +601 -0
  95. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  96. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-metal.h +66 -0
  97. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-opt.h +216 -0
  98. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-quants.h +100 -0
  99. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-threading.h +14 -0
  100. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml.h +2189 -0
  101. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/gguf.h +202 -0
  102. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  103. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/json.hpp +24766 -0
  104. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-adapter.h +76 -0
  105. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-arch.h +437 -0
  106. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-batch.h +89 -0
  107. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-chat.h +57 -0
  108. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-context.h +249 -0
  109. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-cparams.h +38 -0
  110. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-cpp.h +30 -0
  111. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-grammar.h +173 -0
  112. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-graph.h +595 -0
  113. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-hparams.h +161 -0
  114. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-impl.h +61 -0
  115. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-io.h +35 -0
  116. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-kv-cache.h +405 -0
  117. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-memory.h +31 -0
  118. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-mmap.h +68 -0
  119. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-model-loader.h +169 -0
  120. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-model.h +419 -0
  121. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-sampling.h +32 -0
  122. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-vocab.h +125 -0
  123. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama.h +1437 -0
  124. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/log.h +132 -0
  125. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  126. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  127. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/rn-llama.h +134 -0
  128. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/sampling.h +107 -0
  129. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/speculative.h +28 -0
  130. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/unicode-data.h +20 -0
  131. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/unicode.h +66 -0
  132. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Info.plist +0 -0
  133. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/ggml-llama.metallib +0 -0
  134. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/rnllama +0 -0
  135. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/chat.h +143 -0
  136. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/common.h +681 -0
  137. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/cpu-common.h +72 -0
  138. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-alloc.h +76 -0
  139. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  140. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend.h +354 -0
  141. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-common.h +1857 -0
  142. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpp.h +39 -0
  143. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu.h +143 -0
  144. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-impl.h +601 -0
  145. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  146. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal.h +66 -0
  147. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-opt.h +216 -0
  148. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-quants.h +100 -0
  149. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-threading.h +14 -0
  150. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml.h +2189 -0
  151. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/gguf.h +202 -0
  152. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  153. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/json.hpp +24766 -0
  154. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-adapter.h +76 -0
  155. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-arch.h +437 -0
  156. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-batch.h +89 -0
  157. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-chat.h +57 -0
  158. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-context.h +249 -0
  159. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cparams.h +38 -0
  160. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cpp.h +30 -0
  161. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-grammar.h +173 -0
  162. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-graph.h +595 -0
  163. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-hparams.h +161 -0
  164. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-impl.h +61 -0
  165. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-io.h +35 -0
  166. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-kv-cache.h +405 -0
  167. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-memory.h +31 -0
  168. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-mmap.h +68 -0
  169. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model-loader.h +169 -0
  170. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model.h +419 -0
  171. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-sampling.h +32 -0
  172. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-vocab.h +125 -0
  173. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama.h +1437 -0
  174. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/log.h +132 -0
  175. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  176. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  177. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/rn-llama.h +134 -0
  178. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/sampling.h +107 -0
  179. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/speculative.h +28 -0
  180. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/unicode-data.h +20 -0
  181. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/unicode.h +66 -0
  182. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Info.plist +0 -0
  183. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/_CodeSignature/CodeResources +101 -0
  184. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/ggml-llama-sim.metallib +0 -0
  185. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/rnllama +0 -0
  186. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/chat.h +143 -0
  187. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/common.h +681 -0
  188. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/cpu-common.h +72 -0
  189. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-alloc.h +76 -0
  190. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  191. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-backend.h +354 -0
  192. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-common.h +1857 -0
  193. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpp.h +39 -0
  194. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu.h +143 -0
  195. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-impl.h +601 -0
  196. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  197. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-metal.h +66 -0
  198. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-opt.h +216 -0
  199. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-quants.h +100 -0
  200. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-threading.h +14 -0
  201. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml.h +2189 -0
  202. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/gguf.h +202 -0
  203. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  204. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/json.hpp +24766 -0
  205. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-adapter.h +76 -0
  206. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-arch.h +437 -0
  207. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-batch.h +89 -0
  208. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-chat.h +57 -0
  209. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-context.h +249 -0
  210. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-cparams.h +38 -0
  211. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-cpp.h +30 -0
  212. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-grammar.h +173 -0
  213. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-graph.h +595 -0
  214. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-hparams.h +161 -0
  215. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-impl.h +61 -0
  216. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-io.h +35 -0
  217. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-kv-cache.h +405 -0
  218. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-memory.h +31 -0
  219. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-mmap.h +68 -0
  220. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-model-loader.h +169 -0
  221. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-model.h +419 -0
  222. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-sampling.h +32 -0
  223. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-vocab.h +125 -0
  224. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama.h +1437 -0
  225. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/log.h +132 -0
  226. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  227. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  228. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/rn-llama.h +134 -0
  229. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/sampling.h +107 -0
  230. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/speculative.h +28 -0
  231. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/unicode-data.h +20 -0
  232. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/unicode.h +66 -0
  233. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Info.plist +0 -0
  234. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/ggml-llama.metallib +0 -0
  235. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/rnllama +0 -0
  236. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/chat.h +143 -0
  237. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/common.h +681 -0
  238. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/cpu-common.h +72 -0
  239. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-alloc.h +76 -0
  240. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  241. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend.h +354 -0
  242. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-common.h +1857 -0
  243. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpp.h +39 -0
  244. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu.h +143 -0
  245. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-impl.h +601 -0
  246. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  247. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal.h +66 -0
  248. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-opt.h +216 -0
  249. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-quants.h +100 -0
  250. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-threading.h +14 -0
  251. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml.h +2189 -0
  252. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/gguf.h +202 -0
  253. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  254. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/json.hpp +24766 -0
  255. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-adapter.h +76 -0
  256. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-arch.h +437 -0
  257. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-batch.h +89 -0
  258. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-chat.h +57 -0
  259. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-context.h +249 -0
  260. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cparams.h +38 -0
  261. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cpp.h +30 -0
  262. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-grammar.h +173 -0
  263. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-graph.h +595 -0
  264. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-hparams.h +161 -0
  265. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-impl.h +61 -0
  266. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-io.h +35 -0
  267. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-kv-cache.h +405 -0
  268. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-memory.h +31 -0
  269. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-mmap.h +68 -0
  270. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model-loader.h +169 -0
  271. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model.h +419 -0
  272. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-sampling.h +32 -0
  273. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-vocab.h +125 -0
  274. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama.h +1437 -0
  275. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/log.h +132 -0
  276. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  277. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  278. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/rn-llama.h +134 -0
  279. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/sampling.h +107 -0
  280. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/speculative.h +28 -0
  281. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/unicode-data.h +20 -0
  282. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/unicode.h +66 -0
  283. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Info.plist +0 -0
  284. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/_CodeSignature/CodeResources +101 -0
  285. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/ggml-llama-sim.metallib +0 -0
  286. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/rnllama +0 -0
  287. package/jest/mock.js +203 -203
  288. package/lib/commonjs/NativeRNLlama.js +1 -2
  289. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  290. package/lib/commonjs/chat.js.map +1 -1
  291. package/lib/commonjs/grammar.js +12 -31
  292. package/lib/commonjs/grammar.js.map +1 -1
  293. package/lib/commonjs/index.js +47 -47
  294. package/lib/commonjs/index.js.map +1 -1
  295. package/lib/commonjs/package.json +1 -0
  296. package/lib/module/NativeRNLlama.js +2 -0
  297. package/lib/module/NativeRNLlama.js.map +1 -1
  298. package/lib/module/chat.js +2 -0
  299. package/lib/module/chat.js.map +1 -1
  300. package/lib/module/grammar.js +14 -31
  301. package/lib/module/grammar.js.map +1 -1
  302. package/lib/module/index.js +47 -45
  303. package/lib/module/index.js.map +1 -1
  304. package/lib/module/package.json +1 -0
  305. package/lib/typescript/NativeRNLlama.d.ts +10 -4
  306. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  307. package/lib/typescript/index.d.ts.map +1 -1
  308. package/llama-rn.podspec +48 -48
  309. package/package.json +233 -233
  310. package/src/NativeRNLlama.ts +431 -426
  311. package/src/chat.ts +44 -44
  312. package/src/grammar.ts +854 -854
  313. package/src/index.ts +495 -487
  314. /package/cpp/{binary-ops.cpp → ggml-cpu/binary-ops.cpp} +0 -0
  315. /package/cpp/{ggml-cpu-aarch64.h → ggml-cpu/ggml-cpu-aarch64.h} +0 -0
  316. /package/cpp/{ggml-cpu-impl.h → ggml-cpu/ggml-cpu-impl.h} +0 -0
  317. /package/cpp/{ggml-cpu-quants.c → ggml-cpu/ggml-cpu-quants.c} +0 -0
  318. /package/cpp/{ggml-cpu-quants.h → ggml-cpu/ggml-cpu-quants.h} +0 -0
  319. /package/cpp/{ggml-cpu-traits.cpp → ggml-cpu/ggml-cpu-traits.cpp} +0 -0
  320. /package/cpp/{ggml-cpu-traits.h → ggml-cpu/ggml-cpu-traits.h} +0 -0
  321. /package/cpp/{sgemm.h → ggml-cpu/sgemm.h} +0 -0
  322. /package/cpp/{unary-ops.cpp → ggml-cpu/unary-ops.cpp} +0 -0
  323. /package/cpp/{vec.cpp → ggml-cpu/vec.cpp} +0 -0
  324. /package/cpp/{vec.h → ggml-cpu/vec.h} +0 -0
@@ -0,0 +1,1437 @@
1
+ #ifndef LLAMA_H
2
+ #define LLAMA_H
3
+
4
+ #include "ggml.h"
5
+ #include "ggml-cpu.h"
6
+ #include "ggml-backend.h"
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <stdio.h>
11
+ #include <stdbool.h>
12
+ #include <random>
13
+
14
+ #ifdef LLAMA_SHARED
15
+ # if defined(_WIN32) && !defined(__MINGW32__)
16
+ # ifdef LLAMA_BUILD
17
+ # define LLAMA_API __declspec(dllexport)
18
+ # else
19
+ # define LLAMA_API __declspec(dllimport)
20
+ # endif
21
+ # else
22
+ # define LLAMA_API __attribute__ ((visibility ("default")))
23
+ # endif
24
+ #else
25
+ # define LLAMA_API
26
+ #endif
27
+
28
+ #ifdef __GNUC__
29
+ # define DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
30
+ #elif defined(_MSC_VER)
31
+ # define DEPRECATED(func, hint) __declspec(deprecated(hint)) func
32
+ #else
33
+ # define DEPRECATED(func, hint) func
34
+ #endif
35
+
36
+ #define LLAMA_DEFAULT_SEED 0xFFFFFFFF
37
+
38
+ #define LLAMA_TOKEN_NULL -1
39
+
40
+ #define LLAMA_FILE_MAGIC_GGLA 0x67676c61u // 'ggla'
41
+ #define LLAMA_FILE_MAGIC_GGSN 0x6767736eu // 'ggsn'
42
+ #define LLAMA_FILE_MAGIC_GGSQ 0x67677371u // 'ggsq'
43
+
44
+ #define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
45
+ #define LLAMA_SESSION_VERSION 9
46
+
47
+ #define LLAMA_STATE_SEQ_MAGIC LLAMA_FILE_MAGIC_GGSQ
48
+ #define LLAMA_STATE_SEQ_VERSION 2
49
+
50
+ #ifdef __cplusplus
51
+ extern "C" {
52
+ #endif
53
+
54
+ //
55
+ // C interface
56
+ //
57
+ // TODO: show sample usage
58
+ //
59
+
60
+ struct llama_vocab;
61
+ struct llama_model;
62
+ struct llama_context;
63
+ struct llama_sampler;
64
+ struct llama_kv_cache;
65
+
66
+ typedef int32_t llama_pos;
67
+ typedef int32_t llama_token;
68
+ typedef int32_t llama_seq_id;
69
+
70
+ enum llama_vocab_type {
71
+ LLAMA_VOCAB_TYPE_NONE = 0, // For models without vocab
72
+ LLAMA_VOCAB_TYPE_SPM = 1, // LLaMA tokenizer based on byte-level BPE with byte fallback
73
+ LLAMA_VOCAB_TYPE_BPE = 2, // GPT-2 tokenizer based on byte-level BPE
74
+ LLAMA_VOCAB_TYPE_WPM = 3, // BERT tokenizer based on WordPiece
75
+ LLAMA_VOCAB_TYPE_UGM = 4, // T5 tokenizer based on Unigram
76
+ LLAMA_VOCAB_TYPE_RWKV = 5, // RWKV tokenizer based on greedy tokenization
77
+ };
78
+
79
+ // pre-tokenization types
80
+ enum llama_vocab_pre_type {
81
+ LLAMA_VOCAB_PRE_TYPE_DEFAULT = 0,
82
+ LLAMA_VOCAB_PRE_TYPE_LLAMA3 = 1,
83
+ LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_LLM = 2,
84
+ LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER = 3,
85
+ LLAMA_VOCAB_PRE_TYPE_FALCON = 4,
86
+ LLAMA_VOCAB_PRE_TYPE_MPT = 5,
87
+ LLAMA_VOCAB_PRE_TYPE_STARCODER = 6,
88
+ LLAMA_VOCAB_PRE_TYPE_GPT2 = 7,
89
+ LLAMA_VOCAB_PRE_TYPE_REFACT = 8,
90
+ LLAMA_VOCAB_PRE_TYPE_COMMAND_R = 9,
91
+ LLAMA_VOCAB_PRE_TYPE_STABLELM2 = 10,
92
+ LLAMA_VOCAB_PRE_TYPE_QWEN2 = 11,
93
+ LLAMA_VOCAB_PRE_TYPE_OLMO = 12,
94
+ LLAMA_VOCAB_PRE_TYPE_DBRX = 13,
95
+ LLAMA_VOCAB_PRE_TYPE_SMAUG = 14,
96
+ LLAMA_VOCAB_PRE_TYPE_PORO = 15,
97
+ LLAMA_VOCAB_PRE_TYPE_CHATGLM3 = 16,
98
+ LLAMA_VOCAB_PRE_TYPE_CHATGLM4 = 17,
99
+ LLAMA_VOCAB_PRE_TYPE_VIKING = 18,
100
+ LLAMA_VOCAB_PRE_TYPE_JAIS = 19,
101
+ LLAMA_VOCAB_PRE_TYPE_TEKKEN = 20,
102
+ LLAMA_VOCAB_PRE_TYPE_SMOLLM = 21,
103
+ LLAMA_VOCAB_PRE_TYPE_CODESHELL = 22,
104
+ LLAMA_VOCAB_PRE_TYPE_BLOOM = 23,
105
+ LLAMA_VOCAB_PRE_TYPE_GPT3_FINNISH = 24,
106
+ LLAMA_VOCAB_PRE_TYPE_EXAONE = 25,
107
+ LLAMA_VOCAB_PRE_TYPE_CHAMELEON = 26,
108
+ LLAMA_VOCAB_PRE_TYPE_MINERVA = 27,
109
+ LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM = 28,
110
+ LLAMA_VOCAB_PRE_TYPE_GPT4O = 29,
111
+ LLAMA_VOCAB_PRE_TYPE_SUPERBPE = 30,
112
+ LLAMA_VOCAB_PRE_TYPE_TRILLION = 31,
113
+ LLAMA_VOCAB_PRE_TYPE_BAILINGMOE = 32,
114
+ LLAMA_VOCAB_PRE_TYPE_LLAMA4 = 33,
115
+ LLAMA_VOCAB_PRE_TYPE_PIXTRAL = 34,
116
+ };
117
+
118
+ enum llama_rope_type {
119
+ LLAMA_ROPE_TYPE_NONE = -1,
120
+ LLAMA_ROPE_TYPE_NORM = 0,
121
+ LLAMA_ROPE_TYPE_NEOX = LM_GGML_ROPE_TYPE_NEOX,
122
+ LLAMA_ROPE_TYPE_MROPE = LM_GGML_ROPE_TYPE_MROPE,
123
+ LLAMA_ROPE_TYPE_VISION = LM_GGML_ROPE_TYPE_VISION,
124
+ };
125
+
126
+ enum llama_token_type { //TODO: remove, required until per token attributes are available from GGUF file
127
+ LLAMA_TOKEN_TYPE_UNDEFINED = 0,
128
+ LLAMA_TOKEN_TYPE_NORMAL = 1,
129
+ LLAMA_TOKEN_TYPE_UNKNOWN = 2,
130
+ LLAMA_TOKEN_TYPE_CONTROL = 3,
131
+ LLAMA_TOKEN_TYPE_USER_DEFINED = 4,
132
+ LLAMA_TOKEN_TYPE_UNUSED = 5,
133
+ LLAMA_TOKEN_TYPE_BYTE = 6,
134
+ };
135
+
136
+ enum llama_token_attr {
137
+ LLAMA_TOKEN_ATTR_UNDEFINED = 0,
138
+ LLAMA_TOKEN_ATTR_UNKNOWN = 1 << 0,
139
+ LLAMA_TOKEN_ATTR_UNUSED = 1 << 1,
140
+ LLAMA_TOKEN_ATTR_NORMAL = 1 << 2,
141
+ LLAMA_TOKEN_ATTR_CONTROL = 1 << 3, // SPECIAL?
142
+ LLAMA_TOKEN_ATTR_USER_DEFINED = 1 << 4,
143
+ LLAMA_TOKEN_ATTR_BYTE = 1 << 5,
144
+ LLAMA_TOKEN_ATTR_NORMALIZED = 1 << 6,
145
+ LLAMA_TOKEN_ATTR_LSTRIP = 1 << 7,
146
+ LLAMA_TOKEN_ATTR_RSTRIP = 1 << 8,
147
+ LLAMA_TOKEN_ATTR_SINGLE_WORD = 1 << 9,
148
+ };
149
+
150
+ // model file types
151
+ enum llama_ftype {
152
+ LLAMA_FTYPE_ALL_F32 = 0,
153
+ LLAMA_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
154
+ LLAMA_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
155
+ LLAMA_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
156
+ // LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
157
+ // LLAMA_FTYPE_MOSTLY_Q4_2 = 5, // support has been removed
158
+ // LLAMA_FTYPE_MOSTLY_Q4_3 = 6, // support has been removed
159
+ LLAMA_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
160
+ LLAMA_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
161
+ LLAMA_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
162
+ LLAMA_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
163
+ LLAMA_FTYPE_MOSTLY_Q3_K_S = 11, // except 1d tensors
164
+ LLAMA_FTYPE_MOSTLY_Q3_K_M = 12, // except 1d tensors
165
+ LLAMA_FTYPE_MOSTLY_Q3_K_L = 13, // except 1d tensors
166
+ LLAMA_FTYPE_MOSTLY_Q4_K_S = 14, // except 1d tensors
167
+ LLAMA_FTYPE_MOSTLY_Q4_K_M = 15, // except 1d tensors
168
+ LLAMA_FTYPE_MOSTLY_Q5_K_S = 16, // except 1d tensors
169
+ LLAMA_FTYPE_MOSTLY_Q5_K_M = 17, // except 1d tensors
170
+ LLAMA_FTYPE_MOSTLY_Q6_K = 18, // except 1d tensors
171
+ LLAMA_FTYPE_MOSTLY_IQ2_XXS = 19, // except 1d tensors
172
+ LLAMA_FTYPE_MOSTLY_IQ2_XS = 20, // except 1d tensors
173
+ LLAMA_FTYPE_MOSTLY_Q2_K_S = 21, // except 1d tensors
174
+ LLAMA_FTYPE_MOSTLY_IQ3_XS = 22, // except 1d tensors
175
+ LLAMA_FTYPE_MOSTLY_IQ3_XXS = 23, // except 1d tensors
176
+ LLAMA_FTYPE_MOSTLY_IQ1_S = 24, // except 1d tensors
177
+ LLAMA_FTYPE_MOSTLY_IQ4_NL = 25, // except 1d tensors
178
+ LLAMA_FTYPE_MOSTLY_IQ3_S = 26, // except 1d tensors
179
+ LLAMA_FTYPE_MOSTLY_IQ3_M = 27, // except 1d tensors
180
+ LLAMA_FTYPE_MOSTLY_IQ2_S = 28, // except 1d tensors
181
+ LLAMA_FTYPE_MOSTLY_IQ2_M = 29, // except 1d tensors
182
+ LLAMA_FTYPE_MOSTLY_IQ4_XS = 30, // except 1d tensors
183
+ LLAMA_FTYPE_MOSTLY_IQ1_M = 31, // except 1d tensors
184
+ LLAMA_FTYPE_MOSTLY_BF16 = 32, // except 1d tensors
185
+ //LLAMA_FTYPE_MOSTLY_Q4_0_4_4 = 33, // removed from gguf files, use Q4_0 and runtime repack
186
+ //LLAMA_FTYPE_MOSTLY_Q4_0_4_8 = 34, // removed from gguf files, use Q4_0 and runtime repack
187
+ //LLAMA_FTYPE_MOSTLY_Q4_0_8_8 = 35, // removed from gguf files, use Q4_0 and runtime repack
188
+ LLAMA_FTYPE_MOSTLY_TQ1_0 = 36, // except 1d tensors
189
+ LLAMA_FTYPE_MOSTLY_TQ2_0 = 37, // except 1d tensors
190
+
191
+ LLAMA_FTYPE_GUESSED = 1024, // not specified in the model file
192
+ };
193
+
194
+ enum llama_rope_scaling_type {
195
+ LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED = -1,
196
+ LLAMA_ROPE_SCALING_TYPE_NONE = 0,
197
+ LLAMA_ROPE_SCALING_TYPE_LINEAR = 1,
198
+ LLAMA_ROPE_SCALING_TYPE_YARN = 2,
199
+ LLAMA_ROPE_SCALING_TYPE_LONGROPE = 3,
200
+ LLAMA_ROPE_SCALING_TYPE_MAX_VALUE = LLAMA_ROPE_SCALING_TYPE_LONGROPE,
201
+ };
202
+
203
+ enum llama_pooling_type {
204
+ LLAMA_POOLING_TYPE_UNSPECIFIED = -1,
205
+ LLAMA_POOLING_TYPE_NONE = 0,
206
+ LLAMA_POOLING_TYPE_MEAN = 1,
207
+ LLAMA_POOLING_TYPE_CLS = 2,
208
+ LLAMA_POOLING_TYPE_LAST = 3,
209
+ LLAMA_POOLING_TYPE_RANK = 4, // used by reranking models to attach the classification head to the graph
210
+ };
211
+
212
+ enum llama_attention_type {
213
+ LLAMA_ATTENTION_TYPE_UNSPECIFIED = -1,
214
+ LLAMA_ATTENTION_TYPE_CAUSAL = 0,
215
+ LLAMA_ATTENTION_TYPE_NON_CAUSAL = 1,
216
+ };
217
+
218
+ enum llama_split_mode {
219
+ LLAMA_SPLIT_MODE_NONE = 0, // single GPU
220
+ LLAMA_SPLIT_MODE_LAYER = 1, // split layers and KV across GPUs
221
+ LLAMA_SPLIT_MODE_ROW = 2, // split layers and KV across GPUs, use tensor parallelism if supported
222
+ };
223
+
224
+ // TODO: simplify (https://github.com/ggml-org/llama.cpp/pull/9294#pullrequestreview-2286561979)
225
+ typedef struct llama_token_data {
226
+ llama_token id; // token id
227
+ float logit; // log-odds of the token
228
+ float p; // probability of the token
229
+ } llama_token_data;
230
+
231
+ typedef struct llama_token_data_array {
232
+ // TODO: consider SoA
233
+ // NOTE: this pointer can be modified by the samplers
234
+ llama_token_data * data;
235
+ size_t size;
236
+ int64_t selected; // this is the index in the data array (i.e. not the token id)
237
+ bool sorted;
238
+ } llama_token_data_array;
239
+
240
+ typedef bool (*llama_progress_callback)(float progress, void * user_data);
241
+
242
+ // Input data for llama_decode
243
+ // A llama_batch object can contain input about one or many sequences
244
+ // The provided arrays (i.e. token, embd, pos, etc.) must have size of n_tokens
245
+ //
246
+ // - token : the token ids of the input (used when embd is NULL)
247
+ // - embd : token embeddings (i.e. float vector of size n_embd) (used when token is NULL)
248
+ // - pos : the positions of the respective token in the sequence
249
+ // (if set to NULL, the token position will be tracked automatically by llama_decode)
250
+ // - seq_id : the sequence to which the respective token belongs
251
+ // (if set to NULL, the sequence ID will be assumed to be 0)
252
+ // - logits : if zero, the logits (and/or the embeddings) for the respective token will not be output
253
+ // (if set to NULL, only the logits for last token will be returned)
254
+ //
255
+ typedef struct llama_batch {
256
+ int32_t n_tokens;
257
+
258
+ llama_token * token;
259
+ float * embd;
260
+ llama_pos * pos;
261
+ int32_t * n_seq_id;
262
+ llama_seq_id ** seq_id;
263
+ int8_t * logits; // TODO: rename this to "output"
264
+ } llama_batch;
265
+
266
+ enum llama_model_kv_override_type {
267
+ LLAMA_KV_OVERRIDE_TYPE_INT,
268
+ LLAMA_KV_OVERRIDE_TYPE_FLOAT,
269
+ LLAMA_KV_OVERRIDE_TYPE_BOOL,
270
+ LLAMA_KV_OVERRIDE_TYPE_STR,
271
+ };
272
+
273
+ struct llama_model_kv_override {
274
+ enum llama_model_kv_override_type tag;
275
+
276
+ char key[128];
277
+
278
+ union {
279
+ int64_t val_i64;
280
+ double val_f64;
281
+ bool val_bool;
282
+ char val_str[128];
283
+ };
284
+ };
285
+
286
+ struct llama_model_tensor_buft_override {
287
+ const char * pattern;
288
+ lm_ggml_backend_buffer_type_t buft;
289
+ };
290
+
291
+ struct llama_model_params {
292
+ // NULL-terminated list of devices to use for offloading (if NULL, all available devices are used)
293
+ lm_ggml_backend_dev_t * devices;
294
+
295
+ // NULL-terminated list of buffer types to use for tensors that match a pattern
296
+ const struct llama_model_tensor_buft_override * tensor_buft_overrides;
297
+
298
+ int32_t n_gpu_layers; // number of layers to store in VRAM
299
+ enum llama_split_mode split_mode; // how to split the model across multiple GPUs
300
+
301
+ // the GPU that is used for the entire model when split_mode is LLAMA_SPLIT_MODE_NONE
302
+ int32_t main_gpu;
303
+
304
+ // proportion of the model (layers or rows) to offload to each GPU, size: llama_max_devices()
305
+ const float * tensor_split;
306
+
307
+ // Called with a progress value between 0.0 and 1.0. Pass NULL to disable.
308
+ // If the provided progress_callback returns true, model loading continues.
309
+ // If it returns false, model loading is immediately aborted.
310
+ llama_progress_callback progress_callback;
311
+
312
+ // context pointer passed to the progress callback
313
+ void * progress_callback_user_data;
314
+
315
+ // override key-value pairs of the model meta data
316
+ const struct llama_model_kv_override * kv_overrides;
317
+
318
+ // Keep the booleans together to avoid misalignment during copy-by-value.
319
+ bool vocab_only; // only load the vocabulary, no weights
320
+ bool use_mmap; // use mmap if possible
321
+ bool use_mlock; // force system to keep model in RAM
322
+ bool check_tensors; // validate model tensor data
323
+ };
324
+
325
+ // NOTE: changing the default values of parameters marked as [EXPERIMENTAL] may cause crashes or incorrect results in certain configurations
326
+ // https://github.com/ggml-org/llama.cpp/pull/7544
327
+ struct llama_context_params {
328
+ uint32_t n_ctx; // text context, 0 = from model
329
+ uint32_t n_batch; // logical maximum batch size that can be submitted to llama_decode
330
+ uint32_t n_ubatch; // physical maximum batch size
331
+ uint32_t n_seq_max; // max number of sequences (i.e. distinct states for recurrent models)
332
+ int32_t n_threads; // number of threads to use for generation
333
+ int32_t n_threads_batch; // number of threads to use for batch processing
334
+
335
+ enum llama_rope_scaling_type rope_scaling_type; // RoPE scaling type, from `enum llama_rope_scaling_type`
336
+ enum llama_pooling_type pooling_type; // whether to pool (sum) embedding results by sequence id
337
+ enum llama_attention_type attention_type; // attention type to use for embeddings
338
+
339
+ // ref: https://github.com/ggml-org/llama.cpp/pull/2054
340
+ float rope_freq_base; // RoPE base frequency, 0 = from model
341
+ float rope_freq_scale; // RoPE frequency scaling factor, 0 = from model
342
+ float yarn_ext_factor; // YaRN extrapolation mix factor, negative = from model
343
+ float yarn_attn_factor; // YaRN magnitude scaling factor
344
+ float yarn_beta_fast; // YaRN low correction dim
345
+ float yarn_beta_slow; // YaRN high correction dim
346
+ uint32_t yarn_orig_ctx; // YaRN original context size
347
+ float defrag_thold; // defragment the KV cache if holes/size > thold, < 0 disabled (default)
348
+
349
+ lm_ggml_backend_sched_eval_callback cb_eval;
350
+ void * cb_eval_user_data;
351
+
352
+ enum lm_ggml_type type_k; // data type for K cache [EXPERIMENTAL]
353
+ enum lm_ggml_type type_v; // data type for V cache [EXPERIMENTAL]
354
+
355
+ // Keep the booleans together and at the end of the struct to avoid misalignment during copy-by-value.
356
+ // TODO: move at the end of the struct
357
+ bool logits_all; // the llama_decode() call computes all logits, not just the last one (DEPRECATED - set llama_batch.logits instead)
358
+ bool embeddings; // if true, extract embeddings (together with logits)
359
+ bool offload_kqv; // whether to offload the KQV ops (including the KV cache) to GPU
360
+ bool flash_attn; // whether to use flash attention [EXPERIMENTAL]
361
+ bool no_perf; // whether to measure performance timings
362
+
363
+ // Abort callback
364
+ // if it returns true, execution of llama_decode() will be aborted
365
+ // currently works only with CPU execution
366
+ lm_ggml_abort_callback abort_callback;
367
+ void * abort_callback_data;
368
+ };
369
+
370
+ // model quantization parameters
371
+ typedef struct llama_model_quantize_params {
372
+ int32_t nthread; // number of threads to use for quantizing, if <=0 will use std::thread::hardware_concurrency()
373
+ enum llama_ftype ftype; // quantize to this llama_ftype
374
+ enum lm_ggml_type output_tensor_type; // output tensor type
375
+ enum lm_ggml_type token_embedding_type; // token embeddings tensor type
376
+ bool allow_requantize; // allow quantizing non-f32/f16 tensors
377
+ bool quantize_output_tensor; // quantize output.weight
378
+ bool only_copy; // only copy tensors - ftype, allow_requantize and quantize_output_tensor are ignored
379
+ bool pure; // quantize all tensors to the default type
380
+ bool keep_split; // quantize to the same number of shards
381
+ void * imatrix; // pointer to importance matrix data
382
+ void * kv_overrides; // pointer to vector containing overrides
383
+ void * tensor_types; // pointer to vector containing tensor types
384
+ } llama_model_quantize_params;
385
+
386
+ typedef struct llama_logit_bias {
387
+ llama_token token;
388
+ float bias;
389
+ } llama_logit_bias;
390
+
391
+ typedef struct llama_sampler_chain_params {
392
+ bool no_perf; // whether to measure performance timings
393
+ } llama_sampler_chain_params;
394
+
395
+ // used in chat template
396
+ typedef struct llama_chat_message {
397
+ const char * role;
398
+ const char * content;
399
+ } llama_chat_message;
400
+
401
+ // lora adapter
402
+ struct llama_adapter_lora;
403
+
404
+ // Helpers for getting default parameters
405
+ // TODO: update API to start accepting pointers to params structs (https://github.com/ggml-org/llama.cpp/discussions/9172)
406
+ LLAMA_API struct llama_model_params llama_model_default_params(void);
407
+ LLAMA_API struct llama_context_params llama_context_default_params(void);
408
+ LLAMA_API struct llama_sampler_chain_params llama_sampler_chain_default_params(void);
409
+ LLAMA_API struct llama_model_quantize_params llama_model_quantize_default_params(void);
410
+
411
+ // Initialize the llama + ggml backend
412
+ // If numa is true, use NUMA optimizations
413
+ // Call once at the start of the program
414
+ LLAMA_API void llama_backend_init(void);
415
+
416
+ // Call once at the end of the program - currently only used for MPI
417
+ LLAMA_API void llama_backend_free(void);
418
+
419
+ //optional:
420
+ LLAMA_API void llama_numa_init(enum lm_ggml_numa_strategy numa);
421
+
422
+ // Optional: an auto threadpool gets created in ggml if not passed explicitly
423
+ LLAMA_API void llama_attach_threadpool(
424
+ struct llama_context * ctx,
425
+ lm_ggml_threadpool_t threadpool,
426
+ lm_ggml_threadpool_t threadpool_batch);
427
+
428
+ LLAMA_API void llama_detach_threadpool(struct llama_context * ctx);
429
+
430
+ DEPRECATED(LLAMA_API struct llama_model * llama_load_model_from_file(
431
+ const char * path_model,
432
+ struct llama_model_params params),
433
+ "use llama_model_load_from_file instead");
434
+
435
+ // Load the model from a file
436
+ // If the file is split into multiple parts, the file name must follow this pattern: <name>-%05d-of-%05d.gguf
437
+ // If the split file name does not follow this pattern, use llama_model_load_from_splits
438
+ LLAMA_API struct llama_model * llama_model_load_from_file(
439
+ const char * path_model,
440
+ struct llama_model_params params);
441
+
442
+ // Load the model from multiple splits (support custom naming scheme)
443
+ // The paths must be in the correct order
444
+ LLAMA_API struct llama_model * llama_model_load_from_splits(
445
+ const char ** paths,
446
+ size_t n_paths,
447
+ struct llama_model_params params);
448
+
449
+ DEPRECATED(LLAMA_API void llama_free_model(struct llama_model * model),
450
+ "use llama_model_free instead");
451
+
452
+ LLAMA_API void llama_model_free(struct llama_model * model);
453
+
454
+ LLAMA_API struct llama_context * llama_init_from_model(
455
+ struct llama_model * model,
456
+ struct llama_context_params params);
457
+
458
+ DEPRECATED(LLAMA_API struct llama_context * llama_new_context_with_model(
459
+ struct llama_model * model,
460
+ struct llama_context_params params),
461
+ "use llama_init_from_model instead");
462
+
463
+ // Frees all allocated memory
464
+ LLAMA_API void llama_free(struct llama_context * ctx);
465
+
466
+ LLAMA_API int64_t llama_time_us(void);
467
+
468
+ LLAMA_API size_t llama_max_devices(void);
469
+
470
+ LLAMA_API bool llama_supports_mmap (void);
471
+ LLAMA_API bool llama_supports_mlock (void);
472
+ LLAMA_API bool llama_supports_gpu_offload(void);
473
+ LLAMA_API bool llama_supports_rpc (void);
474
+
475
+ LLAMA_API uint32_t llama_n_ctx (const struct llama_context * ctx);
476
+ LLAMA_API uint32_t llama_n_batch (const struct llama_context * ctx);
477
+ LLAMA_API uint32_t llama_n_ubatch (const struct llama_context * ctx);
478
+ LLAMA_API uint32_t llama_n_seq_max (const struct llama_context * ctx);
479
+
480
+ DEPRECATED(LLAMA_API int32_t llama_n_ctx_train(const struct llama_model * model), "use llama_model_n_ctx_train instead");
481
+ DEPRECATED(LLAMA_API int32_t llama_n_embd (const struct llama_model * model), "use llama_model_n_embd instead");
482
+ DEPRECATED(LLAMA_API int32_t llama_n_layer (const struct llama_model * model), "use llama_model_n_layer instead");
483
+ DEPRECATED(LLAMA_API int32_t llama_n_head (const struct llama_model * model), "use llama_model_n_head instead");
484
+
485
+ DEPRECATED(LLAMA_API int32_t llama_n_vocab (const struct llama_vocab * vocab), "use llama_vocab_n_tokens instead");
486
+
487
+ LLAMA_API const struct llama_model * llama_get_model (const struct llama_context * ctx);
488
+ LLAMA_API struct llama_kv_cache * llama_get_kv_self ( struct llama_context * ctx);
489
+ LLAMA_API enum llama_pooling_type llama_pooling_type(const struct llama_context * ctx); // TODO: rename to llama_get_pooling_type
490
+
491
+ LLAMA_API const struct llama_vocab * llama_model_get_vocab(const struct llama_model * model);
492
+ LLAMA_API enum llama_rope_type llama_model_rope_type(const struct llama_model * model);
493
+
494
+ LLAMA_API int32_t llama_model_n_ctx_train(const struct llama_model * model);
495
+ LLAMA_API int32_t llama_model_n_embd (const struct llama_model * model);
496
+ LLAMA_API int32_t llama_model_n_layer (const struct llama_model * model);
497
+ LLAMA_API int32_t llama_model_n_head (const struct llama_model * model);
498
+ LLAMA_API int32_t llama_model_n_head_kv (const struct llama_model * model);
499
+
500
+ // Get the model's RoPE frequency scaling factor
501
+ LLAMA_API float llama_model_rope_freq_scale_train(const struct llama_model * model);
502
+
503
+ LLAMA_API enum llama_vocab_type llama_vocab_type(const struct llama_vocab * vocab);
504
+
505
+ LLAMA_API int32_t llama_vocab_n_tokens(const struct llama_vocab * vocab);
506
+
507
+ // Functions to access the model's GGUF metadata scalar values
508
+ // - The functions return the length of the string on success, or -1 on failure
509
+ // - The output string is always null-terminated and cleared on failure
510
+ // - When retrieving a string, an extra byte must be allocated to account for the null terminator
511
+ // - GGUF array values are not supported by these functions
512
+
513
+ // Get metadata value as a string by key name
514
+ LLAMA_API int32_t llama_model_meta_val_str(const struct llama_model * model, const char * key, char * buf, size_t buf_size);
515
+
516
+ // Get the number of metadata key/value pairs
517
+ LLAMA_API int32_t llama_model_meta_count(const struct llama_model * model);
518
+
519
+ // Get metadata key name by index
520
+ LLAMA_API int32_t llama_model_meta_key_by_index(const struct llama_model * model, int32_t i, char * buf, size_t buf_size);
521
+
522
+ // Get metadata value as a string by index
523
+ LLAMA_API int32_t llama_model_meta_val_str_by_index(const struct llama_model * model, int32_t i, char * buf, size_t buf_size);
524
+
525
+ // Get a string describing the model type
526
+ LLAMA_API int32_t llama_model_desc(const struct llama_model * model, char * buf, size_t buf_size);
527
+
528
+ // Returns the total size of all the tensors in the model in bytes
529
+ LLAMA_API uint64_t llama_model_size(const struct llama_model * model);
530
+
531
+ // Get the default chat template. Returns nullptr if not available
532
+ // If name is NULL, returns the default chat template
533
+ LLAMA_API const char * llama_model_chat_template(const struct llama_model * model, const char * name);
534
+
535
+ // Returns the total number of parameters in the model
536
+ LLAMA_API uint64_t llama_model_n_params(const struct llama_model * model);
537
+
538
+ // Returns true if the model contains an encoder that requires llama_encode() call
539
+ LLAMA_API bool llama_model_has_encoder(const struct llama_model * model);
540
+
541
+ // Returns true if the model contains a decoder that requires llama_decode() call
542
+ LLAMA_API bool llama_model_has_decoder(const struct llama_model * model);
543
+
544
+ // For encoder-decoder models, this function returns id of the token that must be provided
545
+ // to the decoder to start generating output sequence. For other models, it returns -1.
546
+ LLAMA_API llama_token llama_model_decoder_start_token(const struct llama_model * model);
547
+
548
+ // Returns true if the model is recurrent (like Mamba, RWKV, etc.)
549
+ LLAMA_API bool llama_model_is_recurrent(const struct llama_model * model);
550
+
551
+ // Returns 0 on success
552
+ LLAMA_API uint32_t llama_model_quantize(
553
+ const char * fname_inp,
554
+ const char * fname_out,
555
+ const llama_model_quantize_params * params);
556
+
557
+ //
558
+ // Adapters
559
+ //
560
+
561
+ // Load a LoRA adapter from file
562
+ LLAMA_API struct llama_adapter_lora * llama_adapter_lora_init(
563
+ struct llama_model * model,
564
+ const char * path_lora);
565
+
566
+ // Manually free a LoRA adapter
567
+ // Note: loaded adapters will be free when the associated model is deleted
568
+ LLAMA_API void llama_adapter_lora_free(struct llama_adapter_lora * adapter);
569
+
570
+ // The following functions operate on a llama_context, hence the naming: llama_verb_...
571
+
572
+ // Add a loaded LoRA adapter to given context
573
+ // This will not modify model's weight
574
+ LLAMA_API int32_t llama_set_adapter_lora(
575
+ struct llama_context * ctx,
576
+ struct llama_adapter_lora * adapter,
577
+ float scale);
578
+
579
+ // Remove a specific LoRA adapter from given context
580
+ // Return -1 if the adapter is not present in the context
581
+ LLAMA_API int32_t llama_rm_adapter_lora(
582
+ struct llama_context * ctx,
583
+ struct llama_adapter_lora * adapter);
584
+
585
+ // Remove all LoRA adapters from given context
586
+ LLAMA_API void llama_clear_adapter_lora(struct llama_context * ctx);
587
+
588
+ // Apply a loaded control vector to a llama_context, or if data is NULL, clear
589
+ // the currently loaded vector.
590
+ // n_embd should be the size of a single layer's control, and data should point
591
+ // to an n_embd x n_layers buffer starting from layer 1.
592
+ // il_start and il_end are the layer range the vector should apply to (both inclusive)
593
+ // See llama_control_vector_load in common to load a control vector.
594
+ LLAMA_API int32_t llama_apply_adapter_cvec(
595
+ struct llama_context * ctx,
596
+ const float * data,
597
+ size_t len,
598
+ int32_t n_embd,
599
+ int32_t il_start,
600
+ int32_t il_end);
601
+
602
+ //
603
+ // KV cache
604
+ //
605
+
606
+ // TODO: start using struct llama_kv_cache
607
+
608
+ // Information associated with an individual cell in the KV cache view.
609
+ struct llama_kv_cache_view_cell {
610
+ // The position for this cell. Takes KV cache shifts into account.
611
+ // May be negative if the cell is not populated.
612
+ llama_pos pos;
613
+ };
614
+
615
+ // An updateable view of the KV cache.
616
+ struct llama_kv_cache_view {
617
+ // Number of KV cache cells. This will be the same as the context size.
618
+ int32_t n_cells;
619
+
620
+ // Maximum number of sequences that can exist in a cell. It's not an error
621
+ // if there are more sequences in a cell than this value, however they will
622
+ // not be visible in the view cells_sequences.
623
+ int32_t n_seq_max;
624
+
625
+ // Number of tokens in the cache. For example, if there are two populated
626
+ // cells, the first with 1 sequence id in it and the second with 2 sequence
627
+ // ids then you'll have 3 tokens.
628
+ int32_t token_count;
629
+
630
+ // Number of populated cache cells.
631
+ int32_t used_cells;
632
+
633
+ // Maximum contiguous empty slots in the cache.
634
+ int32_t max_contiguous;
635
+
636
+ // Index to the start of the max_contiguous slot range. Can be negative
637
+ // when cache is full.
638
+ int32_t max_contiguous_idx;
639
+
640
+ // Information for an individual cell.
641
+ struct llama_kv_cache_view_cell * cells;
642
+
643
+ // The sequences for each cell. There will be n_seq_max items per cell.
644
+ llama_seq_id * cells_sequences;
645
+ };
646
+
647
+ // Create an empty KV cache view. (use only for debugging purposes)
648
+ LLAMA_API struct llama_kv_cache_view llama_kv_cache_view_init(const struct llama_context * ctx, int32_t n_seq_max);
649
+
650
+ // Free a KV cache view. (use only for debugging purposes)
651
+ LLAMA_API void llama_kv_cache_view_free(struct llama_kv_cache_view * view);
652
+
653
+ // Update the KV cache view structure with the current state of the KV cache. (use only for debugging purposes)
654
+ // TODO: change signature to llama_kv_cache_view_update(struct llama_kv_cache_view * view, const struct llama_context * ctx)
655
+ LLAMA_API void llama_kv_cache_view_update(const struct llama_context * ctx, struct llama_kv_cache_view * view);
656
+
657
+ ///
658
+
659
+ // Returns the number of tokens in the KV cache (slow, use only for debug)
660
+ // If a KV cell has multiple sequences assigned to it, it will be counted multiple times
661
+ LLAMA_API int32_t llama_kv_self_n_tokens(const struct llama_context * ctx);
662
+
663
+ DEPRECATED(LLAMA_API int32_t llama_get_kv_cache_token_count(const struct llama_context * ctx),
664
+ "use llama_kv_self_n_tokens instead");
665
+
666
+ // Returns the number of used KV cells (i.e. have at least one sequence assigned to them)
667
+ LLAMA_API int32_t llama_kv_self_used_cells(const struct llama_context * ctx);
668
+
669
+ DEPRECATED(LLAMA_API int32_t llama_get_kv_cache_used_cells(const struct llama_context * ctx),
670
+ "use llama_kv_self_used_cells instead");
671
+
672
+ // Clear the KV cache - both cell info is erased and KV data is zeroed
673
+ LLAMA_API void llama_kv_self_clear(
674
+ struct llama_context * ctx);
675
+
676
+ // Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
677
+ // Returns false if a partial sequence cannot be removed. Removing a whole sequence never fails
678
+ // seq_id < 0 : match any sequence
679
+ // p0 < 0 : [0, p1]
680
+ // p1 < 0 : [p0, inf)
681
+ LLAMA_API bool llama_kv_self_seq_rm(
682
+ struct llama_context * ctx,
683
+ llama_seq_id seq_id,
684
+ llama_pos p0,
685
+ llama_pos p1);
686
+
687
+ // Copy all tokens that belong to the specified sequence to another sequence
688
+ // Note that this does not allocate extra KV cache memory - it simply assigns the tokens to the new sequence
689
+ // p0 < 0 : [0, p1]
690
+ // p1 < 0 : [p0, inf)
691
+ LLAMA_API void llama_kv_self_seq_cp(
692
+ struct llama_context * ctx,
693
+ llama_seq_id seq_id_src,
694
+ llama_seq_id seq_id_dst,
695
+ llama_pos p0,
696
+ llama_pos p1);
697
+
698
+ // Removes all tokens that do not belong to the specified sequence
699
+ LLAMA_API void llama_kv_self_seq_keep(
700
+ struct llama_context * ctx,
701
+ llama_seq_id seq_id);
702
+
703
+ // Adds relative position "delta" to all tokens that belong to the specified sequence and have positions in [p0, p1)
704
+ // If the KV cache is RoPEd, the KV data is updated accordingly:
705
+ // - lazily on next llama_decode()
706
+ // - explicitly with llama_kv_self_update()
707
+ // p0 < 0 : [0, p1]
708
+ // p1 < 0 : [p0, inf)
709
+ LLAMA_API void llama_kv_self_seq_add(
710
+ struct llama_context * ctx,
711
+ llama_seq_id seq_id,
712
+ llama_pos p0,
713
+ llama_pos p1,
714
+ llama_pos delta);
715
+
716
+ // Integer division of the positions by factor of `d > 1`
717
+ // If the KV cache is RoPEd, the KV data is updated accordingly:
718
+ // - lazily on next llama_decode()
719
+ // - explicitly with llama_kv_self_update()
720
+ // p0 < 0 : [0, p1]
721
+ // p1 < 0 : [p0, inf)
722
+ LLAMA_API void llama_kv_self_seq_div(
723
+ struct llama_context * ctx,
724
+ llama_seq_id seq_id,
725
+ llama_pos p0,
726
+ llama_pos p1,
727
+ int d);
728
+
729
+ // Returns the largest position present in the KV cache for the specified sequence
730
+ LLAMA_API llama_pos llama_kv_self_seq_pos_max(
731
+ struct llama_context * ctx,
732
+ llama_seq_id seq_id);
733
+
734
+ // Defragment the KV cache
735
+ // This will be applied:
736
+ // - lazily on next llama_decode()
737
+ // - explicitly with llama_kv_self_update()
738
+ LLAMA_API void llama_kv_self_defrag(struct llama_context * ctx);
739
+
740
+ // Check if the context supports KV cache shifting
741
+ LLAMA_API bool llama_kv_self_can_shift(const struct llama_context * ctx);
742
+
743
+ // Apply the KV cache updates (such as K-shifts, defragmentation, etc.)
744
+ LLAMA_API void llama_kv_self_update(struct llama_context * ctx);
745
+
746
+ DEPRECATED(LLAMA_API void llama_kv_cache_clear(
747
+ struct llama_context * ctx),
748
+ "use llama_kv_self_clear instead");
749
+
750
+ DEPRECATED(LLAMA_API bool llama_kv_cache_seq_rm(
751
+ struct llama_context * ctx,
752
+ llama_seq_id seq_id,
753
+ llama_pos p0,
754
+ llama_pos p1),
755
+ "use llama_kv_self_seq_rm instead");
756
+
757
+ DEPRECATED(LLAMA_API void llama_kv_cache_seq_cp(
758
+ struct llama_context * ctx,
759
+ llama_seq_id seq_id_src,
760
+ llama_seq_id seq_id_dst,
761
+ llama_pos p0,
762
+ llama_pos p1),
763
+ "use llama_kv_self_seq_cp instead");
764
+
765
+ DEPRECATED(LLAMA_API void llama_kv_cache_seq_keep(
766
+ struct llama_context * ctx,
767
+ llama_seq_id seq_id),
768
+ "use llama_kv_self_seq_keep instead");
769
+
770
+ DEPRECATED(LLAMA_API void llama_kv_cache_seq_add(
771
+ struct llama_context * ctx,
772
+ llama_seq_id seq_id,
773
+ llama_pos p0,
774
+ llama_pos p1,
775
+ llama_pos delta),
776
+ "use llama_kv_self_seq_add instead");
777
+
778
+ DEPRECATED(LLAMA_API void llama_kv_cache_seq_div(
779
+ struct llama_context * ctx,
780
+ llama_seq_id seq_id,
781
+ llama_pos p0,
782
+ llama_pos p1,
783
+ int d),
784
+ "use llama_kv_self_seq_div instead");
785
+
786
+ DEPRECATED(LLAMA_API llama_pos llama_kv_cache_seq_pos_max(
787
+ struct llama_context * ctx,
788
+ llama_seq_id seq_id),
789
+ "use llama_kv_self_seq_pos_max instead");
790
+
791
+ DEPRECATED(LLAMA_API void llama_kv_cache_defrag(struct llama_context * ctx),
792
+ "use llama_kv_self_defrag instead");
793
+
794
+ DEPRECATED(LLAMA_API bool llama_kv_cache_can_shift(const struct llama_context * ctx),
795
+ "use llama_kv_self_can_shift instead");
796
+
797
+ DEPRECATED(LLAMA_API void llama_kv_cache_update(struct llama_context * ctx),
798
+ "use llama_kv_self_update instead");
799
+
800
+
801
+ //
802
+ // State / sessions
803
+ //
804
+
805
+ // Returns the *actual* size in bytes of the state
806
+ // (logits, embedding and kv_cache)
807
+ // Only use when saving the state, not when restoring it, otherwise the size may be too small.
808
+ LLAMA_API size_t llama_state_get_size(struct llama_context * ctx);
809
+ LLAMA_API DEPRECATED(size_t llama_get_state_size(struct llama_context * ctx),
810
+ "use llama_state_get_size instead");
811
+
812
+ // Copies the state to the specified destination address.
813
+ // Destination needs to have allocated enough memory.
814
+ // Returns the number of bytes copied
815
+ LLAMA_API size_t llama_state_get_data(
816
+ struct llama_context * ctx,
817
+ uint8_t * dst,
818
+ size_t size);
819
+ LLAMA_API DEPRECATED(size_t llama_copy_state_data(
820
+ struct llama_context * ctx,
821
+ uint8_t * dst),
822
+ "use llama_state_get_data instead");
823
+
824
+ // Set the state reading from the specified address
825
+ // Returns the number of bytes read
826
+ LLAMA_API size_t llama_state_set_data(
827
+ struct llama_context * ctx,
828
+ const uint8_t * src,
829
+ size_t size);
830
+ LLAMA_API DEPRECATED(size_t llama_set_state_data(
831
+ struct llama_context * ctx,
832
+ const uint8_t * src),
833
+ "use llama_state_set_data instead");
834
+
835
+ // Save/load session file
836
+ LLAMA_API bool llama_state_load_file(
837
+ struct llama_context * ctx,
838
+ const char * path_session,
839
+ llama_token * tokens_out,
840
+ size_t n_token_capacity,
841
+ size_t * n_token_count_out);
842
+ LLAMA_API DEPRECATED(bool llama_load_session_file(
843
+ struct llama_context * ctx,
844
+ const char * path_session,
845
+ llama_token * tokens_out,
846
+ size_t n_token_capacity,
847
+ size_t * n_token_count_out),
848
+ "use llama_state_load_file instead");
849
+
850
+ LLAMA_API bool llama_state_save_file(
851
+ struct llama_context * ctx,
852
+ const char * path_session,
853
+ const llama_token * tokens,
854
+ size_t n_token_count);
855
+ LLAMA_API DEPRECATED(bool llama_save_session_file(
856
+ struct llama_context * ctx,
857
+ const char * path_session,
858
+ const llama_token * tokens,
859
+ size_t n_token_count),
860
+ "use llama_state_save_file instead");
861
+
862
+ // Get the exact size needed to copy the KV cache of a single sequence
863
+ LLAMA_API size_t llama_state_seq_get_size(
864
+ struct llama_context * ctx,
865
+ llama_seq_id seq_id);
866
+
867
+ // Copy the KV cache of a single sequence into the specified buffer
868
+ LLAMA_API size_t llama_state_seq_get_data(
869
+ struct llama_context * ctx,
870
+ uint8_t * dst,
871
+ size_t size,
872
+ llama_seq_id seq_id);
873
+
874
+ // Copy the sequence data (originally copied with `llama_state_seq_get_data`) into the specified sequence
875
+ // Returns:
876
+ // - Positive: Ok
877
+ // - Zero: Failed to load
878
+ LLAMA_API size_t llama_state_seq_set_data(
879
+ struct llama_context * ctx,
880
+ const uint8_t * src,
881
+ size_t size,
882
+ llama_seq_id dest_seq_id);
883
+
884
+ LLAMA_API size_t llama_state_seq_save_file(
885
+ struct llama_context * ctx,
886
+ const char * filepath,
887
+ llama_seq_id seq_id,
888
+ const llama_token * tokens,
889
+ size_t n_token_count);
890
+
891
+ LLAMA_API size_t llama_state_seq_load_file(
892
+ struct llama_context * ctx,
893
+ const char * filepath,
894
+ llama_seq_id dest_seq_id,
895
+ llama_token * tokens_out,
896
+ size_t n_token_capacity,
897
+ size_t * n_token_count_out);
898
+
899
+ //
900
+ // Decoding
901
+ //
902
+
903
+ // Return batch for single sequence of tokens
904
+ // The sequence ID will be fixed to 0
905
+ // The position of the tokens will be tracked automatically by llama_decode
906
+ //
907
+ // NOTE: this is a helper function to facilitate transition to the new batch API - avoid using it
908
+ //
909
+ LLAMA_API struct llama_batch llama_batch_get_one(
910
+ llama_token * tokens,
911
+ int32_t n_tokens);
912
+
913
+ // Allocates a batch of tokens on the heap that can hold a maximum of n_tokens
914
+ // Each token can be assigned up to n_seq_max sequence ids
915
+ // The batch has to be freed with llama_batch_free()
916
+ // If embd != 0, llama_batch.embd will be allocated with size of n_tokens * embd * sizeof(float)
917
+ // Otherwise, llama_batch.token will be allocated to store n_tokens llama_token
918
+ // The rest of the llama_batch members are allocated with size n_tokens
919
+ // All members are left uninitialized
920
+ LLAMA_API struct llama_batch llama_batch_init(
921
+ int32_t n_tokens,
922
+ int32_t embd,
923
+ int32_t n_seq_max);
924
+
925
+ // Frees a batch of tokens allocated with llama_batch_init()
926
+ LLAMA_API void llama_batch_free(struct llama_batch batch);
927
+
928
+ // Processes a batch of tokens with the ecoder part of the encoder-decoder model.
929
+ // Stores the encoder output internally for later use by the decoder cross-attention layers.
930
+ // 0 - success
931
+ // < 0 - error. the KV cache state is restored to the state before this call
932
+ LLAMA_API int32_t llama_encode(
933
+ struct llama_context * ctx,
934
+ struct llama_batch batch);
935
+
936
+ // Positive return values does not mean a fatal error, but rather a warning.
937
+ // 0 - success
938
+ // 1 - could not find a KV slot for the batch (try reducing the size of the batch or increase the context)
939
+ // < 0 - error. the KV cache state is restored to the state before this call
940
+ LLAMA_API int32_t llama_decode(
941
+ struct llama_context * ctx,
942
+ struct llama_batch batch);
943
+
944
+ // Set the number of threads used for decoding
945
+ // n_threads is the number of threads used for generation (single token)
946
+ // n_threads_batch is the number of threads used for prompt and batch processing (multiple tokens)
947
+ LLAMA_API void llama_set_n_threads(struct llama_context * ctx, int32_t n_threads, int32_t n_threads_batch);
948
+
949
+ // Get the number of threads used for generation of a single token.
950
+ LLAMA_API int32_t llama_n_threads(struct llama_context * ctx);
951
+
952
+ // Get the number of threads used for prompt and batch processing (multiple token).
953
+ LLAMA_API int32_t llama_n_threads_batch(struct llama_context * ctx);
954
+
955
+ // Set whether the model is in embeddings mode or not
956
+ // If true, embeddings will be returned but logits will not
957
+ LLAMA_API void llama_set_embeddings(struct llama_context * ctx, bool embeddings);
958
+
959
+ // Set whether to use causal attention or not
960
+ // If set to true, the model will only attend to the past tokens
961
+ LLAMA_API void llama_set_causal_attn(struct llama_context * ctx, bool causal_attn);
962
+
963
+ // Set whether the model is in warmup mode or not
964
+ // If true, all model tensors are activated during llama_decode() to load and cache their weights.
965
+ LLAMA_API void llama_set_warmup(struct llama_context * ctx, bool warmup);
966
+
967
+ // Set abort callback
968
+ LLAMA_API void llama_set_abort_callback(struct llama_context * ctx, lm_ggml_abort_callback abort_callback, void * abort_callback_data);
969
+
970
+ // Wait until all computations are finished
971
+ // This is automatically done when using one of the functions below to obtain the computation results
972
+ // and is not necessary to call it explicitly in most cases
973
+ LLAMA_API void llama_synchronize(struct llama_context * ctx);
974
+
975
+ // Token logits obtained from the last call to llama_decode()
976
+ // The logits for which llama_batch.logits[i] != 0 are stored contiguously
977
+ // in the order they have appeared in the batch.
978
+ // Rows: number of tokens for which llama_batch.logits[i] != 0
979
+ // Cols: n_vocab
980
+ LLAMA_API float * llama_get_logits(struct llama_context * ctx);
981
+
982
+ // Logits for the ith token. For positive indices, Equivalent to:
983
+ // llama_get_logits(ctx) + ctx->output_ids[i]*n_vocab
984
+ // Negative indicies can be used to access logits in reverse order, -1 is the last logit.
985
+ // returns NULL for invalid ids.
986
+ LLAMA_API float * llama_get_logits_ith(struct llama_context * ctx, int32_t i);
987
+
988
+ // Get all output token embeddings.
989
+ // when pooling_type == LLAMA_POOLING_TYPE_NONE or when using a generative model,
990
+ // the embeddings for which llama_batch.logits[i] != 0 are stored contiguously
991
+ // in the order they have appeared in the batch.
992
+ // shape: [n_outputs*n_embd]
993
+ // Otherwise, returns NULL.
994
+ LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
995
+
996
+ // Get the embeddings for the ith token. For positive indices, Equivalent to:
997
+ // llama_get_embeddings(ctx) + ctx->output_ids[i]*n_embd
998
+ // Negative indicies can be used to access embeddings in reverse order, -1 is the last embedding.
999
+ // shape: [n_embd] (1-dimensional)
1000
+ // returns NULL for invalid ids.
1001
+ LLAMA_API float * llama_get_embeddings_ith(struct llama_context * ctx, int32_t i);
1002
+
1003
+ // Get the embeddings for a sequence id
1004
+ // Returns NULL if pooling_type is LLAMA_POOLING_TYPE_NONE
1005
+ // when pooling_type == LLAMA_POOLING_TYPE_RANK, returns float[1] with the rank of the sequence
1006
+ // otherwise: float[n_embd] (1-dimensional)
1007
+ LLAMA_API float * llama_get_embeddings_seq(struct llama_context * ctx, llama_seq_id seq_id);
1008
+
1009
+ //
1010
+ // Vocab
1011
+ //
1012
+
1013
+ LLAMA_API const char * llama_vocab_get_text(const struct llama_vocab * vocab, llama_token token);
1014
+
1015
+ LLAMA_API float llama_vocab_get_score(const struct llama_vocab * vocab, llama_token token);
1016
+
1017
+ LLAMA_API enum llama_token_attr llama_vocab_get_attr(const struct llama_vocab * vocab, llama_token token);
1018
+
1019
+ // Check if the token is supposed to end generation (end-of-generation, eg. EOS, EOT, etc.)
1020
+ LLAMA_API bool llama_vocab_is_eog(const struct llama_vocab * vocab, llama_token token);
1021
+
1022
+ // Identify if Token Id is a control token or a render-able token
1023
+ LLAMA_API bool llama_vocab_is_control(const struct llama_vocab * vocab, llama_token token);
1024
+
1025
+ // Special tokens
1026
+ LLAMA_API llama_token llama_vocab_bos(const struct llama_vocab * vocab); // beginning-of-sentence
1027
+ LLAMA_API llama_token llama_vocab_eos(const struct llama_vocab * vocab); // end-of-sentence
1028
+ LLAMA_API llama_token llama_vocab_eot(const struct llama_vocab * vocab); // end-of-turn
1029
+ LLAMA_API llama_token llama_vocab_sep(const struct llama_vocab * vocab); // sentence separator
1030
+ LLAMA_API llama_token llama_vocab_nl (const struct llama_vocab * vocab); // next-line
1031
+ LLAMA_API llama_token llama_vocab_pad(const struct llama_vocab * vocab); // padding
1032
+
1033
+ LLAMA_API bool llama_vocab_get_add_bos(const struct llama_vocab * vocab);
1034
+ LLAMA_API bool llama_vocab_get_add_eos(const struct llama_vocab * vocab);
1035
+
1036
+ LLAMA_API llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab);
1037
+ LLAMA_API llama_token llama_vocab_fim_suf(const struct llama_vocab * vocab);
1038
+ LLAMA_API llama_token llama_vocab_fim_mid(const struct llama_vocab * vocab);
1039
+ LLAMA_API llama_token llama_vocab_fim_pad(const struct llama_vocab * vocab);
1040
+ LLAMA_API llama_token llama_vocab_fim_rep(const struct llama_vocab * vocab);
1041
+ LLAMA_API llama_token llama_vocab_fim_sep(const struct llama_vocab * vocab);
1042
+
1043
+ DEPRECATED(LLAMA_API const char * llama_token_get_text(const struct llama_vocab * vocab, llama_token token), "use llama_vocab_get_text instead");
1044
+ DEPRECATED(LLAMA_API float llama_token_get_score(const struct llama_vocab * vocab, llama_token token), "use llama_vocab_get_score instead");
1045
+ DEPRECATED(LLAMA_API enum llama_token_attr llama_token_get_attr(const struct llama_vocab * vocab, llama_token token), "use llama_vocab_get_attr instead");
1046
+ DEPRECATED(LLAMA_API bool llama_token_is_eog(const struct llama_vocab * vocab, llama_token token), "use llama_vocab_is_eog instead");
1047
+ DEPRECATED(LLAMA_API bool llama_token_is_control(const struct llama_vocab * vocab, llama_token token), "use llama_vocab_is_control instead");
1048
+ DEPRECATED(LLAMA_API llama_token llama_token_bos(const struct llama_vocab * vocab), "use llama_vocab_bos instead");
1049
+ DEPRECATED(LLAMA_API llama_token llama_token_eos(const struct llama_vocab * vocab), "use llama_vocab_eos instead");
1050
+ DEPRECATED(LLAMA_API llama_token llama_token_eot(const struct llama_vocab * vocab), "use llama_vocab_eot instead");
1051
+ DEPRECATED(LLAMA_API llama_token llama_token_cls(const struct llama_vocab * vocab), "use llama_vocab_cls instead");
1052
+ DEPRECATED(LLAMA_API llama_token llama_token_sep(const struct llama_vocab * vocab), "use llama_vocab_sep instead");
1053
+ DEPRECATED(LLAMA_API llama_token llama_token_nl (const struct llama_vocab * vocab), "use llama_vocab_nl instead");
1054
+ DEPRECATED(LLAMA_API llama_token llama_token_pad(const struct llama_vocab * vocab), "use llama_vocab_pad instead");
1055
+ DEPRECATED(LLAMA_API bool llama_add_bos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_bos instead");
1056
+ DEPRECATED(LLAMA_API bool llama_add_eos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_eos instead");
1057
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_pre(const struct llama_vocab * vocab), "use llama_vocab_fim_pre instead");
1058
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_suf(const struct llama_vocab * vocab), "use llama_vocab_fim_suf instead");
1059
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_mid(const struct llama_vocab * vocab), "use llama_vocab_fim_mid instead");
1060
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_pad(const struct llama_vocab * vocab), "use llama_vocab_fim_pad instead");
1061
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_rep(const struct llama_vocab * vocab), "use llama_vocab_fim_rep instead");
1062
+ DEPRECATED(LLAMA_API llama_token llama_token_fim_sep(const struct llama_vocab * vocab), "use llama_vocab_fim_sep instead");
1063
+
1064
+ // CLS is equivalent to BOS
1065
+ DEPRECATED(LLAMA_API llama_token llama_vocab_cls(const struct llama_vocab * vocab), // classification
1066
+ "use llama_vocab_bos instead");
1067
+
1068
+ //
1069
+ // Tokenization
1070
+ //
1071
+ // The API is thread-safe.
1072
+ //
1073
+
1074
+ /// @details Convert the provided text into tokens.
1075
+ /// @param tokens The tokens pointer must be large enough to hold the resulting tokens.
1076
+ /// @return Returns the number of tokens on success, no more than n_tokens_max
1077
+ /// @return Returns a negative number on failure - the number of tokens that would have been returned
1078
+ /// @param add_special Allow to add BOS and EOS tokens if model is configured to do so.
1079
+ /// @param parse_special Allow tokenizing special and/or control tokens which otherwise are not exposed and treated
1080
+ /// as plaintext. Does not insert a leading space.
1081
+ LLAMA_API int32_t llama_tokenize(
1082
+ const struct llama_vocab * vocab,
1083
+ const char * text,
1084
+ int32_t text_len,
1085
+ llama_token * tokens,
1086
+ int32_t n_tokens_max,
1087
+ bool add_special,
1088
+ bool parse_special);
1089
+
1090
+ // Token Id -> Piece.
1091
+ // Uses the vocabulary in the provided context.
1092
+ // Does not write null terminator to the buffer.
1093
+ // User can skip up to 'lstrip' leading spaces before copying (useful when encoding/decoding multiple tokens with 'add_space_prefix')
1094
+ // @param special If true, special tokens are rendered in the output.
1095
+ LLAMA_API int32_t llama_token_to_piece(
1096
+ const struct llama_vocab * vocab,
1097
+ llama_token token,
1098
+ char * buf,
1099
+ int32_t length,
1100
+ int32_t lstrip,
1101
+ bool special);
1102
+
1103
+ /// @details Convert the provided tokens into text (inverse of llama_tokenize()).
1104
+ /// @param text The char pointer must be large enough to hold the resulting text.
1105
+ /// @return Returns the number of chars/bytes on success, no more than text_len_max.
1106
+ /// @return Returns a negative number on failure - the number of chars/bytes that would have been returned.
1107
+ /// @param remove_special Allow to remove BOS and EOS tokens if model is configured to do so.
1108
+ /// @param unparse_special If true, special tokens are rendered in the output.
1109
+ LLAMA_API int32_t llama_detokenize(
1110
+ const struct llama_vocab * vocab,
1111
+ const llama_token * tokens,
1112
+ int32_t n_tokens,
1113
+ char * text,
1114
+ int32_t text_len_max,
1115
+ bool remove_special,
1116
+ bool unparse_special);
1117
+
1118
+ //
1119
+ // Chat templates
1120
+ //
1121
+
1122
+ /// Apply chat template. Inspired by hf apply_chat_template() on python.
1123
+ /// Both "model" and "custom_template" are optional, but at least one is required. "custom_template" has higher precedence than "model"
1124
+ /// NOTE: This function does not use a jinja parser. It only support a pre-defined list of template. See more: https://github.com/ggml-org/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template
1125
+ /// @param tmpl A Jinja template to use for this chat. If this is nullptr, the model’s default chat template will be used instead.
1126
+ /// @param chat Pointer to a list of multiple llama_chat_message
1127
+ /// @param n_msg Number of llama_chat_message in this chat
1128
+ /// @param add_ass Whether to end the prompt with the token(s) that indicate the start of an assistant message.
1129
+ /// @param buf A buffer to hold the output formatted prompt. The recommended alloc size is 2 * (total number of characters of all messages)
1130
+ /// @param length The size of the allocated buffer
1131
+ /// @return The total number of bytes of the formatted prompt. If is it larger than the size of buffer, you may need to re-alloc it and then re-apply the template.
1132
+ LLAMA_API int32_t llama_chat_apply_template(
1133
+ const char * tmpl,
1134
+ const struct llama_chat_message * chat,
1135
+ size_t n_msg,
1136
+ bool add_ass,
1137
+ char * buf,
1138
+ int32_t length);
1139
+
1140
+ // Get list of built-in chat templates
1141
+ LLAMA_API int32_t llama_chat_builtin_templates(const char ** output, size_t len);
1142
+
1143
+ //
1144
+ // Sampling API
1145
+ //
1146
+ // Sample usage:
1147
+ //
1148
+ // // prepare the sampling chain at the start
1149
+ // auto sparams = llama_sampler_chain_default_params();
1150
+ //
1151
+ // llama_sampler * smpl = llama_sampler_chain_init(sparams);
1152
+ //
1153
+ // llama_sampler_chain_add(smpl, llama_sampler_init_top_k(50));
1154
+ // llama_sampler_chain_add(smpl, llama_sampler_init_top_p(0.9, 1));
1155
+ // llama_sampler_chain_add(smpl, llama_sampler_init_temp (0.8));
1156
+ //
1157
+ // // typically, the chain should end with a sampler such as "greedy", "dist" or "mirostat"
1158
+ // // this sampler will be responsible to select the actual token
1159
+ // llama_sampler_chain_add(smpl, llama_sampler_init_dist(seed));
1160
+ //
1161
+ // ...
1162
+ //
1163
+ // // decoding loop:
1164
+ // while (...) {
1165
+ // ...
1166
+ //
1167
+ // llama_decode(ctx, batch);
1168
+ //
1169
+ // // sample from the logits of the last token in the batch
1170
+ // const llama_token id = llama_sampler_sample(smpl, ctx, -1);
1171
+ //
1172
+ // // accepting the token updates the internal state of certain samplers (e.g. grammar, repetition, etc.)
1173
+ // llama_sampler_accept(smpl, id);
1174
+ // ...
1175
+ // }
1176
+ //
1177
+ // llama_sampler_free(smpl);
1178
+ //
1179
+ // TODO: In the future, llama_sampler will be utilized to offload the sampling to the backends (e.g. GPU).
1180
+ //
1181
+
1182
+ typedef void * llama_sampler_context_t;
1183
+
1184
+ // user code can implement the interface below in order to create custom llama_sampler
1185
+ struct llama_sampler_i {
1186
+ const char * (*name) (const struct llama_sampler * smpl); // can be NULL
1187
+ void (*accept)( struct llama_sampler * smpl, llama_token token); // can be NULL
1188
+ void (*apply) ( struct llama_sampler * smpl, llama_token_data_array * cur_p); // required
1189
+ void (*reset) ( struct llama_sampler * smpl); // can be NULL
1190
+ struct llama_sampler * (*clone) (const struct llama_sampler * smpl); // can be NULL if ctx is NULL
1191
+ void (*free) ( struct llama_sampler * smpl); // can be NULL if ctx is NULL
1192
+
1193
+ // TODO: API for internal libllama usage for appending the sampling to an existing lm_ggml_cgraph
1194
+ //void (*apply_ggml) (struct llama_sampler * smpl, ...);
1195
+ };
1196
+
1197
+ struct llama_sampler {
1198
+ const struct llama_sampler_i * iface;
1199
+ llama_sampler_context_t ctx;
1200
+ };
1201
+
1202
+ // mirror of llama_sampler_i:
1203
+ LLAMA_API struct llama_sampler * llama_sampler_init (const struct llama_sampler_i * iface, llama_sampler_context_t ctx);
1204
+ LLAMA_API const char * llama_sampler_name (const struct llama_sampler * smpl);
1205
+ LLAMA_API void llama_sampler_accept( struct llama_sampler * smpl, llama_token token);
1206
+ LLAMA_API void llama_sampler_apply ( struct llama_sampler * smpl, llama_token_data_array * cur_p);
1207
+ LLAMA_API void llama_sampler_reset ( struct llama_sampler * smpl);
1208
+ LLAMA_API struct llama_sampler * llama_sampler_clone (const struct llama_sampler * smpl);
1209
+ // important: do not free if the sampler has been added to a llama_sampler_chain (via llama_sampler_chain_add)
1210
+ LLAMA_API void llama_sampler_free ( struct llama_sampler * smpl);
1211
+
1212
+ // llama_sampler_chain
1213
+ // a type of llama_sampler that can chain multiple samplers one after another
1214
+
1215
+ LLAMA_API struct llama_sampler * llama_sampler_chain_init(struct llama_sampler_chain_params params);
1216
+
1217
+ // important: takes ownership of the sampler object and will free it when llama_sampler_free is called
1218
+ LLAMA_API void llama_sampler_chain_add( struct llama_sampler * chain, struct llama_sampler * smpl);
1219
+ LLAMA_API struct llama_sampler * llama_sampler_chain_get(const struct llama_sampler * chain, int32_t i);
1220
+ LLAMA_API int llama_sampler_chain_n (const struct llama_sampler * chain);
1221
+
1222
+ // after removing a sampler, the chain will no longer own it, and it will not be freed when the chain is freed
1223
+ LLAMA_API struct llama_sampler * llama_sampler_chain_remove( struct llama_sampler * chain, int32_t i);
1224
+
1225
+ // available samplers:
1226
+
1227
+ LLAMA_API struct llama_sampler * llama_sampler_init_greedy(void);
1228
+ LLAMA_API struct llama_sampler * llama_sampler_init_dist (uint32_t seed);
1229
+
1230
+ /// @details Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
1231
+ /// NOTE: Avoid using on the full vocabulary as the sorting can become slow. For example, apply top-k or top-p sampling first.
1232
+ DEPRECATED(LLAMA_API struct llama_sampler * llama_sampler_init_softmax (void),
1233
+ "will be removed in the future (see https://github.com/ggml-org/llama.cpp/pull/9896#discussion_r1800920915)");
1234
+
1235
+ /// @details Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
1236
+ /// Setting k <= 0 makes this a noop
1237
+ LLAMA_API struct llama_sampler * llama_sampler_init_top_k (int32_t k);
1238
+
1239
+ /// @details Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
1240
+ LLAMA_API struct llama_sampler * llama_sampler_init_top_p (float p, size_t min_keep);
1241
+
1242
+ /// @details Minimum P sampling as described in https://github.com/ggml-org/llama.cpp/pull/3841
1243
+ LLAMA_API struct llama_sampler * llama_sampler_init_min_p (float p, size_t min_keep);
1244
+
1245
+ /// @details Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
1246
+ LLAMA_API struct llama_sampler * llama_sampler_init_typical (float p, size_t min_keep);
1247
+
1248
+ /// #details Updates the logits l_i` = l_i/t. When t <= 0.0f, the maximum logit is kept at it's original value, the rest are set to -inf
1249
+ LLAMA_API struct llama_sampler * llama_sampler_init_temp (float t);
1250
+
1251
+ /// @details Dynamic temperature implementation (a.k.a. entropy) described in the paper https://arxiv.org/abs/2309.02772.
1252
+ LLAMA_API struct llama_sampler * llama_sampler_init_temp_ext (float t, float delta, float exponent);
1253
+
1254
+ /// @details XTC sampler as described in https://github.com/oobabooga/text-generation-webui/pull/6335
1255
+ LLAMA_API struct llama_sampler * llama_sampler_init_xtc (float p, float t, size_t min_keep, uint32_t seed);
1256
+
1257
+ /// @details Top n sigma sampling as described in academic paper "Top-nσ: Not All Logits Are You Need" https://arxiv.org/pdf/2411.07641
1258
+ LLAMA_API struct llama_sampler * llama_sampler_init_top_n_sigma(float n);
1259
+
1260
+ /// @details Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
1261
+ /// @param candidates A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.
1262
+ /// @param tau The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
1263
+ /// @param eta The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
1264
+ /// @param m The number of tokens considered in the estimation of `s_hat`. This is an arbitrary value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`. In the paper, they use `m = 100`, but you can experiment with different values to see how it affects the performance of the algorithm.
1265
+ /// @param mu Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
1266
+ LLAMA_API struct llama_sampler * llama_sampler_init_mirostat(
1267
+ int32_t n_vocab,
1268
+ uint32_t seed,
1269
+ float tau,
1270
+ float eta,
1271
+ int32_t m);
1272
+
1273
+ /// @details Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
1274
+ /// @param candidates A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.
1275
+ /// @param tau The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
1276
+ /// @param eta The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
1277
+ /// @param mu Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
1278
+ LLAMA_API struct llama_sampler * llama_sampler_init_mirostat_v2(
1279
+ uint32_t seed,
1280
+ float tau,
1281
+ float eta);
1282
+
1283
+ /// @details Intializes a GBNF grammar, see grammars/README.md for details.
1284
+ /// @param vocab The vocabulary that this grammar will be used with.
1285
+ /// @param grammar_str The production rules for the grammar, encoded as a string. Returns an empty grammar if empty. Returns NULL if parsing of grammar_str fails.
1286
+ /// @param grammar_root The name of the start symbol for the grammar.
1287
+ LLAMA_API struct llama_sampler * llama_sampler_init_grammar(
1288
+ const struct llama_vocab * vocab,
1289
+ const char * grammar_str,
1290
+ const char * grammar_root);
1291
+
1292
+ DEPRECATED(LLAMA_API struct llama_sampler * llama_sampler_init_grammar_lazy(
1293
+ const struct llama_vocab * vocab,
1294
+ const char * grammar_str,
1295
+ const char * grammar_root,
1296
+ const char ** trigger_words,
1297
+ size_t num_trigger_words,
1298
+ const llama_token * trigger_tokens,
1299
+ size_t num_trigger_tokens),
1300
+ "use llama_sampler_init_grammar_lazy_patterns instead");
1301
+
1302
+
1303
+ /// @details Lazy grammar sampler, introduced in https://github.com/ggml-org/llama.cpp/pull/9639
1304
+ /// @param trigger_patterns A list of patterns that will trigger the grammar sampler. Pattern will be matched from the start of the generation output, and grammar sampler will be fed content starting from its first match group.
1305
+ /// @param trigger_tokens A list of tokens that will trigger the grammar sampler. Grammar sampler will be fed content starting from the trigger token included.
1306
+ LLAMA_API struct llama_sampler * llama_sampler_init_grammar_lazy_patterns(
1307
+ const struct llama_vocab * vocab,
1308
+ const char * grammar_str,
1309
+ const char * grammar_root,
1310
+ const char ** trigger_patterns,
1311
+ size_t num_trigger_patterns,
1312
+ const llama_token * trigger_tokens,
1313
+ size_t num_trigger_tokens);
1314
+
1315
+
1316
+ /// NOTE: Avoid using on the full vocabulary as searching for repeated tokens can become slow. For example, apply top-k or top-p sampling first.
1317
+ LLAMA_API struct llama_sampler * llama_sampler_init_penalties(
1318
+ int32_t penalty_last_n, // last n tokens to penalize (0 = disable penalty, -1 = context size)
1319
+ float penalty_repeat, // 1.0 = disabled
1320
+ float penalty_freq, // 0.0 = disabled
1321
+ float penalty_present); // 0.0 = disabled
1322
+
1323
+ /// @details DRY sampler, designed by p-e-w, as described in: https://github.com/oobabooga/text-generation-webui/pull/5677, porting Koboldcpp implementation authored by pi6am: https://github.com/LostRuins/koboldcpp/pull/982
1324
+ LLAMA_API struct llama_sampler * llama_sampler_init_dry(
1325
+ const struct llama_vocab * vocab,
1326
+ int32_t n_ctx_train,
1327
+ float dry_multiplier,
1328
+ float dry_base,
1329
+ int32_t dry_allowed_length,
1330
+ int32_t dry_penalty_last_n,
1331
+ const char ** seq_breakers,
1332
+ size_t num_breakers);
1333
+
1334
+ LLAMA_API struct llama_sampler * llama_sampler_init_logit_bias(
1335
+ int32_t n_vocab,
1336
+ int32_t n_logit_bias,
1337
+ const llama_logit_bias * logit_bias);
1338
+
1339
+ // this sampler is meant to be used for fill-in-the-middle infilling
1340
+ // it's supposed to be used after top_k + top_p sampling
1341
+ //
1342
+ // 1. if the sum of the EOG probs times the number of candidates is higher than the sum of the other probs -> pick EOG
1343
+ // 2. combine probs of tokens that have the same prefix
1344
+ //
1345
+ // example:
1346
+ //
1347
+ // - before:
1348
+ // "hel": 0.5
1349
+ // "hell": 0.2
1350
+ // "hello": 0.1
1351
+ // "dummy": 0.1
1352
+ //
1353
+ // - after:
1354
+ // "hel": 0.8
1355
+ // "dummy": 0.1
1356
+ //
1357
+ // 3. discard non-EOG tokens with low prob
1358
+ // 4. if no tokens are left -> pick EOT
1359
+ //
1360
+ LLAMA_API struct llama_sampler * llama_sampler_init_infill(const struct llama_vocab * vocab);
1361
+
1362
+ // Returns the seed used by the sampler if applicable, LLAMA_DEFAULT_SEED otherwise
1363
+ LLAMA_API uint32_t llama_sampler_get_seed(const struct llama_sampler * smpl);
1364
+
1365
+ /// @details Sample and accept a token from the idx-th output of the last evaluation
1366
+ //
1367
+ // Shorthand for:
1368
+ // const auto * logits = llama_get_logits_ith(ctx, idx);
1369
+ // llama_token_data_array cur_p = { ... init from logits ... };
1370
+ // llama_sampler_apply(smpl, &cur_p);
1371
+ // auto token = cur_p.data[cur_p.selected].id;
1372
+ // llama_sampler_accept(smpl, token);
1373
+ // return token;
1374
+ // Returns the sampled token
1375
+ LLAMA_API llama_token llama_sampler_sample(struct llama_sampler * smpl, struct llama_context * ctx, int32_t idx);
1376
+
1377
+ // TODO: extend in the future
1378
+ //LLAMA_API void llama_decode_with_sampler(struct llama_context * ctx, struct llama_sampler * smpl, struct llama_batch batch, ...);
1379
+
1380
+ //
1381
+ // Model split
1382
+ //
1383
+
1384
+ /// @details Build a split GGUF final path for this chunk.
1385
+ /// llama_split_path(split_path, sizeof(split_path), "/models/ggml-model-q4_0", 2, 4) => split_path = "/models/ggml-model-q4_0-00002-of-00004.gguf"
1386
+ // Returns the split_path length.
1387
+ LLAMA_API int llama_split_path(char * split_path, size_t maxlen, const char * path_prefix, int split_no, int split_count);
1388
+
1389
+ /// @details Extract the path prefix from the split_path if and only if the split_no and split_count match.
1390
+ /// llama_split_prefix(split_prefix, 64, "/models/ggml-model-q4_0-00002-of-00004.gguf", 2, 4) => split_prefix = "/models/ggml-model-q4_0"
1391
+ // Returns the split_prefix length.
1392
+ LLAMA_API int llama_split_prefix(char * split_prefix, size_t maxlen, const char * split_path, int split_no, int split_count);
1393
+
1394
+ // Print system information
1395
+ LLAMA_API const char * llama_print_system_info(void);
1396
+
1397
+ // Set callback for all future logging events.
1398
+ // If this is not called, or NULL is supplied, everything is output on stderr.
1399
+ LLAMA_API void llama_log_set(lm_ggml_log_callback log_callback, void * user_data);
1400
+
1401
+ //
1402
+ // Performance utils
1403
+ //
1404
+ // NOTE: Used by llama.cpp examples, avoid using in third-party apps. Instead, do your own performance measurements.
1405
+ //
1406
+
1407
+ struct llama_perf_context_data {
1408
+ double t_start_ms;
1409
+ double t_load_ms;
1410
+ double t_p_eval_ms;
1411
+ double t_eval_ms;
1412
+
1413
+ int32_t n_p_eval;
1414
+ int32_t n_eval;
1415
+ };
1416
+
1417
+ struct llama_perf_sampler_data {
1418
+ double t_sample_ms;
1419
+
1420
+ int32_t n_sample;
1421
+ };
1422
+
1423
+ LLAMA_API struct llama_perf_context_data llama_perf_context (const struct llama_context * ctx);
1424
+ LLAMA_API void llama_perf_context_print(const struct llama_context * ctx);
1425
+ LLAMA_API void llama_perf_context_reset( struct llama_context * ctx);
1426
+
1427
+ // NOTE: the following work only with samplers constructed via llama_sampler_chain_init
1428
+ LLAMA_API struct llama_perf_sampler_data llama_perf_sampler (const struct llama_sampler * chain);
1429
+ LLAMA_API void llama_perf_sampler_print(const struct llama_sampler * chain);
1430
+ LLAMA_API void llama_perf_sampler_reset( struct llama_sampler * chain);
1431
+
1432
+ #ifdef __cplusplus
1433
+ }
1434
+ #endif
1435
+
1436
+
1437
+ #endif // LLAMA_H