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,2189 @@
1
+ #pragma once
2
+
3
+ //
4
+ // GGML Tensor Library
5
+ //
6
+ // This documentation is still a work in progress.
7
+ // If you wish some specific topics to be covered, feel free to drop a comment:
8
+ //
9
+ // https://github.com/ggerganov/whisper.cpp/issues/40
10
+ //
11
+ // ## Overview
12
+ //
13
+ // This library implements:
14
+ //
15
+ // - a set of tensor operations
16
+ // - automatic differentiation
17
+ // - basic optimization algorithms
18
+ //
19
+ // The aim of this library is to provide a minimalistic approach for various machine learning tasks. This includes,
20
+ // but is not limited to, the following:
21
+ //
22
+ // - linear regression
23
+ // - support vector machines
24
+ // - neural networks
25
+ //
26
+ // The library allows the user to define a certain function using the available tensor operations. This function
27
+ // definition is represented internally via a computation graph. Each tensor operation in the function definition
28
+ // corresponds to a node in the graph. Having the computation graph defined, the user can choose to compute the
29
+ // function's value and/or its gradient with respect to the input variables. Optionally, the function can be optimized
30
+ // using one of the available optimization algorithms.
31
+ //
32
+ // For example, here we define the function: f(x) = a*x^2 + b
33
+ //
34
+ // {
35
+ // struct lm_ggml_init_params params = {
36
+ // .mem_size = 16*1024*1024,
37
+ // .mem_buffer = NULL,
38
+ // };
39
+ //
40
+ // // memory allocation happens here
41
+ // struct lm_ggml_context * ctx = lm_ggml_init(params);
42
+ //
43
+ // struct lm_ggml_tensor * x = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
44
+ //
45
+ // lm_ggml_set_param(ctx, x); // x is an input variable
46
+ //
47
+ // struct lm_ggml_tensor * a = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
48
+ // struct lm_ggml_tensor * b = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
49
+ // struct lm_ggml_tensor * x2 = lm_ggml_mul(ctx, x, x);
50
+ // struct lm_ggml_tensor * f = lm_ggml_add(ctx, lm_ggml_mul(ctx, a, x2), b);
51
+ //
52
+ // ...
53
+ // }
54
+ //
55
+ // Notice that the function definition above does not involve any actual computation. The computation is performed only
56
+ // when the user explicitly requests it. For example, to compute the function's value at x = 2.0:
57
+ //
58
+ // {
59
+ // ...
60
+ //
61
+ // struct lm_ggml_cgraph * gf = lm_ggml_new_graph(ctx);
62
+ // lm_ggml_build_forward_expand(gf, f);
63
+ //
64
+ // // set the input variable and parameter values
65
+ // lm_ggml_set_f32(x, 2.0f);
66
+ // lm_ggml_set_f32(a, 3.0f);
67
+ // lm_ggml_set_f32(b, 4.0f);
68
+ //
69
+ // lm_ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
+ //
71
+ // printf("f = %f\n", lm_ggml_get_f32_1d(f, 0));
72
+ //
73
+ // ...
74
+ // }
75
+ //
76
+ // The actual computation is performed in the lm_ggml_graph_compute() function.
77
+ //
78
+ // The lm_ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
+ // lm_ggml_init() function. You have to be careful not to exceed the memory buffer size. Therefore, you have to know
80
+ // in advance how much memory you need for your computation. Alternatively, you can allocate a large enough memory
81
+ // and after defining the computation graph, call the lm_ggml_used_mem() function to find out how much memory was
82
+ // actually needed.
83
+ //
84
+ // The lm_ggml_set_param() function marks a tensor as an input variable. This is used by the automatic
85
+ // differentiation and optimization algorithms.
86
+ //
87
+ // The described approach allows to define the function graph once and then compute its forward or backward graphs
88
+ // multiple times. All computations will use the same memory buffer allocated in the lm_ggml_init() function. This way
89
+ // the user can avoid the memory allocation overhead at runtime.
90
+ //
91
+ // The library supports multi-dimensional tensors - up to 4 dimensions. The FP16 and FP32 data types are first class
92
+ // citizens, but in theory the library can be extended to support FP8 and integer data types.
93
+ //
94
+ // Each tensor operation produces a new tensor. Initially the library was envisioned to support only the use of unary
95
+ // and binary operations. Most of the available operations fall into one of these two categories. With time, it became
96
+ // clear that the library needs to support more complex operations. The way to support these operations is not clear
97
+ // yet, but a few examples are demonstrated in the following operations:
98
+ //
99
+ // - lm_ggml_permute()
100
+ // - lm_ggml_conv_1d_1s()
101
+ // - lm_ggml_conv_1d_2s()
102
+ //
103
+ // For each tensor operator, the library implements a forward and backward computation function. The forward function
104
+ // computes the output tensor value given the input tensor values. The backward function computes the adjoint of the
105
+ // input tensors given the adjoint of the output tensor. For a detailed explanation of what this means, take a
106
+ // calculus class, or watch the following video:
107
+ //
108
+ // What is Automatic Differentiation?
109
+ // https://www.youtube.com/watch?v=wG_nF1awSSY
110
+ //
111
+ //
112
+ // ## Tensor data (struct lm_ggml_tensor)
113
+ //
114
+ // The tensors are stored in memory via the lm_ggml_tensor struct. The structure provides information about the size of
115
+ // the tensor, the data type, and the memory buffer where the tensor data is stored. Additionally, it contains
116
+ // pointers to the "source" tensors - i.e. the tensors that were used to compute the current tensor. For example:
117
+ //
118
+ // {
119
+ // struct lm_ggml_tensor * c = lm_ggml_add(ctx, a, b);
120
+ //
121
+ // assert(c->src[0] == a);
122
+ // assert(c->src[1] == b);
123
+ // }
124
+ //
125
+ // The multi-dimensional tensors are stored in row-major order. The lm_ggml_tensor struct contains fields for the
126
+ // number of elements in each dimension ("ne") as well as the number of bytes ("nb", a.k.a. stride). This allows
127
+ // to store tensors that are not contiguous in memory, which is useful for operations such as transposition and
128
+ // permutation. All tensor operations have to take the stride into account and not assume that the tensor is
129
+ // contiguous in memory.
130
+ //
131
+ // The data of the tensor is accessed via the "data" pointer. For example:
132
+ //
133
+ // {
134
+ // const int nx = 2;
135
+ // const int ny = 3;
136
+ //
137
+ // struct lm_ggml_tensor * a = lm_ggml_new_tensor_2d(ctx, LM_GGML_TYPE_F32, nx, ny);
138
+ //
139
+ // for (int y = 0; y < ny; y++) {
140
+ // for (int x = 0; x < nx; x++) {
141
+ // *(float *) ((char *) a->data + y*a->nb[1] + x*a->nb[0]) = x + y;
142
+ // }
143
+ // }
144
+ //
145
+ // ...
146
+ // }
147
+ //
148
+ // Alternatively, there are helper functions, such as lm_ggml_get_f32_1d() and lm_ggml_set_f32_1d() that can be used.
149
+ //
150
+ // ## The matrix multiplication operator (lm_ggml_mul_mat)
151
+ //
152
+ // TODO
153
+ //
154
+ //
155
+ // ## Multi-threading
156
+ //
157
+ // TODO
158
+ //
159
+ //
160
+ // ## Overview of ggml.c
161
+ //
162
+ // TODO
163
+ //
164
+ //
165
+ // ## SIMD optimizations
166
+ //
167
+ // TODO
168
+ //
169
+ //
170
+ // ## Debugging ggml
171
+ //
172
+ // TODO
173
+ //
174
+ //
175
+
176
+ #ifdef LM_GGML_SHARED
177
+ # if defined(_WIN32) && !defined(__MINGW32__)
178
+ # ifdef LM_GGML_BUILD
179
+ # define LM_GGML_API __declspec(dllexport) extern
180
+ # else
181
+ # define LM_GGML_API __declspec(dllimport) extern
182
+ # endif
183
+ # else
184
+ # define LM_GGML_API __attribute__ ((visibility ("default"))) extern
185
+ # endif
186
+ #else
187
+ # define LM_GGML_API extern
188
+ #endif
189
+
190
+ // TODO: support for clang
191
+ #ifdef __GNUC__
192
+ # define LM_GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
193
+ #elif defined(_MSC_VER)
194
+ # define LM_GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
195
+ #else
196
+ # define LM_GGML_DEPRECATED(func, hint) func
197
+ #endif
198
+
199
+ #ifndef __GNUC__
200
+ # define LM_GGML_ATTRIBUTE_FORMAT(...)
201
+ #elif defined(__MINGW32__) && !defined(__clang__)
202
+ # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
203
+ #else
204
+ # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
205
+ #endif
206
+
207
+ #include <stdbool.h>
208
+ #include <stddef.h>
209
+ #include <stdint.h>
210
+ #include <stdio.h>
211
+ #include <string.h>
212
+
213
+ #define LM_GGML_FILE_MAGIC 0x67676d6c // "ggml"
214
+ #define LM_GGML_FILE_VERSION 2
215
+
216
+ #define LM_GGML_QNT_VERSION 2 // bump this on quantization format changes
217
+ #define LM_GGML_QNT_VERSION_FACTOR 1000 // do not change this
218
+
219
+ #define LM_GGML_MAX_DIMS 4
220
+ #define LM_GGML_MAX_PARAMS 2048
221
+ #define LM_GGML_MAX_SRC 10
222
+ #define LM_GGML_MAX_N_THREADS 512
223
+ #define LM_GGML_MAX_OP_PARAMS 64
224
+
225
+ #ifndef LM_GGML_MAX_NAME
226
+ # define LM_GGML_MAX_NAME 64
227
+ #endif
228
+
229
+ #define LM_GGML_DEFAULT_N_THREADS 4
230
+ #define LM_GGML_DEFAULT_GRAPH_SIZE 2048
231
+
232
+ #if UINTPTR_MAX == 0xFFFFFFFF
233
+ #define LM_GGML_MEM_ALIGN 4
234
+ #else
235
+ #define LM_GGML_MEM_ALIGN 16
236
+ #endif
237
+
238
+ #define LM_GGML_EXIT_SUCCESS 0
239
+ #define LM_GGML_EXIT_ABORTED 1
240
+
241
+ #define LM_GGML_ROPE_TYPE_NEOX 2
242
+ #define LM_GGML_ROPE_TYPE_MROPE 8
243
+ #define LM_GGML_ROPE_TYPE_VISION 24
244
+
245
+ #define LM_GGML_UNUSED(x) (void)(x)
246
+
247
+ #define LM_GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
248
+
249
+ #ifndef NDEBUG
250
+ # define LM_GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
251
+ #elif defined(__GNUC__)
252
+ # define LM_GGML_UNREACHABLE() __builtin_unreachable()
253
+ #elif defined(_MSC_VER)
254
+ # define LM_GGML_UNREACHABLE() __assume(0)
255
+ #else
256
+ # define LM_GGML_UNREACHABLE() ((void) 0)
257
+ #endif
258
+
259
+ #ifdef __cplusplus
260
+ # define LM_GGML_NORETURN [[noreturn]]
261
+ #elif defined(_MSC_VER)
262
+ # define LM_GGML_NORETURN __declspec(noreturn)
263
+ #else
264
+ # define LM_GGML_NORETURN _Noreturn
265
+ #endif
266
+
267
+ #define LM_GGML_ABORT(...) lm_ggml_abort((strrchr(__FILE__, '/' ) ? strrchr(__FILE__, '/' ) + 1 : __FILE__), __LINE__, __VA_ARGS__)
268
+ #define LM_GGML_ASSERT(x) if (!(x)) LM_GGML_ABORT("LM_GGML_ASSERT(%s) failed", #x)
269
+
270
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
271
+ // main purpose is to reduce code duplication and improve readability.
272
+ //
273
+ // example:
274
+ //
275
+ // LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
276
+ // LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
277
+ //
278
+ #define LM_GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
279
+ const type prefix##0 = (pointer)->array[0]; \
280
+ LM_GGML_UNUSED(prefix##0);
281
+ #define LM_GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
282
+ LM_GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
283
+ const type prefix##1 = (pointer)->array[1]; \
284
+ LM_GGML_UNUSED(prefix##1);
285
+ #define LM_GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
286
+ LM_GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
287
+ const type prefix##2 = (pointer)->array[2]; \
288
+ LM_GGML_UNUSED(prefix##2);
289
+ #define LM_GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
290
+ LM_GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
291
+ const type prefix##3 = (pointer)->array[3]; \
292
+ LM_GGML_UNUSED(prefix##3);
293
+
294
+ #define LM_GGML_TENSOR_UNARY_OP_LOCALS \
295
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
296
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
297
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
298
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
299
+
300
+ #define LM_GGML_TENSOR_BINARY_OP_LOCALS \
301
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
302
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
303
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
304
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
305
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
306
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
307
+
308
+ #define LM_GGML_TENSOR_BINARY_OP_LOCALS01 \
309
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
310
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
311
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
312
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
313
+
314
+ #ifdef __cplusplus
315
+ extern "C" {
316
+ #endif
317
+
318
+ LM_GGML_NORETURN LM_GGML_ATTRIBUTE_FORMAT(3, 4)
319
+ LM_GGML_API void lm_ggml_abort(const char * file, int line, const char * fmt, ...);
320
+
321
+ enum lm_ggml_status {
322
+ LM_GGML_STATUS_ALLOC_FAILED = -2,
323
+ LM_GGML_STATUS_FAILED = -1,
324
+ LM_GGML_STATUS_SUCCESS = 0,
325
+ LM_GGML_STATUS_ABORTED = 1,
326
+ };
327
+
328
+ // get lm_ggml_status name string
329
+ LM_GGML_API const char * lm_ggml_status_to_string(enum lm_ggml_status status);
330
+
331
+ // ieee 754-2008 half-precision float16
332
+ // todo: make this not an integral type
333
+ typedef uint16_t lm_ggml_fp16_t;
334
+ LM_GGML_API float lm_ggml_fp16_to_fp32(lm_ggml_fp16_t);
335
+ LM_GGML_API lm_ggml_fp16_t lm_ggml_fp32_to_fp16(float);
336
+ LM_GGML_API void lm_ggml_fp16_to_fp32_row(const lm_ggml_fp16_t *, float *, int64_t);
337
+ LM_GGML_API void lm_ggml_fp32_to_fp16_row(const float *, lm_ggml_fp16_t *, int64_t);
338
+
339
+ // google brain half-precision bfloat16
340
+ typedef struct { uint16_t bits; } lm_ggml_bf16_t;
341
+ LM_GGML_API lm_ggml_bf16_t lm_ggml_fp32_to_bf16(float);
342
+ LM_GGML_API float lm_ggml_bf16_to_fp32(lm_ggml_bf16_t); // consider just doing << 16
343
+ LM_GGML_API void lm_ggml_bf16_to_fp32_row(const lm_ggml_bf16_t *, float *, int64_t);
344
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row_ref(const float *, lm_ggml_bf16_t *, int64_t);
345
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row(const float *, lm_ggml_bf16_t *, int64_t);
346
+
347
+ struct lm_ggml_object;
348
+ struct lm_ggml_context;
349
+ struct lm_ggml_cgraph;
350
+
351
+ // NOTE: always add types at the end of the enum to keep backward compatibility
352
+ enum lm_ggml_type {
353
+ LM_GGML_TYPE_F32 = 0,
354
+ LM_GGML_TYPE_F16 = 1,
355
+ LM_GGML_TYPE_Q4_0 = 2,
356
+ LM_GGML_TYPE_Q4_1 = 3,
357
+ // LM_GGML_TYPE_Q4_2 = 4, support has been removed
358
+ // LM_GGML_TYPE_Q4_3 = 5, support has been removed
359
+ LM_GGML_TYPE_Q5_0 = 6,
360
+ LM_GGML_TYPE_Q5_1 = 7,
361
+ LM_GGML_TYPE_Q8_0 = 8,
362
+ LM_GGML_TYPE_Q8_1 = 9,
363
+ LM_GGML_TYPE_Q2_K = 10,
364
+ LM_GGML_TYPE_Q3_K = 11,
365
+ LM_GGML_TYPE_Q4_K = 12,
366
+ LM_GGML_TYPE_Q5_K = 13,
367
+ LM_GGML_TYPE_Q6_K = 14,
368
+ LM_GGML_TYPE_Q8_K = 15,
369
+ LM_GGML_TYPE_IQ2_XXS = 16,
370
+ LM_GGML_TYPE_IQ2_XS = 17,
371
+ LM_GGML_TYPE_IQ3_XXS = 18,
372
+ LM_GGML_TYPE_IQ1_S = 19,
373
+ LM_GGML_TYPE_IQ4_NL = 20,
374
+ LM_GGML_TYPE_IQ3_S = 21,
375
+ LM_GGML_TYPE_IQ2_S = 22,
376
+ LM_GGML_TYPE_IQ4_XS = 23,
377
+ LM_GGML_TYPE_I8 = 24,
378
+ LM_GGML_TYPE_I16 = 25,
379
+ LM_GGML_TYPE_I32 = 26,
380
+ LM_GGML_TYPE_I64 = 27,
381
+ LM_GGML_TYPE_F64 = 28,
382
+ LM_GGML_TYPE_IQ1_M = 29,
383
+ LM_GGML_TYPE_BF16 = 30,
384
+ // LM_GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
385
+ // LM_GGML_TYPE_Q4_0_4_8 = 32,
386
+ // LM_GGML_TYPE_Q4_0_8_8 = 33,
387
+ LM_GGML_TYPE_TQ1_0 = 34,
388
+ LM_GGML_TYPE_TQ2_0 = 35,
389
+ // LM_GGML_TYPE_IQ4_NL_4_4 = 36,
390
+ // LM_GGML_TYPE_IQ4_NL_4_8 = 37,
391
+ // LM_GGML_TYPE_IQ4_NL_8_8 = 38,
392
+ LM_GGML_TYPE_COUNT = 39,
393
+ };
394
+
395
+ // precision
396
+ enum lm_ggml_prec {
397
+ LM_GGML_PREC_DEFAULT = 0, // stored as lm_ggml_tensor.op_params, 0 by default
398
+ LM_GGML_PREC_F32 = 10,
399
+ };
400
+
401
+ // model file types
402
+ enum lm_ggml_ftype {
403
+ LM_GGML_FTYPE_UNKNOWN = -1,
404
+ LM_GGML_FTYPE_ALL_F32 = 0,
405
+ LM_GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
406
+ LM_GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
407
+ LM_GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
408
+ LM_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
409
+ LM_GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
410
+ LM_GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
411
+ LM_GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
412
+ LM_GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
413
+ LM_GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
414
+ LM_GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
415
+ LM_GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
416
+ LM_GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
417
+ LM_GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
418
+ LM_GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
419
+ LM_GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
420
+ LM_GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
421
+ LM_GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
422
+ LM_GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
423
+ LM_GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
424
+ LM_GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
425
+ LM_GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
426
+ LM_GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
427
+ };
428
+
429
+ // available tensor operations:
430
+ enum lm_ggml_op {
431
+ LM_GGML_OP_NONE = 0,
432
+
433
+ LM_GGML_OP_DUP,
434
+ LM_GGML_OP_ADD,
435
+ LM_GGML_OP_ADD1,
436
+ LM_GGML_OP_ACC,
437
+ LM_GGML_OP_SUB,
438
+ LM_GGML_OP_MUL,
439
+ LM_GGML_OP_DIV,
440
+ LM_GGML_OP_SQR,
441
+ LM_GGML_OP_SQRT,
442
+ LM_GGML_OP_LOG,
443
+ LM_GGML_OP_SIN,
444
+ LM_GGML_OP_COS,
445
+ LM_GGML_OP_SUM,
446
+ LM_GGML_OP_SUM_ROWS,
447
+ LM_GGML_OP_MEAN,
448
+ LM_GGML_OP_ARGMAX,
449
+ LM_GGML_OP_COUNT_EQUAL,
450
+ LM_GGML_OP_REPEAT,
451
+ LM_GGML_OP_REPEAT_BACK,
452
+ LM_GGML_OP_CONCAT,
453
+ LM_GGML_OP_SILU_BACK,
454
+ LM_GGML_OP_NORM, // normalize
455
+ LM_GGML_OP_RMS_NORM,
456
+ LM_GGML_OP_RMS_NORM_BACK,
457
+ LM_GGML_OP_GROUP_NORM,
458
+ LM_GGML_OP_L2_NORM,
459
+
460
+ LM_GGML_OP_MUL_MAT,
461
+ LM_GGML_OP_MUL_MAT_ID,
462
+ LM_GGML_OP_OUT_PROD,
463
+
464
+ LM_GGML_OP_SCALE,
465
+ LM_GGML_OP_SET,
466
+ LM_GGML_OP_CPY,
467
+ LM_GGML_OP_CONT,
468
+ LM_GGML_OP_RESHAPE,
469
+ LM_GGML_OP_VIEW,
470
+ LM_GGML_OP_PERMUTE,
471
+ LM_GGML_OP_TRANSPOSE,
472
+ LM_GGML_OP_GET_ROWS,
473
+ LM_GGML_OP_GET_ROWS_BACK,
474
+ LM_GGML_OP_DIAG,
475
+ LM_GGML_OP_DIAG_MASK_INF,
476
+ LM_GGML_OP_DIAG_MASK_ZERO,
477
+ LM_GGML_OP_SOFT_MAX,
478
+ LM_GGML_OP_SOFT_MAX_BACK,
479
+ LM_GGML_OP_ROPE,
480
+ LM_GGML_OP_ROPE_BACK,
481
+ LM_GGML_OP_CLAMP,
482
+ LM_GGML_OP_CONV_TRANSPOSE_1D,
483
+ LM_GGML_OP_IM2COL,
484
+ LM_GGML_OP_IM2COL_BACK,
485
+ LM_GGML_OP_CONV_2D_DW,
486
+ LM_GGML_OP_CONV_TRANSPOSE_2D,
487
+ LM_GGML_OP_POOL_1D,
488
+ LM_GGML_OP_POOL_2D,
489
+ LM_GGML_OP_POOL_2D_BACK,
490
+ LM_GGML_OP_UPSCALE, // nearest interpolate
491
+ LM_GGML_OP_PAD,
492
+ LM_GGML_OP_PAD_REFLECT_1D,
493
+ LM_GGML_OP_ARANGE,
494
+ LM_GGML_OP_TIMESTEP_EMBEDDING,
495
+ LM_GGML_OP_ARGSORT,
496
+ LM_GGML_OP_LEAKY_RELU,
497
+
498
+ LM_GGML_OP_FLASH_ATTN_EXT,
499
+ LM_GGML_OP_FLASH_ATTN_BACK,
500
+ LM_GGML_OP_SSM_CONV,
501
+ LM_GGML_OP_SSM_SCAN,
502
+ LM_GGML_OP_WIN_PART,
503
+ LM_GGML_OP_WIN_UNPART,
504
+ LM_GGML_OP_GET_REL_POS,
505
+ LM_GGML_OP_ADD_REL_POS,
506
+ LM_GGML_OP_RWKV_WKV6,
507
+ LM_GGML_OP_GATED_LINEAR_ATTN,
508
+ LM_GGML_OP_RWKV_WKV7,
509
+
510
+ LM_GGML_OP_UNARY,
511
+
512
+ LM_GGML_OP_MAP_CUSTOM1,
513
+ LM_GGML_OP_MAP_CUSTOM2,
514
+ LM_GGML_OP_MAP_CUSTOM3,
515
+
516
+ LM_GGML_OP_CUSTOM,
517
+
518
+ LM_GGML_OP_CROSS_ENTROPY_LOSS,
519
+ LM_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
520
+ LM_GGML_OP_OPT_STEP_ADAMW,
521
+
522
+ LM_GGML_OP_COUNT,
523
+ };
524
+
525
+ enum lm_ggml_unary_op {
526
+ LM_GGML_UNARY_OP_ABS,
527
+ LM_GGML_UNARY_OP_SGN,
528
+ LM_GGML_UNARY_OP_NEG,
529
+ LM_GGML_UNARY_OP_STEP,
530
+ LM_GGML_UNARY_OP_TANH,
531
+ LM_GGML_UNARY_OP_ELU,
532
+ LM_GGML_UNARY_OP_RELU,
533
+ LM_GGML_UNARY_OP_SIGMOID,
534
+ LM_GGML_UNARY_OP_GELU,
535
+ LM_GGML_UNARY_OP_GELU_QUICK,
536
+ LM_GGML_UNARY_OP_SILU,
537
+ LM_GGML_UNARY_OP_HARDSWISH,
538
+ LM_GGML_UNARY_OP_HARDSIGMOID,
539
+ LM_GGML_UNARY_OP_EXP,
540
+
541
+ LM_GGML_UNARY_OP_COUNT,
542
+ };
543
+
544
+ enum lm_ggml_object_type {
545
+ LM_GGML_OBJECT_TYPE_TENSOR,
546
+ LM_GGML_OBJECT_TYPE_GRAPH,
547
+ LM_GGML_OBJECT_TYPE_WORK_BUFFER
548
+ };
549
+
550
+ enum lm_ggml_log_level {
551
+ LM_GGML_LOG_LEVEL_NONE = 0,
552
+ LM_GGML_LOG_LEVEL_DEBUG = 1,
553
+ LM_GGML_LOG_LEVEL_INFO = 2,
554
+ LM_GGML_LOG_LEVEL_WARN = 3,
555
+ LM_GGML_LOG_LEVEL_ERROR = 4,
556
+ LM_GGML_LOG_LEVEL_CONT = 5, // continue previous log
557
+ };
558
+
559
+ // this tensor...
560
+ enum lm_ggml_tensor_flag {
561
+ LM_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
562
+ LM_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
563
+ LM_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
564
+ LM_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
565
+ };
566
+
567
+ struct lm_ggml_init_params {
568
+ // memory pool
569
+ size_t mem_size; // bytes
570
+ void * mem_buffer; // if NULL, memory will be allocated internally
571
+ bool no_alloc; // don't allocate memory for the tensor data
572
+ };
573
+
574
+ // n-dimensional tensor
575
+ struct lm_ggml_tensor {
576
+ enum lm_ggml_type type;
577
+
578
+ struct lm_ggml_backend_buffer * buffer;
579
+
580
+ int64_t ne[LM_GGML_MAX_DIMS]; // number of elements
581
+ size_t nb[LM_GGML_MAX_DIMS]; // stride in bytes:
582
+ // nb[0] = lm_ggml_type_size(type)
583
+ // nb[1] = nb[0] * (ne[0] / lm_ggml_blck_size(type)) + padding
584
+ // nb[i] = nb[i-1] * ne[i-1]
585
+
586
+ // compute data
587
+ enum lm_ggml_op op;
588
+
589
+ // op params - allocated as int32_t for alignment
590
+ int32_t op_params[LM_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
591
+
592
+ int32_t flags;
593
+
594
+ struct lm_ggml_tensor * src[LM_GGML_MAX_SRC];
595
+
596
+ // source tensor and offset for views
597
+ struct lm_ggml_tensor * view_src;
598
+ size_t view_offs;
599
+
600
+ void * data;
601
+
602
+ char name[LM_GGML_MAX_NAME];
603
+
604
+ void * extra; // extra things e.g. for ggml-cuda.cu
605
+
606
+ char padding[8];
607
+ };
608
+
609
+ static const size_t LM_GGML_TENSOR_SIZE = sizeof(struct lm_ggml_tensor);
610
+
611
+ // Abort callback
612
+ // If not NULL, called before ggml computation
613
+ // If it returns true, the computation is aborted
614
+ typedef bool (*lm_ggml_abort_callback)(void * data);
615
+
616
+
617
+ //
618
+ // GUID
619
+ //
620
+
621
+ // GUID types
622
+ typedef uint8_t lm_ggml_guid[16];
623
+ typedef lm_ggml_guid * lm_ggml_guid_t;
624
+
625
+ LM_GGML_API bool lm_ggml_guid_matches(lm_ggml_guid_t guid_a, lm_ggml_guid_t guid_b);
626
+
627
+ // misc
628
+
629
+ LM_GGML_API void lm_ggml_time_init(void); // call this once at the beginning of the program
630
+ LM_GGML_API int64_t lm_ggml_time_ms(void);
631
+ LM_GGML_API int64_t lm_ggml_time_us(void);
632
+ LM_GGML_API int64_t lm_ggml_cycles(void);
633
+ LM_GGML_API int64_t lm_ggml_cycles_per_ms(void);
634
+
635
+ // accepts a UTF-8 path, even on Windows
636
+ LM_GGML_API FILE * lm_ggml_fopen(const char * fname, const char * mode);
637
+
638
+ LM_GGML_API void lm_ggml_print_object (const struct lm_ggml_object * obj);
639
+ LM_GGML_API void lm_ggml_print_objects(const struct lm_ggml_context * ctx);
640
+
641
+ LM_GGML_API int64_t lm_ggml_nelements (const struct lm_ggml_tensor * tensor);
642
+ LM_GGML_API int64_t lm_ggml_nrows (const struct lm_ggml_tensor * tensor);
643
+ LM_GGML_API size_t lm_ggml_nbytes (const struct lm_ggml_tensor * tensor);
644
+ LM_GGML_API size_t lm_ggml_nbytes_pad(const struct lm_ggml_tensor * tensor); // same as lm_ggml_nbytes() but padded to LM_GGML_MEM_ALIGN
645
+
646
+ LM_GGML_API int64_t lm_ggml_blck_size(enum lm_ggml_type type);
647
+ LM_GGML_API size_t lm_ggml_type_size(enum lm_ggml_type type); // size in bytes for all elements in a block
648
+ LM_GGML_API size_t lm_ggml_row_size (enum lm_ggml_type type, int64_t ne); // size in bytes for all elements in a row
649
+
650
+ LM_GGML_DEPRECATED(
651
+ LM_GGML_API double lm_ggml_type_sizef(enum lm_ggml_type type), // lm_ggml_type_size()/lm_ggml_blck_size() as float
652
+ "use lm_ggml_row_size() instead");
653
+
654
+ LM_GGML_API const char * lm_ggml_type_name(enum lm_ggml_type type);
655
+ LM_GGML_API const char * lm_ggml_op_name (enum lm_ggml_op op);
656
+ LM_GGML_API const char * lm_ggml_op_symbol(enum lm_ggml_op op);
657
+
658
+ LM_GGML_API const char * lm_ggml_unary_op_name(enum lm_ggml_unary_op op);
659
+ LM_GGML_API const char * lm_ggml_op_desc(const struct lm_ggml_tensor * t); // unary or op name
660
+
661
+ LM_GGML_API size_t lm_ggml_element_size(const struct lm_ggml_tensor * tensor);
662
+
663
+ LM_GGML_API bool lm_ggml_is_quantized(enum lm_ggml_type type);
664
+
665
+ // TODO: temporary until model loading of ggml examples is refactored
666
+ LM_GGML_API enum lm_ggml_type lm_ggml_ftype_to_lm_ggml_type(enum lm_ggml_ftype ftype);
667
+
668
+ LM_GGML_API bool lm_ggml_is_transposed(const struct lm_ggml_tensor * tensor);
669
+ LM_GGML_API bool lm_ggml_is_permuted (const struct lm_ggml_tensor * tensor);
670
+ LM_GGML_API bool lm_ggml_is_empty (const struct lm_ggml_tensor * tensor);
671
+ LM_GGML_API bool lm_ggml_is_scalar (const struct lm_ggml_tensor * tensor);
672
+ LM_GGML_API bool lm_ggml_is_vector (const struct lm_ggml_tensor * tensor);
673
+ LM_GGML_API bool lm_ggml_is_matrix (const struct lm_ggml_tensor * tensor);
674
+ LM_GGML_API bool lm_ggml_is_3d (const struct lm_ggml_tensor * tensor);
675
+ LM_GGML_API int lm_ggml_n_dims (const struct lm_ggml_tensor * tensor); // returns 1 for scalars
676
+
677
+ LM_GGML_API bool lm_ggml_is_contiguous (const struct lm_ggml_tensor * tensor);
678
+ LM_GGML_API bool lm_ggml_is_contiguous_0(const struct lm_ggml_tensor * tensor); // same as lm_ggml_is_contiguous()
679
+ LM_GGML_API bool lm_ggml_is_contiguous_1(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 1
680
+ LM_GGML_API bool lm_ggml_is_contiguous_2(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 2
681
+
682
+ // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN
683
+ LM_GGML_API bool lm_ggml_is_contiguous_channels(const struct lm_ggml_tensor * tensor);
684
+
685
+ LM_GGML_API bool lm_ggml_are_same_shape (const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
686
+ LM_GGML_API bool lm_ggml_are_same_stride(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
687
+
688
+ LM_GGML_API bool lm_ggml_can_repeat(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
689
+
690
+ // use this to compute the memory overhead of a tensor
691
+ LM_GGML_API size_t lm_ggml_tensor_overhead(void);
692
+
693
+ LM_GGML_API bool lm_ggml_validate_row_data(enum lm_ggml_type type, const void * data, size_t nbytes);
694
+
695
+ // main
696
+
697
+ LM_GGML_API struct lm_ggml_context * lm_ggml_init (struct lm_ggml_init_params params);
698
+ LM_GGML_API void lm_ggml_reset(struct lm_ggml_context * ctx);
699
+ LM_GGML_API void lm_ggml_free (struct lm_ggml_context * ctx);
700
+
701
+ LM_GGML_API size_t lm_ggml_used_mem(const struct lm_ggml_context * ctx);
702
+
703
+ LM_GGML_API bool lm_ggml_get_no_alloc(struct lm_ggml_context * ctx);
704
+ LM_GGML_API void lm_ggml_set_no_alloc(struct lm_ggml_context * ctx, bool no_alloc);
705
+
706
+ LM_GGML_API void * lm_ggml_get_mem_buffer (const struct lm_ggml_context * ctx);
707
+ LM_GGML_API size_t lm_ggml_get_mem_size (const struct lm_ggml_context * ctx);
708
+ LM_GGML_API size_t lm_ggml_get_max_tensor_size(const struct lm_ggml_context * ctx);
709
+
710
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor(
711
+ struct lm_ggml_context * ctx,
712
+ enum lm_ggml_type type,
713
+ int n_dims,
714
+ const int64_t *ne);
715
+
716
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_1d(
717
+ struct lm_ggml_context * ctx,
718
+ enum lm_ggml_type type,
719
+ int64_t ne0);
720
+
721
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_2d(
722
+ struct lm_ggml_context * ctx,
723
+ enum lm_ggml_type type,
724
+ int64_t ne0,
725
+ int64_t ne1);
726
+
727
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_3d(
728
+ struct lm_ggml_context * ctx,
729
+ enum lm_ggml_type type,
730
+ int64_t ne0,
731
+ int64_t ne1,
732
+ int64_t ne2);
733
+
734
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_4d(
735
+ struct lm_ggml_context * ctx,
736
+ enum lm_ggml_type type,
737
+ int64_t ne0,
738
+ int64_t ne1,
739
+ int64_t ne2,
740
+ int64_t ne3);
741
+
742
+ LM_GGML_API void * lm_ggml_new_buffer(struct lm_ggml_context * ctx, size_t nbytes);
743
+
744
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_tensor (struct lm_ggml_context * ctx, const struct lm_ggml_tensor * src);
745
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_tensor(struct lm_ggml_context * ctx, struct lm_ggml_tensor * src);
746
+
747
+ // Context tensor enumeration and lookup
748
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_first_tensor(const struct lm_ggml_context * ctx);
749
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_next_tensor (const struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
750
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_tensor(struct lm_ggml_context * ctx, const char * name);
751
+
752
+ // Converts a flat index into coordinates
753
+ LM_GGML_API void lm_ggml_unravel_index(const struct lm_ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
754
+
755
+ LM_GGML_API enum lm_ggml_unary_op lm_ggml_get_unary_op(const struct lm_ggml_tensor * tensor);
756
+
757
+ LM_GGML_API void * lm_ggml_get_data (const struct lm_ggml_tensor * tensor);
758
+ LM_GGML_API float * lm_ggml_get_data_f32(const struct lm_ggml_tensor * tensor);
759
+
760
+ LM_GGML_API const char * lm_ggml_get_name (const struct lm_ggml_tensor * tensor);
761
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_name ( struct lm_ggml_tensor * tensor, const char * name);
762
+ LM_GGML_ATTRIBUTE_FORMAT(2, 3)
763
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_format_name( struct lm_ggml_tensor * tensor, const char * fmt, ...);
764
+
765
+ // Tensor flags
766
+ LM_GGML_API void lm_ggml_set_input(struct lm_ggml_tensor * tensor);
767
+ LM_GGML_API void lm_ggml_set_output(struct lm_ggml_tensor * tensor);
768
+ LM_GGML_API void lm_ggml_set_param(struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
769
+ LM_GGML_API void lm_ggml_set_loss(struct lm_ggml_tensor * tensor);
770
+
771
+ //
772
+ // operations on tensors with backpropagation
773
+ //
774
+
775
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup(
776
+ struct lm_ggml_context * ctx,
777
+ struct lm_ggml_tensor * a);
778
+
779
+ // in-place, returns view(a)
780
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_inplace(
781
+ struct lm_ggml_context * ctx,
782
+ struct lm_ggml_tensor * a);
783
+
784
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add(
785
+ struct lm_ggml_context * ctx,
786
+ struct lm_ggml_tensor * a,
787
+ struct lm_ggml_tensor * b);
788
+
789
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_inplace(
790
+ struct lm_ggml_context * ctx,
791
+ struct lm_ggml_tensor * a,
792
+ struct lm_ggml_tensor * b);
793
+
794
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_cast(
795
+ struct lm_ggml_context * ctx,
796
+ struct lm_ggml_tensor * a,
797
+ struct lm_ggml_tensor * b,
798
+ enum lm_ggml_type type);
799
+
800
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1(
801
+ struct lm_ggml_context * ctx,
802
+ struct lm_ggml_tensor * a,
803
+ struct lm_ggml_tensor * b);
804
+
805
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1_inplace(
806
+ struct lm_ggml_context * ctx,
807
+ struct lm_ggml_tensor * a,
808
+ struct lm_ggml_tensor * b);
809
+
810
+ // dst = a
811
+ // view(dst, nb1, nb2, nb3, offset) += b
812
+ // return dst
813
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc(
814
+ struct lm_ggml_context * ctx,
815
+ struct lm_ggml_tensor * a,
816
+ struct lm_ggml_tensor * b,
817
+ size_t nb1,
818
+ size_t nb2,
819
+ size_t nb3,
820
+ size_t offset);
821
+
822
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc_inplace(
823
+ struct lm_ggml_context * ctx,
824
+ struct lm_ggml_tensor * a,
825
+ struct lm_ggml_tensor * b,
826
+ size_t nb1,
827
+ size_t nb2,
828
+ size_t nb3,
829
+ size_t offset);
830
+
831
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub(
832
+ struct lm_ggml_context * ctx,
833
+ struct lm_ggml_tensor * a,
834
+ struct lm_ggml_tensor * b);
835
+
836
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub_inplace(
837
+ struct lm_ggml_context * ctx,
838
+ struct lm_ggml_tensor * a,
839
+ struct lm_ggml_tensor * b);
840
+
841
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul(
842
+ struct lm_ggml_context * ctx,
843
+ struct lm_ggml_tensor * a,
844
+ struct lm_ggml_tensor * b);
845
+
846
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_inplace(
847
+ struct lm_ggml_context * ctx,
848
+ struct lm_ggml_tensor * a,
849
+ struct lm_ggml_tensor * b);
850
+
851
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div(
852
+ struct lm_ggml_context * ctx,
853
+ struct lm_ggml_tensor * a,
854
+ struct lm_ggml_tensor * b);
855
+
856
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div_inplace(
857
+ struct lm_ggml_context * ctx,
858
+ struct lm_ggml_tensor * a,
859
+ struct lm_ggml_tensor * b);
860
+
861
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr(
862
+ struct lm_ggml_context * ctx,
863
+ struct lm_ggml_tensor * a);
864
+
865
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr_inplace(
866
+ struct lm_ggml_context * ctx,
867
+ struct lm_ggml_tensor * a);
868
+
869
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt(
870
+ struct lm_ggml_context * ctx,
871
+ struct lm_ggml_tensor * a);
872
+
873
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt_inplace(
874
+ struct lm_ggml_context * ctx,
875
+ struct lm_ggml_tensor * a);
876
+
877
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log(
878
+ struct lm_ggml_context * ctx,
879
+ struct lm_ggml_tensor * a);
880
+
881
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log_inplace(
882
+ struct lm_ggml_context * ctx,
883
+ struct lm_ggml_tensor * a);
884
+
885
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin(
886
+ struct lm_ggml_context * ctx,
887
+ struct lm_ggml_tensor * a);
888
+
889
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin_inplace(
890
+ struct lm_ggml_context * ctx,
891
+ struct lm_ggml_tensor * a);
892
+
893
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos(
894
+ struct lm_ggml_context * ctx,
895
+ struct lm_ggml_tensor * a);
896
+
897
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos_inplace(
898
+ struct lm_ggml_context * ctx,
899
+ struct lm_ggml_tensor * a);
900
+
901
+ // return scalar
902
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum(
903
+ struct lm_ggml_context * ctx,
904
+ struct lm_ggml_tensor * a);
905
+
906
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
907
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum_rows(
908
+ struct lm_ggml_context * ctx,
909
+ struct lm_ggml_tensor * a);
910
+
911
+ // mean along rows
912
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mean(
913
+ struct lm_ggml_context * ctx,
914
+ struct lm_ggml_tensor * a);
915
+
916
+ // argmax along rows
917
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argmax(
918
+ struct lm_ggml_context * ctx,
919
+ struct lm_ggml_tensor * a);
920
+
921
+ // count number of equal elements in a and b
922
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_count_equal(
923
+ struct lm_ggml_context * ctx,
924
+ struct lm_ggml_tensor * a,
925
+ struct lm_ggml_tensor * b);
926
+
927
+ // if a is the same shape as b, and a is not parameter, return a
928
+ // otherwise, return a new tensor: repeat(a) to fit in b
929
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat(
930
+ struct lm_ggml_context * ctx,
931
+ struct lm_ggml_tensor * a,
932
+ struct lm_ggml_tensor * b);
933
+
934
+ // sums repetitions in a into shape of b
935
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_back(
936
+ struct lm_ggml_context * ctx,
937
+ struct lm_ggml_tensor * a,
938
+ struct lm_ggml_tensor * b);
939
+
940
+ // concat a and b along dim
941
+ // used in stable-diffusion
942
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_concat(
943
+ struct lm_ggml_context * ctx,
944
+ struct lm_ggml_tensor * a,
945
+ struct lm_ggml_tensor * b,
946
+ int dim);
947
+
948
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs(
949
+ struct lm_ggml_context * ctx,
950
+ struct lm_ggml_tensor * a);
951
+
952
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs_inplace(
953
+ struct lm_ggml_context * ctx,
954
+ struct lm_ggml_tensor * a);
955
+
956
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn(
957
+ struct lm_ggml_context * ctx,
958
+ struct lm_ggml_tensor * a);
959
+
960
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn_inplace(
961
+ struct lm_ggml_context * ctx,
962
+ struct lm_ggml_tensor * a);
963
+
964
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg(
965
+ struct lm_ggml_context * ctx,
966
+ struct lm_ggml_tensor * a);
967
+
968
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg_inplace(
969
+ struct lm_ggml_context * ctx,
970
+ struct lm_ggml_tensor * a);
971
+
972
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step(
973
+ struct lm_ggml_context * ctx,
974
+ struct lm_ggml_tensor * a);
975
+
976
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step_inplace(
977
+ struct lm_ggml_context * ctx,
978
+ struct lm_ggml_tensor * a);
979
+
980
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh(
981
+ struct lm_ggml_context * ctx,
982
+ struct lm_ggml_tensor * a);
983
+
984
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh_inplace(
985
+ struct lm_ggml_context * ctx,
986
+ struct lm_ggml_tensor * a);
987
+
988
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu(
989
+ struct lm_ggml_context * ctx,
990
+ struct lm_ggml_tensor * a);
991
+
992
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu_inplace(
993
+ struct lm_ggml_context * ctx,
994
+ struct lm_ggml_tensor * a);
995
+
996
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu(
997
+ struct lm_ggml_context * ctx,
998
+ struct lm_ggml_tensor * a);
999
+
1000
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_leaky_relu(
1001
+ struct lm_ggml_context * ctx,
1002
+ struct lm_ggml_tensor * a, float negative_slope, bool inplace);
1003
+
1004
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu_inplace(
1005
+ struct lm_ggml_context * ctx,
1006
+ struct lm_ggml_tensor * a);
1007
+
1008
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid(
1009
+ struct lm_ggml_context * ctx,
1010
+ struct lm_ggml_tensor * a);
1011
+
1012
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid_inplace(
1013
+ struct lm_ggml_context * ctx,
1014
+ struct lm_ggml_tensor * a);
1015
+
1016
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu(
1017
+ struct lm_ggml_context * ctx,
1018
+ struct lm_ggml_tensor * a);
1019
+
1020
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_inplace(
1021
+ struct lm_ggml_context * ctx,
1022
+ struct lm_ggml_tensor * a);
1023
+
1024
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick(
1025
+ struct lm_ggml_context * ctx,
1026
+ struct lm_ggml_tensor * a);
1027
+
1028
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick_inplace(
1029
+ struct lm_ggml_context * ctx,
1030
+ struct lm_ggml_tensor * a);
1031
+
1032
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu(
1033
+ struct lm_ggml_context * ctx,
1034
+ struct lm_ggml_tensor * a);
1035
+
1036
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_inplace(
1037
+ struct lm_ggml_context * ctx,
1038
+ struct lm_ggml_tensor * a);
1039
+
1040
+ // a - x
1041
+ // b - dy
1042
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_back(
1043
+ struct lm_ggml_context * ctx,
1044
+ struct lm_ggml_tensor * a,
1045
+ struct lm_ggml_tensor * b);
1046
+
1047
+ // hardswish(x) = x * relu6(x + 3) / 6
1048
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardswish(
1049
+ struct lm_ggml_context * ctx,
1050
+ struct lm_ggml_tensor * a);
1051
+
1052
+ // hardsigmoid(x) = relu6(x + 3) / 6
1053
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardsigmoid(
1054
+ struct lm_ggml_context * ctx,
1055
+ struct lm_ggml_tensor * a);
1056
+
1057
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp(
1058
+ struct lm_ggml_context * ctx,
1059
+ struct lm_ggml_tensor * a);
1060
+
1061
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp_inplace(
1062
+ struct lm_ggml_context * ctx,
1063
+ struct lm_ggml_tensor * a);
1064
+
1065
+ // normalize along rows
1066
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm(
1067
+ struct lm_ggml_context * ctx,
1068
+ struct lm_ggml_tensor * a,
1069
+ float eps);
1070
+
1071
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm_inplace(
1072
+ struct lm_ggml_context * ctx,
1073
+ struct lm_ggml_tensor * a,
1074
+ float eps);
1075
+
1076
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm(
1077
+ struct lm_ggml_context * ctx,
1078
+ struct lm_ggml_tensor * a,
1079
+ float eps);
1080
+
1081
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_inplace(
1082
+ struct lm_ggml_context * ctx,
1083
+ struct lm_ggml_tensor * a,
1084
+ float eps);
1085
+
1086
+ // group normalize along ne0*ne1*n_groups
1087
+ // used in stable-diffusion
1088
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm(
1089
+ struct lm_ggml_context * ctx,
1090
+ struct lm_ggml_tensor * a,
1091
+ int n_groups,
1092
+ float eps);
1093
+
1094
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm_inplace(
1095
+ struct lm_ggml_context * ctx,
1096
+ struct lm_ggml_tensor * a,
1097
+ int n_groups,
1098
+ float eps);
1099
+
1100
+ // l2 normalize along rows
1101
+ // used in rwkv v7
1102
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm(
1103
+ struct lm_ggml_context * ctx,
1104
+ struct lm_ggml_tensor * a,
1105
+ float eps);
1106
+
1107
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm_inplace(
1108
+ struct lm_ggml_context * ctx,
1109
+ struct lm_ggml_tensor * a,
1110
+ float eps);
1111
+
1112
+ // a - x
1113
+ // b - dy
1114
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_back(
1115
+ struct lm_ggml_context * ctx,
1116
+ struct lm_ggml_tensor * a,
1117
+ struct lm_ggml_tensor * b,
1118
+ float eps);
1119
+
1120
+ // A: k columns, n rows => [ne03, ne02, n, k]
1121
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1122
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1123
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat(
1124
+ struct lm_ggml_context * ctx,
1125
+ struct lm_ggml_tensor * a,
1126
+ struct lm_ggml_tensor * b);
1127
+
1128
+ // change the precision of a matrix multiplication
1129
+ // set to LM_GGML_PREC_F32 for higher precision (useful for phi-2)
1130
+ LM_GGML_API void lm_ggml_mul_mat_set_prec(
1131
+ struct lm_ggml_tensor * a,
1132
+ enum lm_ggml_prec prec);
1133
+
1134
+ // indirect matrix multiplication
1135
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat_id(
1136
+ struct lm_ggml_context * ctx,
1137
+ struct lm_ggml_tensor * as,
1138
+ struct lm_ggml_tensor * b,
1139
+ struct lm_ggml_tensor * ids);
1140
+
1141
+ // A: m columns, n rows,
1142
+ // B: p columns, n rows,
1143
+ // result is m columns, p rows
1144
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_out_prod(
1145
+ struct lm_ggml_context * ctx,
1146
+ struct lm_ggml_tensor * a,
1147
+ struct lm_ggml_tensor * b);
1148
+
1149
+ //
1150
+ // operations on tensors without backpropagation
1151
+ //
1152
+
1153
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale(
1154
+ struct lm_ggml_context * ctx,
1155
+ struct lm_ggml_tensor * a,
1156
+ float s);
1157
+
1158
+ // in-place, returns view(a)
1159
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_inplace(
1160
+ struct lm_ggml_context * ctx,
1161
+ struct lm_ggml_tensor * a,
1162
+ float s);
1163
+
1164
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1165
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set(
1166
+ struct lm_ggml_context * ctx,
1167
+ struct lm_ggml_tensor * a,
1168
+ struct lm_ggml_tensor * b,
1169
+ size_t nb1,
1170
+ size_t nb2,
1171
+ size_t nb3,
1172
+ size_t offset); // in bytes
1173
+
1174
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1175
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_inplace(
1176
+ struct lm_ggml_context * ctx,
1177
+ struct lm_ggml_tensor * a,
1178
+ struct lm_ggml_tensor * b,
1179
+ size_t nb1,
1180
+ size_t nb2,
1181
+ size_t nb3,
1182
+ size_t offset); // in bytes
1183
+
1184
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d(
1185
+ struct lm_ggml_context * ctx,
1186
+ struct lm_ggml_tensor * a,
1187
+ struct lm_ggml_tensor * b,
1188
+ size_t offset); // in bytes
1189
+
1190
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d_inplace(
1191
+ struct lm_ggml_context * ctx,
1192
+ struct lm_ggml_tensor * a,
1193
+ struct lm_ggml_tensor * b,
1194
+ size_t offset); // in bytes
1195
+
1196
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1197
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d(
1198
+ struct lm_ggml_context * ctx,
1199
+ struct lm_ggml_tensor * a,
1200
+ struct lm_ggml_tensor * b,
1201
+ size_t nb1,
1202
+ size_t offset); // in bytes
1203
+
1204
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1205
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d_inplace(
1206
+ struct lm_ggml_context * ctx,
1207
+ struct lm_ggml_tensor * a,
1208
+ struct lm_ggml_tensor * b,
1209
+ size_t nb1,
1210
+ size_t offset); // in bytes
1211
+
1212
+ // a -> b, return view(b)
1213
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cpy(
1214
+ struct lm_ggml_context * ctx,
1215
+ struct lm_ggml_tensor * a,
1216
+ struct lm_ggml_tensor * b);
1217
+
1218
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cast(
1219
+ struct lm_ggml_context * ctx,
1220
+ struct lm_ggml_tensor * a,
1221
+ enum lm_ggml_type type);
1222
+
1223
+ // make contiguous
1224
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont(
1225
+ struct lm_ggml_context * ctx,
1226
+ struct lm_ggml_tensor * a);
1227
+
1228
+ // make contiguous, with new shape
1229
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_1d(
1230
+ struct lm_ggml_context * ctx,
1231
+ struct lm_ggml_tensor * a,
1232
+ int64_t ne0);
1233
+
1234
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_2d(
1235
+ struct lm_ggml_context * ctx,
1236
+ struct lm_ggml_tensor * a,
1237
+ int64_t ne0,
1238
+ int64_t ne1);
1239
+
1240
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_3d(
1241
+ struct lm_ggml_context * ctx,
1242
+ struct lm_ggml_tensor * a,
1243
+ int64_t ne0,
1244
+ int64_t ne1,
1245
+ int64_t ne2);
1246
+
1247
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_4d(
1248
+ struct lm_ggml_context * ctx,
1249
+ struct lm_ggml_tensor * a,
1250
+ int64_t ne0,
1251
+ int64_t ne1,
1252
+ int64_t ne2,
1253
+ int64_t ne3);
1254
+
1255
+ // return view(a), b specifies the new shape
1256
+ // TODO: when we start computing gradient, make a copy instead of view
1257
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape(
1258
+ struct lm_ggml_context * ctx,
1259
+ struct lm_ggml_tensor * a,
1260
+ struct lm_ggml_tensor * b);
1261
+
1262
+ // return view(a)
1263
+ // TODO: when we start computing gradient, make a copy instead of view
1264
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_1d(
1265
+ struct lm_ggml_context * ctx,
1266
+ struct lm_ggml_tensor * a,
1267
+ int64_t ne0);
1268
+
1269
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_2d(
1270
+ struct lm_ggml_context * ctx,
1271
+ struct lm_ggml_tensor * a,
1272
+ int64_t ne0,
1273
+ int64_t ne1);
1274
+
1275
+ // return view(a)
1276
+ // TODO: when we start computing gradient, make a copy instead of view
1277
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_3d(
1278
+ struct lm_ggml_context * ctx,
1279
+ struct lm_ggml_tensor * a,
1280
+ int64_t ne0,
1281
+ int64_t ne1,
1282
+ int64_t ne2);
1283
+
1284
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_4d(
1285
+ struct lm_ggml_context * ctx,
1286
+ struct lm_ggml_tensor * a,
1287
+ int64_t ne0,
1288
+ int64_t ne1,
1289
+ int64_t ne2,
1290
+ int64_t ne3);
1291
+
1292
+ // offset in bytes
1293
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_1d(
1294
+ struct lm_ggml_context * ctx,
1295
+ struct lm_ggml_tensor * a,
1296
+ int64_t ne0,
1297
+ size_t offset);
1298
+
1299
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_2d(
1300
+ struct lm_ggml_context * ctx,
1301
+ struct lm_ggml_tensor * a,
1302
+ int64_t ne0,
1303
+ int64_t ne1,
1304
+ size_t nb1, // row stride in bytes
1305
+ size_t offset);
1306
+
1307
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_3d(
1308
+ struct lm_ggml_context * ctx,
1309
+ struct lm_ggml_tensor * a,
1310
+ int64_t ne0,
1311
+ int64_t ne1,
1312
+ int64_t ne2,
1313
+ size_t nb1, // row stride in bytes
1314
+ size_t nb2, // slice stride in bytes
1315
+ size_t offset);
1316
+
1317
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_4d(
1318
+ struct lm_ggml_context * ctx,
1319
+ struct lm_ggml_tensor * a,
1320
+ int64_t ne0,
1321
+ int64_t ne1,
1322
+ int64_t ne2,
1323
+ int64_t ne3,
1324
+ size_t nb1, // row stride in bytes
1325
+ size_t nb2, // slice stride in bytes
1326
+ size_t nb3,
1327
+ size_t offset);
1328
+
1329
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_permute(
1330
+ struct lm_ggml_context * ctx,
1331
+ struct lm_ggml_tensor * a,
1332
+ int axis0,
1333
+ int axis1,
1334
+ int axis2,
1335
+ int axis3);
1336
+
1337
+ // alias for lm_ggml_permute(ctx, a, 1, 0, 2, 3)
1338
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_transpose(
1339
+ struct lm_ggml_context * ctx,
1340
+ struct lm_ggml_tensor * a);
1341
+
1342
+ // supports 3D: a->ne[2] == b->ne[1]
1343
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows(
1344
+ struct lm_ggml_context * ctx,
1345
+ struct lm_ggml_tensor * a, // data
1346
+ struct lm_ggml_tensor * b); // row indices
1347
+
1348
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows_back(
1349
+ struct lm_ggml_context * ctx,
1350
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_get_rows result
1351
+ struct lm_ggml_tensor * b, // row indices
1352
+ struct lm_ggml_tensor * c); // data for lm_ggml_get_rows, only used for its shape
1353
+
1354
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag(
1355
+ struct lm_ggml_context * ctx,
1356
+ struct lm_ggml_tensor * a);
1357
+
1358
+ // set elements above the diagonal to -INF
1359
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf(
1360
+ struct lm_ggml_context * ctx,
1361
+ struct lm_ggml_tensor * a,
1362
+ int n_past);
1363
+
1364
+ // in-place, returns view(a)
1365
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf_inplace(
1366
+ struct lm_ggml_context * ctx,
1367
+ struct lm_ggml_tensor * a,
1368
+ int n_past);
1369
+
1370
+ // set elements above the diagonal to 0
1371
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero(
1372
+ struct lm_ggml_context * ctx,
1373
+ struct lm_ggml_tensor * a,
1374
+ int n_past);
1375
+
1376
+ // in-place, returns view(a)
1377
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero_inplace(
1378
+ struct lm_ggml_context * ctx,
1379
+ struct lm_ggml_tensor * a,
1380
+ int n_past);
1381
+
1382
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max(
1383
+ struct lm_ggml_context * ctx,
1384
+ struct lm_ggml_tensor * a);
1385
+
1386
+ // in-place, returns view(a)
1387
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_inplace(
1388
+ struct lm_ggml_context * ctx,
1389
+ struct lm_ggml_tensor * a);
1390
+
1391
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1392
+ // mask is optional
1393
+ // max_bias = 0.0f for no ALiBi
1394
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext(
1395
+ struct lm_ggml_context * ctx,
1396
+ struct lm_ggml_tensor * a,
1397
+ struct lm_ggml_tensor * mask,
1398
+ float scale,
1399
+ float max_bias);
1400
+
1401
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back(
1402
+ struct lm_ggml_context * ctx,
1403
+ struct lm_ggml_tensor * a,
1404
+ struct lm_ggml_tensor * b,
1405
+ float scale,
1406
+ float max_bias);
1407
+
1408
+ // in-place, returns view(a)
1409
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back_inplace(
1410
+ struct lm_ggml_context * ctx,
1411
+ struct lm_ggml_tensor * a,
1412
+ struct lm_ggml_tensor * b,
1413
+ float scale,
1414
+ float max_bias);
1415
+
1416
+ // rotary position embedding
1417
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1418
+ // if (mode & LM_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1419
+ //
1420
+ // b is an int32 vector with size a->ne[2], it contains the positions
1421
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope(
1422
+ struct lm_ggml_context * ctx,
1423
+ struct lm_ggml_tensor * a,
1424
+ struct lm_ggml_tensor * b,
1425
+ int n_dims,
1426
+ int mode);
1427
+
1428
+ // in-place, returns view(a)
1429
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_inplace(
1430
+ struct lm_ggml_context * ctx,
1431
+ struct lm_ggml_tensor * a,
1432
+ struct lm_ggml_tensor * b,
1433
+ int n_dims,
1434
+ int mode);
1435
+
1436
+ // custom RoPE
1437
+ // c is freq factors (e.g. phi3-128k), (optional)
1438
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext(
1439
+ struct lm_ggml_context * ctx,
1440
+ struct lm_ggml_tensor * a,
1441
+ struct lm_ggml_tensor * b,
1442
+ struct lm_ggml_tensor * c,
1443
+ int n_dims,
1444
+ int mode,
1445
+ int n_ctx_orig,
1446
+ float freq_base,
1447
+ float freq_scale,
1448
+ float ext_factor,
1449
+ float attn_factor,
1450
+ float beta_fast,
1451
+ float beta_slow);
1452
+
1453
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi(
1454
+ struct lm_ggml_context * ctx,
1455
+ struct lm_ggml_tensor * a,
1456
+ struct lm_ggml_tensor * b,
1457
+ struct lm_ggml_tensor * c,
1458
+ int n_dims,
1459
+ int sections[4],
1460
+ int mode,
1461
+ int n_ctx_orig,
1462
+ float freq_base,
1463
+ float freq_scale,
1464
+ float ext_factor,
1465
+ float attn_factor,
1466
+ float beta_fast,
1467
+ float beta_slow);
1468
+
1469
+ // in-place, returns view(a)
1470
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_inplace(
1471
+ struct lm_ggml_context * ctx,
1472
+ struct lm_ggml_tensor * a,
1473
+ struct lm_ggml_tensor * b,
1474
+ struct lm_ggml_tensor * c,
1475
+ int n_dims,
1476
+ int mode,
1477
+ int n_ctx_orig,
1478
+ float freq_base,
1479
+ float freq_scale,
1480
+ float ext_factor,
1481
+ float attn_factor,
1482
+ float beta_fast,
1483
+ float beta_slow);
1484
+
1485
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom(
1486
+ struct lm_ggml_context * ctx,
1487
+ struct lm_ggml_tensor * a,
1488
+ struct lm_ggml_tensor * b,
1489
+ int n_dims,
1490
+ int mode,
1491
+ int n_ctx_orig,
1492
+ float freq_base,
1493
+ float freq_scale,
1494
+ float ext_factor,
1495
+ float attn_factor,
1496
+ float beta_fast,
1497
+ float beta_slow),
1498
+ "use lm_ggml_rope_ext instead");
1499
+
1500
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom_inplace(
1501
+ struct lm_ggml_context * ctx,
1502
+ struct lm_ggml_tensor * a,
1503
+ struct lm_ggml_tensor * b,
1504
+ int n_dims,
1505
+ int mode,
1506
+ int n_ctx_orig,
1507
+ float freq_base,
1508
+ float freq_scale,
1509
+ float ext_factor,
1510
+ float attn_factor,
1511
+ float beta_fast,
1512
+ float beta_slow),
1513
+ "use lm_ggml_rope_ext_inplace instead");
1514
+
1515
+ // compute correction dims for YaRN RoPE scaling
1516
+ LM_GGML_API void lm_ggml_rope_yarn_corr_dims(
1517
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1518
+
1519
+ // rotary position embedding backward, i.e compute dx from dy
1520
+ // a - dy
1521
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_back(
1522
+ struct lm_ggml_context * ctx,
1523
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_rope result
1524
+ struct lm_ggml_tensor * b, // positions
1525
+ struct lm_ggml_tensor * c, // freq factors
1526
+ int n_dims,
1527
+ int mode,
1528
+ int n_ctx_orig,
1529
+ float freq_base,
1530
+ float freq_scale,
1531
+ float ext_factor,
1532
+ float attn_factor,
1533
+ float beta_fast,
1534
+ float beta_slow);
1535
+
1536
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi_back(
1537
+ struct lm_ggml_context * ctx,
1538
+ struct lm_ggml_tensor * a,
1539
+ struct lm_ggml_tensor * b,
1540
+ struct lm_ggml_tensor * c,
1541
+ int n_dims,
1542
+ int sections[4],
1543
+ int mode,
1544
+ int n_ctx_orig,
1545
+ float freq_base,
1546
+ float freq_scale,
1547
+ float ext_factor,
1548
+ float attn_factor,
1549
+ float beta_fast,
1550
+ float beta_slow);
1551
+
1552
+
1553
+ // clamp
1554
+ // in-place, returns view(a)
1555
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_clamp(
1556
+ struct lm_ggml_context * ctx,
1557
+ struct lm_ggml_tensor * a,
1558
+ float min,
1559
+ float max);
1560
+
1561
+ // im2col
1562
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1563
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col(
1564
+ struct lm_ggml_context * ctx,
1565
+ struct lm_ggml_tensor * a, // convolution kernel
1566
+ struct lm_ggml_tensor * b, // data
1567
+ int s0, // stride dimension 0
1568
+ int s1, // stride dimension 1
1569
+ int p0, // padding dimension 0
1570
+ int p1, // padding dimension 1
1571
+ int d0, // dilation dimension 0
1572
+ int d1, // dilation dimension 1
1573
+ bool is_2D,
1574
+ enum lm_ggml_type dst_type);
1575
+
1576
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col_back(
1577
+ struct lm_ggml_context * ctx,
1578
+ struct lm_ggml_tensor * a, // convolution kernel
1579
+ struct lm_ggml_tensor * b, // gradient of im2col output
1580
+ int64_t * ne, // shape of im2col input
1581
+ int s0, // stride dimension 0
1582
+ int s1, // stride dimension 1
1583
+ int p0, // padding dimension 0
1584
+ int p1, // padding dimension 1
1585
+ int d0, // dilation dimension 0
1586
+ int d1, // dilation dimension 1
1587
+ bool is_2D);
1588
+
1589
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d(
1590
+ struct lm_ggml_context * ctx,
1591
+ struct lm_ggml_tensor * a, // convolution kernel
1592
+ struct lm_ggml_tensor * b, // data
1593
+ int s0, // stride
1594
+ int p0, // padding
1595
+ int d0); // dilation
1596
+
1597
+ // conv_1d with padding = half
1598
+ // alias for lm_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1599
+ LM_GGML_API struct lm_ggml_tensor* lm_ggml_conv_1d_ph(
1600
+ struct lm_ggml_context * ctx,
1601
+ struct lm_ggml_tensor * a, // convolution kernel
1602
+ struct lm_ggml_tensor * b, // data
1603
+ int s, // stride
1604
+ int d); // dilation
1605
+
1606
+ // depthwise
1607
+ // TODO: this is very likely wrong for some cases! - needs more testing
1608
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw(
1609
+ struct lm_ggml_context * ctx,
1610
+ struct lm_ggml_tensor * a, // convolution kernel
1611
+ struct lm_ggml_tensor * b, // data
1612
+ int s0, // stride
1613
+ int p0, // padding
1614
+ int d0); // dilation
1615
+
1616
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw_ph(
1617
+ struct lm_ggml_context * ctx,
1618
+ struct lm_ggml_tensor * a, // convolution kernel
1619
+ struct lm_ggml_tensor * b, // data
1620
+ int s0, // stride
1621
+ int d0); // dilation
1622
+
1623
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_1d(
1624
+ struct lm_ggml_context * ctx,
1625
+ struct lm_ggml_tensor * a, // convolution kernel
1626
+ struct lm_ggml_tensor * b, // data
1627
+ int s0, // stride
1628
+ int p0, // padding
1629
+ int d0); // dilation
1630
+
1631
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d(
1632
+ struct lm_ggml_context * ctx,
1633
+ struct lm_ggml_tensor * a, // convolution kernel
1634
+ struct lm_ggml_tensor * b, // data
1635
+ int s0, // stride dimension 0
1636
+ int s1, // stride dimension 1
1637
+ int p0, // padding dimension 0
1638
+ int p1, // padding dimension 1
1639
+ int d0, // dilation dimension 0
1640
+ int d1); // dilation dimension 1
1641
+
1642
+ // kernel size is a->ne[0] x a->ne[1]
1643
+ // stride is equal to kernel size
1644
+ // padding is zero
1645
+ // example:
1646
+ // a: 16 16 3 768
1647
+ // b: 1024 1024 3 1
1648
+ // res: 64 64 768 1
1649
+ // used in sam
1650
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_sk_p0(
1651
+ struct lm_ggml_context * ctx,
1652
+ struct lm_ggml_tensor * a,
1653
+ struct lm_ggml_tensor * b);
1654
+
1655
+ // kernel size is a->ne[0] x a->ne[1]
1656
+ // stride is 1
1657
+ // padding is half
1658
+ // example:
1659
+ // a: 3 3 256 256
1660
+ // b: 64 64 256 1
1661
+ // res: 64 64 256 1
1662
+ // used in sam
1663
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_s1_ph(
1664
+ struct lm_ggml_context * ctx,
1665
+ struct lm_ggml_tensor * a,
1666
+ struct lm_ggml_tensor * b);
1667
+
1668
+ // depthwise (via im2col and mul_mat)
1669
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_dw(
1670
+ struct lm_ggml_context * ctx,
1671
+ struct lm_ggml_tensor * a, // convolution kernel
1672
+ struct lm_ggml_tensor * b, // data
1673
+ int s0, // stride dimension 0
1674
+ int s1, // stride dimension 1
1675
+ int p0, // padding dimension 0
1676
+ int p1, // padding dimension 1
1677
+ int d0, // dilation dimension 0
1678
+ int d1); // dilation dimension 1
1679
+
1680
+ // Depthwise 2D convolution
1681
+ // may be faster than lm_ggml_conv_2d_dw, but not available in all backends
1682
+ // a: KW KH 1 C convolution kernel
1683
+ // b: W H C N input data
1684
+ // res: W_out H_out C N
1685
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_dw_direct(
1686
+ struct lm_ggml_context * ctx,
1687
+ struct lm_ggml_tensor * a,
1688
+ struct lm_ggml_tensor * b,
1689
+ int stride0,
1690
+ int stride1,
1691
+ int pad0,
1692
+ int pad1,
1693
+ int dilation0,
1694
+ int dilation1);
1695
+
1696
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_2d_p0(
1697
+ struct lm_ggml_context * ctx,
1698
+ struct lm_ggml_tensor * a,
1699
+ struct lm_ggml_tensor * b,
1700
+ int stride);
1701
+
1702
+ enum lm_ggml_op_pool {
1703
+ LM_GGML_OP_POOL_MAX,
1704
+ LM_GGML_OP_POOL_AVG,
1705
+ LM_GGML_OP_POOL_COUNT,
1706
+ };
1707
+
1708
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_1d(
1709
+ struct lm_ggml_context * ctx,
1710
+ struct lm_ggml_tensor * a,
1711
+ enum lm_ggml_op_pool op,
1712
+ int k0, // kernel size
1713
+ int s0, // stride
1714
+ int p0); // padding
1715
+
1716
+ // the result will have 2*p0 padding for the first dimension
1717
+ // and 2*p1 padding for the second dimension
1718
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d(
1719
+ struct lm_ggml_context * ctx,
1720
+ struct lm_ggml_tensor * a,
1721
+ enum lm_ggml_op_pool op,
1722
+ int k0,
1723
+ int k1,
1724
+ int s0,
1725
+ int s1,
1726
+ float p0,
1727
+ float p1);
1728
+
1729
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d_back(
1730
+ struct lm_ggml_context * ctx,
1731
+ struct lm_ggml_tensor * a,
1732
+ struct lm_ggml_tensor * af, // "a"/input used in forward pass
1733
+ enum lm_ggml_op_pool op,
1734
+ int k0,
1735
+ int k1,
1736
+ int s0,
1737
+ int s1,
1738
+ float p0,
1739
+ float p1);
1740
+
1741
+ enum lm_ggml_scale_mode {
1742
+ LM_GGML_SCALE_MODE_NEAREST = 0,
1743
+ LM_GGML_SCALE_MODE_BILINEAR = 1,
1744
+ };
1745
+
1746
+ // interpolate
1747
+ // multiplies ne0 and ne1 by scale factor
1748
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale(
1749
+ struct lm_ggml_context * ctx,
1750
+ struct lm_ggml_tensor * a,
1751
+ int scale_factor,
1752
+ enum lm_ggml_scale_mode mode);
1753
+
1754
+ // interpolate
1755
+ // interpolate scale to specified dimensions
1756
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale_ext(
1757
+ struct lm_ggml_context * ctx,
1758
+ struct lm_ggml_tensor * a,
1759
+ int ne0,
1760
+ int ne1,
1761
+ int ne2,
1762
+ int ne3,
1763
+ enum lm_ggml_scale_mode mode);
1764
+
1765
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1766
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad(
1767
+ struct lm_ggml_context * ctx,
1768
+ struct lm_ggml_tensor * a,
1769
+ int p0,
1770
+ int p1,
1771
+ int p2,
1772
+ int p3);
1773
+
1774
+ // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
1775
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad_reflect_1d(
1776
+ struct lm_ggml_context * ctx,
1777
+ struct lm_ggml_tensor * a,
1778
+ int p0,
1779
+ int p1);
1780
+
1781
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1782
+ // timesteps: [N,]
1783
+ // return: [N, dim]
1784
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_timestep_embedding(
1785
+ struct lm_ggml_context * ctx,
1786
+ struct lm_ggml_tensor * timesteps,
1787
+ int dim,
1788
+ int max_period);
1789
+
1790
+ // sort rows
1791
+ enum lm_ggml_sort_order {
1792
+ LM_GGML_SORT_ORDER_ASC,
1793
+ LM_GGML_SORT_ORDER_DESC,
1794
+ };
1795
+
1796
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argsort(
1797
+ struct lm_ggml_context * ctx,
1798
+ struct lm_ggml_tensor * a,
1799
+ enum lm_ggml_sort_order order);
1800
+
1801
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_arange(
1802
+ struct lm_ggml_context * ctx,
1803
+ float start,
1804
+ float stop,
1805
+ float step);
1806
+
1807
+ // top k elements per row
1808
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_top_k(
1809
+ struct lm_ggml_context * ctx,
1810
+ struct lm_ggml_tensor * a,
1811
+ int k);
1812
+
1813
+ #define LM_GGML_KQ_MASK_PAD 64
1814
+
1815
+ // q: [n_embd_k, n_batch, n_head, 1]
1816
+ // k: [n_embd_k, n_kv, n_head_kv, 1]
1817
+ // v: [n_embd_v, n_kv, n_head_kv, 1] !! not transposed !!
1818
+ // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = LM_GGML_PAD(n_batch, LM_GGML_KQ_MASK_PAD) !!
1819
+ // res: [n_embd_v, n_head, n_batch, 1] !! permuted !!
1820
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_ext(
1821
+ struct lm_ggml_context * ctx,
1822
+ struct lm_ggml_tensor * q,
1823
+ struct lm_ggml_tensor * k,
1824
+ struct lm_ggml_tensor * v,
1825
+ struct lm_ggml_tensor * mask,
1826
+ float scale,
1827
+ float max_bias,
1828
+ float logit_softcap);
1829
+
1830
+ LM_GGML_API void lm_ggml_flash_attn_ext_set_prec(
1831
+ struct lm_ggml_tensor * a,
1832
+ enum lm_ggml_prec prec);
1833
+
1834
+ LM_GGML_API enum lm_ggml_prec lm_ggml_flash_attn_ext_get_prec(
1835
+ const struct lm_ggml_tensor * a);
1836
+
1837
+ // TODO: needs to be adapted to lm_ggml_flash_attn_ext
1838
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_back(
1839
+ struct lm_ggml_context * ctx,
1840
+ struct lm_ggml_tensor * q,
1841
+ struct lm_ggml_tensor * k,
1842
+ struct lm_ggml_tensor * v,
1843
+ struct lm_ggml_tensor * d,
1844
+ bool masked);
1845
+
1846
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_conv(
1847
+ struct lm_ggml_context * ctx,
1848
+ struct lm_ggml_tensor * sx,
1849
+ struct lm_ggml_tensor * c);
1850
+
1851
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_scan(
1852
+ struct lm_ggml_context * ctx,
1853
+ struct lm_ggml_tensor * s,
1854
+ struct lm_ggml_tensor * x,
1855
+ struct lm_ggml_tensor * dt,
1856
+ struct lm_ggml_tensor * A,
1857
+ struct lm_ggml_tensor * B,
1858
+ struct lm_ggml_tensor * C);
1859
+
1860
+ // partition into non-overlapping windows with padding if needed
1861
+ // example:
1862
+ // a: 768 64 64 1
1863
+ // w: 14
1864
+ // res: 768 14 14 25
1865
+ // used in sam
1866
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_part(
1867
+ struct lm_ggml_context * ctx,
1868
+ struct lm_ggml_tensor * a,
1869
+ int w);
1870
+
1871
+ // reverse of lm_ggml_win_part
1872
+ // used in sam
1873
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_unpart(
1874
+ struct lm_ggml_context * ctx,
1875
+ struct lm_ggml_tensor * a,
1876
+ int w0,
1877
+ int h0,
1878
+ int w);
1879
+
1880
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary(
1881
+ struct lm_ggml_context * ctx,
1882
+ struct lm_ggml_tensor * a,
1883
+ enum lm_ggml_unary_op op);
1884
+
1885
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary_inplace(
1886
+ struct lm_ggml_context * ctx,
1887
+ struct lm_ggml_tensor * a,
1888
+ enum lm_ggml_unary_op op);
1889
+
1890
+ // used in sam
1891
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rel_pos(
1892
+ struct lm_ggml_context * ctx,
1893
+ struct lm_ggml_tensor * a,
1894
+ int qh,
1895
+ int kh);
1896
+
1897
+ // used in sam
1898
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos(
1899
+ struct lm_ggml_context * ctx,
1900
+ struct lm_ggml_tensor * a,
1901
+ struct lm_ggml_tensor * pw,
1902
+ struct lm_ggml_tensor * ph);
1903
+
1904
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos_inplace(
1905
+ struct lm_ggml_context * ctx,
1906
+ struct lm_ggml_tensor * a,
1907
+ struct lm_ggml_tensor * pw,
1908
+ struct lm_ggml_tensor * ph);
1909
+
1910
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv6(
1911
+ struct lm_ggml_context * ctx,
1912
+ struct lm_ggml_tensor * k,
1913
+ struct lm_ggml_tensor * v,
1914
+ struct lm_ggml_tensor * r,
1915
+ struct lm_ggml_tensor * tf,
1916
+ struct lm_ggml_tensor * td,
1917
+ struct lm_ggml_tensor * state);
1918
+
1919
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gated_linear_attn(
1920
+ struct lm_ggml_context * ctx,
1921
+ struct lm_ggml_tensor * k,
1922
+ struct lm_ggml_tensor * v,
1923
+ struct lm_ggml_tensor * q,
1924
+ struct lm_ggml_tensor * g,
1925
+ struct lm_ggml_tensor * state,
1926
+ float scale);
1927
+
1928
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv7(
1929
+ struct lm_ggml_context * ctx,
1930
+ struct lm_ggml_tensor * r,
1931
+ struct lm_ggml_tensor * w,
1932
+ struct lm_ggml_tensor * k,
1933
+ struct lm_ggml_tensor * v,
1934
+ struct lm_ggml_tensor * a,
1935
+ struct lm_ggml_tensor * b,
1936
+ struct lm_ggml_tensor * state);
1937
+
1938
+ // custom operators
1939
+
1940
+ typedef void (*lm_ggml_custom1_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, int ith, int nth, void * userdata);
1941
+ typedef void (*lm_ggml_custom2_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, int ith, int nth, void * userdata);
1942
+ typedef void (*lm_ggml_custom3_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, const struct lm_ggml_tensor * c, int ith, int nth, void * userdata);
1943
+
1944
+ #define LM_GGML_N_TASKS_MAX (-1)
1945
+ // n_tasks == LM_GGML_N_TASKS_MAX means to use max number of tasks
1946
+
1947
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1(
1948
+ struct lm_ggml_context * ctx,
1949
+ struct lm_ggml_tensor * a,
1950
+ lm_ggml_custom1_op_t fun,
1951
+ int n_tasks,
1952
+ void * userdata);
1953
+
1954
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace(
1955
+ struct lm_ggml_context * ctx,
1956
+ struct lm_ggml_tensor * a,
1957
+ lm_ggml_custom1_op_t fun,
1958
+ int n_tasks,
1959
+ void * userdata);
1960
+
1961
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2(
1962
+ struct lm_ggml_context * ctx,
1963
+ struct lm_ggml_tensor * a,
1964
+ struct lm_ggml_tensor * b,
1965
+ lm_ggml_custom2_op_t fun,
1966
+ int n_tasks,
1967
+ void * userdata);
1968
+
1969
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace(
1970
+ struct lm_ggml_context * ctx,
1971
+ struct lm_ggml_tensor * a,
1972
+ struct lm_ggml_tensor * b,
1973
+ lm_ggml_custom2_op_t fun,
1974
+ int n_tasks,
1975
+ void * userdata);
1976
+
1977
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3(
1978
+ struct lm_ggml_context * ctx,
1979
+ struct lm_ggml_tensor * a,
1980
+ struct lm_ggml_tensor * b,
1981
+ struct lm_ggml_tensor * c,
1982
+ lm_ggml_custom3_op_t fun,
1983
+ int n_tasks,
1984
+ void * userdata);
1985
+
1986
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace(
1987
+ struct lm_ggml_context * ctx,
1988
+ struct lm_ggml_tensor * a,
1989
+ struct lm_ggml_tensor * b,
1990
+ struct lm_ggml_tensor * c,
1991
+ lm_ggml_custom3_op_t fun,
1992
+ int n_tasks,
1993
+ void * userdata);
1994
+
1995
+ typedef void (*lm_ggml_custom_op_t)(struct lm_ggml_tensor * dst , int ith, int nth, void * userdata);
1996
+
1997
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_custom_4d(
1998
+ struct lm_ggml_context * ctx,
1999
+ enum lm_ggml_type type,
2000
+ int64_t ne0,
2001
+ int64_t ne1,
2002
+ int64_t ne2,
2003
+ int64_t ne3,
2004
+ struct lm_ggml_tensor ** args,
2005
+ int n_args,
2006
+ lm_ggml_custom_op_t fun,
2007
+ int n_tasks,
2008
+ void * userdata);
2009
+
2010
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_custom_inplace(
2011
+ struct lm_ggml_context * ctx,
2012
+ struct lm_ggml_tensor * a,
2013
+ struct lm_ggml_tensor ** args,
2014
+ int n_args,
2015
+ lm_ggml_custom_op_t fun,
2016
+ int n_tasks,
2017
+ void * userdata);
2018
+
2019
+ // loss function
2020
+
2021
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss(
2022
+ struct lm_ggml_context * ctx,
2023
+ struct lm_ggml_tensor * a, // logits
2024
+ struct lm_ggml_tensor * b); // labels
2025
+
2026
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss_back(
2027
+ struct lm_ggml_context * ctx,
2028
+ struct lm_ggml_tensor * a, // logits
2029
+ struct lm_ggml_tensor * b, // labels
2030
+ struct lm_ggml_tensor * c); // gradients of cross_entropy_loss result
2031
+
2032
+ // AdamW optimizer step
2033
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2034
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2035
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_adamw(
2036
+ struct lm_ggml_context * ctx,
2037
+ struct lm_ggml_tensor * a,
2038
+ struct lm_ggml_tensor * grad,
2039
+ struct lm_ggml_tensor * m,
2040
+ struct lm_ggml_tensor * v,
2041
+ struct lm_ggml_tensor * adamw_params); // parameters such a the learning rate
2042
+
2043
+ //
2044
+ // automatic differentiation
2045
+ //
2046
+
2047
+ LM_GGML_API void lm_ggml_build_forward_expand(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2048
+ LM_GGML_API void lm_ggml_build_backward_expand(
2049
+ struct lm_ggml_context * ctx_static, // context for static gradients (loss + gradient accumulation)
2050
+ struct lm_ggml_context * ctx_compute, // context for gradient computation
2051
+ struct lm_ggml_cgraph * cgraph,
2052
+ bool accumulate); // whether or not gradients should be accumulated, requires static allocation of tensors in ctx_static
2053
+
2054
+ // graph allocation in a context
2055
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph (struct lm_ggml_context * ctx); // size = LM_GGML_DEFAULT_GRAPH_SIZE, grads = false
2056
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph_custom(struct lm_ggml_context * ctx, size_t size, bool grads);
2057
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_dup (struct lm_ggml_context * ctx, struct lm_ggml_cgraph * cgraph);
2058
+ LM_GGML_API void lm_ggml_graph_cpy (struct lm_ggml_cgraph * src, struct lm_ggml_cgraph * dst);
2059
+ LM_GGML_API void lm_ggml_graph_reset (struct lm_ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2060
+ LM_GGML_API void lm_ggml_graph_clear (struct lm_ggml_cgraph * cgraph);
2061
+
2062
+ LM_GGML_API int lm_ggml_graph_size (struct lm_ggml_cgraph * cgraph);
2063
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_node (struct lm_ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2064
+ LM_GGML_API struct lm_ggml_tensor ** lm_ggml_graph_nodes (struct lm_ggml_cgraph * cgraph);
2065
+ LM_GGML_API int lm_ggml_graph_n_nodes(struct lm_ggml_cgraph * cgraph);
2066
+
2067
+ LM_GGML_API void lm_ggml_graph_add_node(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2068
+
2069
+ LM_GGML_API size_t lm_ggml_graph_overhead(void);
2070
+ LM_GGML_API size_t lm_ggml_graph_overhead_custom(size_t size, bool grads);
2071
+
2072
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_tensor (const struct lm_ggml_cgraph * cgraph, const char * name);
2073
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad (const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2074
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad_acc(const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2075
+
2076
+ LM_GGML_API void lm_ggml_graph_export(const struct lm_ggml_cgraph * cgraph, const char * fname);
2077
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_import(const char * fname, struct lm_ggml_context ** ctx_data, struct lm_ggml_context ** ctx_eval);
2078
+
2079
+ // print info and performance information for the graph
2080
+ LM_GGML_API void lm_ggml_graph_print(const struct lm_ggml_cgraph * cgraph);
2081
+
2082
+ // dump the graph into a file using the dot format
2083
+ LM_GGML_API void lm_ggml_graph_dump_dot(const struct lm_ggml_cgraph * gb, const struct lm_ggml_cgraph * gf, const char * filename);
2084
+
2085
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2086
+ typedef void (*lm_ggml_log_callback)(enum lm_ggml_log_level level, const char * text, void * user_data);
2087
+
2088
+ // Set callback for all future logging events.
2089
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2090
+ LM_GGML_API void lm_ggml_log_set(lm_ggml_log_callback log_callback, void * user_data);
2091
+
2092
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_zero(struct lm_ggml_tensor * tensor);
2093
+
2094
+ //
2095
+ // quantization
2096
+ //
2097
+
2098
+ // - lm_ggml_quantize_init can be called multiple times with the same type
2099
+ // it will only initialize the quantization tables for the first call or after lm_ggml_quantize_free
2100
+ // automatically called by lm_ggml_quantize_chunk for convenience
2101
+ //
2102
+ // - lm_ggml_quantize_free will free any memory allocated by lm_ggml_quantize_init
2103
+ // call this at the end of the program to avoid memory leaks
2104
+ //
2105
+ // note: these are thread-safe
2106
+ //
2107
+ LM_GGML_API void lm_ggml_quantize_init(enum lm_ggml_type type);
2108
+ LM_GGML_API void lm_ggml_quantize_free(void);
2109
+
2110
+ // some quantization type cannot be used without an importance matrix
2111
+ LM_GGML_API bool lm_ggml_quantize_requires_imatrix(enum lm_ggml_type type);
2112
+
2113
+ // calls lm_ggml_quantize_init internally (i.e. can allocate memory)
2114
+ LM_GGML_API size_t lm_ggml_quantize_chunk(
2115
+ enum lm_ggml_type type,
2116
+ const float * src,
2117
+ void * dst,
2118
+ int64_t start,
2119
+ int64_t nrows,
2120
+ int64_t n_per_row,
2121
+ const float * imatrix);
2122
+
2123
+ #ifdef __cplusplus
2124
+ // restrict not standard in C++
2125
+ # if defined(__GNUC__)
2126
+ # define LM_GGML_RESTRICT __restrict__
2127
+ # elif defined(__clang__)
2128
+ # define LM_GGML_RESTRICT __restrict
2129
+ # elif defined(_MSC_VER)
2130
+ # define LM_GGML_RESTRICT __restrict
2131
+ # else
2132
+ # define LM_GGML_RESTRICT
2133
+ # endif
2134
+ #else
2135
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2136
+ # define LM_GGML_RESTRICT __restrict
2137
+ # else
2138
+ # define LM_GGML_RESTRICT restrict
2139
+ # endif
2140
+ #endif
2141
+ typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2142
+ typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2143
+
2144
+ struct lm_ggml_type_traits {
2145
+ const char * type_name;
2146
+ int64_t blck_size;
2147
+ int64_t blck_size_interleave; // interleave elements in blocks
2148
+ size_t type_size;
2149
+ bool is_quantized;
2150
+ lm_ggml_to_float_t to_float;
2151
+ lm_ggml_from_float_t from_float_ref;
2152
+ };
2153
+
2154
+ LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2155
+
2156
+ // ggml threadpool
2157
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2158
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2159
+
2160
+ // scheduling priorities
2161
+ enum lm_ggml_sched_priority {
2162
+ LM_GGML_SCHED_PRIO_NORMAL,
2163
+ LM_GGML_SCHED_PRIO_MEDIUM,
2164
+ LM_GGML_SCHED_PRIO_HIGH,
2165
+ LM_GGML_SCHED_PRIO_REALTIME
2166
+ };
2167
+
2168
+ // threadpool params
2169
+ // Use lm_ggml_threadpool_params_default() or lm_ggml_threadpool_params_init() to populate the defaults
2170
+ struct lm_ggml_threadpool_params {
2171
+ bool cpumask[LM_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2172
+ int n_threads; // number of threads
2173
+ enum lm_ggml_sched_priority prio; // thread priority
2174
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2175
+ bool strict_cpu; // strict cpu placement
2176
+ bool paused; // start in paused state
2177
+ };
2178
+
2179
+ struct lm_ggml_threadpool; // forward declaration, see ggml.c
2180
+
2181
+ typedef struct lm_ggml_threadpool * lm_ggml_threadpool_t;
2182
+
2183
+ LM_GGML_API struct lm_ggml_threadpool_params lm_ggml_threadpool_params_default(int n_threads);
2184
+ LM_GGML_API void lm_ggml_threadpool_params_init (struct lm_ggml_threadpool_params * p, int n_threads);
2185
+ LM_GGML_API bool lm_ggml_threadpool_params_match (const struct lm_ggml_threadpool_params * p0, const struct lm_ggml_threadpool_params * p1);
2186
+
2187
+ #ifdef __cplusplus
2188
+ }
2189
+ #endif