@@ -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+
551586static 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