From 8ccf979e502ff7bf24654b2dcb791a64d801e9e6 Mon Sep 17 00:00:00 2001 From: slaren Date: Sat, 27 Jul 2024 02:23:44 +0200 Subject: [PATCH] GGML_ABORT use format string ggml-ci --- ggml/include/ggml.h | 7 ++++--- ggml/src/ggml-alloc.c | 3 +-- ggml/src/ggml-backend.c | 3 +-- ggml/src/ggml-blas.cpp | 3 +-- ggml/src/ggml-cuda/getrows.cu | 3 +-- ggml/src/ggml.c | 13 +++++++++++-- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index 263f590eb8d3e2..464d765da44c42 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -272,8 +272,8 @@ #define GGML_NORETURN _Noreturn #endif -#define GGML_ABORT(x) ggml_abort(__FILE__, __LINE__, x) -#define GGML_ASSERT(x) if (!(x)) GGML_ABORT(#x) +#define GGML_ABORT(...) ggml_abort(__FILE__, __LINE__, __VA_ARGS__) +#define GGML_ASSERT(x) if (!(x)) GGML_ABORT("GGML_ASSERT(%s) failed", #x) // used to copy the number of elements and stride in bytes of tensors into local variables. // main purpose is to reduce code duplication and improve readability. @@ -323,7 +323,8 @@ extern "C" { #endif - GGML_NORETURN GGML_API void ggml_abort(const char * file, int line, const char * expr); + GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4) + GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...); enum ggml_status { GGML_STATUS_ALLOC_FAILED = -2, diff --git a/ggml/src/ggml-alloc.c b/ggml/src/ggml-alloc.c index f11af0a6f99849..e485326abc45d2 100644 --- a/ggml/src/ggml-alloc.c +++ b/ggml/src/ggml-alloc.c @@ -141,8 +141,7 @@ static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, size_t offs return; } } - fprintf(stderr, "tried to free tensor %s not found\n", tensor->name); - GGML_ABORT("tensor not found"); + GGML_ABORT("tried to free tensor %s not found\n", tensor->name); } #endif diff --git a/ggml/src/ggml-backend.c b/ggml/src/ggml-backend.c index 8e9ba5b7373014..954ab20725acc9 100644 --- a/ggml/src/ggml-backend.c +++ b/ggml/src/ggml-backend.c @@ -1279,8 +1279,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg sched->ctx = ggml_init(params); if (sched->ctx == NULL) { - fprintf(stderr, "%s: failed to initialize context\n", __func__); - GGML_ABORT("fatal error"); + GGML_ABORT("%s: failed to initialize context\n", __func__); } // pass 1: assign backends to ops with pre-allocated inputs diff --git a/ggml/src/ggml-blas.cpp b/ggml/src/ggml-blas.cpp index f620a9f93c3ed0..71373173598c77 100644 --- a/ggml/src/ggml-blas.cpp +++ b/ggml/src/ggml-blas.cpp @@ -275,8 +275,7 @@ GGML_CALL static enum ggml_status ggml_backend_blas_graph_compute(ggml_backend_t break; default: - fprintf(stderr, "%s: unsupported op %s\n", __func__, ggml_op_desc(node)); - GGML_ABORT("fatal error"); + GGML_ABORT("%s: unsupported op %s\n", __func__, ggml_op_desc(node)); } } diff --git a/ggml/src/ggml-cuda/getrows.cu b/ggml/src/ggml-cuda/getrows.cu index 5b0598ecfef764..4c3703238cb6eb 100644 --- a/ggml/src/ggml-cuda/getrows.cu +++ b/ggml/src/ggml-cuda/getrows.cu @@ -171,8 +171,7 @@ void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { break; default: // TODO: k-quants - fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type)); - GGML_ABORT("fatal error"); + GGML_ABORT("%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type)); break; } } diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index 8c346a502ee453..c196fd5bf06275 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -191,9 +191,18 @@ static void ggml_print_backtrace(void) { } #endif -void ggml_abort(const char * file, int line, const char * expr) { +void ggml_abort(const char * file, int line, const char * fmt, ...) { fflush(stdout); - fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", file, line, expr); + + fprintf(stderr, "%s:%d: ", file, line); + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + fprintf(stderr, "\n"); + ggml_print_backtrace(); abort(); }