@@ -91,8 +91,7 @@ void ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tenso
91
91
if (talloc -> offset + size > ggml_backend_buffer_get_size (talloc -> buffer )) {
92
92
fprintf (stderr , "%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n" ,
93
93
__func__ , tensor -> name , size , ggml_backend_buffer_get_size (talloc -> buffer ) - talloc -> offset );
94
- GGML_ASSERT (!"not enough space in the buffer" );
95
- return ;
94
+ GGML_ABORT ("not enough space in the buffer" );
96
95
}
97
96
98
97
void * addr = (char * )ggml_backend_buffer_get_base (talloc -> buffer ) + talloc -> offset ;
@@ -133,7 +132,7 @@ static void add_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offset,
133
132
return ;
134
133
}
135
134
}
136
- GGML_ASSERT (! "out of allocated_tensors" );
135
+ GGML_ABORT ( "out of allocated_tensors" );
137
136
}
138
137
static void remove_allocated_tensor (struct ggml_dyn_tallocr * alloc , size_t offset , const struct ggml_tensor * tensor ) {
139
138
for (int i = 0 ; i < 1024 ; i ++ ) {
@@ -142,8 +141,7 @@ static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offs
142
141
return ;
143
142
}
144
143
}
145
- fprintf (stderr , "tried to free tensor %s not found\n" , tensor -> name );
146
- GGML_ASSERT (!"tensor not found" );
144
+ GGML_ABORT ("tried to free tensor %s not found\n" , tensor -> name );
147
145
}
148
146
#endif
149
147
@@ -176,8 +174,7 @@ static size_t ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t siz
176
174
// this should never happen
177
175
fprintf (stderr , "%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n" ,
178
176
__func__ , size , max_avail );
179
- GGML_ASSERT (!"not enough space in the buffer" );
180
- GGML_UNREACHABLE ();
177
+ GGML_ABORT ("not enough space in the buffer" );
181
178
}
182
179
}
183
180
@@ -443,7 +440,7 @@ void ggml_gallocr_free(ggml_gallocr_t galloc) {
443
440
}
444
441
}
445
442
446
- free ( galloc -> hash_set . keys );
443
+ ggml_hash_set_free ( & galloc -> hash_set );
447
444
free (galloc -> hash_values );
448
445
free (galloc -> bufts );
449
446
free (galloc -> buffers );
@@ -456,7 +453,7 @@ void ggml_gallocr_free(ggml_gallocr_t galloc) {
456
453
typedef struct ggml_gallocr * ggml_gallocr_t ;
457
454
458
455
static struct hash_node * ggml_gallocr_hash_get (ggml_gallocr_t galloc , struct ggml_tensor * t ) {
459
- size_t i = ggml_hash_find_or_insert (galloc -> hash_set , t );
456
+ size_t i = ggml_hash_find_or_insert (& galloc -> hash_set , t );
460
457
return & galloc -> hash_values [i ];
461
458
}
462
459
@@ -565,8 +562,8 @@ static int get_node_buffer_id(const int * node_buffer_ids, int i) {
565
562
566
563
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 ) {
567
564
// clear hash tables
568
- memset ( galloc -> hash_set . keys , 0 , galloc -> hash_set . size * sizeof ( struct ggml_tensor * ) );
569
- memset (galloc -> hash_values , 0 , galloc -> hash_set . size * sizeof (struct hash_node ));
565
+ ggml_hash_set_reset ( & galloc -> hash_set );
566
+ memset (galloc -> hash_values , 0 , sizeof (struct hash_node ) * galloc -> hash_set . size );
570
567
571
568
// allocate leafs
572
569
// these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes
@@ -671,21 +668,19 @@ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
671
668
}
672
669
673
670
bool ggml_gallocr_reserve_n (ggml_gallocr_t galloc , struct ggml_cgraph * graph , const int * node_buffer_ids , const int * leaf_buffer_ids ) {
674
- size_t hash_size = graph -> visited_hash_table .size ;
671
+ size_t min_hash_size = graph -> n_nodes + graph -> n_leafs ;
672
+ // add 25% margin to avoid hash collisions
673
+ min_hash_size += min_hash_size / 4 ;
675
674
676
675
// initialize hash table
677
- if (galloc -> hash_set .size < hash_size ) {
678
- free (galloc -> hash_set .keys );
679
- free (galloc -> hash_values );
680
- galloc -> hash_set .size = hash_size ;
681
- galloc -> hash_set .keys = calloc (hash_size , sizeof (struct ggml_tensor * ));
682
- galloc -> hash_values = calloc (hash_size , sizeof (struct hash_node ));
676
+ if (galloc -> hash_set .size < min_hash_size ) {
677
+ ggml_hash_set_free (& galloc -> hash_set );
678
+ galloc -> hash_set = ggml_hash_set_new (min_hash_size );
683
679
GGML_ASSERT (galloc -> hash_set .keys != NULL );
680
+
681
+ free (galloc -> hash_values );
682
+ galloc -> hash_values = malloc (sizeof (struct hash_node ) * galloc -> hash_set .size );
684
683
GGML_ASSERT (galloc -> hash_values != NULL );
685
- } else {
686
- // reset hash table
687
- memset (galloc -> hash_set .keys , 0 , sizeof (struct ggml_tensor * ) * galloc -> hash_set .size );
688
- memset (galloc -> hash_values , 0 , sizeof (struct hash_node ) * galloc -> hash_set .size );
689
684
}
690
685
691
686
// reset allocators
@@ -817,8 +812,7 @@ static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor *
817
812
}
818
813
819
814
static bool ggml_gallocr_node_needs_realloc (ggml_gallocr_t galloc , struct ggml_tensor * node , struct tensor_alloc * talloc ) {
820
- ggml_backend_buffer_type_t buft = talloc -> buffer_id != -1 ? galloc -> bufts [talloc -> buffer_id ] : NULL ;
821
- size_t node_size = (node -> data || node -> view_src ) ? 0 : ggml_backend_buft_get_alloc_size (buft , node );
815
+ size_t node_size = (node -> data || node -> view_src ) ? 0 : ggml_backend_buft_get_alloc_size (galloc -> bufts [talloc -> buffer_id ], node );
822
816
return talloc -> size_max >= node_size ;
823
817
}
824
818
0 commit comments