whisper.rn 0.4.0-rc.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/README.md +5 -1
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +43 -13
  4. package/android/src/main/java/com/rnwhisper/WhisperContext.java +33 -35
  5. package/android/src/main/jni.cpp +9 -0
  6. package/android/src/main/jniLibs/arm64-v8a/librnwhisper.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/librnwhisper_v8fp16_va_2.so +0 -0
  8. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper.so +0 -0
  9. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper_vfpv4.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/librnwhisper.so +0 -0
  11. package/android/src/main/jniLibs/x86_64/librnwhisper_x86_64.so +0 -0
  12. package/cpp/coreml/whisper-compat.h +10 -0
  13. package/cpp/coreml/whisper-compat.m +35 -0
  14. package/cpp/coreml/whisper-decoder-impl.h +27 -15
  15. package/cpp/coreml/whisper-decoder-impl.m +36 -10
  16. package/cpp/coreml/whisper-encoder-impl.h +21 -9
  17. package/cpp/coreml/whisper-encoder-impl.m +29 -3
  18. package/cpp/ggml-alloc.c +39 -37
  19. package/cpp/ggml-alloc.h +1 -1
  20. package/cpp/ggml-backend-impl.h +55 -27
  21. package/cpp/ggml-backend-reg.cpp +591 -0
  22. package/cpp/ggml-backend.cpp +336 -955
  23. package/cpp/ggml-backend.h +70 -42
  24. package/cpp/ggml-common.h +57 -49
  25. package/cpp/ggml-cpp.h +39 -0
  26. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  27. package/cpp/ggml-cpu/amx/amx.h +8 -0
  28. package/cpp/ggml-cpu/amx/common.h +91 -0
  29. package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
  30. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  31. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  32. package/cpp/ggml-cpu/arch/arm/quants.c +4113 -0
  33. package/cpp/ggml-cpu/arch/arm/repack.cpp +2162 -0
  34. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  35. package/cpp/ggml-cpu/arch/x86/quants.c +4310 -0
  36. package/cpp/ggml-cpu/arch/x86/repack.cpp +3284 -0
  37. package/cpp/ggml-cpu/arch-fallback.h +184 -0
  38. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  39. package/cpp/ggml-cpu/binary-ops.h +16 -0
  40. package/cpp/ggml-cpu/common.h +72 -0
  41. package/cpp/ggml-cpu/ggml-cpu-impl.h +511 -0
  42. package/cpp/ggml-cpu/ggml-cpu.c +3473 -0
  43. package/cpp/ggml-cpu/ggml-cpu.cpp +671 -0
  44. package/cpp/ggml-cpu/ops.cpp +9085 -0
  45. package/cpp/ggml-cpu/ops.h +111 -0
  46. package/cpp/ggml-cpu/quants.c +1157 -0
  47. package/cpp/ggml-cpu/quants.h +89 -0
  48. package/cpp/ggml-cpu/repack.cpp +1570 -0
  49. package/cpp/ggml-cpu/repack.h +98 -0
  50. package/cpp/ggml-cpu/simd-mappings.h +1006 -0
  51. package/cpp/ggml-cpu/traits.cpp +36 -0
  52. package/cpp/ggml-cpu/traits.h +38 -0
  53. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  54. package/cpp/ggml-cpu/unary-ops.h +28 -0
  55. package/cpp/ggml-cpu/vec.cpp +321 -0
  56. package/cpp/ggml-cpu/vec.h +973 -0
  57. package/cpp/ggml-cpu.h +143 -0
  58. package/cpp/ggml-impl.h +417 -23
  59. package/cpp/ggml-metal-impl.h +622 -0
  60. package/cpp/ggml-metal.h +9 -9
  61. package/cpp/ggml-metal.m +3451 -1344
  62. package/cpp/ggml-opt.cpp +1037 -0
  63. package/cpp/ggml-opt.h +237 -0
  64. package/cpp/ggml-quants.c +296 -10818
  65. package/cpp/ggml-quants.h +78 -125
  66. package/cpp/ggml-threading.cpp +12 -0
  67. package/cpp/ggml-threading.h +14 -0
  68. package/cpp/ggml-whisper-sim.metallib +0 -0
  69. package/cpp/ggml-whisper.metallib +0 -0
  70. package/cpp/ggml.c +4633 -21450
  71. package/cpp/ggml.h +320 -661
  72. package/cpp/gguf.cpp +1347 -0
  73. package/cpp/gguf.h +202 -0
  74. package/cpp/rn-whisper.cpp +4 -11
  75. package/cpp/whisper-arch.h +197 -0
  76. package/cpp/whisper.cpp +2022 -495
  77. package/cpp/whisper.h +75 -18
  78. package/ios/CMakeLists.txt +95 -0
  79. package/ios/RNWhisper.h +5 -0
  80. package/ios/RNWhisperAudioUtils.m +4 -0
  81. package/ios/RNWhisperContext.h +5 -0
  82. package/ios/RNWhisperContext.mm +4 -2
  83. package/ios/rnwhisper.xcframework/Info.plist +74 -0
  84. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  85. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  86. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  87. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  88. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  89. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  90. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  91. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  92. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  93. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  94. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  95. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  96. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  97. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  98. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  99. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  100. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  101. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  102. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  103. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Info.plist +0 -0
  104. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  105. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/rnwhisper +0 -0
  106. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  107. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  108. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  109. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  110. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  111. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  112. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  113. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  114. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  115. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  116. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  117. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  118. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  119. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  120. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  121. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  122. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  123. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  124. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  125. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  126. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  127. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  128. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  129. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  130. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  131. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  132. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  133. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  134. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  135. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  136. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  137. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  138. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  139. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  140. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  141. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  142. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  143. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  144. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  145. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  146. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  147. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  148. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Info.plist +0 -0
  149. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  150. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/rnwhisper +0 -0
  151. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  152. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  153. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  154. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  155. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  156. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  157. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  158. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  159. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  160. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  161. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  162. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  163. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  164. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  165. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  166. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  167. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  168. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  169. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  170. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  171. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  172. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  173. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  174. package/jest/mock.js +5 -0
  175. package/lib/commonjs/version.json +1 -1
  176. package/lib/module/version.json +1 -1
  177. package/package.json +10 -6
  178. package/src/version.json +1 -1
  179. package/whisper-rn.podspec +11 -18
  180. package/cpp/README.md +0 -4
  181. package/cpp/ggml-aarch64.c +0 -3209
  182. package/cpp/ggml-aarch64.h +0 -39
  183. package/cpp/ggml-cpu-impl.h +0 -614
@@ -0,0 +1,2221 @@
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 wsp_ggml_init_params params = {
36
+ // .mem_size = 16*1024*1024,
37
+ // .mem_buffer = NULL,
38
+ // };
39
+ //
40
+ // // memory allocation happens here
41
+ // struct wsp_ggml_context * ctx = wsp_ggml_init(params);
42
+ //
43
+ // struct wsp_ggml_tensor * x = wsp_ggml_new_tensor_1d(ctx, WSP_GGML_TYPE_F32, 1);
44
+ //
45
+ // wsp_ggml_set_param(ctx, x); // x is an input variable
46
+ //
47
+ // struct wsp_ggml_tensor * a = wsp_ggml_new_tensor_1d(ctx, WSP_GGML_TYPE_F32, 1);
48
+ // struct wsp_ggml_tensor * b = wsp_ggml_new_tensor_1d(ctx, WSP_GGML_TYPE_F32, 1);
49
+ // struct wsp_ggml_tensor * x2 = wsp_ggml_mul(ctx, x, x);
50
+ // struct wsp_ggml_tensor * f = wsp_ggml_add(ctx, wsp_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 wsp_ggml_cgraph * gf = wsp_ggml_new_graph(ctx);
62
+ // wsp_ggml_build_forward_expand(gf, f);
63
+ //
64
+ // // set the input variable and parameter values
65
+ // wsp_ggml_set_f32(x, 2.0f);
66
+ // wsp_ggml_set_f32(a, 3.0f);
67
+ // wsp_ggml_set_f32(b, 4.0f);
68
+ //
69
+ // wsp_ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
+ //
71
+ // printf("f = %f\n", wsp_ggml_get_f32_1d(f, 0));
72
+ //
73
+ // ...
74
+ // }
75
+ //
76
+ // The actual computation is performed in the wsp_ggml_graph_compute() function.
77
+ //
78
+ // The wsp_ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
+ // wsp_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 wsp_ggml_used_mem() function to find out how much memory was
82
+ // actually needed.
83
+ //
84
+ // The wsp_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 wsp_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
+ // - wsp_ggml_permute()
100
+ // - wsp_ggml_conv_1d_1s()
101
+ // - wsp_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 wsp_ggml_tensor)
113
+ //
114
+ // The tensors are stored in memory via the wsp_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 wsp_ggml_tensor * c = wsp_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 wsp_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 wsp_ggml_tensor * a = wsp_ggml_new_tensor_2d(ctx, WSP_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 wsp_ggml_get_f32_1d() and wsp_ggml_set_f32_1d() that can be used.
149
+ //
150
+ // ## The matrix multiplication operator (wsp_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 WSP_GGML_SHARED
177
+ # if defined(_WIN32) && !defined(__MINGW32__)
178
+ # ifdef WSP_GGML_BUILD
179
+ # define WSP_GGML_API __declspec(dllexport) extern
180
+ # else
181
+ # define WSP_GGML_API __declspec(dllimport) extern
182
+ # endif
183
+ # else
184
+ # define WSP_GGML_API __attribute__ ((visibility ("default"))) extern
185
+ # endif
186
+ #else
187
+ # define WSP_GGML_API extern
188
+ #endif
189
+
190
+ // TODO: support for clang
191
+ #ifdef __GNUC__
192
+ # define WSP_GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
193
+ #elif defined(_MSC_VER)
194
+ # define WSP_GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
195
+ #else
196
+ # define WSP_GGML_DEPRECATED(func, hint) func
197
+ #endif
198
+
199
+ #ifndef __GNUC__
200
+ # define WSP_GGML_ATTRIBUTE_FORMAT(...)
201
+ #elif defined(__MINGW32__) && !defined(__clang__)
202
+ # define WSP_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
203
+ #else
204
+ # define WSP_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 WSP_GGML_FILE_MAGIC 0x67676d6c // "ggml"
213
+ #define WSP_GGML_FILE_VERSION 2
214
+
215
+ #define WSP_GGML_QNT_VERSION 2 // bump this on quantization format changes
216
+ #define WSP_GGML_QNT_VERSION_FACTOR 1000 // do not change this
217
+
218
+ #define WSP_GGML_MAX_DIMS 4
219
+ #define WSP_GGML_MAX_PARAMS 2048
220
+ #define WSP_GGML_MAX_SRC 10
221
+ #define WSP_GGML_MAX_N_THREADS 512
222
+ #define WSP_GGML_MAX_OP_PARAMS 64
223
+
224
+ #ifndef WSP_GGML_MAX_NAME
225
+ # define WSP_GGML_MAX_NAME 64
226
+ #endif
227
+
228
+ #define WSP_GGML_DEFAULT_N_THREADS 4
229
+ #define WSP_GGML_DEFAULT_GRAPH_SIZE 2048
230
+
231
+ #if UINTPTR_MAX == 0xFFFFFFFF
232
+ #define WSP_GGML_MEM_ALIGN 4
233
+ #else
234
+ #define WSP_GGML_MEM_ALIGN 16
235
+ #endif
236
+
237
+ #define WSP_GGML_EXIT_SUCCESS 0
238
+ #define WSP_GGML_EXIT_ABORTED 1
239
+
240
+ #define WSP_GGML_ROPE_TYPE_NEOX 2
241
+ #define WSP_GGML_ROPE_TYPE_MROPE 8
242
+ #define WSP_GGML_ROPE_TYPE_VISION 24
243
+
244
+ #define WSP_GGML_UNUSED(x) (void)(x)
245
+
246
+ #define WSP_GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
247
+
248
+ #ifndef NDEBUG
249
+ # define WSP_GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
250
+ #elif defined(__GNUC__)
251
+ # define WSP_GGML_UNREACHABLE() __builtin_unreachable()
252
+ #elif defined(_MSC_VER)
253
+ # define WSP_GGML_UNREACHABLE() __assume(0)
254
+ #else
255
+ # define WSP_GGML_UNREACHABLE() ((void) 0)
256
+ #endif
257
+
258
+ #ifdef __cplusplus
259
+ # define WSP_GGML_NORETURN [[noreturn]]
260
+ #elif defined(_MSC_VER)
261
+ # define WSP_GGML_NORETURN __declspec(noreturn)
262
+ #else
263
+ # define WSP_GGML_NORETURN _Noreturn
264
+ #endif
265
+
266
+ #define WSP_GGML_ABORT(...) wsp_ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
267
+ #define WSP_GGML_ASSERT(x) if (!(x)) WSP_GGML_ABORT("WSP_GGML_ASSERT(%s) failed", #x)
268
+
269
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
270
+ // main purpose is to reduce code duplication and improve readability.
271
+ //
272
+ // example:
273
+ //
274
+ // WSP_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
275
+ // WSP_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
276
+ //
277
+ #define WSP_GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
278
+ const type prefix##0 = (pointer)->array[0]; \
279
+ WSP_GGML_UNUSED(prefix##0);
280
+ #define WSP_GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
281
+ WSP_GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
282
+ const type prefix##1 = (pointer)->array[1]; \
283
+ WSP_GGML_UNUSED(prefix##1);
284
+ #define WSP_GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
285
+ WSP_GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
286
+ const type prefix##2 = (pointer)->array[2]; \
287
+ WSP_GGML_UNUSED(prefix##2);
288
+ #define WSP_GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
289
+ WSP_GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
290
+ const type prefix##3 = (pointer)->array[3]; \
291
+ WSP_GGML_UNUSED(prefix##3);
292
+
293
+ #define WSP_GGML_TENSOR_UNARY_OP_LOCALS \
294
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
295
+ WSP_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
296
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
297
+ WSP_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
298
+
299
+ #define WSP_GGML_TENSOR_BINARY_OP_LOCALS \
300
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
301
+ WSP_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
302
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
303
+ WSP_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
304
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
305
+ WSP_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
306
+
307
+ #define WSP_GGML_TENSOR_BINARY_OP_LOCALS01 \
308
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
309
+ WSP_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
310
+ WSP_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
311
+ WSP_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
312
+
313
+ #ifdef __cplusplus
314
+ extern "C" {
315
+ #endif
316
+
317
+ WSP_GGML_NORETURN WSP_GGML_ATTRIBUTE_FORMAT(3, 4)
318
+ WSP_GGML_API void wsp_ggml_abort(const char * file, int line, const char * fmt, ...);
319
+
320
+ enum wsp_ggml_status {
321
+ WSP_GGML_STATUS_ALLOC_FAILED = -2,
322
+ WSP_GGML_STATUS_FAILED = -1,
323
+ WSP_GGML_STATUS_SUCCESS = 0,
324
+ WSP_GGML_STATUS_ABORTED = 1,
325
+ };
326
+
327
+ // get wsp_ggml_status name string
328
+ WSP_GGML_API const char * wsp_ggml_status_to_string(enum wsp_ggml_status status);
329
+
330
+ // ieee 754-2008 half-precision float16
331
+ // todo: make this not an integral type
332
+ typedef uint16_t wsp_ggml_fp16_t;
333
+ WSP_GGML_API float wsp_ggml_fp16_to_fp32(wsp_ggml_fp16_t);
334
+ WSP_GGML_API wsp_ggml_fp16_t wsp_ggml_fp32_to_fp16(float);
335
+ WSP_GGML_API void wsp_ggml_fp16_to_fp32_row(const wsp_ggml_fp16_t *, float *, int64_t);
336
+ WSP_GGML_API void wsp_ggml_fp32_to_fp16_row(const float *, wsp_ggml_fp16_t *, int64_t);
337
+
338
+ // google brain half-precision bfloat16
339
+ typedef struct { uint16_t bits; } wsp_ggml_bf16_t;
340
+ WSP_GGML_API wsp_ggml_bf16_t wsp_ggml_fp32_to_bf16(float);
341
+ WSP_GGML_API float wsp_ggml_bf16_to_fp32(wsp_ggml_bf16_t); // consider just doing << 16
342
+ WSP_GGML_API void wsp_ggml_bf16_to_fp32_row(const wsp_ggml_bf16_t *, float *, int64_t);
343
+ WSP_GGML_API void wsp_ggml_fp32_to_bf16_row_ref(const float *, wsp_ggml_bf16_t *, int64_t);
344
+ WSP_GGML_API void wsp_ggml_fp32_to_bf16_row(const float *, wsp_ggml_bf16_t *, int64_t);
345
+
346
+ struct wsp_ggml_object;
347
+ struct wsp_ggml_context;
348
+ struct wsp_ggml_cgraph;
349
+
350
+ // NOTE: always add types at the end of the enum to keep backward compatibility
351
+ enum wsp_ggml_type {
352
+ WSP_GGML_TYPE_F32 = 0,
353
+ WSP_GGML_TYPE_F16 = 1,
354
+ WSP_GGML_TYPE_Q4_0 = 2,
355
+ WSP_GGML_TYPE_Q4_1 = 3,
356
+ // WSP_GGML_TYPE_Q4_2 = 4, support has been removed
357
+ // WSP_GGML_TYPE_Q4_3 = 5, support has been removed
358
+ WSP_GGML_TYPE_Q5_0 = 6,
359
+ WSP_GGML_TYPE_Q5_1 = 7,
360
+ WSP_GGML_TYPE_Q8_0 = 8,
361
+ WSP_GGML_TYPE_Q8_1 = 9,
362
+ WSP_GGML_TYPE_Q2_K = 10,
363
+ WSP_GGML_TYPE_Q3_K = 11,
364
+ WSP_GGML_TYPE_Q4_K = 12,
365
+ WSP_GGML_TYPE_Q5_K = 13,
366
+ WSP_GGML_TYPE_Q6_K = 14,
367
+ WSP_GGML_TYPE_Q8_K = 15,
368
+ WSP_GGML_TYPE_IQ2_XXS = 16,
369
+ WSP_GGML_TYPE_IQ2_XS = 17,
370
+ WSP_GGML_TYPE_IQ3_XXS = 18,
371
+ WSP_GGML_TYPE_IQ1_S = 19,
372
+ WSP_GGML_TYPE_IQ4_NL = 20,
373
+ WSP_GGML_TYPE_IQ3_S = 21,
374
+ WSP_GGML_TYPE_IQ2_S = 22,
375
+ WSP_GGML_TYPE_IQ4_XS = 23,
376
+ WSP_GGML_TYPE_I8 = 24,
377
+ WSP_GGML_TYPE_I16 = 25,
378
+ WSP_GGML_TYPE_I32 = 26,
379
+ WSP_GGML_TYPE_I64 = 27,
380
+ WSP_GGML_TYPE_F64 = 28,
381
+ WSP_GGML_TYPE_IQ1_M = 29,
382
+ WSP_GGML_TYPE_BF16 = 30,
383
+ // WSP_GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
384
+ // WSP_GGML_TYPE_Q4_0_4_8 = 32,
385
+ // WSP_GGML_TYPE_Q4_0_8_8 = 33,
386
+ WSP_GGML_TYPE_TQ1_0 = 34,
387
+ WSP_GGML_TYPE_TQ2_0 = 35,
388
+ // WSP_GGML_TYPE_IQ4_NL_4_4 = 36,
389
+ // WSP_GGML_TYPE_IQ4_NL_4_8 = 37,
390
+ // WSP_GGML_TYPE_IQ4_NL_8_8 = 38,
391
+ WSP_GGML_TYPE_COUNT = 39,
392
+ };
393
+
394
+ // precision
395
+ enum wsp_ggml_prec {
396
+ WSP_GGML_PREC_DEFAULT = 0, // stored as wsp_ggml_tensor.op_params, 0 by default
397
+ WSP_GGML_PREC_F32 = 10,
398
+ };
399
+
400
+ // model file types
401
+ enum wsp_ggml_ftype {
402
+ WSP_GGML_FTYPE_UNKNOWN = -1,
403
+ WSP_GGML_FTYPE_ALL_F32 = 0,
404
+ WSP_GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
405
+ WSP_GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
406
+ WSP_GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
407
+ WSP_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
408
+ WSP_GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
409
+ WSP_GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
410
+ WSP_GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
411
+ WSP_GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
412
+ WSP_GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
413
+ WSP_GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
414
+ WSP_GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
415
+ WSP_GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
416
+ WSP_GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
417
+ WSP_GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
418
+ WSP_GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
419
+ WSP_GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
420
+ WSP_GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
421
+ WSP_GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
422
+ WSP_GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
423
+ WSP_GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
424
+ WSP_GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
425
+ WSP_GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
426
+ };
427
+
428
+ // available tensor operations:
429
+ enum wsp_ggml_op {
430
+ WSP_GGML_OP_NONE = 0,
431
+
432
+ WSP_GGML_OP_DUP,
433
+ WSP_GGML_OP_ADD,
434
+ WSP_GGML_OP_ADD1,
435
+ WSP_GGML_OP_ACC,
436
+ WSP_GGML_OP_SUB,
437
+ WSP_GGML_OP_MUL,
438
+ WSP_GGML_OP_DIV,
439
+ WSP_GGML_OP_SQR,
440
+ WSP_GGML_OP_SQRT,
441
+ WSP_GGML_OP_LOG,
442
+ WSP_GGML_OP_SIN,
443
+ WSP_GGML_OP_COS,
444
+ WSP_GGML_OP_SUM,
445
+ WSP_GGML_OP_SUM_ROWS,
446
+ WSP_GGML_OP_MEAN,
447
+ WSP_GGML_OP_ARGMAX,
448
+ WSP_GGML_OP_COUNT_EQUAL,
449
+ WSP_GGML_OP_REPEAT,
450
+ WSP_GGML_OP_REPEAT_BACK,
451
+ WSP_GGML_OP_CONCAT,
452
+ WSP_GGML_OP_SILU_BACK,
453
+ WSP_GGML_OP_NORM, // normalize
454
+ WSP_GGML_OP_RMS_NORM,
455
+ WSP_GGML_OP_RMS_NORM_BACK,
456
+ WSP_GGML_OP_GROUP_NORM,
457
+ WSP_GGML_OP_L2_NORM,
458
+
459
+ WSP_GGML_OP_MUL_MAT,
460
+ WSP_GGML_OP_MUL_MAT_ID,
461
+ WSP_GGML_OP_OUT_PROD,
462
+
463
+ WSP_GGML_OP_SCALE,
464
+ WSP_GGML_OP_SET,
465
+ WSP_GGML_OP_CPY,
466
+ WSP_GGML_OP_CONT,
467
+ WSP_GGML_OP_RESHAPE,
468
+ WSP_GGML_OP_VIEW,
469
+ WSP_GGML_OP_PERMUTE,
470
+ WSP_GGML_OP_TRANSPOSE,
471
+ WSP_GGML_OP_GET_ROWS,
472
+ WSP_GGML_OP_GET_ROWS_BACK,
473
+ WSP_GGML_OP_DIAG,
474
+ WSP_GGML_OP_DIAG_MASK_INF,
475
+ WSP_GGML_OP_DIAG_MASK_ZERO,
476
+ WSP_GGML_OP_SOFT_MAX,
477
+ WSP_GGML_OP_SOFT_MAX_BACK,
478
+ WSP_GGML_OP_ROPE,
479
+ WSP_GGML_OP_ROPE_BACK,
480
+ WSP_GGML_OP_CLAMP,
481
+ WSP_GGML_OP_CONV_TRANSPOSE_1D,
482
+ WSP_GGML_OP_IM2COL,
483
+ WSP_GGML_OP_IM2COL_BACK,
484
+ WSP_GGML_OP_CONV_2D_DW,
485
+ WSP_GGML_OP_CONV_TRANSPOSE_2D,
486
+ WSP_GGML_OP_POOL_1D,
487
+ WSP_GGML_OP_POOL_2D,
488
+ WSP_GGML_OP_POOL_2D_BACK,
489
+ WSP_GGML_OP_UPSCALE, // nearest interpolate
490
+ WSP_GGML_OP_PAD,
491
+ WSP_GGML_OP_PAD_REFLECT_1D,
492
+ WSP_GGML_OP_ROLL,
493
+ WSP_GGML_OP_ARANGE,
494
+ WSP_GGML_OP_TIMESTEP_EMBEDDING,
495
+ WSP_GGML_OP_ARGSORT,
496
+ WSP_GGML_OP_LEAKY_RELU,
497
+
498
+ WSP_GGML_OP_FLASH_ATTN_EXT,
499
+ WSP_GGML_OP_FLASH_ATTN_BACK,
500
+ WSP_GGML_OP_SSM_CONV,
501
+ WSP_GGML_OP_SSM_SCAN,
502
+ WSP_GGML_OP_WIN_PART,
503
+ WSP_GGML_OP_WIN_UNPART,
504
+ WSP_GGML_OP_GET_REL_POS,
505
+ WSP_GGML_OP_ADD_REL_POS,
506
+ WSP_GGML_OP_RWKV_WKV6,
507
+ WSP_GGML_OP_GATED_LINEAR_ATTN,
508
+ WSP_GGML_OP_RWKV_WKV7,
509
+
510
+ WSP_GGML_OP_UNARY,
511
+
512
+ WSP_GGML_OP_MAP_CUSTOM1,
513
+ WSP_GGML_OP_MAP_CUSTOM2,
514
+ WSP_GGML_OP_MAP_CUSTOM3,
515
+
516
+ WSP_GGML_OP_CUSTOM,
517
+
518
+ WSP_GGML_OP_CROSS_ENTROPY_LOSS,
519
+ WSP_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
520
+ WSP_GGML_OP_OPT_STEP_ADAMW,
521
+
522
+ WSP_GGML_OP_COUNT,
523
+ };
524
+
525
+ enum wsp_ggml_unary_op {
526
+ WSP_GGML_UNARY_OP_ABS,
527
+ WSP_GGML_UNARY_OP_SGN,
528
+ WSP_GGML_UNARY_OP_NEG,
529
+ WSP_GGML_UNARY_OP_STEP,
530
+ WSP_GGML_UNARY_OP_TANH,
531
+ WSP_GGML_UNARY_OP_ELU,
532
+ WSP_GGML_UNARY_OP_RELU,
533
+ WSP_GGML_UNARY_OP_SIGMOID,
534
+ WSP_GGML_UNARY_OP_GELU,
535
+ WSP_GGML_UNARY_OP_GELU_QUICK,
536
+ WSP_GGML_UNARY_OP_SILU,
537
+ WSP_GGML_UNARY_OP_HARDSWISH,
538
+ WSP_GGML_UNARY_OP_HARDSIGMOID,
539
+ WSP_GGML_UNARY_OP_EXP,
540
+ WSP_GGML_UNARY_OP_GELU_ERF,
541
+
542
+ WSP_GGML_UNARY_OP_COUNT,
543
+ };
544
+
545
+ enum wsp_ggml_object_type {
546
+ WSP_GGML_OBJECT_TYPE_TENSOR,
547
+ WSP_GGML_OBJECT_TYPE_GRAPH,
548
+ WSP_GGML_OBJECT_TYPE_WORK_BUFFER
549
+ };
550
+
551
+ enum wsp_ggml_log_level {
552
+ WSP_GGML_LOG_LEVEL_NONE = 0,
553
+ WSP_GGML_LOG_LEVEL_DEBUG = 1,
554
+ WSP_GGML_LOG_LEVEL_INFO = 2,
555
+ WSP_GGML_LOG_LEVEL_WARN = 3,
556
+ WSP_GGML_LOG_LEVEL_ERROR = 4,
557
+ WSP_GGML_LOG_LEVEL_CONT = 5, // continue previous log
558
+ };
559
+
560
+ // this tensor...
561
+ enum wsp_ggml_tensor_flag {
562
+ WSP_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
563
+ WSP_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
564
+ WSP_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
565
+ WSP_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
566
+ };
567
+
568
+ struct wsp_ggml_init_params {
569
+ // memory pool
570
+ size_t mem_size; // bytes
571
+ void * mem_buffer; // if NULL, memory will be allocated internally
572
+ bool no_alloc; // don't allocate memory for the tensor data
573
+ };
574
+
575
+ // n-dimensional tensor
576
+ struct wsp_ggml_tensor {
577
+ enum wsp_ggml_type type;
578
+
579
+ struct wsp_ggml_backend_buffer * buffer;
580
+
581
+ int64_t ne[WSP_GGML_MAX_DIMS]; // number of elements
582
+ size_t nb[WSP_GGML_MAX_DIMS]; // stride in bytes:
583
+ // nb[0] = wsp_ggml_type_size(type)
584
+ // nb[1] = nb[0] * (ne[0] / wsp_ggml_blck_size(type)) + padding
585
+ // nb[i] = nb[i-1] * ne[i-1]
586
+
587
+ // compute data
588
+ enum wsp_ggml_op op;
589
+
590
+ // op params - allocated as int32_t for alignment
591
+ int32_t op_params[WSP_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
592
+
593
+ int32_t flags;
594
+
595
+ struct wsp_ggml_tensor * src[WSP_GGML_MAX_SRC];
596
+
597
+ // source tensor and offset for views
598
+ struct wsp_ggml_tensor * view_src;
599
+ size_t view_offs;
600
+
601
+ void * data;
602
+
603
+ char name[WSP_GGML_MAX_NAME];
604
+
605
+ void * extra; // extra things e.g. for ggml-cuda.cu
606
+
607
+ char padding[8];
608
+ };
609
+
610
+ static const size_t WSP_GGML_TENSOR_SIZE = sizeof(struct wsp_ggml_tensor);
611
+
612
+ // Abort callback
613
+ // If not NULL, called before ggml computation
614
+ // If it returns true, the computation is aborted
615
+ typedef bool (*wsp_ggml_abort_callback)(void * data);
616
+
617
+
618
+ //
619
+ // GUID
620
+ //
621
+
622
+ // GUID types
623
+ typedef uint8_t wsp_ggml_guid[16];
624
+ typedef wsp_ggml_guid * wsp_ggml_guid_t;
625
+
626
+ WSP_GGML_API bool wsp_ggml_guid_matches(wsp_ggml_guid_t guid_a, wsp_ggml_guid_t guid_b);
627
+
628
+ // misc
629
+
630
+ WSP_GGML_API void wsp_ggml_time_init(void); // call this once at the beginning of the program
631
+ WSP_GGML_API int64_t wsp_ggml_time_ms(void);
632
+ WSP_GGML_API int64_t wsp_ggml_time_us(void);
633
+ WSP_GGML_API int64_t wsp_ggml_cycles(void);
634
+ WSP_GGML_API int64_t wsp_ggml_cycles_per_ms(void);
635
+
636
+ // accepts a UTF-8 path, even on Windows
637
+ WSP_GGML_API FILE * wsp_ggml_fopen(const char * fname, const char * mode);
638
+
639
+ WSP_GGML_API void wsp_ggml_print_object (const struct wsp_ggml_object * obj);
640
+ WSP_GGML_API void wsp_ggml_print_objects(const struct wsp_ggml_context * ctx);
641
+
642
+ WSP_GGML_API int64_t wsp_ggml_nelements (const struct wsp_ggml_tensor * tensor);
643
+ WSP_GGML_API int64_t wsp_ggml_nrows (const struct wsp_ggml_tensor * tensor);
644
+ WSP_GGML_API size_t wsp_ggml_nbytes (const struct wsp_ggml_tensor * tensor);
645
+ WSP_GGML_API size_t wsp_ggml_nbytes_pad(const struct wsp_ggml_tensor * tensor); // same as wsp_ggml_nbytes() but padded to WSP_GGML_MEM_ALIGN
646
+
647
+ WSP_GGML_API int64_t wsp_ggml_blck_size(enum wsp_ggml_type type);
648
+ WSP_GGML_API size_t wsp_ggml_type_size(enum wsp_ggml_type type); // size in bytes for all elements in a block
649
+ WSP_GGML_API size_t wsp_ggml_row_size (enum wsp_ggml_type type, int64_t ne); // size in bytes for all elements in a row
650
+
651
+ WSP_GGML_DEPRECATED(
652
+ WSP_GGML_API double wsp_ggml_type_sizef(enum wsp_ggml_type type), // wsp_ggml_type_size()/wsp_ggml_blck_size() as float
653
+ "use wsp_ggml_row_size() instead");
654
+
655
+ WSP_GGML_API const char * wsp_ggml_type_name(enum wsp_ggml_type type);
656
+ WSP_GGML_API const char * wsp_ggml_op_name (enum wsp_ggml_op op);
657
+ WSP_GGML_API const char * wsp_ggml_op_symbol(enum wsp_ggml_op op);
658
+
659
+ WSP_GGML_API const char * wsp_ggml_unary_op_name(enum wsp_ggml_unary_op op);
660
+ WSP_GGML_API const char * wsp_ggml_op_desc(const struct wsp_ggml_tensor * t); // unary or op name
661
+
662
+ WSP_GGML_API size_t wsp_ggml_element_size(const struct wsp_ggml_tensor * tensor);
663
+
664
+ WSP_GGML_API bool wsp_ggml_is_quantized(enum wsp_ggml_type type);
665
+
666
+ // TODO: temporary until model loading of ggml examples is refactored
667
+ WSP_GGML_API enum wsp_ggml_type wsp_ggml_ftype_to_wsp_ggml_type(enum wsp_ggml_ftype ftype);
668
+
669
+ WSP_GGML_API bool wsp_ggml_is_transposed(const struct wsp_ggml_tensor * tensor);
670
+ WSP_GGML_API bool wsp_ggml_is_permuted (const struct wsp_ggml_tensor * tensor);
671
+ WSP_GGML_API bool wsp_ggml_is_empty (const struct wsp_ggml_tensor * tensor);
672
+ WSP_GGML_API bool wsp_ggml_is_scalar (const struct wsp_ggml_tensor * tensor);
673
+ WSP_GGML_API bool wsp_ggml_is_vector (const struct wsp_ggml_tensor * tensor);
674
+ WSP_GGML_API bool wsp_ggml_is_matrix (const struct wsp_ggml_tensor * tensor);
675
+ WSP_GGML_API bool wsp_ggml_is_3d (const struct wsp_ggml_tensor * tensor);
676
+ WSP_GGML_API int wsp_ggml_n_dims (const struct wsp_ggml_tensor * tensor); // returns 1 for scalars
677
+
678
+ // returns whether the tensor elements can be iterated over with a flattened index (no gaps, no permutation)
679
+ WSP_GGML_API bool wsp_ggml_is_contiguous (const struct wsp_ggml_tensor * tensor);
680
+ WSP_GGML_API bool wsp_ggml_is_contiguous_0(const struct wsp_ggml_tensor * tensor); // same as wsp_ggml_is_contiguous()
681
+ WSP_GGML_API bool wsp_ggml_is_contiguous_1(const struct wsp_ggml_tensor * tensor); // contiguous for dims >= 1
682
+ WSP_GGML_API bool wsp_ggml_is_contiguous_2(const struct wsp_ggml_tensor * tensor); // contiguous for dims >= 2
683
+
684
+ // returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok)
685
+ WSP_GGML_API bool wsp_ggml_is_contiguously_allocated(const struct wsp_ggml_tensor * tensor);
686
+
687
+ // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN
688
+ WSP_GGML_API bool wsp_ggml_is_contiguous_channels(const struct wsp_ggml_tensor * tensor);
689
+
690
+ WSP_GGML_API bool wsp_ggml_are_same_shape (const struct wsp_ggml_tensor * t0, const struct wsp_ggml_tensor * t1);
691
+ WSP_GGML_API bool wsp_ggml_are_same_stride(const struct wsp_ggml_tensor * t0, const struct wsp_ggml_tensor * t1);
692
+
693
+ WSP_GGML_API bool wsp_ggml_can_repeat(const struct wsp_ggml_tensor * t0, const struct wsp_ggml_tensor * t1);
694
+
695
+ // use this to compute the memory overhead of a tensor
696
+ WSP_GGML_API size_t wsp_ggml_tensor_overhead(void);
697
+
698
+ WSP_GGML_API bool wsp_ggml_validate_row_data(enum wsp_ggml_type type, const void * data, size_t nbytes);
699
+
700
+ // main
701
+
702
+ WSP_GGML_API struct wsp_ggml_context * wsp_ggml_init (struct wsp_ggml_init_params params);
703
+ WSP_GGML_API void wsp_ggml_reset(struct wsp_ggml_context * ctx);
704
+ WSP_GGML_API void wsp_ggml_free (struct wsp_ggml_context * ctx);
705
+
706
+ WSP_GGML_API size_t wsp_ggml_used_mem(const struct wsp_ggml_context * ctx);
707
+
708
+ WSP_GGML_API bool wsp_ggml_get_no_alloc(struct wsp_ggml_context * ctx);
709
+ WSP_GGML_API void wsp_ggml_set_no_alloc(struct wsp_ggml_context * ctx, bool no_alloc);
710
+
711
+ WSP_GGML_API void * wsp_ggml_get_mem_buffer (const struct wsp_ggml_context * ctx);
712
+ WSP_GGML_API size_t wsp_ggml_get_mem_size (const struct wsp_ggml_context * ctx);
713
+ WSP_GGML_API size_t wsp_ggml_get_max_tensor_size(const struct wsp_ggml_context * ctx);
714
+
715
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_new_tensor(
716
+ struct wsp_ggml_context * ctx,
717
+ enum wsp_ggml_type type,
718
+ int n_dims,
719
+ const int64_t *ne);
720
+
721
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_new_tensor_1d(
722
+ struct wsp_ggml_context * ctx,
723
+ enum wsp_ggml_type type,
724
+ int64_t ne0);
725
+
726
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_new_tensor_2d(
727
+ struct wsp_ggml_context * ctx,
728
+ enum wsp_ggml_type type,
729
+ int64_t ne0,
730
+ int64_t ne1);
731
+
732
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_new_tensor_3d(
733
+ struct wsp_ggml_context * ctx,
734
+ enum wsp_ggml_type type,
735
+ int64_t ne0,
736
+ int64_t ne1,
737
+ int64_t ne2);
738
+
739
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_new_tensor_4d(
740
+ struct wsp_ggml_context * ctx,
741
+ enum wsp_ggml_type type,
742
+ int64_t ne0,
743
+ int64_t ne1,
744
+ int64_t ne2,
745
+ int64_t ne3);
746
+
747
+ WSP_GGML_API void * wsp_ggml_new_buffer(struct wsp_ggml_context * ctx, size_t nbytes);
748
+
749
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_dup_tensor (struct wsp_ggml_context * ctx, const struct wsp_ggml_tensor * src);
750
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_view_tensor(struct wsp_ggml_context * ctx, struct wsp_ggml_tensor * src);
751
+
752
+ // Context tensor enumeration and lookup
753
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_first_tensor(const struct wsp_ggml_context * ctx);
754
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_next_tensor (const struct wsp_ggml_context * ctx, struct wsp_ggml_tensor * tensor);
755
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_tensor(struct wsp_ggml_context * ctx, const char * name);
756
+
757
+ // Converts a flat index into coordinates
758
+ WSP_GGML_API void wsp_ggml_unravel_index(const struct wsp_ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
759
+
760
+ WSP_GGML_API enum wsp_ggml_unary_op wsp_ggml_get_unary_op(const struct wsp_ggml_tensor * tensor);
761
+
762
+ WSP_GGML_API void * wsp_ggml_get_data (const struct wsp_ggml_tensor * tensor);
763
+ WSP_GGML_API float * wsp_ggml_get_data_f32(const struct wsp_ggml_tensor * tensor);
764
+
765
+ WSP_GGML_API const char * wsp_ggml_get_name (const struct wsp_ggml_tensor * tensor);
766
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_name ( struct wsp_ggml_tensor * tensor, const char * name);
767
+ WSP_GGML_ATTRIBUTE_FORMAT(2, 3)
768
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_format_name( struct wsp_ggml_tensor * tensor, const char * fmt, ...);
769
+
770
+ // Tensor flags
771
+ WSP_GGML_API void wsp_ggml_set_input(struct wsp_ggml_tensor * tensor);
772
+ WSP_GGML_API void wsp_ggml_set_output(struct wsp_ggml_tensor * tensor);
773
+ WSP_GGML_API void wsp_ggml_set_param(struct wsp_ggml_tensor * tensor);
774
+ WSP_GGML_API void wsp_ggml_set_loss(struct wsp_ggml_tensor * tensor);
775
+
776
+ //
777
+ // operations on tensors with backpropagation
778
+ //
779
+
780
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_dup(
781
+ struct wsp_ggml_context * ctx,
782
+ struct wsp_ggml_tensor * a);
783
+
784
+ // in-place, returns view(a)
785
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_dup_inplace(
786
+ struct wsp_ggml_context * ctx,
787
+ struct wsp_ggml_tensor * a);
788
+
789
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add(
790
+ struct wsp_ggml_context * ctx,
791
+ struct wsp_ggml_tensor * a,
792
+ struct wsp_ggml_tensor * b);
793
+
794
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add_inplace(
795
+ struct wsp_ggml_context * ctx,
796
+ struct wsp_ggml_tensor * a,
797
+ struct wsp_ggml_tensor * b);
798
+
799
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add_cast(
800
+ struct wsp_ggml_context * ctx,
801
+ struct wsp_ggml_tensor * a,
802
+ struct wsp_ggml_tensor * b,
803
+ enum wsp_ggml_type type);
804
+
805
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add1(
806
+ struct wsp_ggml_context * ctx,
807
+ struct wsp_ggml_tensor * a,
808
+ struct wsp_ggml_tensor * b);
809
+
810
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add1_inplace(
811
+ struct wsp_ggml_context * ctx,
812
+ struct wsp_ggml_tensor * a,
813
+ struct wsp_ggml_tensor * b);
814
+
815
+ // dst = a
816
+ // view(dst, nb1, nb2, nb3, offset) += b
817
+ // return dst
818
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_acc(
819
+ struct wsp_ggml_context * ctx,
820
+ struct wsp_ggml_tensor * a,
821
+ struct wsp_ggml_tensor * b,
822
+ size_t nb1,
823
+ size_t nb2,
824
+ size_t nb3,
825
+ size_t offset);
826
+
827
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_acc_inplace(
828
+ struct wsp_ggml_context * ctx,
829
+ struct wsp_ggml_tensor * a,
830
+ struct wsp_ggml_tensor * b,
831
+ size_t nb1,
832
+ size_t nb2,
833
+ size_t nb3,
834
+ size_t offset);
835
+
836
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sub(
837
+ struct wsp_ggml_context * ctx,
838
+ struct wsp_ggml_tensor * a,
839
+ struct wsp_ggml_tensor * b);
840
+
841
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sub_inplace(
842
+ struct wsp_ggml_context * ctx,
843
+ struct wsp_ggml_tensor * a,
844
+ struct wsp_ggml_tensor * b);
845
+
846
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_mul(
847
+ struct wsp_ggml_context * ctx,
848
+ struct wsp_ggml_tensor * a,
849
+ struct wsp_ggml_tensor * b);
850
+
851
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_mul_inplace(
852
+ struct wsp_ggml_context * ctx,
853
+ struct wsp_ggml_tensor * a,
854
+ struct wsp_ggml_tensor * b);
855
+
856
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_div(
857
+ struct wsp_ggml_context * ctx,
858
+ struct wsp_ggml_tensor * a,
859
+ struct wsp_ggml_tensor * b);
860
+
861
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_div_inplace(
862
+ struct wsp_ggml_context * ctx,
863
+ struct wsp_ggml_tensor * a,
864
+ struct wsp_ggml_tensor * b);
865
+
866
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sqr(
867
+ struct wsp_ggml_context * ctx,
868
+ struct wsp_ggml_tensor * a);
869
+
870
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sqr_inplace(
871
+ struct wsp_ggml_context * ctx,
872
+ struct wsp_ggml_tensor * a);
873
+
874
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sqrt(
875
+ struct wsp_ggml_context * ctx,
876
+ struct wsp_ggml_tensor * a);
877
+
878
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sqrt_inplace(
879
+ struct wsp_ggml_context * ctx,
880
+ struct wsp_ggml_tensor * a);
881
+
882
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_log(
883
+ struct wsp_ggml_context * ctx,
884
+ struct wsp_ggml_tensor * a);
885
+
886
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_log_inplace(
887
+ struct wsp_ggml_context * ctx,
888
+ struct wsp_ggml_tensor * a);
889
+
890
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sin(
891
+ struct wsp_ggml_context * ctx,
892
+ struct wsp_ggml_tensor * a);
893
+
894
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sin_inplace(
895
+ struct wsp_ggml_context * ctx,
896
+ struct wsp_ggml_tensor * a);
897
+
898
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cos(
899
+ struct wsp_ggml_context * ctx,
900
+ struct wsp_ggml_tensor * a);
901
+
902
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cos_inplace(
903
+ struct wsp_ggml_context * ctx,
904
+ struct wsp_ggml_tensor * a);
905
+
906
+ // return scalar
907
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sum(
908
+ struct wsp_ggml_context * ctx,
909
+ struct wsp_ggml_tensor * a);
910
+
911
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
912
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sum_rows(
913
+ struct wsp_ggml_context * ctx,
914
+ struct wsp_ggml_tensor * a);
915
+
916
+ // mean along rows
917
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_mean(
918
+ struct wsp_ggml_context * ctx,
919
+ struct wsp_ggml_tensor * a);
920
+
921
+ // argmax along rows
922
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_argmax(
923
+ struct wsp_ggml_context * ctx,
924
+ struct wsp_ggml_tensor * a);
925
+
926
+ // count number of equal elements in a and b
927
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_count_equal(
928
+ struct wsp_ggml_context * ctx,
929
+ struct wsp_ggml_tensor * a,
930
+ struct wsp_ggml_tensor * b);
931
+
932
+ // if a is the same shape as b, and a is not parameter, return a
933
+ // otherwise, return a new tensor: repeat(a) to fit in b
934
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_repeat(
935
+ struct wsp_ggml_context * ctx,
936
+ struct wsp_ggml_tensor * a,
937
+ struct wsp_ggml_tensor * b);
938
+
939
+ // repeat a to the specified shape
940
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_repeat_4d(
941
+ struct wsp_ggml_context * ctx,
942
+ struct wsp_ggml_tensor * a,
943
+ int64_t ne0,
944
+ int64_t ne1,
945
+ int64_t ne2,
946
+ int64_t ne3);
947
+
948
+ // sums repetitions in a into shape of b
949
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_repeat_back(
950
+ struct wsp_ggml_context * ctx,
951
+ struct wsp_ggml_tensor * a,
952
+ struct wsp_ggml_tensor * b); // sum up values that are adjacent in dims > 0 instead of repeated with same stride
953
+
954
+ // concat a and b along dim
955
+ // used in stable-diffusion
956
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_concat(
957
+ struct wsp_ggml_context * ctx,
958
+ struct wsp_ggml_tensor * a,
959
+ struct wsp_ggml_tensor * b,
960
+ int dim);
961
+
962
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_abs(
963
+ struct wsp_ggml_context * ctx,
964
+ struct wsp_ggml_tensor * a);
965
+
966
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_abs_inplace(
967
+ struct wsp_ggml_context * ctx,
968
+ struct wsp_ggml_tensor * a);
969
+
970
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sgn(
971
+ struct wsp_ggml_context * ctx,
972
+ struct wsp_ggml_tensor * a);
973
+
974
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sgn_inplace(
975
+ struct wsp_ggml_context * ctx,
976
+ struct wsp_ggml_tensor * a);
977
+
978
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_neg(
979
+ struct wsp_ggml_context * ctx,
980
+ struct wsp_ggml_tensor * a);
981
+
982
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_neg_inplace(
983
+ struct wsp_ggml_context * ctx,
984
+ struct wsp_ggml_tensor * a);
985
+
986
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_step(
987
+ struct wsp_ggml_context * ctx,
988
+ struct wsp_ggml_tensor * a);
989
+
990
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_step_inplace(
991
+ struct wsp_ggml_context * ctx,
992
+ struct wsp_ggml_tensor * a);
993
+
994
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_tanh(
995
+ struct wsp_ggml_context * ctx,
996
+ struct wsp_ggml_tensor * a);
997
+
998
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_tanh_inplace(
999
+ struct wsp_ggml_context * ctx,
1000
+ struct wsp_ggml_tensor * a);
1001
+
1002
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_elu(
1003
+ struct wsp_ggml_context * ctx,
1004
+ struct wsp_ggml_tensor * a);
1005
+
1006
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_elu_inplace(
1007
+ struct wsp_ggml_context * ctx,
1008
+ struct wsp_ggml_tensor * a);
1009
+
1010
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_relu(
1011
+ struct wsp_ggml_context * ctx,
1012
+ struct wsp_ggml_tensor * a);
1013
+
1014
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_leaky_relu(
1015
+ struct wsp_ggml_context * ctx,
1016
+ struct wsp_ggml_tensor * a, float negative_slope, bool inplace);
1017
+
1018
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_relu_inplace(
1019
+ struct wsp_ggml_context * ctx,
1020
+ struct wsp_ggml_tensor * a);
1021
+
1022
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sigmoid(
1023
+ struct wsp_ggml_context * ctx,
1024
+ struct wsp_ggml_tensor * a);
1025
+
1026
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_sigmoid_inplace(
1027
+ struct wsp_ggml_context * ctx,
1028
+ struct wsp_ggml_tensor * a);
1029
+
1030
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu(
1031
+ struct wsp_ggml_context * ctx,
1032
+ struct wsp_ggml_tensor * a);
1033
+
1034
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu_inplace(
1035
+ struct wsp_ggml_context * ctx,
1036
+ struct wsp_ggml_tensor * a);
1037
+
1038
+ // GELU using erf (error function) when possible
1039
+ // some backends may fallback to approximation based on Abramowitz and Stegun formula
1040
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu_erf(
1041
+ struct wsp_ggml_context * ctx,
1042
+ struct wsp_ggml_tensor * a);
1043
+
1044
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu_erf_inplace(
1045
+ struct wsp_ggml_context * ctx,
1046
+ struct wsp_ggml_tensor * a);
1047
+
1048
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu_quick(
1049
+ struct wsp_ggml_context * ctx,
1050
+ struct wsp_ggml_tensor * a);
1051
+
1052
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gelu_quick_inplace(
1053
+ struct wsp_ggml_context * ctx,
1054
+ struct wsp_ggml_tensor * a);
1055
+
1056
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_silu(
1057
+ struct wsp_ggml_context * ctx,
1058
+ struct wsp_ggml_tensor * a);
1059
+
1060
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_silu_inplace(
1061
+ struct wsp_ggml_context * ctx,
1062
+ struct wsp_ggml_tensor * a);
1063
+
1064
+ // a - x
1065
+ // b - dy
1066
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_silu_back(
1067
+ struct wsp_ggml_context * ctx,
1068
+ struct wsp_ggml_tensor * a,
1069
+ struct wsp_ggml_tensor * b);
1070
+
1071
+ // hardswish(x) = x * relu6(x + 3) / 6
1072
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_hardswish(
1073
+ struct wsp_ggml_context * ctx,
1074
+ struct wsp_ggml_tensor * a);
1075
+
1076
+ // hardsigmoid(x) = relu6(x + 3) / 6
1077
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_hardsigmoid(
1078
+ struct wsp_ggml_context * ctx,
1079
+ struct wsp_ggml_tensor * a);
1080
+
1081
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_exp(
1082
+ struct wsp_ggml_context * ctx,
1083
+ struct wsp_ggml_tensor * a);
1084
+
1085
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_exp_inplace(
1086
+ struct wsp_ggml_context * ctx,
1087
+ struct wsp_ggml_tensor * a);
1088
+
1089
+ // normalize along rows
1090
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_norm(
1091
+ struct wsp_ggml_context * ctx,
1092
+ struct wsp_ggml_tensor * a,
1093
+ float eps);
1094
+
1095
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_norm_inplace(
1096
+ struct wsp_ggml_context * ctx,
1097
+ struct wsp_ggml_tensor * a,
1098
+ float eps);
1099
+
1100
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rms_norm(
1101
+ struct wsp_ggml_context * ctx,
1102
+ struct wsp_ggml_tensor * a,
1103
+ float eps);
1104
+
1105
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rms_norm_inplace(
1106
+ struct wsp_ggml_context * ctx,
1107
+ struct wsp_ggml_tensor * a,
1108
+ float eps);
1109
+
1110
+ // group normalize along ne0*ne1*n_groups
1111
+ // used in stable-diffusion
1112
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_group_norm(
1113
+ struct wsp_ggml_context * ctx,
1114
+ struct wsp_ggml_tensor * a,
1115
+ int n_groups,
1116
+ float eps);
1117
+
1118
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_group_norm_inplace(
1119
+ struct wsp_ggml_context * ctx,
1120
+ struct wsp_ggml_tensor * a,
1121
+ int n_groups,
1122
+ float eps);
1123
+
1124
+ // l2 normalize along rows
1125
+ // used in rwkv v7
1126
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_l2_norm(
1127
+ struct wsp_ggml_context * ctx,
1128
+ struct wsp_ggml_tensor * a,
1129
+ float eps);
1130
+
1131
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_l2_norm_inplace(
1132
+ struct wsp_ggml_context * ctx,
1133
+ struct wsp_ggml_tensor * a,
1134
+ float eps);
1135
+
1136
+ // a - x
1137
+ // b - dy
1138
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rms_norm_back(
1139
+ struct wsp_ggml_context * ctx,
1140
+ struct wsp_ggml_tensor * a,
1141
+ struct wsp_ggml_tensor * b,
1142
+ float eps);
1143
+
1144
+ // A: k columns, n rows => [ne03, ne02, n, k]
1145
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1146
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1147
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_mul_mat(
1148
+ struct wsp_ggml_context * ctx,
1149
+ struct wsp_ggml_tensor * a,
1150
+ struct wsp_ggml_tensor * b);
1151
+
1152
+ // change the precision of a matrix multiplication
1153
+ // set to WSP_GGML_PREC_F32 for higher precision (useful for phi-2)
1154
+ WSP_GGML_API void wsp_ggml_mul_mat_set_prec(
1155
+ struct wsp_ggml_tensor * a,
1156
+ enum wsp_ggml_prec prec);
1157
+
1158
+ // indirect matrix multiplication
1159
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_mul_mat_id(
1160
+ struct wsp_ggml_context * ctx,
1161
+ struct wsp_ggml_tensor * as,
1162
+ struct wsp_ggml_tensor * b,
1163
+ struct wsp_ggml_tensor * ids);
1164
+
1165
+ // A: m columns, n rows,
1166
+ // B: p columns, n rows,
1167
+ // result is m columns, p rows
1168
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_out_prod(
1169
+ struct wsp_ggml_context * ctx,
1170
+ struct wsp_ggml_tensor * a,
1171
+ struct wsp_ggml_tensor * b);
1172
+
1173
+ //
1174
+ // operations on tensors without backpropagation
1175
+ //
1176
+
1177
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_scale(
1178
+ struct wsp_ggml_context * ctx,
1179
+ struct wsp_ggml_tensor * a,
1180
+ float s);
1181
+
1182
+ // in-place, returns view(a)
1183
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_scale_inplace(
1184
+ struct wsp_ggml_context * ctx,
1185
+ struct wsp_ggml_tensor * a,
1186
+ float s);
1187
+
1188
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1189
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set(
1190
+ struct wsp_ggml_context * ctx,
1191
+ struct wsp_ggml_tensor * a,
1192
+ struct wsp_ggml_tensor * b,
1193
+ size_t nb1,
1194
+ size_t nb2,
1195
+ size_t nb3,
1196
+ size_t offset); // in bytes
1197
+
1198
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1199
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_inplace(
1200
+ struct wsp_ggml_context * ctx,
1201
+ struct wsp_ggml_tensor * a,
1202
+ struct wsp_ggml_tensor * b,
1203
+ size_t nb1,
1204
+ size_t nb2,
1205
+ size_t nb3,
1206
+ size_t offset); // in bytes
1207
+
1208
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_1d(
1209
+ struct wsp_ggml_context * ctx,
1210
+ struct wsp_ggml_tensor * a,
1211
+ struct wsp_ggml_tensor * b,
1212
+ size_t offset); // in bytes
1213
+
1214
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_1d_inplace(
1215
+ struct wsp_ggml_context * ctx,
1216
+ struct wsp_ggml_tensor * a,
1217
+ struct wsp_ggml_tensor * b,
1218
+ size_t offset); // in bytes
1219
+
1220
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1221
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_2d(
1222
+ struct wsp_ggml_context * ctx,
1223
+ struct wsp_ggml_tensor * a,
1224
+ struct wsp_ggml_tensor * b,
1225
+ size_t nb1,
1226
+ size_t offset); // in bytes
1227
+
1228
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1229
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_2d_inplace(
1230
+ struct wsp_ggml_context * ctx,
1231
+ struct wsp_ggml_tensor * a,
1232
+ struct wsp_ggml_tensor * b,
1233
+ size_t nb1,
1234
+ size_t offset); // in bytes
1235
+
1236
+ // a -> b, return view(b)
1237
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cpy(
1238
+ struct wsp_ggml_context * ctx,
1239
+ struct wsp_ggml_tensor * a,
1240
+ struct wsp_ggml_tensor * b);
1241
+
1242
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cast(
1243
+ struct wsp_ggml_context * ctx,
1244
+ struct wsp_ggml_tensor * a,
1245
+ enum wsp_ggml_type type);
1246
+
1247
+ // make contiguous
1248
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cont(
1249
+ struct wsp_ggml_context * ctx,
1250
+ struct wsp_ggml_tensor * a);
1251
+
1252
+ // make contiguous, with new shape
1253
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cont_1d(
1254
+ struct wsp_ggml_context * ctx,
1255
+ struct wsp_ggml_tensor * a,
1256
+ int64_t ne0);
1257
+
1258
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cont_2d(
1259
+ struct wsp_ggml_context * ctx,
1260
+ struct wsp_ggml_tensor * a,
1261
+ int64_t ne0,
1262
+ int64_t ne1);
1263
+
1264
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cont_3d(
1265
+ struct wsp_ggml_context * ctx,
1266
+ struct wsp_ggml_tensor * a,
1267
+ int64_t ne0,
1268
+ int64_t ne1,
1269
+ int64_t ne2);
1270
+
1271
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cont_4d(
1272
+ struct wsp_ggml_context * ctx,
1273
+ struct wsp_ggml_tensor * a,
1274
+ int64_t ne0,
1275
+ int64_t ne1,
1276
+ int64_t ne2,
1277
+ int64_t ne3);
1278
+
1279
+ // return view(a), b specifies the new shape
1280
+ // TODO: when we start computing gradient, make a copy instead of view
1281
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_reshape(
1282
+ struct wsp_ggml_context * ctx,
1283
+ struct wsp_ggml_tensor * a,
1284
+ struct wsp_ggml_tensor * b);
1285
+
1286
+ // return view(a)
1287
+ // TODO: when we start computing gradient, make a copy instead of view
1288
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_reshape_1d(
1289
+ struct wsp_ggml_context * ctx,
1290
+ struct wsp_ggml_tensor * a,
1291
+ int64_t ne0);
1292
+
1293
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_reshape_2d(
1294
+ struct wsp_ggml_context * ctx,
1295
+ struct wsp_ggml_tensor * a,
1296
+ int64_t ne0,
1297
+ int64_t ne1);
1298
+
1299
+ // return view(a)
1300
+ // TODO: when we start computing gradient, make a copy instead of view
1301
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_reshape_3d(
1302
+ struct wsp_ggml_context * ctx,
1303
+ struct wsp_ggml_tensor * a,
1304
+ int64_t ne0,
1305
+ int64_t ne1,
1306
+ int64_t ne2);
1307
+
1308
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_reshape_4d(
1309
+ struct wsp_ggml_context * ctx,
1310
+ struct wsp_ggml_tensor * a,
1311
+ int64_t ne0,
1312
+ int64_t ne1,
1313
+ int64_t ne2,
1314
+ int64_t ne3);
1315
+
1316
+ // offset in bytes
1317
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_view_1d(
1318
+ struct wsp_ggml_context * ctx,
1319
+ struct wsp_ggml_tensor * a,
1320
+ int64_t ne0,
1321
+ size_t offset);
1322
+
1323
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_view_2d(
1324
+ struct wsp_ggml_context * ctx,
1325
+ struct wsp_ggml_tensor * a,
1326
+ int64_t ne0,
1327
+ int64_t ne1,
1328
+ size_t nb1, // row stride in bytes
1329
+ size_t offset);
1330
+
1331
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_view_3d(
1332
+ struct wsp_ggml_context * ctx,
1333
+ struct wsp_ggml_tensor * a,
1334
+ int64_t ne0,
1335
+ int64_t ne1,
1336
+ int64_t ne2,
1337
+ size_t nb1, // row stride in bytes
1338
+ size_t nb2, // slice stride in bytes
1339
+ size_t offset);
1340
+
1341
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_view_4d(
1342
+ struct wsp_ggml_context * ctx,
1343
+ struct wsp_ggml_tensor * a,
1344
+ int64_t ne0,
1345
+ int64_t ne1,
1346
+ int64_t ne2,
1347
+ int64_t ne3,
1348
+ size_t nb1, // row stride in bytes
1349
+ size_t nb2, // slice stride in bytes
1350
+ size_t nb3,
1351
+ size_t offset);
1352
+
1353
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_permute(
1354
+ struct wsp_ggml_context * ctx,
1355
+ struct wsp_ggml_tensor * a,
1356
+ int axis0,
1357
+ int axis1,
1358
+ int axis2,
1359
+ int axis3);
1360
+
1361
+ // alias for wsp_ggml_permute(ctx, a, 1, 0, 2, 3)
1362
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_transpose(
1363
+ struct wsp_ggml_context * ctx,
1364
+ struct wsp_ggml_tensor * a);
1365
+
1366
+ // supports 3D: a->ne[2] == b->ne[1]
1367
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_rows(
1368
+ struct wsp_ggml_context * ctx,
1369
+ struct wsp_ggml_tensor * a, // data
1370
+ struct wsp_ggml_tensor * b); // row indices
1371
+
1372
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_rows_back(
1373
+ struct wsp_ggml_context * ctx,
1374
+ struct wsp_ggml_tensor * a, // gradients of wsp_ggml_get_rows result
1375
+ struct wsp_ggml_tensor * b, // row indices
1376
+ struct wsp_ggml_tensor * c); // data for wsp_ggml_get_rows, only used for its shape
1377
+
1378
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_diag(
1379
+ struct wsp_ggml_context * ctx,
1380
+ struct wsp_ggml_tensor * a);
1381
+
1382
+ // set elements above the diagonal to -INF
1383
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_diag_mask_inf(
1384
+ struct wsp_ggml_context * ctx,
1385
+ struct wsp_ggml_tensor * a,
1386
+ int n_past);
1387
+
1388
+ // in-place, returns view(a)
1389
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_diag_mask_inf_inplace(
1390
+ struct wsp_ggml_context * ctx,
1391
+ struct wsp_ggml_tensor * a,
1392
+ int n_past);
1393
+
1394
+ // set elements above the diagonal to 0
1395
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_diag_mask_zero(
1396
+ struct wsp_ggml_context * ctx,
1397
+ struct wsp_ggml_tensor * a,
1398
+ int n_past);
1399
+
1400
+ // in-place, returns view(a)
1401
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_diag_mask_zero_inplace(
1402
+ struct wsp_ggml_context * ctx,
1403
+ struct wsp_ggml_tensor * a,
1404
+ int n_past);
1405
+
1406
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_soft_max(
1407
+ struct wsp_ggml_context * ctx,
1408
+ struct wsp_ggml_tensor * a);
1409
+
1410
+ // in-place, returns view(a)
1411
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_soft_max_inplace(
1412
+ struct wsp_ggml_context * ctx,
1413
+ struct wsp_ggml_tensor * a);
1414
+
1415
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1416
+ // mask is optional
1417
+ // max_bias = 0.0f for no ALiBi
1418
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_soft_max_ext(
1419
+ struct wsp_ggml_context * ctx,
1420
+ struct wsp_ggml_tensor * a,
1421
+ struct wsp_ggml_tensor * mask,
1422
+ float scale,
1423
+ float max_bias);
1424
+
1425
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_soft_max_ext_back(
1426
+ struct wsp_ggml_context * ctx,
1427
+ struct wsp_ggml_tensor * a,
1428
+ struct wsp_ggml_tensor * b,
1429
+ float scale,
1430
+ float max_bias);
1431
+
1432
+ // in-place, returns view(a)
1433
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_soft_max_ext_back_inplace(
1434
+ struct wsp_ggml_context * ctx,
1435
+ struct wsp_ggml_tensor * a,
1436
+ struct wsp_ggml_tensor * b,
1437
+ float scale,
1438
+ float max_bias);
1439
+
1440
+ // rotary position embedding
1441
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1442
+ // if (mode & WSP_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1443
+ //
1444
+ // b is an int32 vector with size a->ne[2], it contains the positions
1445
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope(
1446
+ struct wsp_ggml_context * ctx,
1447
+ struct wsp_ggml_tensor * a,
1448
+ struct wsp_ggml_tensor * b,
1449
+ int n_dims,
1450
+ int mode);
1451
+
1452
+ // in-place, returns view(a)
1453
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_inplace(
1454
+ struct wsp_ggml_context * ctx,
1455
+ struct wsp_ggml_tensor * a,
1456
+ struct wsp_ggml_tensor * b,
1457
+ int n_dims,
1458
+ int mode);
1459
+
1460
+ // custom RoPE
1461
+ // c is freq factors (e.g. phi3-128k), (optional)
1462
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_ext(
1463
+ struct wsp_ggml_context * ctx,
1464
+ struct wsp_ggml_tensor * a,
1465
+ struct wsp_ggml_tensor * b,
1466
+ struct wsp_ggml_tensor * c,
1467
+ int n_dims,
1468
+ int mode,
1469
+ int n_ctx_orig,
1470
+ float freq_base,
1471
+ float freq_scale,
1472
+ float ext_factor,
1473
+ float attn_factor,
1474
+ float beta_fast,
1475
+ float beta_slow);
1476
+
1477
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_multi(
1478
+ struct wsp_ggml_context * ctx,
1479
+ struct wsp_ggml_tensor * a,
1480
+ struct wsp_ggml_tensor * b,
1481
+ struct wsp_ggml_tensor * c,
1482
+ int n_dims,
1483
+ int sections[4],
1484
+ int mode,
1485
+ int n_ctx_orig,
1486
+ float freq_base,
1487
+ float freq_scale,
1488
+ float ext_factor,
1489
+ float attn_factor,
1490
+ float beta_fast,
1491
+ float beta_slow);
1492
+
1493
+ // in-place, returns view(a)
1494
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_ext_inplace(
1495
+ struct wsp_ggml_context * ctx,
1496
+ struct wsp_ggml_tensor * a,
1497
+ struct wsp_ggml_tensor * b,
1498
+ struct wsp_ggml_tensor * c,
1499
+ int n_dims,
1500
+ int mode,
1501
+ int n_ctx_orig,
1502
+ float freq_base,
1503
+ float freq_scale,
1504
+ float ext_factor,
1505
+ float attn_factor,
1506
+ float beta_fast,
1507
+ float beta_slow);
1508
+
1509
+ WSP_GGML_DEPRECATED(WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_custom(
1510
+ struct wsp_ggml_context * ctx,
1511
+ struct wsp_ggml_tensor * a,
1512
+ struct wsp_ggml_tensor * b,
1513
+ int n_dims,
1514
+ int mode,
1515
+ int n_ctx_orig,
1516
+ float freq_base,
1517
+ float freq_scale,
1518
+ float ext_factor,
1519
+ float attn_factor,
1520
+ float beta_fast,
1521
+ float beta_slow),
1522
+ "use wsp_ggml_rope_ext instead");
1523
+
1524
+ WSP_GGML_DEPRECATED(WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_custom_inplace(
1525
+ struct wsp_ggml_context * ctx,
1526
+ struct wsp_ggml_tensor * a,
1527
+ struct wsp_ggml_tensor * b,
1528
+ int n_dims,
1529
+ int mode,
1530
+ int n_ctx_orig,
1531
+ float freq_base,
1532
+ float freq_scale,
1533
+ float ext_factor,
1534
+ float attn_factor,
1535
+ float beta_fast,
1536
+ float beta_slow),
1537
+ "use wsp_ggml_rope_ext_inplace instead");
1538
+
1539
+ // compute correction dims for YaRN RoPE scaling
1540
+ WSP_GGML_API void wsp_ggml_rope_yarn_corr_dims(
1541
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1542
+
1543
+ // rotary position embedding backward, i.e compute dx from dy
1544
+ // a - dy
1545
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_ext_back(
1546
+ struct wsp_ggml_context * ctx,
1547
+ struct wsp_ggml_tensor * a, // gradients of wsp_ggml_rope result
1548
+ struct wsp_ggml_tensor * b, // positions
1549
+ struct wsp_ggml_tensor * c, // freq factors
1550
+ int n_dims,
1551
+ int mode,
1552
+ int n_ctx_orig,
1553
+ float freq_base,
1554
+ float freq_scale,
1555
+ float ext_factor,
1556
+ float attn_factor,
1557
+ float beta_fast,
1558
+ float beta_slow);
1559
+
1560
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rope_multi_back(
1561
+ struct wsp_ggml_context * ctx,
1562
+ struct wsp_ggml_tensor * a,
1563
+ struct wsp_ggml_tensor * b,
1564
+ struct wsp_ggml_tensor * c,
1565
+ int n_dims,
1566
+ int sections[4],
1567
+ int mode,
1568
+ int n_ctx_orig,
1569
+ float freq_base,
1570
+ float freq_scale,
1571
+ float ext_factor,
1572
+ float attn_factor,
1573
+ float beta_fast,
1574
+ float beta_slow);
1575
+
1576
+
1577
+ // clamp
1578
+ // in-place, returns view(a)
1579
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_clamp(
1580
+ struct wsp_ggml_context * ctx,
1581
+ struct wsp_ggml_tensor * a,
1582
+ float min,
1583
+ float max);
1584
+
1585
+ // im2col
1586
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1587
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_im2col(
1588
+ struct wsp_ggml_context * ctx,
1589
+ struct wsp_ggml_tensor * a, // convolution kernel
1590
+ struct wsp_ggml_tensor * b, // data
1591
+ int s0, // stride dimension 0
1592
+ int s1, // stride dimension 1
1593
+ int p0, // padding dimension 0
1594
+ int p1, // padding dimension 1
1595
+ int d0, // dilation dimension 0
1596
+ int d1, // dilation dimension 1
1597
+ bool is_2D,
1598
+ enum wsp_ggml_type dst_type);
1599
+
1600
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_im2col_back(
1601
+ struct wsp_ggml_context * ctx,
1602
+ struct wsp_ggml_tensor * a, // convolution kernel
1603
+ struct wsp_ggml_tensor * b, // gradient of im2col output
1604
+ int64_t * ne, // shape of im2col input
1605
+ int s0, // stride dimension 0
1606
+ int s1, // stride dimension 1
1607
+ int p0, // padding dimension 0
1608
+ int p1, // padding dimension 1
1609
+ int d0, // dilation dimension 0
1610
+ int d1, // dilation dimension 1
1611
+ bool is_2D);
1612
+
1613
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_1d(
1614
+ struct wsp_ggml_context * ctx,
1615
+ struct wsp_ggml_tensor * a, // convolution kernel
1616
+ struct wsp_ggml_tensor * b, // data
1617
+ int s0, // stride
1618
+ int p0, // padding
1619
+ int d0); // dilation
1620
+
1621
+ // conv_1d with padding = half
1622
+ // alias for wsp_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1623
+ WSP_GGML_API struct wsp_ggml_tensor* wsp_ggml_conv_1d_ph(
1624
+ struct wsp_ggml_context * ctx,
1625
+ struct wsp_ggml_tensor * a, // convolution kernel
1626
+ struct wsp_ggml_tensor * b, // data
1627
+ int s, // stride
1628
+ int d); // dilation
1629
+
1630
+ // depthwise
1631
+ // TODO: this is very likely wrong for some cases! - needs more testing
1632
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_1d_dw(
1633
+ struct wsp_ggml_context * ctx,
1634
+ struct wsp_ggml_tensor * a, // convolution kernel
1635
+ struct wsp_ggml_tensor * b, // data
1636
+ int s0, // stride
1637
+ int p0, // padding
1638
+ int d0); // dilation
1639
+
1640
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_1d_dw_ph(
1641
+ struct wsp_ggml_context * ctx,
1642
+ struct wsp_ggml_tensor * a, // convolution kernel
1643
+ struct wsp_ggml_tensor * b, // data
1644
+ int s0, // stride
1645
+ int d0); // dilation
1646
+
1647
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_transpose_1d(
1648
+ struct wsp_ggml_context * ctx,
1649
+ struct wsp_ggml_tensor * a, // convolution kernel
1650
+ struct wsp_ggml_tensor * b, // data
1651
+ int s0, // stride
1652
+ int p0, // padding
1653
+ int d0); // dilation
1654
+
1655
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_2d(
1656
+ struct wsp_ggml_context * ctx,
1657
+ struct wsp_ggml_tensor * a, // convolution kernel
1658
+ struct wsp_ggml_tensor * b, // data
1659
+ int s0, // stride dimension 0
1660
+ int s1, // stride dimension 1
1661
+ int p0, // padding dimension 0
1662
+ int p1, // padding dimension 1
1663
+ int d0, // dilation dimension 0
1664
+ int d1); // dilation dimension 1
1665
+
1666
+ // kernel size is a->ne[0] x a->ne[1]
1667
+ // stride is equal to kernel size
1668
+ // padding is zero
1669
+ // example:
1670
+ // a: 16 16 3 768
1671
+ // b: 1024 1024 3 1
1672
+ // res: 64 64 768 1
1673
+ // used in sam
1674
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_2d_sk_p0(
1675
+ struct wsp_ggml_context * ctx,
1676
+ struct wsp_ggml_tensor * a,
1677
+ struct wsp_ggml_tensor * b);
1678
+
1679
+ // kernel size is a->ne[0] x a->ne[1]
1680
+ // stride is 1
1681
+ // padding is half
1682
+ // example:
1683
+ // a: 3 3 256 256
1684
+ // b: 64 64 256 1
1685
+ // res: 64 64 256 1
1686
+ // used in sam
1687
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_2d_s1_ph(
1688
+ struct wsp_ggml_context * ctx,
1689
+ struct wsp_ggml_tensor * a,
1690
+ struct wsp_ggml_tensor * b);
1691
+
1692
+ // depthwise (via im2col and mul_mat)
1693
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_2d_dw(
1694
+ struct wsp_ggml_context * ctx,
1695
+ struct wsp_ggml_tensor * a, // convolution kernel
1696
+ struct wsp_ggml_tensor * b, // data
1697
+ int s0, // stride dimension 0
1698
+ int s1, // stride dimension 1
1699
+ int p0, // padding dimension 0
1700
+ int p1, // padding dimension 1
1701
+ int d0, // dilation dimension 0
1702
+ int d1); // dilation dimension 1
1703
+
1704
+ // Depthwise 2D convolution
1705
+ // may be faster than wsp_ggml_conv_2d_dw, but not available in all backends
1706
+ // a: KW KH 1 C convolution kernel
1707
+ // b: W H C N input data
1708
+ // res: W_out H_out C N
1709
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_2d_dw_direct(
1710
+ struct wsp_ggml_context * ctx,
1711
+ struct wsp_ggml_tensor * a,
1712
+ struct wsp_ggml_tensor * b,
1713
+ int stride0,
1714
+ int stride1,
1715
+ int pad0,
1716
+ int pad1,
1717
+ int dilation0,
1718
+ int dilation1);
1719
+
1720
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_conv_transpose_2d_p0(
1721
+ struct wsp_ggml_context * ctx,
1722
+ struct wsp_ggml_tensor * a,
1723
+ struct wsp_ggml_tensor * b,
1724
+ int stride);
1725
+
1726
+ enum wsp_ggml_op_pool {
1727
+ WSP_GGML_OP_POOL_MAX,
1728
+ WSP_GGML_OP_POOL_AVG,
1729
+ WSP_GGML_OP_POOL_COUNT,
1730
+ };
1731
+
1732
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_pool_1d(
1733
+ struct wsp_ggml_context * ctx,
1734
+ struct wsp_ggml_tensor * a,
1735
+ enum wsp_ggml_op_pool op,
1736
+ int k0, // kernel size
1737
+ int s0, // stride
1738
+ int p0); // padding
1739
+
1740
+ // the result will have 2*p0 padding for the first dimension
1741
+ // and 2*p1 padding for the second dimension
1742
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_pool_2d(
1743
+ struct wsp_ggml_context * ctx,
1744
+ struct wsp_ggml_tensor * a,
1745
+ enum wsp_ggml_op_pool op,
1746
+ int k0,
1747
+ int k1,
1748
+ int s0,
1749
+ int s1,
1750
+ float p0,
1751
+ float p1);
1752
+
1753
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_pool_2d_back(
1754
+ struct wsp_ggml_context * ctx,
1755
+ struct wsp_ggml_tensor * a,
1756
+ struct wsp_ggml_tensor * af, // "a"/input used in forward pass
1757
+ enum wsp_ggml_op_pool op,
1758
+ int k0,
1759
+ int k1,
1760
+ int s0,
1761
+ int s1,
1762
+ float p0,
1763
+ float p1);
1764
+
1765
+ enum wsp_ggml_scale_mode {
1766
+ WSP_GGML_SCALE_MODE_NEAREST = 0,
1767
+ WSP_GGML_SCALE_MODE_BILINEAR = 1,
1768
+ };
1769
+
1770
+ // interpolate
1771
+ // multiplies ne0 and ne1 by scale factor
1772
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_upscale(
1773
+ struct wsp_ggml_context * ctx,
1774
+ struct wsp_ggml_tensor * a,
1775
+ int scale_factor,
1776
+ enum wsp_ggml_scale_mode mode);
1777
+
1778
+ // interpolate
1779
+ // interpolate scale to specified dimensions
1780
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_upscale_ext(
1781
+ struct wsp_ggml_context * ctx,
1782
+ struct wsp_ggml_tensor * a,
1783
+ int ne0,
1784
+ int ne1,
1785
+ int ne2,
1786
+ int ne3,
1787
+ enum wsp_ggml_scale_mode mode);
1788
+
1789
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1790
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_pad(
1791
+ struct wsp_ggml_context * ctx,
1792
+ struct wsp_ggml_tensor * a,
1793
+ int p0,
1794
+ int p1,
1795
+ int p2,
1796
+ int p3);
1797
+
1798
+ // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
1799
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_pad_reflect_1d(
1800
+ struct wsp_ggml_context * ctx,
1801
+ struct wsp_ggml_tensor * a,
1802
+ int p0,
1803
+ int p1);
1804
+
1805
+ // Move tensor elements by an offset given for each dimension. Elements that
1806
+ // are shifted beyond the last position are wrapped around to the beginning.
1807
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_roll(
1808
+ struct wsp_ggml_context * ctx,
1809
+ struct wsp_ggml_tensor * a,
1810
+ int shift0,
1811
+ int shift1,
1812
+ int shift2,
1813
+ int shift3);
1814
+
1815
+
1816
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1817
+ // timesteps: [N,]
1818
+ // return: [N, dim]
1819
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_timestep_embedding(
1820
+ struct wsp_ggml_context * ctx,
1821
+ struct wsp_ggml_tensor * timesteps,
1822
+ int dim,
1823
+ int max_period);
1824
+
1825
+ // sort rows
1826
+ enum wsp_ggml_sort_order {
1827
+ WSP_GGML_SORT_ORDER_ASC,
1828
+ WSP_GGML_SORT_ORDER_DESC,
1829
+ };
1830
+
1831
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_argsort(
1832
+ struct wsp_ggml_context * ctx,
1833
+ struct wsp_ggml_tensor * a,
1834
+ enum wsp_ggml_sort_order order);
1835
+
1836
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_arange(
1837
+ struct wsp_ggml_context * ctx,
1838
+ float start,
1839
+ float stop,
1840
+ float step);
1841
+
1842
+ // top k elements per row
1843
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_top_k(
1844
+ struct wsp_ggml_context * ctx,
1845
+ struct wsp_ggml_tensor * a,
1846
+ int k);
1847
+
1848
+ #define WSP_GGML_KQ_MASK_PAD 64
1849
+
1850
+ // q: [n_embd_k, n_batch, n_head, 1]
1851
+ // k: [n_embd_k, n_kv, n_head_kv, 1]
1852
+ // v: [n_embd_v, n_kv, n_head_kv, 1] !! not transposed !!
1853
+ // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = WSP_GGML_PAD(n_batch, WSP_GGML_KQ_MASK_PAD) !!
1854
+ // res: [n_embd_v, n_head, n_batch, 1] !! permuted !!
1855
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_flash_attn_ext(
1856
+ struct wsp_ggml_context * ctx,
1857
+ struct wsp_ggml_tensor * q,
1858
+ struct wsp_ggml_tensor * k,
1859
+ struct wsp_ggml_tensor * v,
1860
+ struct wsp_ggml_tensor * mask,
1861
+ float scale,
1862
+ float max_bias,
1863
+ float logit_softcap);
1864
+
1865
+ WSP_GGML_API void wsp_ggml_flash_attn_ext_set_prec(
1866
+ struct wsp_ggml_tensor * a,
1867
+ enum wsp_ggml_prec prec);
1868
+
1869
+ WSP_GGML_API enum wsp_ggml_prec wsp_ggml_flash_attn_ext_get_prec(
1870
+ const struct wsp_ggml_tensor * a);
1871
+
1872
+ // TODO: needs to be adapted to wsp_ggml_flash_attn_ext
1873
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_flash_attn_back(
1874
+ struct wsp_ggml_context * ctx,
1875
+ struct wsp_ggml_tensor * q,
1876
+ struct wsp_ggml_tensor * k,
1877
+ struct wsp_ggml_tensor * v,
1878
+ struct wsp_ggml_tensor * d,
1879
+ bool masked);
1880
+
1881
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_ssm_conv(
1882
+ struct wsp_ggml_context * ctx,
1883
+ struct wsp_ggml_tensor * sx,
1884
+ struct wsp_ggml_tensor * c);
1885
+
1886
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_ssm_scan(
1887
+ struct wsp_ggml_context * ctx,
1888
+ struct wsp_ggml_tensor * s,
1889
+ struct wsp_ggml_tensor * x,
1890
+ struct wsp_ggml_tensor * dt,
1891
+ struct wsp_ggml_tensor * A,
1892
+ struct wsp_ggml_tensor * B,
1893
+ struct wsp_ggml_tensor * C);
1894
+
1895
+ // partition into non-overlapping windows with padding if needed
1896
+ // example:
1897
+ // a: 768 64 64 1
1898
+ // w: 14
1899
+ // res: 768 14 14 25
1900
+ // used in sam
1901
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_win_part(
1902
+ struct wsp_ggml_context * ctx,
1903
+ struct wsp_ggml_tensor * a,
1904
+ int w);
1905
+
1906
+ // reverse of wsp_ggml_win_part
1907
+ // used in sam
1908
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_win_unpart(
1909
+ struct wsp_ggml_context * ctx,
1910
+ struct wsp_ggml_tensor * a,
1911
+ int w0,
1912
+ int h0,
1913
+ int w);
1914
+
1915
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_unary(
1916
+ struct wsp_ggml_context * ctx,
1917
+ struct wsp_ggml_tensor * a,
1918
+ enum wsp_ggml_unary_op op);
1919
+
1920
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_unary_inplace(
1921
+ struct wsp_ggml_context * ctx,
1922
+ struct wsp_ggml_tensor * a,
1923
+ enum wsp_ggml_unary_op op);
1924
+
1925
+ // used in sam
1926
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_get_rel_pos(
1927
+ struct wsp_ggml_context * ctx,
1928
+ struct wsp_ggml_tensor * a,
1929
+ int qh,
1930
+ int kh);
1931
+
1932
+ // used in sam
1933
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add_rel_pos(
1934
+ struct wsp_ggml_context * ctx,
1935
+ struct wsp_ggml_tensor * a,
1936
+ struct wsp_ggml_tensor * pw,
1937
+ struct wsp_ggml_tensor * ph);
1938
+
1939
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_add_rel_pos_inplace(
1940
+ struct wsp_ggml_context * ctx,
1941
+ struct wsp_ggml_tensor * a,
1942
+ struct wsp_ggml_tensor * pw,
1943
+ struct wsp_ggml_tensor * ph);
1944
+
1945
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rwkv_wkv6(
1946
+ struct wsp_ggml_context * ctx,
1947
+ struct wsp_ggml_tensor * k,
1948
+ struct wsp_ggml_tensor * v,
1949
+ struct wsp_ggml_tensor * r,
1950
+ struct wsp_ggml_tensor * tf,
1951
+ struct wsp_ggml_tensor * td,
1952
+ struct wsp_ggml_tensor * state);
1953
+
1954
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_gated_linear_attn(
1955
+ struct wsp_ggml_context * ctx,
1956
+ struct wsp_ggml_tensor * k,
1957
+ struct wsp_ggml_tensor * v,
1958
+ struct wsp_ggml_tensor * q,
1959
+ struct wsp_ggml_tensor * g,
1960
+ struct wsp_ggml_tensor * state,
1961
+ float scale);
1962
+
1963
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_rwkv_wkv7(
1964
+ struct wsp_ggml_context * ctx,
1965
+ struct wsp_ggml_tensor * r,
1966
+ struct wsp_ggml_tensor * w,
1967
+ struct wsp_ggml_tensor * k,
1968
+ struct wsp_ggml_tensor * v,
1969
+ struct wsp_ggml_tensor * a,
1970
+ struct wsp_ggml_tensor * b,
1971
+ struct wsp_ggml_tensor * state);
1972
+
1973
+ // custom operators
1974
+
1975
+ typedef void (*wsp_ggml_custom1_op_t)(struct wsp_ggml_tensor * dst , const struct wsp_ggml_tensor * a, int ith, int nth, void * userdata);
1976
+ typedef void (*wsp_ggml_custom2_op_t)(struct wsp_ggml_tensor * dst , const struct wsp_ggml_tensor * a, const struct wsp_ggml_tensor * b, int ith, int nth, void * userdata);
1977
+ typedef void (*wsp_ggml_custom3_op_t)(struct wsp_ggml_tensor * dst , const struct wsp_ggml_tensor * a, const struct wsp_ggml_tensor * b, const struct wsp_ggml_tensor * c, int ith, int nth, void * userdata);
1978
+
1979
+ #define WSP_GGML_N_TASKS_MAX (-1)
1980
+ // n_tasks == WSP_GGML_N_TASKS_MAX means to use max number of tasks
1981
+
1982
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom1(
1983
+ struct wsp_ggml_context * ctx,
1984
+ struct wsp_ggml_tensor * a,
1985
+ wsp_ggml_custom1_op_t fun,
1986
+ int n_tasks,
1987
+ void * userdata);
1988
+
1989
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom1_inplace(
1990
+ struct wsp_ggml_context * ctx,
1991
+ struct wsp_ggml_tensor * a,
1992
+ wsp_ggml_custom1_op_t fun,
1993
+ int n_tasks,
1994
+ void * userdata);
1995
+
1996
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom2(
1997
+ struct wsp_ggml_context * ctx,
1998
+ struct wsp_ggml_tensor * a,
1999
+ struct wsp_ggml_tensor * b,
2000
+ wsp_ggml_custom2_op_t fun,
2001
+ int n_tasks,
2002
+ void * userdata);
2003
+
2004
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom2_inplace(
2005
+ struct wsp_ggml_context * ctx,
2006
+ struct wsp_ggml_tensor * a,
2007
+ struct wsp_ggml_tensor * b,
2008
+ wsp_ggml_custom2_op_t fun,
2009
+ int n_tasks,
2010
+ void * userdata);
2011
+
2012
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom3(
2013
+ struct wsp_ggml_context * ctx,
2014
+ struct wsp_ggml_tensor * a,
2015
+ struct wsp_ggml_tensor * b,
2016
+ struct wsp_ggml_tensor * c,
2017
+ wsp_ggml_custom3_op_t fun,
2018
+ int n_tasks,
2019
+ void * userdata);
2020
+
2021
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_map_custom3_inplace(
2022
+ struct wsp_ggml_context * ctx,
2023
+ struct wsp_ggml_tensor * a,
2024
+ struct wsp_ggml_tensor * b,
2025
+ struct wsp_ggml_tensor * c,
2026
+ wsp_ggml_custom3_op_t fun,
2027
+ int n_tasks,
2028
+ void * userdata);
2029
+
2030
+ typedef void (*wsp_ggml_custom_op_t)(struct wsp_ggml_tensor * dst , int ith, int nth, void * userdata);
2031
+
2032
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_custom_4d(
2033
+ struct wsp_ggml_context * ctx,
2034
+ enum wsp_ggml_type type,
2035
+ int64_t ne0,
2036
+ int64_t ne1,
2037
+ int64_t ne2,
2038
+ int64_t ne3,
2039
+ struct wsp_ggml_tensor ** args,
2040
+ int n_args,
2041
+ wsp_ggml_custom_op_t fun,
2042
+ int n_tasks,
2043
+ void * userdata);
2044
+
2045
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_custom_inplace(
2046
+ struct wsp_ggml_context * ctx,
2047
+ struct wsp_ggml_tensor * a,
2048
+ struct wsp_ggml_tensor ** args,
2049
+ int n_args,
2050
+ wsp_ggml_custom_op_t fun,
2051
+ int n_tasks,
2052
+ void * userdata);
2053
+
2054
+ // loss function
2055
+
2056
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cross_entropy_loss(
2057
+ struct wsp_ggml_context * ctx,
2058
+ struct wsp_ggml_tensor * a, // logits
2059
+ struct wsp_ggml_tensor * b); // labels
2060
+
2061
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_cross_entropy_loss_back(
2062
+ struct wsp_ggml_context * ctx,
2063
+ struct wsp_ggml_tensor * a, // logits
2064
+ struct wsp_ggml_tensor * b, // labels
2065
+ struct wsp_ggml_tensor * c); // gradients of cross_entropy_loss result
2066
+
2067
+ // AdamW optimizer step
2068
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2069
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2070
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_opt_step_adamw(
2071
+ struct wsp_ggml_context * ctx,
2072
+ struct wsp_ggml_tensor * a,
2073
+ struct wsp_ggml_tensor * grad,
2074
+ struct wsp_ggml_tensor * m,
2075
+ struct wsp_ggml_tensor * v,
2076
+ struct wsp_ggml_tensor * adamw_params); // parameters such a the learning rate
2077
+
2078
+ //
2079
+ // automatic differentiation
2080
+ //
2081
+
2082
+ WSP_GGML_API void wsp_ggml_build_forward_expand(struct wsp_ggml_cgraph * cgraph, struct wsp_ggml_tensor * tensor);
2083
+ WSP_GGML_API void wsp_ggml_build_backward_expand(
2084
+ struct wsp_ggml_context * ctx, // context for gradient computation
2085
+ struct wsp_ggml_cgraph * cgraph,
2086
+ struct wsp_ggml_tensor ** grad_accs);
2087
+
2088
+ // graph allocation in a context
2089
+ WSP_GGML_API struct wsp_ggml_cgraph * wsp_ggml_new_graph (struct wsp_ggml_context * ctx); // size = WSP_GGML_DEFAULT_GRAPH_SIZE, grads = false
2090
+ WSP_GGML_API struct wsp_ggml_cgraph * wsp_ggml_new_graph_custom(struct wsp_ggml_context * ctx, size_t size, bool grads);
2091
+ WSP_GGML_API struct wsp_ggml_cgraph * wsp_ggml_graph_dup (struct wsp_ggml_context * ctx, struct wsp_ggml_cgraph * cgraph, bool force_grads);
2092
+ WSP_GGML_API void wsp_ggml_graph_cpy (struct wsp_ggml_cgraph * src, struct wsp_ggml_cgraph * dst);
2093
+ WSP_GGML_API void wsp_ggml_graph_reset (struct wsp_ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2094
+ WSP_GGML_API void wsp_ggml_graph_clear (struct wsp_ggml_cgraph * cgraph);
2095
+
2096
+ WSP_GGML_API int wsp_ggml_graph_size (struct wsp_ggml_cgraph * cgraph);
2097
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_graph_node (struct wsp_ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2098
+ WSP_GGML_API struct wsp_ggml_tensor ** wsp_ggml_graph_nodes (struct wsp_ggml_cgraph * cgraph);
2099
+ WSP_GGML_API int wsp_ggml_graph_n_nodes(struct wsp_ggml_cgraph * cgraph);
2100
+
2101
+ WSP_GGML_API void wsp_ggml_graph_add_node(struct wsp_ggml_cgraph * cgraph, struct wsp_ggml_tensor * tensor);
2102
+
2103
+ WSP_GGML_API size_t wsp_ggml_graph_overhead(void);
2104
+ WSP_GGML_API size_t wsp_ggml_graph_overhead_custom(size_t size, bool grads);
2105
+
2106
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_graph_get_tensor (const struct wsp_ggml_cgraph * cgraph, const char * name);
2107
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_graph_get_grad (const struct wsp_ggml_cgraph * cgraph, const struct wsp_ggml_tensor * node);
2108
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_graph_get_grad_acc(const struct wsp_ggml_cgraph * cgraph, const struct wsp_ggml_tensor * node);
2109
+
2110
+ // print info and performance information for the graph
2111
+ WSP_GGML_API void wsp_ggml_graph_print(const struct wsp_ggml_cgraph * cgraph);
2112
+
2113
+ // dump the graph into a file using the dot format
2114
+ WSP_GGML_API void wsp_ggml_graph_dump_dot(const struct wsp_ggml_cgraph * gb, const struct wsp_ggml_cgraph * gf, const char * filename);
2115
+
2116
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2117
+ typedef void (*wsp_ggml_log_callback)(enum wsp_ggml_log_level level, const char * text, void * user_data);
2118
+
2119
+ // Set callback for all future logging events.
2120
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2121
+ WSP_GGML_API void wsp_ggml_log_set(wsp_ggml_log_callback log_callback, void * user_data);
2122
+
2123
+ WSP_GGML_API struct wsp_ggml_tensor * wsp_ggml_set_zero(struct wsp_ggml_tensor * tensor);
2124
+
2125
+ //
2126
+ // quantization
2127
+ //
2128
+
2129
+ // - wsp_ggml_wsp_quantize_init can be called multiple times with the same type
2130
+ // it will only initialize the quantization tables for the first call or after wsp_ggml_wsp_quantize_free
2131
+ // automatically called by wsp_ggml_wsp_quantize_chunk for convenience
2132
+ //
2133
+ // - wsp_ggml_wsp_quantize_free will free any memory allocated by wsp_ggml_wsp_quantize_init
2134
+ // call this at the end of the program to avoid memory leaks
2135
+ //
2136
+ // note: these are thread-safe
2137
+ //
2138
+ WSP_GGML_API void wsp_ggml_wsp_quantize_init(enum wsp_ggml_type type);
2139
+ WSP_GGML_API void wsp_ggml_wsp_quantize_free(void);
2140
+
2141
+ // some quantization type cannot be used without an importance matrix
2142
+ WSP_GGML_API bool wsp_ggml_wsp_quantize_requires_imatrix(enum wsp_ggml_type type);
2143
+
2144
+ // calls wsp_ggml_wsp_quantize_init internally (i.e. can allocate memory)
2145
+ WSP_GGML_API size_t wsp_ggml_wsp_quantize_chunk(
2146
+ enum wsp_ggml_type type,
2147
+ const float * src,
2148
+ void * dst,
2149
+ int64_t start,
2150
+ int64_t nrows,
2151
+ int64_t n_per_row,
2152
+ const float * imatrix);
2153
+
2154
+ #ifdef __cplusplus
2155
+ // restrict not standard in C++
2156
+ # if defined(__GNUC__)
2157
+ # define WSP_GGML_RESTRICT __restrict__
2158
+ # elif defined(__clang__)
2159
+ # define WSP_GGML_RESTRICT __restrict
2160
+ # elif defined(_MSC_VER)
2161
+ # define WSP_GGML_RESTRICT __restrict
2162
+ # else
2163
+ # define WSP_GGML_RESTRICT
2164
+ # endif
2165
+ #else
2166
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2167
+ # define WSP_GGML_RESTRICT __restrict
2168
+ # else
2169
+ # define WSP_GGML_RESTRICT restrict
2170
+ # endif
2171
+ #endif
2172
+ typedef void (*wsp_ggml_to_float_t) (const void * WSP_GGML_RESTRICT x, float * WSP_GGML_RESTRICT y, int64_t k);
2173
+ typedef void (*wsp_ggml_from_float_t)(const float * WSP_GGML_RESTRICT x, void * WSP_GGML_RESTRICT y, int64_t k);
2174
+
2175
+ struct wsp_ggml_type_traits {
2176
+ const char * type_name;
2177
+ int64_t blck_size;
2178
+ int64_t blck_size_interleave; // interleave elements in blocks
2179
+ size_t type_size;
2180
+ bool is_quantized;
2181
+ wsp_ggml_to_float_t to_float;
2182
+ wsp_ggml_from_float_t from_float_ref;
2183
+ };
2184
+
2185
+ WSP_GGML_API const struct wsp_ggml_type_traits * wsp_ggml_get_type_traits(enum wsp_ggml_type type);
2186
+
2187
+ // ggml threadpool
2188
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2189
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2190
+
2191
+ // scheduling priorities
2192
+ enum wsp_ggml_sched_priority {
2193
+ WSP_GGML_SCHED_PRIO_LOW = -1,
2194
+ WSP_GGML_SCHED_PRIO_NORMAL,
2195
+ WSP_GGML_SCHED_PRIO_MEDIUM,
2196
+ WSP_GGML_SCHED_PRIO_HIGH,
2197
+ WSP_GGML_SCHED_PRIO_REALTIME
2198
+ };
2199
+
2200
+ // threadpool params
2201
+ // Use wsp_ggml_threadpool_params_default() or wsp_ggml_threadpool_params_init() to populate the defaults
2202
+ struct wsp_ggml_threadpool_params {
2203
+ bool cpumask[WSP_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2204
+ int n_threads; // number of threads
2205
+ enum wsp_ggml_sched_priority prio; // thread priority
2206
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2207
+ bool strict_cpu; // strict cpu placement
2208
+ bool paused; // start in paused state
2209
+ };
2210
+
2211
+ struct wsp_ggml_threadpool; // forward declaration, see ggml.c
2212
+
2213
+ typedef struct wsp_ggml_threadpool * wsp_ggml_threadpool_t;
2214
+
2215
+ WSP_GGML_API struct wsp_ggml_threadpool_params wsp_ggml_threadpool_params_default(int n_threads);
2216
+ WSP_GGML_API void wsp_ggml_threadpool_params_init (struct wsp_ggml_threadpool_params * p, int n_threads);
2217
+ WSP_GGML_API bool wsp_ggml_threadpool_params_match (const struct wsp_ggml_threadpool_params * p0, const struct wsp_ggml_threadpool_params * p1);
2218
+
2219
+ #ifdef __cplusplus
2220
+ }
2221
+ #endif