@@ -89,14 +89,18 @@ struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer) {
89
89
return talloc ;
90
90
}
91
91
92
- void ggml_tallocr_alloc (struct ggml_tallocr * talloc , struct ggml_tensor * tensor ) {
92
+ enum ggml_status ggml_tallocr_alloc (struct ggml_tallocr * talloc , struct ggml_tensor * tensor ) {
93
93
size_t size = ggml_backend_buffer_get_alloc_size (talloc -> buffer , tensor );
94
94
size = GGML_PAD (size , talloc -> alignment );
95
95
96
96
if (talloc -> offset + size > ggml_backend_buffer_get_size (talloc -> buffer )) {
97
- GGML_LOG_ERROR ("%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n" ,
97
+ GGML_LOG_ERROR ("%s: not enough space in the buffer to allocate tensor '%s' (needed %zu, available %zu)\n" ,
98
98
__func__ , tensor -> name , size , ggml_backend_buffer_get_size (talloc -> buffer ) - talloc -> offset );
99
+ #ifdef GGML_NO_ABORT_ON_OOM
100
+ return GGML_STATUS_ALLOC_FAILED ;
101
+ #else
99
102
GGML_ABORT ("not enough space in the buffer" );
103
+ #endif
100
104
}
101
105
102
106
void * addr = (char * )ggml_backend_buffer_get_base (talloc -> buffer ) + talloc -> offset ;
@@ -105,6 +109,7 @@ void ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tenso
105
109
assert (((uintptr_t )addr % talloc -> alignment ) == 0 );
106
110
107
111
ggml_backend_tensor_alloc (talloc -> buffer , tensor , addr );
112
+ return GGML_STATUS_SUCCESS ;
108
113
}
109
114
110
115
// dynamic tensor allocator
@@ -150,6 +155,7 @@ static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offs
150
155
}
151
156
#endif
152
157
158
+ // Check with reviewer: could that function returns a ggm_status (offset being an arg)
153
159
static size_t ggml_dyn_tallocr_alloc (struct ggml_dyn_tallocr * alloc , size_t size , const struct ggml_tensor * tensor ) {
154
160
size = aligned_offset (NULL , size , alloc -> alignment );
155
161
@@ -179,6 +185,7 @@ static size_t ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t siz
179
185
// this should never happen
180
186
GGML_LOG_ERROR ("%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n" ,
181
187
__func__ , size , max_avail );
188
+ // Note: no way to honor GGML_NO_ABORT_ON_OOM since that fn returns the offset, not a ggml_status
182
189
GGML_ABORT ("not enough space in the buffer" );
183
190
}
184
191
}
@@ -378,6 +385,7 @@ struct ggml_gallocr {
378
385
};
379
386
380
387
ggml_gallocr_t ggml_gallocr_new_n (ggml_backend_buffer_type_t * bufts , int n_bufs ) {
388
+ //GGML_LOG_TRACE("%s: nbufs=%d\n", __func__, n_bufs);
381
389
ggml_gallocr_t galloc = (ggml_gallocr_t )calloc (1 , sizeof (struct ggml_gallocr ));
382
390
GGML_ASSERT (galloc != NULL );
383
391
@@ -670,7 +678,10 @@ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
670
678
}
671
679
}
672
680
681
+ // Returns true on success, false otherwise
682
+ // Check with reviewers: any cons to return a ggml_status?
673
683
bool ggml_gallocr_reserve_n (ggml_gallocr_t galloc , struct ggml_cgraph * graph , const int * node_buffer_ids , const int * leaf_buffer_ids ) {
684
+ //GGML_LOG_TRACE("ggml_gallocr_reserve_n\n");
674
685
size_t min_hash_size = graph -> n_nodes + graph -> n_leafs ;
675
686
// add 25% margin to avoid hash collisions
676
687
min_hash_size += min_hash_size / 4 ;
@@ -865,6 +876,7 @@ static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph
865
876
return false;
866
877
}
867
878
879
+ // Check with reviewers: any cons to return a ggml_status here?
868
880
bool ggml_gallocr_alloc_graph (ggml_gallocr_t galloc , struct ggml_cgraph * graph ) {
869
881
if (ggml_gallocr_needs_realloc (galloc , graph )) {
870
882
if (galloc -> n_buffers == 1 ) {
@@ -954,7 +966,9 @@ static bool alloc_tensor_range(struct ggml_context * ctx,
954
966
for (struct ggml_tensor * t = first ; t != last ; t = ggml_get_next_tensor (ctx , t )) {
955
967
if (t -> data == NULL ) {
956
968
if (t -> view_src == NULL ) {
957
- ggml_tallocr_alloc (& tallocr , t );
969
+ enum ggml_status s = ggml_tallocr_alloc (& tallocr , t );
970
+ if (s != GGML_STATUS_SUCCESS )
971
+ GGML_LOG_WARN ("%s: failed to alloc tensor %s \n" , __func__ , t -> name );
958
972
} else if (t -> buffer == NULL ) {
959
973
ggml_backend_view_init (t );
960
974
}
0 commit comments