whisper.rn 0.4.0-rc.1 → 0.4.0-rc.10

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