From 7a6340a0b808a1391f5b1dcdc6b61846c86625ae Mon Sep 17 00:00:00 2001 From: ahmetkca Date: Fri, 24 Mar 2023 03:29:48 -0400 Subject: [PATCH] Fix(mem_buffer): Allocate memory buffer - Check if mem_buffer is NULL - Print error message to stderr - Print debug message - End critical section - Return NULL if memory buffer allocation fails --- ggml.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ggml.c b/ggml.c index 535c7b7d281..aec75dd6457 100644 --- a/ggml.c +++ b/ggml.c @@ -2447,6 +2447,16 @@ struct ggml_context * ggml_init(struct ggml_init_params params) { /*.scratch_save =*/ { 0, 0, NULL, }, }; + if (ctx->mem_buffer == NULL) { + fprintf(stderr, "%s: failed to allocate memory buffer of size %zu\n", __func__, params.mem_size); + + GGML_PRINT_DEBUG("%s: failed to allocate memory buffer\n", __func__); + + ggml_critical_section_end(); + + return NULL; + } + ggml_assert_aligned(ctx->mem_buffer); GGML_PRINT_DEBUG("%s: context initialized\n", __func__);