whisper.rn 0.4.0-rc.8 → 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 (201) hide show
  1. package/README.md +5 -1
  2. package/android/build.gradle +12 -3
  3. package/android/src/main/CMakeLists.txt +44 -13
  4. package/android/src/main/java/com/rnwhisper/AudioUtils.java +27 -12
  5. package/android/src/main/java/com/rnwhisper/RNWhisper.java +75 -34
  6. package/android/src/main/java/com/rnwhisper/WhisperContext.java +53 -38
  7. package/android/src/main/jni.cpp +38 -1
  8. package/android/src/main/jniLibs/arm64-v8a/librnwhisper.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnwhisper_v8fp16_va_2.so +0 -0
  10. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper.so +0 -0
  11. package/android/src/main/jniLibs/armeabi-v7a/librnwhisper_vfpv4.so +0 -0
  12. package/android/src/main/jniLibs/x86_64/librnwhisper.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnwhisper_x86_64.so +0 -0
  14. package/android/src/newarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
  15. package/android/src/oldarch/java/com/rnwhisper/RNWhisperModule.java +10 -0
  16. package/cpp/coreml/whisper-compat.h +10 -0
  17. package/cpp/coreml/whisper-compat.m +35 -0
  18. package/cpp/coreml/whisper-decoder-impl.h +27 -15
  19. package/cpp/coreml/whisper-decoder-impl.m +36 -10
  20. package/cpp/coreml/whisper-encoder-impl.h +21 -9
  21. package/cpp/coreml/whisper-encoder-impl.m +29 -3
  22. package/cpp/ggml-alloc.c +727 -517
  23. package/cpp/ggml-alloc.h +47 -65
  24. package/cpp/ggml-backend-impl.h +196 -57
  25. package/cpp/ggml-backend-reg.cpp +591 -0
  26. package/cpp/ggml-backend.cpp +2016 -0
  27. package/cpp/ggml-backend.h +234 -89
  28. package/cpp/ggml-common.h +1861 -0
  29. package/cpp/ggml-cpp.h +39 -0
  30. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  31. package/cpp/ggml-cpu/amx/amx.h +8 -0
  32. package/cpp/ggml-cpu/amx/common.h +91 -0
  33. package/cpp/ggml-cpu/amx/mmq.cpp +2511 -0
  34. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  35. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  36. package/cpp/ggml-cpu/arch/arm/quants.c +4113 -0
  37. package/cpp/ggml-cpu/arch/arm/repack.cpp +2162 -0
  38. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  39. package/cpp/ggml-cpu/arch/x86/quants.c +4310 -0
  40. package/cpp/ggml-cpu/arch/x86/repack.cpp +3284 -0
  41. package/cpp/ggml-cpu/arch-fallback.h +184 -0
  42. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  43. package/cpp/ggml-cpu/binary-ops.h +16 -0
  44. package/cpp/ggml-cpu/common.h +72 -0
  45. package/cpp/ggml-cpu/ggml-cpu-impl.h +511 -0
  46. package/cpp/ggml-cpu/ggml-cpu.c +3473 -0
  47. package/cpp/ggml-cpu/ggml-cpu.cpp +671 -0
  48. package/cpp/ggml-cpu/ops.cpp +9085 -0
  49. package/cpp/ggml-cpu/ops.h +111 -0
  50. package/cpp/ggml-cpu/quants.c +1157 -0
  51. package/cpp/ggml-cpu/quants.h +89 -0
  52. package/cpp/ggml-cpu/repack.cpp +1570 -0
  53. package/cpp/ggml-cpu/repack.h +98 -0
  54. package/cpp/ggml-cpu/simd-mappings.h +1006 -0
  55. package/cpp/ggml-cpu/traits.cpp +36 -0
  56. package/cpp/ggml-cpu/traits.h +38 -0
  57. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  58. package/cpp/ggml-cpu/unary-ops.h +28 -0
  59. package/cpp/ggml-cpu/vec.cpp +321 -0
  60. package/cpp/ggml-cpu/vec.h +973 -0
  61. package/cpp/ggml-cpu.h +143 -0
  62. package/cpp/ggml-impl.h +525 -168
  63. package/cpp/ggml-metal-impl.h +622 -0
  64. package/cpp/ggml-metal.h +16 -14
  65. package/cpp/ggml-metal.m +5289 -1859
  66. package/cpp/ggml-opt.cpp +1037 -0
  67. package/cpp/ggml-opt.h +237 -0
  68. package/cpp/ggml-quants.c +2916 -6877
  69. package/cpp/ggml-quants.h +87 -249
  70. package/cpp/ggml-threading.cpp +12 -0
  71. package/cpp/ggml-threading.h +14 -0
  72. package/cpp/ggml-whisper-sim.metallib +0 -0
  73. package/cpp/ggml-whisper.metallib +0 -0
  74. package/cpp/ggml.c +3293 -16770
  75. package/cpp/ggml.h +778 -835
  76. package/cpp/gguf.cpp +1347 -0
  77. package/cpp/gguf.h +202 -0
  78. package/cpp/rn-whisper.cpp +84 -0
  79. package/cpp/rn-whisper.h +2 -0
  80. package/cpp/whisper-arch.h +197 -0
  81. package/cpp/whisper.cpp +3240 -944
  82. package/cpp/whisper.h +144 -31
  83. package/ios/CMakeLists.txt +95 -0
  84. package/ios/RNWhisper.h +5 -0
  85. package/ios/RNWhisper.mm +124 -37
  86. package/ios/RNWhisperAudioUtils.h +1 -0
  87. package/ios/RNWhisperAudioUtils.m +24 -13
  88. package/ios/RNWhisperContext.h +8 -2
  89. package/ios/RNWhisperContext.mm +42 -8
  90. package/ios/rnwhisper.xcframework/Info.plist +74 -0
  91. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  92. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  93. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  94. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  95. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  96. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  97. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  98. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  99. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  100. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  101. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  102. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  103. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  104. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  105. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  106. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  107. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  108. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  109. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  110. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/Info.plist +0 -0
  111. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  112. package/ios/rnwhisper.xcframework/ios-arm64/rnwhisper.framework/rnwhisper +0 -0
  113. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  114. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  115. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  116. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  117. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  118. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  119. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  120. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  121. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  122. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  123. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  124. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  125. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  126. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  127. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  128. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  129. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  130. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  131. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  132. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  133. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  134. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  135. package/ios/rnwhisper.xcframework/ios-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  136. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  137. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  138. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  139. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  140. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  141. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  142. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  143. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  144. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  145. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  146. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  147. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  148. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/ggml.h +2221 -0
  149. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/gguf.h +202 -0
  150. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  151. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  152. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  153. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  154. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Headers/whisper.h +739 -0
  155. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/Info.plist +0 -0
  156. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/ggml-whisper.metallib +0 -0
  157. package/ios/rnwhisper.xcframework/tvos-arm64/rnwhisper.framework/rnwhisper +0 -0
  158. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-alloc.h +76 -0
  159. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend-impl.h +255 -0
  160. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-backend.h +354 -0
  161. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-common.h +1861 -0
  162. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpp.h +39 -0
  163. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-cpu.h +143 -0
  164. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-impl.h +603 -0
  165. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal-impl.h +622 -0
  166. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-metal.h +66 -0
  167. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-opt.h +237 -0
  168. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-quants.h +100 -0
  169. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml-threading.h +14 -0
  170. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/ggml.h +2221 -0
  171. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/gguf.h +202 -0
  172. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-audioutils.h +14 -0
  173. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper-log.h +11 -0
  174. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/rn-whisper.h +52 -0
  175. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper-arch.h +197 -0
  176. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Headers/whisper.h +739 -0
  177. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/Info.plist +0 -0
  178. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/_CodeSignature/CodeResources +101 -0
  179. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/ggml-whisper-sim.metallib +0 -0
  180. package/ios/rnwhisper.xcframework/tvos-arm64_x86_64-simulator/rnwhisper.framework/rnwhisper +0 -0
  181. package/jest/mock.js +14 -1
  182. package/lib/commonjs/NativeRNWhisper.js.map +1 -1
  183. package/lib/commonjs/index.js +48 -19
  184. package/lib/commonjs/index.js.map +1 -1
  185. package/lib/commonjs/version.json +1 -1
  186. package/lib/module/NativeRNWhisper.js.map +1 -1
  187. package/lib/module/index.js +48 -19
  188. package/lib/module/index.js.map +1 -1
  189. package/lib/module/version.json +1 -1
  190. package/lib/typescript/NativeRNWhisper.d.ts +6 -3
  191. package/lib/typescript/NativeRNWhisper.d.ts.map +1 -1
  192. package/lib/typescript/index.d.ts +25 -3
  193. package/lib/typescript/index.d.ts.map +1 -1
  194. package/package.json +15 -10
  195. package/src/NativeRNWhisper.ts +12 -3
  196. package/src/index.ts +63 -24
  197. package/src/version.json +1 -1
  198. package/whisper-rn.podspec +18 -18
  199. package/cpp/README.md +0 -4
  200. package/cpp/ggml-backend.c +0 -1718
  201. package/cpp/ggml-metal-whisper.metal +0 -5820
@@ -0,0 +1,2016 @@
1
+ // Note: porting this file to C++ is a work in progress
2
+
3
+ #ifdef _WIN32
4
+ #define WIN32_LEAN_AND_MEAN
5
+ #ifndef NOMINMAX
6
+ # define NOMINMAX
7
+ #endif
8
+ #include <windows.h>
9
+ #endif
10
+
11
+ #include "ggml-backend.h"
12
+ #include "ggml-backend-impl.h"
13
+ #include "ggml-alloc.h"
14
+ #include "ggml-impl.h"
15
+
16
+ #include <assert.h>
17
+ #include <limits.h>
18
+ #include <stdarg.h>
19
+ #include <stdio.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <string>
23
+ #include <vector>
24
+ #include <algorithm>
25
+
26
+ #ifdef __APPLE__
27
+ #include <sys/types.h>
28
+ #include <sys/sysctl.h>
29
+ #endif
30
+
31
+
32
+ // backend buffer type
33
+
34
+ const char * wsp_ggml_backend_buft_name(wsp_ggml_backend_buffer_type_t buft) {
35
+ return buft->iface.get_name(buft);
36
+ }
37
+
38
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_buft_alloc_buffer(wsp_ggml_backend_buffer_type_t buft, size_t size) {
39
+ if (size == 0) {
40
+ // return a dummy buffer for zero-sized allocations
41
+ return wsp_ggml_backend_buffer_init(buft, {}, NULL, 0);
42
+ }
43
+
44
+ return buft->iface.alloc_buffer(buft, size);
45
+ }
46
+
47
+ size_t wsp_ggml_backend_buft_get_alignment(wsp_ggml_backend_buffer_type_t buft) {
48
+ return buft->iface.get_alignment(buft);
49
+ }
50
+
51
+ size_t wsp_ggml_backend_buft_get_max_size(wsp_ggml_backend_buffer_type_t buft) {
52
+ // get_max_size is optional, defaults to SIZE_MAX
53
+ if (buft->iface.get_max_size) {
54
+ return buft->iface.get_max_size(buft);
55
+ }
56
+ return SIZE_MAX;
57
+ }
58
+
59
+ size_t wsp_ggml_backend_buft_get_alloc_size(wsp_ggml_backend_buffer_type_t buft, const struct wsp_ggml_tensor * tensor) {
60
+ // get_alloc_size is optional, defaults to wsp_ggml_nbytes
61
+ if (buft->iface.get_alloc_size) {
62
+ size_t size = buft->iface.get_alloc_size(buft, tensor);
63
+ assert(size >= wsp_ggml_nbytes(tensor));
64
+ return size;
65
+ }
66
+ return wsp_ggml_nbytes(tensor);
67
+ }
68
+
69
+ bool wsp_ggml_backend_buft_is_host(wsp_ggml_backend_buffer_type_t buft) {
70
+ if (buft->iface.is_host) {
71
+ return buft->iface.is_host(buft);
72
+ }
73
+ return false;
74
+ }
75
+
76
+ wsp_ggml_backend_dev_t wsp_ggml_backend_buft_get_device(wsp_ggml_backend_buffer_type_t buft) {
77
+ return buft->device;
78
+ }
79
+
80
+ // backend buffer
81
+
82
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_buffer_init(
83
+ wsp_ggml_backend_buffer_type_t buft,
84
+ struct wsp_ggml_backend_buffer_i iface,
85
+ void * context,
86
+ size_t size) {
87
+ wsp_ggml_backend_buffer_t buffer = new wsp_ggml_backend_buffer {
88
+ /* .interface = */ iface,
89
+ /* .buft = */ buft,
90
+ /* .context = */ context,
91
+ /* .size = */ size,
92
+ /* .usage = */ WSP_GGML_BACKEND_BUFFER_USAGE_ANY
93
+ };
94
+
95
+ return buffer;
96
+ }
97
+
98
+ const char * wsp_ggml_backend_buffer_name(wsp_ggml_backend_buffer_t buffer) {
99
+ return wsp_ggml_backend_buft_name(wsp_ggml_backend_buffer_get_type(buffer));
100
+ }
101
+
102
+ void wsp_ggml_backend_buffer_free(wsp_ggml_backend_buffer_t buffer) {
103
+ if (buffer == NULL) {
104
+ return;
105
+ }
106
+
107
+ if (buffer->iface.free_buffer != NULL) {
108
+ buffer->iface.free_buffer(buffer);
109
+ }
110
+ delete buffer;
111
+ }
112
+
113
+ size_t wsp_ggml_backend_buffer_get_size(wsp_ggml_backend_buffer_t buffer) {
114
+ return buffer->size;
115
+ }
116
+
117
+ void * wsp_ggml_backend_buffer_get_base(wsp_ggml_backend_buffer_t buffer) {
118
+ // get_base is optional if the buffer is zero-sized
119
+ if (buffer->size == 0) {
120
+ return NULL;
121
+ }
122
+
123
+ void * base = buffer->iface.get_base(buffer);
124
+
125
+ WSP_GGML_ASSERT(base != NULL && "backend buffer base cannot be NULL");
126
+
127
+ return base;
128
+ }
129
+
130
+ enum wsp_ggml_status wsp_ggml_backend_buffer_init_tensor(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor) {
131
+ // init_tensor is optional
132
+ if (buffer->iface.init_tensor) {
133
+ return buffer->iface.init_tensor(buffer, tensor);
134
+ }
135
+ return WSP_GGML_STATUS_SUCCESS;
136
+ }
137
+
138
+ void wsp_ggml_backend_buffer_clear(wsp_ggml_backend_buffer_t buffer, uint8_t value) {
139
+ // clear is optional if the buffer is zero-sized
140
+ if (buffer->size == 0) {
141
+ return;
142
+ }
143
+
144
+ buffer->iface.clear(buffer, value);
145
+ }
146
+
147
+ size_t wsp_ggml_backend_buffer_get_alignment(wsp_ggml_backend_buffer_t buffer) {
148
+ return wsp_ggml_backend_buft_get_alignment(wsp_ggml_backend_buffer_get_type(buffer));
149
+ }
150
+
151
+ size_t wsp_ggml_backend_buffer_get_max_size(wsp_ggml_backend_buffer_t buffer) {
152
+ return wsp_ggml_backend_buft_get_max_size(wsp_ggml_backend_buffer_get_type(buffer));
153
+ }
154
+
155
+ size_t wsp_ggml_backend_buffer_get_alloc_size(wsp_ggml_backend_buffer_t buffer, const struct wsp_ggml_tensor * tensor) {
156
+ return wsp_ggml_backend_buft_get_alloc_size(wsp_ggml_backend_buffer_get_type(buffer), tensor);
157
+ }
158
+
159
+ bool wsp_ggml_backend_buffer_is_host(wsp_ggml_backend_buffer_t buffer) {
160
+ return wsp_ggml_backend_buft_is_host(wsp_ggml_backend_buffer_get_type(buffer));
161
+ }
162
+
163
+ void wsp_ggml_backend_buffer_set_usage(wsp_ggml_backend_buffer_t buffer, enum wsp_ggml_backend_buffer_usage usage) {
164
+ buffer->usage = usage;
165
+
166
+ // FIXME: add a generic callback to the buffer interface
167
+ if (wsp_ggml_backend_buffer_is_multi_buffer(buffer)) {
168
+ wsp_ggml_backend_multi_buffer_set_usage(buffer, usage);
169
+ }
170
+ }
171
+
172
+ enum wsp_ggml_backend_buffer_usage wsp_ggml_backend_buffer_get_usage(wsp_ggml_backend_buffer_t buffer) {
173
+ return buffer->usage;
174
+ }
175
+
176
+ wsp_ggml_backend_buffer_type_t wsp_ggml_backend_buffer_get_type(wsp_ggml_backend_buffer_t buffer) {
177
+ return buffer->buft;
178
+ }
179
+
180
+ void wsp_ggml_backend_buffer_reset(wsp_ggml_backend_buffer_t buffer) {
181
+ if (buffer->iface.reset) {
182
+ buffer->iface.reset(buffer);
183
+ }
184
+ }
185
+
186
+ bool wsp_ggml_backend_buffer_copy_tensor(const struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst) {
187
+ wsp_ggml_backend_buffer_t dst_buf = dst->view_src ? dst->view_src->buffer : dst->buffer;
188
+ if (dst_buf->iface.cpy_tensor) {
189
+ return dst_buf->iface.cpy_tensor(dst_buf, src, dst);
190
+ }
191
+ return false;
192
+ }
193
+
194
+ // backend
195
+
196
+ wsp_ggml_guid_t wsp_ggml_backend_guid(wsp_ggml_backend_t backend) {
197
+ if (backend == NULL) {
198
+ return NULL;
199
+ }
200
+ return backend->guid;
201
+ }
202
+
203
+ const char * wsp_ggml_backend_name(wsp_ggml_backend_t backend) {
204
+ if (backend == NULL) {
205
+ return "NULL";
206
+ }
207
+ return backend->iface.get_name(backend);
208
+ }
209
+
210
+ void wsp_ggml_backend_free(wsp_ggml_backend_t backend) {
211
+ if (backend == NULL) {
212
+ return;
213
+ }
214
+
215
+ backend->iface.free(backend);
216
+ }
217
+
218
+ wsp_ggml_backend_buffer_type_t wsp_ggml_backend_get_default_buffer_type(wsp_ggml_backend_t backend) {
219
+ return wsp_ggml_backend_dev_buffer_type(backend->device);
220
+ }
221
+
222
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_alloc_buffer(wsp_ggml_backend_t backend, size_t size) {
223
+ return wsp_ggml_backend_buft_alloc_buffer(wsp_ggml_backend_get_default_buffer_type(backend), size);
224
+ }
225
+
226
+ size_t wsp_ggml_backend_get_alignment(wsp_ggml_backend_t backend) {
227
+ return wsp_ggml_backend_buft_get_alignment(wsp_ggml_backend_get_default_buffer_type(backend));
228
+ }
229
+
230
+ size_t wsp_ggml_backend_get_max_size(wsp_ggml_backend_t backend) {
231
+ return wsp_ggml_backend_buft_get_max_size(wsp_ggml_backend_get_default_buffer_type(backend));
232
+ }
233
+
234
+ void wsp_ggml_backend_tensor_set_async(wsp_ggml_backend_t backend, struct wsp_ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
235
+ WSP_GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
236
+ WSP_GGML_ASSERT(offset + size <= wsp_ggml_nbytes(tensor) && "tensor write out of bounds");
237
+
238
+ if (backend->iface.set_tensor_async == NULL) {
239
+ wsp_ggml_backend_tensor_set(tensor, data, offset, size);
240
+ } else {
241
+ backend->iface.set_tensor_async(backend, tensor, data, offset, size);
242
+ }
243
+ }
244
+
245
+ void wsp_ggml_backend_tensor_get_async(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * tensor, void * data, size_t offset, size_t size) {
246
+ WSP_GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
247
+ WSP_GGML_ASSERT(offset + size <= wsp_ggml_nbytes(tensor) && "tensor read out of bounds");
248
+
249
+ if (backend->iface.get_tensor_async == NULL) {
250
+ wsp_ggml_backend_tensor_get(tensor, data, offset, size);
251
+ } else {
252
+ backend->iface.get_tensor_async(backend, tensor, data, offset, size);
253
+ }
254
+ }
255
+
256
+ void wsp_ggml_backend_tensor_set(struct wsp_ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
257
+ WSP_GGML_ASSERT(tensor);
258
+ wsp_ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
259
+
260
+ if (size == 0) {
261
+ return;
262
+ }
263
+
264
+ WSP_GGML_ASSERT(buf != NULL && "tensor buffer not set");
265
+ WSP_GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
266
+ WSP_GGML_ASSERT(offset + size <= wsp_ggml_nbytes(tensor) && "tensor write out of bounds");
267
+
268
+ buf->iface.set_tensor(buf, tensor, data, offset, size);
269
+ }
270
+
271
+ void wsp_ggml_backend_tensor_get(const struct wsp_ggml_tensor * tensor, void * data, size_t offset, size_t size) {
272
+ WSP_GGML_ASSERT(tensor);
273
+ wsp_ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
274
+
275
+ if (size == 0) {
276
+ return;
277
+ }
278
+
279
+ WSP_GGML_ASSERT(buf != NULL && "tensor buffer not set");
280
+ WSP_GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
281
+ WSP_GGML_ASSERT(offset + size <= wsp_ggml_nbytes(tensor) && "tensor read out of bounds");
282
+
283
+ buf->iface.get_tensor(buf, tensor, data, offset, size);
284
+ }
285
+
286
+ void wsp_ggml_backend_tensor_memset(struct wsp_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
287
+ wsp_ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
288
+
289
+ if (size == 0) {
290
+ return;
291
+ }
292
+
293
+ WSP_GGML_ASSERT(buf != NULL && "tensor buffer not set");
294
+ WSP_GGML_ASSERT(tensor->data != NULL && "tensor not allocated");
295
+ WSP_GGML_ASSERT(offset + size <= wsp_ggml_nbytes(tensor) && "tensor write out of bounds");
296
+ WSP_GGML_ASSERT(buf->iface.memset_tensor != NULL && "memset not implemented by backend buffer");
297
+
298
+ buf->iface.memset_tensor(buf, tensor, value, offset, size);
299
+ }
300
+
301
+ void wsp_ggml_backend_synchronize(wsp_ggml_backend_t backend) {
302
+ if (backend->iface.synchronize == NULL) {
303
+ return;
304
+ }
305
+
306
+ backend->iface.synchronize(backend);
307
+ }
308
+
309
+ wsp_ggml_backend_graph_plan_t wsp_ggml_backend_graph_plan_create(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph) {
310
+ WSP_GGML_ASSERT(backend->iface.graph_plan_create != NULL);
311
+
312
+ return backend->iface.graph_plan_create(backend, cgraph);
313
+ }
314
+
315
+ void wsp_ggml_backend_graph_plan_free(wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan) {
316
+ WSP_GGML_ASSERT(backend->iface.graph_plan_free != NULL);
317
+
318
+ backend->iface.graph_plan_free(backend, plan);
319
+ }
320
+
321
+ enum wsp_ggml_status wsp_ggml_backend_graph_plan_compute(wsp_ggml_backend_t backend, wsp_ggml_backend_graph_plan_t plan) {
322
+ WSP_GGML_ASSERT(backend->iface.graph_plan_compute != NULL);
323
+
324
+ return backend->iface.graph_plan_compute(backend, plan);
325
+ }
326
+
327
+ enum wsp_ggml_status wsp_ggml_backend_graph_compute(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph) {
328
+ enum wsp_ggml_status err = wsp_ggml_backend_graph_compute_async(backend, cgraph);
329
+ wsp_ggml_backend_synchronize(backend);
330
+ return err;
331
+ }
332
+
333
+ enum wsp_ggml_status wsp_ggml_backend_graph_compute_async(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * cgraph) {
334
+ return backend->iface.graph_compute(backend, cgraph);
335
+ }
336
+
337
+ bool wsp_ggml_backend_supports_op(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * op) {
338
+ return wsp_ggml_backend_dev_supports_op(backend->device, op);
339
+ }
340
+
341
+ bool wsp_ggml_backend_supports_buft(wsp_ggml_backend_t backend, wsp_ggml_backend_buffer_type_t buft) {
342
+ return wsp_ggml_backend_dev_supports_buft(backend->device, buft);
343
+ }
344
+
345
+ bool wsp_ggml_backend_offload_op(wsp_ggml_backend_t backend, const struct wsp_ggml_tensor * op) {
346
+ return wsp_ggml_backend_dev_offload_op(backend->device, op);
347
+ }
348
+
349
+ wsp_ggml_backend_dev_t wsp_ggml_backend_get_device(wsp_ggml_backend_t backend) {
350
+ return backend->device;
351
+ }
352
+
353
+ // backend copy
354
+
355
+ static bool wsp_ggml_are_same_layout(const struct wsp_ggml_tensor * a, const struct wsp_ggml_tensor * b) {
356
+ if (a->type != b->type) {
357
+ return false;
358
+ }
359
+ for (int i = 0; i < WSP_GGML_MAX_DIMS; i++) {
360
+ if (a->ne[i] != b->ne[i]) {
361
+ return false;
362
+ }
363
+ if (a->nb[i] != b->nb[i]) {
364
+ return false;
365
+ }
366
+ }
367
+ return true;
368
+ }
369
+
370
+ void wsp_ggml_backend_tensor_copy(struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst) {
371
+ WSP_GGML_ASSERT(wsp_ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
372
+
373
+ if (src == dst) {
374
+ return;
375
+ }
376
+
377
+ if (wsp_ggml_backend_buffer_is_host(src->buffer)) {
378
+ wsp_ggml_backend_tensor_set(dst, src->data, 0, wsp_ggml_nbytes(src));
379
+ } else if (wsp_ggml_backend_buffer_is_host(dst->buffer)) {
380
+ wsp_ggml_backend_tensor_get(src, dst->data, 0, wsp_ggml_nbytes(src));
381
+ } else if (!wsp_ggml_backend_buffer_copy_tensor(src, dst)) {
382
+ #ifndef NDEBUG
383
+ WSP_GGML_LOG_DEBUG("%s: warning: slow copy from %s to %s\n", __func__, wsp_ggml_backend_buffer_name(src->buffer), wsp_ggml_backend_buffer_name(dst->buffer));
384
+ #endif
385
+ size_t nbytes = wsp_ggml_nbytes(src);
386
+ void * data = malloc(nbytes);
387
+ wsp_ggml_backend_tensor_get(src, data, 0, nbytes);
388
+ wsp_ggml_backend_tensor_set(dst, data, 0, nbytes);
389
+ free(data);
390
+ }
391
+ }
392
+
393
+ void wsp_ggml_backend_tensor_copy_async(wsp_ggml_backend_t backend_src, wsp_ggml_backend_t backend_dst, struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst) {
394
+ WSP_GGML_ASSERT(wsp_ggml_are_same_layout(src, dst) && "cannot copy tensors with different layouts");
395
+
396
+ if (src == dst) {
397
+ return;
398
+ }
399
+
400
+ if (backend_dst->iface.cpy_tensor_async != NULL) {
401
+ if (backend_dst->iface.cpy_tensor_async(backend_src, backend_dst, src, dst)) {
402
+ return;
403
+ }
404
+ }
405
+
406
+ // an async copy would normally happen after all the queued operations on both backends are completed
407
+ // to simulate the same behavior, we need to synchronize both backends first, and do a blocking copy
408
+ wsp_ggml_backend_synchronize(backend_src);
409
+ wsp_ggml_backend_synchronize(backend_dst);
410
+ wsp_ggml_backend_tensor_copy(src, dst);
411
+ }
412
+
413
+ // events
414
+
415
+ wsp_ggml_backend_event_t wsp_ggml_backend_event_new(wsp_ggml_backend_dev_t device) {
416
+ // null device is allowed for the transition period to the device interface
417
+ if (device == NULL || device->iface.event_new == NULL) {
418
+ return NULL;
419
+ }
420
+ return device->iface.event_new(device);
421
+ }
422
+
423
+ void wsp_ggml_backend_event_free(wsp_ggml_backend_event_t event) {
424
+ if (event == NULL) {
425
+ return;
426
+ }
427
+ event->device->iface.event_free(event->device, event);
428
+ }
429
+
430
+ void wsp_ggml_backend_event_record(wsp_ggml_backend_event_t event, wsp_ggml_backend_t backend) {
431
+ WSP_GGML_ASSERT(backend->iface.event_record != NULL);
432
+
433
+ backend->iface.event_record(backend, event);
434
+ }
435
+
436
+ void wsp_ggml_backend_event_synchronize(wsp_ggml_backend_event_t event) {
437
+ WSP_GGML_ASSERT(event->device->iface.event_synchronize);
438
+
439
+ event->device->iface.event_synchronize(event->device, event);
440
+ }
441
+
442
+ void wsp_ggml_backend_event_wait(wsp_ggml_backend_t backend, wsp_ggml_backend_event_t event) {
443
+ WSP_GGML_ASSERT(backend->iface.event_wait != NULL);
444
+
445
+ backend->iface.event_wait(backend, event);
446
+ }
447
+
448
+ // Backend device
449
+
450
+ const char * wsp_ggml_backend_dev_name(wsp_ggml_backend_dev_t device) {
451
+ return device->iface.get_name(device);
452
+ }
453
+
454
+ const char * wsp_ggml_backend_dev_description(wsp_ggml_backend_dev_t device) {
455
+ return device->iface.get_description(device);
456
+ }
457
+
458
+ void wsp_ggml_backend_dev_memory(wsp_ggml_backend_dev_t device, size_t * free, size_t * total) {
459
+ device->iface.get_memory(device, free, total);
460
+ }
461
+
462
+ enum wsp_ggml_backend_dev_type wsp_ggml_backend_dev_type(wsp_ggml_backend_dev_t device) {
463
+ return device->iface.get_type(device);
464
+ }
465
+
466
+ void wsp_ggml_backend_dev_get_props(wsp_ggml_backend_dev_t device, struct wsp_ggml_backend_dev_props * props) {
467
+ memset(props, 0, sizeof(*props));
468
+ device->iface.get_props(device, props);
469
+ }
470
+
471
+ wsp_ggml_backend_reg_t wsp_ggml_backend_dev_backend_reg(wsp_ggml_backend_dev_t device) {
472
+ return device->reg;
473
+ }
474
+
475
+ wsp_ggml_backend_t wsp_ggml_backend_dev_init(wsp_ggml_backend_dev_t device, const char * params) {
476
+ return device->iface.init_backend(device, params);
477
+ }
478
+
479
+ wsp_ggml_backend_buffer_type_t wsp_ggml_backend_dev_buffer_type(wsp_ggml_backend_dev_t device) {
480
+ return device->iface.get_buffer_type(device);
481
+ }
482
+
483
+ wsp_ggml_backend_buffer_type_t wsp_ggml_backend_dev_host_buffer_type(wsp_ggml_backend_dev_t device) {
484
+ if (device->iface.get_host_buffer_type == NULL) {
485
+ return NULL;
486
+ }
487
+
488
+ return device->iface.get_host_buffer_type(device);
489
+ }
490
+
491
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_dev_buffer_from_host_ptr(wsp_ggml_backend_dev_t device, void * ptr, size_t size, size_t max_tensor_size) {
492
+ return device->iface.buffer_from_host_ptr(device, ptr, size, max_tensor_size);
493
+ }
494
+
495
+ bool wsp_ggml_backend_dev_supports_op(wsp_ggml_backend_dev_t device, const struct wsp_ggml_tensor * op) {
496
+ return device->iface.supports_op(device, op);
497
+ }
498
+
499
+ bool wsp_ggml_backend_dev_supports_buft(wsp_ggml_backend_dev_t device, wsp_ggml_backend_buffer_type_t buft) {
500
+ return device->iface.supports_buft(device, buft);
501
+ }
502
+
503
+ bool wsp_ggml_backend_dev_offload_op(wsp_ggml_backend_dev_t device, const struct wsp_ggml_tensor * op) {
504
+ if (device->iface.offload_op != NULL) {
505
+ return device->iface.offload_op(device, op);
506
+ }
507
+
508
+ return false;
509
+ }
510
+
511
+ // Backend (reg)
512
+
513
+ const char * wsp_ggml_backend_reg_name(wsp_ggml_backend_reg_t reg) {
514
+ return reg->iface.get_name(reg);
515
+ }
516
+
517
+ size_t wsp_ggml_backend_reg_dev_count(wsp_ggml_backend_reg_t reg) {
518
+ return reg->iface.get_device_count(reg);
519
+ }
520
+
521
+ wsp_ggml_backend_dev_t wsp_ggml_backend_reg_dev_get(wsp_ggml_backend_reg_t reg, size_t index) {
522
+ return reg->iface.get_device(reg, index);
523
+ }
524
+
525
+ void * wsp_ggml_backend_reg_get_proc_address(wsp_ggml_backend_reg_t reg, const char * name) {
526
+ if (!reg->iface.get_proc_address) {
527
+ return NULL;
528
+ }
529
+ return reg->iface.get_proc_address(reg, name);
530
+ }
531
+
532
+ // multi-buffer buffer
533
+
534
+ struct wsp_ggml_backend_multi_buffer_context {
535
+ wsp_ggml_backend_buffer_t * buffers;
536
+ size_t n_buffers;
537
+ };
538
+
539
+ static void wsp_ggml_backend_multi_buffer_free_buffer(wsp_ggml_backend_buffer_t buffer) {
540
+ wsp_ggml_backend_multi_buffer_context * ctx = (wsp_ggml_backend_multi_buffer_context *) buffer->context;
541
+ for (size_t i = 0; i < ctx->n_buffers; i++) {
542
+ wsp_ggml_backend_buffer_free(ctx->buffers[i]);
543
+ }
544
+
545
+ free(ctx->buffers);
546
+ free(ctx);
547
+ }
548
+
549
+ static void wsp_ggml_backend_multi_buffer_clear(wsp_ggml_backend_buffer_t buffer, uint8_t value) {
550
+ wsp_ggml_backend_multi_buffer_context * ctx = (wsp_ggml_backend_multi_buffer_context *) buffer->context;
551
+ for (size_t i = 0; i < ctx->n_buffers; i++) {
552
+ wsp_ggml_backend_buffer_clear(ctx->buffers[i], value);
553
+ }
554
+ }
555
+
556
+ static const struct wsp_ggml_backend_buffer_i wsp_ggml_backend_multi_buffer_i = {
557
+ /* .free_buffer = */ wsp_ggml_backend_multi_buffer_free_buffer,
558
+ /* .get_base = */ NULL,
559
+ /* .init_tensor = */ NULL,
560
+ /* .memset_tensor = */ NULL,
561
+ /* .set_tensor = */ NULL,
562
+ /* .get_tensor = */ NULL,
563
+ /* .cpy_tensor = */ NULL,
564
+ /* .clear = */ wsp_ggml_backend_multi_buffer_clear,
565
+ /* .reset = */ NULL,
566
+ };
567
+
568
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_multi_buffer_alloc_buffer(wsp_ggml_backend_buffer_t * buffers, size_t n_buffers) {
569
+ wsp_ggml_backend_multi_buffer_context * ctx = (wsp_ggml_backend_multi_buffer_context *) malloc(sizeof(struct wsp_ggml_backend_multi_buffer_context));
570
+ ctx->n_buffers = n_buffers;
571
+ ctx->buffers = (wsp_ggml_backend_buffer_t *) malloc(n_buffers * sizeof(wsp_ggml_backend_buffer_t));
572
+
573
+ WSP_GGML_ASSERT(ctx->buffers != NULL);
574
+
575
+ size_t total_size = 0;
576
+ for (size_t i = 0; i < n_buffers; i++) {
577
+ ctx->buffers[i] = buffers[i];
578
+ total_size += wsp_ggml_backend_buffer_get_size(buffers[i]);
579
+ }
580
+
581
+ return wsp_ggml_backend_buffer_init(buffers[0]->buft, wsp_ggml_backend_multi_buffer_i, ctx, total_size);
582
+ }
583
+
584
+ bool wsp_ggml_backend_buffer_is_multi_buffer(wsp_ggml_backend_buffer_t buffer) {
585
+ return buffer->iface.free_buffer == wsp_ggml_backend_multi_buffer_free_buffer;
586
+ }
587
+
588
+ void wsp_ggml_backend_multi_buffer_set_usage(wsp_ggml_backend_buffer_t buffer, enum wsp_ggml_backend_buffer_usage usage) {
589
+ WSP_GGML_ASSERT(wsp_ggml_backend_buffer_is_multi_buffer(buffer));
590
+ wsp_ggml_backend_multi_buffer_context * ctx = (wsp_ggml_backend_multi_buffer_context *) buffer->context;
591
+ for (size_t i = 0; i < ctx->n_buffers; i++) {
592
+ wsp_ggml_backend_buffer_set_usage(ctx->buffers[i], usage);
593
+ }
594
+ }
595
+
596
+ // creates a copy of the tensor with the same memory layout
597
+ static struct wsp_ggml_tensor * wsp_ggml_dup_tensor_layout(struct wsp_ggml_context * ctx, const struct wsp_ggml_tensor * tensor) {
598
+ struct wsp_ggml_tensor * dup = wsp_ggml_dup_tensor(ctx, tensor);
599
+ for (int i = 0; i < WSP_GGML_MAX_DIMS; i++) {
600
+ dup->nb[i] = tensor->nb[i];
601
+ }
602
+ return dup;
603
+ }
604
+
605
+ static bool wsp_ggml_is_view_op(enum wsp_ggml_op op) {
606
+ return op == WSP_GGML_OP_VIEW || op == WSP_GGML_OP_RESHAPE || op == WSP_GGML_OP_PERMUTE || op == WSP_GGML_OP_TRANSPOSE;
607
+ }
608
+
609
+ // scheduler
610
+
611
+ #ifndef WSP_GGML_SCHED_MAX_BACKENDS
612
+ #define WSP_GGML_SCHED_MAX_BACKENDS 16
613
+ #endif
614
+
615
+ #ifndef WSP_GGML_SCHED_MAX_SPLIT_INPUTS
616
+ #define WSP_GGML_SCHED_MAX_SPLIT_INPUTS WSP_GGML_MAX_SRC
617
+ #endif
618
+
619
+ #ifndef WSP_GGML_SCHED_MAX_COPIES
620
+ #define WSP_GGML_SCHED_MAX_COPIES 4
621
+ #endif
622
+
623
+ struct wsp_ggml_backend_sched_split {
624
+ int backend_id;
625
+ int i_start;
626
+ int i_end;
627
+ struct wsp_ggml_tensor * inputs[WSP_GGML_SCHED_MAX_SPLIT_INPUTS];
628
+ int n_inputs;
629
+ // graph view of this split
630
+ struct wsp_ggml_cgraph graph;
631
+ };
632
+
633
+ struct wsp_ggml_backend_sched {
634
+ bool is_reset; // true if the scheduler has been reset since the last graph split
635
+ bool is_alloc;
636
+
637
+ int n_backends;
638
+
639
+ wsp_ggml_backend_t backends[WSP_GGML_SCHED_MAX_BACKENDS];
640
+ wsp_ggml_backend_buffer_type_t bufts[WSP_GGML_SCHED_MAX_BACKENDS];
641
+ wsp_ggml_gallocr_t galloc;
642
+
643
+ // hash map of the nodes in the graph
644
+ struct wsp_ggml_hash_set hash_set;
645
+ int * hv_tensor_backend_ids; // [hash_set.size]
646
+ struct wsp_ggml_tensor ** hv_tensor_copies; // [hash_set.size][n_backends][n_copies]
647
+
648
+ int * node_backend_ids; // [graph_size]
649
+ int * leaf_backend_ids; // [graph_size]
650
+
651
+ int * prev_node_backend_ids; // [graph_size]
652
+ int * prev_leaf_backend_ids; // [graph_size]
653
+
654
+ // copy of the graph with modified inputs
655
+ struct wsp_ggml_cgraph graph;
656
+
657
+ // graph splits
658
+ struct wsp_ggml_backend_sched_split * splits;
659
+ int n_splits;
660
+ int splits_capacity;
661
+
662
+ // pipeline parallelism support
663
+ int n_copies;
664
+ int cur_copy;
665
+ wsp_ggml_backend_event_t events[WSP_GGML_SCHED_MAX_BACKENDS][WSP_GGML_SCHED_MAX_COPIES];
666
+ struct wsp_ggml_tensor * graph_inputs[WSP_GGML_SCHED_MAX_SPLIT_INPUTS];
667
+ int n_graph_inputs;
668
+
669
+ struct wsp_ggml_context * ctx;
670
+
671
+ wsp_ggml_backend_sched_eval_callback callback_eval;
672
+ void * callback_eval_user_data;
673
+
674
+ char * context_buffer;
675
+ size_t context_buffer_size;
676
+
677
+ bool op_offload;
678
+
679
+ int debug;
680
+ };
681
+
682
+ #define hash_id(tensor) wsp_ggml_hash_find_or_insert(&sched->hash_set, tensor)
683
+ #define tensor_backend_id(tensor) sched->hv_tensor_backend_ids[hash_id(tensor)]
684
+ #define tensor_id_copy(id, backend_id, copy_id) sched->hv_tensor_copies[(id) * sched->n_backends * sched->n_copies + (backend_id) * sched->n_copies + (copy_id)]
685
+ #define tensor_copy(tensor, backend_id, copy_id) tensor_id_copy(hash_id(tensor), backend_id, copy_id)
686
+
687
+ // returns the priority of the backend, lower id is higher priority
688
+ static int wsp_ggml_backend_sched_backend_id(wsp_ggml_backend_sched_t sched, wsp_ggml_backend_t backend) {
689
+ for (int i = 0; i < sched->n_backends; i++) {
690
+ if (sched->backends[i] == backend) {
691
+ return i;
692
+ }
693
+ }
694
+ return -1;
695
+ }
696
+
697
+ static int wsp_ggml_backend_sched_backend_from_buffer(wsp_ggml_backend_sched_t sched, const struct wsp_ggml_tensor * tensor, const struct wsp_ggml_tensor * op) {
698
+ wsp_ggml_backend_buffer_t buffer = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
699
+ if (buffer == NULL) {
700
+ return -1;
701
+ }
702
+
703
+ // find highest prio backend that supports the buffer type and the op
704
+ for (int i = 0; i < sched->n_backends; i++) {
705
+ if (wsp_ggml_backend_supports_buft(sched->backends[i], buffer->buft) &&
706
+ wsp_ggml_backend_supports_op(sched->backends[i], op)) {
707
+ return i;
708
+ }
709
+ }
710
+
711
+ #ifndef NDEBUG
712
+ WSP_GGML_LOG_DEBUG("%s: warning: no backend supports op %s with a weight with buffer type %s used in tensor %s, the weight will need to be copied\n",
713
+ __func__, wsp_ggml_op_desc(tensor), wsp_ggml_backend_buffer_name(buffer), tensor->name);
714
+ #endif
715
+
716
+ return -1;
717
+ }
718
+
719
+ #if 0
720
+ #define WSP_GGML_SCHED_MAX_SPLITS_DEBUG 4096
721
+ static char causes[WSP_GGML_DEFAULT_GRAPH_SIZE*16 + WSP_GGML_SCHED_MAX_SPLITS_DEBUG*WSP_GGML_SCHED_MAX_SPLIT_INPUTS][128]; // debug only
722
+ #define SET_CAUSE(node, ...) sprintf(causes[hash_id(node)], __VA_ARGS__)
723
+ #define GET_CAUSE(node) causes[hash_id(node)]
724
+ #else
725
+ #define SET_CAUSE(node, ...)
726
+ #define GET_CAUSE(node) ""
727
+ #endif
728
+
729
+ // returns the backend that should be used for the node based on the current locations
730
+ static int wsp_ggml_backend_sched_backend_id_from_cur(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * tensor) {
731
+ // assign pre-allocated nodes to their backend
732
+ int cur_backend_id = wsp_ggml_backend_sched_backend_from_buffer(sched, tensor, tensor);
733
+ if (cur_backend_id != -1) {
734
+ SET_CAUSE(tensor, "1.dst");
735
+ return cur_backend_id;
736
+ }
737
+
738
+ // view_src
739
+ if (tensor->view_src != NULL) {
740
+ cur_backend_id = wsp_ggml_backend_sched_backend_from_buffer(sched, tensor->view_src, tensor);
741
+ if (cur_backend_id != -1) {
742
+ SET_CAUSE(tensor, "1.vsrc");
743
+ return cur_backend_id;
744
+ }
745
+ }
746
+
747
+ if (tensor->buffer || (tensor->view_src && tensor->view_src->buffer)) {
748
+ // since the tensor is pre-allocated, it cannot be moved to another backend
749
+ wsp_ggml_backend_buffer_t buffer = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
750
+ WSP_GGML_ABORT("pre-allocated tensor (%s) in a buffer (%s) that cannot run the operation (%s)", tensor->name, wsp_ggml_backend_buffer_name(buffer), wsp_ggml_op_name(tensor->op));
751
+ }
752
+
753
+ // graph input
754
+ if (tensor->flags & WSP_GGML_TENSOR_FLAG_INPUT) {
755
+ cur_backend_id = sched->n_backends - 1; // last backend (assumed CPU)
756
+ SET_CAUSE(tensor, "1.inp");
757
+ return cur_backend_id;
758
+ }
759
+
760
+ // operations with weights are preferably run on the same backend as the weights
761
+ for (int i = 0; i < WSP_GGML_MAX_SRC; i++) {
762
+ const struct wsp_ggml_tensor * src = tensor->src[i];
763
+ if (src == NULL) {
764
+ continue;
765
+ }
766
+ // skip ROPE since the rope freqs tensor is too small to choose a backend based on it
767
+ // not an ideal solution
768
+ if (tensor->op != WSP_GGML_OP_ROPE && src->buffer != NULL && src->buffer->usage == WSP_GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
769
+ int src_backend_id = wsp_ggml_backend_sched_backend_from_buffer(sched, src, tensor);
770
+ // check if a backend with higher prio wants to offload the op
771
+ if (sched->op_offload && src_backend_id == sched->n_backends - 1 && wsp_ggml_backend_buffer_is_host(src->buffer)) {
772
+ for (int b = 0; b < src_backend_id; b++) {
773
+ if (wsp_ggml_backend_supports_op(sched->backends[b], tensor) && wsp_ggml_backend_offload_op(sched->backends[b], tensor)) {
774
+ SET_CAUSE(tensor, "1.off");
775
+ return b;
776
+ }
777
+ }
778
+ }
779
+ SET_CAUSE(tensor, "1.wgt%d", i);
780
+ return src_backend_id;
781
+ }
782
+ }
783
+
784
+ return -1;
785
+ }
786
+
787
+ static char * fmt_size(size_t size) {
788
+ static char buffer[128];
789
+ if (size >= 1024*1024) {
790
+ snprintf(buffer, sizeof(buffer), "%zuM", size/1024/1024);
791
+ } else {
792
+ snprintf(buffer, sizeof(buffer), "%zuK", size/1024);
793
+ }
794
+ return buffer;
795
+ }
796
+
797
+ static void wsp_ggml_backend_sched_print_assignments(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph) {
798
+ int cur_split = 0;
799
+ for (int i = 0; i < graph->n_nodes; i++) {
800
+ if (cur_split < sched->n_splits && i == sched->splits[cur_split].i_start) {
801
+ wsp_ggml_backend_t split_backend = sched->backends[sched->splits[cur_split].backend_id];
802
+ WSP_GGML_LOG_DEBUG("\n## SPLIT #%d: %s # %d inputs", cur_split, wsp_ggml_backend_name(split_backend),
803
+ sched->splits[cur_split].n_inputs);
804
+ for (int j = 0; j < sched->splits[cur_split].n_inputs; j++) {
805
+ if (j == 0) {
806
+ WSP_GGML_LOG_DEBUG(": ");
807
+ }
808
+ WSP_GGML_LOG_DEBUG("[%s (%5.5s)] ", sched->splits[cur_split].inputs[j]->name,
809
+ fmt_size(wsp_ggml_nbytes(sched->splits[cur_split].inputs[j])));
810
+ }
811
+ WSP_GGML_LOG_DEBUG("\n");
812
+ cur_split++;
813
+ }
814
+ struct wsp_ggml_tensor * node = graph->nodes[i];
815
+ if (wsp_ggml_is_view_op(node->op)) {
816
+ continue;
817
+ }
818
+ if (sched->debug > 1) {
819
+ wsp_ggml_backend_t tensor_backend = wsp_ggml_backend_sched_get_tensor_backend(sched, node);
820
+ WSP_GGML_LOG_DEBUG("node #%3d (%10.10s): %20.20s (%5.5s) [%5.5s %8.8s]:", i, wsp_ggml_op_name(node->op), node->name,
821
+ fmt_size(wsp_ggml_nbytes(node)), tensor_backend ? wsp_ggml_backend_name(tensor_backend) : "NULL", GET_CAUSE(node));
822
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
823
+ struct wsp_ggml_tensor * src = node->src[j];
824
+ if (src == NULL) {
825
+ continue;
826
+ }
827
+ wsp_ggml_backend_t src_backend = wsp_ggml_backend_sched_get_tensor_backend(sched, src);
828
+ WSP_GGML_LOG_DEBUG(" %20.20s (%5.5s) [%5.5s %8.8s]", src->name,
829
+ fmt_size(wsp_ggml_nbytes(src)), src_backend ? wsp_ggml_backend_name(src_backend) : "NULL", GET_CAUSE(src));
830
+ }
831
+ WSP_GGML_LOG_DEBUG("\n");
832
+ }
833
+ }
834
+ }
835
+
836
+ static bool wsp_ggml_backend_sched_buffer_supported(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * t, int backend_id) {
837
+ wsp_ggml_backend_buffer_t buf = t->view_src ? t->view_src->buffer : t->buffer;
838
+ wsp_ggml_backend_buffer_type_t buft = NULL;
839
+
840
+ if (buf) {
841
+ // the tensor is already allocated
842
+ buft = buf->buft;
843
+ } else {
844
+ // see if the tensor already has a backend assigned, and use the buffer type of that backend
845
+ int tensor_backend_id = tensor_backend_id(t);
846
+ if (tensor_backend_id == -1 && t->view_src) {
847
+ tensor_backend_id = tensor_backend_id(t->view_src);
848
+ }
849
+ if (tensor_backend_id != -1) {
850
+ buft = sched->bufts[tensor_backend_id];
851
+ }
852
+ }
853
+
854
+ return buft != NULL && wsp_ggml_backend_supports_buft(sched->backends[backend_id], buft);
855
+ }
856
+
857
+ static void wsp_ggml_backend_sched_set_if_supported(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * node, int cur_backend_id, int * node_backend_id) {
858
+ if (wsp_ggml_backend_supports_op(sched->backends[cur_backend_id], node)) {
859
+ *node_backend_id = cur_backend_id;
860
+ SET_CAUSE(node, "2.sup");
861
+ }
862
+ }
863
+
864
+ // assigns backends to ops and splits the graph into subgraphs that can be computed on the same backend
865
+ static void wsp_ggml_backend_sched_split_graph(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph) {
866
+ // reset splits
867
+ sched->n_splits = 0;
868
+ sched->n_graph_inputs = 0;
869
+ sched->is_reset = false;
870
+
871
+ struct wsp_ggml_init_params params = {
872
+ /* .mem_size = */ sched->context_buffer_size,
873
+ /* .mem_buffer = */ sched->context_buffer,
874
+ /* .no_alloc = */ true
875
+ };
876
+
877
+ wsp_ggml_free(sched->ctx);
878
+
879
+ sched->ctx = wsp_ggml_init(params);
880
+ if (sched->ctx == NULL) {
881
+ WSP_GGML_ABORT("%s: failed to initialize context\n", __func__);
882
+ }
883
+
884
+ // pass 1: assign backends to ops with pre-allocated inputs
885
+ for (int i = 0; i < graph->n_leafs; i++) {
886
+ struct wsp_ggml_tensor * leaf = graph->leafs[i];
887
+ int * leaf_backend_id = &tensor_backend_id(leaf);
888
+ // do not overwrite user assignments
889
+ if (*leaf_backend_id == -1) {
890
+ *leaf_backend_id = wsp_ggml_backend_sched_backend_id_from_cur(sched, leaf);
891
+ }
892
+ }
893
+
894
+ for (int i = 0; i < graph->n_nodes; i++) {
895
+ struct wsp_ggml_tensor * node = graph->nodes[i];
896
+ int * node_backend_id = &tensor_backend_id(node);
897
+ // do not overwrite user assignments
898
+ if (*node_backend_id == -1) {
899
+ *node_backend_id = wsp_ggml_backend_sched_backend_id_from_cur(sched, node);
900
+
901
+ #if 0
902
+ // src
903
+ if (node->op == WSP_GGML_OP_NONE) {
904
+ continue;
905
+ }
906
+
907
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
908
+ struct wsp_ggml_tensor * src = node->src[j];
909
+ if (src == NULL) {
910
+ continue;
911
+ }
912
+ int * src_backend_id = &tensor_backend_id(src);
913
+ if (*src_backend_id == -1) {
914
+ *src_backend_id = wsp_ggml_backend_sched_backend_id_from_cur(sched, src);
915
+ }
916
+ }
917
+ #endif
918
+ }
919
+ }
920
+
921
+ // pass 2: expand current backend assignments
922
+ // assign the same backend to adjacent nodes
923
+ // expand gpu backends (i.e. non last prio) up and down, ignoring cpu (the lowest priority backend)
924
+ // thus, cpu will never be used unless weights are on cpu, or there are no gpu ops between cpu ops
925
+ // ops unsupported by the backend being expanded will be left unassigned so that they can be assigned later when the locations of its inputs are known
926
+ // expand gpu down
927
+ {
928
+ int cur_backend_id = -1;
929
+ for (int i = 0; i < graph->n_nodes; i++) {
930
+ struct wsp_ggml_tensor * node = graph->nodes[i];
931
+ if (wsp_ggml_is_view_op(node->op)) {
932
+ continue;
933
+ }
934
+ int * node_backend_id = &tensor_backend_id(node);
935
+ if (*node_backend_id != -1) {
936
+ if (*node_backend_id == sched->n_backends - 1) {
937
+ // skip cpu (lowest prio backend)
938
+ cur_backend_id = -1;
939
+ } else {
940
+ cur_backend_id = *node_backend_id;
941
+ }
942
+ } else if (cur_backend_id != -1) {
943
+ wsp_ggml_backend_sched_set_if_supported(sched, node, cur_backend_id, node_backend_id);
944
+ }
945
+ }
946
+ }
947
+ // expand gpu up
948
+ {
949
+ int cur_backend_id = -1;
950
+ for (int i = graph->n_nodes - 1; i >= 0; i--) {
951
+ struct wsp_ggml_tensor * node = graph->nodes[i];
952
+ if (wsp_ggml_is_view_op(node->op)) {
953
+ continue;
954
+ }
955
+ int * node_backend_id = &tensor_backend_id(node);
956
+ if (*node_backend_id != -1) {
957
+ if (*node_backend_id == sched->n_backends - 1) {
958
+ // skip cpu (lowest prio backend)
959
+ cur_backend_id = -1;
960
+ } else {
961
+ cur_backend_id = *node_backend_id;
962
+ }
963
+ } else if (cur_backend_id != -1) {
964
+ wsp_ggml_backend_sched_set_if_supported(sched, node, cur_backend_id, node_backend_id);
965
+ }
966
+ }
967
+ }
968
+ // expand rest down
969
+ {
970
+ int cur_backend_id = -1;
971
+ for (int i = 0; i < graph->n_nodes; i++) {
972
+ struct wsp_ggml_tensor * node = graph->nodes[i];
973
+ if (wsp_ggml_is_view_op(node->op)) {
974
+ continue;
975
+ }
976
+ int * node_backend_id = &tensor_backend_id(node);
977
+ if (*node_backend_id != -1) {
978
+ cur_backend_id = *node_backend_id;
979
+ } else if (cur_backend_id != -1) {
980
+ wsp_ggml_backend_sched_set_if_supported(sched, node, cur_backend_id, node_backend_id);
981
+ }
982
+ }
983
+ }
984
+ // expand rest up
985
+ {
986
+ int cur_backend_id = -1;
987
+ for (int i = graph->n_nodes - 1; i >= 0; i--) {
988
+ struct wsp_ggml_tensor * node = graph->nodes[i];
989
+ if (wsp_ggml_is_view_op(node->op)) {
990
+ continue;
991
+ }
992
+ int * node_backend_id = &tensor_backend_id(node);
993
+ if (*node_backend_id != -1) {
994
+ cur_backend_id = *node_backend_id;
995
+ } else if (cur_backend_id != -1) {
996
+ wsp_ggml_backend_sched_set_if_supported(sched, node, cur_backend_id, node_backend_id);
997
+ }
998
+ }
999
+ }
1000
+
1001
+ // pass 3: upgrade nodes to higher prio backends with compatible buffer types
1002
+ // if the tensor is already in the same buffer type (*) as another higher priority backend, we should move it there
1003
+ // however, we also need to verify that the sources are in compatible buffer types
1004
+ // (*) the actual requirement is more relaxed, the buffer type of the backend should be supported by all the users of this tensor further down the graph
1005
+ // however, this is slow to verify, so we have a more strict requirement that the buffer type is the same
1006
+ // this is not uncommon since multiple backends can use host memory, with the same buffer type (eg. BLAS and CPU)
1007
+ // additionally, set remaining unassigned nodes to the backend with the most supported inputs
1008
+ // only nodes that could not be assigned during expansion due to the backend not supporting the op should be unassigned at this point
1009
+ for (int i = 0; i < graph->n_nodes; i++) {
1010
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1011
+ if (wsp_ggml_is_view_op(node->op)) {
1012
+ continue;
1013
+ }
1014
+ int * node_backend_id = &tensor_backend_id(node);
1015
+ if (*node_backend_id == -1) {
1016
+ // unassigned node: find the backend with the most supported inputs
1017
+ int n_supported_best = -1;
1018
+ for (int b = 0; b < sched->n_backends; b++) {
1019
+ if (wsp_ggml_backend_supports_op(sched->backends[b], node)) {
1020
+ int n_supported = 0;
1021
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
1022
+ struct wsp_ggml_tensor * src = node->src[j];
1023
+ if (src == NULL) {
1024
+ continue;
1025
+ }
1026
+ if ((tensor_backend_id(src) != -1 || tensor_backend_id(src->view_src) != -1) && wsp_ggml_backend_sched_buffer_supported(sched, src, b)) {
1027
+ n_supported++;
1028
+ }
1029
+ }
1030
+ if (n_supported > n_supported_best) {
1031
+ n_supported_best = n_supported;
1032
+ *node_backend_id = b;
1033
+ SET_CAUSE(node, "3.best");
1034
+ }
1035
+ }
1036
+ }
1037
+ } else {
1038
+ // assigned node: upgrade to higher prio backend if possible
1039
+ for (int b = 0; b < *node_backend_id; b++) {
1040
+ if (sched->bufts[b] == sched->bufts[*node_backend_id] && wsp_ggml_backend_supports_op(sched->backends[b], node)) {
1041
+ bool supported = true;
1042
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
1043
+ struct wsp_ggml_tensor * src = node->src[j];
1044
+ if (src == NULL) {
1045
+ continue;
1046
+ }
1047
+ if (!wsp_ggml_backend_sched_buffer_supported(sched, src, b)) {
1048
+ supported = false;
1049
+ break;
1050
+ }
1051
+ }
1052
+ if (supported) {
1053
+ *node_backend_id = b;
1054
+ SET_CAUSE(node, "3.upg");
1055
+ break;
1056
+ }
1057
+ }
1058
+ }
1059
+ }
1060
+ }
1061
+
1062
+ // pass 4: assign backends to remaining src from dst and view_src
1063
+ for (int i = 0; i < graph->n_nodes; i++) {
1064
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1065
+ int * cur_backend_id = &tensor_backend_id(node);
1066
+ if (node->view_src != NULL && *cur_backend_id == -1) {
1067
+ *cur_backend_id = tensor_backend_id(node->view_src);
1068
+ SET_CAUSE(node, "4.vsrc");
1069
+ }
1070
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
1071
+ struct wsp_ggml_tensor * src = node->src[j];
1072
+ if (src == NULL) {
1073
+ continue;
1074
+ }
1075
+ int * src_backend_id = &tensor_backend_id(src);
1076
+ if (*src_backend_id == -1) {
1077
+ if (src->view_src != NULL) {
1078
+ // views are always on the same backend as the source
1079
+ *src_backend_id = tensor_backend_id(src->view_src);
1080
+ SET_CAUSE(src, "4.vsrc");
1081
+ } else {
1082
+ *src_backend_id = *cur_backend_id;
1083
+ SET_CAUSE(src, "4.cur");
1084
+ }
1085
+ }
1086
+ }
1087
+ }
1088
+
1089
+ // pass 5: split graph, find tensors that need to be copied
1090
+ {
1091
+ int i_split = 0;
1092
+ struct wsp_ggml_backend_sched_split * split = &sched->splits[0];
1093
+ // find the backend of the first split, skipping view ops
1094
+ int i = 0;
1095
+ for (; i < graph->n_nodes; i++) {
1096
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1097
+ if (!wsp_ggml_is_view_op(node->op)) {
1098
+ split->backend_id = tensor_backend_id(node);
1099
+ break;
1100
+ }
1101
+ }
1102
+ split->i_start = 0;
1103
+ split->n_inputs = 0;
1104
+ int cur_backend_id = split->backend_id;
1105
+ for (; i < graph->n_nodes; i++) {
1106
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1107
+
1108
+ if (wsp_ggml_is_view_op(node->op)) {
1109
+ continue;
1110
+ }
1111
+
1112
+ const int node_backend_id = tensor_backend_id(node);
1113
+
1114
+ assert(node_backend_id != -1); // all nodes should be assigned by now, this can happen if there is no CPU fallback
1115
+
1116
+ // check if we should start a new split based on the sources of the current node
1117
+ bool need_new_split = false;
1118
+ if (node_backend_id == cur_backend_id && split->n_inputs > 0) {
1119
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
1120
+ struct wsp_ggml_tensor * src = node->src[j];
1121
+ if (src == NULL) {
1122
+ continue;
1123
+ }
1124
+ // check if a weight is on a different and incompatible backend
1125
+ // by starting a new split, the memory of the previously offloaded weights can be reused
1126
+ if (src->buffer != NULL && src->buffer->usage == WSP_GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
1127
+ int src_backend_id = tensor_backend_id(src);
1128
+ if (src_backend_id != cur_backend_id && !wsp_ggml_backend_sched_buffer_supported(sched, src, cur_backend_id)) {
1129
+ need_new_split = true;
1130
+ break;
1131
+ }
1132
+ }
1133
+ // check if the split has too many inputs
1134
+ // FIXME: count the number of inputs instead of only checking when full
1135
+ if (split->n_inputs == WSP_GGML_SCHED_MAX_SPLIT_INPUTS) {
1136
+ const size_t id = hash_id(src);
1137
+ int src_backend_id = sched->hv_tensor_backend_ids[id];
1138
+ bool supported = wsp_ggml_backend_sched_buffer_supported(sched, src, cur_backend_id);
1139
+ if (src_backend_id != cur_backend_id && tensor_id_copy(id, cur_backend_id, 0) == NULL && !supported) {
1140
+ need_new_split = true;
1141
+ break;
1142
+ }
1143
+ }
1144
+ }
1145
+ }
1146
+
1147
+ if (node_backend_id != cur_backend_id || need_new_split) {
1148
+ split->i_end = i;
1149
+ i_split++;
1150
+ if (i_split >= sched->splits_capacity) {
1151
+ sched->splits_capacity *= 2;
1152
+ sched->splits = (wsp_ggml_backend_sched_split *)
1153
+ realloc(sched->splits, sched->splits_capacity * sizeof(struct wsp_ggml_backend_sched_split));
1154
+ WSP_GGML_ASSERT(sched->splits != NULL);
1155
+ }
1156
+ split = &sched->splits[i_split];
1157
+ split->backend_id = node_backend_id;
1158
+ split->i_start = i;
1159
+ split->n_inputs = 0;
1160
+ cur_backend_id = node_backend_id;
1161
+ }
1162
+
1163
+ // find inputs that are not on the same backend
1164
+ for (int j = 0; j < WSP_GGML_MAX_SRC; j++) {
1165
+ struct wsp_ggml_tensor * src = node->src[j];
1166
+ if (src == NULL) {
1167
+ continue;
1168
+ }
1169
+
1170
+ size_t src_id = hash_id(src);
1171
+ const int src_backend_id = sched->hv_tensor_backend_ids[src_id];
1172
+ assert(src_backend_id != -1); // all inputs should be assigned by now
1173
+
1174
+ if (src->flags & WSP_GGML_TENSOR_FLAG_INPUT && sched->n_copies > 1) {
1175
+ if (tensor_id_copy(src_id, src_backend_id, 0) == NULL) {
1176
+ wsp_ggml_backend_t backend = sched->backends[src_backend_id];
1177
+ for (int c = 0; c < sched->n_copies; c++) {
1178
+ struct wsp_ggml_tensor * tensor_copy;
1179
+ if (c == sched->cur_copy) {
1180
+ tensor_copy = src; // use the original tensor as the current copy
1181
+ } else {
1182
+ tensor_copy = wsp_ggml_dup_tensor_layout(sched->ctx, src);
1183
+ wsp_ggml_format_name(tensor_copy, "%s#%s#%d", wsp_ggml_backend_name(backend), src->name, c);
1184
+ }
1185
+ if (sched->n_copies > 1) {
1186
+ wsp_ggml_set_input(tensor_copy);
1187
+ wsp_ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
1188
+ }
1189
+ tensor_id_copy(src_id, src_backend_id, c) = tensor_copy;
1190
+ SET_CAUSE(tensor_copy, "4.cpy");
1191
+ }
1192
+ int n_graph_inputs = sched->n_graph_inputs++;
1193
+ WSP_GGML_ASSERT(n_graph_inputs < WSP_GGML_SCHED_MAX_SPLIT_INPUTS);
1194
+ sched->graph_inputs[n_graph_inputs] = src;
1195
+ }
1196
+ }
1197
+
1198
+ if (src_backend_id != cur_backend_id && !wsp_ggml_backend_sched_buffer_supported(sched, src, cur_backend_id)) {
1199
+ // create a copy of the input in the split's backend
1200
+ if (tensor_id_copy(src_id, cur_backend_id, 0) == NULL) {
1201
+ wsp_ggml_backend_t backend = sched->backends[cur_backend_id];
1202
+ for (int c = 0; c < sched->n_copies; c++) {
1203
+ struct wsp_ggml_tensor * tensor_copy = wsp_ggml_dup_tensor_layout(sched->ctx, src);
1204
+ wsp_ggml_format_name(tensor_copy, "%s#%s#%d", wsp_ggml_backend_name(backend), src->name, c);
1205
+ if (sched->n_copies > 1) {
1206
+ wsp_ggml_set_input(tensor_copy);
1207
+ wsp_ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
1208
+ }
1209
+ tensor_id_copy(src_id, cur_backend_id, c) = tensor_copy;
1210
+ SET_CAUSE(tensor_copy, "4.cpy");
1211
+ }
1212
+ int n_inputs = split->n_inputs++;
1213
+ WSP_GGML_ASSERT(n_inputs < WSP_GGML_SCHED_MAX_SPLIT_INPUTS);
1214
+ split->inputs[n_inputs] = src;
1215
+ }
1216
+ node->src[j] = tensor_id_copy(src_id, cur_backend_id, sched->cur_copy);
1217
+ }
1218
+ }
1219
+ }
1220
+ split->i_end = graph->n_nodes;
1221
+ sched->n_splits = i_split + 1;
1222
+ }
1223
+
1224
+ if (sched->debug) {
1225
+ wsp_ggml_backend_sched_print_assignments(sched, graph);
1226
+ }
1227
+
1228
+ // swap node_backend_ids and leaf _backend_ids with prevs
1229
+ {
1230
+ int * tmp = sched->node_backend_ids;
1231
+ sched->node_backend_ids = sched->prev_node_backend_ids;
1232
+ sched->prev_node_backend_ids = tmp;
1233
+
1234
+ tmp = sched->leaf_backend_ids;
1235
+ sched->leaf_backend_ids = sched->prev_leaf_backend_ids;
1236
+ sched->prev_leaf_backend_ids = tmp;
1237
+ }
1238
+
1239
+ int graph_size = std::max(graph->n_nodes, graph->n_leafs) + sched->n_splits*WSP_GGML_SCHED_MAX_SPLIT_INPUTS*2*sched->n_copies;
1240
+ if (sched->graph.size < graph_size) {
1241
+ sched->graph.size = graph_size;
1242
+ sched->graph.nodes = (wsp_ggml_tensor **) realloc(sched->graph.nodes, graph_size * sizeof(struct wsp_ggml_tensor *));
1243
+ sched->graph.leafs = (wsp_ggml_tensor **) realloc(sched->graph.leafs, graph_size * sizeof(struct wsp_ggml_tensor *));
1244
+ WSP_GGML_ASSERT(sched->graph.nodes != NULL);
1245
+ WSP_GGML_ASSERT(sched->graph.leafs != NULL);
1246
+ }
1247
+ sched->graph.n_nodes = 0;
1248
+ sched->graph.n_leafs = 0;
1249
+
1250
+ struct wsp_ggml_cgraph * graph_copy = &sched->graph;
1251
+
1252
+ for (int i = 0; i < sched->n_splits; i++) {
1253
+ struct wsp_ggml_backend_sched_split * split = &sched->splits[i];
1254
+ split->graph = wsp_ggml_graph_view(graph, split->i_start, split->i_end);
1255
+
1256
+ // add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
1257
+ for (int j = 0; j < split->n_inputs; j++) {
1258
+ assert(graph_copy->size > (graph_copy->n_nodes + 1));
1259
+
1260
+ struct wsp_ggml_tensor * input = split->inputs[j];
1261
+ const size_t input_id = hash_id(input);
1262
+ struct wsp_ggml_tensor * input_cpy = tensor_id_copy(input_id, split->backend_id, sched->cur_copy);
1263
+
1264
+ // add a dependency to the input source so that it is not freed before the copy is done
1265
+ struct wsp_ggml_tensor * input_dep = wsp_ggml_view_tensor(sched->ctx, input);
1266
+ input_dep->src[0] = input;
1267
+ sched->node_backend_ids[graph_copy->n_nodes] = sched->hv_tensor_backend_ids[input_id];
1268
+ graph_copy->nodes[graph_copy->n_nodes++] = input_dep;
1269
+
1270
+ // add a dependency to the input copy so that it is allocated at the start of the split
1271
+ sched->node_backend_ids[graph_copy->n_nodes] = split->backend_id;
1272
+ graph_copy->nodes[graph_copy->n_nodes++] = input_cpy;
1273
+ }
1274
+
1275
+ for (int j = split->i_start; j < split->i_end; j++) {
1276
+ assert(graph_copy->size > graph_copy->n_nodes);
1277
+ sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(graph->nodes[j]);
1278
+ graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
1279
+ }
1280
+ }
1281
+
1282
+ if (sched->n_copies > 1) {
1283
+ // add input copies as leafs so that they are allocated first
1284
+ for (int i = 0; i < sched->n_graph_inputs; i++) {
1285
+ struct wsp_ggml_tensor * input = sched->graph_inputs[i];
1286
+ size_t id = hash_id(input);
1287
+ int backend_id = tensor_backend_id(input);
1288
+ for (int c = 0; c < sched->n_copies; c++) {
1289
+ struct wsp_ggml_tensor * input_cpy = tensor_id_copy(id, backend_id, c);
1290
+ sched->leaf_backend_ids[graph_copy->n_leafs] = backend_id;
1291
+ assert(graph_copy->size > graph_copy->n_leafs);
1292
+ graph_copy->leafs[graph_copy->n_leafs++] = input_cpy;
1293
+ }
1294
+ }
1295
+
1296
+ for (int i = 0; i < sched->n_splits; i++) {
1297
+ struct wsp_ggml_backend_sched_split * split = &sched->splits[i];
1298
+ int backend_id = split->backend_id;
1299
+ for (int j = 0; j < split->n_inputs; j++) {
1300
+ struct wsp_ggml_tensor * input = split->inputs[j];
1301
+ size_t id = hash_id(input);
1302
+ for (int c = 0; c < sched->n_copies; c++) {
1303
+ struct wsp_ggml_tensor * input_cpy = tensor_id_copy(id, backend_id, c);
1304
+ sched->leaf_backend_ids[graph_copy->n_leafs] = backend_id;
1305
+ assert(graph_copy->size > graph_copy->n_leafs);
1306
+ graph_copy->leafs[graph_copy->n_leafs++] = input_cpy;
1307
+ }
1308
+ }
1309
+ }
1310
+ }
1311
+
1312
+ // add leafs from the original graph
1313
+ for (int i = 0; i < graph->n_leafs; i++) {
1314
+ struct wsp_ggml_tensor * leaf = graph->leafs[i];
1315
+ sched->leaf_backend_ids[graph_copy->n_leafs] = tensor_backend_id(leaf);
1316
+ assert(graph_copy->size > graph_copy->n_leafs);
1317
+ graph_copy->leafs[graph_copy->n_leafs++] = leaf;
1318
+ }
1319
+ }
1320
+
1321
+ static bool wsp_ggml_backend_sched_alloc_splits(wsp_ggml_backend_sched_t sched) {
1322
+ bool backend_ids_changed = false;
1323
+ for (int i = 0; i < sched->graph.n_nodes; i++) {
1324
+ if (sched->node_backend_ids[i] != sched->prev_node_backend_ids[i] &&
1325
+ sched->bufts[sched->node_backend_ids[i]] != sched->bufts[sched->prev_node_backend_ids[i]]) {
1326
+ backend_ids_changed = true;
1327
+ break;
1328
+ }
1329
+ }
1330
+ if (!backend_ids_changed) {
1331
+ for (int i = 0; i < sched->graph.n_leafs; i++) {
1332
+ if (sched->leaf_backend_ids[i] != sched->prev_leaf_backend_ids[i] &&
1333
+ sched->bufts[sched->leaf_backend_ids[i]] != sched->bufts[sched->prev_leaf_backend_ids[i]]) {
1334
+ backend_ids_changed = true;
1335
+ break;
1336
+ }
1337
+ }
1338
+ }
1339
+
1340
+ // allocate graph
1341
+ if (backend_ids_changed || !wsp_ggml_gallocr_alloc_graph(sched->galloc, &sched->graph)) {
1342
+ // the re-allocation may cause the split inputs to be moved to a different address
1343
+ // synchronize without wsp_ggml_backend_sched_synchronize to avoid changing cur_copy
1344
+ for (int i = 0; i < sched->n_backends; i++) {
1345
+ wsp_ggml_backend_synchronize(sched->backends[i]);
1346
+ }
1347
+ #ifndef NDEBUG
1348
+ WSP_GGML_LOG_DEBUG("%s: failed to allocate graph, reserving (backend_ids_changed = %d)\n", __func__, backend_ids_changed);
1349
+ #endif
1350
+ wsp_ggml_gallocr_reserve_n(sched->galloc, &sched->graph, sched->node_backend_ids, sched->leaf_backend_ids);
1351
+ if (!wsp_ggml_gallocr_alloc_graph(sched->galloc, &sched->graph)) {
1352
+ WSP_GGML_LOG_ERROR("%s: failed to allocate graph\n", __func__);
1353
+ return false;
1354
+ }
1355
+ }
1356
+
1357
+ return true;
1358
+ }
1359
+
1360
+ static enum wsp_ggml_status wsp_ggml_backend_sched_compute_splits(wsp_ggml_backend_sched_t sched) {
1361
+ struct wsp_ggml_backend_sched_split * splits = sched->splits;
1362
+
1363
+ for (int i = 0; i < sched->n_splits; i++) {
1364
+ struct wsp_ggml_backend_sched_split * split = &splits[i];
1365
+ int split_backend_id = split->backend_id;
1366
+ wsp_ggml_backend_t split_backend = sched->backends[split_backend_id];
1367
+
1368
+ // copy the input tensors to the split backend
1369
+ for (int j = 0; j < split->n_inputs; j++) {
1370
+ wsp_ggml_backend_t input_backend = wsp_ggml_backend_sched_get_tensor_backend(sched, split->inputs[j]);
1371
+ struct wsp_ggml_tensor * input = split->inputs[j];
1372
+ struct wsp_ggml_tensor * input_cpy = tensor_copy(input, split_backend_id, sched->cur_copy);
1373
+
1374
+ if (input->flags & WSP_GGML_TENSOR_FLAG_INPUT) {
1375
+ // inputs from the user must be copied immediately to prevent the user overwriting the data before the copy is done
1376
+ if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
1377
+ wsp_ggml_backend_event_synchronize(sched->events[split_backend_id][sched->cur_copy]);
1378
+ } else {
1379
+ wsp_ggml_backend_synchronize(split_backend);
1380
+ }
1381
+ wsp_ggml_backend_tensor_copy(input, input_cpy);
1382
+ } else {
1383
+ // wait for the split backend to finish using the input before overwriting it
1384
+ if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
1385
+ wsp_ggml_backend_event_wait(split_backend, sched->events[split_backend_id][sched->cur_copy]);
1386
+ } else {
1387
+ wsp_ggml_backend_synchronize(split_backend);
1388
+ }
1389
+ // try async copy, but if not possible, we can still use a sync copy without synchronizing the dst backend, since we handle the synchronization here with multiple copies and events
1390
+ // TODO: add public function to facilitate this, since applications do not have direct access to the backend interface
1391
+ if (!split_backend->iface.cpy_tensor_async || !split_backend->iface.cpy_tensor_async(input_backend, split_backend, input, input_cpy)) {
1392
+ wsp_ggml_backend_synchronize(input_backend);
1393
+ if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
1394
+ wsp_ggml_backend_event_synchronize(sched->events[split_backend_id][sched->cur_copy]);
1395
+ } else {
1396
+ wsp_ggml_backend_synchronize(split_backend);
1397
+ }
1398
+ wsp_ggml_backend_tensor_copy(input, input_cpy);
1399
+ }
1400
+ }
1401
+ }
1402
+
1403
+ if (!sched->callback_eval) {
1404
+ enum wsp_ggml_status ec = wsp_ggml_backend_graph_compute_async(split_backend, &split->graph);
1405
+ if (ec != WSP_GGML_STATUS_SUCCESS) {
1406
+ return ec;
1407
+ }
1408
+ } else {
1409
+ // similar to wsp_ggml_backend_compare_graph_backend
1410
+ for (int j0 = 0; j0 < split->graph.n_nodes; j0++) {
1411
+ struct wsp_ggml_tensor * t = split->graph.nodes[j0];
1412
+
1413
+ // check if the user needs data from this node
1414
+ bool need = sched->callback_eval(t, true, sched->callback_eval_user_data);
1415
+
1416
+ int j1 = j0;
1417
+
1418
+ // determine the range [j0, j1] of nodes that can be computed together
1419
+ while (!need && j1 < split->graph.n_nodes - 1) {
1420
+ t = split->graph.nodes[++j1];
1421
+ need = sched->callback_eval(t, true, sched->callback_eval_user_data);
1422
+ }
1423
+
1424
+ struct wsp_ggml_cgraph gv = wsp_ggml_graph_view(&split->graph, j0, j1 + 1);
1425
+
1426
+ enum wsp_ggml_status ec = wsp_ggml_backend_graph_compute_async(split_backend, &gv);
1427
+ if (ec != WSP_GGML_STATUS_SUCCESS) {
1428
+ return ec;
1429
+ }
1430
+
1431
+ // TODO: pass backend to the callback, then the user can decide if they want to synchronize
1432
+ wsp_ggml_backend_synchronize(split_backend);
1433
+
1434
+ if (need && !sched->callback_eval(t, false, sched->callback_eval_user_data)) {
1435
+ break;
1436
+ }
1437
+
1438
+ j0 = j1;
1439
+ }
1440
+ }
1441
+
1442
+ // record the event of this copy
1443
+ if (split->n_inputs > 0) {
1444
+ if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
1445
+ wsp_ggml_backend_event_record(sched->events[split_backend_id][sched->cur_copy], split_backend);
1446
+ }
1447
+ }
1448
+ }
1449
+
1450
+ sched->cur_copy = (sched->cur_copy + 1) % sched->n_copies;
1451
+
1452
+ return WSP_GGML_STATUS_SUCCESS;
1453
+ }
1454
+
1455
+ wsp_ggml_backend_sched_t wsp_ggml_backend_sched_new(
1456
+ wsp_ggml_backend_t * backends,
1457
+ wsp_ggml_backend_buffer_type_t * bufts,
1458
+ int n_backends,
1459
+ size_t graph_size,
1460
+ bool parallel,
1461
+ bool op_offload) {
1462
+ WSP_GGML_ASSERT(n_backends > 0);
1463
+ WSP_GGML_ASSERT(n_backends <= WSP_GGML_SCHED_MAX_BACKENDS);
1464
+ WSP_GGML_ASSERT(wsp_ggml_backend_dev_type(wsp_ggml_backend_get_device(backends[n_backends - 1])) == WSP_GGML_BACKEND_DEVICE_TYPE_CPU);
1465
+
1466
+ struct wsp_ggml_backend_sched * sched = (wsp_ggml_backend_sched *) calloc(1, sizeof(struct wsp_ggml_backend_sched));
1467
+
1468
+ const char * WSP_GGML_SCHED_DEBUG = getenv("WSP_GGML_SCHED_DEBUG");
1469
+ sched->debug = WSP_GGML_SCHED_DEBUG ? atoi(WSP_GGML_SCHED_DEBUG) : 0;
1470
+ sched->n_backends = n_backends;
1471
+ sched->n_copies = parallel ? WSP_GGML_SCHED_MAX_COPIES : 1;
1472
+
1473
+ // initialize hash table
1474
+ // FIXME: needs to be size*2 to account for leafs (do it in graph_split instead)
1475
+ sched->hash_set = wsp_ggml_hash_set_new(graph_size);
1476
+ sched->hv_tensor_backend_ids = (int *) malloc(sched->hash_set.size * sizeof(sched->hv_tensor_backend_ids[0]));
1477
+ sched->hv_tensor_copies = (wsp_ggml_tensor **) malloc(sched->hash_set.size * sched->n_backends * sched->n_copies * sizeof(struct wsp_ggml_tensor *));
1478
+
1479
+ const size_t wsp_ggml_sched_max_splits = graph_size; // at most there is one split for each node in the graph
1480
+ const size_t nodes_size = graph_size + wsp_ggml_sched_max_splits*WSP_GGML_SCHED_MAX_SPLIT_INPUTS*2;
1481
+ sched->node_backend_ids = (int *) calloc(nodes_size, sizeof(sched->node_backend_ids[0]));
1482
+ sched->leaf_backend_ids = (int *) calloc(nodes_size, sizeof(sched->leaf_backend_ids[0]));
1483
+ sched->prev_node_backend_ids = (int *) calloc(nodes_size, sizeof(sched->prev_node_backend_ids[0]));
1484
+ sched->prev_leaf_backend_ids = (int *) calloc(nodes_size, sizeof(sched->prev_leaf_backend_ids[0]));
1485
+
1486
+ sched->context_buffer_size = wsp_ggml_sched_max_splits*WSP_GGML_SCHED_MAX_SPLIT_INPUTS*2*sizeof(struct wsp_ggml_tensor) + wsp_ggml_graph_overhead_custom(graph_size, false);
1487
+ sched->context_buffer = (char *) malloc(sched->context_buffer_size);
1488
+
1489
+ const int initial_splits_capacity = 16;
1490
+ sched->splits = (wsp_ggml_backend_sched_split *) calloc(initial_splits_capacity, sizeof(sched->splits[0]));
1491
+ sched->splits_capacity = initial_splits_capacity;
1492
+
1493
+ for (int b = 0; b < n_backends; b++) {
1494
+ sched->backends[b] = backends[b];
1495
+ sched->bufts[b] = bufts ? bufts[b] : wsp_ggml_backend_get_default_buffer_type(backends[b]);
1496
+ WSP_GGML_ASSERT(wsp_ggml_backend_supports_buft(backends[b], sched->bufts[b]));
1497
+
1498
+ if (sched->n_copies > 1) {
1499
+ for (int c = 0; c < sched->n_copies; c++) {
1500
+ sched->events[b][c] = wsp_ggml_backend_event_new(backends[b]->device);
1501
+ }
1502
+ }
1503
+ }
1504
+
1505
+ sched->galloc = wsp_ggml_gallocr_new_n(sched->bufts, n_backends);
1506
+ sched->op_offload = op_offload;
1507
+
1508
+ wsp_ggml_backend_sched_reset(sched);
1509
+
1510
+ return sched;
1511
+ }
1512
+
1513
+ void wsp_ggml_backend_sched_free(wsp_ggml_backend_sched_t sched) {
1514
+ if (sched == NULL) {
1515
+ return;
1516
+ }
1517
+ for (int b = 0; b < sched->n_backends; b++) {
1518
+ for (int c = 0; c < sched->n_copies; c++) {
1519
+ wsp_ggml_backend_event_free(sched->events[b][c]);
1520
+ }
1521
+ }
1522
+ wsp_ggml_gallocr_free(sched->galloc);
1523
+ wsp_ggml_free(sched->ctx);
1524
+ wsp_ggml_hash_set_free(&sched->hash_set);
1525
+ free(sched->splits);
1526
+ free(sched->hv_tensor_backend_ids);
1527
+ free(sched->hv_tensor_copies);
1528
+ free(sched->node_backend_ids);
1529
+ free(sched->leaf_backend_ids);
1530
+ free(sched->prev_node_backend_ids);
1531
+ free(sched->prev_leaf_backend_ids);
1532
+ free(sched->context_buffer);
1533
+ free(sched->graph.nodes);
1534
+ free(sched->graph.leafs);
1535
+ free(sched);
1536
+ }
1537
+
1538
+ void wsp_ggml_backend_sched_reset(wsp_ggml_backend_sched_t sched) {
1539
+ // reset state for the next run
1540
+ if (!sched->is_reset) {
1541
+ wsp_ggml_hash_set_reset(&sched->hash_set);
1542
+ memset(sched->hv_tensor_backend_ids, -1, sched->hash_set.size * sizeof(sched->hv_tensor_backend_ids[0]));
1543
+ memset(sched->hv_tensor_copies, 0, sched->hash_set.size * sched->n_backends * sched->n_copies * sizeof(struct wsp_ggml_tensor *));
1544
+ sched->is_reset = true;
1545
+ }
1546
+ sched->is_alloc = false;
1547
+ }
1548
+
1549
+ bool wsp_ggml_backend_sched_reserve(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * measure_graph) {
1550
+ WSP_GGML_ASSERT((int)sched->hash_set.size >= measure_graph->n_nodes + measure_graph->n_leafs);
1551
+
1552
+ wsp_ggml_backend_sched_split_graph(sched, measure_graph);
1553
+
1554
+ wsp_ggml_backend_sched_synchronize(sched);
1555
+
1556
+ if (!wsp_ggml_gallocr_reserve_n(sched->galloc, &sched->graph, sched->node_backend_ids, sched->leaf_backend_ids)) {
1557
+ return false;
1558
+ }
1559
+
1560
+ wsp_ggml_backend_sched_reset(sched);
1561
+
1562
+ return true;
1563
+ }
1564
+
1565
+ bool wsp_ggml_backend_sched_alloc_graph(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph) {
1566
+ WSP_GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes + graph->n_leafs);
1567
+
1568
+ wsp_ggml_backend_sched_split_graph(sched, graph);
1569
+
1570
+ if (!wsp_ggml_backend_sched_alloc_splits(sched)) {
1571
+ return false;
1572
+ }
1573
+
1574
+ sched->is_alloc = true;
1575
+
1576
+ return true;
1577
+ }
1578
+
1579
+ enum wsp_ggml_status wsp_ggml_backend_sched_graph_compute(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph) {
1580
+ enum wsp_ggml_status err = wsp_ggml_backend_sched_graph_compute_async(sched, graph);
1581
+ wsp_ggml_backend_sched_synchronize(sched);
1582
+ return err;
1583
+ }
1584
+
1585
+ enum wsp_ggml_status wsp_ggml_backend_sched_graph_compute_async(wsp_ggml_backend_sched_t sched, struct wsp_ggml_cgraph * graph) {
1586
+ if (!sched->is_reset && !sched->is_alloc) {
1587
+ wsp_ggml_backend_sched_reset(sched);
1588
+ }
1589
+
1590
+ if (!sched->is_alloc) {
1591
+ if (!wsp_ggml_backend_sched_alloc_graph(sched, graph)) {
1592
+ return WSP_GGML_STATUS_ALLOC_FAILED;
1593
+ }
1594
+ }
1595
+
1596
+ return wsp_ggml_backend_sched_compute_splits(sched);
1597
+ }
1598
+
1599
+ void wsp_ggml_backend_sched_synchronize(wsp_ggml_backend_sched_t sched) {
1600
+ for (int i = 0; i < sched->n_backends; i++) {
1601
+ wsp_ggml_backend_synchronize(sched->backends[i]);
1602
+ }
1603
+ if (!sched->is_alloc) {
1604
+ // if the graph is not already allocated, always use copy 0 after a synchronization
1605
+ // this ensures that during generation the same copy is used every time,
1606
+ // which avoids changes in the graph that could cause CUDA or other graphs to be disabled
1607
+ sched->cur_copy = 0;
1608
+ }
1609
+ }
1610
+
1611
+ void wsp_ggml_backend_sched_set_eval_callback(wsp_ggml_backend_sched_t sched, wsp_ggml_backend_sched_eval_callback callback, void * user_data) {
1612
+ sched->callback_eval = callback;
1613
+ sched->callback_eval_user_data = user_data;
1614
+ }
1615
+
1616
+ int wsp_ggml_backend_sched_get_n_splits(wsp_ggml_backend_sched_t sched) {
1617
+ return sched->n_splits;
1618
+ }
1619
+
1620
+ int wsp_ggml_backend_sched_get_n_copies(wsp_ggml_backend_sched_t sched) {
1621
+ return sched->n_copies;
1622
+ }
1623
+
1624
+ int wsp_ggml_backend_sched_get_n_backends(wsp_ggml_backend_sched_t sched) {
1625
+ return sched->n_backends;
1626
+ }
1627
+
1628
+ wsp_ggml_backend_t wsp_ggml_backend_sched_get_backend(wsp_ggml_backend_sched_t sched, int i) {
1629
+ WSP_GGML_ASSERT(i >= 0 && i < sched->n_backends);
1630
+ return sched->backends[i];
1631
+ }
1632
+
1633
+ size_t wsp_ggml_backend_sched_get_buffer_size(wsp_ggml_backend_sched_t sched, wsp_ggml_backend_t backend) {
1634
+ int backend_index = wsp_ggml_backend_sched_backend_id(sched, backend);
1635
+ WSP_GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
1636
+
1637
+ return wsp_ggml_gallocr_get_buffer_size(sched->galloc, backend_index);
1638
+ }
1639
+
1640
+ void wsp_ggml_backend_sched_set_tensor_backend(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * node, wsp_ggml_backend_t backend) {
1641
+ int backend_index = wsp_ggml_backend_sched_backend_id(sched, backend);
1642
+ WSP_GGML_ASSERT(backend_index >= 0 && backend_index < sched->n_backends);
1643
+ tensor_backend_id(node) = backend_index;
1644
+ SET_CAUSE(node, "usr");
1645
+ sched->is_reset = false;
1646
+ }
1647
+
1648
+ wsp_ggml_backend_t wsp_ggml_backend_sched_get_tensor_backend(wsp_ggml_backend_sched_t sched, struct wsp_ggml_tensor * node) {
1649
+ int backend_index = tensor_backend_id(node);
1650
+ if (backend_index == -1) {
1651
+ return NULL;
1652
+ }
1653
+ return sched->backends[backend_index];
1654
+ }
1655
+
1656
+ // utils
1657
+
1658
+ enum wsp_ggml_status wsp_ggml_backend_view_init(struct wsp_ggml_tensor * tensor) {
1659
+ WSP_GGML_ASSERT(tensor->buffer == NULL);
1660
+ WSP_GGML_ASSERT(tensor->view_src != NULL);
1661
+ WSP_GGML_ASSERT(tensor->view_src->buffer != NULL);
1662
+ WSP_GGML_ASSERT(tensor->view_src->data != NULL);
1663
+
1664
+ tensor->buffer = tensor->view_src->buffer;
1665
+ tensor->data = (char *)tensor->view_src->data + tensor->view_offs;
1666
+ return wsp_ggml_backend_buffer_init_tensor(tensor->buffer, tensor);
1667
+ }
1668
+
1669
+ enum wsp_ggml_status wsp_ggml_backend_tensor_alloc(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor, void * addr) {
1670
+ WSP_GGML_ASSERT(tensor->buffer == NULL);
1671
+ WSP_GGML_ASSERT(tensor->data == NULL);
1672
+ WSP_GGML_ASSERT(tensor->view_src == NULL);
1673
+ WSP_GGML_ASSERT(addr >= wsp_ggml_backend_buffer_get_base(buffer));
1674
+ WSP_GGML_ASSERT((char *)addr + wsp_ggml_backend_buffer_get_alloc_size(buffer, tensor) <=
1675
+ (char *)wsp_ggml_backend_buffer_get_base(buffer) + wsp_ggml_backend_buffer_get_size(buffer));
1676
+
1677
+ tensor->buffer = buffer;
1678
+ tensor->data = addr;
1679
+ return wsp_ggml_backend_buffer_init_tensor(buffer, tensor);
1680
+ }
1681
+
1682
+ static struct wsp_ggml_tensor * graph_copy_dup_tensor(struct wsp_ggml_hash_set hash_set, struct wsp_ggml_tensor ** node_copies,
1683
+ struct wsp_ggml_context * ctx_allocated, struct wsp_ggml_context * ctx_unallocated, struct wsp_ggml_tensor * src) {
1684
+
1685
+ WSP_GGML_ASSERT(src != NULL);
1686
+ WSP_GGML_ASSERT(src->data && "graph must be allocated");
1687
+
1688
+ size_t id = wsp_ggml_hash_insert(&hash_set, src);
1689
+ if (id == WSP_GGML_HASHSET_ALREADY_EXISTS) {
1690
+ return node_copies[wsp_ggml_hash_find(&hash_set, src)];
1691
+ }
1692
+
1693
+ struct wsp_ggml_tensor * dst = wsp_ggml_dup_tensor_layout(src->data && !src->view_src ? ctx_allocated : ctx_unallocated, src);
1694
+ if (src->view_src != NULL) {
1695
+ dst->view_src = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, src->view_src);
1696
+ dst->view_offs = src->view_offs;
1697
+ }
1698
+ dst->op = src->op;
1699
+ memcpy(dst->op_params, src->op_params, sizeof(dst->op_params));
1700
+ wsp_ggml_set_name(dst, src->name);
1701
+
1702
+ // copy src
1703
+ for (int i = 0; i < WSP_GGML_MAX_SRC; i++) {
1704
+ struct wsp_ggml_tensor * s = src->src[i];
1705
+ if (s == NULL) {
1706
+ continue;
1707
+ }
1708
+ dst->src[i] = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, s);
1709
+ }
1710
+
1711
+ node_copies[id] = dst;
1712
+ return dst;
1713
+ }
1714
+
1715
+ static void graph_copy_init_tensor(struct wsp_ggml_hash_set * hash_set, struct wsp_ggml_tensor ** node_copies, bool * node_init, struct wsp_ggml_tensor * src) {
1716
+ size_t id = wsp_ggml_hash_find(hash_set, src);
1717
+ if (node_init[id]) {
1718
+ return;
1719
+ }
1720
+ node_init[id] = true;
1721
+
1722
+ struct wsp_ggml_tensor * dst = node_copies[id];
1723
+ if (dst->view_src != NULL) {
1724
+ graph_copy_init_tensor(hash_set, node_copies, node_init, src->view_src);
1725
+ enum wsp_ggml_status status = wsp_ggml_backend_view_init(dst);
1726
+ WSP_GGML_ASSERT(status == WSP_GGML_STATUS_SUCCESS);
1727
+ }
1728
+ else {
1729
+ wsp_ggml_backend_tensor_copy(src, dst);
1730
+ }
1731
+
1732
+ // init src
1733
+ for (int i = 0; i < WSP_GGML_MAX_SRC; i++) {
1734
+ struct wsp_ggml_tensor * s = src->src[i];
1735
+ if (s == NULL) {
1736
+ continue;
1737
+ }
1738
+ graph_copy_init_tensor(hash_set, node_copies, node_init, s);
1739
+ }
1740
+ }
1741
+
1742
+ struct wsp_ggml_backend_graph_copy wsp_ggml_backend_graph_copy(wsp_ggml_backend_t backend, struct wsp_ggml_cgraph * graph) {
1743
+ struct wsp_ggml_hash_set hash_set = wsp_ggml_hash_set_new(graph->visited_hash_set.size);
1744
+ struct wsp_ggml_tensor ** node_copies = (wsp_ggml_tensor **) calloc(hash_set.size, sizeof(node_copies[0])); // NOLINT
1745
+ bool * node_init = (bool *) calloc(hash_set.size, sizeof(node_init[0]));
1746
+
1747
+ struct wsp_ggml_init_params params = {
1748
+ /* .mem_size = */ wsp_ggml_tensor_overhead()*hash_set.size + wsp_ggml_graph_overhead_custom(graph->size, false),
1749
+ /* .mem_buffer = */ NULL,
1750
+ /* .no_alloc = */ true
1751
+ };
1752
+
1753
+ struct wsp_ggml_context * ctx_allocated = wsp_ggml_init(params);
1754
+ struct wsp_ggml_context * ctx_unallocated = wsp_ggml_init(params);
1755
+
1756
+ if (ctx_allocated == NULL || ctx_unallocated == NULL) {
1757
+ WSP_GGML_LOG_ERROR("%s: failed to allocate context for graph copy\n", __func__);
1758
+ wsp_ggml_hash_set_free(&hash_set);
1759
+ free(node_copies);
1760
+ free(node_init);
1761
+ wsp_ggml_free(ctx_allocated);
1762
+ wsp_ggml_free(ctx_unallocated);
1763
+ return {
1764
+ /* .buffer = */ NULL,
1765
+ /* .ctx_allocated = */ NULL,
1766
+ /* .ctx_unallocated = */ NULL,
1767
+ /* .graph = */ NULL,
1768
+ };
1769
+ }
1770
+
1771
+ // dup nodes
1772
+ for (int i = 0; i < graph->n_nodes; i++) {
1773
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1774
+ graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, node);
1775
+ }
1776
+
1777
+ // allocate nodes
1778
+ wsp_ggml_backend_buffer_t buffer = wsp_ggml_backend_alloc_ctx_tensors(ctx_allocated, backend);
1779
+ if (buffer == NULL) {
1780
+ WSP_GGML_LOG_ERROR("%s: failed to allocate buffer for graph copy\n", __func__);
1781
+ wsp_ggml_hash_set_free(&hash_set);
1782
+ free(node_copies);
1783
+ free(node_init);
1784
+ wsp_ggml_free(ctx_allocated);
1785
+ wsp_ggml_free(ctx_unallocated);
1786
+ return {
1787
+ /* .buffer = */ NULL,
1788
+ /* .ctx_allocated = */ NULL,
1789
+ /* .ctx_unallocated = */ NULL,
1790
+ /* .graph = */ NULL,
1791
+ };
1792
+ }
1793
+
1794
+ //printf("copy buffer size: %zu MB\n", wsp_ggml_backend_buffer_get_size(buffer) / 1024 / 1024);
1795
+
1796
+ // copy data and init views
1797
+ for (int i = 0; i < graph->n_nodes; i++) {
1798
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1799
+ graph_copy_init_tensor(&hash_set, node_copies, node_init, node);
1800
+ }
1801
+
1802
+ // build graph copy
1803
+ struct wsp_ggml_cgraph * graph_copy = wsp_ggml_new_graph_custom(ctx_allocated, graph->size, false);
1804
+ for (int i = 0; i < graph->n_nodes; i++) {
1805
+ struct wsp_ggml_tensor * node = graph->nodes[i];
1806
+ struct wsp_ggml_tensor * node_copy = node_copies[wsp_ggml_hash_find(&hash_set, node)];
1807
+ graph_copy->nodes[i] = node_copy;
1808
+ }
1809
+ graph_copy->n_nodes = graph->n_nodes;
1810
+
1811
+ wsp_ggml_hash_set_free(&hash_set);
1812
+ free(node_copies);
1813
+ free(node_init);
1814
+
1815
+ return {
1816
+ /* .buffer = */ buffer,
1817
+ /* .ctx_allocated = */ ctx_allocated,
1818
+ /* .ctx_unallocated = */ ctx_unallocated,
1819
+ /* .graph = */ graph_copy,
1820
+ };
1821
+ }
1822
+
1823
+ void wsp_ggml_backend_graph_copy_free(struct wsp_ggml_backend_graph_copy copy) {
1824
+ wsp_ggml_backend_buffer_free(copy.buffer);
1825
+ wsp_ggml_free(copy.ctx_allocated);
1826
+ wsp_ggml_free(copy.ctx_unallocated);
1827
+ }
1828
+
1829
+ bool wsp_ggml_backend_compare_graph_backend(wsp_ggml_backend_t backend1, wsp_ggml_backend_t backend2, struct wsp_ggml_cgraph * graph, wsp_ggml_backend_eval_callback callback, void * user_data) {
1830
+ struct wsp_ggml_backend_graph_copy copy = wsp_ggml_backend_graph_copy(backend2, graph);
1831
+ if (copy.buffer == NULL) {
1832
+ return false;
1833
+ }
1834
+
1835
+ struct wsp_ggml_cgraph * g1 = graph;
1836
+ struct wsp_ggml_cgraph * g2 = copy.graph;
1837
+
1838
+ assert(g1->n_nodes == g2->n_nodes);
1839
+
1840
+ for (int i = 0; i < g1->n_nodes; i++) {
1841
+ struct wsp_ggml_tensor * t1 = g1->nodes[i];
1842
+ struct wsp_ggml_tensor * t2 = g2->nodes[i];
1843
+
1844
+ assert(t1->op == t2->op && wsp_ggml_are_same_layout(t1, t2));
1845
+
1846
+ struct wsp_ggml_cgraph g1v = wsp_ggml_graph_view(g1, i, i + 1);
1847
+ struct wsp_ggml_cgraph g2v = wsp_ggml_graph_view(g2, i, i + 1);
1848
+
1849
+ wsp_ggml_backend_graph_compute(backend1, &g1v);
1850
+ wsp_ggml_backend_graph_compute(backend2, &g2v);
1851
+
1852
+ if (wsp_ggml_is_view_op(t1->op)) {
1853
+ continue;
1854
+ }
1855
+
1856
+ // compare results, calculate rms etc
1857
+ if (!callback(i, t1, t2, user_data)) {
1858
+ break;
1859
+ }
1860
+ }
1861
+
1862
+ wsp_ggml_backend_graph_copy_free(copy);
1863
+
1864
+ return true;
1865
+ }
1866
+
1867
+ // CPU backend - buffer
1868
+
1869
+ static void * wsp_ggml_backend_cpu_buffer_get_base(wsp_ggml_backend_buffer_t buffer) {
1870
+ uintptr_t data = (uintptr_t)buffer->context;
1871
+
1872
+ // align the buffer
1873
+ if (data % TENSOR_ALIGNMENT != 0) {
1874
+ data = WSP_GGML_PAD(data, TENSOR_ALIGNMENT);
1875
+ }
1876
+
1877
+ return (void *)data;
1878
+ }
1879
+
1880
+ static void wsp_ggml_backend_cpu_buffer_free_buffer(wsp_ggml_backend_buffer_t buffer) {
1881
+ wsp_ggml_aligned_free(buffer->context, buffer->size);
1882
+ }
1883
+
1884
+ static void wsp_ggml_backend_cpu_buffer_memset_tensor(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
1885
+ memset((char *)tensor->data + offset, value, size);
1886
+
1887
+ WSP_GGML_UNUSED(buffer);
1888
+ }
1889
+
1890
+ static void wsp_ggml_backend_cpu_buffer_set_tensor(wsp_ggml_backend_buffer_t buffer, struct wsp_ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
1891
+ memcpy((char *)tensor->data + offset, data, size);
1892
+
1893
+ WSP_GGML_UNUSED(buffer);
1894
+ }
1895
+
1896
+ static void wsp_ggml_backend_cpu_buffer_get_tensor(wsp_ggml_backend_buffer_t buffer, const struct wsp_ggml_tensor * tensor, void * data, size_t offset, size_t size) {
1897
+ memcpy(data, (const char *)tensor->data + offset, size);
1898
+
1899
+ WSP_GGML_UNUSED(buffer);
1900
+ }
1901
+
1902
+ static bool wsp_ggml_backend_cpu_buffer_cpy_tensor(wsp_ggml_backend_buffer_t buffer, const struct wsp_ggml_tensor * src, struct wsp_ggml_tensor * dst) {
1903
+ if (wsp_ggml_backend_buffer_is_host(src->buffer)) {
1904
+ memcpy(dst->data, src->data, wsp_ggml_nbytes(src));
1905
+ return true;
1906
+ }
1907
+ return false;
1908
+
1909
+ WSP_GGML_UNUSED(buffer);
1910
+ }
1911
+
1912
+ static void wsp_ggml_backend_cpu_buffer_clear(wsp_ggml_backend_buffer_t buffer, uint8_t value) {
1913
+ memset(buffer->context, value, buffer->size);
1914
+ }
1915
+
1916
+ static const struct wsp_ggml_backend_buffer_i wsp_ggml_backend_cpu_buffer_i = {
1917
+ /* .free_buffer = */ wsp_ggml_backend_cpu_buffer_free_buffer,
1918
+ /* .get_base = */ wsp_ggml_backend_cpu_buffer_get_base,
1919
+ /* .init_tensor = */ NULL, // no initialization required
1920
+ /* .memset_tensor = */ wsp_ggml_backend_cpu_buffer_memset_tensor,
1921
+ /* .set_tensor = */ wsp_ggml_backend_cpu_buffer_set_tensor,
1922
+ /* .get_tensor = */ wsp_ggml_backend_cpu_buffer_get_tensor,
1923
+ /* .cpy_tensor = */ wsp_ggml_backend_cpu_buffer_cpy_tensor,
1924
+ /* .clear = */ wsp_ggml_backend_cpu_buffer_clear,
1925
+ /* .reset = */ NULL,
1926
+ };
1927
+
1928
+ static const struct wsp_ggml_backend_buffer_i wsp_ggml_backend_cpu_buffer_from_ptr_i = {
1929
+ /* .free_buffer = */ NULL, // ptr is not owned by the buffer, so it does not need to be freed
1930
+ /* .get_base = */ wsp_ggml_backend_cpu_buffer_get_base,
1931
+ /* .init_tensor = */ NULL, // no initialization required
1932
+ /* .memset_tensor = */ wsp_ggml_backend_cpu_buffer_memset_tensor,
1933
+ /* .set_tensor = */ wsp_ggml_backend_cpu_buffer_set_tensor,
1934
+ /* .get_tensor = */ wsp_ggml_backend_cpu_buffer_get_tensor,
1935
+ /* .cpy_tensor = */ wsp_ggml_backend_cpu_buffer_cpy_tensor,
1936
+ /* .clear = */ wsp_ggml_backend_cpu_buffer_clear,
1937
+ /* .reset = */ NULL,
1938
+ };
1939
+
1940
+ // CPU backend buffer type
1941
+
1942
+ // this buffer type is defined here to make it available to all backends
1943
+
1944
+ static const char * wsp_ggml_backend_cpu_buffer_type_get_name(wsp_ggml_backend_buffer_type_t buft) {
1945
+ return "CPU";
1946
+
1947
+ WSP_GGML_UNUSED(buft);
1948
+ }
1949
+
1950
+ static wsp_ggml_backend_buffer_t wsp_ggml_backend_cpu_buffer_type_alloc_buffer(wsp_ggml_backend_buffer_type_t buft, size_t size) {
1951
+ void * data = wsp_ggml_aligned_malloc(size);
1952
+
1953
+ if (data == NULL) {
1954
+ WSP_GGML_LOG_ERROR("%s: failed to allocate buffer of size %zu\n", __func__, size);
1955
+ return NULL;
1956
+ }
1957
+
1958
+ return wsp_ggml_backend_buffer_init(buft, wsp_ggml_backend_cpu_buffer_i, data, size);
1959
+ }
1960
+
1961
+ static size_t wsp_ggml_backend_cpu_buffer_type_get_alignment(wsp_ggml_backend_buffer_type_t buft) {
1962
+ return TENSOR_ALIGNMENT;
1963
+
1964
+ WSP_GGML_UNUSED(buft);
1965
+ }
1966
+
1967
+ static bool wsp_ggml_backend_cpu_buffer_type_is_host(wsp_ggml_backend_buffer_type_t buft) {
1968
+ return true;
1969
+
1970
+ WSP_GGML_UNUSED(buft);
1971
+ }
1972
+
1973
+ wsp_ggml_backend_buffer_type_t wsp_ggml_backend_cpu_buffer_type(void) {
1974
+ static struct wsp_ggml_backend_buffer_type wsp_ggml_backend_cpu_buffer_type = {
1975
+ /* .iface = */ {
1976
+ /* .get_name = */ wsp_ggml_backend_cpu_buffer_type_get_name,
1977
+ /* .alloc_buffer = */ wsp_ggml_backend_cpu_buffer_type_alloc_buffer,
1978
+ /* .get_alignment = */ wsp_ggml_backend_cpu_buffer_type_get_alignment,
1979
+ /* .get_max_size = */ NULL, // defaults to SIZE_MAX
1980
+ /* .get_alloc_size = */ NULL, // defaults to wsp_ggml_nbytes
1981
+ /* .is_host = */ wsp_ggml_backend_cpu_buffer_type_is_host,
1982
+ },
1983
+ /* .device = */ NULL, // FIXME wsp_ggml_backend_reg_dev_get(wsp_ggml_backend_cpu_reg(), 0),
1984
+ /* .context = */ NULL,
1985
+ };
1986
+
1987
+ return &wsp_ggml_backend_cpu_buffer_type;
1988
+ }
1989
+
1990
+ static const char * wsp_ggml_backend_cpu_buffer_from_ptr_type_get_name(wsp_ggml_backend_buffer_type_t buft) {
1991
+ return "CPU_Mapped";
1992
+
1993
+ WSP_GGML_UNUSED(buft);
1994
+ }
1995
+
1996
+ static wsp_ggml_backend_buffer_type_t wsp_ggml_backend_cpu_buffer_from_ptr_type(void) {
1997
+ static struct wsp_ggml_backend_buffer_type wsp_ggml_backend_cpu_buffer_type = {
1998
+ /* .iface = */ {
1999
+ /* .get_name = */ wsp_ggml_backend_cpu_buffer_from_ptr_type_get_name,
2000
+ /* .alloc_buffer = */ wsp_ggml_backend_cpu_buffer_type_alloc_buffer,
2001
+ /* .get_alignment = */ wsp_ggml_backend_cpu_buffer_type_get_alignment,
2002
+ /* .get_max_size = */ NULL, // defaults to SIZE_MAX
2003
+ /* .get_alloc_size = */ NULL, // defaults to wsp_ggml_nbytes
2004
+ /* .is_host = */ wsp_ggml_backend_cpu_buffer_type_is_host,
2005
+ },
2006
+ /* .device = */ NULL, // FIXME wsp_ggml_backend_reg_dev_get(wsp_ggml_backend_cpu_reg(), 0),
2007
+ /* .context = */ NULL,
2008
+ };
2009
+
2010
+ return &wsp_ggml_backend_cpu_buffer_type;
2011
+ }
2012
+
2013
+ wsp_ggml_backend_buffer_t wsp_ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size) {
2014
+ WSP_GGML_ASSERT((uintptr_t)ptr % TENSOR_ALIGNMENT == 0 && "buffer pointer must be aligned");
2015
+ return wsp_ggml_backend_buffer_init(wsp_ggml_backend_cpu_buffer_from_ptr_type(), wsp_ggml_backend_cpu_buffer_from_ptr_i, ptr, size);
2016
+ }