Coverage Report

Created: 2025-12-14 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/ggml/src/ggml-alloc.c
Line
Count
Source
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
0
#define MAX(a, b) ((a) > (b) ? (a) : (b))
13
#define MAX_FREE_BLOCKS 256
14
15
//#define GGML_ALLOCATOR_DEBUG
16
17
//#define AT_PRINTF(...) GGML_LOG_DEBUG(__VA_ARGS__)
18
#define AT_PRINTF(...)
19
20
21
0
static bool ggml_is_view(const struct ggml_tensor * t) {
22
0
    return t->view_src != NULL;
23
0
}
24
25
// ops that return true for this function must not use restrict pointers for their backend implementations
26
0
bool ggml_op_can_inplace(enum ggml_op op) {
27
0
    switch (op) {
28
0
        case GGML_OP_FILL:
29
0
        case GGML_OP_SCALE:
30
0
        case GGML_OP_DIAG_MASK_ZERO:
31
0
        case GGML_OP_DIAG_MASK_INF:
32
0
        case GGML_OP_ADD:
33
0
        case GGML_OP_ADD_ID:
34
0
        case GGML_OP_ADD1:
35
0
        case GGML_OP_SUB:
36
0
        case GGML_OP_MUL:
37
0
        case GGML_OP_DIV:
38
0
        case GGML_OP_SQR:
39
0
        case GGML_OP_SQRT:
40
0
        case GGML_OP_LOG:
41
0
        case GGML_OP_UNARY:
42
0
        case GGML_OP_ROPE:
43
0
        case GGML_OP_ROPE_BACK:
44
0
        case GGML_OP_SILU_BACK:
45
0
        case GGML_OP_RMS_NORM:
46
0
        case GGML_OP_RMS_NORM_BACK:
47
0
        case GGML_OP_SOFT_MAX:
48
0
        case GGML_OP_SOFT_MAX_BACK:
49
0
            return true;
50
51
0
        default:
52
0
            return false;
53
0
    }
54
0
}
55
56
0
static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
57
0
    assert(alignment && !(alignment & (alignment - 1))); // power of 2
58
0
    size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
59
0
    return offset + align;
60
0
}
61
62
// tallocr
63
64
0
struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer) {
65
0
    void * base = ggml_backend_buffer_get_base(buffer);
66
0
    size_t align = ggml_backend_buffer_get_alignment(buffer);
67
68
0
    assert(align && !(align & (align - 1))); // power of 2
69
70
0
    struct ggml_tallocr talloc = (struct ggml_tallocr) {
71
0
        /*.buffer    = */ buffer,
72
0
        /*.base      = */ base,
73
0
        /*.alignment = */ align,
74
0
        /*.offset    = */ aligned_offset(base, 0, align),
75
0
    };
76
0
    return talloc;
77
0
}
78
79
0
enum ggml_status ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor) {
80
0
    size_t size = ggml_backend_buffer_get_alloc_size(talloc->buffer, tensor);
81
0
    size = GGML_PAD(size, talloc->alignment);
82
83
0
    if (talloc->offset + size > ggml_backend_buffer_get_size(talloc->buffer)) {
84
0
        GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n",
85
0
                __func__, tensor->name, size, ggml_backend_buffer_get_size(talloc->buffer) - talloc->offset);
86
0
        GGML_ABORT("not enough space in the buffer");
87
0
    }
88
89
0
    void * addr = (char *)ggml_backend_buffer_get_base(talloc->buffer) + talloc->offset;
90
0
    talloc->offset += size;
91
92
0
    assert(((uintptr_t)addr % talloc->alignment) == 0);
93
94
0
    return ggml_backend_tensor_alloc(talloc->buffer, tensor, addr);
95
0
}
96
97
// dynamic tensor allocator
98
99
0
#define GGML_VBUFFER_MAX_CHUNKS 16
100
101
// relative memory address within an allocation that can be split into multiple buffers (chunks)
102
struct buffer_address {
103
    int chunk;     // index of a backend buffer
104
    size_t offset; // local memory offset within the buffer
105
};
106
107
static const struct buffer_address GGML_BUFFER_ADDRESS_INVALID = { -1, SIZE_MAX };
108
109
0
static bool ggml_buffer_address_less(struct buffer_address a, struct buffer_address b) {
110
0
    return a.chunk != b.chunk ? a.chunk < b.chunk : a.offset < b.offset;
111
0
}
112
113
struct free_block {
114
    size_t offset;
115
    size_t size;
116
};
117
118
struct tallocr_chunk {
119
    struct free_block free_blocks[MAX_FREE_BLOCKS];
120
    int n_free_blocks;
121
    size_t max_size;
122
};
123
124
struct ggml_dyn_tallocr {
125
    size_t alignment;
126
    size_t max_chunk_size;
127
    struct tallocr_chunk * chunks[GGML_VBUFFER_MAX_CHUNKS];
128
    int n_chunks;
129
130
#ifdef GGML_ALLOCATOR_DEBUG
131
    struct {
132
        const struct ggml_tensor * tensor;
133
        struct buffer_address addr;
134
    } allocated_tensors[1024];
135
#endif
136
};
137
138
0
static void ggml_dyn_tallocr_insert_block(struct tallocr_chunk * chunk, size_t offset, size_t size) {
139
0
    GGML_ASSERT(chunk->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
140
    // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
141
0
    int insert_pos = 0;
142
0
    while (insert_pos < chunk->n_free_blocks && chunk->free_blocks[insert_pos].offset < offset) {
143
0
        insert_pos++;
144
0
    }
145
    // shift all blocks from insert_pos onward to make room for the new block
146
0
    for (int i = chunk->n_free_blocks; i > insert_pos; i--) {
147
0
        chunk->free_blocks[i] = chunk->free_blocks[i-1];
148
0
    }
149
    // insert the new block
150
0
    chunk->free_blocks[insert_pos].offset = offset;
151
0
    chunk->free_blocks[insert_pos].size = size;
152
0
    chunk->n_free_blocks++;
153
0
}
154
155
0
static void ggml_dyn_tallocr_remove_block(struct tallocr_chunk * chunk, int idx) {
156
    // shift all elements after idx by 1 to the left, overwriting the element at idx
157
0
    for (int i = idx; i < chunk->n_free_blocks; i++) {
158
0
        chunk->free_blocks[i] = chunk->free_blocks[i+1];
159
0
    }
160
0
    chunk->n_free_blocks--;
161
0
}
162
163
0
static int ggml_dyn_tallocr_new_chunk(struct ggml_dyn_tallocr * alloc, size_t min_size) {
164
0
    if (alloc->n_chunks >= GGML_VBUFFER_MAX_CHUNKS) {
165
0
        return -1;
166
0
    }
167
0
    struct tallocr_chunk * chunk = calloc(1, sizeof(struct tallocr_chunk));
168
0
    chunk->n_free_blocks = 1;
169
0
    chunk->free_blocks[0].offset = 0;
170
    // available space in a chunk is limited to max_chunk_size, but can be higher if:
171
    // 1. a single tensor exceeds the maximum, and cannot fit any other way
172
    // 2. we are running out of chunks
173
    // backends will either manage to allocate the larger size, or report an error.
174
0
    chunk->free_blocks[0].size = MAX(min_size, alloc->max_chunk_size);
175
0
    if (alloc->n_chunks == GGML_VBUFFER_MAX_CHUNKS - 1) {
176
0
        chunk->free_blocks[0].size = SIZE_MAX/2;
177
0
    }
178
0
    alloc->chunks[alloc->n_chunks] = chunk;
179
0
    alloc->n_chunks++;
180
0
    return alloc->n_chunks - 1;
181
0
}
182
183
#ifdef GGML_ALLOCATOR_DEBUG
184
static void add_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) {
185
    for (int i = 0; i < 1024; i++) {
186
        if (alloc->allocated_tensors[i].tensor == NULL) {
187
            alloc->allocated_tensors[i].tensor = tensor;
188
            alloc->allocated_tensors[i].addr = addr;
189
            return;
190
        }
191
    }
192
    GGML_ABORT("out of allocated_tensors");
193
}
194
static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) {
195
    for (int i = 0; i < 1024; i++) {
196
        if (alloc->allocated_tensors[i].addr.chunk == addr.chunk && alloc->allocated_tensors[i].addr.offset == addr.offset) {
197
            alloc->allocated_tensors[i].tensor = NULL;
198
            return;
199
        }
200
    }
201
    GGML_ABORT("tried to free tensor %s not found\n", tensor->name);
202
}
203
#endif
204
205
0
static struct buffer_address ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t size, const struct ggml_tensor * tensor) {
206
0
    size = aligned_offset(NULL, size, alloc->alignment);
207
208
0
    AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
209
210
0
    int best_fit_chunk = -1;
211
0
    int best_fit_block = -1;
212
0
    size_t max_avail = 0;
213
214
    // find the best fitting free block besides the last block, within any chunk
215
0
    for (int c = 0; c < alloc->n_chunks; ++c) {
216
0
        struct tallocr_chunk * chunk = alloc->chunks[c];
217
0
        size_t best_fit_size = SIZE_MAX;
218
0
        for (int i = 0; i < chunk->n_free_blocks - 1; i++) {
219
0
            struct free_block * block = &chunk->free_blocks[i];
220
0
            max_avail = MAX(max_avail, block->size);
221
0
            if (block->size >= size && block->size <= best_fit_size) {
222
0
                best_fit_chunk = c;
223
0
                best_fit_block = i;
224
0
                best_fit_size = block->size;
225
0
            }
226
0
        }
227
0
    }
228
229
0
    if (best_fit_block == -1) {
230
        // no suitable block found, try the last block (this may grow a chunks size)
231
0
        int64_t best_reuse = INT64_MIN;
232
0
        for (int c = 0; c < alloc->n_chunks; ++c) {
233
0
            struct tallocr_chunk * chunk = alloc->chunks[c];
234
0
            if (chunk->n_free_blocks > 0) {
235
0
                struct free_block * block = &chunk->free_blocks[chunk->n_free_blocks - 1];
236
0
                max_avail = MAX(max_avail, block->size);
237
0
                int64_t reuse_factor = chunk->max_size - block->offset - size;
238
                // reuse_factor < 0 : amount of extra memory that needs to be allocated
239
                // reuse_factor = 0 : allocated free space exactly matches tensor size
240
                // reuse_factor > 0 : superfluous memory that will remain unused
241
0
                bool better_reuse = best_reuse < 0 && reuse_factor > best_reuse;
242
0
                bool better_fit = reuse_factor >= 0 && reuse_factor < best_reuse;
243
0
                if (block->size >= size && (better_reuse || better_fit)) {
244
0
                    best_fit_chunk = c;
245
0
                    best_fit_block = chunk->n_free_blocks - 1;
246
0
                    best_reuse = reuse_factor;
247
0
                }
248
0
            }
249
0
        }
250
0
    }
251
252
0
    if (best_fit_block == -1) {
253
        // none of the existing chunks have enough space left
254
0
        best_fit_chunk = ggml_dyn_tallocr_new_chunk(alloc, size);
255
0
        best_fit_block = 0;
256
0
    }
257
0
    if (best_fit_chunk == -1) {
258
        // since the last chunk always has virtually endless memory, this should never happen
259
0
        GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n",
260
0
            __func__, size, max_avail);
261
0
        GGML_ABORT("graph allocation: failed to reserve memory");
262
0
    }
263
264
0
    struct tallocr_chunk * chunk = alloc->chunks[best_fit_chunk];
265
0
    struct free_block    * block = &chunk->free_blocks[best_fit_block];
266
0
    struct buffer_address  addr  = {.chunk = best_fit_chunk, .offset = block->offset };
267
0
    block->offset += size;
268
0
    block->size -= size;
269
0
    if (block->size == 0) {
270
        // remove block if empty
271
0
        ggml_dyn_tallocr_remove_block(chunk, best_fit_block);
272
0
    }
273
274
0
    AT_PRINTF("block %d, offset %zu, chunk %d\n", best_fit_block, addr.offset, addr.chunk);
275
276
#ifdef GGML_ALLOCATOR_DEBUG
277
    add_allocated_tensor(alloc, addr, tensor);
278
    size_t cur_max = addr.offset + size;
279
    if (cur_max > chunk->max_size) {
280
        // sort allocated_tensors by chunk/offset
281
        for (int i = 0; i < 1024; i++) {
282
            for (int j = i + 1; j < 1024; j++) {
283
                if (ggml_buffer_address_less(alloc->allocated_tensors[j].addr, alloc->allocated_tensors[i].addr)) {
284
                    const struct ggml_tensor * tmp_tensor = alloc->allocated_tensors[i].tensor;
285
                    struct buffer_address tmp_addr = alloc->allocated_tensors[i].addr;
286
                    alloc->allocated_tensors[i].tensor = alloc->allocated_tensors[j].tensor;
287
                    alloc->allocated_tensors[i].addr = alloc->allocated_tensors[j].addr;
288
                    alloc->allocated_tensors[j].tensor = tmp_tensor;
289
                    alloc->allocated_tensors[j].addr = tmp_addr;
290
                }
291
            }
292
        }
293
        GGML_LOG_DEBUG("max_size[%d] = %.2f MB: tensors: ", addr.chunk, cur_max / 1024.0 / 1024.0);
294
        for (int i = 0; i < 1024; i++) {
295
            if (alloc->allocated_tensors[i].tensor) {
296
                GGML_LOG_DEBUG("%s [%d: %zx-%zx] (%.2f MB) ", alloc->allocated_tensors[i].tensor->name,
297
                    alloc->allocated_tensors[i].addr.chunk,
298
                    alloc->allocated_tensors[i].addr.offset,
299
                    alloc->allocated_tensors[i].addr.offset + ggml_nbytes(alloc->allocated_tensors[i].tensor),
300
                    ggml_nbytes(alloc->allocated_tensors[i].tensor) / 1024.0 / 1024.0);
301
            }
302
        }
303
        GGML_LOG_DEBUG("\n");
304
    }
305
#endif
306
307
0
    chunk->max_size = MAX(chunk->max_size, addr.offset + size);
308
309
0
    return addr;
310
311
0
    GGML_UNUSED(tensor);
312
0
}
313
314
// this is a very naive implementation, but for our case the number of free blocks should be very small
315
0
static void ggml_dyn_tallocr_free_bytes(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, size_t size) {
316
0
    size = aligned_offset(NULL, size, alloc->alignment);
317
318
0
    struct tallocr_chunk * chunk = alloc->chunks[addr.chunk];
319
320
    // see if we can merge with an existing block
321
0
    for (int i = 0; i < chunk->n_free_blocks; i++) {
322
0
        struct free_block * block = &chunk->free_blocks[i];
323
        // check if ptr is at the end of the block
324
0
        if (block->offset + block->size == addr.offset) {
325
0
            block->size += size;
326
            // check if we can merge with the next block
327
0
            if (i < chunk->n_free_blocks - 1) {
328
0
                struct free_block * next = &chunk->free_blocks[i+1];
329
0
                if (block->offset + block->size == next->offset) {
330
0
                    block->size += next->size;
331
0
                    ggml_dyn_tallocr_remove_block(chunk, i+1);
332
0
                }
333
0
            }
334
0
            return;
335
0
        }
336
        // check if ptr is at the beginning of the block
337
0
        if (addr.offset + size == block->offset) {
338
0
            block->offset = addr.offset;
339
0
            block->size += size;
340
            // check if we can merge with the previous block
341
0
            if (i > 0) {
342
0
                struct free_block * prev = &chunk->free_blocks[i-1];
343
0
                if (prev->offset + prev->size == block->offset) {
344
0
                    prev->size += block->size;
345
0
                    ggml_dyn_tallocr_remove_block(chunk, i);
346
0
                }
347
0
            }
348
0
            return;
349
0
        }
350
0
    }
351
    // otherwise, add a new block
352
0
    ggml_dyn_tallocr_insert_block(chunk, addr.offset, size);
353
0
}
354
355
0
static void ggml_dyn_tallocr_reset(struct ggml_dyn_tallocr * alloc) {
356
0
    for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; i++) {
357
0
        free(alloc->chunks[i]);
358
0
        alloc->chunks[i] = NULL;
359
0
    }
360
0
    alloc->n_chunks = 0;
361
362
#ifdef GGML_ALLOCATOR_DEBUG
363
    for (int i = 0; i < 1024; i++) {
364
        alloc->allocated_tensors[i].tensor = NULL;
365
    }
366
#endif
367
0
}
368
369
0
static struct ggml_dyn_tallocr * ggml_dyn_tallocr_new(size_t alignment, size_t max_buffer_size) {
370
0
    struct ggml_dyn_tallocr * alloc = (struct ggml_dyn_tallocr *)malloc(sizeof(struct ggml_dyn_tallocr));
371
372
0
    *alloc = (struct ggml_dyn_tallocr) {
373
0
        /*.alignment      = */ alignment,
374
0
        /*.max_chunk_size = */ MIN(max_buffer_size, SIZE_MAX/2), // clamp to avoid overflows
375
0
        /*.chunks         = */ {NULL},
376
0
        /*.n_chunks       = */ 0,
377
#ifdef GGML_ALLOCATOR_DEBUG
378
        /*.allocated_tensors = */ {{0}},
379
#endif
380
0
    };
381
382
0
    ggml_dyn_tallocr_reset(alloc);
383
384
0
    return alloc;
385
0
}
386
387
0
static void ggml_dyn_tallocr_free(struct ggml_dyn_tallocr * alloc) {
388
0
    for (int i = 0; i < alloc->n_chunks; ++i) {
389
0
        free(alloc->chunks[i]);
390
0
    }
391
0
    free(alloc);
392
0
}
393
394
0
static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc, int chunk) {
395
0
    return chunk < alloc->n_chunks ? alloc->chunks[chunk]->max_size : 0;
396
0
}
397
398
399
// virtual buffer with contiguous memory range, split into multiple backend buffers (chunks)
400
401
struct vbuffer {
402
    ggml_backend_buffer_t chunks[GGML_VBUFFER_MAX_CHUNKS];
403
};
404
405
0
static void ggml_vbuffer_free(struct vbuffer * buf) {
406
0
    if (buf == NULL) {
407
0
        return;
408
0
    }
409
0
    for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; ++i) {
410
0
        ggml_backend_buffer_free(buf->chunks[i]);
411
0
    }
412
0
    free(buf);
413
0
}
414
415
0
static size_t ggml_vbuffer_chunk_size(struct vbuffer * buf, int chunk) {
416
0
    return buf->chunks[chunk] ? ggml_backend_buffer_get_size(buf->chunks[chunk]) : 0;
417
0
}
418
419
0
static size_t ggml_vbuffer_size(struct vbuffer * buf) {
420
0
    size_t size = 0;
421
0
    for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) {
422
0
        size += ggml_backend_buffer_get_size(buf->chunks[i]);
423
0
    }
424
0
    return size;
425
0
}
426
427
0
static struct vbuffer * ggml_vbuffer_alloc(ggml_backend_buffer_type_t buft, const struct ggml_dyn_tallocr * talloc, enum ggml_backend_buffer_usage usage) {
428
0
    struct vbuffer * buf = (struct vbuffer *)calloc(1, sizeof(struct vbuffer));
429
0
    if (buf == NULL) {
430
0
        return NULL;
431
0
    }
432
433
0
    for (int n = 0; n < talloc->n_chunks; n++) {
434
0
        size_t chunk_size = talloc->chunks[n]->max_size;
435
0
        buf->chunks[n] = ggml_backend_buft_alloc_buffer(buft, chunk_size);
436
0
        if (buf->chunks[n] == NULL) {
437
0
            ggml_vbuffer_free(buf);
438
0
            return NULL;
439
0
        }
440
0
        ggml_backend_buffer_set_usage(buf->chunks[n], usage);
441
0
    }
442
0
    return buf;
443
0
}
444
445
0
static void ggml_vbuffer_tensor_alloc(struct vbuffer * buf, struct ggml_tensor * tensor, struct buffer_address buf_addr) {
446
0
    void * base = ggml_backend_buffer_get_base(buf->chunks[buf_addr.chunk]);
447
0
    void * addr = (char *)base + buf_addr.offset;
448
0
    ggml_backend_tensor_alloc(buf->chunks[buf_addr.chunk], tensor, addr);
449
0
}
450
451
0
static void ggml_vbuffer_reset(struct vbuffer * buf) {
452
0
    for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) {
453
0
        ggml_backend_buffer_reset(buf->chunks[i]);
454
0
    }
455
0
}
456
457
458
/////////////////////////////////////
459
460
// graph allocator
461
462
struct hash_node {
463
    int n_children;
464
    int n_views;
465
    int buffer_id;
466
    struct buffer_address addr;
467
    bool allocated;
468
};
469
470
struct tensor_alloc {
471
    int buffer_id;
472
    struct buffer_address addr;
473
    size_t size_max; // 0 = pre-allocated, unused, or view
474
};
475
476
struct leaf_alloc {
477
    struct tensor_alloc leaf;
478
};
479
480
struct node_alloc {
481
    struct tensor_alloc dst;
482
    struct tensor_alloc src[GGML_MAX_SRC];
483
};
484
485
struct ggml_gallocr {
486
    ggml_backend_buffer_type_t * bufts; // [n_buffers]
487
    struct vbuffer ** buffers; // [n_buffers]
488
    struct ggml_dyn_tallocr ** buf_tallocs; // [n_buffers]
489
    int n_buffers;
490
491
    struct ggml_hash_set hash_set;
492
    struct hash_node * hash_values; // [hash_set.size]
493
494
    struct node_alloc * node_allocs; // [n_nodes]
495
    int n_nodes;
496
497
    struct leaf_alloc * leaf_allocs; // [n_leafs]
498
    int n_leafs;
499
};
500
501
0
ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) {
502
0
    ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr));
503
0
    GGML_ASSERT(galloc != NULL);
504
505
0
    galloc->bufts = calloc(n_bufs, sizeof(ggml_backend_buffer_type_t));
506
0
    GGML_ASSERT(galloc->bufts != NULL);
507
508
0
    galloc->buffers = calloc(n_bufs, sizeof(struct vbuffer *));
509
0
    GGML_ASSERT(galloc->buffers != NULL);
510
511
0
    galloc->buf_tallocs = calloc(n_bufs, sizeof(struct ggml_dyn_tallocr *));
512
0
    GGML_ASSERT(galloc->buf_tallocs != NULL);
513
514
0
    for (int i = 0; i < n_bufs; i++) {
515
0
        galloc->bufts[i] = bufts[i];
516
0
        galloc->buffers[i] = NULL;
517
518
        // check if the same buffer type is used multiple times and reuse the same allocator
519
0
        for (int j = 0; j < i; j++) {
520
0
            if (bufts[i] == bufts[j]) {
521
0
                galloc->buf_tallocs[i] = galloc->buf_tallocs[j];
522
0
                break;
523
0
            }
524
0
        }
525
526
0
        if (galloc->buf_tallocs[i] == NULL) {
527
0
            size_t alignment = ggml_backend_buft_get_alignment(bufts[i]);
528
0
            size_t max_size = ggml_backend_buft_get_max_size(bufts[i]);
529
0
            galloc->buf_tallocs[i] = ggml_dyn_tallocr_new(alignment, max_size);
530
0
        }
531
0
    }
532
0
    galloc->n_buffers = n_bufs;
533
534
0
    return galloc;
535
0
}
536
537
0
ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft) {
538
0
    return ggml_gallocr_new_n(&buft, 1);
539
0
}
540
541
0
void ggml_gallocr_free(ggml_gallocr_t galloc) {
542
0
    if (galloc == NULL) {
543
0
        return;
544
0
    }
545
546
0
    for (int i = 0; i < galloc->n_buffers; i++) {
547
0
        if (galloc->buffers != NULL) {
548
            // skip if already freed
549
0
            bool freed = false;
550
0
            for (int j = 0; j < i; j++) {
551
0
                if (galloc->buffers[j] == galloc->buffers[i]) {
552
0
                    freed = true;
553
0
                    break;
554
0
                }
555
0
            }
556
0
            if (!freed) {
557
0
                ggml_vbuffer_free(galloc->buffers[i]);
558
0
            }
559
0
        }
560
0
        if (galloc->buf_tallocs != NULL) {
561
            // skip if already freed
562
0
            bool freed = false;
563
0
            for (int j = 0; j < i; j++) {
564
0
                if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
565
0
                    freed = true;
566
0
                    break;
567
0
                }
568
0
            }
569
0
            if (!freed) {
570
0
                ggml_dyn_tallocr_free(galloc->buf_tallocs[i]);
571
0
            }
572
0
        }
573
0
    }
574
575
0
    ggml_hash_set_free(&galloc->hash_set);
576
0
    free(galloc->hash_values);
577
0
    free(galloc->bufts);
578
0
    free(galloc->buffers);
579
0
    free(galloc->buf_tallocs);
580
0
    free(galloc->node_allocs);
581
0
    free(galloc->leaf_allocs);
582
0
    free(galloc);
583
0
}
584
585
typedef struct ggml_gallocr * ggml_gallocr_t;
586
587
0
static struct hash_node * ggml_gallocr_hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) {
588
0
    size_t i = ggml_hash_find_or_insert(&galloc->hash_set, t);
589
0
    return &galloc->hash_values[i];
590
0
}
591
592
0
static bool ggml_gallocr_is_own(ggml_gallocr_t galloc, struct ggml_tensor * t) {
593
0
    return ggml_gallocr_hash_get(galloc, t)->allocated;
594
0
}
595
596
0
static bool ggml_gallocr_is_allocated(ggml_gallocr_t galloc, struct ggml_tensor * t) {
597
0
    return t->data != NULL || ggml_gallocr_hash_get(galloc, t)->allocated;
598
0
}
599
600
// free the extra space at the end if the new tensor is smaller
601
0
static void ggml_gallocr_free_extra_space(ggml_gallocr_t galloc, struct ggml_tensor * node, struct ggml_tensor * parent) {
602
0
    struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
603
0
    struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
604
605
0
    size_t parent_size = ggml_backend_buft_get_alloc_size(galloc->bufts[p_hn->buffer_id], parent);
606
0
    size_t node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
607
608
0
    GGML_ASSERT(parent_size >= node_size);
609
610
    // note: we want after the freeing the chunks to continue to be aligned
611
0
    struct ggml_dyn_tallocr * p_alloc = galloc->buf_tallocs[p_hn->buffer_id];
612
0
    parent_size = aligned_offset(NULL, parent_size, p_alloc->alignment);
613
0
    node_size = aligned_offset(NULL, node_size, p_alloc->alignment);
614
615
0
    if (parent_size > node_size) {
616
0
        struct buffer_address p_addr = p_hn->addr;
617
0
        p_addr.offset += node_size;
618
0
        size_t extra_size = parent_size - node_size;
619
0
        AT_PRINTF("freeing extra %zu bytes from parent %s for %s\n", extra_size, parent->name, node->name);
620
0
        ggml_dyn_tallocr_free_bytes(p_alloc, p_addr, extra_size);
621
0
    }
622
0
}
623
624
0
static void ggml_gallocr_allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node, int buffer_id) {
625
0
    GGML_ASSERT(buffer_id >= 0);
626
0
    struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
627
628
0
    if (!ggml_gallocr_is_allocated(galloc, node) && !ggml_is_view(node)) {
629
0
        hn->allocated = true;
630
0
        assert(hn->addr.offset == 0);
631
632
        // try to reuse a parent's buffer (inplace)
633
0
        if (ggml_op_can_inplace(node->op)) {
634
0
            for (int i = 0; i < GGML_MAX_SRC; i++) {
635
0
                struct ggml_tensor * parent = node->src[i];
636
0
                if (parent == NULL) {
637
0
                    continue;
638
0
                }
639
640
                // if the node's data is external, then we cannot re-use it
641
0
                if (!ggml_gallocr_is_own(galloc, parent)) {
642
0
                    AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
643
0
                    continue;
644
0
                }
645
646
                // outputs cannot be reused
647
0
                if (parent->flags & GGML_TENSOR_FLAG_OUTPUT || (parent->view_src != NULL && parent->view_src->flags & GGML_TENSOR_FLAG_OUTPUT)) {
648
0
                    AT_PRINTF("not reusing parent %s for %s as it is an output\n", parent->name, node->name);
649
0
                    continue;
650
0
                }
651
652
0
                if (!ggml_are_same_layout(node, parent)) {
653
0
                    AT_PRINTF("not reusing parent %s for %s as layouts are different\n", parent->name, node->name);
654
0
                    continue;
655
0
                }
656
657
0
                struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
658
0
                if (p_hn->n_children == 1 && p_hn->n_views == 0) {
659
0
                    if (ggml_is_view(parent)) {
660
0
                        struct ggml_tensor * view_src = parent->view_src;
661
0
                        struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
662
0
                        if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
663
0
                            AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
664
0
                            assert(view_src_hn->addr.chunk == p_hn->addr.chunk && view_src_hn->addr.offset == p_hn->addr.offset);
665
0
                            hn->buffer_id = p_hn->buffer_id;
666
0
                            hn->addr = p_hn->addr;
667
0
                            p_hn->allocated = false; // avoid freeing the parent
668
0
                            view_src_hn->allocated = false;
669
0
                            ggml_gallocr_free_extra_space(galloc, node, view_src);
670
0
                            return;
671
0
                        }
672
0
                    } else {
673
0
                        AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
674
0
                        hn->buffer_id = p_hn->buffer_id;
675
0
                        hn->addr = p_hn->addr;
676
0
                        p_hn->allocated = false; // avoid freeing the parent
677
0
                        ggml_gallocr_free_extra_space(galloc, node, parent);
678
0
                        return;
679
0
                    }
680
0
                }
681
0
            }
682
0
        }
683
        // allocate tensor from the buffer
684
0
        struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
685
0
        ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
686
0
        size_t size = ggml_backend_buft_get_alloc_size(buft, node);
687
0
        hn->buffer_id = buffer_id;
688
0
        hn->addr = ggml_dyn_tallocr_alloc(alloc, size, node);
689
0
    }
690
0
}
691
692
0
static void ggml_gallocr_free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
693
    // graph outputs are never freed
694
0
    if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {
695
0
        AT_PRINTF("not freeing output %s\n", node->name);
696
0
        return;
697
0
    }
698
699
0
    struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
700
0
    int buffer_id = hn->buffer_id;
701
0
    struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
702
0
    ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
703
0
    size_t size = ggml_backend_buft_get_alloc_size(buft, node);
704
705
0
    AT_PRINTF("%s: freeing %s at {chunk=%d, offset=%zu} (%zu bytes) - n_free_blocks = %d\n",
706
0
        __func__, node->name, hn->addr.chunk, hn->addr.offset, size, alloc->chunks[hn->addr.chunk]->n_free_blocks);
707
#ifdef GGML_ALLOCATOR_DEBUG
708
    remove_allocated_tensor(alloc, hn->addr, node);
709
#endif
710
711
0
    ggml_dyn_tallocr_free_bytes(alloc, hn->addr, size);
712
0
    hn->allocated = false;
713
0
}
714
715
0
static int get_node_buffer_id(const int * node_buffer_ids, int i) {
716
0
    return node_buffer_ids ? node_buffer_ids[i] : 0;
717
0
}
718
719
0
static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
720
    // clear hash tables
721
0
    ggml_hash_set_reset(&galloc->hash_set);
722
0
    memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size);
723
724
    // allocate leafs
725
    // these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes
726
0
    for (int i = 0; i < graph->n_leafs; i++) {
727
0
        struct ggml_tensor * leaf = graph->leafs[i];
728
0
        ggml_gallocr_allocate_node(galloc, leaf, get_node_buffer_id(leaf_buffer_ids, i));
729
0
    }
730
731
    // count number of children and views
732
    // allocate other graph inputs and leafs first to avoid overwriting them
733
0
    for (int i = 0; i < graph->n_nodes; i++) {
734
0
        struct ggml_tensor * node = graph->nodes[i];
735
736
        // TODO: better way to add external dependencies
737
        // GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to
738
        // control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node
739
        // itself is never used and should not be considered a dependency
740
0
        if (ggml_is_view(node) && node->op != GGML_OP_NONE) {
741
0
            struct ggml_tensor * view_src = node->view_src;
742
0
            ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
743
0
        }
744
745
0
        if (node->flags & GGML_TENSOR_FLAG_INPUT) {
746
0
            ggml_gallocr_allocate_node(galloc, graph->nodes[i], get_node_buffer_id(node_buffer_ids, i));
747
0
        }
748
749
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
750
0
            struct ggml_tensor * src = node->src[j];
751
0
            if (src == NULL) {
752
0
                continue;
753
0
            }
754
755
0
            ggml_gallocr_hash_get(galloc, src)->n_children += 1;
756
757
            // allocate explicit inputs
758
0
            if (src->flags & GGML_TENSOR_FLAG_INPUT) {
759
0
                ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
760
0
            }
761
0
        }
762
0
    }
763
764
    // allocate tensors
765
0
    for (int i = 0; i < graph->n_nodes; i++) {
766
0
        struct ggml_tensor * node = graph->nodes[i];
767
0
        int buffer_id = get_node_buffer_id(node_buffer_ids, i);
768
769
        // allocate parents (only leafs need to be allocated at this point)
770
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
771
0
            struct ggml_tensor * parent = node->src[j];
772
0
            if (parent == NULL) {
773
0
                continue;
774
0
            }
775
0
            ggml_gallocr_allocate_node(galloc, parent, buffer_id);
776
0
        }
777
778
        // allocate node
779
0
        ggml_gallocr_allocate_node(galloc, node, buffer_id);
780
781
0
        AT_PRINTF("exec: %s (%s) <= ", ggml_op_desc(node), node->name);
782
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
783
0
            struct ggml_tensor * parent = node->src[j];
784
0
            if (parent == NULL) {
785
0
                continue;
786
0
            }
787
0
            AT_PRINTF("%s", parent->name);
788
0
            if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
789
0
                AT_PRINTF(", ");
790
0
            }
791
0
        }
792
0
        AT_PRINTF("\n");
793
794
        // update parents
795
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
796
0
            struct ggml_tensor * parent = node->src[j];
797
0
            if (parent == NULL) {
798
0
                continue;
799
0
            }
800
0
            struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
801
0
            p_hn->n_children -= 1;
802
803
0
            AT_PRINTF("parent %s: %d children, %d views, allocated: %d\n",
804
0
                parent->name, p_hn->n_children, p_hn->n_views, p_hn->allocated);
805
806
0
            if (p_hn->n_children == 0 && p_hn->n_views == 0) {
807
0
                if (ggml_is_view(parent)) {
808
0
                    struct ggml_tensor * view_src = parent->view_src;
809
0
                    struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
810
0
                    view_src_hn->n_views -= 1;
811
0
                    AT_PRINTF("view_src %s: %d children, %d views\n",
812
0
                        view_src->name, view_src_hn->n_children, view_src_hn->n_views);
813
0
                    if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src_hn->allocated) {
814
0
                        ggml_gallocr_free_node(galloc, view_src);
815
0
                    }
816
0
                }
817
0
                else if (p_hn->allocated) {
818
0
                    ggml_gallocr_free_node(galloc, parent);
819
0
                }
820
0
            }
821
0
            AT_PRINTF("\n");
822
0
        }
823
0
    }
824
0
}
825
826
0
bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
827
0
    size_t min_hash_size = graph->n_nodes + graph->n_leafs;
828
    // add 25% margin to avoid hash collisions
829
0
    min_hash_size += min_hash_size / 4;
830
831
    // initialize hash table
832
0
    if (galloc->hash_set.size < min_hash_size) {
833
0
        ggml_hash_set_free(&galloc->hash_set);
834
0
        galloc->hash_set = ggml_hash_set_new(min_hash_size);
835
0
        GGML_ASSERT(galloc->hash_set.keys != NULL);
836
837
0
        free(galloc->hash_values);
838
0
        galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size);
839
0
        GGML_ASSERT(galloc->hash_values != NULL);
840
0
    }
841
842
    // reset allocators
843
0
    for (int i = 0; i < galloc->n_buffers; i++) {
844
0
        ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]);
845
0
    }
846
847
    // allocate in hash table
848
0
    ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids);
849
850
    // set the node_allocs from the hash table
851
0
    if (galloc->n_nodes < graph->n_nodes) {
852
0
        free(galloc->node_allocs);
853
0
        galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc));
854
0
        GGML_ASSERT(galloc->node_allocs != NULL);
855
0
    }
856
0
    galloc->n_nodes = graph->n_nodes;
857
0
    for (int i = 0; i < graph->n_nodes; i++) {
858
0
        struct ggml_tensor * node = graph->nodes[i];
859
0
        struct node_alloc * node_alloc = &galloc->node_allocs[i];
860
0
        if (node->view_src || node->data) {
861
0
            node_alloc->dst.buffer_id = -1;
862
0
            node_alloc->dst.addr = GGML_BUFFER_ADDRESS_INVALID;
863
0
            node_alloc->dst.size_max = 0;
864
0
        } else {
865
0
            struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
866
0
            node_alloc->dst.buffer_id = hn->buffer_id;
867
0
            node_alloc->dst.addr = hn->addr;
868
0
            node_alloc->dst.size_max  = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
869
0
        }
870
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
871
0
            struct ggml_tensor * src = node->src[j];
872
0
            if (!src || src->view_src || src->data) {
873
0
                node_alloc->src[j].buffer_id = -1;
874
0
                node_alloc->src[j].addr = GGML_BUFFER_ADDRESS_INVALID;
875
0
                node_alloc->src[j].size_max = 0;
876
0
            } else {
877
0
                struct hash_node * hn = ggml_gallocr_hash_get(galloc, src);
878
0
                node_alloc->src[j].buffer_id = hn->buffer_id;
879
0
                node_alloc->src[j].addr = hn->addr;
880
0
                node_alloc->src[j].size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src);
881
0
            }
882
0
        }
883
0
    }
884
0
    if (galloc->n_leafs < graph->n_leafs) {
885
0
        free(galloc->leaf_allocs);
886
0
        galloc->leaf_allocs = calloc(graph->n_leafs, sizeof(galloc->leaf_allocs[0]));
887
0
        GGML_ASSERT(galloc->leaf_allocs != NULL);
888
0
    }
889
0
    galloc->n_leafs = graph->n_leafs;
890
0
    for (int i = 0; i < graph->n_leafs; i++) {
891
0
        struct ggml_tensor * leaf = graph->leafs[i];
892
0
        struct hash_node * hn = ggml_gallocr_hash_get(galloc, leaf);
893
0
        if (leaf->view_src || leaf->data) {
894
0
            galloc->leaf_allocs[i].leaf.buffer_id = -1;
895
0
            galloc->leaf_allocs[i].leaf.addr = GGML_BUFFER_ADDRESS_INVALID;
896
0
            galloc->leaf_allocs[i].leaf.size_max = 0;
897
0
        } else {
898
0
            galloc->leaf_allocs[i].leaf.buffer_id = hn->buffer_id;
899
0
            galloc->leaf_allocs[i].leaf.addr = hn->addr;
900
0
            galloc->leaf_allocs[i].leaf.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], leaf);
901
0
        }
902
0
    }
903
904
    // reallocate buffers if needed
905
0
    for (int i = 0; i < galloc->n_buffers; i++) {
906
        // if the buffer type is used multiple times, we reuse the same buffer
907
0
        for (int j = 0; j < i; j++) {
908
0
            if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
909
0
                galloc->buffers[i] = galloc->buffers[j];
910
0
                break;
911
0
            }
912
0
        }
913
914
        // even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views
915
0
        bool realloc = galloc->buffers[i] == NULL;
916
0
        size_t new_size = 0;
917
0
        for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) {
918
0
            size_t cur_chunk_size = galloc->buffers[i] ? ggml_vbuffer_chunk_size(galloc->buffers[i], c) : 0;
919
0
            size_t new_chunk_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i], c);
920
0
            new_size += new_chunk_size;
921
0
            if (new_chunk_size > cur_chunk_size) {
922
0
                realloc = true;
923
0
            }
924
0
        }
925
0
        if (realloc) {
926
#ifndef NDEBUG
927
            {
928
                size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
929
                if (cur_size > 0) {
930
                    GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n",
931
                        __func__, ggml_backend_buft_name(galloc->bufts[i]),
932
                        cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
933
                }
934
            }
935
#endif
936
0
            ggml_vbuffer_free(galloc->buffers[i]);
937
0
            galloc->buffers[i] = ggml_vbuffer_alloc(galloc->bufts[i], galloc->buf_tallocs[i], GGML_BACKEND_BUFFER_USAGE_COMPUTE);
938
0
            if (galloc->buffers[i] == NULL) {
939
0
                GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), new_size);
940
0
                return false;
941
0
            }
942
0
        }
943
0
    }
944
945
0
    return true;
946
0
}
947
948
0
bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph *graph) {
949
0
    return ggml_gallocr_reserve_n(galloc, graph, NULL, NULL);
950
0
}
951
952
0
static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) {
953
0
    int buffer_id = tensor_alloc->buffer_id;
954
0
    assert(tensor->data || tensor->view_src || ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max);
955
956
0
    if (tensor->view_src != NULL) {
957
0
        if (tensor->buffer == NULL) {
958
0
            assert(tensor_alloc->addr.offset == SIZE_MAX);
959
0
            if (tensor->view_src->buffer == NULL) {
960
                // this tensor was allocated without ggml-backend
961
0
                return;
962
0
            }
963
0
            ggml_backend_view_init(tensor);
964
0
        }
965
0
    } else {
966
0
        if (tensor->data == NULL) {
967
0
            assert(tensor_alloc->addr.offset != SIZE_MAX);
968
0
            assert(ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max);
969
0
            ggml_vbuffer_tensor_alloc(galloc->buffers[buffer_id], tensor, tensor_alloc->addr);
970
0
        } else {
971
0
            if (tensor->buffer == NULL) {
972
                // this tensor was allocated without ggml-backend
973
0
                return;
974
0
            }
975
0
        }
976
0
    }
977
0
}
978
979
0
static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, struct tensor_alloc * talloc) {
980
0
    size_t node_size = 0;
981
0
    if (!node->data && !node->view_src) {
982
        // If we previously had data but don't now then reallocate
983
0
        if (talloc->buffer_id < 0) {
984
0
            return false;
985
0
        }
986
0
        node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[talloc->buffer_id], node);
987
0
    }
988
0
    return talloc->size_max >= node_size;
989
0
}
990
991
0
static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
992
0
    if (galloc->n_nodes != graph->n_nodes) {
993
#ifndef NDEBUG
994
        GGML_LOG_DEBUG("%s: graph has different number of nodes\n", __func__);
995
#endif
996
0
        return true;
997
0
    }
998
999
0
    if (galloc->n_leafs != graph->n_leafs) {
1000
#ifndef NDEBUG
1001
        GGML_LOG_DEBUG("%s: graph has different number of leafs\n", __func__);
1002
#endif
1003
0
        return true;
1004
0
    }
1005
1006
0
    for (int i = 0; i < graph->n_nodes; i++) {
1007
0
        struct ggml_tensor * node = graph->nodes[i];
1008
0
        struct node_alloc * node_alloc = &galloc->node_allocs[i];
1009
1010
0
        if (!ggml_gallocr_node_needs_realloc(galloc, node, &node_alloc->dst)) {
1011
#ifndef NDEBUG
1012
            GGML_LOG_DEBUG("%s: node %s is not valid\n", __func__, node->name);
1013
#endif
1014
0
            return true;
1015
0
        }
1016
1017
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
1018
0
            struct ggml_tensor * src = node->src[j];
1019
0
            if (src == NULL) {
1020
0
                continue;
1021
0
            }
1022
0
            if (!ggml_gallocr_node_needs_realloc(galloc, src, &node_alloc->src[j])) {
1023
#ifndef NDEBUG
1024
                GGML_LOG_DEBUG("%s: src %d (%s) of node %s is not valid\n", __func__, j, src->name, node->name);
1025
#endif
1026
0
                return true;
1027
0
            }
1028
0
        }
1029
0
    }
1030
1031
0
    return false;
1032
0
}
1033
1034
0
bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
1035
0
    if (ggml_gallocr_needs_realloc(galloc, graph)) {
1036
0
        if (galloc->n_buffers == 1) {
1037
#ifndef NDEBUG
1038
            GGML_LOG_DEBUG("%s: reallocating buffers automatically\n", __func__);
1039
#endif
1040
0
            if (!ggml_gallocr_reserve(galloc, graph)) {
1041
0
                return false;
1042
0
            }
1043
0
        } else {
1044
#ifndef NDEBUG
1045
            GGML_LOG_DEBUG("%s: cannot reallocate multi buffer graph automatically, call reserve\n", __func__);
1046
#endif
1047
0
            return false;
1048
0
        }
1049
0
    }
1050
1051
    // reset buffers
1052
0
    for (int i = 0; i < galloc->n_buffers; i++) {
1053
0
        if (galloc->buffers[i] != NULL) {
1054
0
            ggml_vbuffer_reset(galloc->buffers[i]);
1055
0
        }
1056
0
    }
1057
1058
    // allocate the graph tensors from the previous assignments
1059
    // leafs
1060
0
    for (int i = 0; i < graph->n_leafs; i++) {
1061
0
        struct ggml_tensor * leaf = graph->leafs[i];
1062
0
        struct leaf_alloc * leaf_alloc = &galloc->leaf_allocs[i];
1063
0
        ggml_gallocr_init_tensor(galloc, leaf, &leaf_alloc->leaf);
1064
0
    }
1065
    // nodes
1066
0
    for (int i = 0; i < graph->n_nodes; i++) {
1067
0
        struct ggml_tensor * node = graph->nodes[i];
1068
0
        struct node_alloc * node_alloc = &galloc->node_allocs[i];
1069
0
        for (int j = 0; j < GGML_MAX_SRC; j++) {
1070
0
            struct ggml_tensor * src = node->src[j];
1071
0
            if (src == NULL) {
1072
0
                continue;
1073
0
            }
1074
0
            ggml_gallocr_init_tensor(galloc, src, &node_alloc->src[j]);
1075
0
        }
1076
0
        ggml_gallocr_init_tensor(galloc, node, &node_alloc->dst);
1077
0
    }
1078
1079
0
    return true;
1080
0
}
1081
1082
0
size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id) {
1083
0
    GGML_ASSERT(buffer_id >= 0 && buffer_id < galloc->n_buffers);
1084
1085
0
    if (galloc->buffers[buffer_id] == NULL) {
1086
0
        return 0;
1087
0
    }
1088
1089
0
    for (int i = 0; i < buffer_id; i++) {
1090
0
        if (galloc->buffers[i] == galloc->buffers[buffer_id]) {
1091
            // this buffer is the same as a previous one due to the same buffer type being used multiple times
1092
            // only return the buffer size the first time it appears to avoid double counting
1093
0
            return 0;
1094
0
        }
1095
0
    }
1096
1097
0
    return ggml_vbuffer_size(galloc->buffers[buffer_id]);
1098
0
}
1099
1100
// utils
1101
1102
0
static void free_buffers(ggml_backend_buffer_t ** buffers, const size_t * n_buffers) {
1103
0
    for (size_t i = 0; i < *n_buffers; i++) {
1104
0
        ggml_backend_buffer_free((*buffers)[i]);
1105
0
    }
1106
0
    free(*buffers);
1107
0
}
1108
1109
static bool alloc_tensor_range(struct ggml_context * ctx,
1110
        struct ggml_tensor * first, struct ggml_tensor * last,
1111
        ggml_backend_buffer_type_t buft, size_t size,
1112
0
        ggml_backend_buffer_t ** buffers, size_t * n_buffers) {
1113
1114
0
    ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, size);
1115
0
    if (buffer == NULL) {
1116
0
        GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(buft), size);
1117
0
        free_buffers(buffers, n_buffers);
1118
0
        return false;
1119
0
    }
1120
1121
0
    *buffers = realloc(*buffers, sizeof(ggml_backend_buffer_t) * (*n_buffers + 1));
1122
0
    (*buffers)[(*n_buffers)++] = buffer;
1123
1124
0
    struct ggml_tallocr tallocr = ggml_tallocr_new(buffer);
1125
1126
0
    for (struct ggml_tensor * t = first; t != last; t = ggml_get_next_tensor(ctx, t)) {
1127
0
        enum ggml_status status = GGML_STATUS_SUCCESS;
1128
0
        if (t->data == NULL) {
1129
0
            if (t->view_src == NULL) {
1130
0
                status = ggml_tallocr_alloc(&tallocr, t);
1131
0
            } else if (t->buffer == NULL) {
1132
0
                status = ggml_backend_view_init(t);
1133
0
            }
1134
0
        } else {
1135
0
            if (t->view_src != NULL && t->buffer == NULL) {
1136
                // view of a pre-allocated tensor
1137
0
                status = ggml_backend_view_init(t);
1138
0
            }
1139
0
        }
1140
0
        if (status != GGML_STATUS_SUCCESS) {
1141
0
            GGML_LOG_ERROR("%s: failed to initialize tensor %s\n", __func__, t->name);
1142
0
            free_buffers(buffers, n_buffers);
1143
0
            return false;
1144
0
        }
1145
0
    }
1146
1147
0
    return true;
1148
0
}
1149
1150
0
ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
1151
0
    GGML_ASSERT(ggml_get_no_alloc(ctx) == true);
1152
1153
0
    size_t alignment = ggml_backend_buft_get_alignment(buft);
1154
0
    size_t max_size = ggml_backend_buft_get_max_size(buft);
1155
1156
0
    ggml_backend_buffer_t * buffers = NULL;
1157
0
    size_t n_buffers = 0;
1158
1159
0
    size_t cur_buf_size = 0;
1160
0
    struct ggml_tensor * first = ggml_get_first_tensor(ctx);
1161
0
    for (struct ggml_tensor * t = first; t != NULL; t = ggml_get_next_tensor(ctx, t)) {
1162
0
        size_t this_size = 0;
1163
0
        if (t->data == NULL && t->view_src == NULL) {
1164
0
            this_size = GGML_PAD(ggml_backend_buft_get_alloc_size(buft, t), alignment);
1165
0
        }
1166
1167
0
        if (cur_buf_size > 0 && (cur_buf_size + this_size) > max_size) {
1168
            // allocate tensors in the current buffer
1169
0
            if (!alloc_tensor_range(ctx, first, t, buft, cur_buf_size, &buffers, &n_buffers)) {
1170
0
                return NULL;
1171
0
            }
1172
0
            first = t;
1173
0
            cur_buf_size = this_size;
1174
0
        } else {
1175
0
            cur_buf_size += this_size;
1176
0
        }
1177
0
    }
1178
1179
    // allocate remaining tensors
1180
0
    if (cur_buf_size > 0) {
1181
0
        if (!alloc_tensor_range(ctx, first, NULL, buft, cur_buf_size, &buffers, &n_buffers)) {
1182
0
            return NULL;
1183
0
        }
1184
0
    }
1185
1186
0
    if (n_buffers == 0) {
1187
#ifndef NDEBUG
1188
        GGML_LOG_DEBUG("%s: all tensors in the context are already allocated\n", __func__);
1189
#endif
1190
0
        return NULL;
1191
0
    }
1192
1193
0
    ggml_backend_buffer_t buffer;
1194
0
    if (n_buffers == 1) {
1195
0
        buffer = buffers[0];
1196
0
    } else {
1197
0
        buffer = ggml_backend_multi_buffer_alloc_buffer(buffers, n_buffers);
1198
0
    }
1199
0
    free(buffers);
1200
0
    return buffer;
1201
0
}
1202
1203
0
ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) {
1204
0
    return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend));
1205
0
}