cui-llama.rn 1.5.0 → 1.6.0

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 (309) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +317 -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 +124 -124
  7. package/android/src/main/java/com/rnllama/LlamaContext.java +645 -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 +1263 -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/README.md +4 -4
  23. package/cpp/ggml-llama-sim.metallib +0 -0
  24. package/cpp/ggml-llama.metallib +0 -0
  25. package/cpp/ggml-metal-impl.h +597 -597
  26. package/cpp/ggml-metal.m +4 -0
  27. package/cpp/ggml.h +1 -1
  28. package/cpp/rn-llama.cpp +873 -873
  29. package/cpp/rn-llama.h +138 -138
  30. package/cpp/sampling.h +107 -107
  31. package/cpp/unicode-data.cpp +7034 -7034
  32. package/cpp/unicode-data.h +20 -20
  33. package/cpp/unicode.cpp +849 -849
  34. package/cpp/unicode.h +66 -66
  35. package/ios/CMakeLists.txt +116 -108
  36. package/ios/RNLlama.h +7 -7
  37. package/ios/RNLlama.mm +418 -405
  38. package/ios/RNLlamaContext.h +57 -57
  39. package/ios/RNLlamaContext.mm +835 -835
  40. package/ios/rnllama.xcframework/Info.plist +74 -74
  41. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/binary-ops.h +16 -0
  42. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/chat.h +143 -0
  43. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/common.h +677 -0
  44. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/cpu-common.h +72 -0
  45. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-alloc.h +76 -0
  46. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  47. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-backend.h +354 -0
  48. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-common.h +1857 -0
  49. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpp.h +39 -0
  50. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu-aarch64.h +8 -0
  51. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu-impl.h +512 -0
  52. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu-quants.h +63 -0
  53. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu-traits.h +38 -0
  54. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-cpu.h +138 -0
  55. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-impl.h +594 -0
  56. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  57. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-metal.h +66 -0
  58. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-opt.h +216 -0
  59. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-quants.h +100 -0
  60. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml-threading.h +14 -0
  61. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ggml.h +2222 -0
  62. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/gguf.h +202 -0
  63. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  64. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/json.hpp +24766 -0
  65. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-adapter.h +76 -0
  66. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-arch.h +428 -0
  67. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-batch.h +88 -0
  68. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-chat.h +56 -0
  69. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-context.h +265 -0
  70. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-cparams.h +38 -0
  71. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-cpp.h +30 -0
  72. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-grammar.h +173 -0
  73. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-graph.h +592 -0
  74. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-hparams.h +156 -0
  75. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-impl.h +61 -0
  76. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-io.h +35 -0
  77. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-kv-cache.h +213 -0
  78. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-memory.h +21 -0
  79. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-mmap.h +68 -0
  80. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-model-loader.h +169 -0
  81. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-model.h +409 -0
  82. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-sampling.h +32 -0
  83. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama-vocab.h +125 -0
  84. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/llama.h +1434 -0
  85. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/log.h +132 -0
  86. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  87. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  88. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/ops.h +128 -0
  89. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/rn-llama.h +138 -0
  90. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/sampling.h +107 -0
  91. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/sgemm.h +14 -0
  92. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/simd-mappings.h +888 -0
  93. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/speculative.h +28 -0
  94. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/unary-ops.h +28 -0
  95. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/unicode-data.h +20 -0
  96. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/unicode.h +66 -0
  97. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Headers/vec.h +802 -0
  98. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/Info.plist +0 -0
  99. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/ggml-llama.metallib +0 -0
  100. package/ios/rnllama.xcframework/ios-arm64/rnllama.framework/rnllama +0 -0
  101. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/binary-ops.h +16 -0
  102. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/chat.h +143 -0
  103. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/common.h +677 -0
  104. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/cpu-common.h +72 -0
  105. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-alloc.h +76 -0
  106. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  107. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend.h +354 -0
  108. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-common.h +1857 -0
  109. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpp.h +39 -0
  110. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-aarch64.h +8 -0
  111. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-impl.h +512 -0
  112. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-quants.h +63 -0
  113. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-traits.h +38 -0
  114. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu.h +138 -0
  115. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-impl.h +594 -0
  116. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  117. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal.h +66 -0
  118. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-opt.h +216 -0
  119. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-quants.h +100 -0
  120. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-threading.h +14 -0
  121. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ggml.h +2222 -0
  122. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/gguf.h +202 -0
  123. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  124. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/json.hpp +24766 -0
  125. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-adapter.h +76 -0
  126. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-arch.h +428 -0
  127. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-batch.h +88 -0
  128. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-chat.h +56 -0
  129. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-context.h +265 -0
  130. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cparams.h +38 -0
  131. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cpp.h +30 -0
  132. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-grammar.h +173 -0
  133. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-graph.h +592 -0
  134. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-hparams.h +156 -0
  135. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-impl.h +61 -0
  136. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-io.h +35 -0
  137. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-kv-cache.h +213 -0
  138. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-memory.h +21 -0
  139. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-mmap.h +68 -0
  140. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model-loader.h +169 -0
  141. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model.h +409 -0
  142. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-sampling.h +32 -0
  143. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama-vocab.h +125 -0
  144. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/llama.h +1434 -0
  145. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/log.h +132 -0
  146. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  147. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  148. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/ops.h +128 -0
  149. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/rn-llama.h +138 -0
  150. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/sampling.h +107 -0
  151. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/sgemm.h +14 -0
  152. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/simd-mappings.h +888 -0
  153. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/speculative.h +28 -0
  154. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/unary-ops.h +28 -0
  155. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/unicode-data.h +20 -0
  156. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/unicode.h +66 -0
  157. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Headers/vec.h +802 -0
  158. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/Info.plist +0 -0
  159. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/_CodeSignature/CodeResources +101 -0
  160. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/ggml-llama-sim.metallib +0 -0
  161. package/ios/rnllama.xcframework/ios-arm64_x86_64-simulator/rnllama.framework/rnllama +0 -0
  162. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/binary-ops.h +16 -0
  163. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/chat.h +143 -0
  164. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/common.h +677 -0
  165. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/cpu-common.h +72 -0
  166. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-alloc.h +76 -0
  167. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  168. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-backend.h +354 -0
  169. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-common.h +1857 -0
  170. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpp.h +39 -0
  171. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu-aarch64.h +8 -0
  172. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu-impl.h +512 -0
  173. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu-quants.h +63 -0
  174. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu-traits.h +38 -0
  175. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-cpu.h +138 -0
  176. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-impl.h +594 -0
  177. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  178. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-metal.h +66 -0
  179. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-opt.h +216 -0
  180. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-quants.h +100 -0
  181. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml-threading.h +14 -0
  182. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ggml.h +2222 -0
  183. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/gguf.h +202 -0
  184. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  185. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/json.hpp +24766 -0
  186. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-adapter.h +76 -0
  187. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-arch.h +428 -0
  188. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-batch.h +88 -0
  189. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-chat.h +56 -0
  190. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-context.h +265 -0
  191. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-cparams.h +38 -0
  192. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-cpp.h +30 -0
  193. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-grammar.h +173 -0
  194. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-graph.h +592 -0
  195. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-hparams.h +156 -0
  196. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-impl.h +61 -0
  197. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-io.h +35 -0
  198. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-kv-cache.h +213 -0
  199. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-memory.h +21 -0
  200. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-mmap.h +68 -0
  201. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-model-loader.h +169 -0
  202. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-model.h +409 -0
  203. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-sampling.h +32 -0
  204. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama-vocab.h +125 -0
  205. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/llama.h +1434 -0
  206. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/log.h +132 -0
  207. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  208. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  209. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/ops.h +128 -0
  210. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/rn-llama.h +138 -0
  211. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/sampling.h +107 -0
  212. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/sgemm.h +14 -0
  213. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/simd-mappings.h +888 -0
  214. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/speculative.h +28 -0
  215. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/unary-ops.h +28 -0
  216. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/unicode-data.h +20 -0
  217. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/unicode.h +66 -0
  218. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Headers/vec.h +802 -0
  219. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/Info.plist +0 -0
  220. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/ggml-llama.metallib +0 -0
  221. package/ios/rnllama.xcframework/tvos-arm64/rnllama.framework/rnllama +0 -0
  222. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/binary-ops.h +16 -0
  223. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/chat.h +143 -0
  224. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/common.h +677 -0
  225. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/cpu-common.h +72 -0
  226. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-alloc.h +76 -0
  227. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend-impl.h +255 -0
  228. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-backend.h +354 -0
  229. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-common.h +1857 -0
  230. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpp.h +39 -0
  231. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-aarch64.h +8 -0
  232. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-impl.h +512 -0
  233. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-quants.h +63 -0
  234. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu-traits.h +38 -0
  235. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-cpu.h +138 -0
  236. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-impl.h +594 -0
  237. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal-impl.h +597 -0
  238. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-metal.h +66 -0
  239. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-opt.h +216 -0
  240. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-quants.h +100 -0
  241. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml-threading.h +14 -0
  242. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ggml.h +2222 -0
  243. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/gguf.h +202 -0
  244. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/json-schema-to-grammar.h +21 -0
  245. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/json.hpp +24766 -0
  246. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-adapter.h +76 -0
  247. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-arch.h +428 -0
  248. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-batch.h +88 -0
  249. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-chat.h +56 -0
  250. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-context.h +265 -0
  251. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cparams.h +38 -0
  252. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-cpp.h +30 -0
  253. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-grammar.h +173 -0
  254. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-graph.h +592 -0
  255. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-hparams.h +156 -0
  256. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-impl.h +61 -0
  257. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-io.h +35 -0
  258. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-kv-cache.h +213 -0
  259. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-memory.h +21 -0
  260. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-mmap.h +68 -0
  261. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model-loader.h +169 -0
  262. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-model.h +409 -0
  263. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-sampling.h +32 -0
  264. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama-vocab.h +125 -0
  265. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/llama.h +1434 -0
  266. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/log.h +132 -0
  267. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/minja/chat-template.hpp +537 -0
  268. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/minja/minja.hpp +2941 -0
  269. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/ops.h +128 -0
  270. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/rn-llama.h +138 -0
  271. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/sampling.h +107 -0
  272. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/sgemm.h +14 -0
  273. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/simd-mappings.h +888 -0
  274. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/speculative.h +28 -0
  275. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/unary-ops.h +28 -0
  276. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/unicode-data.h +20 -0
  277. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/unicode.h +66 -0
  278. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Headers/vec.h +802 -0
  279. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/Info.plist +0 -0
  280. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/_CodeSignature/CodeResources +101 -0
  281. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/ggml-llama-sim.metallib +0 -0
  282. package/ios/rnllama.xcframework/tvos-arm64_x86_64-simulator/rnllama.framework/rnllama +0 -0
  283. package/jest/mock.js +203 -203
  284. package/lib/commonjs/NativeRNLlama.js +1 -2
  285. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  286. package/lib/commonjs/chat.js.map +1 -1
  287. package/lib/commonjs/grammar.js +12 -31
  288. package/lib/commonjs/grammar.js.map +1 -1
  289. package/lib/commonjs/index.js +47 -47
  290. package/lib/commonjs/index.js.map +1 -1
  291. package/lib/commonjs/package.json +1 -0
  292. package/lib/module/NativeRNLlama.js +2 -0
  293. package/lib/module/NativeRNLlama.js.map +1 -1
  294. package/lib/module/chat.js +2 -0
  295. package/lib/module/chat.js.map +1 -1
  296. package/lib/module/grammar.js +14 -31
  297. package/lib/module/grammar.js.map +1 -1
  298. package/lib/module/index.js +47 -45
  299. package/lib/module/index.js.map +1 -1
  300. package/lib/module/package.json +1 -0
  301. package/lib/typescript/NativeRNLlama.d.ts +6 -4
  302. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  303. package/lib/typescript/index.d.ts.map +1 -1
  304. package/llama-rn.podspec +48 -48
  305. package/package.json +233 -233
  306. package/src/NativeRNLlama.ts +426 -426
  307. package/src/chat.ts +44 -44
  308. package/src/grammar.ts +854 -854
  309. package/src/index.ts +495 -487
@@ -0,0 +1,2222 @@
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,
398
+ LM_GGML_PREC_F32,
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_TRANSPOSE_2D,
486
+ LM_GGML_OP_POOL_1D,
487
+ LM_GGML_OP_POOL_2D,
488
+ LM_GGML_OP_POOL_2D_BACK,
489
+ LM_GGML_OP_UPSCALE, // nearest interpolate
490
+ LM_GGML_OP_PAD,
491
+ LM_GGML_OP_PAD_REFLECT_1D,
492
+ LM_GGML_OP_ARANGE,
493
+ LM_GGML_OP_TIMESTEP_EMBEDDING,
494
+ LM_GGML_OP_ARGSORT,
495
+ LM_GGML_OP_LEAKY_RELU,
496
+
497
+ LM_GGML_OP_FLASH_ATTN_EXT,
498
+ LM_GGML_OP_FLASH_ATTN_BACK,
499
+ LM_GGML_OP_SSM_CONV,
500
+ LM_GGML_OP_SSM_SCAN,
501
+ LM_GGML_OP_WIN_PART,
502
+ LM_GGML_OP_WIN_UNPART,
503
+ LM_GGML_OP_GET_REL_POS,
504
+ LM_GGML_OP_ADD_REL_POS,
505
+ LM_GGML_OP_RWKV_WKV6,
506
+ LM_GGML_OP_GATED_LINEAR_ATTN,
507
+ LM_GGML_OP_RWKV_WKV7,
508
+
509
+ LM_GGML_OP_UNARY,
510
+
511
+ LM_GGML_OP_MAP_UNARY,
512
+ LM_GGML_OP_MAP_BINARY,
513
+
514
+ LM_GGML_OP_MAP_CUSTOM1_F32,
515
+ LM_GGML_OP_MAP_CUSTOM2_F32,
516
+ LM_GGML_OP_MAP_CUSTOM3_F32,
517
+
518
+ LM_GGML_OP_MAP_CUSTOM1,
519
+ LM_GGML_OP_MAP_CUSTOM2,
520
+ LM_GGML_OP_MAP_CUSTOM3,
521
+
522
+ LM_GGML_OP_CROSS_ENTROPY_LOSS,
523
+ LM_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
524
+ LM_GGML_OP_OPT_STEP_ADAMW,
525
+
526
+ LM_GGML_OP_COUNT,
527
+ };
528
+
529
+ enum lm_ggml_unary_op {
530
+ LM_GGML_UNARY_OP_ABS,
531
+ LM_GGML_UNARY_OP_SGN,
532
+ LM_GGML_UNARY_OP_NEG,
533
+ LM_GGML_UNARY_OP_STEP,
534
+ LM_GGML_UNARY_OP_TANH,
535
+ LM_GGML_UNARY_OP_ELU,
536
+ LM_GGML_UNARY_OP_RELU,
537
+ LM_GGML_UNARY_OP_SIGMOID,
538
+ LM_GGML_UNARY_OP_GELU,
539
+ LM_GGML_UNARY_OP_GELU_QUICK,
540
+ LM_GGML_UNARY_OP_SILU,
541
+ LM_GGML_UNARY_OP_HARDSWISH,
542
+ LM_GGML_UNARY_OP_HARDSIGMOID,
543
+ LM_GGML_UNARY_OP_EXP,
544
+
545
+ LM_GGML_UNARY_OP_COUNT,
546
+ };
547
+
548
+ enum lm_ggml_object_type {
549
+ LM_GGML_OBJECT_TYPE_TENSOR,
550
+ LM_GGML_OBJECT_TYPE_GRAPH,
551
+ LM_GGML_OBJECT_TYPE_WORK_BUFFER
552
+ };
553
+
554
+ enum lm_ggml_log_level {
555
+ LM_GGML_LOG_LEVEL_NONE = 0,
556
+ LM_GGML_LOG_LEVEL_DEBUG = 1,
557
+ LM_GGML_LOG_LEVEL_INFO = 2,
558
+ LM_GGML_LOG_LEVEL_WARN = 3,
559
+ LM_GGML_LOG_LEVEL_ERROR = 4,
560
+ LM_GGML_LOG_LEVEL_CONT = 5, // continue previous log
561
+ };
562
+
563
+ // this tensor...
564
+ enum lm_ggml_tensor_flag {
565
+ LM_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
566
+ LM_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
567
+ LM_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
568
+ LM_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
569
+ };
570
+
571
+ struct lm_ggml_init_params {
572
+ // memory pool
573
+ size_t mem_size; // bytes
574
+ void * mem_buffer; // if NULL, memory will be allocated internally
575
+ bool no_alloc; // don't allocate memory for the tensor data
576
+ };
577
+
578
+ // n-dimensional tensor
579
+ struct lm_ggml_tensor {
580
+ enum lm_ggml_type type;
581
+
582
+ struct lm_ggml_backend_buffer * buffer;
583
+
584
+ int64_t ne[LM_GGML_MAX_DIMS]; // number of elements
585
+ size_t nb[LM_GGML_MAX_DIMS]; // stride in bytes:
586
+ // nb[0] = lm_ggml_type_size(type)
587
+ // nb[1] = nb[0] * (ne[0] / lm_ggml_blck_size(type)) + padding
588
+ // nb[i] = nb[i-1] * ne[i-1]
589
+
590
+ // compute data
591
+ enum lm_ggml_op op;
592
+
593
+ // op params - allocated as int32_t for alignment
594
+ int32_t op_params[LM_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
595
+
596
+ int32_t flags;
597
+
598
+ struct lm_ggml_tensor * src[LM_GGML_MAX_SRC];
599
+
600
+ // source tensor and offset for views
601
+ struct lm_ggml_tensor * view_src;
602
+ size_t view_offs;
603
+
604
+ void * data;
605
+
606
+ char name[LM_GGML_MAX_NAME];
607
+
608
+ void * extra; // extra things e.g. for ggml-cuda.cu
609
+
610
+ char padding[8];
611
+ };
612
+
613
+ static const size_t LM_GGML_TENSOR_SIZE = sizeof(struct lm_ggml_tensor);
614
+
615
+ // Abort callback
616
+ // If not NULL, called before ggml computation
617
+ // If it returns true, the computation is aborted
618
+ typedef bool (*lm_ggml_abort_callback)(void * data);
619
+
620
+
621
+ //
622
+ // GUID
623
+ //
624
+
625
+ // GUID types
626
+ typedef uint8_t lm_ggml_guid[16];
627
+ typedef lm_ggml_guid * lm_ggml_guid_t;
628
+
629
+ LM_GGML_API bool lm_ggml_guid_matches(lm_ggml_guid_t guid_a, lm_ggml_guid_t guid_b);
630
+
631
+ // misc
632
+
633
+ LM_GGML_API void lm_ggml_time_init(void); // call this once at the beginning of the program
634
+ LM_GGML_API int64_t lm_ggml_time_ms(void);
635
+ LM_GGML_API int64_t lm_ggml_time_us(void);
636
+ LM_GGML_API int64_t lm_ggml_cycles(void);
637
+ LM_GGML_API int64_t lm_ggml_cycles_per_ms(void);
638
+
639
+ // accepts a UTF-8 path, even on Windows
640
+ LM_GGML_API FILE * lm_ggml_fopen(const char * fname, const char * mode);
641
+
642
+ LM_GGML_API void lm_ggml_print_object (const struct lm_ggml_object * obj);
643
+ LM_GGML_API void lm_ggml_print_objects(const struct lm_ggml_context * ctx);
644
+
645
+ LM_GGML_API int64_t lm_ggml_nelements (const struct lm_ggml_tensor * tensor);
646
+ LM_GGML_API int64_t lm_ggml_nrows (const struct lm_ggml_tensor * tensor);
647
+ LM_GGML_API size_t lm_ggml_nbytes (const struct lm_ggml_tensor * tensor);
648
+ 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
649
+
650
+ LM_GGML_API int64_t lm_ggml_blck_size(enum lm_ggml_type type);
651
+ LM_GGML_API size_t lm_ggml_type_size(enum lm_ggml_type type); // size in bytes for all elements in a block
652
+ 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
653
+
654
+ LM_GGML_DEPRECATED(
655
+ LM_GGML_API double lm_ggml_type_sizef(enum lm_ggml_type type), // lm_ggml_type_size()/lm_ggml_blck_size() as float
656
+ "use lm_ggml_row_size() instead");
657
+
658
+ LM_GGML_API const char * lm_ggml_type_name(enum lm_ggml_type type);
659
+ LM_GGML_API const char * lm_ggml_op_name (enum lm_ggml_op op);
660
+ LM_GGML_API const char * lm_ggml_op_symbol(enum lm_ggml_op op);
661
+
662
+ LM_GGML_API const char * lm_ggml_unary_op_name(enum lm_ggml_unary_op op);
663
+ LM_GGML_API const char * lm_ggml_op_desc(const struct lm_ggml_tensor * t); // unary or op name
664
+
665
+ LM_GGML_API size_t lm_ggml_element_size(const struct lm_ggml_tensor * tensor);
666
+
667
+ LM_GGML_API bool lm_ggml_is_quantized(enum lm_ggml_type type);
668
+
669
+ // TODO: temporary until model loading of ggml examples is refactored
670
+ LM_GGML_API enum lm_ggml_type lm_ggml_ftype_to_lm_ggml_type(enum lm_ggml_ftype ftype);
671
+
672
+ LM_GGML_API bool lm_ggml_is_transposed(const struct lm_ggml_tensor * tensor);
673
+ LM_GGML_API bool lm_ggml_is_permuted (const struct lm_ggml_tensor * tensor);
674
+ LM_GGML_API bool lm_ggml_is_empty (const struct lm_ggml_tensor * tensor);
675
+ LM_GGML_API bool lm_ggml_is_scalar (const struct lm_ggml_tensor * tensor);
676
+ LM_GGML_API bool lm_ggml_is_vector (const struct lm_ggml_tensor * tensor);
677
+ LM_GGML_API bool lm_ggml_is_matrix (const struct lm_ggml_tensor * tensor);
678
+ LM_GGML_API bool lm_ggml_is_3d (const struct lm_ggml_tensor * tensor);
679
+ LM_GGML_API int lm_ggml_n_dims (const struct lm_ggml_tensor * tensor); // returns 1 for scalars
680
+
681
+ LM_GGML_API bool lm_ggml_is_contiguous (const struct lm_ggml_tensor * tensor);
682
+ LM_GGML_API bool lm_ggml_is_contiguous_0(const struct lm_ggml_tensor * tensor); // same as lm_ggml_is_contiguous()
683
+ LM_GGML_API bool lm_ggml_is_contiguous_1(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 1
684
+ LM_GGML_API bool lm_ggml_is_contiguous_2(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 2
685
+
686
+ LM_GGML_API bool lm_ggml_are_same_shape (const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
687
+ LM_GGML_API bool lm_ggml_are_same_stride(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
688
+
689
+ LM_GGML_API bool lm_ggml_can_repeat(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
690
+
691
+ // use this to compute the memory overhead of a tensor
692
+ LM_GGML_API size_t lm_ggml_tensor_overhead(void);
693
+
694
+ LM_GGML_API bool lm_ggml_validate_row_data(enum lm_ggml_type type, const void * data, size_t nbytes);
695
+
696
+ // main
697
+
698
+ LM_GGML_API struct lm_ggml_context * lm_ggml_init (struct lm_ggml_init_params params);
699
+ LM_GGML_API void lm_ggml_reset(struct lm_ggml_context * ctx);
700
+ LM_GGML_API void lm_ggml_free (struct lm_ggml_context * ctx);
701
+
702
+ LM_GGML_API size_t lm_ggml_used_mem(const struct lm_ggml_context * ctx);
703
+
704
+ LM_GGML_API bool lm_ggml_get_no_alloc(struct lm_ggml_context * ctx);
705
+ LM_GGML_API void lm_ggml_set_no_alloc(struct lm_ggml_context * ctx, bool no_alloc);
706
+
707
+ LM_GGML_API void * lm_ggml_get_mem_buffer (const struct lm_ggml_context * ctx);
708
+ LM_GGML_API size_t lm_ggml_get_mem_size (const struct lm_ggml_context * ctx);
709
+ LM_GGML_API size_t lm_ggml_get_max_tensor_size(const struct lm_ggml_context * ctx);
710
+
711
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor(
712
+ struct lm_ggml_context * ctx,
713
+ enum lm_ggml_type type,
714
+ int n_dims,
715
+ const int64_t *ne);
716
+
717
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_1d(
718
+ struct lm_ggml_context * ctx,
719
+ enum lm_ggml_type type,
720
+ int64_t ne0);
721
+
722
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_2d(
723
+ struct lm_ggml_context * ctx,
724
+ enum lm_ggml_type type,
725
+ int64_t ne0,
726
+ int64_t ne1);
727
+
728
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_3d(
729
+ struct lm_ggml_context * ctx,
730
+ enum lm_ggml_type type,
731
+ int64_t ne0,
732
+ int64_t ne1,
733
+ int64_t ne2);
734
+
735
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_4d(
736
+ struct lm_ggml_context * ctx,
737
+ enum lm_ggml_type type,
738
+ int64_t ne0,
739
+ int64_t ne1,
740
+ int64_t ne2,
741
+ int64_t ne3);
742
+
743
+ LM_GGML_API void * lm_ggml_new_buffer(struct lm_ggml_context * ctx, size_t nbytes);
744
+
745
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_tensor (struct lm_ggml_context * ctx, const struct lm_ggml_tensor * src);
746
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_tensor(struct lm_ggml_context * ctx, struct lm_ggml_tensor * src);
747
+
748
+ // Context tensor enumeration and lookup
749
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_first_tensor(const struct lm_ggml_context * ctx);
750
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_next_tensor (const struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
751
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_tensor(struct lm_ggml_context * ctx, const char * name);
752
+
753
+ // Converts a flat index into coordinates
754
+ 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);
755
+
756
+ LM_GGML_API enum lm_ggml_unary_op lm_ggml_get_unary_op(const struct lm_ggml_tensor * tensor);
757
+
758
+ LM_GGML_API void * lm_ggml_get_data (const struct lm_ggml_tensor * tensor);
759
+ LM_GGML_API float * lm_ggml_get_data_f32(const struct lm_ggml_tensor * tensor);
760
+
761
+ LM_GGML_API const char * lm_ggml_get_name (const struct lm_ggml_tensor * tensor);
762
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_name ( struct lm_ggml_tensor * tensor, const char * name);
763
+ LM_GGML_ATTRIBUTE_FORMAT(2, 3)
764
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_format_name( struct lm_ggml_tensor * tensor, const char * fmt, ...);
765
+
766
+ // Tensor flags
767
+ LM_GGML_API void lm_ggml_set_input(struct lm_ggml_tensor * tensor);
768
+ LM_GGML_API void lm_ggml_set_output(struct lm_ggml_tensor * tensor);
769
+ LM_GGML_API void lm_ggml_set_param(struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
770
+ LM_GGML_API void lm_ggml_set_loss(struct lm_ggml_tensor * tensor);
771
+
772
+ //
773
+ // operations on tensors with backpropagation
774
+ //
775
+
776
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup(
777
+ struct lm_ggml_context * ctx,
778
+ struct lm_ggml_tensor * a);
779
+
780
+ // in-place, returns view(a)
781
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_inplace(
782
+ struct lm_ggml_context * ctx,
783
+ struct lm_ggml_tensor * a);
784
+
785
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add(
786
+ struct lm_ggml_context * ctx,
787
+ struct lm_ggml_tensor * a,
788
+ struct lm_ggml_tensor * b);
789
+
790
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_inplace(
791
+ struct lm_ggml_context * ctx,
792
+ struct lm_ggml_tensor * a,
793
+ struct lm_ggml_tensor * b);
794
+
795
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_cast(
796
+ struct lm_ggml_context * ctx,
797
+ struct lm_ggml_tensor * a,
798
+ struct lm_ggml_tensor * b,
799
+ enum lm_ggml_type type);
800
+
801
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1(
802
+ struct lm_ggml_context * ctx,
803
+ struct lm_ggml_tensor * a,
804
+ struct lm_ggml_tensor * b);
805
+
806
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1_inplace(
807
+ struct lm_ggml_context * ctx,
808
+ struct lm_ggml_tensor * a,
809
+ struct lm_ggml_tensor * b);
810
+
811
+ // dst = a
812
+ // view(dst, nb1, nb2, nb3, offset) += b
813
+ // return dst
814
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc(
815
+ struct lm_ggml_context * ctx,
816
+ struct lm_ggml_tensor * a,
817
+ struct lm_ggml_tensor * b,
818
+ size_t nb1,
819
+ size_t nb2,
820
+ size_t nb3,
821
+ size_t offset);
822
+
823
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc_inplace(
824
+ struct lm_ggml_context * ctx,
825
+ struct lm_ggml_tensor * a,
826
+ struct lm_ggml_tensor * b,
827
+ size_t nb1,
828
+ size_t nb2,
829
+ size_t nb3,
830
+ size_t offset);
831
+
832
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub(
833
+ struct lm_ggml_context * ctx,
834
+ struct lm_ggml_tensor * a,
835
+ struct lm_ggml_tensor * b);
836
+
837
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub_inplace(
838
+ struct lm_ggml_context * ctx,
839
+ struct lm_ggml_tensor * a,
840
+ struct lm_ggml_tensor * b);
841
+
842
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul(
843
+ struct lm_ggml_context * ctx,
844
+ struct lm_ggml_tensor * a,
845
+ struct lm_ggml_tensor * b);
846
+
847
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_inplace(
848
+ struct lm_ggml_context * ctx,
849
+ struct lm_ggml_tensor * a,
850
+ struct lm_ggml_tensor * b);
851
+
852
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div(
853
+ struct lm_ggml_context * ctx,
854
+ struct lm_ggml_tensor * a,
855
+ struct lm_ggml_tensor * b);
856
+
857
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div_inplace(
858
+ struct lm_ggml_context * ctx,
859
+ struct lm_ggml_tensor * a,
860
+ struct lm_ggml_tensor * b);
861
+
862
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr(
863
+ struct lm_ggml_context * ctx,
864
+ struct lm_ggml_tensor * a);
865
+
866
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr_inplace(
867
+ struct lm_ggml_context * ctx,
868
+ struct lm_ggml_tensor * a);
869
+
870
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt(
871
+ struct lm_ggml_context * ctx,
872
+ struct lm_ggml_tensor * a);
873
+
874
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt_inplace(
875
+ struct lm_ggml_context * ctx,
876
+ struct lm_ggml_tensor * a);
877
+
878
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log(
879
+ struct lm_ggml_context * ctx,
880
+ struct lm_ggml_tensor * a);
881
+
882
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log_inplace(
883
+ struct lm_ggml_context * ctx,
884
+ struct lm_ggml_tensor * a);
885
+
886
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin(
887
+ struct lm_ggml_context * ctx,
888
+ struct lm_ggml_tensor * a);
889
+
890
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin_inplace(
891
+ struct lm_ggml_context * ctx,
892
+ struct lm_ggml_tensor * a);
893
+
894
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos(
895
+ struct lm_ggml_context * ctx,
896
+ struct lm_ggml_tensor * a);
897
+
898
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos_inplace(
899
+ struct lm_ggml_context * ctx,
900
+ struct lm_ggml_tensor * a);
901
+
902
+ // return scalar
903
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum(
904
+ struct lm_ggml_context * ctx,
905
+ struct lm_ggml_tensor * a);
906
+
907
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
908
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum_rows(
909
+ struct lm_ggml_context * ctx,
910
+ struct lm_ggml_tensor * a);
911
+
912
+ // mean along rows
913
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mean(
914
+ struct lm_ggml_context * ctx,
915
+ struct lm_ggml_tensor * a);
916
+
917
+ // argmax along rows
918
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argmax(
919
+ struct lm_ggml_context * ctx,
920
+ struct lm_ggml_tensor * a);
921
+
922
+ // count number of equal elements in a and b
923
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_count_equal(
924
+ struct lm_ggml_context * ctx,
925
+ struct lm_ggml_tensor * a,
926
+ struct lm_ggml_tensor * b);
927
+
928
+ // if a is the same shape as b, and a is not parameter, return a
929
+ // otherwise, return a new tensor: repeat(a) to fit in b
930
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat(
931
+ struct lm_ggml_context * ctx,
932
+ struct lm_ggml_tensor * a,
933
+ struct lm_ggml_tensor * b);
934
+
935
+ // sums repetitions in a into shape of b
936
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_back(
937
+ struct lm_ggml_context * ctx,
938
+ struct lm_ggml_tensor * a,
939
+ struct lm_ggml_tensor * b);
940
+
941
+ // concat a and b along dim
942
+ // used in stable-diffusion
943
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_concat(
944
+ struct lm_ggml_context * ctx,
945
+ struct lm_ggml_tensor * a,
946
+ struct lm_ggml_tensor * b,
947
+ int dim);
948
+
949
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs(
950
+ struct lm_ggml_context * ctx,
951
+ struct lm_ggml_tensor * a);
952
+
953
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs_inplace(
954
+ struct lm_ggml_context * ctx,
955
+ struct lm_ggml_tensor * a);
956
+
957
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn(
958
+ struct lm_ggml_context * ctx,
959
+ struct lm_ggml_tensor * a);
960
+
961
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn_inplace(
962
+ struct lm_ggml_context * ctx,
963
+ struct lm_ggml_tensor * a);
964
+
965
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg(
966
+ struct lm_ggml_context * ctx,
967
+ struct lm_ggml_tensor * a);
968
+
969
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg_inplace(
970
+ struct lm_ggml_context * ctx,
971
+ struct lm_ggml_tensor * a);
972
+
973
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step(
974
+ struct lm_ggml_context * ctx,
975
+ struct lm_ggml_tensor * a);
976
+
977
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step_inplace(
978
+ struct lm_ggml_context * ctx,
979
+ struct lm_ggml_tensor * a);
980
+
981
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh(
982
+ struct lm_ggml_context * ctx,
983
+ struct lm_ggml_tensor * a);
984
+
985
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh_inplace(
986
+ struct lm_ggml_context * ctx,
987
+ struct lm_ggml_tensor * a);
988
+
989
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu(
990
+ struct lm_ggml_context * ctx,
991
+ struct lm_ggml_tensor * a);
992
+
993
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu_inplace(
994
+ struct lm_ggml_context * ctx,
995
+ struct lm_ggml_tensor * a);
996
+
997
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu(
998
+ struct lm_ggml_context * ctx,
999
+ struct lm_ggml_tensor * a);
1000
+
1001
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_leaky_relu(
1002
+ struct lm_ggml_context * ctx,
1003
+ struct lm_ggml_tensor * a, float negative_slope, bool inplace);
1004
+
1005
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu_inplace(
1006
+ struct lm_ggml_context * ctx,
1007
+ struct lm_ggml_tensor * a);
1008
+
1009
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid(
1010
+ struct lm_ggml_context * ctx,
1011
+ struct lm_ggml_tensor * a);
1012
+
1013
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid_inplace(
1014
+ struct lm_ggml_context * ctx,
1015
+ struct lm_ggml_tensor * a);
1016
+
1017
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu(
1018
+ struct lm_ggml_context * ctx,
1019
+ struct lm_ggml_tensor * a);
1020
+
1021
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_inplace(
1022
+ struct lm_ggml_context * ctx,
1023
+ struct lm_ggml_tensor * a);
1024
+
1025
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick(
1026
+ struct lm_ggml_context * ctx,
1027
+ struct lm_ggml_tensor * a);
1028
+
1029
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick_inplace(
1030
+ struct lm_ggml_context * ctx,
1031
+ struct lm_ggml_tensor * a);
1032
+
1033
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu(
1034
+ struct lm_ggml_context * ctx,
1035
+ struct lm_ggml_tensor * a);
1036
+
1037
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_inplace(
1038
+ struct lm_ggml_context * ctx,
1039
+ struct lm_ggml_tensor * a);
1040
+
1041
+ // a - x
1042
+ // b - dy
1043
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_back(
1044
+ struct lm_ggml_context * ctx,
1045
+ struct lm_ggml_tensor * a,
1046
+ struct lm_ggml_tensor * b);
1047
+
1048
+ // hardswish(x) = x * relu6(x + 3) / 6
1049
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardswish(
1050
+ struct lm_ggml_context * ctx,
1051
+ struct lm_ggml_tensor * a);
1052
+
1053
+ // hardsigmoid(x) = relu6(x + 3) / 6
1054
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardsigmoid(
1055
+ struct lm_ggml_context * ctx,
1056
+ struct lm_ggml_tensor * a);
1057
+
1058
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp(
1059
+ struct lm_ggml_context * ctx,
1060
+ struct lm_ggml_tensor * a);
1061
+
1062
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp_inplace(
1063
+ struct lm_ggml_context * ctx,
1064
+ struct lm_ggml_tensor * a);
1065
+
1066
+ // normalize along rows
1067
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm(
1068
+ struct lm_ggml_context * ctx,
1069
+ struct lm_ggml_tensor * a,
1070
+ float eps);
1071
+
1072
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm_inplace(
1073
+ struct lm_ggml_context * ctx,
1074
+ struct lm_ggml_tensor * a,
1075
+ float eps);
1076
+
1077
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm(
1078
+ struct lm_ggml_context * ctx,
1079
+ struct lm_ggml_tensor * a,
1080
+ float eps);
1081
+
1082
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_inplace(
1083
+ struct lm_ggml_context * ctx,
1084
+ struct lm_ggml_tensor * a,
1085
+ float eps);
1086
+
1087
+ // group normalize along ne0*ne1*n_groups
1088
+ // used in stable-diffusion
1089
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm(
1090
+ struct lm_ggml_context * ctx,
1091
+ struct lm_ggml_tensor * a,
1092
+ int n_groups,
1093
+ float eps);
1094
+
1095
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm_inplace(
1096
+ struct lm_ggml_context * ctx,
1097
+ struct lm_ggml_tensor * a,
1098
+ int n_groups,
1099
+ float eps);
1100
+
1101
+ // l2 normalize along rows
1102
+ // used in rwkv v7
1103
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm(
1104
+ struct lm_ggml_context * ctx,
1105
+ struct lm_ggml_tensor * a,
1106
+ float eps);
1107
+
1108
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm_inplace(
1109
+ struct lm_ggml_context * ctx,
1110
+ struct lm_ggml_tensor * a,
1111
+ float eps);
1112
+
1113
+ // a - x
1114
+ // b - dy
1115
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_back(
1116
+ struct lm_ggml_context * ctx,
1117
+ struct lm_ggml_tensor * a,
1118
+ struct lm_ggml_tensor * b,
1119
+ float eps);
1120
+
1121
+ // A: k columns, n rows => [ne03, ne02, n, k]
1122
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1123
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1124
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat(
1125
+ struct lm_ggml_context * ctx,
1126
+ struct lm_ggml_tensor * a,
1127
+ struct lm_ggml_tensor * b);
1128
+
1129
+ // change the precision of a matrix multiplication
1130
+ // set to LM_GGML_PREC_F32 for higher precision (useful for phi-2)
1131
+ LM_GGML_API void lm_ggml_mul_mat_set_prec(
1132
+ struct lm_ggml_tensor * a,
1133
+ enum lm_ggml_prec prec);
1134
+
1135
+ // indirect matrix multiplication
1136
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat_id(
1137
+ struct lm_ggml_context * ctx,
1138
+ struct lm_ggml_tensor * as,
1139
+ struct lm_ggml_tensor * b,
1140
+ struct lm_ggml_tensor * ids);
1141
+
1142
+ // A: m columns, n rows,
1143
+ // B: p columns, n rows,
1144
+ // result is m columns, p rows
1145
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_out_prod(
1146
+ struct lm_ggml_context * ctx,
1147
+ struct lm_ggml_tensor * a,
1148
+ struct lm_ggml_tensor * b);
1149
+
1150
+ //
1151
+ // operations on tensors without backpropagation
1152
+ //
1153
+
1154
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale(
1155
+ struct lm_ggml_context * ctx,
1156
+ struct lm_ggml_tensor * a,
1157
+ float s);
1158
+
1159
+ // in-place, returns view(a)
1160
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_inplace(
1161
+ struct lm_ggml_context * ctx,
1162
+ struct lm_ggml_tensor * a,
1163
+ float s);
1164
+
1165
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1166
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set(
1167
+ struct lm_ggml_context * ctx,
1168
+ struct lm_ggml_tensor * a,
1169
+ struct lm_ggml_tensor * b,
1170
+ size_t nb1,
1171
+ size_t nb2,
1172
+ size_t nb3,
1173
+ size_t offset); // in bytes
1174
+
1175
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1176
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_inplace(
1177
+ struct lm_ggml_context * ctx,
1178
+ struct lm_ggml_tensor * a,
1179
+ struct lm_ggml_tensor * b,
1180
+ size_t nb1,
1181
+ size_t nb2,
1182
+ size_t nb3,
1183
+ size_t offset); // in bytes
1184
+
1185
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d(
1186
+ struct lm_ggml_context * ctx,
1187
+ struct lm_ggml_tensor * a,
1188
+ struct lm_ggml_tensor * b,
1189
+ size_t offset); // in bytes
1190
+
1191
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d_inplace(
1192
+ struct lm_ggml_context * ctx,
1193
+ struct lm_ggml_tensor * a,
1194
+ struct lm_ggml_tensor * b,
1195
+ size_t offset); // in bytes
1196
+
1197
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1198
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d(
1199
+ struct lm_ggml_context * ctx,
1200
+ struct lm_ggml_tensor * a,
1201
+ struct lm_ggml_tensor * b,
1202
+ size_t nb1,
1203
+ size_t offset); // in bytes
1204
+
1205
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1206
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d_inplace(
1207
+ struct lm_ggml_context * ctx,
1208
+ struct lm_ggml_tensor * a,
1209
+ struct lm_ggml_tensor * b,
1210
+ size_t nb1,
1211
+ size_t offset); // in bytes
1212
+
1213
+ // a -> b, return view(b)
1214
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cpy(
1215
+ struct lm_ggml_context * ctx,
1216
+ struct lm_ggml_tensor * a,
1217
+ struct lm_ggml_tensor * b);
1218
+
1219
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cast(
1220
+ struct lm_ggml_context * ctx,
1221
+ struct lm_ggml_tensor * a,
1222
+ enum lm_ggml_type type);
1223
+
1224
+ // make contiguous
1225
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont(
1226
+ struct lm_ggml_context * ctx,
1227
+ struct lm_ggml_tensor * a);
1228
+
1229
+ // make contiguous, with new shape
1230
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_1d(
1231
+ struct lm_ggml_context * ctx,
1232
+ struct lm_ggml_tensor * a,
1233
+ int64_t ne0);
1234
+
1235
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_2d(
1236
+ struct lm_ggml_context * ctx,
1237
+ struct lm_ggml_tensor * a,
1238
+ int64_t ne0,
1239
+ int64_t ne1);
1240
+
1241
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_3d(
1242
+ struct lm_ggml_context * ctx,
1243
+ struct lm_ggml_tensor * a,
1244
+ int64_t ne0,
1245
+ int64_t ne1,
1246
+ int64_t ne2);
1247
+
1248
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_4d(
1249
+ struct lm_ggml_context * ctx,
1250
+ struct lm_ggml_tensor * a,
1251
+ int64_t ne0,
1252
+ int64_t ne1,
1253
+ int64_t ne2,
1254
+ int64_t ne3);
1255
+
1256
+ // return view(a), b specifies the new shape
1257
+ // TODO: when we start computing gradient, make a copy instead of view
1258
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape(
1259
+ struct lm_ggml_context * ctx,
1260
+ struct lm_ggml_tensor * a,
1261
+ struct lm_ggml_tensor * b);
1262
+
1263
+ // return view(a)
1264
+ // TODO: when we start computing gradient, make a copy instead of view
1265
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_1d(
1266
+ struct lm_ggml_context * ctx,
1267
+ struct lm_ggml_tensor * a,
1268
+ int64_t ne0);
1269
+
1270
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_2d(
1271
+ struct lm_ggml_context * ctx,
1272
+ struct lm_ggml_tensor * a,
1273
+ int64_t ne0,
1274
+ int64_t ne1);
1275
+
1276
+ // return view(a)
1277
+ // TODO: when we start computing gradient, make a copy instead of view
1278
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_3d(
1279
+ struct lm_ggml_context * ctx,
1280
+ struct lm_ggml_tensor * a,
1281
+ int64_t ne0,
1282
+ int64_t ne1,
1283
+ int64_t ne2);
1284
+
1285
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_4d(
1286
+ struct lm_ggml_context * ctx,
1287
+ struct lm_ggml_tensor * a,
1288
+ int64_t ne0,
1289
+ int64_t ne1,
1290
+ int64_t ne2,
1291
+ int64_t ne3);
1292
+
1293
+ // offset in bytes
1294
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_1d(
1295
+ struct lm_ggml_context * ctx,
1296
+ struct lm_ggml_tensor * a,
1297
+ int64_t ne0,
1298
+ size_t offset);
1299
+
1300
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_2d(
1301
+ struct lm_ggml_context * ctx,
1302
+ struct lm_ggml_tensor * a,
1303
+ int64_t ne0,
1304
+ int64_t ne1,
1305
+ size_t nb1, // row stride in bytes
1306
+ size_t offset);
1307
+
1308
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_3d(
1309
+ struct lm_ggml_context * ctx,
1310
+ struct lm_ggml_tensor * a,
1311
+ int64_t ne0,
1312
+ int64_t ne1,
1313
+ int64_t ne2,
1314
+ size_t nb1, // row stride in bytes
1315
+ size_t nb2, // slice stride in bytes
1316
+ size_t offset);
1317
+
1318
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_4d(
1319
+ struct lm_ggml_context * ctx,
1320
+ struct lm_ggml_tensor * a,
1321
+ int64_t ne0,
1322
+ int64_t ne1,
1323
+ int64_t ne2,
1324
+ int64_t ne3,
1325
+ size_t nb1, // row stride in bytes
1326
+ size_t nb2, // slice stride in bytes
1327
+ size_t nb3,
1328
+ size_t offset);
1329
+
1330
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_permute(
1331
+ struct lm_ggml_context * ctx,
1332
+ struct lm_ggml_tensor * a,
1333
+ int axis0,
1334
+ int axis1,
1335
+ int axis2,
1336
+ int axis3);
1337
+
1338
+ // alias for lm_ggml_permute(ctx, a, 1, 0, 2, 3)
1339
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_transpose(
1340
+ struct lm_ggml_context * ctx,
1341
+ struct lm_ggml_tensor * a);
1342
+
1343
+ // supports 3D: a->ne[2] == b->ne[1]
1344
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows(
1345
+ struct lm_ggml_context * ctx,
1346
+ struct lm_ggml_tensor * a, // data
1347
+ struct lm_ggml_tensor * b); // row indices
1348
+
1349
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows_back(
1350
+ struct lm_ggml_context * ctx,
1351
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_get_rows result
1352
+ struct lm_ggml_tensor * b, // row indices
1353
+ struct lm_ggml_tensor * c); // data for lm_ggml_get_rows, only used for its shape
1354
+
1355
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag(
1356
+ struct lm_ggml_context * ctx,
1357
+ struct lm_ggml_tensor * a);
1358
+
1359
+ // set elements above the diagonal to -INF
1360
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf(
1361
+ struct lm_ggml_context * ctx,
1362
+ struct lm_ggml_tensor * a,
1363
+ int n_past);
1364
+
1365
+ // in-place, returns view(a)
1366
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf_inplace(
1367
+ struct lm_ggml_context * ctx,
1368
+ struct lm_ggml_tensor * a,
1369
+ int n_past);
1370
+
1371
+ // set elements above the diagonal to 0
1372
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero(
1373
+ struct lm_ggml_context * ctx,
1374
+ struct lm_ggml_tensor * a,
1375
+ int n_past);
1376
+
1377
+ // in-place, returns view(a)
1378
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero_inplace(
1379
+ struct lm_ggml_context * ctx,
1380
+ struct lm_ggml_tensor * a,
1381
+ int n_past);
1382
+
1383
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max(
1384
+ struct lm_ggml_context * ctx,
1385
+ struct lm_ggml_tensor * a);
1386
+
1387
+ // in-place, returns view(a)
1388
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_inplace(
1389
+ struct lm_ggml_context * ctx,
1390
+ struct lm_ggml_tensor * a);
1391
+
1392
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1393
+ // mask is optional
1394
+ // max_bias = 0.0f for no ALiBi
1395
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext(
1396
+ struct lm_ggml_context * ctx,
1397
+ struct lm_ggml_tensor * a,
1398
+ struct lm_ggml_tensor * mask,
1399
+ float scale,
1400
+ float max_bias);
1401
+
1402
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back(
1403
+ struct lm_ggml_context * ctx,
1404
+ struct lm_ggml_tensor * a,
1405
+ struct lm_ggml_tensor * b,
1406
+ float scale,
1407
+ float max_bias);
1408
+
1409
+ // in-place, returns view(a)
1410
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back_inplace(
1411
+ struct lm_ggml_context * ctx,
1412
+ struct lm_ggml_tensor * a,
1413
+ struct lm_ggml_tensor * b,
1414
+ float scale,
1415
+ float max_bias);
1416
+
1417
+ // rotary position embedding
1418
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1419
+ // if (mode & LM_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1420
+ //
1421
+ // b is an int32 vector with size a->ne[2], it contains the positions
1422
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope(
1423
+ struct lm_ggml_context * ctx,
1424
+ struct lm_ggml_tensor * a,
1425
+ struct lm_ggml_tensor * b,
1426
+ int n_dims,
1427
+ int mode);
1428
+
1429
+ // in-place, returns view(a)
1430
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_inplace(
1431
+ struct lm_ggml_context * ctx,
1432
+ struct lm_ggml_tensor * a,
1433
+ struct lm_ggml_tensor * b,
1434
+ int n_dims,
1435
+ int mode);
1436
+
1437
+ // custom RoPE
1438
+ // c is freq factors (e.g. phi3-128k), (optional)
1439
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext(
1440
+ struct lm_ggml_context * ctx,
1441
+ struct lm_ggml_tensor * a,
1442
+ struct lm_ggml_tensor * b,
1443
+ struct lm_ggml_tensor * c,
1444
+ int n_dims,
1445
+ int mode,
1446
+ int n_ctx_orig,
1447
+ float freq_base,
1448
+ float freq_scale,
1449
+ float ext_factor,
1450
+ float attn_factor,
1451
+ float beta_fast,
1452
+ float beta_slow);
1453
+
1454
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi(
1455
+ struct lm_ggml_context * ctx,
1456
+ struct lm_ggml_tensor * a,
1457
+ struct lm_ggml_tensor * b,
1458
+ struct lm_ggml_tensor * c,
1459
+ int n_dims,
1460
+ int sections[4],
1461
+ int mode,
1462
+ int n_ctx_orig,
1463
+ float freq_base,
1464
+ float freq_scale,
1465
+ float ext_factor,
1466
+ float attn_factor,
1467
+ float beta_fast,
1468
+ float beta_slow);
1469
+
1470
+ // in-place, returns view(a)
1471
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_inplace(
1472
+ struct lm_ggml_context * ctx,
1473
+ struct lm_ggml_tensor * a,
1474
+ struct lm_ggml_tensor * b,
1475
+ struct lm_ggml_tensor * c,
1476
+ int n_dims,
1477
+ int mode,
1478
+ int n_ctx_orig,
1479
+ float freq_base,
1480
+ float freq_scale,
1481
+ float ext_factor,
1482
+ float attn_factor,
1483
+ float beta_fast,
1484
+ float beta_slow);
1485
+
1486
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom(
1487
+ struct lm_ggml_context * ctx,
1488
+ struct lm_ggml_tensor * a,
1489
+ struct lm_ggml_tensor * b,
1490
+ int n_dims,
1491
+ int mode,
1492
+ int n_ctx_orig,
1493
+ float freq_base,
1494
+ float freq_scale,
1495
+ float ext_factor,
1496
+ float attn_factor,
1497
+ float beta_fast,
1498
+ float beta_slow),
1499
+ "use lm_ggml_rope_ext instead");
1500
+
1501
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom_inplace(
1502
+ struct lm_ggml_context * ctx,
1503
+ struct lm_ggml_tensor * a,
1504
+ struct lm_ggml_tensor * b,
1505
+ int n_dims,
1506
+ int mode,
1507
+ int n_ctx_orig,
1508
+ float freq_base,
1509
+ float freq_scale,
1510
+ float ext_factor,
1511
+ float attn_factor,
1512
+ float beta_fast,
1513
+ float beta_slow),
1514
+ "use lm_ggml_rope_ext_inplace instead");
1515
+
1516
+ // compute correction dims for YaRN RoPE scaling
1517
+ LM_GGML_API void lm_ggml_rope_yarn_corr_dims(
1518
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1519
+
1520
+ // rotary position embedding backward, i.e compute dx from dy
1521
+ // a - dy
1522
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_back(
1523
+ struct lm_ggml_context * ctx,
1524
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_rope result
1525
+ struct lm_ggml_tensor * b, // positions
1526
+ struct lm_ggml_tensor * c, // freq factors
1527
+ int n_dims,
1528
+ int mode,
1529
+ int n_ctx_orig,
1530
+ float freq_base,
1531
+ float freq_scale,
1532
+ float ext_factor,
1533
+ float attn_factor,
1534
+ float beta_fast,
1535
+ float beta_slow);
1536
+
1537
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi_back(
1538
+ struct lm_ggml_context * ctx,
1539
+ struct lm_ggml_tensor * a,
1540
+ struct lm_ggml_tensor * b,
1541
+ struct lm_ggml_tensor * c,
1542
+ int n_dims,
1543
+ int sections[4],
1544
+ int mode,
1545
+ int n_ctx_orig,
1546
+ float freq_base,
1547
+ float freq_scale,
1548
+ float ext_factor,
1549
+ float attn_factor,
1550
+ float beta_fast,
1551
+ float beta_slow);
1552
+
1553
+
1554
+ // clamp
1555
+ // in-place, returns view(a)
1556
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_clamp(
1557
+ struct lm_ggml_context * ctx,
1558
+ struct lm_ggml_tensor * a,
1559
+ float min,
1560
+ float max);
1561
+
1562
+ // im2col
1563
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1564
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col(
1565
+ struct lm_ggml_context * ctx,
1566
+ struct lm_ggml_tensor * a, // convolution kernel
1567
+ struct lm_ggml_tensor * b, // data
1568
+ int s0, // stride dimension 0
1569
+ int s1, // stride dimension 1
1570
+ int p0, // padding dimension 0
1571
+ int p1, // padding dimension 1
1572
+ int d0, // dilation dimension 0
1573
+ int d1, // dilation dimension 1
1574
+ bool is_2D,
1575
+ enum lm_ggml_type dst_type);
1576
+
1577
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col_back(
1578
+ struct lm_ggml_context * ctx,
1579
+ struct lm_ggml_tensor * a, // convolution kernel
1580
+ struct lm_ggml_tensor * b, // gradient of im2col output
1581
+ int64_t * ne, // shape of im2col input
1582
+ int s0, // stride dimension 0
1583
+ int s1, // stride dimension 1
1584
+ int p0, // padding dimension 0
1585
+ int p1, // padding dimension 1
1586
+ int d0, // dilation dimension 0
1587
+ int d1, // dilation dimension 1
1588
+ bool is_2D);
1589
+
1590
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d(
1591
+ struct lm_ggml_context * ctx,
1592
+ struct lm_ggml_tensor * a, // convolution kernel
1593
+ struct lm_ggml_tensor * b, // data
1594
+ int s0, // stride
1595
+ int p0, // padding
1596
+ int d0); // dilation
1597
+
1598
+ // conv_1d with padding = half
1599
+ // alias for lm_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1600
+ LM_GGML_API struct lm_ggml_tensor* lm_ggml_conv_1d_ph(
1601
+ struct lm_ggml_context * ctx,
1602
+ struct lm_ggml_tensor * a, // convolution kernel
1603
+ struct lm_ggml_tensor * b, // data
1604
+ int s, // stride
1605
+ int d); // dilation
1606
+
1607
+ // depthwise
1608
+ // TODO: this is very likely wrong for some cases! - needs more testing
1609
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw(
1610
+ struct lm_ggml_context * ctx,
1611
+ struct lm_ggml_tensor * a, // convolution kernel
1612
+ struct lm_ggml_tensor * b, // data
1613
+ int s0, // stride
1614
+ int p0, // padding
1615
+ int d0); // dilation
1616
+
1617
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw_ph(
1618
+ struct lm_ggml_context * ctx,
1619
+ struct lm_ggml_tensor * a, // convolution kernel
1620
+ struct lm_ggml_tensor * b, // data
1621
+ int s0, // stride
1622
+ int d0); // dilation
1623
+
1624
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_1d(
1625
+ struct lm_ggml_context * ctx,
1626
+ struct lm_ggml_tensor * a, // convolution kernel
1627
+ struct lm_ggml_tensor * b, // data
1628
+ int s0, // stride
1629
+ int p0, // padding
1630
+ int d0); // dilation
1631
+
1632
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d(
1633
+ struct lm_ggml_context * ctx,
1634
+ struct lm_ggml_tensor * a, // convolution kernel
1635
+ struct lm_ggml_tensor * b, // data
1636
+ int s0, // stride dimension 0
1637
+ int s1, // stride dimension 1
1638
+ int p0, // padding dimension 0
1639
+ int p1, // padding dimension 1
1640
+ int d0, // dilation dimension 0
1641
+ int d1); // dilation dimension 1
1642
+
1643
+ // kernel size is a->ne[0] x a->ne[1]
1644
+ // stride is equal to kernel size
1645
+ // padding is zero
1646
+ // example:
1647
+ // a: 16 16 3 768
1648
+ // b: 1024 1024 3 1
1649
+ // res: 64 64 768 1
1650
+ // used in sam
1651
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_sk_p0(
1652
+ struct lm_ggml_context * ctx,
1653
+ struct lm_ggml_tensor * a,
1654
+ struct lm_ggml_tensor * b);
1655
+
1656
+ // kernel size is a->ne[0] x a->ne[1]
1657
+ // stride is 1
1658
+ // padding is half
1659
+ // example:
1660
+ // a: 3 3 256 256
1661
+ // b: 64 64 256 1
1662
+ // res: 64 64 256 1
1663
+ // used in sam
1664
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_s1_ph(
1665
+ struct lm_ggml_context * ctx,
1666
+ struct lm_ggml_tensor * a,
1667
+ struct lm_ggml_tensor * b);
1668
+
1669
+ // depthwise
1670
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_dw(
1671
+ struct lm_ggml_context * ctx,
1672
+ struct lm_ggml_tensor * a, // convolution kernel
1673
+ struct lm_ggml_tensor * b, // data
1674
+ int s0, // stride dimension 0
1675
+ int s1, // stride dimension 1
1676
+ int p0, // padding dimension 0
1677
+ int p1, // padding dimension 1
1678
+ int d0, // dilation dimension 0
1679
+ int d1); // dilation dimension 1
1680
+
1681
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_2d_p0(
1682
+ struct lm_ggml_context * ctx,
1683
+ struct lm_ggml_tensor * a,
1684
+ struct lm_ggml_tensor * b,
1685
+ int stride);
1686
+
1687
+ enum lm_ggml_op_pool {
1688
+ LM_GGML_OP_POOL_MAX,
1689
+ LM_GGML_OP_POOL_AVG,
1690
+ LM_GGML_OP_POOL_COUNT,
1691
+ };
1692
+
1693
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_1d(
1694
+ struct lm_ggml_context * ctx,
1695
+ struct lm_ggml_tensor * a,
1696
+ enum lm_ggml_op_pool op,
1697
+ int k0, // kernel size
1698
+ int s0, // stride
1699
+ int p0); // padding
1700
+
1701
+ // the result will have 2*p0 padding for the first dimension
1702
+ // and 2*p1 padding for the second dimension
1703
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d(
1704
+ struct lm_ggml_context * ctx,
1705
+ struct lm_ggml_tensor * a,
1706
+ enum lm_ggml_op_pool op,
1707
+ int k0,
1708
+ int k1,
1709
+ int s0,
1710
+ int s1,
1711
+ float p0,
1712
+ float p1);
1713
+
1714
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d_back(
1715
+ struct lm_ggml_context * ctx,
1716
+ struct lm_ggml_tensor * a,
1717
+ struct lm_ggml_tensor * af, // "a"/input used in forward pass
1718
+ enum lm_ggml_op_pool op,
1719
+ int k0,
1720
+ int k1,
1721
+ int s0,
1722
+ int s1,
1723
+ float p0,
1724
+ float p1);
1725
+
1726
+ // nearest interpolate
1727
+ // multiplies ne0 and ne1 by scale factor
1728
+ // used in stable-diffusion
1729
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale(
1730
+ struct lm_ggml_context * ctx,
1731
+ struct lm_ggml_tensor * a,
1732
+ int scale_factor);
1733
+
1734
+ // nearest interpolate
1735
+ // nearest interpolate to specified dimensions
1736
+ // used in tortoise.cpp
1737
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale_ext(
1738
+ struct lm_ggml_context * ctx,
1739
+ struct lm_ggml_tensor * a,
1740
+ int ne0,
1741
+ int ne1,
1742
+ int ne2,
1743
+ int ne3);
1744
+
1745
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1746
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad(
1747
+ struct lm_ggml_context * ctx,
1748
+ struct lm_ggml_tensor * a,
1749
+ int p0,
1750
+ int p1,
1751
+ int p2,
1752
+ int p3);
1753
+
1754
+ // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
1755
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad_reflect_1d(
1756
+ struct lm_ggml_context * ctx,
1757
+ struct lm_ggml_tensor * a,
1758
+ int p0,
1759
+ int p1);
1760
+
1761
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1762
+ // timesteps: [N,]
1763
+ // return: [N, dim]
1764
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_timestep_embedding(
1765
+ struct lm_ggml_context * ctx,
1766
+ struct lm_ggml_tensor * timesteps,
1767
+ int dim,
1768
+ int max_period);
1769
+
1770
+ // sort rows
1771
+ enum lm_ggml_sort_order {
1772
+ LM_GGML_SORT_ORDER_ASC,
1773
+ LM_GGML_SORT_ORDER_DESC,
1774
+ };
1775
+
1776
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argsort(
1777
+ struct lm_ggml_context * ctx,
1778
+ struct lm_ggml_tensor * a,
1779
+ enum lm_ggml_sort_order order);
1780
+
1781
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_arange(
1782
+ struct lm_ggml_context * ctx,
1783
+ float start,
1784
+ float stop,
1785
+ float step);
1786
+
1787
+ // top k elements per row
1788
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_top_k(
1789
+ struct lm_ggml_context * ctx,
1790
+ struct lm_ggml_tensor * a,
1791
+ int k);
1792
+
1793
+ #define LM_GGML_KQ_MASK_PAD 64
1794
+
1795
+ // q: [n_embd_k, n_batch, n_head, 1]
1796
+ // k: [n_embd_k, n_kv, n_head_kv, 1]
1797
+ // v: [n_embd_v, n_kv, n_head_kv, 1] !! not transposed !!
1798
+ // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = LM_GGML_PAD(n_batch, LM_GGML_KQ_MASK_PAD) !!
1799
+ // res: [n_embd_v, n_head, n_batch, 1] !! permuted !!
1800
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_ext(
1801
+ struct lm_ggml_context * ctx,
1802
+ struct lm_ggml_tensor * q,
1803
+ struct lm_ggml_tensor * k,
1804
+ struct lm_ggml_tensor * v,
1805
+ struct lm_ggml_tensor * mask,
1806
+ float scale,
1807
+ float max_bias,
1808
+ float logit_softcap);
1809
+
1810
+ LM_GGML_API void lm_ggml_flash_attn_ext_set_prec(
1811
+ struct lm_ggml_tensor * a,
1812
+ enum lm_ggml_prec prec);
1813
+
1814
+ LM_GGML_API enum lm_ggml_prec lm_ggml_flash_attn_ext_get_prec(
1815
+ const struct lm_ggml_tensor * a);
1816
+
1817
+ // TODO: needs to be adapted to lm_ggml_flash_attn_ext
1818
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_back(
1819
+ struct lm_ggml_context * ctx,
1820
+ struct lm_ggml_tensor * q,
1821
+ struct lm_ggml_tensor * k,
1822
+ struct lm_ggml_tensor * v,
1823
+ struct lm_ggml_tensor * d,
1824
+ bool masked);
1825
+
1826
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_conv(
1827
+ struct lm_ggml_context * ctx,
1828
+ struct lm_ggml_tensor * sx,
1829
+ struct lm_ggml_tensor * c);
1830
+
1831
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_scan(
1832
+ struct lm_ggml_context * ctx,
1833
+ struct lm_ggml_tensor * s,
1834
+ struct lm_ggml_tensor * x,
1835
+ struct lm_ggml_tensor * dt,
1836
+ struct lm_ggml_tensor * A,
1837
+ struct lm_ggml_tensor * B,
1838
+ struct lm_ggml_tensor * C);
1839
+
1840
+ // partition into non-overlapping windows with padding if needed
1841
+ // example:
1842
+ // a: 768 64 64 1
1843
+ // w: 14
1844
+ // res: 768 14 14 25
1845
+ // used in sam
1846
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_part(
1847
+ struct lm_ggml_context * ctx,
1848
+ struct lm_ggml_tensor * a,
1849
+ int w);
1850
+
1851
+ // reverse of lm_ggml_win_part
1852
+ // used in sam
1853
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_unpart(
1854
+ struct lm_ggml_context * ctx,
1855
+ struct lm_ggml_tensor * a,
1856
+ int w0,
1857
+ int h0,
1858
+ int w);
1859
+
1860
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary(
1861
+ struct lm_ggml_context * ctx,
1862
+ struct lm_ggml_tensor * a,
1863
+ enum lm_ggml_unary_op op);
1864
+
1865
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary_inplace(
1866
+ struct lm_ggml_context * ctx,
1867
+ struct lm_ggml_tensor * a,
1868
+ enum lm_ggml_unary_op op);
1869
+
1870
+ // used in sam
1871
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rel_pos(
1872
+ struct lm_ggml_context * ctx,
1873
+ struct lm_ggml_tensor * a,
1874
+ int qh,
1875
+ int kh);
1876
+
1877
+ // used in sam
1878
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos(
1879
+ struct lm_ggml_context * ctx,
1880
+ struct lm_ggml_tensor * a,
1881
+ struct lm_ggml_tensor * pw,
1882
+ struct lm_ggml_tensor * ph);
1883
+
1884
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos_inplace(
1885
+ struct lm_ggml_context * ctx,
1886
+ struct lm_ggml_tensor * a,
1887
+ struct lm_ggml_tensor * pw,
1888
+ struct lm_ggml_tensor * ph);
1889
+
1890
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv6(
1891
+ struct lm_ggml_context * ctx,
1892
+ struct lm_ggml_tensor * k,
1893
+ struct lm_ggml_tensor * v,
1894
+ struct lm_ggml_tensor * r,
1895
+ struct lm_ggml_tensor * tf,
1896
+ struct lm_ggml_tensor * td,
1897
+ struct lm_ggml_tensor * state);
1898
+
1899
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gated_linear_attn(
1900
+ struct lm_ggml_context * ctx,
1901
+ struct lm_ggml_tensor * k,
1902
+ struct lm_ggml_tensor * v,
1903
+ struct lm_ggml_tensor * q,
1904
+ struct lm_ggml_tensor * g,
1905
+ struct lm_ggml_tensor * state,
1906
+ float scale);
1907
+
1908
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv7(
1909
+ struct lm_ggml_context * ctx,
1910
+ struct lm_ggml_tensor * r,
1911
+ struct lm_ggml_tensor * w,
1912
+ struct lm_ggml_tensor * k,
1913
+ struct lm_ggml_tensor * v,
1914
+ struct lm_ggml_tensor * a,
1915
+ struct lm_ggml_tensor * b,
1916
+ struct lm_ggml_tensor * state);
1917
+
1918
+ // custom operators
1919
+
1920
+ typedef void (*lm_ggml_unary_op_f32_t) (const int, float *, const float *);
1921
+ typedef void (*lm_ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
1922
+
1923
+ typedef void (*lm_ggml_custom1_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1924
+ typedef void (*lm_ggml_custom2_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1925
+ typedef void (*lm_ggml_custom3_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1926
+
1927
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_f32(
1928
+ struct lm_ggml_context * ctx,
1929
+ struct lm_ggml_tensor * a,
1930
+ lm_ggml_unary_op_f32_t fun),
1931
+ "use lm_ggml_map_custom1 instead");
1932
+
1933
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_inplace_f32(
1934
+ struct lm_ggml_context * ctx,
1935
+ struct lm_ggml_tensor * a,
1936
+ lm_ggml_unary_op_f32_t fun),
1937
+ "use lm_ggml_map_custom1_inplace instead");
1938
+
1939
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_f32(
1940
+ struct lm_ggml_context * ctx,
1941
+ struct lm_ggml_tensor * a,
1942
+ struct lm_ggml_tensor * b,
1943
+ lm_ggml_binary_op_f32_t fun),
1944
+ "use lm_ggml_map_custom2 instead");
1945
+
1946
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_inplace_f32(
1947
+ struct lm_ggml_context * ctx,
1948
+ struct lm_ggml_tensor * a,
1949
+ struct lm_ggml_tensor * b,
1950
+ lm_ggml_binary_op_f32_t fun),
1951
+ "use lm_ggml_map_custom2_inplace instead");
1952
+
1953
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_f32(
1954
+ struct lm_ggml_context * ctx,
1955
+ struct lm_ggml_tensor * a,
1956
+ lm_ggml_custom1_op_f32_t fun),
1957
+ "use lm_ggml_map_custom1 instead");
1958
+
1959
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace_f32(
1960
+ struct lm_ggml_context * ctx,
1961
+ struct lm_ggml_tensor * a,
1962
+ lm_ggml_custom1_op_f32_t fun),
1963
+ "use lm_ggml_map_custom1_inplace instead");
1964
+
1965
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_f32(
1966
+ struct lm_ggml_context * ctx,
1967
+ struct lm_ggml_tensor * a,
1968
+ struct lm_ggml_tensor * b,
1969
+ lm_ggml_custom2_op_f32_t fun),
1970
+ "use lm_ggml_map_custom2 instead");
1971
+
1972
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace_f32(
1973
+ struct lm_ggml_context * ctx,
1974
+ struct lm_ggml_tensor * a,
1975
+ struct lm_ggml_tensor * b,
1976
+ lm_ggml_custom2_op_f32_t fun),
1977
+ "use lm_ggml_map_custom2_inplace instead");
1978
+
1979
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_f32(
1980
+ struct lm_ggml_context * ctx,
1981
+ struct lm_ggml_tensor * a,
1982
+ struct lm_ggml_tensor * b,
1983
+ struct lm_ggml_tensor * c,
1984
+ lm_ggml_custom3_op_f32_t fun),
1985
+ "use lm_ggml_map_custom3 instead");
1986
+
1987
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace_f32(
1988
+ struct lm_ggml_context * ctx,
1989
+ struct lm_ggml_tensor * a,
1990
+ struct lm_ggml_tensor * b,
1991
+ struct lm_ggml_tensor * c,
1992
+ lm_ggml_custom3_op_f32_t fun),
1993
+ "use lm_ggml_map_custom3_inplace instead");
1994
+
1995
+ // custom operators v2
1996
+
1997
+ typedef void (*lm_ggml_custom1_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, int ith, int nth, void * userdata);
1998
+ 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);
1999
+ 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);
2000
+
2001
+ #define LM_GGML_N_TASKS_MAX (-1)
2002
+ // n_tasks == LM_GGML_N_TASKS_MAX means to use max number of tasks
2003
+
2004
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1(
2005
+ struct lm_ggml_context * ctx,
2006
+ struct lm_ggml_tensor * a,
2007
+ lm_ggml_custom1_op_t fun,
2008
+ int n_tasks,
2009
+ void * userdata);
2010
+
2011
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace(
2012
+ struct lm_ggml_context * ctx,
2013
+ struct lm_ggml_tensor * a,
2014
+ lm_ggml_custom1_op_t fun,
2015
+ int n_tasks,
2016
+ void * userdata);
2017
+
2018
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2(
2019
+ struct lm_ggml_context * ctx,
2020
+ struct lm_ggml_tensor * a,
2021
+ struct lm_ggml_tensor * b,
2022
+ lm_ggml_custom2_op_t fun,
2023
+ int n_tasks,
2024
+ void * userdata);
2025
+
2026
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace(
2027
+ struct lm_ggml_context * ctx,
2028
+ struct lm_ggml_tensor * a,
2029
+ struct lm_ggml_tensor * b,
2030
+ lm_ggml_custom2_op_t fun,
2031
+ int n_tasks,
2032
+ void * userdata);
2033
+
2034
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3(
2035
+ struct lm_ggml_context * ctx,
2036
+ struct lm_ggml_tensor * a,
2037
+ struct lm_ggml_tensor * b,
2038
+ struct lm_ggml_tensor * c,
2039
+ lm_ggml_custom3_op_t fun,
2040
+ int n_tasks,
2041
+ void * userdata);
2042
+
2043
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace(
2044
+ struct lm_ggml_context * ctx,
2045
+ struct lm_ggml_tensor * a,
2046
+ struct lm_ggml_tensor * b,
2047
+ struct lm_ggml_tensor * c,
2048
+ lm_ggml_custom3_op_t fun,
2049
+ int n_tasks,
2050
+ void * userdata);
2051
+
2052
+ // loss function
2053
+
2054
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss(
2055
+ struct lm_ggml_context * ctx,
2056
+ struct lm_ggml_tensor * a, // logits
2057
+ struct lm_ggml_tensor * b); // labels
2058
+
2059
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss_back(
2060
+ struct lm_ggml_context * ctx,
2061
+ struct lm_ggml_tensor * a, // logits
2062
+ struct lm_ggml_tensor * b, // labels
2063
+ struct lm_ggml_tensor * c); // gradients of cross_entropy_loss result
2064
+
2065
+ // AdamW optimizer step
2066
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2067
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2068
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_adamw(
2069
+ struct lm_ggml_context * ctx,
2070
+ struct lm_ggml_tensor * a,
2071
+ struct lm_ggml_tensor * grad,
2072
+ struct lm_ggml_tensor * m,
2073
+ struct lm_ggml_tensor * v,
2074
+ struct lm_ggml_tensor * adamw_params); // parameters such a the learning rate
2075
+
2076
+ //
2077
+ // automatic differentiation
2078
+ //
2079
+
2080
+ LM_GGML_API void lm_ggml_build_forward_expand(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2081
+ LM_GGML_API void lm_ggml_build_backward_expand(
2082
+ struct lm_ggml_context * ctx_static, // context for static gradients (loss + gradient accumulation)
2083
+ struct lm_ggml_context * ctx_compute, // context for gradient computation
2084
+ struct lm_ggml_cgraph * cgraph,
2085
+ bool accumulate); // whether or not gradients should be accumulated, requires static allocation of tensors in ctx_static
2086
+
2087
+ // graph allocation in a context
2088
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph (struct lm_ggml_context * ctx); // size = LM_GGML_DEFAULT_GRAPH_SIZE, grads = false
2089
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph_custom(struct lm_ggml_context * ctx, size_t size, bool grads);
2090
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_dup (struct lm_ggml_context * ctx, struct lm_ggml_cgraph * cgraph);
2091
+ LM_GGML_API void lm_ggml_graph_cpy (struct lm_ggml_cgraph * src, struct lm_ggml_cgraph * dst);
2092
+ 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
2093
+ LM_GGML_API void lm_ggml_graph_clear (struct lm_ggml_cgraph * cgraph);
2094
+
2095
+ LM_GGML_API int lm_ggml_graph_size (struct lm_ggml_cgraph * cgraph);
2096
+ 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]
2097
+ LM_GGML_API struct lm_ggml_tensor ** lm_ggml_graph_nodes (struct lm_ggml_cgraph * cgraph);
2098
+ LM_GGML_API int lm_ggml_graph_n_nodes(struct lm_ggml_cgraph * cgraph);
2099
+
2100
+ LM_GGML_API void lm_ggml_graph_add_node(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2101
+
2102
+ LM_GGML_API size_t lm_ggml_graph_overhead(void);
2103
+ LM_GGML_API size_t lm_ggml_graph_overhead_custom(size_t size, bool grads);
2104
+
2105
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_tensor (const struct lm_ggml_cgraph * cgraph, const char * name);
2106
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad (const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2107
+ 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);
2108
+
2109
+ LM_GGML_API void lm_ggml_graph_export(const struct lm_ggml_cgraph * cgraph, const char * fname);
2110
+ 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);
2111
+
2112
+ // print info and performance information for the graph
2113
+ LM_GGML_API void lm_ggml_graph_print(const struct lm_ggml_cgraph * cgraph);
2114
+
2115
+ // dump the graph into a file using the dot format
2116
+ LM_GGML_API void lm_ggml_graph_dump_dot(const struct lm_ggml_cgraph * gb, const struct lm_ggml_cgraph * gf, const char * filename);
2117
+
2118
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2119
+ typedef void (*lm_ggml_log_callback)(enum lm_ggml_log_level level, const char * text, void * user_data);
2120
+
2121
+ // Set callback for all future logging events.
2122
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2123
+ LM_GGML_API void lm_ggml_log_set(lm_ggml_log_callback log_callback, void * user_data);
2124
+
2125
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_zero(struct lm_ggml_tensor * tensor);
2126
+
2127
+ //
2128
+ // quantization
2129
+ //
2130
+
2131
+ // - lm_ggml_quantize_init can be called multiple times with the same type
2132
+ // it will only initialize the quantization tables for the first call or after lm_ggml_quantize_free
2133
+ // automatically called by lm_ggml_quantize_chunk for convenience
2134
+ //
2135
+ // - lm_ggml_quantize_free will free any memory allocated by lm_ggml_quantize_init
2136
+ // call this at the end of the program to avoid memory leaks
2137
+ //
2138
+ // note: these are thread-safe
2139
+ //
2140
+ LM_GGML_API void lm_ggml_quantize_init(enum lm_ggml_type type);
2141
+ LM_GGML_API void lm_ggml_quantize_free(void);
2142
+
2143
+ // some quantization type cannot be used without an importance matrix
2144
+ LM_GGML_API bool lm_ggml_quantize_requires_imatrix(enum lm_ggml_type type);
2145
+
2146
+ // calls lm_ggml_quantize_init internally (i.e. can allocate memory)
2147
+ LM_GGML_API size_t lm_ggml_quantize_chunk(
2148
+ enum lm_ggml_type type,
2149
+ const float * src,
2150
+ void * dst,
2151
+ int64_t start,
2152
+ int64_t nrows,
2153
+ int64_t n_per_row,
2154
+ const float * imatrix);
2155
+
2156
+ #ifdef __cplusplus
2157
+ // restrict not standard in C++
2158
+ # if defined(__GNUC__)
2159
+ # define LM_GGML_RESTRICT __restrict__
2160
+ # elif defined(__clang__)
2161
+ # define LM_GGML_RESTRICT __restrict
2162
+ # elif defined(_MSC_VER)
2163
+ # define LM_GGML_RESTRICT __restrict
2164
+ # else
2165
+ # define LM_GGML_RESTRICT
2166
+ # endif
2167
+ #else
2168
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2169
+ # define LM_GGML_RESTRICT __restrict
2170
+ # else
2171
+ # define LM_GGML_RESTRICT restrict
2172
+ # endif
2173
+ #endif
2174
+ typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2175
+ typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2176
+
2177
+ struct lm_ggml_type_traits {
2178
+ const char * type_name;
2179
+ int64_t blck_size;
2180
+ int64_t blck_size_interleave; // interleave elements in blocks
2181
+ size_t type_size;
2182
+ bool is_quantized;
2183
+ lm_ggml_to_float_t to_float;
2184
+ lm_ggml_from_float_t from_float_ref;
2185
+ };
2186
+
2187
+ LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2188
+
2189
+ // ggml threadpool
2190
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2191
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2192
+
2193
+ // scheduling priorities
2194
+ enum lm_ggml_sched_priority {
2195
+ LM_GGML_SCHED_PRIO_NORMAL,
2196
+ LM_GGML_SCHED_PRIO_MEDIUM,
2197
+ LM_GGML_SCHED_PRIO_HIGH,
2198
+ LM_GGML_SCHED_PRIO_REALTIME
2199
+ };
2200
+
2201
+ // threadpool params
2202
+ // Use lm_ggml_threadpool_params_default() or lm_ggml_threadpool_params_init() to populate the defaults
2203
+ struct lm_ggml_threadpool_params {
2204
+ bool cpumask[LM_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2205
+ int n_threads; // number of threads
2206
+ enum lm_ggml_sched_priority prio; // thread priority
2207
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2208
+ bool strict_cpu; // strict cpu placement
2209
+ bool paused; // start in paused state
2210
+ };
2211
+
2212
+ struct lm_ggml_threadpool; // forward declaration, see ggml.c
2213
+
2214
+ typedef struct lm_ggml_threadpool * lm_ggml_threadpool_t;
2215
+
2216
+ LM_GGML_API struct lm_ggml_threadpool_params lm_ggml_threadpool_params_default(int n_threads);
2217
+ LM_GGML_API void lm_ggml_threadpool_params_init (struct lm_ggml_threadpool_params * p, int n_threads);
2218
+ LM_GGML_API bool lm_ggml_threadpool_params_match (const struct lm_ggml_threadpool_params * p0, const struct lm_ggml_threadpool_params * p1);
2219
+
2220
+ #ifdef __cplusplus
2221
+ }
2222
+ #endif