llama-cpp-capacitor 0.0.6 → 0.0.8

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 (150) hide show
  1. package/android/src/main/CMakeLists.txt +9 -9
  2. package/cpp/LICENSE +21 -0
  3. package/cpp/README.md +4 -0
  4. package/cpp/anyascii.c +22223 -0
  5. package/cpp/anyascii.h +42 -0
  6. package/cpp/chat-parser.cpp +393 -0
  7. package/cpp/chat-parser.h +120 -0
  8. package/cpp/chat.cpp +2315 -0
  9. package/cpp/chat.h +221 -0
  10. package/cpp/common.cpp +1619 -0
  11. package/cpp/common.h +744 -0
  12. package/cpp/ggml-alloc.c +1028 -0
  13. package/cpp/ggml-alloc.h +76 -0
  14. package/cpp/ggml-backend-impl.h +255 -0
  15. package/cpp/ggml-backend-reg.cpp +600 -0
  16. package/cpp/ggml-backend.cpp +2118 -0
  17. package/cpp/ggml-backend.h +354 -0
  18. package/cpp/ggml-common.h +1878 -0
  19. package/cpp/ggml-cpp.h +39 -0
  20. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  21. package/cpp/ggml-cpu/amx/amx.h +8 -0
  22. package/cpp/ggml-cpu/amx/common.h +91 -0
  23. package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
  24. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  25. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  26. package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
  27. package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
  28. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  29. package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
  30. package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
  31. package/cpp/ggml-cpu/arch-fallback.h +215 -0
  32. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  33. package/cpp/ggml-cpu/binary-ops.h +16 -0
  34. package/cpp/ggml-cpu/common.h +73 -0
  35. package/cpp/ggml-cpu/ggml-cpu-impl.h +525 -0
  36. package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
  37. package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
  38. package/cpp/ggml-cpu/ops.cpp +10587 -0
  39. package/cpp/ggml-cpu/ops.h +114 -0
  40. package/cpp/ggml-cpu/quants.c +1193 -0
  41. package/cpp/ggml-cpu/quants.h +97 -0
  42. package/cpp/ggml-cpu/repack.cpp +1982 -0
  43. package/cpp/ggml-cpu/repack.h +120 -0
  44. package/cpp/ggml-cpu/simd-mappings.h +1184 -0
  45. package/cpp/ggml-cpu/traits.cpp +36 -0
  46. package/cpp/ggml-cpu/traits.h +38 -0
  47. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  48. package/cpp/ggml-cpu/unary-ops.h +28 -0
  49. package/cpp/ggml-cpu/vec.cpp +348 -0
  50. package/cpp/ggml-cpu/vec.h +1121 -0
  51. package/cpp/ggml-cpu.h +145 -0
  52. package/cpp/ggml-impl.h +622 -0
  53. package/cpp/ggml-metal-impl.h +688 -0
  54. package/cpp/ggml-metal.h +66 -0
  55. package/cpp/ggml-metal.m +6833 -0
  56. package/cpp/ggml-opt.cpp +1093 -0
  57. package/cpp/ggml-opt.h +256 -0
  58. package/cpp/ggml-quants.c +5324 -0
  59. package/cpp/ggml-quants.h +106 -0
  60. package/cpp/ggml-threading.cpp +12 -0
  61. package/cpp/ggml-threading.h +14 -0
  62. package/cpp/ggml.c +7108 -0
  63. package/cpp/ggml.h +2492 -0
  64. package/cpp/gguf.cpp +1358 -0
  65. package/cpp/gguf.h +202 -0
  66. package/cpp/json-partial.cpp +256 -0
  67. package/cpp/json-partial.h +38 -0
  68. package/cpp/json-schema-to-grammar.cpp +985 -0
  69. package/cpp/json-schema-to-grammar.h +21 -0
  70. package/cpp/llama-adapter.cpp +388 -0
  71. package/cpp/llama-adapter.h +76 -0
  72. package/cpp/llama-arch.cpp +2355 -0
  73. package/cpp/llama-arch.h +499 -0
  74. package/cpp/llama-batch.cpp +875 -0
  75. package/cpp/llama-batch.h +160 -0
  76. package/cpp/llama-chat.cpp +783 -0
  77. package/cpp/llama-chat.h +65 -0
  78. package/cpp/llama-context.cpp +2748 -0
  79. package/cpp/llama-context.h +306 -0
  80. package/cpp/llama-cparams.cpp +5 -0
  81. package/cpp/llama-cparams.h +41 -0
  82. package/cpp/llama-cpp.h +30 -0
  83. package/cpp/llama-grammar.cpp +1229 -0
  84. package/cpp/llama-grammar.h +173 -0
  85. package/cpp/llama-graph.cpp +1891 -0
  86. package/cpp/llama-graph.h +810 -0
  87. package/cpp/llama-hparams.cpp +180 -0
  88. package/cpp/llama-hparams.h +233 -0
  89. package/cpp/llama-impl.cpp +167 -0
  90. package/cpp/llama-impl.h +61 -0
  91. package/cpp/llama-io.cpp +15 -0
  92. package/cpp/llama-io.h +35 -0
  93. package/cpp/llama-kv-cache-iswa.cpp +318 -0
  94. package/cpp/llama-kv-cache-iswa.h +135 -0
  95. package/cpp/llama-kv-cache.cpp +2059 -0
  96. package/cpp/llama-kv-cache.h +374 -0
  97. package/cpp/llama-kv-cells.h +491 -0
  98. package/cpp/llama-memory-hybrid.cpp +258 -0
  99. package/cpp/llama-memory-hybrid.h +137 -0
  100. package/cpp/llama-memory-recurrent.cpp +1146 -0
  101. package/cpp/llama-memory-recurrent.h +179 -0
  102. package/cpp/llama-memory.cpp +59 -0
  103. package/cpp/llama-memory.h +119 -0
  104. package/cpp/llama-mmap.cpp +600 -0
  105. package/cpp/llama-mmap.h +68 -0
  106. package/cpp/llama-model-loader.cpp +1164 -0
  107. package/cpp/llama-model-loader.h +170 -0
  108. package/cpp/llama-model-saver.cpp +282 -0
  109. package/cpp/llama-model-saver.h +37 -0
  110. package/cpp/llama-model.cpp +19042 -0
  111. package/cpp/llama-model.h +491 -0
  112. package/cpp/llama-sampling.cpp +2575 -0
  113. package/cpp/llama-sampling.h +32 -0
  114. package/cpp/llama-vocab.cpp +3792 -0
  115. package/cpp/llama-vocab.h +176 -0
  116. package/cpp/llama.cpp +358 -0
  117. package/cpp/llama.h +1373 -0
  118. package/cpp/log.cpp +427 -0
  119. package/cpp/log.h +103 -0
  120. package/cpp/minja/chat-template.hpp +550 -0
  121. package/cpp/minja/minja.hpp +3009 -0
  122. package/cpp/nlohmann/json.hpp +25526 -0
  123. package/cpp/nlohmann/json_fwd.hpp +187 -0
  124. package/cpp/regex-partial.cpp +204 -0
  125. package/cpp/regex-partial.h +56 -0
  126. package/cpp/rn-completion.cpp +681 -0
  127. package/cpp/rn-completion.h +116 -0
  128. package/cpp/rn-llama.cpp +345 -0
  129. package/cpp/rn-llama.h +149 -0
  130. package/cpp/rn-mtmd.hpp +602 -0
  131. package/cpp/rn-tts.cpp +591 -0
  132. package/cpp/rn-tts.h +59 -0
  133. package/cpp/sampling.cpp +579 -0
  134. package/cpp/sampling.h +107 -0
  135. package/cpp/tools/mtmd/clip-impl.h +473 -0
  136. package/cpp/tools/mtmd/clip.cpp +4322 -0
  137. package/cpp/tools/mtmd/clip.h +106 -0
  138. package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
  139. package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
  140. package/cpp/tools/mtmd/mtmd-audio.h +47 -0
  141. package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
  142. package/cpp/tools/mtmd/mtmd-helper.h +91 -0
  143. package/cpp/tools/mtmd/mtmd.cpp +1066 -0
  144. package/cpp/tools/mtmd/mtmd.h +298 -0
  145. package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
  146. package/cpp/unicode-data.cpp +7034 -0
  147. package/cpp/unicode-data.h +20 -0
  148. package/cpp/unicode.cpp +1061 -0
  149. package/cpp/unicode.h +68 -0
  150. package/package.json +2 -1
@@ -0,0 +1,1028 @@
1
+ #include "ggml-alloc.h"
2
+ #include "ggml-backend-impl.h"
3
+ #include "ggml.h"
4
+ #include "ggml-impl.h"
5
+ #include <assert.h>
6
+ #include <limits.h>
7
+ #include <stdarg.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
13
+ #define MAX_FREE_BLOCKS 256
14
+
15
+ //#define LM_GGML_ALLOCATOR_DEBUG
16
+
17
+ //#define AT_PRINTF(...) LM_GGML_LOG_DEBUG(__VA_ARGS__)
18
+ #define AT_PRINTF(...)
19
+
20
+
21
+ static bool lm_ggml_is_view(const struct lm_ggml_tensor * t) {
22
+ return t->view_src != NULL;
23
+ }
24
+
25
+ // ops that return true for this function must not use restrict pointers for their backend implementations
26
+ static bool lm_ggml_op_can_inplace(enum lm_ggml_op op) {
27
+ switch (op) {
28
+ case LM_GGML_OP_SCALE:
29
+ case LM_GGML_OP_DIAG_MASK_ZERO:
30
+ case LM_GGML_OP_DIAG_MASK_INF:
31
+ case LM_GGML_OP_ADD:
32
+ case LM_GGML_OP_ADD_ID:
33
+ case LM_GGML_OP_ADD1:
34
+ case LM_GGML_OP_SUB:
35
+ case LM_GGML_OP_MUL:
36
+ case LM_GGML_OP_DIV:
37
+ case LM_GGML_OP_SQR:
38
+ case LM_GGML_OP_SQRT:
39
+ case LM_GGML_OP_LOG:
40
+ case LM_GGML_OP_UNARY:
41
+ case LM_GGML_OP_ROPE:
42
+ case LM_GGML_OP_ROPE_BACK:
43
+ case LM_GGML_OP_SILU_BACK:
44
+ case LM_GGML_OP_RMS_NORM:
45
+ case LM_GGML_OP_RMS_NORM_BACK:
46
+ case LM_GGML_OP_SOFT_MAX:
47
+ case LM_GGML_OP_SOFT_MAX_BACK:
48
+ return true;
49
+
50
+ default:
51
+ return false;
52
+ }
53
+ }
54
+
55
+ static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
56
+ assert(alignment && !(alignment & (alignment - 1))); // power of 2
57
+ size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
58
+ return offset + align;
59
+ }
60
+
61
+ // tallocr
62
+
63
+ struct lm_ggml_tallocr lm_ggml_tallocr_new(lm_ggml_backend_buffer_t buffer) {
64
+ void * base = lm_ggml_backend_buffer_get_base(buffer);
65
+ size_t align = lm_ggml_backend_buffer_get_alignment(buffer);
66
+
67
+ assert(align && !(align & (align - 1))); // power of 2
68
+
69
+ struct lm_ggml_tallocr talloc = (struct lm_ggml_tallocr) {
70
+ /*.buffer = */ buffer,
71
+ /*.base = */ base,
72
+ /*.alignment = */ align,
73
+ /*.offset = */ aligned_offset(base, 0, align),
74
+ };
75
+ return talloc;
76
+ }
77
+
78
+ enum lm_ggml_status lm_ggml_tallocr_alloc(struct lm_ggml_tallocr * talloc, struct lm_ggml_tensor * tensor) {
79
+ size_t size = lm_ggml_backend_buffer_get_alloc_size(talloc->buffer, tensor);
80
+ size = LM_GGML_PAD(size, talloc->alignment);
81
+
82
+ if (talloc->offset + size > lm_ggml_backend_buffer_get_size(talloc->buffer)) {
83
+ LM_GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n",
84
+ __func__, tensor->name, size, lm_ggml_backend_buffer_get_size(talloc->buffer) - talloc->offset);
85
+ LM_GGML_ABORT("not enough space in the buffer");
86
+ }
87
+
88
+ void * addr = (char *)lm_ggml_backend_buffer_get_base(talloc->buffer) + talloc->offset;
89
+ talloc->offset += size;
90
+
91
+ assert(((uintptr_t)addr % talloc->alignment) == 0);
92
+
93
+ return lm_ggml_backend_tensor_alloc(talloc->buffer, tensor, addr);
94
+ }
95
+
96
+ // dynamic tensor allocator
97
+
98
+ struct free_block {
99
+ size_t offset;
100
+ size_t size;
101
+ };
102
+
103
+ struct lm_ggml_dyn_tallocr {
104
+ size_t alignment;
105
+ int n_free_blocks;
106
+ struct free_block free_blocks[MAX_FREE_BLOCKS];
107
+ size_t max_size;
108
+
109
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
110
+ struct {
111
+ const struct lm_ggml_tensor * tensor;
112
+ size_t offset;
113
+ } allocated_tensors[1024];
114
+ #endif
115
+ };
116
+
117
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
118
+ static void add_allocated_tensor(struct lm_ggml_dyn_tallocr * alloc, size_t offset, const struct lm_ggml_tensor * tensor) {
119
+ for (int i = 0; i < 1024; i++) {
120
+ if (alloc->allocated_tensors[i].tensor == NULL) {
121
+ alloc->allocated_tensors[i].tensor = tensor;
122
+ alloc->allocated_tensors[i].offset = offset;
123
+ return;
124
+ }
125
+ }
126
+ LM_GGML_ABORT("out of allocated_tensors");
127
+ }
128
+ static void remove_allocated_tensor(struct lm_ggml_dyn_tallocr * alloc, size_t offset, const struct lm_ggml_tensor * tensor) {
129
+ for (int i = 0; i < 1024; i++) {
130
+ if (alloc->allocated_tensors[i].offset == offset) {
131
+ alloc->allocated_tensors[i].tensor = NULL;
132
+ return;
133
+ }
134
+ }
135
+ LM_GGML_ABORT("tried to free tensor %s not found\n", tensor->name);
136
+ }
137
+ #endif
138
+
139
+ static size_t lm_ggml_dyn_tallocr_alloc(struct lm_ggml_dyn_tallocr * alloc, size_t size, const struct lm_ggml_tensor * tensor) {
140
+ size = aligned_offset(NULL, size, alloc->alignment);
141
+
142
+ AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
143
+
144
+ size_t max_avail = 0;
145
+
146
+ // find the best fitting free block besides the last block
147
+ int best_fit_block = -1;
148
+ size_t best_fit_size = SIZE_MAX;
149
+ for (int i = 0; i < alloc->n_free_blocks - 1; i++) {
150
+ struct free_block * block = &alloc->free_blocks[i];
151
+ max_avail = MAX(max_avail, block->size);
152
+ if (block->size >= size && block->size <= best_fit_size) {
153
+ best_fit_block = i;
154
+ best_fit_size = block->size;
155
+ }
156
+ }
157
+
158
+ if (best_fit_block == -1) {
159
+ // the last block is our last resort
160
+ struct free_block * block = &alloc->free_blocks[alloc->n_free_blocks - 1];
161
+ max_avail = MAX(max_avail, block->size);
162
+ if (block->size >= size) {
163
+ best_fit_block = alloc->n_free_blocks - 1;
164
+ } else {
165
+ // this should never happen
166
+ LM_GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n",
167
+ __func__, size, max_avail);
168
+ LM_GGML_ABORT("not enough space in the buffer");
169
+ }
170
+ }
171
+
172
+ struct free_block * block = &alloc->free_blocks[best_fit_block];
173
+ size_t offset = block->offset;
174
+ block->offset = offset + size;
175
+ block->size -= size;
176
+ if (block->size == 0) {
177
+ // remove block if empty
178
+ alloc->n_free_blocks--;
179
+ for (int j = best_fit_block; j < alloc->n_free_blocks; j++) {
180
+ alloc->free_blocks[j] = alloc->free_blocks[j+1];
181
+ }
182
+ }
183
+
184
+ AT_PRINTF("block %d, offset %zu\n", best_fit_block, offset);
185
+
186
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
187
+ add_allocated_tensor(alloc, offset, tensor);
188
+ size_t cur_max = offset + size;
189
+ if (cur_max > alloc->max_size) {
190
+ // sort allocated_tensors by offset
191
+ for (int i = 0; i < 1024; i++) {
192
+ for (int j = i + 1; j < 1024; j++) {
193
+ if (alloc->allocated_tensors[i].offset > alloc->allocated_tensors[j].offset) {
194
+ const struct lm_ggml_tensor * tmp_tensor = alloc->allocated_tensors[i].tensor;
195
+ size_t tmp_offset = alloc->allocated_tensors[i].offset;
196
+ alloc->allocated_tensors[i].tensor = alloc->allocated_tensors[j].tensor;
197
+ alloc->allocated_tensors[i].offset = alloc->allocated_tensors[j].offset;
198
+ alloc->allocated_tensors[j].tensor = tmp_tensor;
199
+ alloc->allocated_tensors[j].offset = tmp_offset;
200
+ }
201
+ }
202
+ }
203
+ LM_GGML_LOG_DEBUG("max_size = %.2f MB: tensors: ", cur_max / 1024.0 / 1024.0);
204
+ for (int i = 0; i < 1024; i++) {
205
+ if (alloc->allocated_tensors[i].tensor) {
206
+ LM_GGML_LOG_DEBUG("%s [%zx-%zx] (%.2f MB) ", alloc->allocated_tensors[i].tensor->name,
207
+ alloc->allocated_tensors[i].offset,
208
+ alloc->allocated_tensors[i].offset + lm_ggml_nbytes(alloc->allocated_tensors[i].tensor),
209
+ lm_ggml_nbytes(alloc->allocated_tensors[i].tensor) / 1024.0 / 1024.0);
210
+ }
211
+ }
212
+ LM_GGML_LOG_DEBUG("\n");
213
+ }
214
+ #endif
215
+
216
+ alloc->max_size = MAX(alloc->max_size, offset + size);
217
+
218
+ return offset;
219
+
220
+ LM_GGML_UNUSED(tensor);
221
+ }
222
+
223
+ // this is a very naive implementation, but for our case the number of free blocks should be very small
224
+ static void lm_ggml_dyn_tallocr_free_tensor(struct lm_ggml_dyn_tallocr * alloc, size_t offset, size_t size, const struct lm_ggml_tensor * tensor) {
225
+ size = aligned_offset(NULL, size, alloc->alignment);
226
+
227
+ AT_PRINTF("%s: freeing %s at %zu (%zu bytes) - n_free_blocks = %d\n", __func__, tensor->name, offset, size, alloc->n_free_blocks);
228
+
229
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
230
+ remove_allocated_tensor(alloc, offset, tensor);
231
+ #endif
232
+
233
+ // see if we can merge with an existing block
234
+ for (int i = 0; i < alloc->n_free_blocks; i++) {
235
+ struct free_block * block = &alloc->free_blocks[i];
236
+ // check if ptr is at the end of the block
237
+ if (block->offset + block->size == offset) {
238
+ block->size += size;
239
+ // check if we can merge with the next block
240
+ if (i < alloc->n_free_blocks - 1 && block->offset + block->size == alloc->free_blocks[i+1].offset) {
241
+ block->size += alloc->free_blocks[i+1].size;
242
+ alloc->n_free_blocks--;
243
+ for (int j = i+1; j < alloc->n_free_blocks; j++) {
244
+ alloc->free_blocks[j] = alloc->free_blocks[j+1];
245
+ }
246
+ }
247
+ return;
248
+ }
249
+ // check if ptr is at the beginning of the block
250
+ if (offset + size == block->offset) {
251
+ block->offset = offset;
252
+ block->size += size;
253
+ // check if we can merge with the previous block
254
+ if (i > 0 && alloc->free_blocks[i-1].offset + alloc->free_blocks[i-1].size == block->offset) {
255
+ alloc->free_blocks[i-1].size += block->size;
256
+ alloc->n_free_blocks--;
257
+ for (int j = i; j < alloc->n_free_blocks; j++) {
258
+ alloc->free_blocks[j] = alloc->free_blocks[j+1];
259
+ }
260
+ }
261
+ return;
262
+ }
263
+ }
264
+ // otherwise, add a new block
265
+ LM_GGML_ASSERT(alloc->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
266
+ // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
267
+ int insert_pos = 0;
268
+ while (insert_pos < alloc->n_free_blocks && alloc->free_blocks[insert_pos].offset < offset) {
269
+ insert_pos++;
270
+ }
271
+ // shift all blocks from insert_pos onward to make room for the new block
272
+ for (int i = alloc->n_free_blocks; i > insert_pos; i--) {
273
+ alloc->free_blocks[i] = alloc->free_blocks[i-1];
274
+ }
275
+ // insert the new block
276
+ alloc->free_blocks[insert_pos].offset = offset;
277
+ alloc->free_blocks[insert_pos].size = size;
278
+ alloc->n_free_blocks++;
279
+
280
+ LM_GGML_UNUSED(tensor);
281
+ }
282
+
283
+ static void lm_ggml_dyn_tallocr_reset(struct lm_ggml_dyn_tallocr * alloc) {
284
+ alloc->n_free_blocks = 1;
285
+ alloc->free_blocks[0].offset = 0;
286
+ alloc->free_blocks[0].size = SIZE_MAX/2; // restrict maximum size of a measure allocator to half size_t max to avoid overflows
287
+ alloc->max_size = 0;
288
+
289
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
290
+ for (int i = 0; i < 1024; i++) {
291
+ alloc->allocated_tensors[i].tensor = NULL;
292
+ }
293
+ #endif
294
+ }
295
+
296
+ static struct lm_ggml_dyn_tallocr * lm_ggml_dyn_tallocr_new(size_t alignment) {
297
+ struct lm_ggml_dyn_tallocr * alloc = (struct lm_ggml_dyn_tallocr *)malloc(sizeof(struct lm_ggml_dyn_tallocr));
298
+
299
+ *alloc = (struct lm_ggml_dyn_tallocr) {
300
+ /*.alignment = */ alignment,
301
+ /*.n_free_blocks = */ 0,
302
+ /*.free_blocks = */ {{0}},
303
+ /*.max_size = */ 0,
304
+ #ifdef LM_GGML_ALLOCATOR_DEBUG
305
+ /*.allocated_tensors = */ {{0}},
306
+ #endif
307
+ };
308
+
309
+ lm_ggml_dyn_tallocr_reset(alloc);
310
+
311
+ return alloc;
312
+ }
313
+
314
+ static void lm_ggml_dyn_tallocr_free(struct lm_ggml_dyn_tallocr * alloc) {
315
+ free(alloc);
316
+ }
317
+
318
+ static size_t lm_ggml_dyn_tallocr_max_size(struct lm_ggml_dyn_tallocr * alloc) {
319
+ return alloc->max_size;
320
+ }
321
+
322
+
323
+ /////////////////////////////////////
324
+
325
+ // graph allocator
326
+
327
+ struct hash_node {
328
+ int n_children;
329
+ int n_views;
330
+ int buffer_id;
331
+ size_t offset; // offset within the buffer
332
+ bool allocated;
333
+ };
334
+
335
+ struct tensor_alloc {
336
+ int buffer_id;
337
+ size_t offset;
338
+ size_t size_max; // 0 = pre-allocated, unused, or view
339
+ };
340
+
341
+ struct leaf_alloc {
342
+ struct tensor_alloc leaf;
343
+ };
344
+
345
+ struct node_alloc {
346
+ struct tensor_alloc dst;
347
+ struct tensor_alloc src[LM_GGML_MAX_SRC];
348
+ };
349
+
350
+ struct lm_ggml_gallocr {
351
+ lm_ggml_backend_buffer_type_t * bufts; // [n_buffers]
352
+ lm_ggml_backend_buffer_t * buffers; // [n_buffers]
353
+ struct lm_ggml_dyn_tallocr ** buf_tallocs; // [n_buffers]
354
+ int n_buffers;
355
+
356
+ struct lm_ggml_hash_set hash_set;
357
+ struct hash_node * hash_values; // [hash_set.size]
358
+
359
+ struct node_alloc * node_allocs; // [n_nodes]
360
+ int n_nodes;
361
+
362
+ struct leaf_alloc * leaf_allocs; // [n_leafs]
363
+ int n_leafs;
364
+ };
365
+
366
+ lm_ggml_gallocr_t lm_ggml_gallocr_new_n(lm_ggml_backend_buffer_type_t * bufts, int n_bufs) {
367
+ lm_ggml_gallocr_t galloc = (lm_ggml_gallocr_t)calloc(1, sizeof(struct lm_ggml_gallocr));
368
+ LM_GGML_ASSERT(galloc != NULL);
369
+
370
+ galloc->bufts = calloc(n_bufs, sizeof(lm_ggml_backend_buffer_type_t));
371
+ LM_GGML_ASSERT(galloc->bufts != NULL);
372
+
373
+ galloc->buffers = calloc(n_bufs, sizeof(lm_ggml_backend_buffer_t));
374
+ LM_GGML_ASSERT(galloc->buffers != NULL);
375
+
376
+ galloc->buf_tallocs = calloc(n_bufs, sizeof(struct lm_ggml_dyn_tallocr *));
377
+ LM_GGML_ASSERT(galloc->buf_tallocs != NULL);
378
+
379
+ for (int i = 0; i < n_bufs; i++) {
380
+ galloc->bufts[i] = bufts[i];
381
+ galloc->buffers[i] = NULL;
382
+
383
+ // check if the same buffer type is used multiple times and reuse the same allocator
384
+ for (int j = 0; j < i; j++) {
385
+ if (bufts[i] == bufts[j]) {
386
+ galloc->buf_tallocs[i] = galloc->buf_tallocs[j];
387
+ break;
388
+ }
389
+ }
390
+
391
+ if (galloc->buf_tallocs[i] == NULL) {
392
+ size_t alignment = lm_ggml_backend_buft_get_alignment(bufts[i]);
393
+ galloc->buf_tallocs[i] = lm_ggml_dyn_tallocr_new(alignment);
394
+ }
395
+ }
396
+ galloc->n_buffers = n_bufs;
397
+
398
+ return galloc;
399
+ }
400
+
401
+ lm_ggml_gallocr_t lm_ggml_gallocr_new(lm_ggml_backend_buffer_type_t buft) {
402
+ return lm_ggml_gallocr_new_n(&buft, 1);
403
+ }
404
+
405
+ void lm_ggml_gallocr_free(lm_ggml_gallocr_t galloc) {
406
+ if (galloc == NULL) {
407
+ return;
408
+ }
409
+
410
+ for (int i = 0; i < galloc->n_buffers; i++) {
411
+ if (galloc->buffers != NULL) {
412
+ // skip if already freed
413
+ bool freed = false;
414
+ for (int j = 0; j < i; j++) {
415
+ if (galloc->buffers[j] == galloc->buffers[i]) {
416
+ freed = true;
417
+ break;
418
+ }
419
+ }
420
+ if (!freed) {
421
+ lm_ggml_backend_buffer_free(galloc->buffers[i]);
422
+ }
423
+ }
424
+ if (galloc->buf_tallocs != NULL) {
425
+ // skip if already freed
426
+ bool freed = false;
427
+ for (int j = 0; j < i; j++) {
428
+ if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
429
+ freed = true;
430
+ break;
431
+ }
432
+ }
433
+ if (!freed) {
434
+ lm_ggml_dyn_tallocr_free(galloc->buf_tallocs[i]);
435
+ }
436
+ }
437
+ }
438
+
439
+ lm_ggml_hash_set_free(&galloc->hash_set);
440
+ free(galloc->hash_values);
441
+ free(galloc->bufts);
442
+ free(galloc->buffers);
443
+ free(galloc->buf_tallocs);
444
+ free(galloc->node_allocs);
445
+ free(galloc->leaf_allocs);
446
+ free(galloc);
447
+ }
448
+
449
+ typedef struct lm_ggml_gallocr * lm_ggml_gallocr_t;
450
+
451
+ static struct hash_node * lm_ggml_gallocr_hash_get(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * t) {
452
+ size_t i = lm_ggml_hash_find_or_insert(&galloc->hash_set, t);
453
+ return &galloc->hash_values[i];
454
+ }
455
+
456
+ static bool lm_ggml_gallocr_is_own(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * t) {
457
+ return lm_ggml_gallocr_hash_get(galloc, t)->allocated;
458
+ }
459
+
460
+ static bool lm_ggml_gallocr_is_allocated(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * t) {
461
+ return t->data != NULL || lm_ggml_gallocr_hash_get(galloc, t)->allocated;
462
+ }
463
+
464
+ static void lm_ggml_gallocr_allocate_node(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * node, int buffer_id) {
465
+ LM_GGML_ASSERT(buffer_id >= 0);
466
+ struct hash_node * hn = lm_ggml_gallocr_hash_get(galloc, node);
467
+
468
+ if (!lm_ggml_gallocr_is_allocated(galloc, node) && !lm_ggml_is_view(node)) {
469
+ hn->allocated = true;
470
+ assert(hn->offset == 0);
471
+
472
+ // try to reuse a parent's buffer (inplace)
473
+ if (lm_ggml_op_can_inplace(node->op)) {
474
+ for (int i = 0; i < LM_GGML_MAX_SRC; i++) {
475
+ struct lm_ggml_tensor * parent = node->src[i];
476
+ if (parent == NULL) {
477
+ continue;
478
+ }
479
+
480
+ // if the node's data is external, then we cannot re-use it
481
+ if (!lm_ggml_gallocr_is_own(galloc, parent)) {
482
+ AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
483
+ continue;
484
+ }
485
+
486
+ // outputs cannot be reused
487
+ if (parent->flags & LM_GGML_TENSOR_FLAG_OUTPUT || (parent->view_src != NULL && parent->view_src->flags & LM_GGML_TENSOR_FLAG_OUTPUT)) {
488
+ AT_PRINTF("not reusing parent %s for %s as it is an output\n", parent->name, node->name);
489
+ continue;
490
+ }
491
+
492
+ if (!lm_ggml_are_same_layout(node, parent)) {
493
+ AT_PRINTF("not reusing parent %s for %s as layouts are different\n", parent->name, node->name);
494
+ continue;
495
+ }
496
+
497
+ struct hash_node * p_hn = lm_ggml_gallocr_hash_get(galloc, parent);
498
+ if (p_hn->n_children == 1 && p_hn->n_views == 0) {
499
+ if (lm_ggml_is_view(parent)) {
500
+ struct lm_ggml_tensor * view_src = parent->view_src;
501
+ struct hash_node * view_src_hn = lm_ggml_gallocr_hash_get(galloc, view_src);
502
+ if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
503
+ AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
504
+ assert(view_src_hn->offset == p_hn->offset);
505
+ hn->buffer_id = p_hn->buffer_id;
506
+ hn->offset = p_hn->offset;
507
+ p_hn->allocated = false; // avoid freeing the parent
508
+ view_src_hn->allocated = false;
509
+ return;
510
+ }
511
+ } else {
512
+ AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
513
+ hn->buffer_id = p_hn->buffer_id;
514
+ hn->offset = p_hn->offset;
515
+ p_hn->allocated = false; // avoid freeing the parent
516
+ return;
517
+ }
518
+ }
519
+ }
520
+ }
521
+ // allocate tensor from the buffer
522
+ struct lm_ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
523
+ lm_ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
524
+ size_t size = lm_ggml_backend_buft_get_alloc_size(buft, node);
525
+ size_t offset = lm_ggml_dyn_tallocr_alloc(alloc, size, node);
526
+ hn->buffer_id = buffer_id;
527
+ hn->offset = offset;
528
+ }
529
+ }
530
+
531
+ static void lm_ggml_gallocr_free_node(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * node) {
532
+ // graph outputs are never freed
533
+ if (node->flags & LM_GGML_TENSOR_FLAG_OUTPUT) {
534
+ AT_PRINTF("not freeing output %s\n", node->name);
535
+ return;
536
+ }
537
+
538
+ struct hash_node * hn = lm_ggml_gallocr_hash_get(galloc, node);
539
+ size_t offset = hn->offset;
540
+ int buffer_id = hn->buffer_id;
541
+ struct lm_ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
542
+ lm_ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
543
+ size_t size = lm_ggml_backend_buft_get_alloc_size(buft, node);
544
+ lm_ggml_dyn_tallocr_free_tensor(alloc, offset, size, node);
545
+ hn->allocated = false;
546
+ }
547
+
548
+ static int get_node_buffer_id(const int * node_buffer_ids, int i) {
549
+ return node_buffer_ids ? node_buffer_ids[i] : 0;
550
+ }
551
+
552
+ static void lm_ggml_gallocr_alloc_graph_impl(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
553
+ // clear hash tables
554
+ lm_ggml_hash_set_reset(&galloc->hash_set);
555
+ memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size);
556
+
557
+ // allocate leafs
558
+ // these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes
559
+ for (int i = 0; i < graph->n_leafs; i++) {
560
+ struct lm_ggml_tensor * leaf = graph->leafs[i];
561
+ lm_ggml_gallocr_allocate_node(galloc, leaf, get_node_buffer_id(leaf_buffer_ids, i));
562
+ }
563
+
564
+ // count number of children and views
565
+ // allocate other graph inputs and leafs first to avoid overwriting them
566
+ for (int i = 0; i < graph->n_nodes; i++) {
567
+ struct lm_ggml_tensor * node = graph->nodes[i];
568
+
569
+ // TODO: better way to add external dependencies
570
+ // LM_GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to
571
+ // control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node
572
+ // itself is never used and should not be considered a dependency
573
+ if (lm_ggml_is_view(node) && node->op != LM_GGML_OP_NONE) {
574
+ struct lm_ggml_tensor * view_src = node->view_src;
575
+ lm_ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
576
+ }
577
+
578
+ if (node->flags & LM_GGML_TENSOR_FLAG_INPUT) {
579
+ lm_ggml_gallocr_allocate_node(galloc, graph->nodes[i], get_node_buffer_id(node_buffer_ids, i));
580
+ }
581
+
582
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
583
+ struct lm_ggml_tensor * src = node->src[j];
584
+ if (src == NULL) {
585
+ continue;
586
+ }
587
+
588
+ lm_ggml_gallocr_hash_get(galloc, src)->n_children += 1;
589
+
590
+ // allocate explicit inputs
591
+ if (src->flags & LM_GGML_TENSOR_FLAG_INPUT) {
592
+ lm_ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
593
+ }
594
+ }
595
+ }
596
+
597
+ // allocate tensors
598
+ for (int i = 0; i < graph->n_nodes; i++) {
599
+ struct lm_ggml_tensor * node = graph->nodes[i];
600
+ int buffer_id = get_node_buffer_id(node_buffer_ids, i);
601
+
602
+ // allocate parents (only leafs need to be allocated at this point)
603
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
604
+ struct lm_ggml_tensor * parent = node->src[j];
605
+ if (parent == NULL) {
606
+ continue;
607
+ }
608
+ lm_ggml_gallocr_allocate_node(galloc, parent, buffer_id);
609
+ }
610
+
611
+ // allocate node
612
+ lm_ggml_gallocr_allocate_node(galloc, node, buffer_id);
613
+
614
+ AT_PRINTF("exec: %s (%s) <= ", lm_ggml_op_desc(node), node->name);
615
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
616
+ struct lm_ggml_tensor * parent = node->src[j];
617
+ if (parent == NULL) {
618
+ continue;
619
+ }
620
+ AT_PRINTF("%s", parent->name);
621
+ if (j < LM_GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
622
+ AT_PRINTF(", ");
623
+ }
624
+ }
625
+ AT_PRINTF("\n");
626
+
627
+ // update parents
628
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
629
+ struct lm_ggml_tensor * parent = node->src[j];
630
+ if (parent == NULL) {
631
+ continue;
632
+ }
633
+ struct hash_node * p_hn = lm_ggml_gallocr_hash_get(galloc, parent);
634
+ p_hn->n_children -= 1;
635
+
636
+ AT_PRINTF("parent %s: %d children, %d views, allocated: %d\n",
637
+ parent->name, p_hn->n_children, p_hn->n_views, p_hn->allocated);
638
+
639
+ if (p_hn->n_children == 0 && p_hn->n_views == 0) {
640
+ if (lm_ggml_is_view(parent)) {
641
+ struct lm_ggml_tensor * view_src = parent->view_src;
642
+ struct hash_node * view_src_hn = lm_ggml_gallocr_hash_get(galloc, view_src);
643
+ view_src_hn->n_views -= 1;
644
+ AT_PRINTF("view_src %s: %d children, %d views\n",
645
+ view_src->name, view_src_hn->n_children, view_src_hn->n_views);
646
+ if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src_hn->allocated) {
647
+ lm_ggml_gallocr_free_node(galloc, view_src);
648
+ }
649
+ }
650
+ else if (p_hn->allocated) {
651
+ lm_ggml_gallocr_free_node(galloc, parent);
652
+ }
653
+ }
654
+ AT_PRINTF("\n");
655
+ }
656
+ }
657
+ }
658
+
659
+ bool lm_ggml_gallocr_reserve_n(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
660
+ size_t min_hash_size = graph->n_nodes + graph->n_leafs;
661
+ // add 25% margin to avoid hash collisions
662
+ min_hash_size += min_hash_size / 4;
663
+
664
+ // initialize hash table
665
+ if (galloc->hash_set.size < min_hash_size) {
666
+ lm_ggml_hash_set_free(&galloc->hash_set);
667
+ galloc->hash_set = lm_ggml_hash_set_new(min_hash_size);
668
+ LM_GGML_ASSERT(galloc->hash_set.keys != NULL);
669
+
670
+ free(galloc->hash_values);
671
+ galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size);
672
+ LM_GGML_ASSERT(galloc->hash_values != NULL);
673
+ }
674
+
675
+ // reset allocators
676
+ for (int i = 0; i < galloc->n_buffers; i++) {
677
+ lm_ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]);
678
+ }
679
+
680
+ // allocate in hash table
681
+ lm_ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids);
682
+
683
+ // set the node_allocs from the hash table
684
+ if (galloc->n_nodes < graph->n_nodes) {
685
+ free(galloc->node_allocs);
686
+ galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc));
687
+ LM_GGML_ASSERT(galloc->node_allocs != NULL);
688
+ }
689
+ galloc->n_nodes = graph->n_nodes;
690
+ for (int i = 0; i < graph->n_nodes; i++) {
691
+ struct lm_ggml_tensor * node = graph->nodes[i];
692
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
693
+ if (node->view_src || node->data) {
694
+ node_alloc->dst.buffer_id = -1;
695
+ node_alloc->dst.offset = SIZE_MAX;
696
+ node_alloc->dst.size_max = 0;
697
+ } else {
698
+ struct hash_node * hn = lm_ggml_gallocr_hash_get(galloc, node);
699
+ node_alloc->dst.buffer_id = hn->buffer_id;
700
+ node_alloc->dst.offset = hn->offset;
701
+ node_alloc->dst.size_max = lm_ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
702
+ }
703
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
704
+ struct lm_ggml_tensor * src = node->src[j];
705
+ if (!src || src->view_src || src->data) {
706
+ node_alloc->src[j].buffer_id = -1;
707
+ node_alloc->src[j].offset = SIZE_MAX;
708
+ node_alloc->src[j].size_max = 0;
709
+ } else {
710
+ struct hash_node * hn = lm_ggml_gallocr_hash_get(galloc, src);
711
+ node_alloc->src[j].buffer_id = hn->buffer_id;
712
+ node_alloc->src[j].offset = hn->offset;
713
+ node_alloc->src[j].size_max = lm_ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src);
714
+ }
715
+ }
716
+ }
717
+ if (galloc->n_leafs < graph->n_leafs) {
718
+ free(galloc->leaf_allocs);
719
+ galloc->leaf_allocs = calloc(graph->n_leafs, sizeof(galloc->leaf_allocs[0]));
720
+ LM_GGML_ASSERT(galloc->leaf_allocs != NULL);
721
+ }
722
+ galloc->n_leafs = graph->n_leafs;
723
+ for (int i = 0; i < graph->n_leafs; i++) {
724
+ struct lm_ggml_tensor * leaf = graph->leafs[i];
725
+ struct hash_node * hn = lm_ggml_gallocr_hash_get(galloc, leaf);
726
+ if (leaf->view_src || leaf->data) {
727
+ galloc->leaf_allocs[i].leaf.buffer_id = -1;
728
+ galloc->leaf_allocs[i].leaf.offset = SIZE_MAX;
729
+ galloc->leaf_allocs[i].leaf.size_max = 0;
730
+ } else {
731
+ galloc->leaf_allocs[i].leaf.buffer_id = hn->buffer_id;
732
+ galloc->leaf_allocs[i].leaf.offset = hn->offset;
733
+ galloc->leaf_allocs[i].leaf.size_max = lm_ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], leaf);
734
+ }
735
+ }
736
+
737
+ // reallocate buffers if needed
738
+ for (int i = 0; i < galloc->n_buffers; i++) {
739
+ // if the buffer type is used multiple times, we reuse the same buffer
740
+ for (int j = 0; j < i; j++) {
741
+ if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
742
+ galloc->buffers[i] = galloc->buffers[j];
743
+ break;
744
+ }
745
+ }
746
+
747
+ size_t cur_size = galloc->buffers[i] ? lm_ggml_backend_buffer_get_size(galloc->buffers[i]) : 0;
748
+ size_t new_size = lm_ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i]);
749
+
750
+ // even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views
751
+ if (new_size > cur_size || galloc->buffers[i] == NULL) {
752
+ #ifndef NDEBUG
753
+ LM_GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n", __func__, lm_ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
754
+ #endif
755
+
756
+ lm_ggml_backend_buffer_free(galloc->buffers[i]);
757
+ galloc->buffers[i] = lm_ggml_backend_buft_alloc_buffer(galloc->bufts[i], new_size);
758
+ if (galloc->buffers[i] == NULL) {
759
+ LM_GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, lm_ggml_backend_buft_name(galloc->bufts[i]), new_size);
760
+ return false;
761
+ }
762
+ lm_ggml_backend_buffer_set_usage(galloc->buffers[i], LM_GGML_BACKEND_BUFFER_USAGE_COMPUTE);
763
+ }
764
+ }
765
+
766
+ return true;
767
+ }
768
+
769
+ bool lm_ggml_gallocr_reserve(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph *graph) {
770
+ return lm_ggml_gallocr_reserve_n(galloc, graph, NULL, NULL);
771
+ }
772
+
773
+ static void lm_ggml_gallocr_init_tensor(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) {
774
+ int buffer_id = tensor_alloc->buffer_id;
775
+ assert(tensor->data || tensor->view_src || lm_ggml_backend_buffer_get_alloc_size(galloc->buffers[buffer_id], tensor) <= tensor_alloc->size_max);
776
+
777
+ if (tensor->view_src != NULL) {
778
+ if (tensor->buffer == NULL) {
779
+ assert(tensor_alloc->offset == SIZE_MAX);
780
+ if (tensor->view_src->buffer == NULL) {
781
+ // this tensor was allocated without ggml-backend
782
+ return;
783
+ }
784
+ lm_ggml_backend_view_init(tensor);
785
+ }
786
+ } else {
787
+ if (tensor->data == NULL) {
788
+ assert(tensor_alloc->offset != SIZE_MAX);
789
+ assert(lm_ggml_backend_buffer_get_alloc_size(galloc->buffers[buffer_id], tensor) <= tensor_alloc->size_max);
790
+ void * base = lm_ggml_backend_buffer_get_base(galloc->buffers[buffer_id]);
791
+ void * addr = (char *)base + tensor_alloc->offset;
792
+ lm_ggml_backend_tensor_alloc(galloc->buffers[buffer_id], tensor, addr);
793
+ } else {
794
+ if (tensor->buffer == NULL) {
795
+ // this tensor was allocated without ggml-backend
796
+ return;
797
+ }
798
+ }
799
+ }
800
+ }
801
+
802
+ static bool lm_ggml_gallocr_node_needs_realloc(lm_ggml_gallocr_t galloc, struct lm_ggml_tensor * node, struct tensor_alloc * talloc) {
803
+ size_t node_size = 0;
804
+ if (!node->data && !node->view_src) {
805
+ // If we previously had data but don't now then reallocate
806
+ if (talloc->buffer_id < 0) {
807
+ return false;
808
+ }
809
+ node_size = lm_ggml_backend_buft_get_alloc_size(galloc->bufts[talloc->buffer_id], node);
810
+ }
811
+ return talloc->size_max >= node_size;
812
+ }
813
+
814
+ static bool lm_ggml_gallocr_needs_realloc(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph) {
815
+ if (galloc->n_nodes != graph->n_nodes) {
816
+ #ifndef NDEBUG
817
+ LM_GGML_LOG_DEBUG("%s: graph has different number of nodes\n", __func__);
818
+ #endif
819
+ return true;
820
+ }
821
+
822
+ if (galloc->n_leafs != graph->n_leafs) {
823
+ #ifndef NDEBUG
824
+ LM_GGML_LOG_DEBUG("%s: graph has different number of leafs\n", __func__);
825
+ #endif
826
+ return true;
827
+ }
828
+
829
+ for (int i = 0; i < graph->n_nodes; i++) {
830
+ struct lm_ggml_tensor * node = graph->nodes[i];
831
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
832
+
833
+ if (!lm_ggml_gallocr_node_needs_realloc(galloc, node, &node_alloc->dst)) {
834
+ #ifndef NDEBUG
835
+ LM_GGML_LOG_DEBUG("%s: node %s is not valid\n", __func__, node->name);
836
+ #endif
837
+ return true;
838
+ }
839
+
840
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
841
+ struct lm_ggml_tensor * src = node->src[j];
842
+ if (src == NULL) {
843
+ continue;
844
+ }
845
+ if (!lm_ggml_gallocr_node_needs_realloc(galloc, src, &node_alloc->src[j])) {
846
+ #ifndef NDEBUG
847
+ LM_GGML_LOG_DEBUG("%s: src %d (%s) of node %s is not valid\n", __func__, j, src->name, node->name);
848
+ #endif
849
+ return true;
850
+ }
851
+ }
852
+ }
853
+
854
+ return false;
855
+ }
856
+
857
+ bool lm_ggml_gallocr_alloc_graph(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph) {
858
+ if (lm_ggml_gallocr_needs_realloc(galloc, graph)) {
859
+ if (galloc->n_buffers == 1) {
860
+ #ifndef NDEBUG
861
+ LM_GGML_LOG_DEBUG("%s: reallocating buffers automatically\n", __func__);
862
+ #endif
863
+ if (!lm_ggml_gallocr_reserve(galloc, graph)) {
864
+ return false;
865
+ }
866
+ } else {
867
+ #ifndef NDEBUG
868
+ LM_GGML_LOG_DEBUG("%s: cannot reallocate multi buffer graph automatically, call reserve\n", __func__);
869
+ #endif
870
+ return false;
871
+ }
872
+ }
873
+
874
+ // reset buffers
875
+ for (int i = 0; i < galloc->n_buffers; i++) {
876
+ if (galloc->buffers[i] != NULL) {
877
+ lm_ggml_backend_buffer_reset(galloc->buffers[i]);
878
+ }
879
+ }
880
+
881
+ // allocate the graph tensors from the previous assignments
882
+ // leafs
883
+ for (int i = 0; i < graph->n_leafs; i++) {
884
+ struct lm_ggml_tensor * leaf = graph->leafs[i];
885
+ struct leaf_alloc * leaf_alloc = &galloc->leaf_allocs[i];
886
+ lm_ggml_gallocr_init_tensor(galloc, leaf, &leaf_alloc->leaf);
887
+ }
888
+ // nodes
889
+ for (int i = 0; i < graph->n_nodes; i++) {
890
+ struct lm_ggml_tensor * node = graph->nodes[i];
891
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
892
+ for (int j = 0; j < LM_GGML_MAX_SRC; j++) {
893
+ struct lm_ggml_tensor * src = node->src[j];
894
+ if (src == NULL) {
895
+ continue;
896
+ }
897
+ lm_ggml_gallocr_init_tensor(galloc, src, &node_alloc->src[j]);
898
+ }
899
+ lm_ggml_gallocr_init_tensor(galloc, node, &node_alloc->dst);
900
+ }
901
+
902
+ return true;
903
+ }
904
+
905
+ size_t lm_ggml_gallocr_get_buffer_size(lm_ggml_gallocr_t galloc, int buffer_id) {
906
+ LM_GGML_ASSERT(buffer_id >= 0 && buffer_id < galloc->n_buffers);
907
+
908
+ if (galloc->buffers[buffer_id] == NULL) {
909
+ return 0;
910
+ }
911
+
912
+ for (int i = 0; i < buffer_id; i++) {
913
+ if (galloc->buffers[i] == galloc->buffers[buffer_id]) {
914
+ // this buffer is the same as a previous one due to the same buffer type being used multiple times
915
+ // only return the buffer size the first time it appears to avoid double counting
916
+ return 0;
917
+ }
918
+ }
919
+
920
+ return lm_ggml_backend_buffer_get_size(galloc->buffers[buffer_id]);
921
+ }
922
+
923
+ // utils
924
+
925
+ static void free_buffers(lm_ggml_backend_buffer_t ** buffers, const size_t * n_buffers) {
926
+ for (size_t i = 0; i < *n_buffers; i++) {
927
+ lm_ggml_backend_buffer_free((*buffers)[i]);
928
+ }
929
+ free(*buffers);
930
+ }
931
+
932
+ static bool alloc_tensor_range(struct lm_ggml_context * ctx,
933
+ struct lm_ggml_tensor * first, struct lm_ggml_tensor * last,
934
+ lm_ggml_backend_buffer_type_t buft, size_t size,
935
+ lm_ggml_backend_buffer_t ** buffers, size_t * n_buffers) {
936
+
937
+ lm_ggml_backend_buffer_t buffer = lm_ggml_backend_buft_alloc_buffer(buft, size);
938
+ if (buffer == NULL) {
939
+ LM_GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, lm_ggml_backend_buft_name(buft), size);
940
+ free_buffers(buffers, n_buffers);
941
+ return false;
942
+ }
943
+
944
+ *buffers = realloc(*buffers, sizeof(lm_ggml_backend_buffer_t) * (*n_buffers + 1));
945
+ (*buffers)[(*n_buffers)++] = buffer;
946
+
947
+ struct lm_ggml_tallocr tallocr = lm_ggml_tallocr_new(buffer);
948
+
949
+ for (struct lm_ggml_tensor * t = first; t != last; t = lm_ggml_get_next_tensor(ctx, t)) {
950
+ enum lm_ggml_status status = LM_GGML_STATUS_SUCCESS;
951
+ if (t->data == NULL) {
952
+ if (t->view_src == NULL) {
953
+ status = lm_ggml_tallocr_alloc(&tallocr, t);
954
+ } else if (t->buffer == NULL) {
955
+ status = lm_ggml_backend_view_init(t);
956
+ }
957
+ } else {
958
+ if (t->view_src != NULL && t->buffer == NULL) {
959
+ // view of a pre-allocated tensor
960
+ status = lm_ggml_backend_view_init(t);
961
+ }
962
+ }
963
+ if (status != LM_GGML_STATUS_SUCCESS) {
964
+ LM_GGML_LOG_ERROR("%s: failed to initialize tensor %s\n", __func__, t->name);
965
+ free_buffers(buffers, n_buffers);
966
+ return false;
967
+ }
968
+ }
969
+
970
+ return true;
971
+ }
972
+
973
+ lm_ggml_backend_buffer_t lm_ggml_backend_alloc_ctx_tensors_from_buft(struct lm_ggml_context * ctx, lm_ggml_backend_buffer_type_t buft) {
974
+ LM_GGML_ASSERT(lm_ggml_get_no_alloc(ctx) == true);
975
+
976
+ size_t alignment = lm_ggml_backend_buft_get_alignment(buft);
977
+ size_t max_size = lm_ggml_backend_buft_get_max_size(buft);
978
+
979
+ lm_ggml_backend_buffer_t * buffers = NULL;
980
+ size_t n_buffers = 0;
981
+
982
+ size_t cur_buf_size = 0;
983
+ struct lm_ggml_tensor * first = lm_ggml_get_first_tensor(ctx);
984
+ for (struct lm_ggml_tensor * t = first; t != NULL; t = lm_ggml_get_next_tensor(ctx, t)) {
985
+ size_t this_size = 0;
986
+ if (t->data == NULL && t->view_src == NULL) {
987
+ this_size = LM_GGML_PAD(lm_ggml_backend_buft_get_alloc_size(buft, t), alignment);
988
+ }
989
+
990
+ if (cur_buf_size > 0 && (cur_buf_size + this_size) > max_size) {
991
+ // allocate tensors in the current buffer
992
+ if (!alloc_tensor_range(ctx, first, t, buft, cur_buf_size, &buffers, &n_buffers)) {
993
+ return NULL;
994
+ }
995
+ first = t;
996
+ cur_buf_size = this_size;
997
+ } else {
998
+ cur_buf_size += this_size;
999
+ }
1000
+ }
1001
+
1002
+ // allocate remaining tensors
1003
+ if (cur_buf_size > 0) {
1004
+ if (!alloc_tensor_range(ctx, first, NULL, buft, cur_buf_size, &buffers, &n_buffers)) {
1005
+ return NULL;
1006
+ }
1007
+ }
1008
+
1009
+ if (n_buffers == 0) {
1010
+ #ifndef NDEBUG
1011
+ LM_GGML_LOG_DEBUG("%s: all tensors in the context are already allocated\n", __func__);
1012
+ #endif
1013
+ return NULL;
1014
+ }
1015
+
1016
+ lm_ggml_backend_buffer_t buffer;
1017
+ if (n_buffers == 1) {
1018
+ buffer = buffers[0];
1019
+ } else {
1020
+ buffer = lm_ggml_backend_multi_buffer_alloc_buffer(buffers, n_buffers);
1021
+ }
1022
+ free(buffers);
1023
+ return buffer;
1024
+ }
1025
+
1026
+ lm_ggml_backend_buffer_t lm_ggml_backend_alloc_ctx_tensors(struct lm_ggml_context * ctx, lm_ggml_backend_t backend) {
1027
+ return lm_ggml_backend_alloc_ctx_tensors_from_buft(ctx, lm_ggml_backend_get_default_buffer_type(backend));
1028
+ }