Skip to content

Commit 794ab95

Browse files
bandotiarthw
authored andcommitted
ggml: unify backend logging mechanism (ggml-org#9709)
* Add scaffolding for ggml logging macros * Metal backend now uses GGML logging * Cuda backend now uses GGML logging * Cann backend now uses GGML logging * Add enum tag to parameters * Use C memory allocation funcs * Fix compile error * Use GGML_LOG instead of GGML_PRINT * Rename llama_state to llama_logger_state * Prevent null format string * Fix whitespace * Remove log callbacks from ggml backends * Remove cuda log statement
1 parent 89f1bb3 commit 794ab95

13 files changed

+197
-340
lines changed

ggml/include/ggml-backend.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ extern "C" {
164164
GGML_API size_t ggml_backend_reg_dev_count(ggml_backend_reg_t reg);
165165
GGML_API ggml_backend_dev_t ggml_backend_reg_dev_get(ggml_backend_reg_t reg, size_t index);
166166
GGML_API void * ggml_backend_reg_get_proc_address(ggml_backend_reg_t reg, const char * name);
167-
GGML_API void ggml_backend_reg_set_log_callback(ggml_backend_reg_t reg, ggml_log_callback log_callback, void * user_data);
167+
168168

169169
// Functions that may be obtained using ggml_backend_reg_get_proc_address
170170
typedef ggml_backend_buffer_type_t (*ggml_backend_split_buffer_type_t)(const float *);
@@ -184,9 +184,6 @@ extern "C" {
184184
GGML_API ggml_backend_dev_t ggml_backend_dev_by_name(const char * name);
185185
GGML_API ggml_backend_dev_t ggml_backend_dev_by_type(enum ggml_backend_dev_type type);
186186

187-
// Set the log callback for all registered backends
188-
GGML_API void ggml_backend_set_log_callback(ggml_log_callback log_callback, void * user_data);
189-
190187
// Direct backend (stream) initialization
191188
// = ggml_backend_dev_init(ggml_backend_dev_by_name(name), params)
192189
GGML_API ggml_backend_t ggml_backend_init_by_name(const char * name, const char * params);

ggml/include/ggml-cann.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,6 @@ GGML_API void ggml_backend_cann_get_device_memory(int32_t device,
116116
size_t* free,
117117
size_t* total);
118118

119-
/**
120-
* @brief Set the logging callback for GGML.
121-
*
122-
* This function sets the logging callback and user data for logging.
123-
*
124-
* @param log_callback The logging callback to set.
125-
* @param user_data User data to pass to the logging callback.
126-
*/
127-
GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
128-
void* user_data);
129-
130119
#ifdef __cplusplus
131120
}
132121
#endif

ggml/include/ggml-cuda.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ GGML_API void ggml_backend_cuda_get_device_memory(int device, size_t * free, siz
4040
GGML_API bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size);
4141
GGML_API void ggml_backend_cuda_unregister_host_buffer(void * buffer);
4242

43-
GGML_API void ggml_backend_cuda_log_set_callback(ggml_log_callback log_callback, void * user_data);
44-
4543
GGML_API ggml_backend_reg_t ggml_backend_cuda_reg(void);
4644

4745
#ifdef __cplusplus

ggml/include/ggml-metal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ extern "C" {
3939
// user-code should use only these functions
4040
//
4141

42-
GGML_API void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
43-
4442
GGML_API ggml_backend_t ggml_backend_metal_init(void);
4543

4644
GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);

ggml/include/ggml.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,10 @@ extern "C" {
21672167
typedef void (*ggml_opt_callback)(void * data, int accum_step, float * sched, bool * cancel);
21682168
typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
21692169

2170+
// Set callback for all future logging events.
2171+
// If this is not called, or NULL is supplied, everything is output on stderr.
2172+
GGML_API void ggml_log_set(ggml_log_callback log_callback, void * user_data);
2173+
21702174
// optimization parameters
21712175
//
21722176
// see ggml.c (ggml_opt_default_params) for default values

ggml/src/ggml-backend-impl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ extern "C" {
215215
// (optional) get a pointer to a function in the backend
216216
// backends can add custom functions that are not part of the standard ggml-backend interface
217217
void * (*get_proc_address)(ggml_backend_reg_t reg, const char * name);
218-
219-
// (optional) set the log callback for the backend
220-
void (*set_log_callback)(ggml_backend_reg_t reg, ggml_log_callback log_callback, void * user_data);
221218
};
222219

223220
struct ggml_backend_reg {

ggml/src/ggml-backend.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,6 @@ void * ggml_backend_reg_get_proc_address(ggml_backend_reg_t reg, const char * na
505505
return reg->iface.get_proc_address(reg, name);
506506
}
507507

508-
void ggml_backend_reg_set_log_callback(ggml_backend_reg_t reg, ggml_log_callback log_callback, void * user_data) {
509-
if (reg->iface.set_log_callback) {
510-
reg->iface.set_log_callback(reg, log_callback, user_data);
511-
}
512-
}
513-
514508
// Backend registry
515509

516510
#ifdef GGML_USE_CUDA
@@ -614,13 +608,6 @@ ggml_backend_dev_t ggml_backend_dev_by_type(enum ggml_backend_dev_type type) {
614608
return NULL;
615609
}
616610

617-
void ggml_backend_set_log_callback(ggml_log_callback log_callback, void * user_data) {
618-
for (size_t i = 0; i < ggml_backend_reg_count(); i++) {
619-
ggml_backend_reg_t reg = ggml_backend_reg_get(i);
620-
ggml_backend_reg_set_log_callback(reg, log_callback, user_data);
621-
}
622-
}
623-
624611
// Convenience functions
625612
ggml_backend_t ggml_backend_init_by_name(const char * name, const char * params) {
626613
ggml_backend_dev_t dev = ggml_backend_dev_by_name(name);
@@ -1161,7 +1148,6 @@ static const struct ggml_backend_reg_i ggml_backend_cpu_reg_i = {
11611148
/* .get_device_count = */ ggml_backend_cpu_reg_get_device_count,
11621149
/* .get_device = */ ggml_backend_cpu_reg_get_device,
11631150
/* .get_proc_address = */ NULL,
1164-
/* .set_log_callback = */ NULL,
11651151
};
11661152

11671153
ggml_backend_reg_t ggml_backend_cpu_reg(void) {

ggml/src/ggml-cann.cpp

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -39,69 +39,6 @@
3939

4040
#include "ggml-common.h"
4141

42-
/**
43-
* @brief Default logging callback for GGML.
44-
*
45-
* This function is the default logging callback that logs messages to stderr.
46-
*
47-
* @param level The log level.
48-
* @param msg The log message.
49-
* @param user_data User data passed to the callback.
50-
*/
51-
static void ggml_cann_default_log_callback(enum ggml_log_level level,
52-
const char* msg, void* user_data) {
53-
GGML_UNUSED(level);
54-
GGML_UNUSED(user_data);
55-
fprintf(stderr, "%s", msg);
56-
}
57-
58-
ggml_log_callback ggml_cann_log_callback = ggml_cann_default_log_callback;
59-
void* ggml_cann_log_user_data = NULL;
60-
61-
GGML_API void ggml_backend_cann_log_set_callback(ggml_log_callback log_callback,
62-
void* user_data) {
63-
ggml_cann_log_callback = log_callback;
64-
ggml_cann_log_user_data = user_data;
65-
}
66-
67-
#define GGML_CANN_LOG_INFO(...) ggml_cann_log(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
68-
#define GGML_CANN_LOG_WARN(...) ggml_cann_log(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
69-
#define GGML_CANN_LOG_ERROR(...) \
70-
ggml_cann_log(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
71-
72-
GGML_ATTRIBUTE_FORMAT(2, 3)
73-
74-
/**
75-
* @brief Log a message using the current logging callback.
76-
*
77-
* This function formats a log message and passes it to the current logging
78-
* callback.
79-
*
80-
* @param level The log level.
81-
* @param format The format string for the log message.
82-
* @param ... The arguments for the format string.
83-
*/
84-
static void ggml_cann_log(enum ggml_log_level level, const char* format, ...) {
85-
if (ggml_cann_log_callback != NULL) {
86-
va_list args;
87-
va_start(args, format);
88-
char buffer[128];
89-
int len = vsnprintf(buffer, 128, format, args);
90-
if (len < 128) {
91-
ggml_cann_log_callback(level, buffer, ggml_cann_log_user_data);
92-
} else {
93-
// vsnprintf adds a null terminator
94-
std::vector<char> buffer2(len + 1);
95-
va_end(args);
96-
va_start(args, format);
97-
vsnprintf(&buffer2[0], buffer2.size(), format, args);
98-
ggml_cann_log_callback(level, buffer2.data(),
99-
ggml_cann_log_user_data);
100-
}
101-
va_end(args);
102-
}
103-
}
104-
10542
/**
10643
* @brief Handles CANN errors by printing an error message and aborting.
10744
*
@@ -116,10 +53,10 @@ static void ggml_cann_log(enum ggml_log_level level, const char* format, ...) {
11653
int32_t id = -1;
11754
aclrtGetDevice(&id);
11855

119-
GGML_CANN_LOG_ERROR("CANN error: %s\n", msg);
120-
GGML_CANN_LOG_ERROR(" current device: %d, in function %s at %s:%d\n", id, func,
56+
GGML_LOG_ERROR("CANN error: %s\n", msg);
57+
GGML_LOG_ERROR(" current device: %d, in function %s at %s:%d\n", id, func,
12158
file, line);
122-
GGML_CANN_LOG_ERROR(" %s\n", stmt);
59+
GGML_LOG_ERROR(" %s\n", stmt);
12360
// abort with GGML_ASSERT to get a stack trace
12461
GGML_ABORT("CANN error");
12562
}
@@ -165,7 +102,7 @@ static ggml_cann_device_info ggml_cann_init() {
165102
aclError err = aclrtGetDeviceCount((uint32_t*)&info.device_count);
166103

167104
if (err != ACL_SUCCESS) {
168-
GGML_CANN_LOG_ERROR("%s: failed to initialize CANN: %s\n",
105+
GGML_LOG_ERROR("%s: failed to initialize CANN: %s\n",
169106
__func__, aclGetRecentErrMsg());
170107
return info;
171108
}
@@ -315,7 +252,7 @@ struct ggml_cann_pool_leg : public ggml_cann_pool {
315252
*actual_size = look_ahead_size;
316253
pool_size += look_ahead_size;
317254
#ifdef DEBUG_CANN_MALLOC
318-
GGML_CANN_LOG_INFO(
255+
GGML_LOG_INFO(
319256
"%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, "
320257
"requested %u MB\n",
321258
__func__, device, nnz, (uint32_t)(max_size / 1024 / 1024),
@@ -470,7 +407,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
470407
// add to the pool
471408
pool_size += reserve_size;
472409

473-
// GGML_CANN_LOG_INFO("cann pool[%d]: size increased to %llu MB (
410+
// GGML_LOG_INFO("cann pool[%d]: size increased to %llu MB (
474411
// reserved %llu MB)\n",
475412
// device, (unsigned long long) (pool_size/1024/1024),
476413
// (unsigned long long) (reserve_size/1024/1024));
@@ -483,7 +420,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
483420
pool_used += size;
484421

485422
#ifdef DEBUG_CANN_MALLOC
486-
GGML_CANN_LOG_INFO("cann pool[%d]: allocated %llu bytes at %llx\n", device,
423+
GGML_LOG_INFO("cann pool[%d]: allocated %llu bytes at %llx\n", device,
487424
(unsigned long long)size, (unsigned long long)ptr);
488425
#endif
489426
return ptr;
@@ -497,7 +434,7 @@ struct ggml_cann_pool_vmm : public ggml_cann_pool {
497434
*/
498435
void free(void* ptr, size_t size) override {
499436
#ifdef DEBUG_CANN_MALLOC
500-
GGML_CANN_LOG_INFO("cann pool[%d]: freed %llu bytes at %llx\n", device,
437+
GGML_LOG_INFO("cann pool[%d]: freed %llu bytes at %llx\n", device,
501438
(unsigned long long)size, (unsigned long long)ptr);
502439
#endif
503440

@@ -1095,7 +1032,7 @@ ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
10951032
void* dev_ptr;
10961033
aclError err = aclrtMalloc(&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST);
10971034
if (err != ACL_SUCCESS) {
1098-
GGML_CANN_LOG_ERROR(
1035+
GGML_LOG_ERROR(
10991036
"%s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n",
11001037
__func__, size / 1024.0 / 1024.0, buft_ctx->device,
11011038
aclGetRecentErrMsg());
@@ -1280,7 +1217,7 @@ static void * ggml_cann_host_malloc(size_t size) {
12801217
aclError err = aclrtMallocHost((void **) &hostPtr, size);
12811218
if (err != ACL_SUCCESS) {
12821219

1283-
GGML_CANN_LOG_WARN("%s: failed to allocate %.2f MiB of pinned memory: %s\n", __func__,
1220+
GGML_LOG_WARN("%s: failed to allocate %.2f MiB of pinned memory: %s\n", __func__,
12841221
size / 1024.0 / 1024.0, aclGetRecentErrMsg());
12851222
return nullptr;
12861223
}
@@ -1733,7 +1670,7 @@ static enum ggml_status ggml_backend_cann_graph_compute(
17331670
bool ok = ggml_cann_compute_forward(*cann_ctx, node);
17341671

17351672
if (!ok) {
1736-
GGML_CANN_LOG_ERROR("%s: error: op not supported %s (%s)\n", __func__,
1673+
GGML_LOG_ERROR("%s: error: op not supported %s (%s)\n", __func__,
17371674
node->name, ggml_op_name(node->op));
17381675
}
17391676
GGML_ASSERT(ok);
@@ -2043,13 +1980,13 @@ static ggml_guid_t ggml_backend_cann_guid() {
20431980
ggml_backend_t ggml_backend_cann_init(int32_t device) {
20441981
aclInit(nullptr);
20451982
if (device < 0 || device >= ggml_backend_cann_get_device_count()) {
2046-
GGML_CANN_LOG_ERROR("%s: error: invalid device %d\n", __func__, device);
1983+
GGML_LOG_ERROR("%s: error: invalid device %d\n", __func__, device);
20471984
return nullptr;
20481985
}
20491986

20501987
ggml_backend_cann_context* ctx = new ggml_backend_cann_context(device);
20511988
if (ctx == nullptr) {
2052-
GGML_CANN_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
1989+
GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__);
20531990
return nullptr;
20541991
}
20551992
ggml_cann_set_device(ctx->device);

0 commit comments

Comments
 (0)