cui-llama.rn 1.3.0 → 1.3.3

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