llama-cpp-pro 0.2.2

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 (310) hide show
  1. package/CHANGELOG.md +295 -0
  2. package/LICENSE +21 -0
  3. package/LlamaCpp.podspec +33 -0
  4. package/LlamaCppCapacitor.podspec +33 -0
  5. package/Package.swift +29 -0
  6. package/README.md +93 -0
  7. package/android/build.gradle +88 -0
  8. package/android/src/main/AndroidManifest.xml +4 -0
  9. package/android/src/main/CMakeLists-arm64.txt +138 -0
  10. package/android/src/main/CMakeLists-x86_64.txt +141 -0
  11. package/android/src/main/CMakeLists.txt +138 -0
  12. package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCpp.java +1340 -0
  13. package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCppPlugin.java +814 -0
  14. package/android/src/main/java/ai/annadata/plugin/capacitor/ModelAdmissionController.java +214 -0
  15. package/android/src/main/jni-chat-session.cpp +261 -0
  16. package/android/src/main/jni-lora.cpp +197 -0
  17. package/android/src/main/jni-multimodal.cpp +167 -0
  18. package/android/src/main/jni-tts.cpp +290 -0
  19. package/android/src/main/jni-utils.h +148 -0
  20. package/android/src/main/jni.cpp +1808 -0
  21. package/android/src/main/jniLibs/arm64-v8a/libllama-cpp-arm64.so +0 -0
  22. package/android/src/main/res/.gitkeep +0 -0
  23. package/build-native.sh +280 -0
  24. package/cmake/desktop-metal-embed.cmake +30 -0
  25. package/cmake/desktop-sources.cmake +100 -0
  26. package/cmake/ggml-backends.cmake +44 -0
  27. package/cpp/LICENSE +21 -0
  28. package/cpp/README.md +15 -0
  29. package/cpp/anyascii.c +22223 -0
  30. package/cpp/anyascii.h +42 -0
  31. package/cpp/cap-completion.cpp +942 -0
  32. package/cpp/cap-completion.h +127 -0
  33. package/cpp/cap-embedding.cpp +193 -0
  34. package/cpp/cap-embedding.h +35 -0
  35. package/cpp/cap-ios-bridge.cpp +1810 -0
  36. package/cpp/cap-ios-bridge.h +61 -0
  37. package/cpp/cap-llama.cpp +412 -0
  38. package/cpp/cap-llama.h +161 -0
  39. package/cpp/cap-mtmd.hpp +602 -0
  40. package/cpp/cap-native-server.cpp +1095 -0
  41. package/cpp/cap-native-server.h +40 -0
  42. package/cpp/cap-tts.cpp +591 -0
  43. package/cpp/cap-tts.h +59 -0
  44. package/cpp/cap-wasm-fs.cpp +156 -0
  45. package/cpp/cap-wasm-jspi.cpp +19 -0
  46. package/cpp/cap-wasm-jspi.h +30 -0
  47. package/cpp/cap-wasm-vfs.cpp +22 -0
  48. package/cpp/chat-parser.cpp +393 -0
  49. package/cpp/chat-parser.h +120 -0
  50. package/cpp/chat.cpp +2315 -0
  51. package/cpp/chat.h +221 -0
  52. package/cpp/common.cpp +1664 -0
  53. package/cpp/common.h +744 -0
  54. package/cpp/ggml-alloc.c +1028 -0
  55. package/cpp/ggml-alloc.h +76 -0
  56. package/cpp/ggml-backend-impl.h +255 -0
  57. package/cpp/ggml-backend-reg.cpp +600 -0
  58. package/cpp/ggml-backend.cpp +2121 -0
  59. package/cpp/ggml-backend.h +354 -0
  60. package/cpp/ggml-common.h +1878 -0
  61. package/cpp/ggml-cpp.h +39 -0
  62. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  63. package/cpp/ggml-cpu/amx/amx.h +8 -0
  64. package/cpp/ggml-cpu/amx/common.h +91 -0
  65. package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
  66. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  67. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  68. package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
  69. package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
  70. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  71. package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
  72. package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
  73. package/cpp/ggml-cpu/arch-fallback.h +215 -0
  74. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  75. package/cpp/ggml-cpu/binary-ops.h +16 -0
  76. package/cpp/ggml-cpu/common.h +73 -0
  77. package/cpp/ggml-cpu/ggml-cpu-impl.h +559 -0
  78. package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
  79. package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
  80. package/cpp/ggml-cpu/ops.cpp +10587 -0
  81. package/cpp/ggml-cpu/ops.h +114 -0
  82. package/cpp/ggml-cpu/quants.c +1193 -0
  83. package/cpp/ggml-cpu/quants.h +97 -0
  84. package/cpp/ggml-cpu/repack.cpp +1982 -0
  85. package/cpp/ggml-cpu/repack.h +120 -0
  86. package/cpp/ggml-cpu/simd-mappings.h +1184 -0
  87. package/cpp/ggml-cpu/traits.cpp +36 -0
  88. package/cpp/ggml-cpu/traits.h +38 -0
  89. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  90. package/cpp/ggml-cpu/unary-ops.h +28 -0
  91. package/cpp/ggml-cpu/vec.cpp +348 -0
  92. package/cpp/ggml-cpu/vec.h +1121 -0
  93. package/cpp/ggml-cpu.h +145 -0
  94. package/cpp/ggml-impl.h +622 -0
  95. package/cpp/ggml-metal-impl.h +688 -0
  96. package/cpp/ggml-metal.h +66 -0
  97. package/cpp/ggml-metal.m +6833 -0
  98. package/cpp/ggml-metal.metal +10754 -0
  99. package/cpp/ggml-opt.cpp +1093 -0
  100. package/cpp/ggml-opt.h +256 -0
  101. package/cpp/ggml-quants.c +5324 -0
  102. package/cpp/ggml-quants.h +106 -0
  103. package/cpp/ggml-threading.cpp +12 -0
  104. package/cpp/ggml-threading.h +14 -0
  105. package/cpp/ggml.c +7108 -0
  106. package/cpp/ggml.h +2492 -0
  107. package/cpp/gguf.cpp +1358 -0
  108. package/cpp/gguf.h +202 -0
  109. package/cpp/json-partial.cpp +256 -0
  110. package/cpp/json-partial.h +38 -0
  111. package/cpp/json-schema-to-grammar.cpp +985 -0
  112. package/cpp/json-schema-to-grammar.h +21 -0
  113. package/cpp/llama-adapter.cpp +388 -0
  114. package/cpp/llama-adapter.h +76 -0
  115. package/cpp/llama-arch.cpp +2355 -0
  116. package/cpp/llama-arch.h +499 -0
  117. package/cpp/llama-batch.cpp +875 -0
  118. package/cpp/llama-batch.h +160 -0
  119. package/cpp/llama-chat.cpp +783 -0
  120. package/cpp/llama-chat.h +65 -0
  121. package/cpp/llama-context.cpp +2788 -0
  122. package/cpp/llama-context.h +306 -0
  123. package/cpp/llama-cparams.cpp +5 -0
  124. package/cpp/llama-cparams.h +41 -0
  125. package/cpp/llama-cpp.h +30 -0
  126. package/cpp/llama-grammar.cpp +1229 -0
  127. package/cpp/llama-grammar.h +173 -0
  128. package/cpp/llama-graph.cpp +1891 -0
  129. package/cpp/llama-graph.h +810 -0
  130. package/cpp/llama-hparams.cpp +180 -0
  131. package/cpp/llama-hparams.h +233 -0
  132. package/cpp/llama-impl.cpp +167 -0
  133. package/cpp/llama-impl.h +61 -0
  134. package/cpp/llama-io.cpp +15 -0
  135. package/cpp/llama-io.h +35 -0
  136. package/cpp/llama-kv-cache-iswa.cpp +318 -0
  137. package/cpp/llama-kv-cache-iswa.h +135 -0
  138. package/cpp/llama-kv-cache.cpp +2059 -0
  139. package/cpp/llama-kv-cache.h +374 -0
  140. package/cpp/llama-kv-cells.h +491 -0
  141. package/cpp/llama-memory-hybrid.cpp +258 -0
  142. package/cpp/llama-memory-hybrid.h +137 -0
  143. package/cpp/llama-memory-recurrent.cpp +1146 -0
  144. package/cpp/llama-memory-recurrent.h +179 -0
  145. package/cpp/llama-memory.cpp +59 -0
  146. package/cpp/llama-memory.h +119 -0
  147. package/cpp/llama-mmap.cpp +609 -0
  148. package/cpp/llama-mmap.h +68 -0
  149. package/cpp/llama-model-loader.cpp +1166 -0
  150. package/cpp/llama-model-loader.h +170 -0
  151. package/cpp/llama-model-saver.cpp +282 -0
  152. package/cpp/llama-model-saver.h +37 -0
  153. package/cpp/llama-model.cpp +19061 -0
  154. package/cpp/llama-model.h +491 -0
  155. package/cpp/llama-sampling.cpp +2575 -0
  156. package/cpp/llama-sampling.h +32 -0
  157. package/cpp/llama-vocab.cpp +3792 -0
  158. package/cpp/llama-vocab.h +176 -0
  159. package/cpp/llama.cpp +358 -0
  160. package/cpp/llama.h +1373 -0
  161. package/cpp/log.cpp +428 -0
  162. package/cpp/log.h +103 -0
  163. package/cpp/minja/chat-template.hpp +550 -0
  164. package/cpp/minja/minja.hpp +3009 -0
  165. package/cpp/nlohmann/json.hpp +25526 -0
  166. package/cpp/nlohmann/json_fwd.hpp +187 -0
  167. package/cpp/regex-partial.cpp +204 -0
  168. package/cpp/regex-partial.h +56 -0
  169. package/cpp/sampling.cpp +579 -0
  170. package/cpp/sampling.h +107 -0
  171. package/cpp/tools/mtmd/clip-impl.h +473 -0
  172. package/cpp/tools/mtmd/clip.cpp +4322 -0
  173. package/cpp/tools/mtmd/clip.h +106 -0
  174. package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
  175. package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
  176. package/cpp/tools/mtmd/mtmd-audio.h +47 -0
  177. package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
  178. package/cpp/tools/mtmd/mtmd-helper.h +95 -0
  179. package/cpp/tools/mtmd/mtmd.cpp +1066 -0
  180. package/cpp/tools/mtmd/mtmd.h +302 -0
  181. package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
  182. package/cpp/unicode-data.cpp +7034 -0
  183. package/cpp/unicode-data.h +20 -0
  184. package/cpp/unicode.cpp +1061 -0
  185. package/cpp/unicode.h +68 -0
  186. package/cpp/vendor/cpp-httplib/httplib.cpp +16509 -0
  187. package/cpp/vendor/cpp-httplib/httplib.h +3883 -0
  188. package/desktop/electron-builder.config.cjs +157 -0
  189. package/desktop/entitlements.mac.plist +12 -0
  190. package/desktop/package.json +11 -0
  191. package/desktop/resolve-package-root.cjs +88 -0
  192. package/desktop/src/main/backend-selector.cjs +148 -0
  193. package/desktop/src/main/gpu-probe.cjs +142 -0
  194. package/desktop/src/main/index.cjs +58 -0
  195. package/desktop/src/main/ipc-handlers.cjs +212 -0
  196. package/desktop/src/main/model-store.cjs +50 -0
  197. package/desktop/src/main/preload.cjs +39 -0
  198. package/desktop/src/main/sidecar-client.cjs +76 -0
  199. package/desktop/src/main/sidecar-manager.cjs +364 -0
  200. package/dist/docs.json +14357 -0
  201. package/dist/esm/definitions.d.ts +731 -0
  202. package/dist/esm/definitions.js +2 -0
  203. package/dist/esm/definitions.js.map +1 -0
  204. package/dist/esm/desktop.d.ts +37 -0
  205. package/dist/esm/desktop.js +133 -0
  206. package/dist/esm/desktop.js.map +1 -0
  207. package/dist/esm/index.d.ts +200 -0
  208. package/dist/esm/index.js +612 -0
  209. package/dist/esm/index.js.map +1 -0
  210. package/dist/esm/isomorphic/desktop.runtime.d.ts +37 -0
  211. package/dist/esm/isomorphic/desktop.runtime.js +36 -0
  212. package/dist/esm/isomorphic/desktop.runtime.js.map +1 -0
  213. package/dist/esm/isomorphic/errors.d.ts +6 -0
  214. package/dist/esm/isomorphic/errors.js +9 -0
  215. package/dist/esm/isomorphic/errors.js.map +1 -0
  216. package/dist/esm/isomorphic/model.admission.d.ts +17 -0
  217. package/dist/esm/isomorphic/model.admission.js +27 -0
  218. package/dist/esm/isomorphic/model.admission.js.map +1 -0
  219. package/dist/esm/isomorphic/model.scheduler.d.ts +31 -0
  220. package/dist/esm/isomorphic/model.scheduler.js +95 -0
  221. package/dist/esm/isomorphic/model.scheduler.js.map +1 -0
  222. package/dist/esm/isomorphic/provider.desktop.d.ts +40 -0
  223. package/dist/esm/isomorphic/provider.desktop.js +411 -0
  224. package/dist/esm/isomorphic/provider.desktop.js.map +1 -0
  225. package/dist/esm/isomorphic/provider.factory.d.ts +2 -0
  226. package/dist/esm/isomorphic/provider.factory.js +16 -0
  227. package/dist/esm/isomorphic/provider.factory.js.map +1 -0
  228. package/dist/esm/isomorphic/provider.interface.d.ts +76 -0
  229. package/dist/esm/isomorphic/provider.interface.js +2 -0
  230. package/dist/esm/isomorphic/provider.interface.js.map +1 -0
  231. package/dist/esm/isomorphic/provider.native.d.ts +18 -0
  232. package/dist/esm/isomorphic/provider.native.js +173 -0
  233. package/dist/esm/isomorphic/provider.native.js.map +1 -0
  234. package/dist/esm/isomorphic/provider.web.d.ts +88 -0
  235. package/dist/esm/isomorphic/provider.web.js +573 -0
  236. package/dist/esm/isomorphic/provider.web.js.map +1 -0
  237. package/dist/esm/isomorphic/sidecar-sse.d.ts +14 -0
  238. package/dist/esm/isomorphic/sidecar-sse.js +76 -0
  239. package/dist/esm/isomorphic/sidecar-sse.js.map +1 -0
  240. package/dist/esm/isomorphic/wasmMemoryCalibration.d.ts +26 -0
  241. package/dist/esm/isomorphic/wasmMemoryCalibration.js +60 -0
  242. package/dist/esm/isomorphic/wasmMemoryCalibration.js.map +1 -0
  243. package/dist/esm/isomorphic/wasmMemoryPolicy.d.ts +55 -0
  244. package/dist/esm/isomorphic/wasmMemoryPolicy.js +93 -0
  245. package/dist/esm/isomorphic/wasmMemoryPolicy.js.map +1 -0
  246. package/dist/esm/storage/manifest.d.ts +13 -0
  247. package/dist/esm/storage/manifest.js +87 -0
  248. package/dist/esm/storage/manifest.js.map +1 -0
  249. package/dist/esm/storage/opfs.store.d.ts +31 -0
  250. package/dist/esm/storage/opfs.store.js +241 -0
  251. package/dist/esm/storage/opfs.store.js.map +1 -0
  252. package/dist/esm/web.d.ts +206 -0
  253. package/dist/esm/web.js +521 -0
  254. package/dist/esm/web.js.map +1 -0
  255. package/dist/esm/workers/async-file.d.ts +13 -0
  256. package/dist/esm/workers/async-file.js +12 -0
  257. package/dist/esm/workers/async-file.js.map +1 -0
  258. package/dist/esm/workers/heapfs.d.ts +55 -0
  259. package/dist/esm/workers/heapfs.js +115 -0
  260. package/dist/esm/workers/heapfs.js.map +1 -0
  261. package/dist/esm/workers/wasm.engine.d.ts +86 -0
  262. package/dist/esm/workers/wasm.engine.js +504 -0
  263. package/dist/esm/workers/wasm.engine.js.map +1 -0
  264. package/dist/esm/workers/worker.protocol.d.ts +160 -0
  265. package/dist/esm/workers/worker.protocol.js +2 -0
  266. package/dist/esm/workers/worker.protocol.js.map +1 -0
  267. package/dist/plugin.cjs +3179 -0
  268. package/dist/plugin.cjs.map +1 -0
  269. package/dist/plugin.js +3181 -0
  270. package/dist/plugin.js.map +1 -0
  271. package/dist/wasm/llama_engine.d.ts +74 -0
  272. package/dist/wasm/llama_engine.js +1829 -0
  273. package/dist/wasm/llama_engine.wasm +0 -0
  274. package/dist/wasm/llama_engine_emscripten.mjs +2 -0
  275. package/dist/wasm/package.json +16 -0
  276. package/dist/workers/llm.worker.js +1226 -0
  277. package/dist/workers/llm.worker.js.map +7 -0
  278. package/extraResources/llama-wasm/llama_engine.d.ts +74 -0
  279. package/extraResources/llama-wasm/llama_engine.js +1829 -0
  280. package/extraResources/llama-wasm/llama_engine.wasm +0 -0
  281. package/extraResources/llama-wasm/llama_engine_emscripten.mjs +2 -0
  282. package/extraResources/llama-wasm/package.json +16 -0
  283. package/extraResources/sidecar/README.md +11 -0
  284. package/extraResources/sidecar/darwin-arm64 +0 -0
  285. package/extraResources/sidecar/darwin-x64 +0 -0
  286. package/ios/CMakeLists-arm64.txt +161 -0
  287. package/ios/CMakeLists-x86_64.txt +188 -0
  288. package/ios/CMakeLists.txt +161 -0
  289. package/ios/Frameworks/llama-cpp.framework/Info.plist +28 -0
  290. package/ios/Frameworks/llama-cpp.framework/llama-cpp +0 -0
  291. package/ios/Sources/LlamaCppCapacitor/LlamaCpp.swift +1365 -0
  292. package/ios/Sources/LlamaCppCapacitor/LlamaCppPlugin.swift +694 -0
  293. package/ios/Sources/LlamaCppCapacitor/LlamaNativeBridge.swift +349 -0
  294. package/ios/Sources/LlamaCppCapacitor/ModelAdmissionController.swift +177 -0
  295. package/ios/embed-metal-shaders.sh +24 -0
  296. package/ios/metal-embed.cmake +29 -0
  297. package/package.json +281 -0
  298. package/scripts/build-js-preserve-wasm.cjs +53 -0
  299. package/scripts/build-sidecar-linux.sh +6 -0
  300. package/scripts/build-sidecar-win.bat +19 -0
  301. package/scripts/build-sidecar.sh +184 -0
  302. package/scripts/embed-llama-ios-app-framework.sh +63 -0
  303. package/scripts/ensure-desktop-sidecar-bundle.cjs +103 -0
  304. package/scripts/ensure-llama-ios-xcframework.sh +108 -0
  305. package/scripts/fix-esm-extensions.cjs +45 -0
  306. package/scripts/prepare-js-dist.cjs +28 -0
  307. package/scripts/stage-desktop-resources.cjs +116 -0
  308. package/sidecar/CMakeLists.txt +192 -0
  309. package/sidecar/cap-sidecar-main.cpp +68 -0
  310. package/types/llama-cpp-pro.d.ts +441 -0
package/cpp/ggml.h ADDED
@@ -0,0 +1,2492 @@
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
+
212
+ #define LM_GGML_FILE_MAGIC 0x67676d6c // "ggml"
213
+ #define LM_GGML_FILE_VERSION 2
214
+
215
+ #define LM_GGML_QNT_VERSION 2 // bump this on quantization format changes
216
+ #define LM_GGML_QNT_VERSION_FACTOR 1000 // do not change this
217
+
218
+ #define LM_GGML_MAX_DIMS 4
219
+ #define LM_GGML_MAX_PARAMS 2048
220
+ #define LM_GGML_MAX_SRC 10
221
+ #define LM_GGML_MAX_N_THREADS 512
222
+ #define LM_GGML_MAX_OP_PARAMS 64
223
+
224
+ #ifndef LM_GGML_MAX_NAME
225
+ # define LM_GGML_MAX_NAME 64
226
+ #endif
227
+
228
+ #define LM_GGML_DEFAULT_N_THREADS 4
229
+ #define LM_GGML_DEFAULT_GRAPH_SIZE 2048
230
+
231
+ #if UINTPTR_MAX == 0xFFFFFFFF
232
+ #define LM_GGML_MEM_ALIGN 4
233
+ #else
234
+ #define LM_GGML_MEM_ALIGN 16
235
+ #endif
236
+
237
+ #define LM_GGML_EXIT_SUCCESS 0
238
+ #define LM_GGML_EXIT_ABORTED 1
239
+
240
+ #define LM_GGML_ROPE_TYPE_NEOX 2
241
+ #define LM_GGML_ROPE_TYPE_MROPE 8
242
+ #define LM_GGML_ROPE_TYPE_VISION 24
243
+
244
+ #define LM_GGML_MROPE_SECTIONS 4
245
+
246
+ #define LM_GGML_UNUSED(x) (void)(x)
247
+ #ifdef __CUDACC__
248
+ template<typename... Args>
249
+ __host__ __device__ constexpr inline void lm_ggml_unused_vars_impl(Args&&...) noexcept {}
250
+ #define LM_GGML_UNUSED_VARS(...) lm_ggml_unused_vars_impl(__VA_ARGS__)
251
+ #else
252
+ #define LM_GGML_UNUSED_VARS(...) do { (void)sizeof((__VA_ARGS__, 0)); } while(0)
253
+ #endif // __CUDACC__
254
+
255
+ #define LM_GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
256
+
257
+ #ifndef NDEBUG
258
+ # define LM_GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
259
+ #elif defined(__GNUC__)
260
+ # define LM_GGML_UNREACHABLE() __builtin_unreachable()
261
+ #elif defined(_MSC_VER)
262
+ # define LM_GGML_UNREACHABLE() __assume(0)
263
+ #else
264
+ # define LM_GGML_UNREACHABLE() ((void) 0)
265
+ #endif
266
+
267
+ #ifdef __cplusplus
268
+ # define LM_GGML_NORETURN [[noreturn]]
269
+ #elif defined(_MSC_VER)
270
+ # define LM_GGML_NORETURN __declspec(noreturn)
271
+ #else
272
+ # define LM_GGML_NORETURN _Noreturn
273
+ #endif
274
+
275
+ #define LM_GGML_ABORT(...) lm_ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
276
+ #define LM_GGML_ASSERT(x) if (!(x)) LM_GGML_ABORT("LM_GGML_ASSERT(%s) failed", #x)
277
+
278
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
279
+ // main purpose is to reduce code duplication and improve readability.
280
+ //
281
+ // example:
282
+ //
283
+ // LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
284
+ // LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
285
+ //
286
+ #define LM_GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
287
+ const type prefix##0 = (pointer)->array[0]; \
288
+ LM_GGML_UNUSED(prefix##0);
289
+ #define LM_GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
290
+ LM_GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
291
+ const type prefix##1 = (pointer)->array[1]; \
292
+ LM_GGML_UNUSED(prefix##1);
293
+ #define LM_GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
294
+ LM_GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
295
+ const type prefix##2 = (pointer)->array[2]; \
296
+ LM_GGML_UNUSED(prefix##2);
297
+ #define LM_GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
298
+ LM_GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
299
+ const type prefix##3 = (pointer)->array[3]; \
300
+ LM_GGML_UNUSED(prefix##3);
301
+
302
+ #define LM_GGML_TENSOR_UNARY_OP_LOCALS \
303
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
304
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, 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_LOCALS \
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
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
314
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
315
+
316
+ #define LM_GGML_TENSOR_TERNARY_OP_LOCALS \
317
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
318
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
319
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
320
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
321
+ LM_GGML_TENSOR_LOCALS(int64_t, ne2, src2, ne) \
322
+ LM_GGML_TENSOR_LOCALS(size_t, nb2, src2, nb) \
323
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
324
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
325
+
326
+ #define LM_GGML_TENSOR_BINARY_OP_LOCALS01 \
327
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
328
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
329
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
330
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
331
+
332
+ #ifdef __cplusplus
333
+ extern "C" {
334
+ #endif
335
+
336
+ // Function type used in fatal error callbacks
337
+ typedef void (*lm_ggml_abort_callback_t)(const char * error_message);
338
+
339
+ // Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
340
+ // Returns the old callback for chaining
341
+ LM_GGML_API lm_ggml_abort_callback_t lm_ggml_set_abort_callback(lm_ggml_abort_callback_t callback);
342
+
343
+ LM_GGML_NORETURN LM_GGML_ATTRIBUTE_FORMAT(3, 4)
344
+ LM_GGML_API void lm_ggml_abort(const char * file, int line, const char * fmt, ...);
345
+
346
+ enum lm_ggml_status {
347
+ LM_GGML_STATUS_ALLOC_FAILED = -2,
348
+ LM_GGML_STATUS_FAILED = -1,
349
+ LM_GGML_STATUS_SUCCESS = 0,
350
+ LM_GGML_STATUS_ABORTED = 1,
351
+ };
352
+
353
+ // get lm_ggml_status name string
354
+ LM_GGML_API const char * lm_ggml_status_to_string(enum lm_ggml_status status);
355
+
356
+ // ieee 754-2008 half-precision float16
357
+ // todo: make this not an integral type
358
+ typedef uint16_t lm_ggml_fp16_t;
359
+ LM_GGML_API float lm_ggml_fp16_to_fp32(lm_ggml_fp16_t);
360
+ LM_GGML_API lm_ggml_fp16_t lm_ggml_fp32_to_fp16(float);
361
+ LM_GGML_API void lm_ggml_fp16_to_fp32_row(const lm_ggml_fp16_t *, float *, int64_t);
362
+ LM_GGML_API void lm_ggml_fp32_to_fp16_row(const float *, lm_ggml_fp16_t *, int64_t);
363
+
364
+ // google brain half-precision bfloat16
365
+ typedef struct { uint16_t bits; } lm_ggml_bf16_t;
366
+ LM_GGML_API lm_ggml_bf16_t lm_ggml_fp32_to_bf16(float);
367
+ LM_GGML_API float lm_ggml_bf16_to_fp32(lm_ggml_bf16_t); // consider just doing << 16
368
+ LM_GGML_API void lm_ggml_bf16_to_fp32_row(const lm_ggml_bf16_t *, float *, int64_t);
369
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row_ref(const float *, lm_ggml_bf16_t *, int64_t);
370
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row(const float *, lm_ggml_bf16_t *, int64_t);
371
+
372
+ struct lm_ggml_object;
373
+ struct lm_ggml_context;
374
+ struct lm_ggml_cgraph;
375
+
376
+ // NOTE: always add types at the end of the enum to keep backward compatibility
377
+ enum lm_ggml_type {
378
+ LM_GGML_TYPE_F32 = 0,
379
+ LM_GGML_TYPE_F16 = 1,
380
+ LM_GGML_TYPE_Q4_0 = 2,
381
+ LM_GGML_TYPE_Q4_1 = 3,
382
+ // LM_GGML_TYPE_Q4_2 = 4, support has been removed
383
+ // LM_GGML_TYPE_Q4_3 = 5, support has been removed
384
+ LM_GGML_TYPE_Q5_0 = 6,
385
+ LM_GGML_TYPE_Q5_1 = 7,
386
+ LM_GGML_TYPE_Q8_0 = 8,
387
+ LM_GGML_TYPE_Q8_1 = 9,
388
+ LM_GGML_TYPE_Q2_K = 10,
389
+ LM_GGML_TYPE_Q3_K = 11,
390
+ LM_GGML_TYPE_Q4_K = 12,
391
+ LM_GGML_TYPE_Q5_K = 13,
392
+ LM_GGML_TYPE_Q6_K = 14,
393
+ LM_GGML_TYPE_Q8_K = 15,
394
+ LM_GGML_TYPE_IQ2_XXS = 16,
395
+ LM_GGML_TYPE_IQ2_XS = 17,
396
+ LM_GGML_TYPE_IQ3_XXS = 18,
397
+ LM_GGML_TYPE_IQ1_S = 19,
398
+ LM_GGML_TYPE_IQ4_NL = 20,
399
+ LM_GGML_TYPE_IQ3_S = 21,
400
+ LM_GGML_TYPE_IQ2_S = 22,
401
+ LM_GGML_TYPE_IQ4_XS = 23,
402
+ LM_GGML_TYPE_I8 = 24,
403
+ LM_GGML_TYPE_I16 = 25,
404
+ LM_GGML_TYPE_I32 = 26,
405
+ LM_GGML_TYPE_I64 = 27,
406
+ LM_GGML_TYPE_F64 = 28,
407
+ LM_GGML_TYPE_IQ1_M = 29,
408
+ LM_GGML_TYPE_BF16 = 30,
409
+ // LM_GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
410
+ // LM_GGML_TYPE_Q4_0_4_8 = 32,
411
+ // LM_GGML_TYPE_Q4_0_8_8 = 33,
412
+ LM_GGML_TYPE_TQ1_0 = 34,
413
+ LM_GGML_TYPE_TQ2_0 = 35,
414
+ // LM_GGML_TYPE_IQ4_NL_4_4 = 36,
415
+ // LM_GGML_TYPE_IQ4_NL_4_8 = 37,
416
+ // LM_GGML_TYPE_IQ4_NL_8_8 = 38,
417
+ LM_GGML_TYPE_MXFP4 = 39, // MXFP4 (1 block)
418
+ LM_GGML_TYPE_COUNT = 40,
419
+ };
420
+
421
+ // precision
422
+ enum lm_ggml_prec {
423
+ LM_GGML_PREC_DEFAULT = 0, // stored as lm_ggml_tensor.op_params, 0 by default
424
+ LM_GGML_PREC_F32 = 10,
425
+ };
426
+
427
+ // model file types
428
+ enum lm_ggml_ftype {
429
+ LM_GGML_FTYPE_UNKNOWN = -1,
430
+ LM_GGML_FTYPE_ALL_F32 = 0,
431
+ LM_GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
432
+ LM_GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
433
+ LM_GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
434
+ LM_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
435
+ LM_GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
436
+ LM_GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
437
+ LM_GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
438
+ LM_GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
439
+ LM_GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
440
+ LM_GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
441
+ LM_GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
442
+ LM_GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
443
+ LM_GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
444
+ LM_GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
445
+ LM_GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
446
+ LM_GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
447
+ LM_GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
448
+ LM_GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
449
+ LM_GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
450
+ LM_GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
451
+ LM_GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
452
+ LM_GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
453
+ LM_GGML_FTYPE_MOSTLY_MXFP4 = 25, // except 1d tensors
454
+ };
455
+
456
+ // available tensor operations:
457
+ enum lm_ggml_op {
458
+ LM_GGML_OP_NONE = 0,
459
+
460
+ LM_GGML_OP_DUP,
461
+ LM_GGML_OP_ADD,
462
+ LM_GGML_OP_ADD_ID,
463
+ LM_GGML_OP_ADD1,
464
+ LM_GGML_OP_ACC,
465
+ LM_GGML_OP_SUB,
466
+ LM_GGML_OP_MUL,
467
+ LM_GGML_OP_DIV,
468
+ LM_GGML_OP_SQR,
469
+ LM_GGML_OP_SQRT,
470
+ LM_GGML_OP_LOG,
471
+ LM_GGML_OP_SIN,
472
+ LM_GGML_OP_COS,
473
+ LM_GGML_OP_SUM,
474
+ LM_GGML_OP_SUM_ROWS,
475
+ LM_GGML_OP_MEAN,
476
+ LM_GGML_OP_ARGMAX,
477
+ LM_GGML_OP_COUNT_EQUAL,
478
+ LM_GGML_OP_REPEAT,
479
+ LM_GGML_OP_REPEAT_BACK,
480
+ LM_GGML_OP_CONCAT,
481
+ LM_GGML_OP_SILU_BACK,
482
+ LM_GGML_OP_NORM, // normalize
483
+ LM_GGML_OP_RMS_NORM,
484
+ LM_GGML_OP_RMS_NORM_BACK,
485
+ LM_GGML_OP_GROUP_NORM,
486
+ LM_GGML_OP_L2_NORM,
487
+
488
+ LM_GGML_OP_MUL_MAT,
489
+ LM_GGML_OP_MUL_MAT_ID,
490
+ LM_GGML_OP_OUT_PROD,
491
+
492
+ LM_GGML_OP_SCALE,
493
+ LM_GGML_OP_SET,
494
+ LM_GGML_OP_CPY,
495
+ LM_GGML_OP_CONT,
496
+ LM_GGML_OP_RESHAPE,
497
+ LM_GGML_OP_VIEW,
498
+ LM_GGML_OP_PERMUTE,
499
+ LM_GGML_OP_TRANSPOSE,
500
+ LM_GGML_OP_GET_ROWS,
501
+ LM_GGML_OP_GET_ROWS_BACK,
502
+ LM_GGML_OP_SET_ROWS,
503
+ LM_GGML_OP_DIAG,
504
+ LM_GGML_OP_DIAG_MASK_INF,
505
+ LM_GGML_OP_DIAG_MASK_ZERO,
506
+ LM_GGML_OP_SOFT_MAX,
507
+ LM_GGML_OP_SOFT_MAX_BACK,
508
+ LM_GGML_OP_ROPE,
509
+ LM_GGML_OP_ROPE_BACK,
510
+ LM_GGML_OP_CLAMP,
511
+ LM_GGML_OP_CONV_TRANSPOSE_1D,
512
+ LM_GGML_OP_IM2COL,
513
+ LM_GGML_OP_IM2COL_BACK,
514
+ LM_GGML_OP_CONV_2D,
515
+ LM_GGML_OP_CONV_3D,
516
+ LM_GGML_OP_CONV_2D_DW,
517
+ LM_GGML_OP_CONV_TRANSPOSE_2D,
518
+ LM_GGML_OP_POOL_1D,
519
+ LM_GGML_OP_POOL_2D,
520
+ LM_GGML_OP_POOL_2D_BACK,
521
+ LM_GGML_OP_UPSCALE,
522
+ LM_GGML_OP_PAD,
523
+ LM_GGML_OP_PAD_REFLECT_1D,
524
+ LM_GGML_OP_ROLL,
525
+ LM_GGML_OP_ARANGE,
526
+ LM_GGML_OP_TIMESTEP_EMBEDDING,
527
+ LM_GGML_OP_ARGSORT,
528
+ LM_GGML_OP_LEAKY_RELU,
529
+
530
+ LM_GGML_OP_FLASH_ATTN_EXT,
531
+ LM_GGML_OP_FLASH_ATTN_BACK,
532
+ LM_GGML_OP_SSM_CONV,
533
+ LM_GGML_OP_SSM_SCAN,
534
+ LM_GGML_OP_WIN_PART,
535
+ LM_GGML_OP_WIN_UNPART,
536
+ LM_GGML_OP_GET_REL_POS,
537
+ LM_GGML_OP_ADD_REL_POS,
538
+ LM_GGML_OP_RWKV_WKV6,
539
+ LM_GGML_OP_GATED_LINEAR_ATTN,
540
+ LM_GGML_OP_RWKV_WKV7,
541
+
542
+ LM_GGML_OP_UNARY,
543
+
544
+ LM_GGML_OP_MAP_CUSTOM1,
545
+ LM_GGML_OP_MAP_CUSTOM2,
546
+ LM_GGML_OP_MAP_CUSTOM3,
547
+
548
+ LM_GGML_OP_CUSTOM,
549
+
550
+ LM_GGML_OP_CROSS_ENTROPY_LOSS,
551
+ LM_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
552
+ LM_GGML_OP_OPT_STEP_ADAMW,
553
+ LM_GGML_OP_OPT_STEP_SGD,
554
+
555
+ LM_GGML_OP_GLU,
556
+
557
+ LM_GGML_OP_COUNT,
558
+ };
559
+
560
+ enum lm_ggml_unary_op {
561
+ LM_GGML_UNARY_OP_ABS,
562
+ LM_GGML_UNARY_OP_SGN,
563
+ LM_GGML_UNARY_OP_NEG,
564
+ LM_GGML_UNARY_OP_STEP,
565
+ LM_GGML_UNARY_OP_TANH,
566
+ LM_GGML_UNARY_OP_ELU,
567
+ LM_GGML_UNARY_OP_RELU,
568
+ LM_GGML_UNARY_OP_SIGMOID,
569
+ LM_GGML_UNARY_OP_GELU,
570
+ LM_GGML_UNARY_OP_GELU_QUICK,
571
+ LM_GGML_UNARY_OP_SILU,
572
+ LM_GGML_UNARY_OP_HARDSWISH,
573
+ LM_GGML_UNARY_OP_HARDSIGMOID,
574
+ LM_GGML_UNARY_OP_EXP,
575
+ LM_GGML_UNARY_OP_GELU_ERF,
576
+
577
+ LM_GGML_UNARY_OP_COUNT,
578
+ };
579
+
580
+ enum lm_ggml_glu_op {
581
+ LM_GGML_GLU_OP_REGLU,
582
+ LM_GGML_GLU_OP_GEGLU,
583
+ LM_GGML_GLU_OP_SWIGLU,
584
+ LM_GGML_GLU_OP_SWIGLU_OAI,
585
+ LM_GGML_GLU_OP_GEGLU_ERF,
586
+ LM_GGML_GLU_OP_GEGLU_QUICK,
587
+
588
+ LM_GGML_GLU_OP_COUNT,
589
+ };
590
+
591
+ enum lm_ggml_object_type {
592
+ LM_GGML_OBJECT_TYPE_TENSOR,
593
+ LM_GGML_OBJECT_TYPE_GRAPH,
594
+ LM_GGML_OBJECT_TYPE_WORK_BUFFER
595
+ };
596
+
597
+ enum lm_ggml_log_level {
598
+ LM_GGML_LOG_LEVEL_NONE = 0,
599
+ LM_GGML_LOG_LEVEL_DEBUG = 1,
600
+ LM_GGML_LOG_LEVEL_INFO = 2,
601
+ LM_GGML_LOG_LEVEL_WARN = 3,
602
+ LM_GGML_LOG_LEVEL_ERROR = 4,
603
+ LM_GGML_LOG_LEVEL_CONT = 5, // continue previous log
604
+ };
605
+
606
+ // this tensor...
607
+ enum lm_ggml_tensor_flag {
608
+ LM_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
609
+ LM_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
610
+ LM_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
611
+ LM_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
612
+ };
613
+
614
+ struct lm_ggml_init_params {
615
+ // memory pool
616
+ size_t mem_size; // bytes
617
+ void * mem_buffer; // if NULL, memory will be allocated internally
618
+ bool no_alloc; // don't allocate memory for the tensor data
619
+ };
620
+
621
+ // n-dimensional tensor
622
+ struct lm_ggml_tensor {
623
+ enum lm_ggml_type type;
624
+
625
+ struct lm_ggml_backend_buffer * buffer;
626
+
627
+ int64_t ne[LM_GGML_MAX_DIMS]; // number of elements
628
+ size_t nb[LM_GGML_MAX_DIMS]; // stride in bytes:
629
+ // nb[0] = lm_ggml_type_size(type)
630
+ // nb[1] = nb[0] * (ne[0] / lm_ggml_blck_size(type)) + padding
631
+ // nb[i] = nb[i-1] * ne[i-1]
632
+
633
+ // compute data
634
+ enum lm_ggml_op op;
635
+
636
+ // op params - allocated as int32_t for alignment
637
+ int32_t op_params[LM_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
638
+
639
+ int32_t flags;
640
+
641
+ struct lm_ggml_tensor * src[LM_GGML_MAX_SRC];
642
+
643
+ // source tensor and offset for views
644
+ struct lm_ggml_tensor * view_src;
645
+ size_t view_offs;
646
+
647
+ void * data;
648
+
649
+ char name[LM_GGML_MAX_NAME];
650
+
651
+ void * extra; // extra things e.g. for ggml-cuda.cu
652
+
653
+ char padding[8];
654
+ };
655
+
656
+ static const size_t LM_GGML_TENSOR_SIZE = sizeof(struct lm_ggml_tensor);
657
+
658
+ // Abort callback
659
+ // If not NULL, called before ggml computation
660
+ // If it returns true, the computation is aborted
661
+ typedef bool (*lm_ggml_abort_callback)(void * data);
662
+
663
+
664
+ //
665
+ // GUID
666
+ //
667
+
668
+ // GUID types
669
+ typedef uint8_t lm_ggml_guid[16];
670
+ typedef lm_ggml_guid * lm_ggml_guid_t;
671
+
672
+ LM_GGML_API bool lm_ggml_guid_matches(lm_ggml_guid_t guid_a, lm_ggml_guid_t guid_b);
673
+
674
+ // misc
675
+
676
+ LM_GGML_API const char * lm_ggml_version(void);
677
+ LM_GGML_API const char * lm_ggml_commit(void);
678
+
679
+ LM_GGML_API void lm_ggml_time_init(void); // call this once at the beginning of the program
680
+ LM_GGML_API int64_t lm_ggml_time_ms(void);
681
+ LM_GGML_API int64_t lm_ggml_time_us(void);
682
+ LM_GGML_API int64_t lm_ggml_cycles(void);
683
+ LM_GGML_API int64_t lm_ggml_cycles_per_ms(void);
684
+
685
+ // accepts a UTF-8 path, even on Windows
686
+ LM_GGML_API FILE * lm_ggml_fopen(const char * fname, const char * mode);
687
+
688
+ LM_GGML_API void lm_ggml_print_object (const struct lm_ggml_object * obj);
689
+ LM_GGML_API void lm_ggml_print_objects(const struct lm_ggml_context * ctx);
690
+
691
+ LM_GGML_API int64_t lm_ggml_nelements (const struct lm_ggml_tensor * tensor);
692
+ LM_GGML_API int64_t lm_ggml_nrows (const struct lm_ggml_tensor * tensor);
693
+ LM_GGML_API size_t lm_ggml_nbytes (const struct lm_ggml_tensor * tensor);
694
+ 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
695
+
696
+ LM_GGML_API int64_t lm_ggml_blck_size(enum lm_ggml_type type);
697
+ LM_GGML_API size_t lm_ggml_type_size(enum lm_ggml_type type); // size in bytes for all elements in a block
698
+ 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
699
+
700
+ LM_GGML_DEPRECATED(
701
+ LM_GGML_API double lm_ggml_type_sizef(enum lm_ggml_type type), // lm_ggml_type_size()/lm_ggml_blck_size() as float
702
+ "use lm_ggml_row_size() instead");
703
+
704
+ LM_GGML_API const char * lm_ggml_type_name(enum lm_ggml_type type);
705
+ LM_GGML_API const char * lm_ggml_op_name (enum lm_ggml_op op);
706
+ LM_GGML_API const char * lm_ggml_op_symbol(enum lm_ggml_op op);
707
+
708
+ LM_GGML_API const char * lm_ggml_unary_op_name(enum lm_ggml_unary_op op);
709
+ LM_GGML_API const char * lm_ggml_glu_op_name(enum lm_ggml_glu_op op);
710
+ LM_GGML_API const char * lm_ggml_op_desc(const struct lm_ggml_tensor * t); // unary or op name
711
+
712
+ LM_GGML_API size_t lm_ggml_element_size(const struct lm_ggml_tensor * tensor);
713
+
714
+ LM_GGML_API bool lm_ggml_is_quantized(enum lm_ggml_type type);
715
+
716
+ // TODO: temporary until model loading of ggml examples is refactored
717
+ LM_GGML_API enum lm_ggml_type lm_ggml_ftype_to_lm_ggml_type(enum lm_ggml_ftype ftype);
718
+
719
+ LM_GGML_API bool lm_ggml_is_transposed(const struct lm_ggml_tensor * tensor);
720
+ LM_GGML_API bool lm_ggml_is_permuted (const struct lm_ggml_tensor * tensor);
721
+ LM_GGML_API bool lm_ggml_is_empty (const struct lm_ggml_tensor * tensor);
722
+ LM_GGML_API bool lm_ggml_is_scalar (const struct lm_ggml_tensor * tensor);
723
+ LM_GGML_API bool lm_ggml_is_vector (const struct lm_ggml_tensor * tensor);
724
+ LM_GGML_API bool lm_ggml_is_matrix (const struct lm_ggml_tensor * tensor);
725
+ LM_GGML_API bool lm_ggml_is_3d (const struct lm_ggml_tensor * tensor);
726
+ LM_GGML_API int lm_ggml_n_dims (const struct lm_ggml_tensor * tensor); // returns 1 for scalars
727
+
728
+ // returns whether the tensor elements can be iterated over with a flattened index (no gaps, no permutation)
729
+ LM_GGML_API bool lm_ggml_is_contiguous (const struct lm_ggml_tensor * tensor);
730
+ LM_GGML_API bool lm_ggml_is_contiguous_0(const struct lm_ggml_tensor * tensor); // same as lm_ggml_is_contiguous()
731
+ LM_GGML_API bool lm_ggml_is_contiguous_1(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 1
732
+ LM_GGML_API bool lm_ggml_is_contiguous_2(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 2
733
+
734
+ // returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok)
735
+ LM_GGML_API bool lm_ggml_is_contiguously_allocated(const struct lm_ggml_tensor * tensor);
736
+
737
+ // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN
738
+ LM_GGML_API bool lm_ggml_is_contiguous_channels(const struct lm_ggml_tensor * tensor);
739
+
740
+ // true if the elements in dimension 0 are contiguous, or there is just 1 block of elements
741
+ LM_GGML_API bool lm_ggml_is_contiguous_rows(const struct lm_ggml_tensor * tensor);
742
+
743
+ LM_GGML_API bool lm_ggml_are_same_shape (const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
744
+ LM_GGML_API bool lm_ggml_are_same_stride(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
745
+
746
+ LM_GGML_API bool lm_ggml_can_repeat(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
747
+
748
+ // use this to compute the memory overhead of a tensor
749
+ LM_GGML_API size_t lm_ggml_tensor_overhead(void);
750
+
751
+ LM_GGML_API bool lm_ggml_validate_row_data(enum lm_ggml_type type, const void * data, size_t nbytes);
752
+
753
+ // main
754
+
755
+ LM_GGML_API struct lm_ggml_context * lm_ggml_init (struct lm_ggml_init_params params);
756
+ LM_GGML_API void lm_ggml_reset(struct lm_ggml_context * ctx);
757
+ LM_GGML_API void lm_ggml_free (struct lm_ggml_context * ctx);
758
+
759
+ LM_GGML_API size_t lm_ggml_used_mem(const struct lm_ggml_context * ctx);
760
+
761
+ LM_GGML_API bool lm_ggml_get_no_alloc(struct lm_ggml_context * ctx);
762
+ LM_GGML_API void lm_ggml_set_no_alloc(struct lm_ggml_context * ctx, bool no_alloc);
763
+
764
+ LM_GGML_API void * lm_ggml_get_mem_buffer (const struct lm_ggml_context * ctx);
765
+ LM_GGML_API size_t lm_ggml_get_mem_size (const struct lm_ggml_context * ctx);
766
+ LM_GGML_API size_t lm_ggml_get_max_tensor_size(const struct lm_ggml_context * ctx);
767
+
768
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor(
769
+ struct lm_ggml_context * ctx,
770
+ enum lm_ggml_type type,
771
+ int n_dims,
772
+ const int64_t *ne);
773
+
774
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_1d(
775
+ struct lm_ggml_context * ctx,
776
+ enum lm_ggml_type type,
777
+ int64_t ne0);
778
+
779
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_2d(
780
+ struct lm_ggml_context * ctx,
781
+ enum lm_ggml_type type,
782
+ int64_t ne0,
783
+ int64_t ne1);
784
+
785
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_3d(
786
+ struct lm_ggml_context * ctx,
787
+ enum lm_ggml_type type,
788
+ int64_t ne0,
789
+ int64_t ne1,
790
+ int64_t ne2);
791
+
792
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_4d(
793
+ struct lm_ggml_context * ctx,
794
+ enum lm_ggml_type type,
795
+ int64_t ne0,
796
+ int64_t ne1,
797
+ int64_t ne2,
798
+ int64_t ne3);
799
+
800
+ LM_GGML_API void * lm_ggml_new_buffer(struct lm_ggml_context * ctx, size_t nbytes);
801
+
802
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_tensor (struct lm_ggml_context * ctx, const struct lm_ggml_tensor * src);
803
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_tensor(struct lm_ggml_context * ctx, struct lm_ggml_tensor * src);
804
+
805
+ // Context tensor enumeration and lookup
806
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_first_tensor(const struct lm_ggml_context * ctx);
807
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_next_tensor (const struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
808
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_tensor(struct lm_ggml_context * ctx, const char * name);
809
+
810
+ // Converts a flat index into coordinates
811
+ 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);
812
+
813
+ LM_GGML_API enum lm_ggml_unary_op lm_ggml_get_unary_op(const struct lm_ggml_tensor * tensor);
814
+ LM_GGML_API enum lm_ggml_glu_op lm_ggml_get_glu_op(const struct lm_ggml_tensor * tensor);
815
+
816
+ LM_GGML_API void * lm_ggml_get_data (const struct lm_ggml_tensor * tensor);
817
+ LM_GGML_API float * lm_ggml_get_data_f32(const struct lm_ggml_tensor * tensor);
818
+
819
+ LM_GGML_API const char * lm_ggml_get_name (const struct lm_ggml_tensor * tensor);
820
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_name ( struct lm_ggml_tensor * tensor, const char * name);
821
+ LM_GGML_ATTRIBUTE_FORMAT(2, 3)
822
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_format_name( struct lm_ggml_tensor * tensor, const char * fmt, ...);
823
+
824
+ // Tensor flags
825
+ LM_GGML_API void lm_ggml_set_input(struct lm_ggml_tensor * tensor);
826
+ LM_GGML_API void lm_ggml_set_output(struct lm_ggml_tensor * tensor);
827
+ LM_GGML_API void lm_ggml_set_param(struct lm_ggml_tensor * tensor);
828
+ LM_GGML_API void lm_ggml_set_loss(struct lm_ggml_tensor * tensor);
829
+
830
+ //
831
+ // operations on tensors with backpropagation
832
+ //
833
+
834
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup(
835
+ struct lm_ggml_context * ctx,
836
+ struct lm_ggml_tensor * a);
837
+
838
+ // in-place, returns view(a)
839
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_inplace(
840
+ struct lm_ggml_context * ctx,
841
+ struct lm_ggml_tensor * a);
842
+
843
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add(
844
+ struct lm_ggml_context * ctx,
845
+ struct lm_ggml_tensor * a,
846
+ struct lm_ggml_tensor * b);
847
+
848
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_inplace(
849
+ struct lm_ggml_context * ctx,
850
+ struct lm_ggml_tensor * a,
851
+ struct lm_ggml_tensor * b);
852
+
853
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_cast(
854
+ struct lm_ggml_context * ctx,
855
+ struct lm_ggml_tensor * a,
856
+ struct lm_ggml_tensor * b,
857
+ enum lm_ggml_type type);
858
+
859
+ // dst[i0, i1, i2] = a[i0, i1, i2] + b[i0, ids[i1, i2]]
860
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_id(
861
+ struct lm_ggml_context * ctx,
862
+ struct lm_ggml_tensor * a,
863
+ struct lm_ggml_tensor * b,
864
+ struct lm_ggml_tensor * ids);
865
+
866
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1(
867
+ struct lm_ggml_context * ctx,
868
+ struct lm_ggml_tensor * a,
869
+ struct lm_ggml_tensor * b);
870
+
871
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1_inplace(
872
+ struct lm_ggml_context * ctx,
873
+ struct lm_ggml_tensor * a,
874
+ struct lm_ggml_tensor * b);
875
+
876
+ // dst = a
877
+ // view(dst, nb1, nb2, nb3, offset) += b
878
+ // return dst
879
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc(
880
+ struct lm_ggml_context * ctx,
881
+ struct lm_ggml_tensor * a,
882
+ struct lm_ggml_tensor * b,
883
+ size_t nb1,
884
+ size_t nb2,
885
+ size_t nb3,
886
+ size_t offset);
887
+
888
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc_inplace(
889
+ struct lm_ggml_context * ctx,
890
+ struct lm_ggml_tensor * a,
891
+ struct lm_ggml_tensor * b,
892
+ size_t nb1,
893
+ size_t nb2,
894
+ size_t nb3,
895
+ size_t offset);
896
+
897
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub(
898
+ struct lm_ggml_context * ctx,
899
+ struct lm_ggml_tensor * a,
900
+ struct lm_ggml_tensor * b);
901
+
902
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub_inplace(
903
+ struct lm_ggml_context * ctx,
904
+ struct lm_ggml_tensor * a,
905
+ struct lm_ggml_tensor * b);
906
+
907
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul(
908
+ struct lm_ggml_context * ctx,
909
+ struct lm_ggml_tensor * a,
910
+ struct lm_ggml_tensor * b);
911
+
912
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_inplace(
913
+ struct lm_ggml_context * ctx,
914
+ struct lm_ggml_tensor * a,
915
+ struct lm_ggml_tensor * b);
916
+
917
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div(
918
+ struct lm_ggml_context * ctx,
919
+ struct lm_ggml_tensor * a,
920
+ struct lm_ggml_tensor * b);
921
+
922
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div_inplace(
923
+ struct lm_ggml_context * ctx,
924
+ struct lm_ggml_tensor * a,
925
+ struct lm_ggml_tensor * b);
926
+
927
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr(
928
+ struct lm_ggml_context * ctx,
929
+ struct lm_ggml_tensor * a);
930
+
931
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr_inplace(
932
+ struct lm_ggml_context * ctx,
933
+ struct lm_ggml_tensor * a);
934
+
935
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt(
936
+ struct lm_ggml_context * ctx,
937
+ struct lm_ggml_tensor * a);
938
+
939
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt_inplace(
940
+ struct lm_ggml_context * ctx,
941
+ struct lm_ggml_tensor * a);
942
+
943
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log(
944
+ struct lm_ggml_context * ctx,
945
+ struct lm_ggml_tensor * a);
946
+
947
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log_inplace(
948
+ struct lm_ggml_context * ctx,
949
+ struct lm_ggml_tensor * a);
950
+
951
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin(
952
+ struct lm_ggml_context * ctx,
953
+ struct lm_ggml_tensor * a);
954
+
955
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin_inplace(
956
+ struct lm_ggml_context * ctx,
957
+ struct lm_ggml_tensor * a);
958
+
959
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos(
960
+ struct lm_ggml_context * ctx,
961
+ struct lm_ggml_tensor * a);
962
+
963
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos_inplace(
964
+ struct lm_ggml_context * ctx,
965
+ struct lm_ggml_tensor * a);
966
+
967
+ // return scalar
968
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum(
969
+ struct lm_ggml_context * ctx,
970
+ struct lm_ggml_tensor * a);
971
+
972
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
973
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum_rows(
974
+ struct lm_ggml_context * ctx,
975
+ struct lm_ggml_tensor * a);
976
+
977
+ // mean along rows
978
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mean(
979
+ struct lm_ggml_context * ctx,
980
+ struct lm_ggml_tensor * a);
981
+
982
+ // argmax along rows
983
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argmax(
984
+ struct lm_ggml_context * ctx,
985
+ struct lm_ggml_tensor * a);
986
+
987
+ // count number of equal elements in a and b
988
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_count_equal(
989
+ struct lm_ggml_context * ctx,
990
+ struct lm_ggml_tensor * a,
991
+ struct lm_ggml_tensor * b);
992
+
993
+ // if a is the same shape as b, and a is not parameter, return a
994
+ // otherwise, return a new tensor: repeat(a) to fit in b
995
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat(
996
+ struct lm_ggml_context * ctx,
997
+ struct lm_ggml_tensor * a,
998
+ struct lm_ggml_tensor * b);
999
+
1000
+ // repeat a to the specified shape
1001
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_4d(
1002
+ struct lm_ggml_context * ctx,
1003
+ struct lm_ggml_tensor * a,
1004
+ int64_t ne0,
1005
+ int64_t ne1,
1006
+ int64_t ne2,
1007
+ int64_t ne3);
1008
+
1009
+ // sums repetitions in a into shape of b
1010
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_back(
1011
+ struct lm_ggml_context * ctx,
1012
+ struct lm_ggml_tensor * a,
1013
+ struct lm_ggml_tensor * b); // sum up values that are adjacent in dims > 0 instead of repeated with same stride
1014
+
1015
+ // concat a and b along dim
1016
+ // used in stable-diffusion
1017
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_concat(
1018
+ struct lm_ggml_context * ctx,
1019
+ struct lm_ggml_tensor * a,
1020
+ struct lm_ggml_tensor * b,
1021
+ int dim);
1022
+
1023
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs(
1024
+ struct lm_ggml_context * ctx,
1025
+ struct lm_ggml_tensor * a);
1026
+
1027
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs_inplace(
1028
+ struct lm_ggml_context * ctx,
1029
+ struct lm_ggml_tensor * a);
1030
+
1031
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn(
1032
+ struct lm_ggml_context * ctx,
1033
+ struct lm_ggml_tensor * a);
1034
+
1035
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn_inplace(
1036
+ struct lm_ggml_context * ctx,
1037
+ struct lm_ggml_tensor * a);
1038
+
1039
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg(
1040
+ struct lm_ggml_context * ctx,
1041
+ struct lm_ggml_tensor * a);
1042
+
1043
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg_inplace(
1044
+ struct lm_ggml_context * ctx,
1045
+ struct lm_ggml_tensor * a);
1046
+
1047
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step(
1048
+ struct lm_ggml_context * ctx,
1049
+ struct lm_ggml_tensor * a);
1050
+
1051
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step_inplace(
1052
+ struct lm_ggml_context * ctx,
1053
+ struct lm_ggml_tensor * a);
1054
+
1055
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh(
1056
+ struct lm_ggml_context * ctx,
1057
+ struct lm_ggml_tensor * a);
1058
+
1059
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh_inplace(
1060
+ struct lm_ggml_context * ctx,
1061
+ struct lm_ggml_tensor * a);
1062
+
1063
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu(
1064
+ struct lm_ggml_context * ctx,
1065
+ struct lm_ggml_tensor * a);
1066
+
1067
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu_inplace(
1068
+ struct lm_ggml_context * ctx,
1069
+ struct lm_ggml_tensor * a);
1070
+
1071
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu(
1072
+ struct lm_ggml_context * ctx,
1073
+ struct lm_ggml_tensor * a);
1074
+
1075
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_leaky_relu(
1076
+ struct lm_ggml_context * ctx,
1077
+ struct lm_ggml_tensor * a, float negative_slope, bool inplace);
1078
+
1079
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu_inplace(
1080
+ struct lm_ggml_context * ctx,
1081
+ struct lm_ggml_tensor * a);
1082
+
1083
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid(
1084
+ struct lm_ggml_context * ctx,
1085
+ struct lm_ggml_tensor * a);
1086
+
1087
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid_inplace(
1088
+ struct lm_ggml_context * ctx,
1089
+ struct lm_ggml_tensor * a);
1090
+
1091
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu(
1092
+ struct lm_ggml_context * ctx,
1093
+ struct lm_ggml_tensor * a);
1094
+
1095
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_inplace(
1096
+ struct lm_ggml_context * ctx,
1097
+ struct lm_ggml_tensor * a);
1098
+
1099
+ // GELU using erf (error function) when possible
1100
+ // some backends may fallback to approximation based on Abramowitz and Stegun formula
1101
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_erf(
1102
+ struct lm_ggml_context * ctx,
1103
+ struct lm_ggml_tensor * a);
1104
+
1105
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_erf_inplace(
1106
+ struct lm_ggml_context * ctx,
1107
+ struct lm_ggml_tensor * a);
1108
+
1109
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick(
1110
+ struct lm_ggml_context * ctx,
1111
+ struct lm_ggml_tensor * a);
1112
+
1113
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick_inplace(
1114
+ struct lm_ggml_context * ctx,
1115
+ struct lm_ggml_tensor * a);
1116
+
1117
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu(
1118
+ struct lm_ggml_context * ctx,
1119
+ struct lm_ggml_tensor * a);
1120
+
1121
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_inplace(
1122
+ struct lm_ggml_context * ctx,
1123
+ struct lm_ggml_tensor * a);
1124
+
1125
+ // a - x
1126
+ // b - dy
1127
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_back(
1128
+ struct lm_ggml_context * ctx,
1129
+ struct lm_ggml_tensor * a,
1130
+ struct lm_ggml_tensor * b);
1131
+
1132
+ // hardswish(x) = x * relu6(x + 3) / 6
1133
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardswish(
1134
+ struct lm_ggml_context * ctx,
1135
+ struct lm_ggml_tensor * a);
1136
+
1137
+ // hardsigmoid(x) = relu6(x + 3) / 6
1138
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardsigmoid(
1139
+ struct lm_ggml_context * ctx,
1140
+ struct lm_ggml_tensor * a);
1141
+
1142
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp(
1143
+ struct lm_ggml_context * ctx,
1144
+ struct lm_ggml_tensor * a);
1145
+
1146
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp_inplace(
1147
+ struct lm_ggml_context * ctx,
1148
+ struct lm_ggml_tensor * a);
1149
+
1150
+ // gated linear unit ops
1151
+ // A: n columns, r rows,
1152
+ // result is n / 2 columns, r rows,
1153
+ // expects gate in second half of row, unless swapped is true
1154
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_glu(
1155
+ struct lm_ggml_context * ctx,
1156
+ struct lm_ggml_tensor * a,
1157
+ enum lm_ggml_glu_op op,
1158
+ bool swapped);
1159
+
1160
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reglu(
1161
+ struct lm_ggml_context * ctx,
1162
+ struct lm_ggml_tensor * a);
1163
+
1164
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reglu_swapped(
1165
+ struct lm_ggml_context * ctx,
1166
+ struct lm_ggml_tensor * a);
1167
+
1168
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu(
1169
+ struct lm_ggml_context * ctx,
1170
+ struct lm_ggml_tensor * a);
1171
+
1172
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_swapped(
1173
+ struct lm_ggml_context * ctx,
1174
+ struct lm_ggml_tensor * a);
1175
+
1176
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_swiglu(
1177
+ struct lm_ggml_context * ctx,
1178
+ struct lm_ggml_tensor * a);
1179
+
1180
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_swiglu_swapped(
1181
+ struct lm_ggml_context * ctx,
1182
+ struct lm_ggml_tensor * a);
1183
+
1184
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_erf(
1185
+ struct lm_ggml_context * ctx,
1186
+ struct lm_ggml_tensor * a);
1187
+
1188
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_erf_swapped(
1189
+ struct lm_ggml_context * ctx,
1190
+ struct lm_ggml_tensor * a);
1191
+
1192
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_quick(
1193
+ struct lm_ggml_context * ctx,
1194
+ struct lm_ggml_tensor * a);
1195
+
1196
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_quick_swapped(
1197
+ struct lm_ggml_context * ctx,
1198
+ struct lm_ggml_tensor * a);
1199
+
1200
+ // A: n columns, r rows,
1201
+ // B: n columns, r rows,
1202
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_glu_split(
1203
+ struct lm_ggml_context * ctx,
1204
+ struct lm_ggml_tensor * a,
1205
+ struct lm_ggml_tensor * b,
1206
+ enum lm_ggml_glu_op op);
1207
+
1208
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reglu_split(
1209
+ struct lm_ggml_context * ctx,
1210
+ struct lm_ggml_tensor * a,
1211
+ struct lm_ggml_tensor * b);
1212
+
1213
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_split(
1214
+ struct lm_ggml_context * ctx,
1215
+ struct lm_ggml_tensor * a,
1216
+ struct lm_ggml_tensor * b);
1217
+
1218
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_swiglu_split(
1219
+ struct lm_ggml_context * ctx,
1220
+ struct lm_ggml_tensor * a,
1221
+ struct lm_ggml_tensor * b);
1222
+
1223
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_erf_split(
1224
+ struct lm_ggml_context * ctx,
1225
+ struct lm_ggml_tensor * a,
1226
+ struct lm_ggml_tensor * b);
1227
+
1228
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_geglu_quick_split(
1229
+ struct lm_ggml_context * ctx,
1230
+ struct lm_ggml_tensor * a,
1231
+ struct lm_ggml_tensor * b);
1232
+
1233
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_swiglu_oai(
1234
+ struct lm_ggml_context * ctx,
1235
+ struct lm_ggml_tensor * a,
1236
+ struct lm_ggml_tensor * b,
1237
+ float alpha,
1238
+ float limit);
1239
+
1240
+ // normalize along rows
1241
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm(
1242
+ struct lm_ggml_context * ctx,
1243
+ struct lm_ggml_tensor * a,
1244
+ float eps);
1245
+
1246
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm_inplace(
1247
+ struct lm_ggml_context * ctx,
1248
+ struct lm_ggml_tensor * a,
1249
+ float eps);
1250
+
1251
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm(
1252
+ struct lm_ggml_context * ctx,
1253
+ struct lm_ggml_tensor * a,
1254
+ float eps);
1255
+
1256
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_inplace(
1257
+ struct lm_ggml_context * ctx,
1258
+ struct lm_ggml_tensor * a,
1259
+ float eps);
1260
+
1261
+ // group normalize along ne0*ne1*n_groups
1262
+ // used in stable-diffusion
1263
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm(
1264
+ struct lm_ggml_context * ctx,
1265
+ struct lm_ggml_tensor * a,
1266
+ int n_groups,
1267
+ float eps);
1268
+
1269
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm_inplace(
1270
+ struct lm_ggml_context * ctx,
1271
+ struct lm_ggml_tensor * a,
1272
+ int n_groups,
1273
+ float eps);
1274
+
1275
+ // l2 normalize along rows
1276
+ // used in rwkv v7
1277
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm(
1278
+ struct lm_ggml_context * ctx,
1279
+ struct lm_ggml_tensor * a,
1280
+ float eps);
1281
+
1282
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_l2_norm_inplace(
1283
+ struct lm_ggml_context * ctx,
1284
+ struct lm_ggml_tensor * a,
1285
+ float eps);
1286
+
1287
+ // a - x
1288
+ // b - dy
1289
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_back(
1290
+ struct lm_ggml_context * ctx,
1291
+ struct lm_ggml_tensor * a,
1292
+ struct lm_ggml_tensor * b,
1293
+ float eps);
1294
+
1295
+ // A: k columns, n rows => [ne03, ne02, n, k]
1296
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1297
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1298
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat(
1299
+ struct lm_ggml_context * ctx,
1300
+ struct lm_ggml_tensor * a,
1301
+ struct lm_ggml_tensor * b);
1302
+
1303
+ // change the precision of a matrix multiplication
1304
+ // set to LM_GGML_PREC_F32 for higher precision (useful for phi-2)
1305
+ LM_GGML_API void lm_ggml_mul_mat_set_prec(
1306
+ struct lm_ggml_tensor * a,
1307
+ enum lm_ggml_prec prec);
1308
+
1309
+ // indirect matrix multiplication
1310
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat_id(
1311
+ struct lm_ggml_context * ctx,
1312
+ struct lm_ggml_tensor * as,
1313
+ struct lm_ggml_tensor * b,
1314
+ struct lm_ggml_tensor * ids);
1315
+
1316
+ // A: m columns, n rows,
1317
+ // B: p columns, n rows,
1318
+ // result is m columns, p rows
1319
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_out_prod(
1320
+ struct lm_ggml_context * ctx,
1321
+ struct lm_ggml_tensor * a,
1322
+ struct lm_ggml_tensor * b);
1323
+
1324
+ //
1325
+ // operations on tensors without backpropagation
1326
+ //
1327
+
1328
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale(
1329
+ struct lm_ggml_context * ctx,
1330
+ struct lm_ggml_tensor * a,
1331
+ float s);
1332
+
1333
+ // in-place, returns view(a)
1334
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_inplace(
1335
+ struct lm_ggml_context * ctx,
1336
+ struct lm_ggml_tensor * a,
1337
+ float s);
1338
+
1339
+ // x = s * a + b
1340
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_bias(
1341
+ struct lm_ggml_context * ctx,
1342
+ struct lm_ggml_tensor * a,
1343
+ float s,
1344
+ float b);
1345
+
1346
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_bias_inplace(
1347
+ struct lm_ggml_context * ctx,
1348
+ struct lm_ggml_tensor * a,
1349
+ float s,
1350
+ float b);
1351
+
1352
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1353
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set(
1354
+ struct lm_ggml_context * ctx,
1355
+ struct lm_ggml_tensor * a,
1356
+ struct lm_ggml_tensor * b,
1357
+ size_t nb1,
1358
+ size_t nb2,
1359
+ size_t nb3,
1360
+ size_t offset); // in bytes
1361
+
1362
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1363
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_inplace(
1364
+ struct lm_ggml_context * ctx,
1365
+ struct lm_ggml_tensor * a,
1366
+ struct lm_ggml_tensor * b,
1367
+ size_t nb1,
1368
+ size_t nb2,
1369
+ size_t nb3,
1370
+ size_t offset); // in bytes
1371
+
1372
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d(
1373
+ struct lm_ggml_context * ctx,
1374
+ struct lm_ggml_tensor * a,
1375
+ struct lm_ggml_tensor * b,
1376
+ size_t offset); // in bytes
1377
+
1378
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d_inplace(
1379
+ struct lm_ggml_context * ctx,
1380
+ struct lm_ggml_tensor * a,
1381
+ struct lm_ggml_tensor * b,
1382
+ size_t offset); // in bytes
1383
+
1384
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1385
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d(
1386
+ struct lm_ggml_context * ctx,
1387
+ struct lm_ggml_tensor * a,
1388
+ struct lm_ggml_tensor * b,
1389
+ size_t nb1,
1390
+ size_t offset); // in bytes
1391
+
1392
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1393
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d_inplace(
1394
+ struct lm_ggml_context * ctx,
1395
+ struct lm_ggml_tensor * a,
1396
+ struct lm_ggml_tensor * b,
1397
+ size_t nb1,
1398
+ size_t offset); // in bytes
1399
+
1400
+ // a -> b, return view(b)
1401
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cpy(
1402
+ struct lm_ggml_context * ctx,
1403
+ struct lm_ggml_tensor * a,
1404
+ struct lm_ggml_tensor * b);
1405
+
1406
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cast(
1407
+ struct lm_ggml_context * ctx,
1408
+ struct lm_ggml_tensor * a,
1409
+ enum lm_ggml_type type);
1410
+
1411
+ // make contiguous
1412
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont(
1413
+ struct lm_ggml_context * ctx,
1414
+ struct lm_ggml_tensor * a);
1415
+
1416
+ // make contiguous, with new shape
1417
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_1d(
1418
+ struct lm_ggml_context * ctx,
1419
+ struct lm_ggml_tensor * a,
1420
+ int64_t ne0);
1421
+
1422
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_2d(
1423
+ struct lm_ggml_context * ctx,
1424
+ struct lm_ggml_tensor * a,
1425
+ int64_t ne0,
1426
+ int64_t ne1);
1427
+
1428
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_3d(
1429
+ struct lm_ggml_context * ctx,
1430
+ struct lm_ggml_tensor * a,
1431
+ int64_t ne0,
1432
+ int64_t ne1,
1433
+ int64_t ne2);
1434
+
1435
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_4d(
1436
+ struct lm_ggml_context * ctx,
1437
+ struct lm_ggml_tensor * a,
1438
+ int64_t ne0,
1439
+ int64_t ne1,
1440
+ int64_t ne2,
1441
+ int64_t ne3);
1442
+
1443
+ // return view(a), b specifies the new shape
1444
+ // TODO: when we start computing gradient, make a copy instead of view
1445
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape(
1446
+ struct lm_ggml_context * ctx,
1447
+ struct lm_ggml_tensor * a,
1448
+ struct lm_ggml_tensor * b);
1449
+
1450
+ // return view(a)
1451
+ // TODO: when we start computing gradient, make a copy instead of view
1452
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_1d(
1453
+ struct lm_ggml_context * ctx,
1454
+ struct lm_ggml_tensor * a,
1455
+ int64_t ne0);
1456
+
1457
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_2d(
1458
+ struct lm_ggml_context * ctx,
1459
+ struct lm_ggml_tensor * a,
1460
+ int64_t ne0,
1461
+ int64_t ne1);
1462
+
1463
+ // return view(a)
1464
+ // TODO: when we start computing gradient, make a copy instead of view
1465
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_3d(
1466
+ struct lm_ggml_context * ctx,
1467
+ struct lm_ggml_tensor * a,
1468
+ int64_t ne0,
1469
+ int64_t ne1,
1470
+ int64_t ne2);
1471
+
1472
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_4d(
1473
+ struct lm_ggml_context * ctx,
1474
+ struct lm_ggml_tensor * a,
1475
+ int64_t ne0,
1476
+ int64_t ne1,
1477
+ int64_t ne2,
1478
+ int64_t ne3);
1479
+
1480
+ // offset in bytes
1481
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_1d(
1482
+ struct lm_ggml_context * ctx,
1483
+ struct lm_ggml_tensor * a,
1484
+ int64_t ne0,
1485
+ size_t offset);
1486
+
1487
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_2d(
1488
+ struct lm_ggml_context * ctx,
1489
+ struct lm_ggml_tensor * a,
1490
+ int64_t ne0,
1491
+ int64_t ne1,
1492
+ size_t nb1, // row stride in bytes
1493
+ size_t offset);
1494
+
1495
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_3d(
1496
+ struct lm_ggml_context * ctx,
1497
+ struct lm_ggml_tensor * a,
1498
+ int64_t ne0,
1499
+ int64_t ne1,
1500
+ int64_t ne2,
1501
+ size_t nb1, // row stride in bytes
1502
+ size_t nb2, // slice stride in bytes
1503
+ size_t offset);
1504
+
1505
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_4d(
1506
+ struct lm_ggml_context * ctx,
1507
+ struct lm_ggml_tensor * a,
1508
+ int64_t ne0,
1509
+ int64_t ne1,
1510
+ int64_t ne2,
1511
+ int64_t ne3,
1512
+ size_t nb1, // row stride in bytes
1513
+ size_t nb2, // slice stride in bytes
1514
+ size_t nb3,
1515
+ size_t offset);
1516
+
1517
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_permute(
1518
+ struct lm_ggml_context * ctx,
1519
+ struct lm_ggml_tensor * a,
1520
+ int axis0,
1521
+ int axis1,
1522
+ int axis2,
1523
+ int axis3);
1524
+
1525
+ // alias for lm_ggml_permute(ctx, a, 1, 0, 2, 3)
1526
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_transpose(
1527
+ struct lm_ggml_context * ctx,
1528
+ struct lm_ggml_tensor * a);
1529
+
1530
+ // supports 3D: a->ne[2] == b->ne[1]
1531
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows(
1532
+ struct lm_ggml_context * ctx,
1533
+ struct lm_ggml_tensor * a, // data
1534
+ struct lm_ggml_tensor * b); // row indices
1535
+
1536
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows_back(
1537
+ struct lm_ggml_context * ctx,
1538
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_get_rows result
1539
+ struct lm_ggml_tensor * b, // row indices
1540
+ struct lm_ggml_tensor * c); // data for lm_ggml_get_rows, only used for its shape
1541
+
1542
+ // a TD [n_embd, ne1, ne2, ne3]
1543
+ // b TS [n_embd, n_rows, ne02, ne03] | ne02 == ne2, ne03 == ne3
1544
+ // c I64 [n_rows, ne11, ne12, 1] | c[i] in [0, ne1)
1545
+ //
1546
+ // undefined behavior if destination rows overlap
1547
+ //
1548
+ // broadcast:
1549
+ // ne2 % ne11 == 0
1550
+ // ne3 % ne12 == 0
1551
+ //
1552
+ // return view(a)
1553
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_rows(
1554
+ struct lm_ggml_context * ctx,
1555
+ struct lm_ggml_tensor * a, // destination
1556
+ struct lm_ggml_tensor * b, // source
1557
+ struct lm_ggml_tensor * c); // row indices
1558
+
1559
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag(
1560
+ struct lm_ggml_context * ctx,
1561
+ struct lm_ggml_tensor * a);
1562
+
1563
+ // set elements above the diagonal to -INF
1564
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf(
1565
+ struct lm_ggml_context * ctx,
1566
+ struct lm_ggml_tensor * a,
1567
+ int n_past);
1568
+
1569
+ // in-place, returns view(a)
1570
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf_inplace(
1571
+ struct lm_ggml_context * ctx,
1572
+ struct lm_ggml_tensor * a,
1573
+ int n_past);
1574
+
1575
+ // set elements above the diagonal to 0
1576
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero(
1577
+ struct lm_ggml_context * ctx,
1578
+ struct lm_ggml_tensor * a,
1579
+ int n_past);
1580
+
1581
+ // in-place, returns view(a)
1582
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero_inplace(
1583
+ struct lm_ggml_context * ctx,
1584
+ struct lm_ggml_tensor * a,
1585
+ int n_past);
1586
+
1587
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max(
1588
+ struct lm_ggml_context * ctx,
1589
+ struct lm_ggml_tensor * a);
1590
+
1591
+ // in-place, returns view(a)
1592
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_inplace(
1593
+ struct lm_ggml_context * ctx,
1594
+ struct lm_ggml_tensor * a);
1595
+
1596
+ // a [ne0, ne01, ne02, ne03]
1597
+ // mask [ne0, ne11, ne12, ne13] | ne11 >= ne01, F16 or F32, optional
1598
+ //
1599
+ // broadcast:
1600
+ // ne02 % ne12 == 0
1601
+ // ne03 % ne13 == 0
1602
+ //
1603
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1604
+ // max_bias = 0.0f for no ALiBi
1605
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext(
1606
+ struct lm_ggml_context * ctx,
1607
+ struct lm_ggml_tensor * a,
1608
+ struct lm_ggml_tensor * mask,
1609
+ float scale,
1610
+ float max_bias);
1611
+
1612
+ LM_GGML_API void lm_ggml_soft_max_add_sinks(
1613
+ struct lm_ggml_tensor * a,
1614
+ struct lm_ggml_tensor * sinks);
1615
+
1616
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back(
1617
+ struct lm_ggml_context * ctx,
1618
+ struct lm_ggml_tensor * a,
1619
+ struct lm_ggml_tensor * b,
1620
+ float scale,
1621
+ float max_bias);
1622
+
1623
+ // in-place, returns view(a)
1624
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext_back_inplace(
1625
+ struct lm_ggml_context * ctx,
1626
+ struct lm_ggml_tensor * a,
1627
+ struct lm_ggml_tensor * b,
1628
+ float scale,
1629
+ float max_bias);
1630
+
1631
+ // rotary position embedding
1632
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1633
+ // if (mode & LM_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1634
+ //
1635
+ // b is an int32 vector with size a->ne[2], it contains the positions
1636
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope(
1637
+ struct lm_ggml_context * ctx,
1638
+ struct lm_ggml_tensor * a,
1639
+ struct lm_ggml_tensor * b,
1640
+ int n_dims,
1641
+ int mode);
1642
+
1643
+ // in-place, returns view(a)
1644
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_inplace(
1645
+ struct lm_ggml_context * ctx,
1646
+ struct lm_ggml_tensor * a,
1647
+ struct lm_ggml_tensor * b,
1648
+ int n_dims,
1649
+ int mode);
1650
+
1651
+ // custom RoPE
1652
+ // c is freq factors (e.g. phi3-128k), (optional)
1653
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext(
1654
+ struct lm_ggml_context * ctx,
1655
+ struct lm_ggml_tensor * a,
1656
+ struct lm_ggml_tensor * b,
1657
+ struct lm_ggml_tensor * c,
1658
+ int n_dims,
1659
+ int mode,
1660
+ int n_ctx_orig,
1661
+ float freq_base,
1662
+ float freq_scale,
1663
+ float ext_factor,
1664
+ float attn_factor,
1665
+ float beta_fast,
1666
+ float beta_slow);
1667
+
1668
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi(
1669
+ struct lm_ggml_context * ctx,
1670
+ struct lm_ggml_tensor * a,
1671
+ struct lm_ggml_tensor * b,
1672
+ struct lm_ggml_tensor * c,
1673
+ int n_dims,
1674
+ int sections[LM_GGML_MROPE_SECTIONS],
1675
+ int mode,
1676
+ int n_ctx_orig,
1677
+ float freq_base,
1678
+ float freq_scale,
1679
+ float ext_factor,
1680
+ float attn_factor,
1681
+ float beta_fast,
1682
+ float beta_slow);
1683
+
1684
+ // in-place, returns view(a)
1685
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_inplace(
1686
+ struct lm_ggml_context * ctx,
1687
+ struct lm_ggml_tensor * a,
1688
+ struct lm_ggml_tensor * b,
1689
+ struct lm_ggml_tensor * c,
1690
+ int n_dims,
1691
+ int mode,
1692
+ int n_ctx_orig,
1693
+ float freq_base,
1694
+ float freq_scale,
1695
+ float ext_factor,
1696
+ float attn_factor,
1697
+ float beta_fast,
1698
+ float beta_slow);
1699
+
1700
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi_inplace(
1701
+ struct lm_ggml_context * ctx,
1702
+ struct lm_ggml_tensor * a,
1703
+ struct lm_ggml_tensor * b,
1704
+ struct lm_ggml_tensor * c,
1705
+ int n_dims,
1706
+ int sections[LM_GGML_MROPE_SECTIONS],
1707
+ int mode,
1708
+ int n_ctx_orig,
1709
+ float freq_base,
1710
+ float freq_scale,
1711
+ float ext_factor,
1712
+ float attn_factor,
1713
+ float beta_fast,
1714
+ float beta_slow);
1715
+
1716
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom(
1717
+ struct lm_ggml_context * ctx,
1718
+ struct lm_ggml_tensor * a,
1719
+ struct lm_ggml_tensor * b,
1720
+ int n_dims,
1721
+ int mode,
1722
+ int n_ctx_orig,
1723
+ float freq_base,
1724
+ float freq_scale,
1725
+ float ext_factor,
1726
+ float attn_factor,
1727
+ float beta_fast,
1728
+ float beta_slow),
1729
+ "use lm_ggml_rope_ext instead");
1730
+
1731
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom_inplace(
1732
+ struct lm_ggml_context * ctx,
1733
+ struct lm_ggml_tensor * a,
1734
+ struct lm_ggml_tensor * b,
1735
+ int n_dims,
1736
+ int mode,
1737
+ int n_ctx_orig,
1738
+ float freq_base,
1739
+ float freq_scale,
1740
+ float ext_factor,
1741
+ float attn_factor,
1742
+ float beta_fast,
1743
+ float beta_slow),
1744
+ "use lm_ggml_rope_ext_inplace instead");
1745
+
1746
+ // compute correction dims for YaRN RoPE scaling
1747
+ LM_GGML_API void lm_ggml_rope_yarn_corr_dims(
1748
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1749
+
1750
+ // rotary position embedding backward, i.e compute dx from dy
1751
+ // a - dy
1752
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_back(
1753
+ struct lm_ggml_context * ctx,
1754
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_rope result
1755
+ struct lm_ggml_tensor * b, // positions
1756
+ struct lm_ggml_tensor * c, // freq factors
1757
+ int n_dims,
1758
+ int mode,
1759
+ int n_ctx_orig,
1760
+ float freq_base,
1761
+ float freq_scale,
1762
+ float ext_factor,
1763
+ float attn_factor,
1764
+ float beta_fast,
1765
+ float beta_slow);
1766
+
1767
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_multi_back(
1768
+ struct lm_ggml_context * ctx,
1769
+ struct lm_ggml_tensor * a,
1770
+ struct lm_ggml_tensor * b,
1771
+ struct lm_ggml_tensor * c,
1772
+ int n_dims,
1773
+ int sections[4],
1774
+ int mode,
1775
+ int n_ctx_orig,
1776
+ float freq_base,
1777
+ float freq_scale,
1778
+ float ext_factor,
1779
+ float attn_factor,
1780
+ float beta_fast,
1781
+ float beta_slow);
1782
+
1783
+
1784
+ // clamp
1785
+ // in-place, returns view(a)
1786
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_clamp(
1787
+ struct lm_ggml_context * ctx,
1788
+ struct lm_ggml_tensor * a,
1789
+ float min,
1790
+ float max);
1791
+
1792
+ // im2col
1793
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1794
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col(
1795
+ struct lm_ggml_context * ctx,
1796
+ struct lm_ggml_tensor * a, // convolution kernel
1797
+ struct lm_ggml_tensor * b, // data
1798
+ int s0, // stride dimension 0
1799
+ int s1, // stride dimension 1
1800
+ int p0, // padding dimension 0
1801
+ int p1, // padding dimension 1
1802
+ int d0, // dilation dimension 0
1803
+ int d1, // dilation dimension 1
1804
+ bool is_2D,
1805
+ enum lm_ggml_type dst_type);
1806
+
1807
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col_back(
1808
+ struct lm_ggml_context * ctx,
1809
+ struct lm_ggml_tensor * a, // convolution kernel
1810
+ struct lm_ggml_tensor * b, // gradient of im2col output
1811
+ int64_t * ne, // shape of im2col input
1812
+ int s0, // stride dimension 0
1813
+ int s1, // stride dimension 1
1814
+ int p0, // padding dimension 0
1815
+ int p1, // padding dimension 1
1816
+ int d0, // dilation dimension 0
1817
+ int d1, // dilation dimension 1
1818
+ bool is_2D);
1819
+
1820
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d(
1821
+ struct lm_ggml_context * ctx,
1822
+ struct lm_ggml_tensor * a, // convolution kernel
1823
+ struct lm_ggml_tensor * b, // data
1824
+ int s0, // stride
1825
+ int p0, // padding
1826
+ int d0); // dilation
1827
+
1828
+ // conv_1d with padding = half
1829
+ // alias for lm_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1830
+ LM_GGML_API struct lm_ggml_tensor* lm_ggml_conv_1d_ph(
1831
+ struct lm_ggml_context * ctx,
1832
+ struct lm_ggml_tensor * a, // convolution kernel
1833
+ struct lm_ggml_tensor * b, // data
1834
+ int s, // stride
1835
+ int d); // dilation
1836
+
1837
+ // depthwise
1838
+ // TODO: this is very likely wrong for some cases! - needs more testing
1839
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw(
1840
+ struct lm_ggml_context * ctx,
1841
+ struct lm_ggml_tensor * a, // convolution kernel
1842
+ struct lm_ggml_tensor * b, // data
1843
+ int s0, // stride
1844
+ int p0, // padding
1845
+ int d0); // dilation
1846
+
1847
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d_dw_ph(
1848
+ struct lm_ggml_context * ctx,
1849
+ struct lm_ggml_tensor * a, // convolution kernel
1850
+ struct lm_ggml_tensor * b, // data
1851
+ int s0, // stride
1852
+ int d0); // dilation
1853
+
1854
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_1d(
1855
+ struct lm_ggml_context * ctx,
1856
+ struct lm_ggml_tensor * a, // convolution kernel
1857
+ struct lm_ggml_tensor * b, // data
1858
+ int s0, // stride
1859
+ int p0, // padding
1860
+ int d0); // dilation
1861
+
1862
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d(
1863
+ struct lm_ggml_context * ctx,
1864
+ struct lm_ggml_tensor * a, // convolution kernel
1865
+ struct lm_ggml_tensor * b, // data
1866
+ int s0, // stride dimension 0
1867
+ int s1, // stride dimension 1
1868
+ int p0, // padding dimension 0
1869
+ int p1, // padding dimension 1
1870
+ int d0, // dilation dimension 0
1871
+ int d1); // dilation dimension 1
1872
+
1873
+ // kernel size is a->ne[0] x a->ne[1]
1874
+ // stride is equal to kernel size
1875
+ // padding is zero
1876
+ // example:
1877
+ // a: 16 16 3 768
1878
+ // b: 1024 1024 3 1
1879
+ // res: 64 64 768 1
1880
+ // used in sam
1881
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_sk_p0(
1882
+ struct lm_ggml_context * ctx,
1883
+ struct lm_ggml_tensor * a,
1884
+ struct lm_ggml_tensor * b);
1885
+
1886
+ // kernel size is a->ne[0] x a->ne[1]
1887
+ // stride is 1
1888
+ // padding is half
1889
+ // example:
1890
+ // a: 3 3 256 256
1891
+ // b: 64 64 256 1
1892
+ // res: 64 64 256 1
1893
+ // used in sam
1894
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_s1_ph(
1895
+ struct lm_ggml_context * ctx,
1896
+ struct lm_ggml_tensor * a,
1897
+ struct lm_ggml_tensor * b);
1898
+
1899
+ // depthwise (via im2col and mul_mat)
1900
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_dw(
1901
+ struct lm_ggml_context * ctx,
1902
+ struct lm_ggml_tensor * a, // convolution kernel
1903
+ struct lm_ggml_tensor * b, // data
1904
+ int s0, // stride dimension 0
1905
+ int s1, // stride dimension 1
1906
+ int p0, // padding dimension 0
1907
+ int p1, // padding dimension 1
1908
+ int d0, // dilation dimension 0
1909
+ int d1); // dilation dimension 1
1910
+
1911
+ // Depthwise 2D convolution
1912
+ // may be faster than lm_ggml_conv_2d_dw, but not available in all backends
1913
+ // a: KW KH 1 C convolution kernel
1914
+ // b: W H C N input data
1915
+ // res: W_out H_out C N
1916
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_dw_direct(
1917
+ struct lm_ggml_context * ctx,
1918
+ struct lm_ggml_tensor * a,
1919
+ struct lm_ggml_tensor * b,
1920
+ int stride0,
1921
+ int stride1,
1922
+ int pad0,
1923
+ int pad1,
1924
+ int dilation0,
1925
+ int dilation1);
1926
+
1927
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_2d_p0(
1928
+ struct lm_ggml_context * ctx,
1929
+ struct lm_ggml_tensor * a,
1930
+ struct lm_ggml_tensor * b,
1931
+ int stride);
1932
+
1933
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_direct(
1934
+ struct lm_ggml_context * ctx,
1935
+ struct lm_ggml_tensor * a, // convolution kernel [KW, KH, IC, OC]
1936
+ struct lm_ggml_tensor * b, // input data [W, H, C, N]
1937
+ int s0, // stride dimension 0
1938
+ int s1, // stride dimension 1
1939
+ int p0, // padding dimension 0
1940
+ int p1, // padding dimension 1
1941
+ int d0, // dilation dimension 0
1942
+ int d1); // dilation dimension 1
1943
+
1944
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_3d(
1945
+ struct lm_ggml_context * ctx,
1946
+ struct lm_ggml_tensor * a, // kernel [KW, KH, KD, IC * OC]
1947
+ struct lm_ggml_tensor * b, // input [W, H, D, C * N]
1948
+ int s0, // stride
1949
+ int s1,
1950
+ int s2,
1951
+ int p0, // padding
1952
+ int p1,
1953
+ int p2,
1954
+ int d0, // dilation
1955
+ int d1,
1956
+ int d2,
1957
+ int n_channels,
1958
+ int n_batch,
1959
+ int n_channels_out);
1960
+
1961
+ enum lm_ggml_op_pool {
1962
+ LM_GGML_OP_POOL_MAX,
1963
+ LM_GGML_OP_POOL_AVG,
1964
+ LM_GGML_OP_POOL_COUNT,
1965
+ };
1966
+
1967
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_1d(
1968
+ struct lm_ggml_context * ctx,
1969
+ struct lm_ggml_tensor * a,
1970
+ enum lm_ggml_op_pool op,
1971
+ int k0, // kernel size
1972
+ int s0, // stride
1973
+ int p0); // padding
1974
+
1975
+ // the result will have 2*p0 padding for the first dimension
1976
+ // and 2*p1 padding for the second dimension
1977
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d(
1978
+ struct lm_ggml_context * ctx,
1979
+ struct lm_ggml_tensor * a,
1980
+ enum lm_ggml_op_pool op,
1981
+ int k0,
1982
+ int k1,
1983
+ int s0,
1984
+ int s1,
1985
+ float p0,
1986
+ float p1);
1987
+
1988
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d_back(
1989
+ struct lm_ggml_context * ctx,
1990
+ struct lm_ggml_tensor * a,
1991
+ struct lm_ggml_tensor * af, // "a"/input used in forward pass
1992
+ enum lm_ggml_op_pool op,
1993
+ int k0,
1994
+ int k1,
1995
+ int s0,
1996
+ int s1,
1997
+ float p0,
1998
+ float p1);
1999
+
2000
+ enum lm_ggml_scale_mode {
2001
+ LM_GGML_SCALE_MODE_NEAREST = 0,
2002
+ LM_GGML_SCALE_MODE_BILINEAR = 1,
2003
+
2004
+ LM_GGML_SCALE_MODE_COUNT
2005
+ };
2006
+
2007
+ enum lm_ggml_scale_flag {
2008
+ LM_GGML_SCALE_FLAG_ALIGN_CORNERS = (1 << 8)
2009
+ };
2010
+
2011
+ // interpolate
2012
+ // multiplies ne0 and ne1 by scale factor
2013
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale(
2014
+ struct lm_ggml_context * ctx,
2015
+ struct lm_ggml_tensor * a,
2016
+ int scale_factor,
2017
+ enum lm_ggml_scale_mode mode);
2018
+
2019
+ // interpolate
2020
+ // interpolate scale to specified dimensions
2021
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale_ext(
2022
+ struct lm_ggml_context * ctx,
2023
+ struct lm_ggml_tensor * a,
2024
+ int ne0,
2025
+ int ne1,
2026
+ int ne2,
2027
+ int ne3,
2028
+ enum lm_ggml_scale_mode mode),
2029
+ "use lm_ggml_interpolate instead");
2030
+
2031
+ // Up- or downsamples the input to the specified size.
2032
+ // 2D scale modes (eg. bilinear) are applied to the first two dimensions.
2033
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_interpolate(
2034
+ struct lm_ggml_context * ctx,
2035
+ struct lm_ggml_tensor * a,
2036
+ int64_t ne0,
2037
+ int64_t ne1,
2038
+ int64_t ne2,
2039
+ int64_t ne3,
2040
+ uint32_t mode); // lm_ggml_scale_mode [ | lm_ggml_scale_flag...]
2041
+
2042
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
2043
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad(
2044
+ struct lm_ggml_context * ctx,
2045
+ struct lm_ggml_tensor * a,
2046
+ int p0,
2047
+ int p1,
2048
+ int p2,
2049
+ int p3);
2050
+
2051
+ // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
2052
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad_reflect_1d(
2053
+ struct lm_ggml_context * ctx,
2054
+ struct lm_ggml_tensor * a,
2055
+ int p0,
2056
+ int p1);
2057
+
2058
+ // Move tensor elements by an offset given for each dimension. Elements that
2059
+ // are shifted beyond the last position are wrapped around to the beginning.
2060
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_roll(
2061
+ struct lm_ggml_context * ctx,
2062
+ struct lm_ggml_tensor * a,
2063
+ int shift0,
2064
+ int shift1,
2065
+ int shift2,
2066
+ int shift3);
2067
+
2068
+
2069
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
2070
+ // timesteps: [N,]
2071
+ // return: [N, dim]
2072
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_timestep_embedding(
2073
+ struct lm_ggml_context * ctx,
2074
+ struct lm_ggml_tensor * timesteps,
2075
+ int dim,
2076
+ int max_period);
2077
+
2078
+ // sort rows
2079
+ enum lm_ggml_sort_order {
2080
+ LM_GGML_SORT_ORDER_ASC,
2081
+ LM_GGML_SORT_ORDER_DESC,
2082
+ };
2083
+
2084
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argsort(
2085
+ struct lm_ggml_context * ctx,
2086
+ struct lm_ggml_tensor * a,
2087
+ enum lm_ggml_sort_order order);
2088
+
2089
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_arange(
2090
+ struct lm_ggml_context * ctx,
2091
+ float start,
2092
+ float stop,
2093
+ float step);
2094
+
2095
+ // top k elements per row
2096
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_top_k(
2097
+ struct lm_ggml_context * ctx,
2098
+ struct lm_ggml_tensor * a,
2099
+ int k);
2100
+
2101
+ #define LM_GGML_KQ_MASK_PAD 64
2102
+
2103
+ // q: [n_embd_k, n_batch, n_head, ne3 ]
2104
+ // k: [n_embd_k, n_kv, n_head_kv, ne3 ]
2105
+ // v: [n_embd_v, n_kv, n_head_kv, ne3 ] !! not transposed !!
2106
+ // mask: [n_kv, n_batch_pad, ne32, ne33] !! n_batch_pad = LM_GGML_PAD(n_batch, LM_GGML_KQ_MASK_PAD) !!
2107
+ // res: [n_embd_v, n_head, n_batch, ne3 ] !! permuted !!
2108
+ //
2109
+ // broadcast:
2110
+ // n_head % n_head_kv == 0
2111
+ // n_head % ne32 == 0
2112
+ // ne3 % ne33 == 0
2113
+ //
2114
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_ext(
2115
+ struct lm_ggml_context * ctx,
2116
+ struct lm_ggml_tensor * q,
2117
+ struct lm_ggml_tensor * k,
2118
+ struct lm_ggml_tensor * v,
2119
+ struct lm_ggml_tensor * mask,
2120
+ float scale,
2121
+ float max_bias,
2122
+ float logit_softcap);
2123
+
2124
+ LM_GGML_API void lm_ggml_flash_attn_ext_set_prec(
2125
+ struct lm_ggml_tensor * a,
2126
+ enum lm_ggml_prec prec);
2127
+
2128
+ LM_GGML_API enum lm_ggml_prec lm_ggml_flash_attn_ext_get_prec(
2129
+ const struct lm_ggml_tensor * a);
2130
+
2131
+ LM_GGML_API void lm_ggml_flash_attn_ext_add_sinks(
2132
+ struct lm_ggml_tensor * a,
2133
+ struct lm_ggml_tensor * sinks);
2134
+
2135
+ // TODO: needs to be adapted to lm_ggml_flash_attn_ext
2136
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_back(
2137
+ struct lm_ggml_context * ctx,
2138
+ struct lm_ggml_tensor * q,
2139
+ struct lm_ggml_tensor * k,
2140
+ struct lm_ggml_tensor * v,
2141
+ struct lm_ggml_tensor * d,
2142
+ bool masked);
2143
+
2144
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_conv(
2145
+ struct lm_ggml_context * ctx,
2146
+ struct lm_ggml_tensor * sx,
2147
+ struct lm_ggml_tensor * c);
2148
+
2149
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_scan(
2150
+ struct lm_ggml_context * ctx,
2151
+ struct lm_ggml_tensor * s,
2152
+ struct lm_ggml_tensor * x,
2153
+ struct lm_ggml_tensor * dt,
2154
+ struct lm_ggml_tensor * A,
2155
+ struct lm_ggml_tensor * B,
2156
+ struct lm_ggml_tensor * C,
2157
+ struct lm_ggml_tensor * ids);
2158
+
2159
+ // partition into non-overlapping windows with padding if needed
2160
+ // example:
2161
+ // a: 768 64 64 1
2162
+ // w: 14
2163
+ // res: 768 14 14 25
2164
+ // used in sam
2165
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_part(
2166
+ struct lm_ggml_context * ctx,
2167
+ struct lm_ggml_tensor * a,
2168
+ int w);
2169
+
2170
+ // reverse of lm_ggml_win_part
2171
+ // used in sam
2172
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_unpart(
2173
+ struct lm_ggml_context * ctx,
2174
+ struct lm_ggml_tensor * a,
2175
+ int w0,
2176
+ int h0,
2177
+ int w);
2178
+
2179
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary(
2180
+ struct lm_ggml_context * ctx,
2181
+ struct lm_ggml_tensor * a,
2182
+ enum lm_ggml_unary_op op);
2183
+
2184
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary_inplace(
2185
+ struct lm_ggml_context * ctx,
2186
+ struct lm_ggml_tensor * a,
2187
+ enum lm_ggml_unary_op op);
2188
+
2189
+ // used in sam
2190
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rel_pos(
2191
+ struct lm_ggml_context * ctx,
2192
+ struct lm_ggml_tensor * a,
2193
+ int qh,
2194
+ int kh);
2195
+
2196
+ // used in sam
2197
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos(
2198
+ struct lm_ggml_context * ctx,
2199
+ struct lm_ggml_tensor * a,
2200
+ struct lm_ggml_tensor * pw,
2201
+ struct lm_ggml_tensor * ph);
2202
+
2203
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos_inplace(
2204
+ struct lm_ggml_context * ctx,
2205
+ struct lm_ggml_tensor * a,
2206
+ struct lm_ggml_tensor * pw,
2207
+ struct lm_ggml_tensor * ph);
2208
+
2209
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv6(
2210
+ struct lm_ggml_context * ctx,
2211
+ struct lm_ggml_tensor * k,
2212
+ struct lm_ggml_tensor * v,
2213
+ struct lm_ggml_tensor * r,
2214
+ struct lm_ggml_tensor * tf,
2215
+ struct lm_ggml_tensor * td,
2216
+ struct lm_ggml_tensor * state);
2217
+
2218
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gated_linear_attn(
2219
+ struct lm_ggml_context * ctx,
2220
+ struct lm_ggml_tensor * k,
2221
+ struct lm_ggml_tensor * v,
2222
+ struct lm_ggml_tensor * q,
2223
+ struct lm_ggml_tensor * g,
2224
+ struct lm_ggml_tensor * state,
2225
+ float scale);
2226
+
2227
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv7(
2228
+ struct lm_ggml_context * ctx,
2229
+ struct lm_ggml_tensor * r,
2230
+ struct lm_ggml_tensor * w,
2231
+ struct lm_ggml_tensor * k,
2232
+ struct lm_ggml_tensor * v,
2233
+ struct lm_ggml_tensor * a,
2234
+ struct lm_ggml_tensor * b,
2235
+ struct lm_ggml_tensor * state);
2236
+
2237
+ // custom operators
2238
+
2239
+ typedef void (*lm_ggml_custom1_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, int ith, int nth, void * userdata);
2240
+ 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);
2241
+ 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);
2242
+
2243
+ #define LM_GGML_N_TASKS_MAX (-1)
2244
+ // n_tasks == LM_GGML_N_TASKS_MAX means to use max number of tasks
2245
+
2246
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1(
2247
+ struct lm_ggml_context * ctx,
2248
+ struct lm_ggml_tensor * a,
2249
+ lm_ggml_custom1_op_t fun,
2250
+ int n_tasks,
2251
+ void * userdata);
2252
+
2253
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace(
2254
+ struct lm_ggml_context * ctx,
2255
+ struct lm_ggml_tensor * a,
2256
+ lm_ggml_custom1_op_t fun,
2257
+ int n_tasks,
2258
+ void * userdata);
2259
+
2260
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2(
2261
+ struct lm_ggml_context * ctx,
2262
+ struct lm_ggml_tensor * a,
2263
+ struct lm_ggml_tensor * b,
2264
+ lm_ggml_custom2_op_t fun,
2265
+ int n_tasks,
2266
+ void * userdata);
2267
+
2268
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace(
2269
+ struct lm_ggml_context * ctx,
2270
+ struct lm_ggml_tensor * a,
2271
+ struct lm_ggml_tensor * b,
2272
+ lm_ggml_custom2_op_t fun,
2273
+ int n_tasks,
2274
+ void * userdata);
2275
+
2276
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3(
2277
+ struct lm_ggml_context * ctx,
2278
+ struct lm_ggml_tensor * a,
2279
+ struct lm_ggml_tensor * b,
2280
+ struct lm_ggml_tensor * c,
2281
+ lm_ggml_custom3_op_t fun,
2282
+ int n_tasks,
2283
+ void * userdata);
2284
+
2285
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace(
2286
+ struct lm_ggml_context * ctx,
2287
+ struct lm_ggml_tensor * a,
2288
+ struct lm_ggml_tensor * b,
2289
+ struct lm_ggml_tensor * c,
2290
+ lm_ggml_custom3_op_t fun,
2291
+ int n_tasks,
2292
+ void * userdata);
2293
+
2294
+ typedef void (*lm_ggml_custom_op_t)(struct lm_ggml_tensor * dst , int ith, int nth, void * userdata);
2295
+
2296
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_custom_4d(
2297
+ struct lm_ggml_context * ctx,
2298
+ enum lm_ggml_type type,
2299
+ int64_t ne0,
2300
+ int64_t ne1,
2301
+ int64_t ne2,
2302
+ int64_t ne3,
2303
+ struct lm_ggml_tensor ** args,
2304
+ int n_args,
2305
+ lm_ggml_custom_op_t fun,
2306
+ int n_tasks,
2307
+ void * userdata);
2308
+
2309
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_custom_inplace(
2310
+ struct lm_ggml_context * ctx,
2311
+ struct lm_ggml_tensor * a,
2312
+ struct lm_ggml_tensor ** args,
2313
+ int n_args,
2314
+ lm_ggml_custom_op_t fun,
2315
+ int n_tasks,
2316
+ void * userdata);
2317
+
2318
+ // loss function
2319
+
2320
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss(
2321
+ struct lm_ggml_context * ctx,
2322
+ struct lm_ggml_tensor * a, // logits
2323
+ struct lm_ggml_tensor * b); // labels
2324
+
2325
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss_back(
2326
+ struct lm_ggml_context * ctx,
2327
+ struct lm_ggml_tensor * a, // logits
2328
+ struct lm_ggml_tensor * b, // labels
2329
+ struct lm_ggml_tensor * c); // gradients of cross_entropy_loss result
2330
+
2331
+ // AdamW optimizer step
2332
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2333
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2334
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_adamw(
2335
+ struct lm_ggml_context * ctx,
2336
+ struct lm_ggml_tensor * a,
2337
+ struct lm_ggml_tensor * grad,
2338
+ struct lm_ggml_tensor * m,
2339
+ struct lm_ggml_tensor * v,
2340
+ struct lm_ggml_tensor * adamw_params); // parameters such as the learning rate
2341
+
2342
+ // stochastic gradient descent step (with weight decay)
2343
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_sgd(
2344
+ struct lm_ggml_context * ctx,
2345
+ struct lm_ggml_tensor * a,
2346
+ struct lm_ggml_tensor * grad,
2347
+ struct lm_ggml_tensor * sgd_params); // alpha, weight decay
2348
+
2349
+ //
2350
+ // automatic differentiation
2351
+ //
2352
+
2353
+ LM_GGML_API void lm_ggml_build_forward_expand(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2354
+ LM_GGML_API void lm_ggml_build_backward_expand(
2355
+ struct lm_ggml_context * ctx, // context for gradient computation
2356
+ struct lm_ggml_cgraph * cgraph,
2357
+ struct lm_ggml_tensor ** grad_accs);
2358
+
2359
+ // graph allocation in a context
2360
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph (struct lm_ggml_context * ctx); // size = LM_GGML_DEFAULT_GRAPH_SIZE, grads = false
2361
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph_custom(struct lm_ggml_context * ctx, size_t size, bool grads);
2362
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_dup (struct lm_ggml_context * ctx, struct lm_ggml_cgraph * cgraph, bool force_grads);
2363
+ LM_GGML_API void lm_ggml_graph_cpy (struct lm_ggml_cgraph * src, struct lm_ggml_cgraph * dst);
2364
+ 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
2365
+ LM_GGML_API void lm_ggml_graph_clear (struct lm_ggml_cgraph * cgraph);
2366
+
2367
+ LM_GGML_API int lm_ggml_graph_size (struct lm_ggml_cgraph * cgraph);
2368
+ 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]
2369
+ LM_GGML_API struct lm_ggml_tensor ** lm_ggml_graph_nodes (struct lm_ggml_cgraph * cgraph);
2370
+ LM_GGML_API int lm_ggml_graph_n_nodes(struct lm_ggml_cgraph * cgraph);
2371
+
2372
+ LM_GGML_API void lm_ggml_graph_add_node(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2373
+
2374
+ LM_GGML_API size_t lm_ggml_graph_overhead(void);
2375
+ LM_GGML_API size_t lm_ggml_graph_overhead_custom(size_t size, bool grads);
2376
+
2377
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_tensor (const struct lm_ggml_cgraph * cgraph, const char * name);
2378
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad (const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2379
+ 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);
2380
+
2381
+ // print info and performance information for the graph
2382
+ LM_GGML_API void lm_ggml_graph_print(const struct lm_ggml_cgraph * cgraph);
2383
+
2384
+ // dump the graph into a file using the dot format
2385
+ LM_GGML_API void lm_ggml_graph_dump_dot(const struct lm_ggml_cgraph * gb, const struct lm_ggml_cgraph * gf, const char * filename);
2386
+
2387
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2388
+ typedef void (*lm_ggml_log_callback)(enum lm_ggml_log_level level, const char * text, void * user_data);
2389
+
2390
+ // Set callback for all future logging events.
2391
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2392
+ LM_GGML_API void lm_ggml_log_set(lm_ggml_log_callback log_callback, void * user_data);
2393
+
2394
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_zero(struct lm_ggml_tensor * tensor);
2395
+
2396
+ //
2397
+ // quantization
2398
+ //
2399
+
2400
+ // - lm_ggml_quantize_init can be called multiple times with the same type
2401
+ // it will only initialize the quantization tables for the first call or after lm_ggml_quantize_free
2402
+ // automatically called by lm_ggml_quantize_chunk for convenience
2403
+ //
2404
+ // - lm_ggml_quantize_free will free any memory allocated by lm_ggml_quantize_init
2405
+ // call this at the end of the program to avoid memory leaks
2406
+ //
2407
+ // note: these are thread-safe
2408
+ //
2409
+ LM_GGML_API void lm_ggml_quantize_init(enum lm_ggml_type type);
2410
+ LM_GGML_API void lm_ggml_quantize_free(void);
2411
+
2412
+ // some quantization type cannot be used without an importance matrix
2413
+ LM_GGML_API bool lm_ggml_quantize_requires_imatrix(enum lm_ggml_type type);
2414
+
2415
+ // calls lm_ggml_quantize_init internally (i.e. can allocate memory)
2416
+ LM_GGML_API size_t lm_ggml_quantize_chunk(
2417
+ enum lm_ggml_type type,
2418
+ const float * src,
2419
+ void * dst,
2420
+ int64_t start,
2421
+ int64_t nrows,
2422
+ int64_t n_per_row,
2423
+ const float * imatrix);
2424
+
2425
+ #ifdef __cplusplus
2426
+ // restrict not standard in C++
2427
+ # if defined(__GNUC__)
2428
+ # define LM_GGML_RESTRICT __restrict__
2429
+ # elif defined(__clang__)
2430
+ # define LM_GGML_RESTRICT __restrict
2431
+ # elif defined(_MSC_VER)
2432
+ # define LM_GGML_RESTRICT __restrict
2433
+ # else
2434
+ # define LM_GGML_RESTRICT
2435
+ # endif
2436
+ #else
2437
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2438
+ # define LM_GGML_RESTRICT __restrict
2439
+ # else
2440
+ # define LM_GGML_RESTRICT restrict
2441
+ # endif
2442
+ #endif
2443
+ typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2444
+ typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2445
+
2446
+ struct lm_ggml_type_traits {
2447
+ const char * type_name;
2448
+ int64_t blck_size;
2449
+ int64_t blck_size_interleave; // interleave elements in blocks
2450
+ size_t type_size;
2451
+ bool is_quantized;
2452
+ lm_ggml_to_float_t to_float;
2453
+ lm_ggml_from_float_t from_float_ref;
2454
+ };
2455
+
2456
+ LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2457
+
2458
+ // ggml threadpool
2459
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2460
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2461
+
2462
+ // scheduling priorities
2463
+ enum lm_ggml_sched_priority {
2464
+ LM_GGML_SCHED_PRIO_LOW = -1,
2465
+ LM_GGML_SCHED_PRIO_NORMAL,
2466
+ LM_GGML_SCHED_PRIO_MEDIUM,
2467
+ LM_GGML_SCHED_PRIO_HIGH,
2468
+ LM_GGML_SCHED_PRIO_REALTIME
2469
+ };
2470
+
2471
+ // threadpool params
2472
+ // Use lm_ggml_threadpool_params_default() or lm_ggml_threadpool_params_init() to populate the defaults
2473
+ struct lm_ggml_threadpool_params {
2474
+ bool cpumask[LM_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2475
+ int n_threads; // number of threads
2476
+ enum lm_ggml_sched_priority prio; // thread priority
2477
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2478
+ bool strict_cpu; // strict cpu placement
2479
+ bool paused; // start in paused state
2480
+ };
2481
+
2482
+ struct lm_ggml_threadpool; // forward declaration, see ggml.c
2483
+
2484
+ typedef struct lm_ggml_threadpool * lm_ggml_threadpool_t;
2485
+
2486
+ LM_GGML_API struct lm_ggml_threadpool_params lm_ggml_threadpool_params_default(int n_threads);
2487
+ LM_GGML_API void lm_ggml_threadpool_params_init (struct lm_ggml_threadpool_params * p, int n_threads);
2488
+ LM_GGML_API bool lm_ggml_threadpool_params_match (const struct lm_ggml_threadpool_params * p0, const struct lm_ggml_threadpool_params * p1);
2489
+
2490
+ #ifdef __cplusplus
2491
+ }
2492
+ #endif