Skip to content

Commit 638d330

Browse files
authored
ggml : fix graph reallocation with multiple chunks (#16396)
reallocation is needed if a single chunk grows in size, even if total allocation size stays the same or is lower
1 parent 84c8e30 commit 638d330

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

ggml/src/ggml-alloc.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,8 @@ static void ggml_dyn_tallocr_free(struct ggml_dyn_tallocr * alloc) {
392392
free(alloc);
393393
}
394394

395-
static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc) {
396-
size_t max_size = 0;
397-
for (int i = 0; i < alloc->n_chunks; i++) {
398-
max_size += alloc->chunks[i]->max_size;
399-
}
400-
return max_size;
395+
static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc, int chunk) {
396+
return chunk < alloc->n_chunks ? alloc->chunks[chunk]->max_size : 0;
401397
}
402398

403399

@@ -417,10 +413,8 @@ static void ggml_vbuffer_free(struct vbuffer * buf) {
417413
free(buf);
418414
}
419415

420-
static int ggml_vbuffer_n_chunks(struct vbuffer * buf) {
421-
int n = 0;
422-
while (n < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[n]) n++;
423-
return n;
416+
static size_t ggml_vbuffer_chunk_size(struct vbuffer * buf, int chunk) {
417+
return buf->chunks[chunk] ? ggml_backend_buffer_get_size(buf->chunks[chunk]) : 0;
424418
}
425419

426420
static size_t ggml_vbuffer_size(struct vbuffer * buf) {
@@ -885,12 +879,20 @@ bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, c
885879
}
886880
}
887881

888-
size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
889-
size_t new_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i]);
890-
891882
// even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views
892-
if (new_size > cur_size || galloc->buffers[i] == NULL) {
883+
bool realloc = galloc->buffers[i] == NULL;
884+
size_t new_size = 0;
885+
for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) {
886+
size_t cur_chunk_size = galloc->buffers[i] ? ggml_vbuffer_chunk_size(galloc->buffers[i], c) : 0;
887+
size_t new_chunk_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i], c);
888+
new_size += new_chunk_size;
889+
if (new_chunk_size > cur_chunk_size) {
890+
realloc = true;
891+
}
892+
}
893+
if (realloc) {
893894
#ifndef NDEBUG
895+
size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
894896
GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
895897
#endif
896898

tests/test-alloc.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,41 @@ static void test_buffer_size_zero() {
548548
GGML_ASSERT(backend_b.context->allocated_total() == 0);
549549
}
550550

551+
// Test re-using gallocr for a different graph. The new graph has the same
552+
// total size, but one of the chunks is larger, so reallocation is required.
553+
static void test_reallocation() {
554+
dummy_backend backend = dummy_backend_init(32, /*align*/ 4);
555+
ggml_gallocr_ptr galloc;
556+
{
557+
auto [ctx, graph, ctx_ptr] = make_context();
558+
ggml_tensor * x[4];
559+
x[0] = make_input_with_size(ctx, 24);
560+
x[1] = make_input_with_size(ctx, 16);
561+
x[2] = ggml_view_1d(ctx, x[0], 4, 0);
562+
x[3] = ggml_add(ctx, x[2], x[1]);
563+
assign_names(ctx);
564+
565+
galloc = allocate_graph(graph, x[3], &backend.buffer_type);
566+
check_all_allocated(graph);
567+
GGML_ASSERT(backend.context->allocated_total() == 40);
568+
}
569+
{
570+
auto [ctx, graph, ctx_ptr] = make_context();
571+
ggml_tensor * x[3];
572+
x[0] = make_input_with_size(ctx, 20);
573+
x[1] = make_input_with_size(ctx, 20);
574+
x[2] = ggml_add(ctx, x[0], x[1]);
575+
assign_names(ctx);
576+
ggml_set_output(x[2]);
577+
ggml_build_forward_expand(graph, x[2]);
578+
579+
bool result = ggml_gallocr_alloc_graph(galloc.get(), graph);
580+
GGML_ASSERT(result);
581+
check_all_allocated(graph);
582+
GGML_ASSERT(backend.context->allocated_total() == 40);
583+
}
584+
}
585+
551586
static void run(const char * name, void (*f)()) {
552587
printf("%s ", name);
553588
fflush(stdout);
@@ -568,5 +603,6 @@ int main() {
568603
run("test_prefer_already_allocated_memory", test_prefer_already_allocated_memory);
569604
run("test_multiple_buffer_types", test_multiple_buffer_types);
570605
run("test_buffer_size_zero", test_buffer_size_zero);
606+
run("test_reallocation", test_reallocation);
571607
return 0;
572608
}

0 commit comments

Comments
 (0)